Commits

Michał Górny committed 4ff1733

Use exec() to run scripts.

This is much faster than re-executing the interpreter, and it preserves
the flags.

We are not using execfile() since it fails to provide a way to clearly
detect missing file.

  • Participants
  • Parent commits 6c5ed77

Comments (0)

Files changed (1)

src/python-exec.in

 # -pypy* or -jython* suffixes. You are most likely looking for one
 # of those.
 
-import os, os.path, sys
+from __future__ import with_statement
+import os.path, sys
 
 # XXX: avoid sys.executable if possible
 epython = os.path.basename(sys.executable)
 self = sys.argv[0] + '-' + epython
 
-# XXX: avoid re-executing python (slow!)
-os.execl(sys.executable, sys.executable, self, *sys.argv[1:])
+__file__ = self
+sys.argv[0] = self
+
+kwargs = {}
+if sys.version_info[0] >= 3:
+	import tokenize
+
+	# need to provide encoding
+	with open(self, 'rb') as f:
+		kwargs['encoding'] = tokenize.detect_encoding(f.readline)[0]
+
+with open(self, 'r', **kwargs) as f:
+	data = f.read()
+
+exec(data)