Skip to content

Commit 24e360a

Browse files
authored
BUG: Segfault in to_json with tzaware datetime (#45558)
1 parent 991fc27 commit 24e360a

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

doc/source/whatsnew/v1.4.1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Fixed regressions
2323

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

2929
.. ---------------------------------------------------------------------------

pandas/_libs/tslibs/src/datetime/np_datetime.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ int convert_pydatetime_to_datetimestruct(PyObject *dtobj,
360360
Py_DECREF(tmp);
361361
} else {
362362
PyObject *offset;
363+
PyObject *tmp_int;
363364
int seconds_offset, minutes_offset;
364365

365366
/* The utcoffset function should return a timedelta */
@@ -378,11 +379,18 @@ int convert_pydatetime_to_datetimestruct(PyObject *dtobj,
378379
if (tmp == NULL) {
379380
return -1;
380381
}
381-
seconds_offset = PyInt_AsLong(tmp);
382+
tmp_int = PyNumber_Long(tmp);
383+
if (tmp_int == NULL) {
384+
Py_DECREF(tmp);
385+
return -1;
386+
}
387+
seconds_offset = PyInt_AsLong(tmp_int);
382388
if (seconds_offset == -1 && PyErr_Occurred()) {
389+
Py_DECREF(tmp_int);
383390
Py_DECREF(tmp);
384391
return -1;
385392
}
393+
Py_DECREF(tmp_int);
386394
Py_DECREF(tmp);
387395

388396
/* Convert to a minutes offset and apply it */

pandas/tests/io/json/test_pandas.py

-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
from pandas.compat import (
1313
IS64,
14-
PY310,
1514
is_platform_windows,
1615
)
1716
import pandas.util._test_decorators as td
@@ -1177,7 +1176,6 @@ def test_sparse(self):
11771176
expected = s.to_json()
11781177
assert expected == ss.to_json()
11791178

1180-
@pytest.mark.skipif(PY310, reason="segfault GH 42130")
11811179
@pytest.mark.parametrize(
11821180
"ts",
11831181
[
@@ -1195,7 +1193,6 @@ def test_tz_is_utc(self, ts):
11951193
dt = ts.to_pydatetime()
11961194
assert dumps(dt, iso_dates=True) == exp
11971195

1198-
@pytest.mark.skipif(PY310, reason="segfault GH 42130")
11991196
@pytest.mark.parametrize(
12001197
"tz_range",
12011198
[

0 commit comments

Comments
 (0)