Skip to content

BUG: doing a class method lookup in __dealloc__ is dangerous #44879

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 1 commit into from
Dec 16, 2021
Merged
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
28 changes: 18 additions & 10 deletions pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -558,18 +558,11 @@ cdef class TextReader:
pass

def __dealloc__(self):
self.close()
_close(self)
parser_del(self.parser)

def close(self) -> None:
# also preemptively free all allocated memory
parser_free(self.parser)
if self.true_set:
kh_destroy_str_starts(self.true_set)
self.true_set = NULL
if self.false_set:
kh_destroy_str_starts(self.false_set)
self.false_set = NULL
def close(self):
_close(self)

def _set_quoting(self, quote_char: str | bytes | None, quoting: int):
if not isinstance(quoting, int):
Expand Down Expand Up @@ -1292,6 +1285,21 @@ cdef class TextReader:
return None


# Factor out code common to TextReader.__dealloc__ and TextReader.close
# It cannot be a class method, since calling self.close() in __dealloc__
# which causes a class attribute lookup and violates best parctices
# https://cython.readthedocs.io/en/latest/src/userguide/special_methods.html#finalization-method-dealloc
cdef _close(TextReader reader):
# also preemptively free all allocated memory
parser_free(reader.parser)
if reader.true_set:
kh_destroy_str_starts(reader.true_set)
reader.true_set = NULL
if reader.false_set:
kh_destroy_str_starts(reader.false_set)
reader.false_set = NULL


cdef:
object _true_values = [b'True', b'TRUE', b'true']
object _false_values = [b'False', b'FALSE', b'false']
Expand Down