def run_file_as_main(self, filename, args):
f = open(filename, 'rb')
try:
contents = f.read().replace(to_bytes('\r\n'), to_bytes('\n'))
finally:
f.close()
sys.argv = [filename]
sys.argv.extend(_command_line_to_args_list(args))
self.exec_mod.__file__ = filename
if sys.platform == 'cli':
code = python_context.CreateSnippet(contents, None, SourceCodeKind.File)
code.Execute(self.exec_mod)
else:
self.code_flags = 0
real_file = filename
if isinstance(filename, unicode) and unicode is not str:
# http://pytools.codeplex.com/workitem/696
# We need to encode the unicode filename here, Python 2.x will throw trying
# to convert it to ASCII instead of the filesystem encoding.
real_file = filename.encode(sys.getfilesystemencoding())
code = compile(contents, real_file, 'exec')
self.code_flags |= (code.co_flags & BasicReplBackend.future_bits)
exec(code, self.exec_mod.__dict__, self.exec_mod.__dict__)
评论列表
文章目录