Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ModuleType.__repr__ can fail on py312+ #112414

Closed
Labels
3.12only security fixes3.13bugs and security fixestopic-importlibtype-bugAn unexpected behavior, bug, or error
@AlexWaygood

Description

@AlexWaygood

Bug report

Bug description:

Callingrepr() on aModuleType instance can fail withAttributeError on py312+, due to changes made in#98870. This is because the implementation ofimportlib._bootstrap._module_repr_from_spec assumes that all loaders will have a_path attribute. But if the module had a custom loader, that won't necessarily hold true:_path is an undocumented, internal, private attribute that CPython's import loaders have, but custom third-party loaders won't necessarily have.

An easy way to see this is the following (in a py312 venv withsetuptools installed):

Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct  2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>>from setuptools._vendorimport packaging>>>repr(packaging)Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "<frozen importlib._bootstrap>", line 545, in _module_repr  File "<frozen importlib._bootstrap>", line 830, in _module_repr_from_specAttributeError: 'VendorImporter' object has no attribute '_path'

setuptools implements the customVendorImporter loader here:https://github.com/pypa/setuptools/blob/b1bf87be7869df40872e947e27296ef87e3125ae/setuptools/extern/__init__.py#L5-L70. Instances ofVendorImporter do not have a_path attribute.

This kind of thing trips up external tools such as mypy'sstubtest.py script, which callsrepr() on arbitrary instances oftypes.ModuleType on certain code paths and (reasonably) expects that to always succeed:https://github.com/python/mypy/blob/1200d1d956e589a0a33c86ef8a7cb3f5a9b64f1f/mypy/stubtest.py#L224

Cc.@FFY00 and@jaraco, as participants in#98139 (the issue#98870 was linked to)

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13bugs and security fixestopic-importlibtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp