Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Make command line more like regular python interpreter's

  • Loading branch information...
commit 43390ec57956a5f4655f680883d9a1462752edc3 1 parent c9342aa
Ben Darnell authored

Showing 1 changed file with 44 additions and 20 deletions. Show diff stats Hide diff stats

  1. +44 20 auto2to3.py
64 auto2to3.py
@@ -40,6 +40,33 @@
40 40 PACKAGES = []
41 41 DIRS = []
42 42
  43 +def maybe_2to3(filename, modname=None):
  44 + """Returns a python3 version of filename."""
  45 + need_2to3 = False
  46 + filename = os.path.abspath(filename)
  47 + if any(filename.startswith(d) for d in DIRS):
  48 + need_2to3 = True
  49 + elif modname is not None and any(modname.startswith(p) for p in PACKAGES):
  50 + need_2to3 = True
  51 + if not need_2to3:
  52 + return filename
  53 + outfilename = '/_auto2to3_'.join(os.path.split(filename))
  54 + if (not os.path.exists(outfilename) or
  55 + os.stat(filename).st_mtime > os.stat(outfilename).st_mtime):
  56 + try:
  57 + with open(filename) as file:
  58 + contents = file.read()
  59 + contents = rt.refactor_docstring(contents, filename)
  60 + tree = rt.refactor_string(contents, filename)
  61 + except Exception as err:
  62 + raise ImportError("2to3 couldn't convert %r" % filename)
  63 + outfile = open(outfilename, 'wb')
  64 + outfile.write(str(tree).encode('utf8'))
  65 + outfile.close()
  66 + return outfilename
  67 +
  68 +
  69 +
43 70 class ToThreeImporter(ImpImporter):
44 71 def find_module(self, fullname, path=None):
45 72 # this duplicates most of ImpImporter.find_module
@@ -55,20 +82,8 @@ def find_module(self, fullname, path=None):
55 82 except ImportError:
56 83 return None
57 84 if file and etc[2] == imp.PY_SOURCE:
58   - if (any(fullname.startswith(p) for p in PACKAGES) or
59   - any(filename.startswith(d) for d in DIRS)):
60   - outfilename = '/_auto2to3_'.join(os.path.split(filename))
61   - if (not os.path.exists(outfilename) or
62   - os.stat(filename).st_mtime > os.stat(outfilename).st_mtime):
63   - try:
64   - contents = file.read()
65   - contents = rt.refactor_docstring(contents, filename)
66   - tree = rt.refactor_string(contents, filename)
67   - except Exception as err:
68   - raise ImportError("2to3 couldn't convert %r" % filename)
69   - outfile = open(outfilename, 'wb')
70   - outfile.write(str(tree).encode('utf8'))
71   - outfile.close()
  85 + outfilename = maybe_2to3(filename, modname=fullname)
  86 + if outfilename != filename:
72 87 file.close()
73 88 filename = outfilename
74 89 file = open(filename, 'rb')
@@ -85,18 +100,27 @@ def main():
85 100 parser = argparse.ArgumentParser()
86 101 parser.add_argument('--package', action='append')
87 102 parser.add_argument('--dir', action='append')
88   - # accept and ignore -m for compatibility with the python interpreter
89   - parser.add_argument('-m', action='store_true')
90   - parser.add_argument('main')
  103 + parser.add_argument('-m', action='store', metavar='MODULE')
91 104 args, rest = parser.parse_known_args()
92 105 if args.package:
93 106 PACKAGES.extend(args.package)
94 107 if args.dir:
95   - DIRS.extend(args.dir)
  108 + DIRS.extend(os.path.abspath(d) for d in args.dir)
96 109 if not PACKAGES and not DIRS:
97 110 DIRS.append(os.getcwd())
98   - sys.argv[1:] = rest
99   - runpy.run_module(args.main, run_name='__main__', alter_sys=True)
  111 + if args.m:
  112 + sys.argv[1:] = rest
  113 + runpy.run_module(args.m, run_name='__main__', alter_sys=True)
  114 + elif rest:
  115 + sys.argv = rest
  116 + converted = maybe_2to3(rest[0])
  117 + with open(converted) as f:
  118 + new_globals = dict(__name__='__main__',
  119 + __file__=rest[0])
  120 + exec(f.read(), new_globals)
  121 + else:
  122 + import code
  123 + code.interact()
100 124
101 125 if __name__ == '__main__':
102 126 main()

0 comments on commit 43390ec

Please sign in to comment.
Something went wrong with that request. Please try again.