Skip to content

Call to _imp.source_hash with incorrect arguments in (unreachable?) part of SourceLoader.get_code (importlib._bootstrap_external) #99508

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
DavidCEllis opened this issue Nov 15, 2022 · 2 comments
Labels
topic-importlib type-bug An unexpected behavior, bug, or error

Comments

@DavidCEllis
Copy link
Contributor

DavidCEllis commented Nov 15, 2022

Bug report

As far as I can tell this line:

source_hash = _imp.source_hash(source_bytes)

will raise a TypeError and should be a 2 argument form as seen earlier in the same method.

source_hash = _imp.source_hash(
_RAW_MAGIC_NUMBER,
source_bytes,
)

However, as either hash_based is False, source_hash has already been generated or the code returns early I don't think this line can be reached. (Maybe if you have a flaky version of get_data that sometimes raises an ImportError/EOFError??).

Example:

>>> _imp.source_hash(_RAW_MAGIC_NUMBER, b'')
b's\x8d\x9c\xd5\xd5\xe8\x7fs'

>>> _imp.source_hash(b'')
Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
TypeError: source_hash() missing required argument 'source' (pos 2)

Edit: Sorry, as I opened the issue from within the source line via 'reference in new issue' I think I accidentally skipped the bug report format that would have given this a label.

Linked PRs

@sobolevn sobolevn added topic-importlib type-bug An unexpected behavior, bug, or error labels Nov 16, 2022
@sobolevn
Copy link
Member

sobolevn commented Nov 16, 2022

Yes, this seems incorrect. _imp.source_hash always has two arguments, here one is used.

@FFY00
Copy link
Member

FFY00 commented Nov 21, 2022

It seems this was a bug of #4575, which went unnoticed until now. Looks like it is just missing _RAW_MAGIC_NUMBER as the first argument.

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

No branches or pull requests

3 participants