Skip to content

Commit

Permalink
Update PyResource functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Jul 17, 2023
1 parent 8af4606 commit 46d3c3a
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 18 deletions.
3 changes: 2 additions & 1 deletion Include/bytearrayobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Include/bytesobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down
3 changes: 2 additions & 1 deletion Include/ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down
15 changes: 10 additions & 5 deletions Include/cpython/unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 =============================================== */

Expand Down
5 changes: 4 additions & 1 deletion Include/pycapsule.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
15 changes: 14 additions & 1 deletion Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ PyByteArray_Size(PyObject *self)
return PyByteArray_GET_SIZE(self);
}

char *
char*
PyByteArray_AsString(PyObject *self)
{
assert(self != NULL);
Expand All @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions Objects/capsule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
34 changes: 29 additions & 5 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
{
Expand Down

0 comments on commit 46d3c3a

Please sign in to comment.