Skip to content

Question about cast std::wstring with Python3 #674

Closed
@czastack

Description

@czastack

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions