From 8a188cff4334a02f241b1748dfe8d5d4b5f4f37e Mon Sep 17 00:00:00 2001 From: BLUELOVETH Date: Mon, 28 Aug 2023 13:17:58 +0800 Subject: [PATCH] ... --- src/vm.cpp | 19 +++++++++++++------ tests/test1/__init__.py | 7 ++++++- tests/test2/utils/r.py | 11 ++++++----- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/vm.cpp b/src/vm.cpp index 848c07b4..eb296095 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -217,6 +217,8 @@ namespace pkpy{ PyObject* VM::py_import(Str path, bool throw_err){ if(path.empty()) vm->ValueError("empty module name"); + + // std::cout << ">> py_import(" << path.escape() << ")" << std::endl; auto f_join = [](const std::vector& cpnts){ std::stringstream ss; @@ -243,18 +245,21 @@ namespace pkpy{ if(prefix > cpnts.size()) ImportError("attempted relative import beyond top-level package"); path = path.substr(prefix); // remove prefix for(int i=(int)curr_is_init; iattr().set(__package__, VAR(package)); // we do not allow override in order to avoid memory leak // it is because Module objects are not garbage collected - if(_modules.contains(name)) throw std::runtime_error("module already exists"); + if(_modules.contains(name)){ + throw std::runtime_error(fmt("module ", name.escape(), " already exists")); + } // convert to fullname and set it into _modules if(!package.empty()) name = package + "." + name; obj->attr().set(__path__, VAR(name)); diff --git a/tests/test1/__init__.py b/tests/test1/__init__.py index 35563054..eb5878c2 100644 --- a/tests/test1/__init__.py +++ b/tests/test1/__init__.py @@ -1 +1,6 @@ -from ._a import add \ No newline at end of file +from . import _a +add = _a.add + +from ._a import add as add2 + +assert add is add2 \ No newline at end of file diff --git a/tests/test2/utils/r.py b/tests/test2/utils/r.py index f3c6116e..da470d87 100644 --- a/tests/test2/utils/r.py +++ b/tests/test2/utils/r.py @@ -1,7 +1,8 @@ value = '123' -try: - from test2.a import g -except ImportError: - # circular import - pass \ No newline at end of file +# try: +# from test2.a import g +# exit(1) +# except ImportError: +# # circular import +# pass \ No newline at end of file