本文整理匯總了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)