Skip to content

Commit 921d010

Browse files
TST: Use try/except block to properly catch and handle the exception (#33235)
1 parent df5eee6 commit 921d010

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

pandas/_libs/hashtable_class_helper.pxi.in

+3-6
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
1212
from pandas._libs.tslibs.util cimport get_c_string
1313
from pandas._libs.missing cimport C_NA
1414

15-
cdef extern from "Python.h":
16-
void PyErr_Clear()
17-
1815
{{py:
1916

2017
# name, dtype, c_type
@@ -792,9 +789,9 @@ cdef class StringHashTable(HashTable):
792789
labels[i] = na_sentinel
793790
else:
794791
# if ignore_na is False, we also stringify NaN/None/etc.
795-
v = get_c_string(<str>val)
796-
if v == NULL:
797-
PyErr_Clear()
792+
try:
793+
v = get_c_string(<str>val)
794+
except UnicodeEncodeError:
798795
v = get_c_string(<str>repr(val))
799796
vecs[i] = v
800797

pandas/_libs/tslibs/util.pxd

+3-7
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ cdef inline bint is_nan(object val):
219219

220220

221221
cdef inline const char* get_c_string_buf_and_size(str py_string,
222-
Py_ssize_t *length):
222+
Py_ssize_t *length) except NULL:
223223
"""
224224
Extract internal char* buffer of unicode or bytes object `py_string` with
225225
getting length of this internal buffer saved in `length`.
@@ -238,12 +238,8 @@ cdef inline const char* get_c_string_buf_and_size(str py_string,
238238
-------
239239
buf : const char*
240240
"""
241-
cdef:
242-
const char *buf
241+
return PyUnicode_AsUTF8AndSize(py_string, length)
243242

244-
buf = PyUnicode_AsUTF8AndSize(py_string, length)
245-
return buf
246243

247-
248-
cdef inline const char* get_c_string(str py_string):
244+
cdef inline const char* get_c_string(str py_string) except NULL:
249245
return get_c_string_buf_and_size(py_string, NULL)

0 commit comments

Comments
 (0)