MCPcopy Create free account
hub / github.com/ipython/ipython / safe_execfile

Method safe_execfile

IPython/core/interactiveshell.py:2695–2761  ·  view source on GitHub ↗

A safe version of the builtin execfile(). This version will never throw an exception, but instead print helpful error messages to the screen. This only works on pure Python files with the .py extension. Parameters ---------- fname : string

(self, fname, *where, exit_ignore=False, raise_exceptions=False, shell_futures=False)

Source from the content-addressed store, hash-verified

2693 return eval(expr, self.user_global_ns, self.user_ns)
2694
2695 def safe_execfile(self, fname, *where, exit_ignore=False, raise_exceptions=False, shell_futures=False):
2696 """A safe version of the builtin execfile().
2697
2698 This version will never throw an exception, but instead print
2699 helpful error messages to the screen. This only works on pure
2700 Python files with the .py extension.
2701
2702 Parameters
2703 ----------
2704 fname : string
2705 The name of the file to be executed.
2706 where : tuple
2707 One or two namespaces, passed to execfile() as (globals,locals).
2708 If only one is given, it is passed as both.
2709 exit_ignore : bool (False)
2710 If True, then silence SystemExit for non-zero status (it is always
2711 silenced for zero status, as it is so common).
2712 raise_exceptions : bool (False)
2713 If True raise exceptions everywhere. Meant for testing.
2714 shell_futures : bool (False)
2715 If True, the code will share future statements with the interactive
2716 shell. It will both be affected by previous __future__ imports, and
2717 any __future__ imports in the code will affect the shell. If False,
2718 __future__ imports are not shared in either direction.
2719
2720 """
2721 fname = os.path.abspath(os.path.expanduser(fname))
2722
2723 # Make sure we can open the file
2724 try:
2725 with open(fname):
2726 pass
2727 except:
2728 warn('Could not open file <%s> for safe execution.' % fname)
2729 return
2730
2731 # Find things also in current directory. This is needed to mimic the
2732 # behavior of running a script from the system command line, where
2733 # Python inserts the script's directory into sys.path
2734 dname = os.path.dirname(fname)
2735
2736 with prepended_to_syspath(dname), self.builtin_trap:
2737 try:
2738 glob, loc = (where + (None, ))[:2]
2739 py3compat.execfile(
2740 fname, glob, loc,
2741 self.compile if shell_futures else None)
2742 except SystemExit as status:
2743 # If the call was made with 0 or None exit status (sys.exit(0)
2744 # or sys.exit() ), don't bother showing a traceback, as both of
2745 # these are considered normal by the OS:
2746 # > python -c'import sys;sys.exit(0)'; echo $?
2747 # 0
2748 # > python -c'import sys;sys.exit()'; echo $?
2749 # 0
2750 # For other exit status, we show the exception unless
2751 # explicitly silenced, but only in short form.
2752 if status.code:

Callers 3

_exec_fileMethod · 0.80
test_1Method · 0.80
editMethod · 0.80

Calls 2

showtracebackMethod · 0.95

Tested by 1

test_1Method · 0.64