[PATCH 1 of 3] py3: wrap tempfile.mkstemp() to use bytes path

Yuya Nishihara yuya at tcha.org
Sat May 26 00:47:12 EDT 2018


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1527304444 -32400
#      Sat May 26 12:14:04 2018 +0900
# Node ID 15be905498df86a8ff372e822a22262289f115da
# Parent  aa10675c5dd6769511e7b3ffb95279057ea4c2dd
py3: wrap tempfile.mkstemp() to use bytes path

This patch just flips the default to use a bytes path on Python 3.
ca1cf9b3cce7 is backed out as the bundlepath should be bytes now.

diff --git a/hgext/convert/subversion.py b/hgext/convert/subversion.py
--- a/hgext/convert/subversion.py
+++ b/hgext/convert/subversion.py
@@ -5,7 +5,6 @@ from __future__ import absolute_import
 
 import os
 import re
-import tempfile
 import xml.dom.minidom
 
 from mercurial.i18n import _
@@ -1225,7 +1224,7 @@ class svn_sink(converter_sink, commandli
         wdest = self.wjoin(dest)
         exists = os.path.lexists(wdest)
         if exists:
-            fd, tempname = tempfile.mkstemp(
+            fd, tempname = pycompat.mkstemp(
                 prefix='hg-copy-', dir=os.path.dirname(wdest))
             os.close(fd)
             os.unlink(tempname)
@@ -1313,7 +1312,7 @@ class svn_sink(converter_sink, commandli
             self.xargs(self.setexec, 'propset', 'svn:executable', '*')
             self.setexec = []
 
-        fd, messagefile = tempfile.mkstemp(prefix='hg-convert-')
+        fd, messagefile = pycompat.mkstemp(prefix='hg-convert-')
         fp = os.fdopen(fd, r'wb')
         fp.write(util.tonativeeol(commit.desc))
         fp.close()
diff --git a/hgext/gpg.py b/hgext/gpg.py
--- a/hgext/gpg.py
+++ b/hgext/gpg.py
@@ -9,7 +9,6 @@ from __future__ import absolute_import
 
 import binascii
 import os
-import tempfile
 
 from mercurial.i18n import _
 from mercurial import (
@@ -61,11 +60,11 @@ class gpg(object):
         sigfile = datafile = None
         try:
             # create temporary files
-            fd, sigfile = tempfile.mkstemp(prefix="hg-gpg-", suffix=".sig")
+            fd, sigfile = pycompat.mkstemp(prefix="hg-gpg-", suffix=".sig")
             fp = os.fdopen(fd, r'wb')
             fp.write(sig)
             fp.close()
-            fd, datafile = tempfile.mkstemp(prefix="hg-gpg-", suffix=".txt")
+            fd, datafile = pycompat.mkstemp(prefix="hg-gpg-", suffix=".txt")
             fp = os.fdopen(fd, r'wb')
             fp.write(data)
             fp.close()
diff --git a/hgext/infinitepush/__init__.py b/hgext/infinitepush/__init__.py
--- a/hgext/infinitepush/__init__.py
+++ b/hgext/infinitepush/__init__.py
@@ -94,7 +94,6 @@ import random
 import re
 import socket
 import subprocess
-import tempfile
 import time
 
 from mercurial.node import (
@@ -912,7 +911,7 @@ def storetobundlestore(orig, repo, op, u
 
     # storing the bundle in the bundlestore
     buf = util.chunkbuffer(bundler.getchunks())
-    fd, bundlefile = tempfile.mkstemp()
+    fd, bundlefile = pycompat.mkstemp()
     try:
         try:
             fp = os.fdopen(fd, r'wb')
@@ -998,7 +997,7 @@ def processparts(orig, repo, op, unbundl
     # If commits were sent, store them
     if cgparams:
         buf = util.chunkbuffer(bundler.getchunks())
-        fd, bundlefile = tempfile.mkstemp()
+        fd, bundlefile = pycompat.mkstemp()
         try:
             try:
                 fp = os.fdopen(fd, r'wb')
@@ -1023,8 +1022,7 @@ def storebundle(op, params, bundlefile):
 
     bundle = None
     try:  # guards bundle
-        bundlepath = "bundle:%s+%s" % (op.repo.root,
-                                       pycompat.fsencode(bundlefile))
+        bundlepath = "bundle:%s+%s" % (op.repo.root, bundlefile)
         bundle = hg.repository(op.repo.ui, bundlepath)
 
         bookmark = params.get('bookmark')
@@ -1111,7 +1109,7 @@ def bundle2scratchbranch(op, part):
     bundler.addpart(cgpart)
     buf = util.chunkbuffer(bundler.getchunks())
 
-    fd, bundlefile = tempfile.mkstemp()
+    fd, bundlefile = pycompat.mkstemp()
     try:
         try:
             fp = os.fdopen(fd, r'wb')
diff --git a/hgext/infinitepush/common.py b/hgext/infinitepush/common.py
--- a/hgext/infinitepush/common.py
+++ b/hgext/infinitepush/common.py
@@ -6,13 +6,13 @@
 from __future__ import absolute_import
 
 import os
-import tempfile
 
 from mercurial.node import hex
 
 from mercurial import (
     error,
     extensions,
+    pycompat,
 )
 
 def isremotebooksenabled(ui):
@@ -30,7 +30,7 @@ def downloadbundle(repo, unknownbinhead)
 
 def _makebundlefromraw(data):
     fp = None
-    fd, bundlefile = tempfile.mkstemp()
+    fd, bundlefile = pycompat.mkstemp()
     try:  # guards bundlefile
         try:  # guards fp
             fp = os.fdopen(fd, 'wb')
diff --git a/hgext/transplant.py b/hgext/transplant.py
--- a/hgext/transplant.py
+++ b/hgext/transplant.py
@@ -16,7 +16,7 @@ map from a changeset hash to its hash in
 from __future__ import absolute_import
 
 import os
-import tempfile
+
 from mercurial.i18n import _
 from mercurial import (
     bundlerepo,
@@ -215,7 +215,7 @@ class transplanter(object):
                 if skipmerge:
                     patchfile = None
                 else:
-                    fd, patchfile = tempfile.mkstemp(prefix='hg-transplant-')
+                    fd, patchfile = pycompat.mkstemp(prefix='hg-transplant-')
                     fp = os.fdopen(fd, r'wb')
                     gen = patch.diff(source, parent, node, opts=diffopts)
                     for chunk in gen:
@@ -263,7 +263,7 @@ class transplanter(object):
 
         self.ui.status(_('filtering %s\n') % patchfile)
         user, date, msg = (changelog[1], changelog[2], changelog[4])
-        fd, headerfile = tempfile.mkstemp(prefix='hg-transplant-')
+        fd, headerfile = pycompat.mkstemp(prefix='hg-transplant-')
         fp = os.fdopen(fd, r'wb')
         fp.write("# HG changeset patch\n")
         fp.write("# User %s\n" % user)
diff --git a/mercurial/changegroup.py b/mercurial/changegroup.py
--- a/mercurial/changegroup.py
+++ b/mercurial/changegroup.py
@@ -9,7 +9,6 @@ from __future__ import absolute_import
 
 import os
 import struct
-import tempfile
 import weakref
 
 from .i18n import _
@@ -80,7 +79,7 @@ def writechunks(ui, chunks, filename, vf
                 # small (4k is common on Linux).
                 fh = open(filename, "wb", 131072)
         else:
-            fd, filename = tempfile.mkstemp(prefix="hg-bundle-", suffix=".hg")
+            fd, filename = pycompat.mkstemp(prefix="hg-bundle-", suffix=".hg")
             fh = os.fdopen(fd, r"wb")
         cleanup = filename
         for c in chunks:
diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -10,7 +10,6 @@ from __future__ import absolute_import
 import errno
 import os
 import re
-import tempfile
 
 from .i18n import _
 from .node import (
@@ -329,7 +328,7 @@ def dorecord(ui, repo, commitfunc, cmdsu
         try:
             # backup continues
             for f in tobackup:
-                fd, tmpname = tempfile.mkstemp(prefix=f.replace('/', '_')+'.',
+                fd, tmpname = pycompat.mkstemp(prefix=f.replace('/', '_') + '.',
                                                dir=backupdir)
                 os.close(fd)
                 ui.debug('backup %r as %r\n' % (f, tmpname))
diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py
--- a/mercurial/debugcommands.py
+++ b/mercurial/debugcommands.py
@@ -1142,7 +1142,7 @@ def debuginstall(ui, **opts):
     opts = pycompat.byteskwargs(opts)
 
     def writetemp(contents):
-        (fd, name) = tempfile.mkstemp(prefix="hg-debuginstall-")
+        (fd, name) = pycompat.mkstemp(prefix="hg-debuginstall-")
         f = os.fdopen(fd, r"wb")
         f.write(contents)
         f.close()
diff --git a/mercurial/filemerge.py b/mercurial/filemerge.py
--- a/mercurial/filemerge.py
+++ b/mercurial/filemerge.py
@@ -724,7 +724,7 @@ def _maketempfiles(repo, fco, fca, local
                 name += ext
             f = open(name, r"wb")
         else:
-            fd, name = tempfile.mkstemp(prefix=pre + '.', suffix=ext)
+            fd, name = pycompat.mkstemp(prefix=pre + '.', suffix=ext)
             f = os.fdopen(fd, r"wb")
         return f, name
 
diff --git a/mercurial/httppeer.py b/mercurial/httppeer.py
--- a/mercurial/httppeer.py
+++ b/mercurial/httppeer.py
@@ -13,7 +13,6 @@ import io
 import os
 import socket
 import struct
-import tempfile
 import weakref
 
 from .i18n import _
@@ -519,7 +518,7 @@ class httppeer(wireprotov1peer.wirepeer)
         filename = None
         try:
             # dump bundle to disk
-            fd, filename = tempfile.mkstemp(prefix="hg-bundle-", suffix=".hg")
+            fd, filename = pycompat.mkstemp(prefix="hg-bundle-", suffix=".hg")
             fh = os.fdopen(fd, r"wb")
             d = fp.read(4096)
             while d:
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -211,7 +211,7 @@ def extract(ui, fileobj):
     Any item can be missing from the dictionary. If filename is missing,
     fileobj did not contain a patch. Caller must unlink filename when done.'''
 
-    fd, tmpname = tempfile.mkstemp(prefix='hg-patch-')
+    fd, tmpname = pycompat.mkstemp(prefix='hg-patch-')
     tmpfp = os.fdopen(fd, r'wb')
     try:
         yield _extract(ui, fileobj, tmpname, tmpfp)
@@ -1109,7 +1109,7 @@ file will be generated: you can use that
 all lines of the hunk are removed, then the edit is aborted and
 the hunk is left unchanged.
 """)
-                (patchfd, patchfn) = tempfile.mkstemp(prefix="hg-editor-",
+                (patchfd, patchfn) = pycompat.mkstemp(prefix="hg-editor-",
                                                       suffix=".diff")
                 ncpatchfp = None
                 try:
diff --git a/mercurial/posix.py b/mercurial/posix.py
--- a/mercurial/posix.py
+++ b/mercurial/posix.py
@@ -216,7 +216,7 @@ def checkexec(path):
             # check directly in path and don't leave checkisexec behind
             checkdir = path
             checkisexec = None
-        fh, fn = tempfile.mkstemp(dir=checkdir, prefix='hg-checkexec-')
+        fh, fn = pycompat.mkstemp(dir=checkdir, prefix='hg-checkexec-')
         try:
             os.close(fh)
             m = os.stat(fn).st_mode
diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -15,6 +15,7 @@ import inspect
 import os
 import shlex
 import sys
+import tempfile
 
 ispy3 = (sys.version_info[0] >= 3)
 ispypy = (r'__pypy__' in sys.builtin_module_names)
@@ -384,3 +385,7 @@ def getoptb(args, shortlist, namelist):
 
 def gnugetoptb(args, shortlist, namelist):
     return _getoptbwrapper(getopt.gnu_getopt, args, shortlist, namelist)
+
+# text=True is not supported; use util.from/tonativeeol() instead
+def mkstemp(suffix=b'', prefix=b'tmp', dir=None):
+    return tempfile.mkstemp(suffix, prefix, dir)
diff --git a/mercurial/server.py b/mercurial/server.py
--- a/mercurial/server.py
+++ b/mercurial/server.py
@@ -8,7 +8,6 @@
 from __future__ import absolute_import
 
 import os
-import tempfile
 
 from .i18n import _
 
@@ -72,7 +71,7 @@ def runservice(opts, parentfn=None, init
 
     if opts['daemon'] and not opts['daemon_postexec']:
         # Signal child process startup with file removal
-        lockfd, lockpath = tempfile.mkstemp(prefix='hg-service-')
+        lockfd, lockpath = pycompat.mkstemp(prefix='hg-service-')
         os.close(lockfd)
         try:
             if not runargs:
diff --git a/mercurial/statprof.py b/mercurial/statprof.py
--- a/mercurial/statprof.py
+++ b/mercurial/statprof.py
@@ -112,7 +112,6 @@ import json
 import os
 import signal
 import sys
-import tempfile
 import threading
 import time
 
@@ -691,7 +690,7 @@ def write_to_flame(data, fp, scriptpath=
               file=fp)
         return
 
-    fd, path = tempfile.mkstemp()
+    fd, path = pycompat.mkstemp()
 
     file = open(path, "w+")
 
diff --git a/mercurial/streamclone.py b/mercurial/streamclone.py
--- a/mercurial/streamclone.py
+++ b/mercurial/streamclone.py
@@ -10,7 +10,6 @@ from __future__ import absolute_import
 import contextlib
 import os
 import struct
-import tempfile
 import warnings
 
 from .i18n import _
@@ -19,6 +18,7 @@ from . import (
     cacheutil,
     error,
     phases,
+    pycompat,
     store,
     util,
 )
@@ -469,7 +469,7 @@ def maketempcopies():
     files = []
     try:
         def copy(src):
-            fd, dst = tempfile.mkstemp()
+            fd, dst = pycompat.mkstemp()
             os.close(fd)
             files.append(dst)
             util.copyfiles(src, dst, hardlink=True)
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -18,7 +18,6 @@ import signal
 import socket
 import subprocess
 import sys
-import tempfile
 import traceback
 
 from .i18n import _
@@ -1446,7 +1445,7 @@ class ui(object):
         rdir = None
         if self.configbool('experimental', 'editortmpinhg'):
             rdir = repopath
-        (fd, name) = tempfile.mkstemp(prefix='hg-' + extra['prefix'] + '-',
+        (fd, name) = pycompat.mkstemp(prefix='hg-' + extra['prefix'] + '-',
                                       suffix=suffix,
                                       dir=rdir)
         try:
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -31,7 +31,6 @@ import shutil
 import socket
 import stat
 import sys
-import tempfile
 import time
 import traceback
 import warnings
@@ -1893,7 +1892,7 @@ def checknlink(testfile):
     # work around issue2543 (or testfile may get lost on Samba shares)
     f1, f2, fp = None, None, None
     try:
-        fd, f1 = tempfile.mkstemp(prefix='.%s-' % os.path.basename(testfile),
+        fd, f1 = pycompat.mkstemp(prefix='.%s-' % os.path.basename(testfile),
                                   suffix='1~', dir=os.path.dirname(testfile))
         os.close(fd)
         f2 = '%s2~' % f1[:-2]
@@ -1939,7 +1938,7 @@ def mktempcopy(name, emptyok=False, crea
     Returns the name of the temporary file.
     """
     d, fn = os.path.split(name)
-    fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, suffix='~', dir=d)
+    fd, temp = pycompat.mkstemp(prefix='.%s-' % fn, suffix='~', dir=d)
     os.close(fd)
     # Temporary files are created with mode 0600, which is usually not
     # what we want.  If the original file already exists, just copy
diff --git a/mercurial/utils/procutil.py b/mercurial/utils/procutil.py
--- a/mercurial/utils/procutil.py
+++ b/mercurial/utils/procutil.py
@@ -16,7 +16,6 @@ import os
 import signal
 import subprocess
 import sys
-import tempfile
 import time
 
 from ..i18n import _
@@ -164,11 +163,11 @@ def tempfilter(s, cmd):
     the temporary files generated.'''
     inname, outname = None, None
     try:
-        infd, inname = tempfile.mkstemp(prefix='hg-filter-in-')
+        infd, inname = pycompat.mkstemp(prefix='hg-filter-in-')
         fp = os.fdopen(infd, r'wb')
         fp.write(s)
         fp.close()
-        outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-')
+        outfd, outname = pycompat.mkstemp(prefix='hg-filter-out-')
         os.close(outfd)
         cmd = cmd.replace('INFILE', inname)
         cmd = cmd.replace('OUTFILE', outname)
diff --git a/mercurial/vfs.py b/mercurial/vfs.py
--- a/mercurial/vfs.py
+++ b/mercurial/vfs.py
@@ -11,7 +11,6 @@ import errno
 import os
 import shutil
 import stat
-import tempfile
 import threading
 
 from .i18n import _
@@ -171,7 +170,7 @@ class abstractvfs(object):
         return os.mkdir(self.join(path))
 
     def mkstemp(self, suffix='', prefix='tmp', dir=None):
-        fd, name = tempfile.mkstemp(suffix=suffix, prefix=prefix,
+        fd, name = pycompat.mkstemp(suffix=suffix, prefix=prefix,
                                     dir=self.join(dir))
         dname, fname = util.split(name)
         if dir:
diff --git a/mercurial/wireprotov1server.py b/mercurial/wireprotov1server.py
--- a/mercurial/wireprotov1server.py
+++ b/mercurial/wireprotov1server.py
@@ -8,7 +8,6 @@
 from __future__ import absolute_import
 
 import os
-import tempfile
 
 from .i18n import _
 from .node import (
@@ -568,7 +567,7 @@ def unbundle(repo, proto, heads):
                             fp.close()
                         if tempname:
                             os.unlink(tempname)
-                    fd, tempname = tempfile.mkstemp(prefix='hg-unbundle-')
+                    fd, tempname = pycompat.mkstemp(prefix='hg-unbundle-')
                     repo.ui.debug('redirecting incoming bundle to %s\n' %
                         tempname)
                     fp = os.fdopen(fd, pycompat.sysstr('wb+'))


More information about the Mercurial-devel mailing list