Repository navigation
Raise TypeError when a stream's read() doesn't return a string - #1641
Open
buzztouniverse wants to merge 3 commits into
Open
buzztouniverse wants to merge 3 commits into
buzztouniverse wants to merge 3 commits into
Conversation
pganssle
reviewed
Oct 24, 2026
| # The check in __init__ only looks for a read attribute, so | ||
| # something like a Mock gets through, and since its read() | ||
| # never returns an empty string we would never hit EOF. | ||
| if not isinstance(nextchar, (text_type, bytes)): |
Author
There was a problem hiding this comment.
Rebased on master and switched it to str.
pganssle
reviewed
Oct 24, 2026
| def test_read_not_returning_string_invalid(self): | ||
| # GH #1428: A Mock passes the duck typing check, but its read() never | ||
| # returns an empty string, so this used to loop forever | ||
| try: |
Member
There was a problem hiding this comment.
Do not import in the test and don't make it conditional.
Author
There was a problem hiding this comment.
Moved from unittest import mock to the top of the file.
The tokenizer only checks that its input has a read attribute, so a Mock gets through. Its read() never returns an empty string, so parse() never reached EOF and kept allocating memory until the process ran out. Fixes GH dateutil#1428
buzztouniverse
force-pushed
the
fix-mock-stream-loop
branch
from
October 24, 2026 21:18
cd5eddb to
f79aad9
Compare
This branch has not been deployed
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.
Summary of changes
_timelexonly checks that the input has areadattribute, so aMockgets through. Itsread()never returns'', so the tokenizer never reaches EOF and keeps growing the token until it runs out of memory.This raises
TypeErrorwhenread()returns something other thanstrorbytes.bytesis still accepted so aBytesIOkeeps raisingParserErrorlike before.An earlier attempt at this was #1553, closed by its author.
Closes #1428
Pull Request Checklist