# HG changeset patch
# User Yuya Nishihara <y...@tcha.org>
# Date 1527305887 -32400
#      Sat May 26 12:38:07 2018 +0900
# Node ID edb77549a0ac6a6d6f3c53ead56738ca7f0dabce
# Parent  7fb141e660f0e83b98388f1fdaf3480b548b2ce8
py3: wrap tempfile.NamedTemporaryFile() to return bytes fp.name

Unlike its name, tempfile.NamedTemporaryFile is not a class, so I renamed
the pycompat version to look like a plain function.

Since temp.name uses in the infinitepush extension aren't bytes-safe, this
patch leaves them unmodified. Another weird thing is tempfile.mktemp(),
which does not accept bytes suffix nor prefix. Sigh.

diff --git a/hgext/infinitepush/store.py b/hgext/infinitepush/store.py
--- a/hgext/infinitepush/store.py
+++ b/hgext/infinitepush/store.py
@@ -120,6 +120,8 @@ class externalbundlestore(abstractbundle
     def write(self, data):
         # Won't work on windows because you can't open file second time without
         # closing it
+        # TODO: rewrite without str.format() and replace NamedTemporaryFile()
+        # with pycompat.namedtempfile()
         with NamedTemporaryFile() as temp:
             temp.write(data)
             temp.flush()
@@ -142,6 +144,8 @@ class externalbundlestore(abstractbundle
     def read(self, handle):
         # Won't work on windows because you can't open file second time without
         # closing it
+        # TODO: rewrite without str.format() and replace NamedTemporaryFile()
+        # with pycompat.namedtempfile()
         with NamedTemporaryFile() as temp:
             formatted_args = [arg.format(filename=temp.name, handle=handle)
                               for arg in self.get_args]
diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py
--- a/mercurial/debugcommands.py
+++ b/mercurial/debugcommands.py
@@ -21,7 +21,6 @@ import stat
 import string
 import subprocess
 import sys
-import tempfile
 import time
 
 from .i18n import _
@@ -970,7 +969,7 @@ def debugfsinfo(ui, path="."):
     ui.write(('hardlink: %s\n') % (util.checknlink(path) and 'yes' or 'no'))
     casesensitive = '(unknown)'
     try:
-        with tempfile.NamedTemporaryFile(prefix='.debugfsinfo', dir=path) as f:
+        with pycompat.namedtempfile(prefix='.debugfsinfo', dir=path) as f:
             casesensitive = util.fscasesensitive(f.name) and 'yes' or 'no'
     except OSError:
         pass
diff --git a/mercurial/posix.py b/mercurial/posix.py
--- a/mercurial/posix.py
+++ b/mercurial/posix.py
@@ -249,16 +249,15 @@ def checklink(path):
         else:
             checkdir = path
             cachedir = None
-        fscheckdir = pycompat.fsdecode(checkdir)
-        name = tempfile.mktemp(dir=fscheckdir,
+        name = tempfile.mktemp(dir=pycompat.fsdecode(checkdir),
                                prefix=r'checklink-')
         name = pycompat.fsencode(name)
         try:
             fd = None
             if cachedir is None:
-                fd = tempfile.NamedTemporaryFile(dir=fscheckdir,
-                                                 prefix=r'hg-checklink-')
-                target = pycompat.fsencode(os.path.basename(fd.name))
+                fd = pycompat.namedtempfile(dir=checkdir,
+                                            prefix='hg-checklink-')
+                target = os.path.basename(fd.name)
             else:
                 # create a fixed file to link to; doesn't matter if it
                 # already exists.
diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -392,3 +392,11 @@ def mkdtemp(suffix=b'', prefix=b'tmp', d
 # 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)
+
+# mode must include 'b'ytes as encoding= is not supported
+def namedtempfile(mode=b'w+b', bufsize=-1, suffix=b'', prefix=b'tmp', dir=None,
+                  delete=True):
+    mode = sysstr(mode)
+    assert r'b' in mode
+    return tempfile.NamedTemporaryFile(mode, bufsize, suffix=suffix,
+                                       prefix=prefix, dir=dir, delete=delete)
_______________________________________________
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Reply via email to