Skip to content

Commit d939bde

Browse files
committed
revert DEF component of pandas-dev#21878
1 parent 63eff04 commit d939bde

File tree

4 files changed

+24
-30
lines changed

4 files changed

+24
-30
lines changed

pandas/_libs/src/numpy_helper.h

+17
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ PANDAS_INLINE PyObject* get_value_1d(PyArrayObject* ap, Py_ssize_t i) {
3030
return PyArray_Scalar(item, PyArray_DESCR(ap), (PyObject*)ap);
3131
}
3232

33+
// returns ASCII or UTF8 (py3) view on python str
34+
// python object owns memory, should not be freed
35+
PANDAS_INLINE const char* get_c_string(PyObject* obj) {
36+
#if PY_VERSION_HEX >= 0x03000000
37+
return PyUnicode_AsUTF8(obj);
38+
#else
39+
return PyString_AsString(obj);
40+
#endif
41+
}
42+
43+
PANDAS_INLINE PyObject* char_to_string(const char* data) {
44+
#if PY_VERSION_HEX >= 0x03000000
45+
return PyUnicode_FromString(data);
46+
#else
47+
return PyString_FromString(data);
48+
#endif
49+
}
3350

3451
void set_array_not_contiguous(PyArrayObject* ao) {
3552
ao->flags &= ~(NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS);

pandas/_libs/src/util.pxd

+2-24
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ cnp.import_array()
44

55
cimport cpython
66
from cpython cimport PyTypeObject
7-
from cpython.string cimport PyString_FromString, PyString_AsString
87

9-
DEF PY3 = bytes != str
108

119
cdef extern from "Python.h":
1210
# Note: importing extern-style allows us to declare these as nogil
@@ -17,8 +15,6 @@ cdef extern from "Python.h":
1715
bint PyFloat_Check(object obj) nogil
1816
bint PyComplex_Check(object obj) nogil
1917
bint PyObject_TypeCheck(object obj, PyTypeObject* type) nogil
20-
char* PyUnicode_AsUTF8(object unicode)
21-
object PyUnicode_FromString(const char* u) nogil
2218

2319

2420
cdef extern from "numpy/arrayobject.h":
@@ -74,6 +70,8 @@ cdef extern from "numpy_helper.h":
7470
int assign_value_1d(ndarray, Py_ssize_t, object) except -1
7571
cnp.int64_t get_nat()
7672
object get_value_1d(ndarray, Py_ssize_t)
73+
char *get_c_string(object) except NULL
74+
object char_to_string(char*)
7775

7876
ctypedef fused numeric:
7977
cnp.int8_t
@@ -104,26 +102,6 @@ cdef extern from "headers/stdint.h":
104102
enum: INT64_MIN
105103

106104

107-
cdef inline const char* get_c_string(object obj) except NULL:
108-
"""
109-
returns ASCII or UTF8 (py3) view on python str
110-
python object owns memory, should not be freed
111-
"""
112-
# TODO: this docstring is copied verbatim from version that was
113-
# directly in numpy_helper.C; is it still accurate?
114-
IF PY3:
115-
return PyUnicode_AsUTF8(obj)
116-
ELSE:
117-
return PyString_AsString(obj)
118-
119-
120-
cdef inline object char_to_string(const char* data):
121-
IF PY3:
122-
return PyUnicode_FromString(data)
123-
ELSE:
124-
return PyString_FromString(data)
125-
126-
127105
cdef inline object get_value_at(ndarray arr, object loc):
128106
cdef:
129107
Py_ssize_t i, sz

pandas/_libs/tslib.pyx

+3-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ from tslibs.timestamps cimport (create_timestamp_from_ts,
5353
from tslibs.timestamps import Timestamp
5454

5555

56-
DEF PY2 = str == bytes
56+
cdef bint PY2 = str == bytes
5757

5858

5959
cdef inline object create_datetime_from_ts(
@@ -555,9 +555,8 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
555555
if len(val) == 0 or val in nat_strings:
556556
iresult[i] = NPY_NAT
557557
continue
558-
if PY2:
559-
if PyUnicode_Check(val):
560-
val = val.encode('utf-8')
558+
if PyUnicode_Check(val) and PY2:
559+
val = val.encode('utf-8')
561560

562561
try:
563562
_string_to_dts(val, &dts, &out_local, &out_tzoffset)

pandas/_libs/tslibs/period.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ from nattype cimport _nat_scalar_rules, NPY_NAT, is_null_datetimelike
5252
from offsets cimport to_offset
5353
from offsets import _Tick
5454

55-
DEF PY2 = str == bytes
55+
cdef bint PY2 = str == bytes
5656

5757

5858
cdef extern from "period_helper.h":
@@ -728,7 +728,7 @@ cdef object _period_strftime(int64_t value, int freq, object fmt):
728728

729729
result = result.replace(str_extra_fmts[i], repl)
730730

731-
IF PY2:
731+
if PY2:
732732
result = result.decode('utf-8', 'ignore')
733733

734734
return result

0 commit comments

Comments
 (0)