Skip to content

gh-154892: Fix PyLong_AsLong() error checks in _zoneinfo - #154901

Open
BHUVANSH855 wants to merge 1 commit into
python:mainfrom
BHUVANSH855:fix-zoneinfo-minus-one
Open

gh-154892: Fix PyLong_AsLong() error checks in _zoneinfo#154901
BHUVANSH855 wants to merge 1 commit into
python:mainfrom
BHUVANSH855:fix-zoneinfo-minus-one

Conversation

@BHUVANSH855

Copy link
Copy Markdown
Contributor

Summary

Fix Modules/_zoneinfo.c:get_local_timestamp() to correctly distinguish a legitimate -1 result from PyLong_AsLong() from an actual conversion error by checking PyErr_Occurred(), matching the documented C API behavior and the pure-Python implementation.

The previous implementation treated any -1 return value from PyLong_AsLong() as an error, even when no exception was set. This caused datetime subclasses overriding hour, minute, or second to return -1 to incorrectly raise an exception in the C accelerator.

This PR also adds a regression test covering datetime subclasses whose hour, minute, and second properties return -1.

Issue

Closes: gh-154892

self.assertEqual(dt_fromutc.fold, 1)
self.assertEqual(dt.fold, 0)

def test_datetime_subclass_negative_components(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to ZoneInfoTest? Then it verifies both the C and Python behavior.

Comment thread Modules/_zoneinfo.c
@@ -2311,7 +2311,7 @@ get_local_timestamp(PyObject *dt, int64_t *local_ts)
}
hour = PyLong_AsLong(num);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exactly the issue, but related. What if a subclass returns 2**40? Maybe PyLong_AsInt can be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_zoneinfo: get_local_timestamp() misinterprets valid -1 results from PyLong_AsLong()

2 participants