##// END OF EJS Templates
py3: introduce a wrapper for __builtins__.{raw_,}input()...
py3: introduce a wrapper for __builtins__.{raw_,}input() In order to make this work, we have to wrap the io streams in a TextIOWrapper so that __builtins__.input() can do unicode IO on Python 3. We can't just restore the original (unicode) sys.std* because we might be running a cmdserver, and if we blindly restore sys.* to the original values then we end up breaking the cmdserver. Sadly, TextIOWrapper tries to close the underlying stream during its __del__, so we have to make a sublcass to prevent that. If you see errors like: TypeError: a bytes-like object is required, not 'str' On an input() or print() call on Python 3, the substitution of sys.std* is probably the root cause. A previous version of this change tried to put the bytesinput() method in pycompat - it turns out we need to do some encoding handling, so we have to be in a higher layer that's allowed to use mercurial.encoding.encoding. As a result, this is in util for now, with the TextIOWrapper subclass hiding in encoding.py. I'm not sure of a better place for the time being. Differential Revision: https://phab.mercurial-scm.org/D299

File last commit:

r31050:20653270 default
r33838:7d5bc0e5 default
Show More
txnutil.py
36 lines | 1.0 KiB | text/x-python | PythonLexer
# txnutil.py - transaction related utilities
#
# Copyright FUJIWARA Katsunori <foozy@lares.dti.ne.jp> and others
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
from __future__ import absolute_import
import errno
from . import (
encoding,
)
def mayhavepending(root):
'''return whether 'root' may have pending changes, which are
visible to this process.
'''
return root == encoding.environ.get('HG_PENDING')
def trypending(root, vfs, filename, **kwargs):
'''Open file to be read according to HG_PENDING environment variable
This opens '.pending' of specified 'filename' only when HG_PENDING
is equal to 'root'.
This returns '(fp, is_pending_opened)' tuple.
'''
if mayhavepending(root):
try:
return (vfs('%s.pending' % filename, **kwargs), True)
except IOError as inst:
if inst.errno != errno.ENOENT:
raise
return (vfs(filename, **kwargs), False)