Skip to content

Function must not return a pointer to content without an explicit resource management #57

@vstinner

Description

@vstinner

The Python C API has many functions giving a direct access to an object content as a pointer:

  • PyBytes_AsString(), PyBytes_AS_STRING()
  • PyByteArray_AsString(), PyByteArray_AS_STRING()
  • PyEval_GetFuncName()
  • PyUnicode_AsUTF8(), PyUnicode_AsUTF8AndSize()
  • PyCapsule_GetName()

Problem: the returned pointer becomes a dangling pointer if the object is deleted. It's not explicit for the caller how long the pointer remains valid.

For PyUnicode_AsUTF8(), the caller can guess that the pointer remains valid until the Python str is deleted (until the last strong reference is deleted).

Problem: These API make assumptions on how Python objects are implemented. What if tomorrow we want to remove the UTF-8 cached string of Python str object?

See "C API: Add PyObject_AsObjectArray() function: get tuple/list items as PyObject** array" issue python/cpython#106592 to have a different look at this problem.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions