Skip to content

Clean Up nogil Warning From Parsers #30369

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 4 commits into from
Jan 2, 2020
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
36 changes: 9 additions & 27 deletions pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,9 @@ cdef extern from "parser/tokenizer.h":
int64_t skip_first_N_rows
int64_t skipfooter
# pick one, depending on whether the converter requires GIL
Copy link
Member

Choose a reason for hiding this comment

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

comment is now extraneous

float64_t (*double_converter_nogil)(const char *, char **,
char, char, char,
int, int *, int *) nogil
float64_t (*double_converter_withgil)(const char *, char **,
char, char, char,
int, int *, int *)
float64_t (*double_converter)(const char *, char **,
char, char, char,
int, int *, int *) nogil

# error handling
char *warn_msg
Expand Down Expand Up @@ -469,16 +466,11 @@ cdef class TextReader:

if float_precision == "round_trip":
# see gh-15140
#
# Our current roundtrip implementation requires the GIL.
self.parser.double_converter_nogil = NULL
self.parser.double_converter_withgil = round_trip
self.parser.double_converter = round_trip
elif float_precision == "high":
self.parser.double_converter_withgil = NULL
self.parser.double_converter_nogil = precise_xstrtod
self.parser.double_converter = precise_xstrtod
else:
self.parser.double_converter_withgil = NULL
self.parser.double_converter_nogil = xstrtod
self.parser.double_converter = xstrtod

if isinstance(dtype, dict):
dtype = {k: pandas_dtype(dtype[k])
Expand Down Expand Up @@ -1663,22 +1655,12 @@ cdef _try_double(parser_t *parser, int64_t col,
result = np.empty(lines, dtype=np.float64)
data = <float64_t *>result.data
na_fset = kset_float64_from_list(na_flist)
if parser.double_converter_nogil != NULL: # if it can run without the GIL
with nogil:
error = _try_double_nogil(parser, parser.double_converter_nogil,
col, line_start, line_end,
na_filter, na_hashset, use_na_flist,
na_fset, NA, data, &na_count)
else:
assert parser.double_converter_withgil != NULL
error = _try_double_nogil(parser,
<float64_t (*)(const char *, char **,
char, char, char,
int, int *, int *)
nogil>parser.double_converter_withgil,
with nogil:
error = _try_double_nogil(parser, parser.double_converter,
col, line_start, line_end,
na_filter, na_hashset, use_na_flist,
na_fset, NA, data, &na_count)

kh_destroy_float64(na_fset)
if error != 0:
return None, None
Expand Down
7 changes: 7 additions & 0 deletions pandas/_libs/src/parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1774,11 +1774,18 @@ double precise_xstrtod(const char *str, char **endptr, char decimal,

double round_trip(const char *p, char **q, char decimal, char sci, char tsep,
int skip_trailing, int *error, int *maybe_int) {
// This is called from a nogil block in parsers.pyx
// so need to explicitly get GIL before Python calls
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

double r = PyOS_string_to_double(p, q, 0);
if (maybe_int != NULL) *maybe_int = 0;
if (PyErr_Occurred() != NULL) *error = -1;
else if (r == Py_HUGE_VAL) *error = (int)Py_HUGE_VAL;
PyErr_Clear();

PyGILState_Release(gstate);
return r;
}

Expand Down
9 changes: 4 additions & 5 deletions pandas/_libs/src/parser/tokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,8 @@ typedef struct parser_t {
PyObject *skipfunc;
int64_t skip_first_N_rows;
int64_t skip_footer;
// pick one, depending on whether the converter requires GIL
double (*double_converter_nogil)(const char *, char **,
char, char, char, int, int *, int *);
double (*double_converter_withgil)(const char *, char **,
char, char, char, int, int *, int *);
double (*double_converter)(const char *, char **,
char, char, char, int, int *, int *);

// error handling
char *warn_msg;
Expand Down Expand Up @@ -226,6 +223,8 @@ double xstrtod(const char *p, char **q, char decimal, char sci, char tsep,
double precise_xstrtod(const char *p, char **q, char decimal,
char sci, char tsep, int skip_trailing,
int *error, int *maybe_int);

// GH-15140 - round_trip requires and acquires the GIL on its own
double round_trip(const char *p, char **q, char decimal, char sci, char tsep,
int skip_trailing, int *error, int *maybe_int);
int to_boolean(const char *item, uint8_t *val);
Expand Down