Skip to content

Commit 3551121

Browse files
anmyachevvnlitvinov
authored andcommitted
removed extra layer; using get_string_data now
1 parent ac318d2 commit 3551121

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

pandas/_libs/tslibs/np_datetime.pyx

+6-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
from cpython cimport (Py_EQ, Py_NE, Py_GE, Py_GT, Py_LT, Py_LE,
4-
PyUnicode_AsASCIIString)
3+
from cpython cimport Py_EQ, Py_NE, Py_GE, Py_GT, Py_LT, Py_LE
54

65
from cpython.datetime cimport (datetime, date,
76
PyDateTime_IMPORT,
@@ -13,6 +12,7 @@ from cpython.datetime cimport (datetime, date,
1312
PyDateTime_IMPORT
1413

1514
from numpy cimport int64_t
15+
from pandas._libs.tslibs.util cimport get_string_data
1616

1717
cdef extern from "src/datetime/np_datetime.h":
1818
int cmp_npy_datetimestruct(npy_datetimestruct *a,
@@ -174,30 +174,11 @@ cdef inline int64_t pydate_to_dt64(date val, npy_datetimestruct *dts):
174174
cdef inline int _string_to_dts(object val, npy_datetimestruct* dts,
175175
int* out_local, int* out_tzoffset) except? -1:
176176
cdef:
177-
int result
177+
Py_ssize_t length
178178
char *tmp
179179

180-
if isinstance(val, unicode):
181-
val = PyUnicode_AsASCIIString(val)
182-
183-
tmp = val
184-
result = _cstring_to_dts(tmp, len(val), dts, out_local, out_tzoffset)
185-
186-
if result == -1:
180+
if not get_string_data(val, &tmp, &length):
187181
raise ValueError('Unable to parse %s' % str(val))
188-
return result
189-
190-
191-
cdef inline int _cstring_to_dts(char *val, int length,
192-
npy_datetimestruct* dts,
193-
int* out_local, int* out_tzoffset) except? -1:
194-
# Note: without this "extra layer" between _string_to_dts
195-
# and parse_iso_8601_datetime, calling _string_to_dts raises
196-
# `SystemError: <class 'str'> returned a result with an error set`
197-
# in Python3
198-
cdef:
199-
int result
182+
return parse_iso_8601_datetime(tmp, length,
183+
dts, out_local, out_tzoffset)
200184

201-
result = parse_iso_8601_datetime(val, length,
202-
dts, out_local, out_tzoffset)
203-
return result

pandas/_libs/tslibs/util.pxd

+12
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ cdef extern from "Python.h":
1818
# Note: importing extern-style allows us to declare these as nogil
1919
# functions, whereas `from cpython cimport` does not.
2020
bint PyUnicode_Check(object obj) nogil
21+
bint PyBytes_Check(object obj) nogil
2122
bint PyString_Check(object obj) nogil
2223
bint PyBool_Check(object obj) nogil
2324
bint PyFloat_Check(object obj) nogil
2425
bint PyComplex_Check(object obj) nogil
2526
bint PyObject_TypeCheck(object obj, PyTypeObject* type) nogil
27+
bint PyBytes_AsStringAndSize(object obj, char** buf, Py_ssize_t* length) nogil
28+
char* PyUnicode_AsUTF8AndSize(object obj, Py_ssize_t* length) nogil
2629

2730
from numpy cimport int64_t
2831

@@ -227,3 +230,12 @@ cdef inline bint is_nan(object val):
227230
is_nan : bool
228231
"""
229232
return (is_float_object(val) or is_complex_object(val)) and val != val
233+
234+
235+
cdef inline bint get_string_data(object s, char **buf, Py_ssize_t *length):
236+
if PyUnicode_Check(s):
237+
buf[0] = PyUnicode_AsUTF8AndSize(s, length)
238+
return buf[0] != NULL
239+
if PyBytes_Check(s):
240+
return PyBytes_AsStringAndSize(s, buf, length) == 0
241+
return False

0 commit comments

Comments
 (0)