Skip to content

Question about cast std::wstring with Python3 #674

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

Closed
czastack opened this issue Feb 16, 2017 · 2 comments
Closed

Question about cast std::wstring with Python3 #674

czastack opened this issue Feb 16, 2017 · 2 comments

Comments

@czastack
Copy link

The document of Python3 says:
wchar_t* PyUnicode_AsWideCharString(PyObject *unicode, Py_ssize_t *size)
Returns a buffer allocated by PyMem_Alloc() (use PyMem_Free() to free it) on success.

and in cast.h of pybind11:
template <> class type_casterstd::wstring

#if PY_MAJOR_VERSION >= 3
    buffer = PyUnicode_AsWideCharString(load_src.ptr(), &length);
#else
...
#endif
    if (!buffer) { PyErr_Clear(); return false; }
    value = std::wstring(buffer, (size_t) length);
    success = true;
    return true;

Why we don't need to call PyMem_Free to free the buffer in this case?

@jagerman
Copy link
Member

It looks like a bug, but it's no longer used in the pybind11 master branch (as of PR #624, which largely rewrote the unicode support). We now go via PyUnicode_AsEncodedString, and manage the returned reference.

@wjakob
Copy link
Member

wjakob commented Feb 17, 2017

I'll close this for now -- please comment here if you still have problems with the master version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants