all_fault_handlers
¶
Fault Handlers API¶
Reorganizing this code to focus on setting up tracers, debuggers and formatters for exceptions.
The code that’s more important than anything should execute regardless
of whether someone has pip install
-ed it.
As a result, local imports or any imports not in the standard library
should be discouraged here.
Tip
A possible alternative to get_ipython().showsyntaxerror might
possibly be dis.distb()
.
Temporary Directories¶
Useful when you want to use in_tempdir
for the final test, but
you are still debugging. For example, you may want to do this in the end.:
>>> with in_tempdir() as tmpdir:
... # do something complicated which might break
... pass
But indeed the complicated thing does break, and meanwhile the
in_tempdir
context manager wiped out the directory with the
temporary files that you wanted for debugging. So, while debugging, you
replace with something like.:
>>> with in_dir() as tmpdir: # Use working directory by default
... # do something complicated which might break
... pass
You can then look at the temporary file outputs to debug what is happening,
fix, and finally replace in_dir
with in_tempdir
again.
-
formatted_tb
() → List[bytes][source][source]¶ Return a str of the last exception.
- Returns
- Return type
-
last_exc
() → AnyStr[source][source]¶ Return
traceback.format_exc
.
-
class
Fr
(frame)[source][source]¶ Bases:
object
Frames don’t define dict so vars doesnt work on it.
-
Frame attributes.
-
frame.f_back frame.f_lasti frame.f_trace_lines
-
frame.f_builtins frame.f_lineno frame.f_trace_opcodes
-
frame.f_code frame.f_locals
-
frame.f_globals frame.f_trace
-
-
find_exec_dir
() → Union[AnyStr, os.PathLike][source][source]¶ Returns IPython’s profile_dir.startup_dir. If that can’t be determined, return CWD.
-
safe_run_path
(fileobj: Union[AnyStr, os.PathLike], logger: Optional[logging.Logger] = None) → Union[str, os.PathLike][source][source]¶ Run a file with runpy.run_path and try to catch everything.
-
rerun_startup
() → Dict[source][source]¶ Rerun the files in the startup directory.
- Returns
ret – Namespace of all successful files.
- Return type
-
execfile
(filename: Union[AnyStr, os.PathLike], global_namespace: Optional[Mapping] = None, local_namespace: Optional[Mapping] = None)[source][source]¶ Bring execfile back from python2.
This function is similar to the
exec
statement, but parses a file instead of a string. It is different from theimport
statement in that it does not use the module administration — it reads the file unconditionally and does not create a new module.The arguments are a file name and two optional dictionaries. The file is parsed and evaluated as a sequence of Python statements (similarly to a module) using the globals and locals dictionaries as global and local namespace. If provided, locals can be any mapping object. Remember that at module level, globals and locals are the same dictionary. If two separate objects are passed as globals and locals, the code will be executed as if it were embedded in a class definition.
If the locals dictionary is omitted it defaults to the globals dictionary. If both dictionaries are omitted, the expression is executed in the environment where
execfile()
is called. The return value isNone
.Note
The default locals act as described for function
locals()
below: modifications to the default locals dictionary should not be attempted. Pass an explicit locals dictionary if you need to see effects of the code on locals after functionexecfile()
returns.execfile()
cannot be used reliably to modify a function’s locals.
-
ipy_execfile
(f: Union[AnyStr, os.PathLike])[source][source]¶ Run the IPython
%run
-i on a file.
-
ipy_execdir
(directory: Union[AnyStr, os.PathLike])[source][source]¶ Execute the python files in
directory
.The idea was to create a function that actually does what the function in this module
execfile
was trying to do. Because thatexecfile
executes everything in separate namespaces, it doesn’t get added into the user’slocals
, which is fairly pointless for interactive use.- Parameters
directory (str (os.Pathlike)) – Dir to execute.
-
tempdir
()[source][source]¶ Create and return a temporary directory. This has the same behavior as mkdtemp but can be used as a context manager.
Upon exiting the context, the directory and everything contained in it are removed.
Examples
>>> import os >>> with tempdir() as tmpdir: ... fname = os.path.join(tmpdir, 'example_file.txt') ... with open(fname, 'wt') as fobj: ... _ = fobj.write('a string\n') >>> os.path.exists(tmpdir) False
-
in_tempdir
()[source][source]¶ Create, return, and change directory to a temporary directory
Examples
>>> import os >>> my_cwd = os.getcwd() >>> with in_tempdir() as tmpdir: ... _ = open('test.txt', 'wt').write('some text') ... assert os.path.isfile('test.txt') ... assert os.path.isfile(os.path.join(tmpdir, 'test.txt')) >>> os.path.exists(tmpdir) False >>> os.getcwd() == my_cwd True
-
in_dir
(dir: Union[AnyStr, os.PathLike])[source][source]¶ Change directory to given directory for duration of
with
block.