-
Notifications
You must be signed in to change notification settings - Fork 922
Fix utf8 string conversion memory leak on Python 2 (#198) #239
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,8 +64,15 @@ | |
|
||
/** | ||
* @returns Unicode Python object as char * in UTF-8 encoding | ||
* @param uobjp might be set to NULL or a new object reference (depending | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Along the same lines as my other comment -- why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this comment is misleading, it is not a new object reference to the original object, but a new object reference to an utf8 reprsentation object of the original object, do you object? |
||
* on Python version) which needs to be cleaned up with | ||
* Py_XDECREF() after finished use of the returned string. | ||
*/ | ||
#define cfl_PyUnistr_AsUTF8(X) PyUnicode_AsUTF8(X) | ||
static __inline const char * | ||
cfl_PyUnistr_AsUTF8 (PyObject *o, PyObject **uobjp) { | ||
*uobjp = NULL; /* No intermediary object needed in Py3 */ | ||
return PyUnicode_AsUTF8(o); | ||
} | ||
|
||
/** | ||
* @returns Unicode Python string object | ||
|
@@ -77,7 +84,11 @@ | |
/* See comments above */ | ||
#define cfl_PyBin(X) PyString ## X | ||
#define cfl_PyUnistr(X) PyUnicode ## X | ||
#define cfl_PyUnistr_AsUTF8(X) PyBytes_AsString(PyUnicode_AsUTF8String(X)) | ||
static __inline const char * | ||
cfl_PyUnistr_AsUTF8 (PyObject *o, PyObject **uobjp) { | ||
*uobjp = PyUnicode_AsUTF8String(o); /*UTF8 intermediary object on Py2*/ | ||
return PyBytes_AsString(*uobjp); | ||
} | ||
#define cfl_PyObject_Unistr(X) PyObject_Unicode(X) | ||
#endif | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it becomes inconsistent with the way
ks
is handled, but couldn't we justPy_XDECREF
immediately so we only have it once and don't have to scatter it on all of these paths? In fact, it looks like we could do that forks
in some of the cases that have been converted here.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Python 2.x ks8 will point to another object than ks, namely the utf8 representation of ks, and since
k
is a char pointer to ks's underlying memory we must retain the reference until all usage ofk
is over.On Python 3 ks8 will always be NULL, thus causing XDECREF to be a no-op.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ack, makes sense. Kinda sucks that the API naming doesn't make it really obvious when you're getting internal data vs a completely new object.