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
--- 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(