Skip to content

TST: Use try/except block to properly catch and handle the exception #33235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions pandas/_libs/hashtable_class_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(<str>val)
if v == NULL:
PyErr_Clear()
try:
v = get_c_string(<str>val)
except UnicodeEncodeError:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have anything in the test suite that hits this now? (if we do can you add this issue number there)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue was triggered by the following test: pandas/tests/frame/test_api.py::test_column_name_contains_unicode_surrogate
Which was added because of a fix for #25509

v = get_c_string(<str>repr(val))
vecs[i] = v

Expand Down
10 changes: 3 additions & 7 deletions pandas/_libs/tslibs/util.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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)