Skip to content

Commit

Permalink
Fix xontrib unloading
Browse files Browse the repository at this point in the history
Refs   #724.
  • Loading branch information
evhub committed Mar 30, 2023
1 parent 0b75f88 commit 0b4cc09
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
12 changes: 6 additions & 6 deletions coconut/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ def get_bool_env_var(env_var, default=False):
# min versions are inclusive
min_versions = {
"cPyparsing": (2, 4, 7, 1, 2, 0),
("pre-commit", "py3"): (2, 21),
("pre-commit", "py3"): (3,),
"psutil": (5,),
"jupyter": (1, 0),
"types-backports": (0, 1),
Expand All @@ -860,11 +860,11 @@ def get_bool_env_var(env_var, default=False):
("numpy", "py2;cpy"): (1,),
("dataclasses", "py==36"): (0, 8),
("aenum", "py<34"): (3,),
"pydata-sphinx-theme": (0, 12),
"myst-parser": (0, 18),
"sphinx": (5, 3), # don't upgrade until myst-parser works with it
"mypy[python2]": (0, 991),
("jupyter-console", "py36"): (6, 4),
"pydata-sphinx-theme": (0, 13),
"myst-parser": (1,),
"sphinx": (6,),
"mypy[python2]": (1, 1),
("jupyter-console", "py36"): (6, 6),
("typing", "py<35"): (3, 10),
("jedi", "py37"): (0, 18),

Expand Down
20 changes: 19 additions & 1 deletion coconut/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ def magic(line, cell=None):

class CoconutXontribLoader(object):
"""Implements Coconut's _load_xontrib_."""
timing_info = []
compiler = None
runner = None

def __init__(self):
self.timing_info = []

def __call__(self, xsh, **kwargs):
# hide imports to avoid circular dependencies
from coconut.exceptions import CoconutException
Expand Down Expand Up @@ -121,14 +123,30 @@ def new_parse(execer, s, *args, **kwargs):
return execer.__class__.parse(execer, s, *args, **kwargs)

main_parser = xsh.execer.parser
main_parser._coconut_old_parse = main_parser.parse
main_parser.parse = MethodType(new_parse, main_parser)

ctx_parser = xsh.execer.ctxtransformer.parser
ctx_parser._coconut_old_parse = ctx_parser.parse
ctx_parser.parse = MethodType(new_parse, ctx_parser)

self.timing_info.append(("load", get_clock_time() - start_time))

return self.runner.vars

def unload(self, xsh):
# import here to avoid circular dependencies
from coconut.exceptions import CoconutException

main_parser = xsh.execer.parser
if not hasattr(main_parser, "_coconut_old_parse"):
raise CoconutException("attempting to unldoad Coconut xontrib but it was never loaded")
main_parser.parser = main_parser._coconut_old_parse

ctx_parser = xsh.execer.ctxtransformer.parser
ctx_parser.parse = ctx_parser._coconut_old_parse


_load_xontrib_ = CoconutXontribLoader()

_unload_xontrib_ = _load_xontrib_.unload
2 changes: 1 addition & 1 deletion coconut/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
VERSION = "3.0.0"
VERSION_NAME = None
# False for release, int >= 1 for develop
DEVELOP = 17
DEVELOP = 18
ALPHA = True # for pre releases rather than post releases

# -----------------------------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion xontrib/coconut.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from coconut.root import * # NOQA

from coconut.integrations import _load_xontrib_
from coconut.integrations import _load_xontrib_, _unload_xontrib_ # NOQA

# -----------------------------------------------------------------------------------------------------------------------
# MAIN:
Expand Down

0 comments on commit 0b4cc09

Please sign in to comment.