Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed test_get_package_tuple_compiled: AttributeError: module 'zlib' has no attribute '__file__' #495

Closed
hosiet opened this issue Jul 11, 2022 · 2 comments · Fixed by #500
Labels
affected-version:1.1 bug Unexpected or incorrect user-visible behavior
Milestone

Comments

@hosiet
Copy link

hosiet commented Jul 11, 2022

Describe the bug
When building rope package, test test_get_package_tuple_compiled fails on my system due to unrelated error (assertion on zlib module).

To Reproduce

After cloning the GitHub project, execute the following commands in the source dir:

python3 setup.py config
python3 setup.py build
python3 -m pytest

The following error will occur:

==================================== ERRORS ====================================
______________ ERROR at setup of test_get_package_tuple_compiled _______________

    @pytest.fixture
    def zlib_path():
        import zlib
    
>       yield pathlib.Path(zlib.__file__)
E       AttributeError: module 'zlib' has no attribute '__file__'

ropetest/contrib/autoimport/conftest.py:58: AttributeError

I am not sure why the test assumes that zlib.__file__ should exist:

% python3
Python 3.10.5 (main, Jun  8 2022, 09:26:22) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import zlib
>>> zlib.__file__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'zlib' has no attribute '__file__'. Did you mean: '__name__'?
>>> zlib
<module 'zlib' (built-in)>
>>>

Editor information (please complete the following information):

  • OS version: Debian Unstable/Sid
  • Python version: 3.10.5
  • Rope version: 1.2.0
@hosiet hosiet added the bug Unexpected or incorrect user-visible behavior label Jul 11, 2022
@lieryan
Copy link
Member

lieryan commented Jul 12, 2022

Thanks for writing this bug report this @hosiet. I was not really quite sure what really caused it, running the test on Linux/Ubuntu in Github Actions seems to be fine, but I can see this error on my local machine which is also using Ubuntu. I thought it was something quirky about my local setup but seems that I'm not the only one that sees this error, this means it might be something more widespread.

I think the reason for this error is because zlib may depend on how Python is compiled and installed on the distro. Sometimes, zlib may be compiled in as a builtin library, while other times, it's just a regular library. The way to fix this would likely be to just use a different library in the test.

@lieryan lieryan added this to the 1.3.0 milestone Jul 12, 2022
@eivindjahren
Copy link

eivindjahren commented Jul 16, 2022

The intention of the test seems to check that the autoimporter can get the info of a package with source.STANDARD and PackageType.COMPILED, hence the use of zlib. However, on my system, zlib is actually a builtin so this test fails in two ways. I suspect that all builtin packages don't have a __file__. So I suggest the following:

@pytest.fixture
def zlib_package():
    if "zlib" in sys.builtin_module_names:
        return Package("zlib", Source.BUILTIN, None, PackageType.BUILTIN)
    else:
        import zlib

        return Package(
            "zlib", Source.STANDARD, pathlib.Path(zlib.__file__), PackageType.COMPILED
        )


@pytest.mark.skipif(platform == "win32", reason="Windows doesn't have compiled modules")
def test_get_package_tuple_compiled(zlib_package):
    assert utils.get_package_tuple(zlib_path) == zlib_package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affected-version:1.1 bug Unexpected or incorrect user-visible behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants