diff --git a/pandas/_libs/hashtable_class_helper.pxi.in b/pandas/_libs/hashtable_class_helper.pxi.in index 3ce3bc519b311..c251c92cb072a 100644 --- a/pandas/_libs/hashtable_class_helper.pxi.in +++ b/pandas/_libs/hashtable_class_helper.pxi.in @@ -12,9 +12,6 @@ WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in from pandas._libs.tslibs.util cimport get_c_string from pandas._libs.missing cimport C_NA -cdef extern from "Python.h": - void PyErr_Clear() - {{py: # name, dtype, c_type @@ -792,9 +789,9 @@ cdef class StringHashTable(HashTable): labels[i] = na_sentinel else: # if ignore_na is False, we also stringify NaN/None/etc. - v = get_c_string(val) - if v == NULL: - PyErr_Clear() + try: + v = get_c_string(val) + except UnicodeEncodeError: v = get_c_string(repr(val)) vecs[i] = v diff --git a/pandas/_libs/tslibs/util.pxd b/pandas/_libs/tslibs/util.pxd index e7f6b3334eb65..cc98781dc73cf 100644 --- a/pandas/_libs/tslibs/util.pxd +++ b/pandas/_libs/tslibs/util.pxd @@ -219,7 +219,7 @@ cdef inline bint is_nan(object val): cdef inline const char* get_c_string_buf_and_size(str py_string, - Py_ssize_t *length): + Py_ssize_t *length) except NULL: """ Extract internal char* buffer of unicode or bytes object `py_string` with 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, ------- buf : const char* """ - cdef: - const char *buf + return PyUnicode_AsUTF8AndSize(py_string, length) - buf = PyUnicode_AsUTF8AndSize(py_string, length) - return buf - -cdef inline const char* get_c_string(str py_string): +cdef inline const char* get_c_string(str py_string) except NULL: return get_c_string_buf_and_size(py_string, NULL)