From the docs for the "s" format argument:
Python docs wrote:"s" (string) [char *]
Convert a Python string to a C pointer to a character string. You must not provide storage for the string itself; a pointer to an existing string is stored into the character pointer variable whose address you pass. The C string is null-terminated. The Python string must not contain embedded null bytes; if it does, a TypeError exception is raised.
(emphasis added). I.e. the pointer get's set to an existing string object, so you don't need to worry about its dealocation.
Every Python string has a PyStringObject struct underpining it (and the struct contains a pointer to the actual char array that is the string in memory). From quick look at Python source (and I'm no expert, so hopefully someone will correct me if I'm misreading it), it looks like "s" format eventually results in a call to PyString_TO_STRING macro that just returns the pointer to char array in PyStringObject instance:
- Code: Select all
#define PyString_AS_STRING(op) (((PyStringObject *)(op))->ob_sval)
So basically, it looks like you're getting a pointer to the char array that is managed by the reference-counted PyObject that is the Python string. So allocation and deallocation of that memory is managed by the Python string object.
for more details on Python string implementation.