@@ -41,6 +41,27 @@ def valuesname(name) -> str:
4141 return name [7 :- 6 ].lower ()
4242
4343
44+ def autoload_files (defs , root_dir_path : str , autoload_dir ):
45+ from mathics .core .evaluation import Evaluation
46+
47+ # Load symbols from the autoload folder
48+ for root , dirs , files in os .walk (os .path .join (root_dir_path , autoload_dir )):
49+ for path in [os .path .join (root , f ) for f in files if f .endswith (".m" )]:
50+ Expression ("Get" , String (path )).evaluate (Evaluation (defs ))
51+
52+ # Move any user definitions created by autoloaded files to
53+ # builtins, and clear out the user definitions list. This
54+ # means that any autoloaded definitions become shared
55+ # between users and no longer disappear after a Quit[].
56+ #
57+ # Autoloads that accidentally define a name in Global`
58+ # could cause confusion, so check for this.
59+ #
60+ for name in defs .user :
61+ if name .startswith ("Global`" ):
62+ raise ValueError ("autoload defined %s." % name )
63+
64+
4465class PyMathicsLoadException (Exception ):
4566 def __init__ (self , module ):
4667 self .name = module + " is not a valid pymathics module"
@@ -78,22 +99,17 @@ def __init__(
7899 contribute (self )
79100 for module in extension_modules :
80101 try :
81- loaded_module = self .load_pymathics_module (
82- module , remove_on_quit = False
83- )
84- except PyMathicsLoadException as e :
102+ self .load_pymathics_module (module , remove_on_quit = False )
103+ except PyMathicsLoadException :
85104 raise
86- except ImportError as e :
105+ except ImportError :
87106 raise
88107
89108 if builtin_filename is not None :
90109 builtin_file = open (builtin_filename , "wb" )
91110 pickle .dump (self .builtin , builtin_file , - 1 )
92111
93- # Load symbols from the autoload folder
94- for root , dirs , files in os .walk (os .path .join (ROOT_DIR , "autoload" )):
95- for path in [os .path .join (root , f ) for f in files if f .endswith (".m" )]:
96- Expression ("Get" , String (path )).evaluate (Evaluation (self ))
112+ autoload_files (self , ROOT_DIR , "autoload" )
97113
98114 # Move any user definitions created by autoloaded files to
99115 # builtins, and clear out the user definitions list. This
@@ -106,6 +122,7 @@ def __init__(
106122 for name in self .user :
107123 if name .startswith ("Global`" ):
108124 raise ValueError ("autoload defined %s." % name )
125+
109126 self .builtin .update (self .user )
110127 self .user = {}
111128 self .clear_cache ()
@@ -475,7 +492,7 @@ def get_definition(self, name, only_if_exists=False) -> "Definition":
475492 self .lookup_cache [original_name ] = name
476493 elif not only_if_exists :
477494 definition = Definition (name = name )
478- if name [- 1 ] != '`' :
495+ if name [- 1 ] != "`" :
479496 self .user [name ] = definition
480497
481498 return definition
0 commit comments