diff --git a/doc/source/whatsnew/v1.4.1.rst b/doc/source/whatsnew/v1.4.1.rst index 48b62637c26b1..79dae514b77e9 100644 --- a/doc/source/whatsnew/v1.4.1.rst +++ b/doc/source/whatsnew/v1.4.1.rst @@ -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`) - .. --------------------------------------------------------------------------- diff --git a/pandas/_libs/tslibs/src/datetime/np_datetime.c b/pandas/_libs/tslibs/src/datetime/np_datetime.c index 9ad2ead5f919f..0efed210a7fdf 100644 --- a/pandas/_libs/tslibs/src/datetime/np_datetime.c +++ b/pandas/_libs/tslibs/src/datetime/np_datetime.c @@ -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 */ @@ -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 */ diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index 0b8548f98b03b..f3a6f1f80359c 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -11,7 +11,6 @@ from pandas.compat import ( IS64, - PY310, is_platform_windows, ) import pandas.util._test_decorators as td @@ -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", [ @@ -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", [