Skip to content

gh-138542: error message is wrong when inspect a custom module#143769

Open
yihong0618 wants to merge 11 commits into
python:mainfrom
yihong0618:pr-138543
Open

gh-138542: error message is wrong when inspect a custom module#143769
yihong0618 wants to merge 11 commits into
python:mainfrom
yihong0618:pr-138543

Conversation

@yihong0618

@yihong0618 yihong0618 commented Jan 13, 2026

Copy link
Copy Markdown
Contributor

My fault that deleted my fork for cpython most of my patch auto close this can be restore because it had been
approved the old patch link #138543

yihong0618 and others added 8 commits September 5, 2025 23:19
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Co-authored-by: sobolevn <mail@sobolevn.me>
Co-authored-by: sobolevn <mail@sobolevn.me>
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
Comment thread Lib/inspect.py Outdated
return object.__file__
raise TypeError('{!r} is a built-in module'.format(object))
if getattr(object, '__spec__', None) is not None:
raise TypeError(f'{object!r} is a built-in module')

@caje731 caje731 Feb 11, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the existence of spec does not always guarantee a built-in. The module could also be a pure-Python module loaded from a .py file, or a compiled extension module like .so, .pyd, .dylib, or a namespace package (init) or a built-in, or a frozen module (embedded bytecode in the executable, and so on.

I think it is best to use spec.origin if a spec exists:

            raise TypeError(f'{object!r} is: {object.__spec__.origin}')

In a limited number of cases, origin may be unset/None, but I feel that's better than clubbing everything as a built-in module. WDYT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better will do it

Signed-off-by: yihong0618 <zouzou0208@gmail.com>
@github-actions

github-actions Bot commented May 4, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants