pycompat: add an entry for unnamedtmpfile
authorPierre-Yves David <pierre-yves.david@octobus.net>
Wed, 14 Oct 2020 17:51:56 +0200
changeset 45727 d4c4391aa7f2
parent 45726 d1072cba8aff
child 45728 232c88dd89e3
pycompat: add an entry for unnamedtmpfile I am going to use unnamed temporary files to pass arbitrarily large input data to worker creating bundles. To do so, I need a unified API that work on py2 and py3. Differential Revision: https://phab.mercurial-scm.org/D9211
mercurial/pycompat.py
--- a/mercurial/pycompat.py	Wed Oct 14 21:47:14 2020 +0200
+++ b/mercurial/pycompat.py	Wed Oct 14 17:51:56 2020 +0200
@@ -506,6 +506,17 @@
     return tempfile.mkstemp(suffix, prefix, dir)
 
 
+# TemporaryFile does not support an "encoding=" argument on python2.
+# This wrapper file are always open in byte mode.
+def unnamedtempfile(mode=None, *args, **kwargs):
+    if mode is None:
+        mode = b'w+b'
+    else:
+        mode = sysstr(mode)
+    assert 'b' in mode
+    return tempfile.TemporaryFile(mode, *args, **kwargs)
+
+
 # NamedTemporaryFile does not support an "encoding=" argument on python2.
 # This wrapper file are always open in byte mode.
 def namedtempfile(