Skip to content

Commit

Permalink
Merge branch 'improve_namespace' into magics_runfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Peter committed Mar 30, 2023
2 parents 2e9bb31 + 64fa795 commit d10dfe8
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions spyder_kernels/customize/code_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,9 @@
from spyder_kernels.customize.utils import capture_last_Expr, canonic, create_pathlist


# UMR instance
__umr__ = UserModuleReloader(namelist=os.environ.get("SPY_UMR_NAMELIST", None))

# For logging
logger = logging.getLogger(__name__)

# Main constants
SHOW_GLOBAL_MSG = True
SHOW_INVALID_SYNTAX_MSG = True


def runfile_arguments(func):
"""Decorator to add runfile magic arguments to magic."""
Expand Down Expand Up @@ -152,6 +145,13 @@ class SpyderCodeRunner(Magics):
"""
Functions and magics related to code execution, debugging, profiling, etc.
"""
def __init__(self, *args, **kwargs):
self.show_global_msg = True
self.show_invalid_syntax_msg = True
self.umr = UserModuleReloader(
namelist=os.environ.get("SPY_UMR_NAMELIST", None)
)
super().__init__(*args, **kwargs)

@runfile_arguments
@needs_local_scope
Expand Down Expand Up @@ -377,8 +377,8 @@ def _exec_file(
"""
Execute a file.
"""
if __umr__.enabled:
__umr__.run()
if self.umr.enabled:
self.umr.run()
if args is not None and not isinstance(args, str):
raise TypeError("expected a character buffer object")

Expand Down Expand Up @@ -435,7 +435,7 @@ def _exec_file(
print("Working directory {} doesn't exist.\n".format(wdir))

try:
if __umr__.has_cython:
if self.umr.has_cython:
# Cython files
with io.open(filename, encoding="utf-8") as f:
self.shell.run_cell_magic("cython", "", f.read())
Expand Down Expand Up @@ -554,9 +554,6 @@ def _exec_code(
global_warning=False,
):
"""Execute code and display any exception."""
global SHOW_INVALID_SYNTAX_MSG
global SHOW_GLOBAL_MSG

if exec_fun is None:
exec_fun = exec

Expand All @@ -575,20 +572,20 @@ def _exec_code(
except SyntaxError:
raise e from None
else:
if SHOW_INVALID_SYNTAX_MSG:
if self.show_invalid_syntax_msg:
print(
"\nWARNING: This is not valid Python code. "
"If you want to use IPython magics, "
"flexible indentation, and prompt removal, "
"we recommend that you save this file with the "
".ipy extension.\n"
)
SHOW_INVALID_SYNTAX_MSG = False
self.show_invalid_syntax_msg = False
else:
ast_code = ast.parse(self._transform_cell(code))

# Print warning for global
if global_warning and SHOW_GLOBAL_MSG:
if global_warning and self.show_global_msg:
has_global = any(
isinstance(node, ast.Global) for node in ast.walk(ast_code)
)
Expand All @@ -602,7 +599,7 @@ def _exec_code(
"Configuration per file', if you want to capture the "
"namespace.\n"
)
SHOW_GLOBAL_MSG = False
self.show_global_msg = False

if code.rstrip()[-1] == ";":
# Supress output with ;
Expand Down

0 comments on commit d10dfe8

Please sign in to comment.