From 4c4573bc4b50274bafe7872b23a560cfdccbb6df Mon Sep 17 00:00:00 2001 From: James Hutchison <122519877+JamesHutchison@users.noreply.github.com> Date: Thu, 18 Sep 2025 19:26:23 +0000 Subject: [PATCH 1/7] consolidate on intiialized instead of initializing --- www/src/Lib/importlib/util.py | 4 ++-- www/src/Lib/pydoc.py | 1 - www/src/Lib/test/test_pkg.py | 4 ++-- www/src/py_import.js | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/www/src/Lib/importlib/util.py b/www/src/Lib/importlib/util.py index 8623c8984..d98fa9448 100644 --- a/www/src/Lib/importlib/util.py +++ b/www/src/Lib/importlib/util.py @@ -127,7 +127,7 @@ def _module_to_load(name): module = type(sys)(name) # This must be done before putting the module in sys.modules # (otherwise an optimization shortcut in import.c becomes wrong) - module.__initializing__ = True + module.__initialized__ = False sys.modules[name] = module try: yield module @@ -138,7 +138,7 @@ def _module_to_load(name): except KeyError: pass finally: - module.__initializing__ = False + module.__initialized__ = True def set_package(fxn): diff --git a/www/src/Lib/pydoc.py b/www/src/Lib/pydoc.py index 84e673a7f..e24667cf8 100644 --- a/www/src/Lib/pydoc.py +++ b/www/src/Lib/pydoc.py @@ -276,7 +276,6 @@ def _split_list(s, predicate): def visiblename(name, all=None, obj=None): """Decide whether to show documentation on a variable.""" # Certain special names are redundant or internal. - # XXX Remove __initializing__? if name in {'__author__', '__builtins__', '__cached__', '__credits__', '__date__', '__doc__', '__file__', '__spec__', '__loader__', '__module__', '__name__', '__package__', diff --git a/www/src/Lib/test/test_pkg.py b/www/src/Lib/test/test_pkg.py index eed0fd1c6..004cda19e 100644 --- a/www/src/Lib/test/test_pkg.py +++ b/www/src/Lib/test/test_pkg.py @@ -22,8 +22,8 @@ def cleanout(root): def fixdir(lst): if "__builtins__" in lst: lst.remove("__builtins__") - if "__initializing__" in lst: - lst.remove("__initializing__") + if "__initialized__" in lst: + lst.remove("__initialized__") return lst diff --git a/www/src/py_import.js b/www/src/py_import.js index a1cc58047..d5ae35880 100644 --- a/www/src/py_import.js +++ b/www/src/py_import.js @@ -332,7 +332,7 @@ function run_py(module_contents, path, module, compiled) { for(let attr in mod){ module[attr] = mod[attr] } - module.__initializing__ = false + module.__initialized__ = true // $B.imported[mod.__name__] must be the module object, so that // setting attributes in a program affects the module namespace // See issue #7 From 36cece2f1ed71a9fbce4de85730e26c282d65443 Mon Sep 17 00:00:00 2001 From: James Hutchison <122519877+JamesHutchison@users.noreply.github.com> Date: Thu, 18 Sep 2025 19:30:17 +0000 Subject: [PATCH 2/7] assign both file and path --- www/src/py_import.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/src/py_import.js b/www/src/py_import.js index d5ae35880..0be6ce020 100644 --- a/www/src/py_import.js +++ b/www/src/py_import.js @@ -509,7 +509,7 @@ VFSLoader.exec_module = function(self, modobj){ elts.pop() mod.__package__ = elts.join(".") } - mod.__file__ = path + mod.__file__ = mod.__path__ = path try{ var parent_id = parent.replace(/\./g, "_"), prefix = 'locals_' From 779e5cf3f83b7fc83dd8da31dc5dbf861b3f57c9 Mon Sep 17 00:00:00 2001 From: James Hutchison <122519877+JamesHutchison@users.noreply.github.com> Date: Thu, 18 Sep 2025 20:59:46 +0000 Subject: [PATCH 3/7] Replace path check with file or path --- www/src/py_import.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/www/src/py_import.js b/www/src/py_import.js index 0be6ce020..a115e03a4 100644 --- a/www/src/py_import.js +++ b/www/src/py_import.js @@ -1093,11 +1093,15 @@ $B.$__import__ = function(mod_name, globals, locals, fromlist){ _b_.setattr($B.imported[_parent_name], parsed_name[i], $B.imported[_mod_name]) } - // [Import spec] If __path__ can not be accessed an ImportError is raised + // Note: Brython doesn't follow Python convention and every module should have __file__ + // __path__ should be redundant but kept in as precaution + // [Import spec] Module check if(i < len){ - try{ - __path__ = $B.$getattr($B.imported[_mod_name], "__path__") - }catch(e){ + if ( + $B.$getattr($B.imported[_mod_name], "__file__", + $B.$getattr($B.imported[_mod_name], "__path__", $B.None)) + === $B.None + ) { // If this is the last but one part, and the last part is // an attribute of module, and this attribute is a module, // return it. This is the case for os.path for instance From 27c1357ebea82cbfc7d128ff3b21102c9b8e60a5 Mon Sep 17 00:00:00 2001 From: James Hutchison <122519877+JamesHutchison@users.noreply.github.com> Date: Thu, 18 Sep 2025 21:09:59 +0000 Subject: [PATCH 4/7] revert setting path (__file__ and __path__ aren't the same thing / type) --- www/src/py_import.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/src/py_import.js b/www/src/py_import.js index a115e03a4..513f03184 100644 --- a/www/src/py_import.js +++ b/www/src/py_import.js @@ -509,7 +509,7 @@ VFSLoader.exec_module = function(self, modobj){ elts.pop() mod.__package__ = elts.join(".") } - mod.__file__ = mod.__path__ = path + mod.__file__ = path try{ var parent_id = parent.replace(/\./g, "_"), prefix = 'locals_' From 6a6a6786c7a4bc82c0857015e39223192d8066b9 Mon Sep 17 00:00:00 2001 From: James Hutchison <122519877+JamesHutchison@users.noreply.github.com> Date: Thu, 18 Sep 2025 22:34:40 +0000 Subject: [PATCH 5/7] user prior logic here to fix issue with unfound imports --- www/src/py_import.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/www/src/py_import.js b/www/src/py_import.js index 513f03184..82fef92e0 100644 --- a/www/src/py_import.js +++ b/www/src/py_import.js @@ -1097,11 +1097,9 @@ $B.$__import__ = function(mod_name, globals, locals, fromlist){ // __path__ should be redundant but kept in as precaution // [Import spec] Module check if(i < len){ - if ( - $B.$getattr($B.imported[_mod_name], "__file__", - $B.$getattr($B.imported[_mod_name], "__path__", $B.None)) - === $B.None - ) { + try { + __path__ = $B.$getattr($B.imported[_mod_name], "__path__") + } catch(e) { // If this is the last but one part, and the last part is // an attribute of module, and this attribute is a module, // return it. This is the case for os.path for instance From efe449791c81abcd12b1fe1623867712bb482eb9 Mon Sep 17 00:00:00 2001 From: James Hutchison <122519877+JamesHutchison@users.noreply.github.com> Date: Thu, 18 Sep 2025 22:35:43 +0000 Subject: [PATCH 6/7] revert whitespace change to match rest of file --- www/src/py_import.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/www/src/py_import.js b/www/src/py_import.js index 82fef92e0..035aa4b2d 100644 --- a/www/src/py_import.js +++ b/www/src/py_import.js @@ -1097,9 +1097,9 @@ $B.$__import__ = function(mod_name, globals, locals, fromlist){ // __path__ should be redundant but kept in as precaution // [Import spec] Module check if(i < len){ - try { + try{ __path__ = $B.$getattr($B.imported[_mod_name], "__path__") - } catch(e) { + }catch(e){ // If this is the last but one part, and the last part is // an attribute of module, and this attribute is a module, // return it. This is the case for os.path for instance From 91f70dc2ed57284cb8e6ee51ac340725d7b176bb Mon Sep 17 00:00:00 2001 From: James Hutchison <122519877+JamesHutchison@users.noreply.github.com> Date: Thu, 18 Sep 2025 16:27:21 -0700 Subject: [PATCH 7/7] Remove obsolete comment from this PR --- www/src/py_import.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/www/src/py_import.js b/www/src/py_import.js index 035aa4b2d..a2e141fbf 100644 --- a/www/src/py_import.js +++ b/www/src/py_import.js @@ -1093,8 +1093,6 @@ $B.$__import__ = function(mod_name, globals, locals, fromlist){ _b_.setattr($B.imported[_parent_name], parsed_name[i], $B.imported[_mod_name]) } - // Note: Brython doesn't follow Python convention and every module should have __file__ - // __path__ should be redundant but kept in as precaution // [Import spec] Module check if(i < len){ try{