Patchwork D9211: pycompat: add an entry for unnamedtmpfile

login
register
mail settings
Submitter phabricator
Date Oct. 14, 2020, 11:34 p.m.
Message ID <differential-rev-PHID-DREV-3ml6ztejuzm5662krlki-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/47468/
State Superseded
Headers show

Comments

phabricator - Oct. 14, 2020, 11:34 p.m.
marmoute created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D9211

AFFECTED FILES
  mercurial/pycompat.py

CHANGE DETAILS




To: marmoute, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -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(