Skip to content

Backport PR #45558 on branch 1.4.x (BUG: Segfault in to_json with tzaware datetime) #45584

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Fixed regressions

Bug fixes
~~~~~~~~~
-
- Fixed segfault in :meth:``DataFrame.to_json`` when dumping tz-aware datetimes in Python 3.10 (:issue:`42130`)
-

.. ---------------------------------------------------------------------------
Expand Down
10 changes: 9 additions & 1 deletion pandas/_libs/tslibs/src/datetime/np_datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ int convert_pydatetime_to_datetimestruct(PyObject *dtobj,
Py_DECREF(tmp);
} else {
PyObject *offset;
PyObject *tmp_int;
int seconds_offset, minutes_offset;

/* The utcoffset function should return a timedelta */
Expand All @@ -378,11 +379,18 @@ int convert_pydatetime_to_datetimestruct(PyObject *dtobj,
if (tmp == NULL) {
return -1;
}
seconds_offset = PyInt_AsLong(tmp);
tmp_int = PyNumber_Long(tmp);
if (tmp_int == NULL) {
Py_DECREF(tmp);
return -1;
}
seconds_offset = PyInt_AsLong(tmp_int);
if (seconds_offset == -1 && PyErr_Occurred()) {
Py_DECREF(tmp_int);
Py_DECREF(tmp);
return -1;
}
Py_DECREF(tmp_int);
Py_DECREF(tmp);

/* Convert to a minutes offset and apply it */
Expand Down
3 changes: 0 additions & 3 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from pandas.compat import (
IS64,
PY310,
is_platform_windows,
)
import pandas.util._test_decorators as td
Expand Down Expand Up @@ -1177,7 +1176,6 @@ def test_sparse(self):
expected = s.to_json()
assert expected == ss.to_json()

@pytest.mark.skipif(PY310, reason="segfault GH 42130")
@pytest.mark.parametrize(
"ts",
[
Expand All @@ -1195,7 +1193,6 @@ def test_tz_is_utc(self, ts):
dt = ts.to_pydatetime()
assert dumps(dt, iso_dates=True) == exp

@pytest.mark.skipif(PY310, reason="segfault GH 42130")
@pytest.mark.parametrize(
"tz_range",
[
Expand Down