Skip to content

Fix test_import: import machinery, circular imports, and script shadowing#7034

Draft
youknowone wants to merge 7 commits intoRustPython:mainfrom
youknowone:test_import
Draft

Fix test_import: import machinery, circular imports, and script shadowing#7034
youknowone wants to merge 7 commits intoRustPython:mainfrom
youknowone:test_import

Conversation

@youknowone
Copy link
Member

  • Emit IMPORT_FROM instead of LOAD_ATTR for import a.b.c as m (compile.rs)
  • Add "partially initialized module" error in module getattr for circular imports
  • Add "cannot access submodule" error for initializing submodules
  • Implement "consider renaming" script shadowing detection (module.rs, frame.rs)
    • Detect when a user script shadows a stdlib or third-party module
    • Compute original sys.path[0] from sys.argv[0] (like CPython's config->sys_path_0)
    • Check sys.stdlib_module_names for stdlib module detection
    • Respect safe_path setting
  • Implement _imp._fix_co_filename to rewrite code object source_path in-place
  • Add data parameter support to _imp.get_frozen_object with marshal deserialization
  • Fix import_from error messages: check spec.has_location before using origin
  • Set ImportError.path attribute on import failures
  • Fix import_star error messages for non-str items in all and dict
  • Always call builtins.import (remove sys.modules cache bypass in import_inner)

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 7, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

  • 🔍 Trigger a full review
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 7, 2026

Code has been automatically formatted

The code in this PR has been formatted using:

  • cargo fmt --all
    Please pull the latest changes before pushing again:
git pull origin test_import

…wing

- Emit IMPORT_FROM instead of LOAD_ATTR for `import a.b.c as m` (compile.rs)
- Add "partially initialized module" error in module getattr for circular imports
- Add "cannot access submodule" error for initializing submodules
- Implement "consider renaming" script shadowing detection (module.rs, frame.rs)
  - Detect when a user script shadows a stdlib or third-party module
  - Compute original sys.path[0] from sys.argv[0] (like CPython's config->sys_path_0)
  - Check sys.stdlib_module_names for stdlib module detection
  - Respect safe_path setting
- Implement _imp._fix_co_filename to rewrite code object source_path in-place
- Add data parameter support to _imp.get_frozen_object with marshal deserialization
- Fix import_from error messages: check __spec__.has_location before using origin
- Set ImportError.path attribute on import failures
- Fix import_star error messages for non-str items in __all__ and __dict__
- Always call builtins.__import__ (remove sys.modules cache bypass in import_inner)
@youknowone youknowone force-pushed the test_import branch 2 times, most recently from 0c03ef7 to 3bcc98b Compare February 8, 2026 03:14
…egistration

- Look up encoding submodules in sys.modules after import, since
  __import__("encodings.ascii") returns the top-level "encodings" package
- Remove @expectedfailure from test_frozen_submodule_in_unfrozen_package
  and test_unfrozen_submodule_in_frozen_package (now passing)
- Add __hello_only__.py frozen module source (was untracked)
…indows-only tests

- Use to_str() instead of as_str() in import_module_level and
  get_spec_origin to avoid panic on strings with surrogates
- Add @expectedfailure for test_dll_dependency_import (no C extension),
  test_unencodable_filename (subprocess), test_tagged_suffix (no .pyd)
…lookup

Change import_module_level signature from &PyStr to &Py<PyStr> to enable
direct DictKey usage for sys.modules lookup. When module name contains
surrogates (e.g. from Windows unencodable filenames), fall back to
sys.modules lookup using the original Python string key, preserving
surrogates for correct hash/equality matching.
normalize_encoding_name maps "utf-8" to "utf_8" but leaves "utf8" as-is,
so codecs.lookup("utf8") misses the cache when only "utf_8" is registered.
The full encodings.search_function handles this via aliases, but register
the common "utf8" alias during bootstrap as a safety net.
@youknowone youknowone force-pushed the test_import branch 2 times, most recently from 1f78b06 to 294d609 Compare February 8, 2026 11:44
@github-actions
Copy link
Contributor

github-actions bot commented Feb 8, 2026

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

(module 'hello_only' not found)

[ ] lib: cpython/Lib/argparse.py
[ ] test: cpython/Lib/test/test_argparse.py

dependencies:

  • argparse (native: sys)
    • gettext, os, re

dependent tests: (3 tests)

  • argparse: test_argparse
    • ensurepip._uninstall: test_ensurepip
    • sqlite3.main: test_sqlite3

[ ] lib: cpython/Lib/configparser.py
[ ] test: cpython/Lib/test/test_configparser.py (TODO: 1)

dependencies:

  • configparser (native: collections.abc, itertools, sys)
    • collections (native: _weakref, itertools, sys)
    • io (native: _io, _thread, errno, sys)
    • contextlib, functools, os, re

dependent tests: (2 tests)

  • configparser: test_configparser test_logging

[ ] lib: cpython/Lib/dataclasses.py
[ ] test: cpython/Lib/test/test_dataclasses (TODO: 10)

dependencies:

  • dataclasses (native: itertools, sys)
    • annotationlib (native: builtins, sys)
    • copy
    • inspect (native: builtins, collections.abc, importlib.machinery, itertools, sys)
    • types
    • abc, keyword, re, reprlib

dependent tests: (6 tests)

  • dataclasses: test__colorize test_genericalias test_patma test_pprint test_regrtest test_zoneinfo

[ ] lib: cpython/Lib/difflib.py
[ ] test: cpython/Lib/test/test_difflib.py

dependencies:

  • difflib
    • collections, types
    • heapq

dependent tests: (4 tests)

  • difflib: test_difflib test_genericalias test_sys_settrace test_unittest

[ ] lib: cpython/Lib/getopt.py
[ ] test: cpython/Lib/test/test_getopt.py

dependencies:

  • getopt
    • gettext, os

dependent tests: (1 tests)

  • getopt: test_getopt

[ ] lib: cpython/Lib/getpass.py
[ ] test: cpython/Lib/test/test_getpass.py

dependencies:

  • getpass (native: sys)
    • io
    • contextlib, os

dependent tests: (1 tests)

  • getpass: test_getpass

[ ] lib: cpython/Lib/inspect.py
[ ] test: cpython/Lib/test/test_inspect (TODO: 53)

dependencies:

  • inspect

dependent tests: (41 tests)

  • inspect: test_abc test_argparse test_asyncgen test_builtin test_code test_collections test_coroutines test_decimal test_enum test_functools test_generators test_grammar test_inspect test_ntpath test_operator test_patma test_posixpath test_signal test_traceback test_types test_typing test_unittest test_yield_from test_zipimport
    • asyncio: test_asyncio test_contextlib_async test_logging test_os test_sys_settrace test_unittest
    • bdb: test_bdb
    • dataclasses: test__colorize test_genericalias test_pprint test_regrtest test_zoneinfo
    • importlib.metadata: test_importlib
    • pydoc:
      • xmlrpc.server: test_docxmlrpc test_xmlrpc
    • rlcompleter: test_rlcompleter
    • trace: test_trace

[ ] test: cpython/Lib/test/test___all__.py (TODO: 1)

dependencies:

dependent tests: (no tests depend on all)

[ ] test: cpython/Lib/test/test_binop.py

dependencies:

dependent tests: (no tests depend on binop)

[ ] test: cpython/Lib/test/test_contains.py

dependencies:

dependent tests: (no tests depend on contains)

[x] test: cpython/Lib/test/test_dict.py (TODO: 6)
[ ] test: cpython/Lib/test/test_dictcomps.py (TODO: 1)
[ ] test: cpython/Lib/test/test_dictviews.py (TODO: 2)
[ ] test: cpython/Lib/test/test_userdict.py

dependencies:

dependent tests: (no tests depend on dict)

[x] test: cpython/Lib/test/test_exceptions.py (TODO: 25)
[ ] test: cpython/Lib/test/test_baseexception.py
[x] test: cpython/Lib/test/test_except_star.py (TODO: 1)
[ ] test: cpython/Lib/test/test_exception_group.py (TODO: 1)
[x] test: cpython/Lib/test/test_exception_hierarchy.py (TODO: 2)
[x] test: cpython/Lib/test/test_exception_variations.py

dependencies:

dependent tests: (no tests depend on exception)

[x] test: cpython/Lib/test/test_frozen.py

dependencies:

dependent tests: (no tests depend on frozen)

[ ] test: cpython/Lib/test/test_import (TODO: 3)

dependencies:

dependent tests: (no tests depend on import)

[x] lib: cpython/Lib/importlib
[ ] test: cpython/Lib/test/test_importlib (TODO: 16)

dependencies:

  • importlib

dependent tests: (66 tests)

  • importlib: test_bdb test_cmd_line_script test_codecs test_compileall test_ctypes test_doctest test_frozen test_hashlib test_importlib test_inspect test_linecache test_multiprocessing_main_handling test_pkgutil test_py_compile test_reprlib test_runpy test_sundry test_support test_unittest test_zipfile test_zipimport test_zoneinfo
    • ensurepip: test_ensurepip test_venv
    • inspect: test_abc test_argparse test_asyncgen test_builtin test_code test_collections test_coroutines test_decimal test_enum test_functools test_generators test_grammar test_ntpath test_operator test_patma test_posixpath test_signal test_traceback test_types test_typing test_unittest test_yield_from
      • asyncio: test_asyncio test_contextlib_async test_logging test_os test_sys_settrace test_unittest
      • dataclasses: test__colorize test_genericalias test_pprint test_regrtest
      • rlcompleter: test_rlcompleter
      • trace: test_trace
      • xmlrpc.server: test_docxmlrpc test_xmlrpc
    • py_compile: test_importlib
    • pyclbr: test_pyclbr
    • zipfile: test_shutil test_zipapp test_zipfile test_zipfile64

[x] lib: cpython/Lib/pickle.py
[ ] lib: cpython/Lib/_compat_pickle.py
[ ] test: cpython/Lib/test/test_pickle.py (TODO: 22)
[ ] test: cpython/Lib/test/test_picklebuffer.py (TODO: 12)
[ ] test: cpython/Lib/test/test_pickletools.py (TODO: 8)

dependencies:

  • pickle (native: itertools, sys)
    • _compat_pickle
    • _compat_pickle
    • io, types
    • codecs, copyreg, functools, struct

dependent tests: (70 tests)

  • pickle: test_annotationlib test_array test_asyncio test_builtin test_bytes test_bz2 test_codecs test_collections test_concurrent_futures test_coroutines test_csv test_ctypes test_decimal test_defaultdict test_deque test_descr test_dict test_dictviews test_email test_enum test_enumerate test_exceptions test_fractions test_functools test_generators test_genericalias test_http_cookies test_importlib test_inspect test_io test_ipaddress test_iter test_itertools test_list test_logging test_lzma test_memoryio test_memoryview test_opcache test_operator test_ordered_dict test_os test_pathlib test_pickle test_pickletools test_platform test_plistlib test_positional_only_arg test_posix test_random test_range test_set test_shelve test_slice test_socket test_statistics test_str test_string test_trace test_tuple test_types test_typing test_unittest test_uuid test_xml_dom_minicompat test_xml_etree test_zipfile test_zlib test_zoneinfo
    • logging.handlers: test_concurrent_futures

[x] lib: cpython/Lib/reprlib.py
[x] test: cpython/Lib/test/test_reprlib.py (TODO: 4)

dependencies:

  • reprlib

dependent tests: (162 tests)

  • reprlib: test_reprlib
    • collections: test_annotationlib test_array test_asyncio test_bisect test_builtin test_c_locale_coercion test_call test_collections test_configparser test_contains test_csv test_ctypes test_defaultdict test_deque test_dict test_dictviews test_enum test_exception_group test_file test_fileinput test_fileio test_functools test_genericalias test_hash test_httpservers test_inspect test_io test_ipaddress test_iter test_iterlen test_json test_ordered_dict test_pathlib test_patma test_pickle test_plistlib test_pprint test_random test_set test_shelve test_sqlite3 test_statistics test_string test_struct test_traceback test_types test_typing test_unittest test_urllib test_userdict test_userlist test_userstring test_weakref test_weakset test_with
      • concurrent.futures._base: test_concurrent_futures
      • configparser: test_logging
      • dbm.dumb: test_dbm_dumb
      • dbm.sqlite3: test_dbm_sqlite3
      • difflib: test_difflib test_sys_settrace
      • dis: test__opcode test_ast test_code test_compile test_compiler_assemble test_dis test_dtrace test_fstring test_opcache test_peepholer test_positional_only_arg
      • http.client: test_docxmlrpc test_hashlib test_ssl test_ucn test_unicodedata test_urllib2 test_wsgiref test_xmlrpc
      • importlib.metadata: test_importlib test_zoneinfo
      • inspect: test_abc test_argparse test_asyncgen test_coroutines test_decimal test_generators test_grammar test_ntpath test_operator test_posixpath test_signal test_yield_from test_zipimport
      • multiprocessing: test_asyncio test_concurrent_futures test_fcntl test_multiprocessing_main_handling
      • pkgutil: test_pkgutil
      • platform: test__locale test__osx_support test_android test_asyncio test_baseexception test_cmath test_ctypes test_math test_mimetypes test_os test_platform test_posix test_regrtest test_socket test_sysconfig test_time test_winreg
      • pprint: test_htmlparser test_sys_setprofile
      • queue: test_asyncio test_concurrent_futures test_dummy_thread test_sched
      • selectors: test_selectors test_subprocess
      • shutil: test_bz2 test_compileall test_ctypes test_filecmp test_glob test_importlib test_largefile test_py_compile test_shutil test_site test_string_literals test_support test_tempfile test_venv
      • tokenize: test_linecache test_tabnanny test_tokenize test_unparse
      • traceback: test_asyncio test_code_module test_contextlib test_contextlib_async test_importlib test_listcomps test_pyexpat test_setcomps test_threadedtempfile test_threading test_unittest
      • urllib.parse: test_sqlite3 test_urllib2_localnet test_urllibnet test_urlparse
      • urllib.robotparser: test_robotparser
      • wave: test_wave
    • dataclasses: test__colorize

[ ] test: cpython/Lib/test/test_sort.py (TODO: 2)

dependencies:

dependent tests: (no tests depend on sort)

[x] lib: cpython/Lib/threading.py
[x] lib: cpython/Lib/_threading_local.py
[ ] test: cpython/Lib/test/test_threading.py (TODO: 20)
[ ] test: cpython/Lib/test/test_threadedtempfile.py
[ ] test: cpython/Lib/test/test_threading_local.py (TODO: 3)

dependencies:

  • threading

dependent tests: (109 tests)

  • threading: test_android test_asyncio test_bz2 test_code test_concurrent_futures test_contextlib test_ctypes test_decimal test_docxmlrpc test_email test_enum test_fork1 test_ftplib test_functools test_hashlib test_httplib test_httpservers test_imaplib test_importlib test_io test_itertools test_largefile test_logging test_opcache test_poll test_queue test_robotparser test_sched test_signal test_smtplib test_socket test_socketserver test_sqlite3 test_ssl test_subprocess test_super test_syslog test_termios test_threadedtempfile test_threading test_threading_local test_time test_urllib2_localnet test_weakref test_winreg test_wsgiref test_xmlrpc test_zstd
    • asyncio: test_asyncio test_contextlib_async test_inspect test_os test_sys_settrace test_unittest
    • concurrent.futures._base: test_concurrent_futures
    • concurrent.futures.process: test_concurrent_futures
    • concurrent.futures.thread: test_genericalias
    • http.cookiejar: test_http_cookiejar
    • logging: test_support test_unittest
      • venv: test_venv
    • multiprocessing: test_fcntl test_multiprocessing_main_handling
    • queue: test_dummy_thread
    • subprocess: test_audit test_c_locale_coercion test_cmd_line test_cmd_line_script test_ctypes test_dtrace test_faulthandler test_file_eintr test_gzip test_json test_msvcrt test_ntpath test_osx_env test_platform test_plistlib test_py_compile test_regrtest test_repl test_runpy test_script_helper test_select test_shutil test_site test_sys test_sysconfig test_tempfile test_unittest test_urllib2 test_utf8_mode test_wait3 test_webbrowser test_zipfile
      • ctypes.util: test_ctypes
      • ensurepip: test_ensurepip
    • sysconfig: test_pyexpat test_tools test_urllib2net
      • trace: test_trace
    • zipfile: test_pkgutil test_zipapp test_zipfile test_zipfile64 test_zipimport
      • importlib.metadata: test_importlib test_zoneinfo

[ ] lib: cpython/Lib/tomllib
[ ] test: cpython/Lib/test/test_tomllib

dependencies:

  • tomllib
    • datetime (native: _thread, math, sys, time)
    • typing (native: _typing, collections.abc, sys)
    • types
    • future, functools, re

dependent tests: (no tests depend on tomllib)

[x] test: cpython/Lib/test/test_list.py (TODO: 5)
[x] test: cpython/Lib/test/test_listcomps.py (TODO: 1)
[x] test: cpython/Lib/test/test_userlist.py

dependencies:

dependent tests: (no tests depend on list)

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant