2018-02-02 23:18:05 +03:00
|
|
|
# Emulating the Python interpreter
|
|
|
|
#
|
|
|
|
# Copyright 2018 Facebook, Inc.
|
|
|
|
#
|
|
|
|
# This software may be used and distributed according to the terms of the
|
|
|
|
# GNU General Public License version 2 or any later version.
|
|
|
|
|
|
|
|
# The emulated python interpreter:
|
|
|
|
# - Can import modules (including native ones) that can be imported here.
|
|
|
|
# - Support simple command line flags like "-m", "-c", etc.
|
|
|
|
# - Do not go through the default entry point (mercurial.dispatch).
|
|
|
|
# This is useful for testing when the main hg program is built into a single
|
|
|
|
# binary that always goes through the default entry point.
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from . import (
|
|
|
|
encoding,
|
|
|
|
pycompat,
|
|
|
|
)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
argv = sys.argv
|
|
|
|
# PYTHONPATH is not always respected by a "python binary" wrapper.
|
|
|
|
# Also respect HGPYTHONPATH.
|
|
|
|
sys.path.extend(encoding.environ.get('PYTHONPATH', '').split(
|
|
|
|
pycompat.ospathsep))
|
|
|
|
sys.path[0:0] = encoding.environ.get('HGPYTHONPATH', '').split(
|
|
|
|
pycompat.ospathsep)
|
|
|
|
|
|
|
|
# Silent warnings like "ImportWarning: Not importing ..."
|
|
|
|
import warnings
|
|
|
|
warnings.filterwarnings("ignore")
|
|
|
|
|
|
|
|
if len(argv) >= 2 and os.path.exists(argv[1]):
|
|
|
|
# python FILE ...
|
|
|
|
globalvars = globals()
|
|
|
|
globalvars.update({
|
|
|
|
'__file__': os.path.realpath(argv[1]),
|
|
|
|
'__name__': '__main__',
|
|
|
|
})
|
|
|
|
# Make it use this script as the interpreter again
|
|
|
|
sys.executable = argv[0]
|
|
|
|
sys.argv = argv[1:]
|
flake8: enable F821 check
Summary:
This check is useful and detects real errors (ex. fbconduit). Unfortunately
`arc lint` will run it with both py2 and py3 so a lot of py2 builtins will
still be warned.
I didn't find a clean way to disable py3 check. So this diff tries to fix them.
For `xrange`, the change was done by a script:
```
import sys
import redbaron
headertypes = {'comment', 'endl', 'from_import', 'import', 'string',
'assignment', 'atomtrailers'}
xrangefix = '''try:
xrange(0)
except NameError:
xrange = range
'''
def isxrange(x):
try:
return x[0].value == 'xrange'
except Exception:
return False
def main(argv):
for i, path in enumerate(argv):
print('(%d/%d) scanning %s' % (i + 1, len(argv), path))
content = open(path).read()
try:
red = redbaron.RedBaron(content)
except Exception:
print(' warning: failed to parse')
continue
hasxrange = red.find('atomtrailersnode', value=isxrange)
hasxrangefix = 'xrange = range' in content
if hasxrangefix or not hasxrange:
print(' no need to change')
continue
# find a place to insert the compatibility statement
changed = False
for node in red:
if node.type in headertypes:
continue
# node.insert_before is an easier API, but it has bugs changing
# other "finally" and "except" positions. So do the insert
# manually.
# # node.insert_before(xrangefix)
line = node.absolute_bounding_box.top_left.line - 1
lines = content.splitlines(1)
content = ''.join(lines[:line]) + xrangefix + ''.join(lines[line:])
changed = True
break
if changed:
# "content" is faster than "red.dumps()"
open(path, 'w').write(content)
print(' updated')
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
```
For other py2 builtins that do not have a py3 equivalent, some `# noqa`
were added as a workaround for now.
Reviewed By: DurhamG
Differential Revision: D6934535
fbshipit-source-id: 546b62830af144bc8b46788d2e0fd00496838939
2018-02-10 04:31:44 +03:00
|
|
|
exec(open(argv[1]).read(), globalvars)
|
2018-02-02 23:18:05 +03:00
|
|
|
elif len(argv) == 3 and argv[1] == '-m':
|
|
|
|
# python -m MODULE ...
|
|
|
|
# This includes cases like "-m heredoctest" used by run-tests.py
|
|
|
|
__import__(argv[2])
|
|
|
|
elif len(argv) >= 3 and argv[1] == '-c':
|
|
|
|
# python -c COMMND ...
|
|
|
|
content = argv[2]
|
|
|
|
sys.argv = sys.argv[0:1] + argv[3:]
|
|
|
|
exec(content)
|
|
|
|
elif len(argv) == 1:
|
|
|
|
# python << EOF
|
|
|
|
# Read from stdin
|
|
|
|
content = sys.stdin.read()
|
|
|
|
exec(content)
|