本文整理汇总了Python中tempfile.NameTemporaryFile方法的典型用法代码示例。如果您正苦于以下问题:Python tempfile.NameTemporaryFile方法的具体用法?Python tempfile.NameTemporaryFile怎么用?Python tempfile.NameTemporaryFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tempfile
的用法示例。
在下文中一共展示了tempfile.NameTemporaryFile方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: debug_script
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NameTemporaryFile [as 别名]
def debug_script(src, pm=False, globs=None):
"Debug a test script. `src` is the script, as a string."
import pdb
# Note that tempfile.NameTemporaryFile() cannot be used. As the
# docs say, a file so created cannot be opened by name a second time
# on modern Windows boxes, and execfile() needs to open it.
srcfilename = tempfile.mktemp(".py", "doctestdebug")
f = open(srcfilename, 'w')
f.write(src)
f.close()
try:
if globs:
globs = globs.copy()
else:
globs = {}
if pm:
try:
execfile(srcfilename, globs, globs)
except:
print sys.exc_info()[1]
pdb.post_mortem(sys.exc_info()[2])
else:
# Note that %r is vital here. '%s' instead can, e.g., cause
# backslashes to get treated as metacharacters on Windows.
pdb.run("execfile(%r)" % srcfilename, globs, globs)
finally:
os.remove(srcfilename)
示例2: debug_script
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NameTemporaryFile [as 别名]
def debug_script(src, pm=False, globs=None):
"Debug a test script. `src` is the script, as a string."
import pdb
# Note that tempfile.NameTemporaryFile() cannot be used. As the
# docs say, a file so created cannot be opened by name a second time
# on modern Windows boxes, and execfile() needs to open it.
srcfilename = tempfile.mktemp(".py", "doctestdebug")
f = open(srcfilename, 'w')
f.write(src)
f.close()
try:
if globs:
globs = globs.copy()
else:
globs = {}
if pm:
try:
execfile(srcfilename, globs, globs)
except:
print(sys.exc_info()[1])
pdb.post_mortem(sys.exc_info()[2])
else:
# Note that %r is vital here. '%s' instead can, e.g., cause
# backslashes to get treated as metacharacters on Windows.
pdb.run("execfile(%r)" % srcfilename, globs, globs)
finally:
os.remove(srcfilename)
示例3: debug_script
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NameTemporaryFile [as 别名]
def debug_script(src, pm=False, globs=None):
"Debug a test script. `src` is the script, as a string."
import pdb
# Note that tempfile.NameTemporaryFile() cannot be used. As the
# docs say, a file so created cannot be opened by name a second time
# on modern Windows boxes, and execfile() needs to open it.
srcfilename = tempfile.mktemp(".py", "doctestdebug")
with open(srcfilename, 'w') as fp:
fp.write(src)
try:
if globs:
globs = globs.copy()
else:
globs = {}
if pm:
try:
execfile(srcfilename, globs, globs)
except:
print(sys.exc_info()[1])
pdb.post_mortem(sys.exc_info()[2])
else:
# Note that %r is vital here. '%s' instead can, e.g., cause
# backslashes to get treated as metacharacters on Windows.
pdb.run("execfile(%r)" % srcfilename, globs, globs)
finally:
os.remove(srcfilename)
示例4: debug
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NameTemporaryFile [as 别名]
def debug(self, test, pm=False):
self.test = test
# Save the old stdout
self.save_stdout = sys.stdout
# Convert the source docstring to a script.
script = self._script_from_examples(test.docstring)
# Create a debugger.
debugger = _OutputRedirectingPdb(sys.stdout)
# Patch pdb.set_trace to restore sys.stdout during interactive
# debugging (so it's not still redirected to self._fakeout).
save_set_trace = pdb.set_trace
pdb.set_trace = debugger.set_trace
# Write the script to a temporary file. Note that
# tempfile.NameTemporaryFile() cannot be used. As the docs
# say, a file so created cannot be opened by name a second
# time on modern Windows boxes, and execfile() needs to open
# it.
srcfilename = tempfile.mktemp(".py", "doctestdebug_")
f = open(srcfilename, 'w')
f.write(script)
f.close()
# Set up the globals
test.globs['CHECK_OUTPUT'] = self._check_output
test.globs['CHECK_EXCEPTION'] = self._check_exception
test.globs['__print__'] = self._print_if_not_none
test.globs['__set_trace__'] = debugger.set_trace
test.globs['__examples__'] = self.test.examples
try:
if pm is False:
debugger.run("execfile(%r)" % srcfilename,
test.globs, test.globs)
else:
try:
sys.stdout = _SpoofOut()
try:
execfile(srcfilename, test.globs)
except bdb.BdbQuit:
return
except:
sys.stdout = self.save_stdout
exc_info = sys.exc_info()
exc_msg = traceback.format_exception_only(
exc_info[0], exc_info[1])[-1]
self.save_stdout.write(self.runner.DIVIDER+'\n')
self.save_stdout.write('Unexpected exception:\n' +
_indent(exc_msg))
raise
#self.post_mortem(debugger, exc_info[2])
finally:
sys.stdout = self.save_stdout
finally:
sys.set_trace = save_set_trace
os.remove(srcfilename)