def execfile(filename, myglobals=None, mylocals=None):
"""
Read and execute a Python script from a file in the given namespaces.
The globals and locals are dictionaries, defaulting to the current
globals and locals. If only globals is given, locals defaults to it.
"""
if myglobals is None:
# There seems to be no alternative to frame hacking here.
caller_frame = inspect.stack()[1]
myglobals = caller_frame[0].f_globals
mylocals = caller_frame[0].f_locals
elif mylocals is None:
# Only if myglobals is given do we set mylocals to it.
mylocals = myglobals
if not isinstance(myglobals, Mapping):
raise TypeError('globals must be a mapping')
if not isinstance(mylocals, Mapping):
raise TypeError('locals must be a mapping')
with open(filename, "rbU") as fin:
source = fin.read()
code = compile(source, filename, "exec")
exec_(code, myglobals, mylocals)
python类execfile()的实例源码
def execfile(filename, myglobals=None, mylocals=None):
"""
Read and execute a Python script from a file in the given namespaces.
The globals and locals are dictionaries, defaulting to the current
globals and locals. If only globals is given, locals defaults to it.
"""
if myglobals is None:
# There seems to be no alternative to frame hacking here.
caller_frame = inspect.stack()[1]
myglobals = caller_frame[0].f_globals
mylocals = caller_frame[0].f_locals
elif mylocals is None:
# Only if myglobals is given do we set mylocals to it.
mylocals = myglobals
if not isinstance(myglobals, Mapping):
raise TypeError('globals must be a mapping')
if not isinstance(mylocals, Mapping):
raise TypeError('locals must be a mapping')
with open(filename, "rbU") as fin:
source = fin.read()
code = compile(source, filename, "exec")
exec_(code, myglobals, mylocals)
def execfile(filename, myglobals=None, mylocals=None):
"""
Read and execute a Python script from a file in the given namespaces.
The globals and locals are dictionaries, defaulting to the current
globals and locals. If only globals is given, locals defaults to it.
"""
if myglobals is None:
# There seems to be no alternative to frame hacking here.
caller_frame = inspect.stack()[1]
myglobals = caller_frame[0].f_globals
mylocals = caller_frame[0].f_locals
elif mylocals is None:
# Only if myglobals is given do we set mylocals to it.
mylocals = myglobals
if not isinstance(myglobals, Mapping):
raise TypeError('globals must be a mapping')
if not isinstance(mylocals, Mapping):
raise TypeError('locals must be a mapping')
with open(filename, "rbU") as fin:
source = fin.read()
code = compile(source, filename, "exec")
exec_(code, myglobals, mylocals)
def execfile(filename, myglobals=None, mylocals=None):
"""
Read and execute a Python script from a file in the given namespaces.
The globals and locals are dictionaries, defaulting to the current
globals and locals. If only globals is given, locals defaults to it.
"""
if myglobals is None:
# There seems to be no alternative to frame hacking here.
caller_frame = inspect.stack()[1]
myglobals = caller_frame[0].f_globals
mylocals = caller_frame[0].f_locals
elif mylocals is None:
# Only if myglobals is given do we set mylocals to it.
mylocals = myglobals
if not isinstance(myglobals, Mapping):
raise TypeError('globals must be a mapping')
if not isinstance(mylocals, Mapping):
raise TypeError('locals must be a mapping')
with open(filename, "rbU") as fin:
source = fin.read()
code = compile(source, filename, "exec")
exec_(code, myglobals, mylocals)
def execfile(filename, myglobals=None, mylocals=None):
"""
Read and execute a Python script from a file in the given namespaces.
The globals and locals are dictionaries, defaulting to the current
globals and locals. If only globals is given, locals defaults to it.
"""
if myglobals is None:
# There seems to be no alternative to frame hacking here.
caller_frame = inspect.stack()[1]
myglobals = caller_frame[0].f_globals
mylocals = caller_frame[0].f_locals
elif mylocals is None:
# Only if myglobals is given do we set mylocals to it.
mylocals = myglobals
if not isinstance(myglobals, Mapping):
raise TypeError('globals must be a mapping')
if not isinstance(mylocals, Mapping):
raise TypeError('locals must be a mapping')
with open(filename, "rbU") as fin:
source = fin.read()
code = compile(source, filename, "exec")
exec_(code, myglobals, mylocals)
def execfile(filename, myglobals=None, mylocals=None):
"""
Read and execute a Python script from a file in the given namespaces.
The globals and locals are dictionaries, defaulting to the current
globals and locals. If only globals is given, locals defaults to it.
"""
if myglobals is None:
# There seems to be no alternative to frame hacking here.
caller_frame = inspect.stack()[1]
myglobals = caller_frame[0].f_globals
mylocals = caller_frame[0].f_locals
elif mylocals is None:
# Only if myglobals is given do we set mylocals to it.
mylocals = myglobals
if not isinstance(myglobals, Mapping):
raise TypeError('globals must be a mapping')
if not isinstance(mylocals, Mapping):
raise TypeError('locals must be a mapping')
with open(filename, "rbU") as fin:
source = fin.read()
code = compile(source, filename, "exec")
exec_(code, myglobals, mylocals)
def execfile(filename, myglobals=None, mylocals=None):
"""
Read and execute a Python script from a file in the given namespaces.
The globals and locals are dictionaries, defaulting to the current
globals and locals. If only globals is given, locals defaults to it.
"""
if myglobals is None:
# There seems to be no alternative to frame hacking here.
caller_frame = inspect.stack()[1]
myglobals = caller_frame[0].f_globals
mylocals = caller_frame[0].f_locals
elif mylocals is None:
# Only if myglobals is given do we set mylocals to it.
mylocals = myglobals
if not isinstance(myglobals, Mapping):
raise TypeError('globals must be a mapping')
if not isinstance(mylocals, Mapping):
raise TypeError('locals must be a mapping')
with open(filename, "rbU") as fin:
source = fin.read()
code = compile(source, filename, "exec")
exec_(code, myglobals, mylocals)
def execfile(filename, myglobals=None, mylocals=None):
"""
Read and execute a Python script from a file in the given namespaces.
The globals and locals are dictionaries, defaulting to the current
globals and locals. If only globals is given, locals defaults to it.
"""
if myglobals is None:
# There seems to be no alternative to frame hacking here.
caller_frame = inspect.stack()[1]
myglobals = caller_frame[0].f_globals
mylocals = caller_frame[0].f_locals
elif mylocals is None:
# Only if myglobals is given do we set mylocals to it.
mylocals = myglobals
if not isinstance(myglobals, Mapping):
raise TypeError('globals must be a mapping')
if not isinstance(mylocals, Mapping):
raise TypeError('locals must be a mapping')
with open(filename, "rbU") as fin:
source = fin.read()
code = compile(source, filename, "exec")
exec_(code, myglobals, mylocals)
def execfile(filename, myglobals=None, mylocals=None):
"""
Read and execute a Python script from a file in the given namespaces.
The globals and locals are dictionaries, defaulting to the current
globals and locals. If only globals is given, locals defaults to it.
"""
if myglobals is None:
# There seems to be no alternative to frame hacking here.
caller_frame = inspect.stack()[1]
myglobals = caller_frame[0].f_globals
mylocals = caller_frame[0].f_locals
elif mylocals is None:
# Only if myglobals is given do we set mylocals to it.
mylocals = myglobals
if not isinstance(myglobals, Mapping):
raise TypeError('globals must be a mapping')
if not isinstance(mylocals, Mapping):
raise TypeError('locals must be a mapping')
with open(filename, "rbU") as fin:
source = fin.read()
code = compile(source, filename, "exec")
exec_(code, myglobals, mylocals)
def execfile(filename, myglobals=None, mylocals=None):
"""
Read and execute a Python script from a file in the given namespaces.
The globals and locals are dictionaries, defaulting to the current
globals and locals. If only globals is given, locals defaults to it.
"""
if myglobals is None:
# There seems to be no alternative to frame hacking here.
caller_frame = inspect.stack()[1]
myglobals = caller_frame[0].f_globals
mylocals = caller_frame[0].f_locals
elif mylocals is None:
# Only if myglobals is given do we set mylocals to it.
mylocals = myglobals
if not isinstance(myglobals, Mapping):
raise TypeError('globals must be a mapping')
if not isinstance(mylocals, Mapping):
raise TypeError('locals must be a mapping')
with open(filename, "rbU") as fin:
source = fin.read()
code = compile(source, filename, "exec")
exec_(code, myglobals, mylocals)
def execfile(fname, glob, loc=None, compiler=None):
loc = loc if (loc is not None) else glob
with open(fname, 'rb') as f:
compiler = compiler or compile
exec(compiler(f.read(), fname, 'exec'), glob, loc)
# Refactor print statements in doctests.
def execfile(fname, glob=None, loc=None, compiler=None):
loc = loc if (loc is not None) else glob
scripttext = builtin_mod.open(fname).read()+ '\n'
# compile converts unicode filename to str assuming
# ascii. Let's do the conversion before calling compile
if isinstance(fname, unicode):
filename = unicode_to_str(fname)
else:
filename = fname
compiler = compiler or compile
exec(compiler(scripttext, filename, 'exec'), glob, loc)
def execfile(fname, glob=None, loc=None, compiler=None):
if isinstance(fname, unicode):
filename = fname.encode(sys.getfilesystemencoding())
else:
filename = fname
where = [ns for ns in [glob, loc] if ns is not None]
if compiler is None:
builtin_mod.execfile(filename, *where)
else:
scripttext = builtin_mod.open(fname).read().rstrip() + '\n'
exec(compiler(scripttext, filename, 'exec'), glob, loc)
def execfile(fname, glob, loc=None, compiler=None):
loc = loc if (loc is not None) else glob
with open(fname, 'rb') as f:
compiler = compiler or compile
exec(compiler(f.read(), fname, 'exec'), glob, loc)
# Refactor print statements in doctests.
def execfile(fname, glob=None, loc=None, compiler=None):
loc = loc if (loc is not None) else glob
scripttext = builtin_mod.open(fname).read()+ '\n'
# compile converts unicode filename to str assuming
# ascii. Let's do the conversion before calling compile
if isinstance(fname, unicode):
filename = unicode_to_str(fname)
else:
filename = fname
compiler = compiler or compile
exec(compiler(scripttext, filename, 'exec'), glob, loc)
def execfile(fname, glob, loc=None):
loc = loc if (loc is not None) else glob
exec compile(open(fname, 'rb').read(), fname, 'exec') in glob, loc
# Refactor print statements in doctests.
def MethodType(func, instance):
return types.MethodType(func, instance, type(instance))
# don't override system execfile on 2.x:
def execfile(fname, glob=None, loc=None):
loc = loc if (loc is not None) else glob
# The rstrip() is necessary b/c trailing whitespace in files will
# cause an IndentationError in Python 2.6 (this was fixed in 2.7,
# but we still support 2.6). See issue 1027.
scripttext = __builtin__.open(fname).read().rstrip() + '\n'
# compile converts unicode filename to str assuming
# ascii. Let's do the conversion before calling compile
if isinstance(fname, unicode):
filename = unicode_to_str(fname)
else:
filename = fname
exec compile(scripttext, filename, 'exec') in glob, loc
def execfile(fname, *where):
if isinstance(fname, unicode):
filename = fname.encode(sys.getfilesystemencoding())
else:
filename = fname
__builtin__.execfile(filename, *where)
def execfile(fname, glob, loc=None, compiler=None):
loc = loc if (loc is not None) else glob
with open(fname, 'rb') as f:
compiler = compiler or compile
exec(compiler(f.read(), fname, 'exec'), glob, loc)
# Refactor print statements in doctests.
def execfile(fname, glob=None, loc=None, compiler=None):
loc = loc if (loc is not None) else glob
scripttext = builtin_mod.open(fname).read()+ '\n'
# compile converts unicode filename to str assuming
# ascii. Let's do the conversion before calling compile
if isinstance(fname, unicode):
filename = unicode_to_str(fname)
else:
filename = fname
compiler = compiler or compile
exec(compiler(scripttext, filename, 'exec'), glob, loc)
def execfile(fname, glob=None, loc=None, compiler=None):
if isinstance(fname, unicode):
filename = fname.encode(sys.getfilesystemencoding())
else:
filename = fname
where = [ns for ns in [glob, loc] if ns is not None]
if compiler is None:
builtin_mod.execfile(filename, *where)
else:
scripttext = builtin_mod.open(fname).read().rstrip() + '\n'
exec(compiler(scripttext, filename, 'exec'), glob, loc)
def execfile(fname, glob, loc=None, compiler=None):
loc = loc if (loc is not None) else glob
with open(fname, 'rb') as f:
compiler = compiler or compile
exec(compiler(f.read(), fname, 'exec'), glob, loc)
# Refactor print statements in doctests.
def execfile(fname, glob=None, loc=None, compiler=None):
if isinstance(fname, unicode):
filename = fname.encode(sys.getfilesystemencoding())
else:
filename = fname
where = [ns for ns in [glob, loc] if ns is not None]
if compiler is None:
builtin_mod.execfile(filename, *where)
else:
scripttext = builtin_mod.open(fname).read().rstrip() + '\n'
exec(compiler(scripttext, filename, 'exec'), glob, loc)
def execfile(fname, glob, loc=None, compiler=None):
loc = loc if (loc is not None) else glob
with open(fname, 'rb') as f:
compiler = compiler or compile
exec(compiler(f.read(), fname, 'exec'), glob, loc)
# Refactor print statements in doctests.
def execfile(fname, glob=None, loc=None, compiler=None):
loc = loc if (loc is not None) else glob
scripttext = builtin_mod.open(fname).read()+ '\n'
# compile converts unicode filename to str assuming
# ascii. Let's do the conversion before calling compile
if isinstance(fname, unicode):
filename = unicode_to_str(fname)
else:
filename = fname
compiler = compiler or compile
exec(compiler(scripttext, filename, 'exec'), glob, loc)
def execfile(fname, glob=None, loc=None, compiler=None):
if isinstance(fname, unicode):
filename = fname.encode(sys.getfilesystemencoding())
else:
filename = fname
where = [ns for ns in [glob, loc] if ns is not None]
if compiler is None:
builtin_mod.execfile(filename, *where)
else:
scripttext = builtin_mod.open(fname).read().rstrip() + '\n'
exec(compiler(scripttext, filename, 'exec'), glob, loc)
def execfile(fname, glob, loc=None, compiler=None):
loc = loc if (loc is not None) else glob
with open(fname, 'rb') as f:
compiler = compiler or compile
exec(compiler(f.read(), fname, 'exec'), glob, loc)
# Refactor print statements in doctests.
def execfile(fname, glob=None, loc=None, compiler=None):
loc = loc if (loc is not None) else glob
scripttext = builtin_mod.open(fname).read()+ '\n'
# compile converts unicode filename to str assuming
# ascii. Let's do the conversion before calling compile
if isinstance(fname, unicode):
filename = unicode_to_str(fname)
else:
filename = fname
compiler = compiler or compile
exec(compiler(scripttext, filename, 'exec'), glob, loc)
def execfile(fname, glob, loc=None, compiler=None):
loc = loc if (loc is not None) else glob
with open(fname, 'rb') as f:
compiler = compiler or compile
exec(compiler(f.read(), fname, 'exec'), glob, loc)
# Refactor print statements in doctests.