Skip to content

Commit 78f99cf

Browse files
chris-b1pcluo
authored andcommitted
BUG: memory leak on get_c_string (pandas-dev#16060)
* BUG: memory leak on get_c_string * add except NULL
1 parent fbe5295 commit 78f99cf

File tree

3 files changed

+5
-10
lines changed

3 files changed

+5
-10
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ Performance Improvements
15231523
~~~~~~~~~~~~~~~~~~~~~~~~
15241524

15251525
- Improved performance of ``pd.wide_to_long()`` (:issue:`14779`)
1526-
- Improved performance of ``pd.factorize()`` by releasing the GIL with ``object`` dtype when inferred as strings (:issue:`14859`)
1526+
- Improved performance of ``pd.factorize()`` by releasing the GIL with ``object`` dtype when inferred as strings (:issue:`14859`, :issue:`16057`)
15271527
- Improved performance of timeseries plotting with an irregular DatetimeIndex
15281528
(or with ``compat_x=True``) (:issue:`15073`).
15291529
- Improved performance of ``groupby().cummin()`` and ``groupby().cummax()`` (:issue:`15048`, :issue:`15109`, :issue:`15561`, :issue:`15635`)

pandas/_libs/src/numpy_helper.h

+3-8
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,11 @@ PANDAS_INLINE PyObject* get_value_1d(PyArrayObject* ap, Py_ssize_t i) {
8787
return PyArray_Scalar(item, PyArray_DESCR(ap), (PyObject*)ap);
8888
}
8989

90+
// returns ASCII or UTF8 (py3) view on python str
91+
// python object owns memory, should not be freed
9092
PANDAS_INLINE char* get_c_string(PyObject* obj) {
9193
#if PY_VERSION_HEX >= 0x03000000
92-
PyObject* enc_str = PyUnicode_AsEncodedString(obj, "utf-8", "error");
93-
94-
char* ret;
95-
ret = PyBytes_AS_STRING(enc_str);
96-
97-
// TODO(general): memory leak here
98-
99-
return ret;
94+
return PyUnicode_AsUTF8(obj);
10095
#else
10196
return PyString_AsString(obj);
10297
#endif

pandas/_libs/src/util.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ cdef extern from "numpy_helper.h":
1717
inline cnp.int64_t get_nat()
1818
inline object get_value_1d(ndarray, Py_ssize_t)
1919
inline int floatify(object, double*) except -1
20-
inline char *get_c_string(object)
20+
inline char *get_c_string(object) except NULL
2121
inline object char_to_string(char*)
2222
inline void transfer_object_column(char *dst, char *src, size_t stride,
2323
size_t length)

0 commit comments

Comments
 (0)