我们从Python开源项目中,提取了以下28个代码示例,用于说明如何使用tempfile.NameTemporaryFile()。
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)
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)
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)