Thanks to visit codestin.com
Credit goes to github.com

Skip to content

doctest.testmod() fails for a file called tokenize.py #113488

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

Closed
baoilleach opened this issue Dec 26, 2023 · 3 comments
Closed

doctest.testmod() fails for a file called tokenize.py #113488

baoilleach opened this issue Dec 26, 2023 · 3 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@baoilleach
Copy link

baoilleach commented Dec 26, 2023

Bug report

Bug description:

If the following file is saved as tokenize.py or linecache.py, then it fails with an error when run:

if __name__ == "__main__":
    import doctest
    doctest.testmod()

For tokenize.py:

Traceback (most recent call last):
  File "/home/noel/Work/20231226-doctest-bug/tokenize.py", line 3, in <module>
    doctest.testmod()
  File "/usr/lib/python3.10/doctest.py", line 1969, in testmod
    for test in finder.find(m, name, globs=globs, extraglobs=extraglobs):
  File "/usr/lib/python3.10/doctest.py", line 917, in find
    source_lines = linecache.getlines(file, module.__dict__)
  File "/usr/lib/python3.10/linecache.py", line 46, in getlines
    return updatecache(filename, module_globals)
  File "/usr/lib/python3.10/linecache.py", line 136, in updatecache
    with tokenize.open(fullname) as fp:
AttributeError: module 'tokenize' has no attribute 'open'

CPython versions tested on:

3.10, 3.12

Operating systems tested on:

Linux

@baoilleach baoilleach added the type-bug An unexpected behavior, bug, or error label Dec 26, 2023
@carljm
Copy link
Member

carljm commented Dec 26, 2023

Hi, thanks for the report!

This is expected behavior, not a bug. Your modules take precedence over the standard library, so if you shadow a standard library module name, your module will be imported in place of the standard library one, and won't have the contents expected by other standard library modules using it. The solution is not to shadow standard library module names with your modules.

In #95754 we recently merged a change to help clarify the error messages in this situation.

@carljm carljm closed this as not planned Won't fix, can't repro, duplicate, stale Dec 26, 2023
@baoilleach
Copy link
Author

Thanks @carljm. I wasn't sure if expected or not in this case.

@terryjreedy
Copy link
Member

https://discuss.python.org/c/users/7 is a good place to ask "Is this a bug?"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants