diff --git a/Include/bytearrayobject.h b/Include/bytearrayobject.h index 3d53fdba6432672..db9e8fe003471ea 100644 --- a/Include/bytearrayobject.h +++ b/Include/bytearrayobject.h @@ -29,7 +29,8 @@ PyAPI_FUNC(PyObject *) PyByteArray_FromObject(PyObject *); PyAPI_FUNC(PyObject *) PyByteArray_Concat(PyObject *, PyObject *); PyAPI_FUNC(PyObject *) PyByteArray_FromStringAndSize(const char *, Py_ssize_t); PyAPI_FUNC(Py_ssize_t) PyByteArray_Size(PyObject *); -PyAPI_FUNC(char *) PyByteArray_AsString(PyObject *); +PyAPI_FUNC(char*) PyByteArray_AsString(PyObject *op); +PyAPI_FUNC(char*) PyByteArray_AsStringRes(PyObject *op, PyResource *res); PyAPI_FUNC(int) PyByteArray_Resize(PyObject *, Py_ssize_t); #ifndef Py_LIMITED_API diff --git a/Include/bytesobject.h b/Include/bytesobject.h index cd1442e6e94b2b9..5b7a4255b1c1643 100644 --- a/Include/bytesobject.h +++ b/Include/bytesobject.h @@ -39,8 +39,8 @@ PyAPI_FUNC(PyObject *) PyBytes_FromFormatV(const char*, va_list) PyAPI_FUNC(PyObject *) PyBytes_FromFormat(const char*, ...) Py_GCC_ATTRIBUTE((format(printf, 1, 2))); PyAPI_FUNC(Py_ssize_t) PyBytes_Size(PyObject *); -PyAPI_FUNC(char *) PyBytes_AsString(PyObject *); -PyAPI_FUNC(char *) PyBytes_AsStringResource(PyObject *, PyResource *res); +PyAPI_FUNC(char*) PyBytes_AsString(PyObject *op); +PyAPI_FUNC(const char*) PyBytes_AsStringRes(PyObject *op, PyResource *res); PyAPI_FUNC(PyObject *) PyBytes_Repr(PyObject *, int); PyAPI_FUNC(void) PyBytes_Concat(PyObject **, PyObject *); PyAPI_FUNC(void) PyBytes_ConcatAndDel(PyObject **, PyObject *); diff --git a/Include/ceval.h b/Include/ceval.h index 9885bdb7febc21a..7978caacc28aab9 100644 --- a/Include/ceval.h +++ b/Include/ceval.h @@ -56,7 +56,8 @@ PyAPI_FUNC(int) Py_GetRecursionLimit(void); PyAPI_FUNC(int) Py_EnterRecursiveCall(const char *where); PyAPI_FUNC(void) Py_LeaveRecursiveCall(void); -PyAPI_FUNC(const char *) PyEval_GetFuncName(PyObject *); +PyAPI_FUNC(const char *) PyEval_GetFuncName(PyObject *func); +PyAPI_FUNC(const char *) PyEval_GetFuncNameRes(PyObject *func, PyResource *res); PyAPI_FUNC(const char *) PyEval_GetFuncDesc(PyObject *); PyAPI_FUNC(PyObject *) PyEval_EvalFrame(PyFrameObject *); diff --git a/Include/cpython/unicodeobject.h b/Include/cpython/unicodeobject.h index cfe4ba1cc4a4421..9b18670aa9c3fe8 100644 --- a/Include/cpython/unicodeobject.h +++ b/Include/cpython/unicodeobject.h @@ -438,7 +438,7 @@ PyAPI_FUNC(PyObject*) PyUnicode_FromKindAndData( const void *buffer, Py_ssize_t size); -/* --- Manage the default encoding ---------------------------------------- */ +/* --- Manage the UTF-8 encoding ------------------------------------------ */ /* Returns a pointer to the default encoding (UTF-8) of the Unicode object unicode. @@ -452,14 +452,19 @@ PyAPI_FUNC(PyObject*) PyUnicode_FromKindAndData( Use of this API is DEPRECATED since no size information can be extracted from the returned data. */ +PyAPI_FUNC(const char*) PyUnicode_AsUTF8(PyObject *unicode); -PyAPI_FUNC(const char *) PyUnicode_AsUTF8(PyObject *unicode); +#define _PyUnicode_AsString PyUnicode_AsUTF8 -PyAPI_FUNC(const char *) PyUnicode_AsUTF8(PyObject *unicode); -PyAPI_FUNC(const char *) PyUnicode_AsUTF8Resource(PyObject *unicode, PyResource *res); +PyAPI_FUNC(const char *) PyUnicode_AsUTF8AndSizeRes( + PyObject *unicode, + Py_ssize_t *size, + PyResource *res); -#define _PyUnicode_AsString PyUnicode_AsUTF8 +PyAPI_FUNC(const char*) PyUnicode_AsUTF8Res( + PyObject *unicode, + PyResource *res); /* === Characters Type APIs =============================================== */ diff --git a/Include/pycapsule.h b/Include/pycapsule.h index 929a9a685259fb3..c0861db6c2d122f 100644 --- a/Include/pycapsule.h +++ b/Include/pycapsule.h @@ -34,7 +34,10 @@ PyAPI_FUNC(void *) PyCapsule_GetPointer(PyObject *capsule, const char *name); PyAPI_FUNC(PyCapsule_Destructor) PyCapsule_GetDestructor(PyObject *capsule); -PyAPI_FUNC(const char *) PyCapsule_GetName(PyObject *capsule); +PyAPI_FUNC(const char*) PyCapsule_GetName(PyObject *capsule); +#ifndef Py_LIMITED_API +PyAPI_FUNC(const char*) PyCapsule_GetNameRes(PyObject *capsule, PyResource *res); +#endif PyAPI_FUNC(void *) PyCapsule_GetContext(PyObject *capsule); diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 18a24a369a64c13..d20ceed2b88b548 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -157,7 +157,7 @@ PyByteArray_Size(PyObject *self) return PyByteArray_GET_SIZE(self); } -char * +char* PyByteArray_AsString(PyObject *self) { assert(self != NULL); @@ -166,6 +166,19 @@ PyByteArray_AsString(PyObject *self) return PyByteArray_AS_STRING(self); } + +char* +PyByteArray_AsStringRes(PyObject *self, PyResource *res) +{ + assert(self != NULL); + assert(PyByteArray_Check(self)); + + res->close_func = _PyResource_DECREF; + res->data = Py_NewRef(op); + return PyByteArray_AS_STRING(self); +} + + int PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size) { diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index b923830c6f11df8..51df31a317da383 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -1220,8 +1220,8 @@ PyBytes_AsString(PyObject *op) return ((PyBytesObject *)op)->ob_sval; } -char * -PyBytes_AsStringResource(PyObject *op, PyResource *res) +const char* +PyBytes_AsStringRes(PyObject *op, PyResource *res) { if (!PyBytes_Check(op)) { PyErr_Format(PyExc_TypeError, diff --git a/Objects/capsule.c b/Objects/capsule.c index baaddb3f1f0849c..9ed671ffc7b02da 100644 --- a/Objects/capsule.c +++ b/Objects/capsule.c @@ -106,6 +106,20 @@ PyCapsule_GetName(PyObject *o) } +const char * +PyCapsule_GetNameRes(PyObject *o, PyResource *res) +{ + PyCapsule *capsule = (PyCapsule *)o; + + if (!is_legal_capsule(capsule, "PyCapsule_GetName")) { + return NULL; + } + res->close_func = _PyResource_DECREF; + res->data = Py_NewRef(capsule); + return capsule->name; +} + + PyCapsule_Destructor PyCapsule_GetDestructor(PyObject *o) { diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 63e2b1351c657c5..a023551e918e33c 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -3781,7 +3781,7 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr) static int unicode_fill_utf8(PyObject *unicode); -const char * +const char* PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) { if (!PyUnicode_Check(unicode)) { @@ -3800,20 +3800,44 @@ PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) return PyUnicode_UTF8(unicode); } -const char * +const char* PyUnicode_AsUTF8(PyObject *unicode) { return PyUnicode_AsUTF8AndSize(unicode, NULL); } -const char * -PyUnicode_AsUTF8Resource(PyObject *unicode, PyResource *res) + +const char* +PyUnicode_AsUTF8AndSizeRes(PyObject *unicode, Py_ssize_t *psize, PyResource *res) { + if (!PyUnicode_Check(unicode)) { + PyErr_BadArgument(); + return NULL; + } + + if (PyUnicode_UTF8(unicode) == NULL) { + if (unicode_fill_utf8(unicode) == -1) { + return NULL; + } + } + + if (psize) { + *psize = PyUnicode_UTF8_LENGTH(unicode); + } + res->close_func = _PyResource_DECREF; res->data = Py_NewRef(unicode); - return PyUnicode_AsUTF8AndSize(unicode, NULL); + return PyUnicode_UTF8(unicode); +} + + +const char* +PyUnicode_AsUTF8Res(PyObject *unicode, PyResource *res) +{ + return PyUnicode_AsUTF8AndSizeRes(unicode, NULL, res); } + /* PyUnicode_GetSize() has been deprecated since Python 3.3 because it returned length of Py_UNICODE. diff --git a/Python/ceval.c b/Python/ceval.c index d6c72fa3ff386cb..2c077afb2efe29a 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -2298,6 +2298,7 @@ PyEval_MergeCompilerFlags(PyCompilerFlags *cf) const char * PyEval_GetFuncName(PyObject *func) { + // Should be kept in sync with PyEval_GetFuncNameRes() if (PyMethod_Check(func)) return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); else if (PyFunction_Check(func)) @@ -2308,6 +2309,29 @@ PyEval_GetFuncName(PyObject *func) return Py_TYPE(func)->tp_name; } +const char * +PyEval_GetFuncNameRes(PyObject *func, PyResource *res) +{ + // Should be kept in sync with PyEval_GetFuncName() + if (PyMethod_Check(func)) { + return PyEval_GetFuncNameRes(PyMethod_GET_FUNCTION(func), res); + } + else if (PyFunction_Check(func)) { + return PyUnicode_AsUTF8Res(((PyFunctionObject*)func)->func_name, res); + } + else if (PyCFunction_Check(func)) { + res->close_func = _PyResource_DECREF; + res->data = Py_NewRef(func); + return ((PyCFunctionObject*)func)->m_ml->ml_name; + } + else { + PyTypeObject *type = (PyTypeObject*)Py_NewRef(Py_TYPE(func)); + res->close_func = _PyResource_DECREF; + res->data = (PyObject *)type; // steal the reference + return type->tp_name; + } +} + const char * PyEval_GetFuncDesc(PyObject *func) {