Skip to content

Commit 2bdddc8

Browse files
committed
change get_string_data signature to more pythonic
1 parent 789614e commit 2bdddc8

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

pandas/_libs/hashtable_class_helper.pxi.in

+8-8
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ cdef class StringHashTable(HashTable):
597597
cdef:
598598
khiter_t k
599599
const char *v
600-
get_string_data_checked(val, &v, NULL)
600+
v = get_string_data_checked(val, NULL)
601601

602602
k = kh_get_str(self.table, v)
603603
if k != self.table.n_buckets:
@@ -611,7 +611,7 @@ cdef class StringHashTable(HashTable):
611611
int ret = 0
612612
const char *v
613613

614-
get_string_data_checked(val, &v, NULL)
614+
v = get_string_data_checked(val, NULL)
615615

616616
k = kh_put_str(self.table, v, &ret)
617617
self.table.keys[k] = key
@@ -634,7 +634,7 @@ cdef class StringHashTable(HashTable):
634634
vecs = <const char **>malloc(n * sizeof(char *))
635635
for i in range(n):
636636
val = values[i]
637-
get_string_data_checked(val, &v, NULL)
637+
v = get_string_data_checked(val, NULL)
638638
vecs[i] = v
639639

640640
with nogil:
@@ -664,9 +664,9 @@ cdef class StringHashTable(HashTable):
664664
val = values[i]
665665

666666
if isinstance(val, (str, unicode)):
667-
get_string_data(val, &v, NULL)
667+
v = get_string_data(val, NULL)
668668
else:
669-
get_string_data(self.na_string_sentinel, &v, NULL)
669+
v = get_string_data(self.na_string_sentinel, NULL)
670670
vecs[i] = v
671671

672672
with nogil:
@@ -697,9 +697,9 @@ cdef class StringHashTable(HashTable):
697697
val = values[i]
698698

699699
if isinstance(val, (str, unicode)):
700-
get_string_data(val, &v, NULL)
700+
v = get_string_data(val, NULL)
701701
else:
702-
get_string_data(self.na_string_sentinel, &v, NULL)
702+
v = get_string_data(self.na_string_sentinel, NULL)
703703
vecs[i] = v
704704

705705
with nogil:
@@ -778,7 +778,7 @@ cdef class StringHashTable(HashTable):
778778
labels[i] = na_sentinel
779779
else:
780780
# if ignore_na is False, we also stringify NaN/None/etc.
781-
get_string_data_checked(val, &v, NULL)
781+
v = get_string_data_checked(val, NULL)
782782
vecs[i] = v
783783

784784
# compute

pandas/_libs/tslibs/np_datetime.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,6 @@ cdef inline int _string_to_dts(object val, npy_datetimestruct* dts,
177177
Py_ssize_t length
178178
const char* tmp
179179

180-
get_string_data_checked(val, &tmp, &length)
180+
tmp = get_string_data_checked(val, &length)
181181
return parse_iso_8601_datetime(tmp, length,
182182
dts, out_local, out_tzoffset)

pandas/_libs/tslibs/util.pxd

+16-11
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,7 @@ cdef inline bint is_nan(object val):
238238
return (is_float_object(val) or is_complex_object(val)) and val != val
239239

240240

241-
cdef inline bint get_string_data(object s, const char **buf,
242-
Py_ssize_t *length):
241+
cdef inline const char* get_string_data(object s, Py_ssize_t *length):
243242
"""
244243
Extract internal char * buffer of unicode or bytes object `s` to `buf` with
245244
getting length of this internal buffer saved in `length`.
@@ -255,25 +254,31 @@ cdef inline bint get_string_data(object s, const char **buf,
255254
Parameters
256255
----------
257256
s : object
258-
buf : const char**
259257
length : Py_ssize_t*
260258
261259
Returns
262260
-------
263-
bint
261+
buf : const char*
264262
"""
263+
cdef:
264+
const char *buf
265+
265266
if PyUnicode_Check(s):
266-
buf[0] = PyUnicode_AsUTF8AndSize(s, length)
267-
return buf[0] != NULL
267+
buf = PyUnicode_AsUTF8AndSize(s, length)
268268
if PyBytes_Check(s):
269-
return PyBytes_AsStringAndSize(s, <char**>buf, length) == 0
270-
return False
269+
if PyBytes_AsStringAndSize(s, <char**>&buf, length) != 0:
270+
return NULL
271+
return buf
272+
271273

272-
cdef inline void get_string_data_checked(object s, const char **buf,
273-
Py_ssize_t *length):
274+
cdef inline const char* get_string_data_checked(object s, Py_ssize_t *length):
274275
"""
275276
This is a wrapper for get_string_data() that raises TypeError
276277
when supplied with neither unicode nor bytes object
277278
"""
278-
if not get_string_data(s, buf, length):
279+
cdef:
280+
const char *buf = get_string_data(s, length)
281+
282+
if not buf:
279283
PyErr_BadArgument()
284+
return buf

0 commit comments

Comments
 (0)