← 输入框|github.com|刷新
Skip to content

Implement import by using importlib #958

Closed

Description

@BenLewis-Seequent

The standard library for Python contains importlib, which contains a pure* Python implementation of the import machinery. This is lucky as we then don't have to implement it all in Rust. But we have to bootstrap it and implement all of the functionality it uses from builtin modules.

This is blocked by the following:

  • compile should accept bytes as well as strings
  • _os.fspath - https://docs.python.org/3/library/os.html#os.fspath
  • sys.implementation.cache_tag - This just needs to exist we don't support caching bytecode at the moment
  • sys.pycache_prefix - same as above
  • sys.flags
  • str.endswith needs to support tuple argument
  • _io.FileIO __enter__ and __exit__ methods
  • sys.platform
  • os.getcwd
  • os.stat st_mtime attribute
  • _warnings module
  • possibly more

* Uses built-in module _imp to actually create module objects.

Activity

  1. palaviv commented on Jun 5, 2019

    @palaviv
    Contributor

    An initial implementation can be found in #1018.

  2. palaviv commented on Jun 7, 2019

    @palaviv
    Contributor

    Another thing that would help this is supporting {!r} in string format.

  3. palaviv commented on Jun 18, 2019

    @palaviv
    Contributor

    This can be closed not that #1018 was merged

  4. apatrushev commented on Jun 18, 2019

    @apatrushev

    UPD: Related to #55

    level and globals argument is not provided and relative import is not working.
    As example import unittest is broken at:

    from .result import TestResult

    Example:

    module/__init__.py:

    from .second import value

    module/second.py:

    value = 1

    Expected result

    python -c 'import module; print(module.value)'
    1
    

    Current result

    cargo run -- -c 'import module; print(module.value)'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "None", line 1093, in __import__
      File "None", line 1014, in _gcd_import
      File "None", line 991, in _find_and_load
      File "None", line 975, in _find_and_load_unlocked
      File "None", line 686, in _load_unlocked
      File "None", line 677, in _load_unlocked
      File "None", line 671, in _load_unlocked
      File "None", line 778, in exec_module
      File "None", line 219, in _call_with_frames_removed
      File ".../RustPython/module/__init__.py", line 1, in <module>
        from .second import value
      File "None", line 1093, in __import__
      File "None", line 1014, in _gcd_import
      File "None", line 991, in _find_and_load
      File "None", line 973, in _find_and_load_unlocked
    ModuleNotFoundError: No module named '.second'
    
  5. apatrushev commented on Jun 21, 2019

    @apatrushev

    Additional investigation - CPython does level magic in compile:

    python -c "import dis; dis.dis(compile('from .second import value', '', 'exec'))"
      1           0 LOAD_CONST               0 (1)
                  2 LOAD_CONST               1 (('value',))
                  4 IMPORT_NAME              0 (second)
                  6 IMPORT_FROM              1 (value)
                  8 STORE_NAME               1 (value)
    [...skipped...]
    

    (note second dot)

    python -c "import dis; dis.dis(compile('from ..second import value', '', 'exec'))"
      1           0 LOAD_CONST               0 (2)
                  2 LOAD_CONST               1 (('value',))
                  4 IMPORT_NAME              0 (second)
                  6 IMPORT_FROM              1 (value)
                  8 STORE_NAME               1 (value)
    [...skipped...]
    
  6. palaviv commented on Jun 21, 2019

    @palaviv
    Contributor

    @apatrushev please look at the implementation of the relative import at #1050.

  7. apatrushev commented on Jun 21, 2019

    @apatrushev

    @apatrushev please look at the implementation of the relative import at #1050.

    I took a short look on the code in PR. Unfortunately I have no strong opinion on where to place this code in compile or in vm execution because I am not confident neither in CPython code nor in RustPython. And I have no idea why CPython doing this in compile time - I just put my investigations here for the history to save the time later.

    Besides this disclaimer your code LGTM (and import unittest moved forward!).

  8. palaviv commented on Jun 30, 2019

    @palaviv
    Contributor

    @Skinny121, @apatrushev can we close this issue?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-compatA discrepancy between RustPython and CPython

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions