Make patch_submodule robust to __builtins__ being a module (PyPy / __main__)#8310
Open
vineethsaivs wants to merge 1 commit into
Open
Make patch_submodule robust to __builtins__ being a module (PyPy / __main__)#8310vineethsaivs wants to merge 1 commit into
vineethsaivs wants to merge 1 commit into
Conversation
patch_submodule read the builtins via globals()["__builtins__"]. The value of a module's __builtins__ global is a CPython implementation detail: it is the builtins module (not its dict) in the __main__ module and under PyPy, and only usually a dict in imported modules. When it is the module, 'target_attr in globals()["__builtins__"]' raises 'TypeError: argument of type \'module\' is not iterable', so patching a builtin such as open fails (e.g. when running datasets streaming under PyPy, as reported in huggingface#7636). Use builtins.__dict__ directly, which is always the builtins mapping regardless of the runtime. Add a regression test that simulates the module case. Closes huggingface#7636
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #7636.
Problem
patch_submodulereads the builtins throughglobals()["__builtins__"]:The value of a module's
__builtins__global is a CPython implementation detail: it is thebuiltinsmodule (not its dict) in the__main__module and under PyPy, and only usually a dict in imported modules (docs). When it is the module,target_attr in globals()["__builtins__"]raises:so patching a builtin such as
openfails. This is what #7636 hit runningdatasetsstreaming under PyPy.Fix
Use
builtins.__dict__directly, which is always the builtins mapping regardless of the runtime. In the common CPython case (an imported module whose__builtins__already isbuiltins.__dict__) the behavior is unchanged; it just also works under__main__/PyPy.Test
test_patch_submodule_builtin_when_builtins_is_modulesetsdatasets.utils.patching.__builtins__to thebuiltinsmodule (simulating the PyPy /__main__runtime) and patchesopen. It raises theTypeErrorabove on the current code and passes with this change; the existing patching tests are unaffected (fulltests/test_patching.pyis green).