gh-154892: Fix PyLong_AsLong() error checks in _zoneinfo - #154901
Open
BHUVANSH855 wants to merge 1 commit into
Open
gh-154892: Fix PyLong_AsLong() error checks in _zoneinfo#154901BHUVANSH855 wants to merge 1 commit into
BHUVANSH855 wants to merge 1 commit into
Conversation
eendebakpt
reviewed
Jul 29, 2026
| self.assertEqual(dt_fromutc.fold, 1) | ||
| self.assertEqual(dt.fold, 0) | ||
|
|
||
| def test_datetime_subclass_negative_components(self): |
Contributor
There was a problem hiding this comment.
Can we move this to ZoneInfoTest? Then it verifies both the C and Python behavior.
| @@ -2311,7 +2311,7 @@ get_local_timestamp(PyObject *dt, int64_t *local_ts) | |||
| } | |||
| hour = PyLong_AsLong(num); | |||
Contributor
There was a problem hiding this comment.
Not exactly the issue, but related. What if a subclass returns 2**40? Maybe PyLong_AsInt can be used.
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
Fix
Modules/_zoneinfo.c:get_local_timestamp()to correctly distinguish a legitimate-1result fromPyLong_AsLong()from an actual conversion error by checkingPyErr_Occurred(), matching the documented C API behavior and the pure-Python implementation.The previous implementation treated any
-1return value fromPyLong_AsLong()as an error, even when no exception was set. This causeddatetimesubclasses overridinghour,minute, orsecondto return-1to incorrectly raise an exception in the C accelerator.This PR also adds a regression test covering
datetimesubclasses whosehour,minute, andsecondproperties return-1.Issue
Closes: gh-154892