Skip to content

BUG: Fix segfault on dir of a DataFrame with a unicode surrogate character in the column name #32701

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
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ Other
- Fixed :func:`pandas.testing.assert_series_equal` to correctly raise if left object is a different subclass with ``check_series_type=True`` (:issue:`32670`).
- :meth:`IntegerArray.astype` now supports ``datetime64`` dtype (:issue:32538`)
- Fixed bug in :func:`pandas.testing.assert_series_equal` where dtypes were checked for ``Interval`` and ``ExtensionArray`` operands when ``check_dtype`` was ``False`` (:issue:`32747`)
- Bug in :meth:`DataFrame.__dir__` caused a segfault when using unicode surrogates in a column name (:issue:`25509`)

.. ---------------------------------------------------------------------------

Expand Down
8 changes: 8 additions & 0 deletions pandas/_libs/hashtable_class_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ 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":
# Note: importing extern-style allows us to declare these as nogil
Copy link
Member

Choose a reason for hiding this comment

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

This is interacting with Python objects so it needs to be called with the GIL

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will remove it, I am quite new to cython, so I have to read up a bit on these things.

Copy link
Member

Choose a reason for hiding this comment

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

If you are getting warnings / errors when trying to cythonize you might need a with gil: block in your error handler

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not getting any cython errors, so I am probably good.

# functions, whereas `from cpython cimport` does not.
void PyErr_Clear() nogil

{{py:

# name, dtype, c_type
Expand Down Expand Up @@ -790,6 +795,9 @@ cdef class StringHashTable(HashTable):
else:
# if ignore_na is False, we also stringify NaN/None/etc.
v = get_c_string(<str>val)
if v == NULL:
PyErr_Clear()
v = get_c_string(<str>repr(val))
vecs[i] = v

# compute
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ def test_not_hashable(self):
with pytest.raises(TypeError, match=msg):
hash(empty_frame)

def test_column_name_contains_unicode_surrogate(self):
# GH 25509
colname = "\ud83d"
df = DataFrame({colname: []})
# this should not crash
assert colname not in dir(df)
assert df.columns[0] == colname

def test_new_empty_index(self):
df1 = DataFrame(np.random.randn(0, 3))
df2 = DataFrame(np.random.randn(0, 3))
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/parser/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def test_categorical_dtype_utf16(all_parsers, csv_dir_path):
pth = os.path.join(csv_dir_path, "utf16_ex.txt")
parser = all_parsers
encoding = "utf-16"
sep = ","
sep = "\t"
Copy link
Contributor

Choose a reason for hiding this comment

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

why did this change?

Copy link
Contributor Author

@roberthdevries roberthdevries Mar 17, 2020

Choose a reason for hiding this comment

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

This test failed for some reason. So when I investigated this I noticed that the separator in this file is a tab rather than a ,. So I changed this and the test passed. I am not sure what caused the failure, but I think that the test was wrong to begin with and there is some weird feature interaction that caused the failure.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm yea very strange. I guess since there isn't a comma in this file at all it just reads every line into a single column....


expected = parser.read_csv(pth, sep=sep, encoding=encoding)
expected = expected.apply(Categorical)
Expand Down