Skip to content

[WIP] gh-106592: Add PyResource API #107202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Doc/c-api/abstract.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ but whose items have not been set to some non-\ ``NULL`` value yet.
mapping.rst
iter.rst
buffer.rst
resource.rst
7 changes: 7 additions & 0 deletions Doc/c-api/bytes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ called with a non-bytes parameter.
Similar to :c:func:`PyBytes_AsString`, but without error checking.


.. c:function:: char* PyBytes_AsStringRes(PyObject *o, PyResource *res)

Similar to :c:func:`PyBytes_AsString`, but the returning string remains
valid until :c:func:`PyResource_Close(res) <PyResource_Close>` is called.

.. versionadded:: 3.13

.. c:function:: int PyBytes_AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *length)

Return the null-terminated contents of the object *obj*
Expand Down
25 changes: 25 additions & 0 deletions Doc/c-api/resource.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.. highlight:: c

PyResource
==========

.. versionadded:: 3.13

API using a callback function to close a resource.


.. c:type:: PyResource

.. c:member:: void (*close_func) (void *data)

Callback function called by :c:func:`PyResource_Close`.

.. c:member:: void *data

Argument passed to :c:member:`close_func` by :c:func:`PyResource_Close`.


.. c:function:: void PyResource_Close(PyResource *res)

Close a resource: call :c:member:`PyResource.close_func` with
:c:member:`PyResource.data`.
3 changes: 3 additions & 0 deletions Doc/data/stable_abi.dat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,12 @@ New Features
not needed.
(Contributed by Victor Stinner in :gh:`106004`.)

* Added the :c:func:`PyResource_Close` function.
(Contributed by Victor Stinner in :gh:`106592`.)

* Added the :c:func:`PyBytes_AsStringRes` function.
(Contributed by Victor Stinner in :gh:`106592`.)

Porting to Python 3.13
----------------------

Expand Down
1 change: 1 addition & 0 deletions Include/Python.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "pymacro.h"
#include "pymath.h"
#include "pymem.h"
#include "pyresource.h"
#include "pytypedefs.h"
#include "pybuffer.h"
#include "object.h"
Expand Down
3 changes: 2 additions & 1 deletion Include/bytesobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +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_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
21 changes: 21 additions & 0 deletions Include/pyresource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef Py_RESOURCE_H
#define Py_RESOURCE_H
#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
void (*close_func) (void *data);
void *data;
} PyResource;

PyAPI_FUNC(void) PyResource_Close(PyResource *res);

#ifdef Py_BUILD_CORE
extern void _PyResource_DECREF(void *data);
#endif

#ifdef __cplusplus
}
#endif
#endif // !Py_RESOURCE_H
2 changes: 2 additions & 0 deletions Lib/test/test_stable_abi_ctypes.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/pymath.h \
$(srcdir)/Include/pymem.h \
$(srcdir)/Include/pyport.h \
$(srcdir)/Include/pyresource.h \
$(srcdir)/Include/pystate.h \
$(srcdir)/Include/pystats.h \
$(srcdir)/Include/pystrcmp.h \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added the :c:func:`PyResource_Close` function. Patch by Victor Stinner.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added the :c:func:`PyBytes_AsStringRes` function. Patch by Victor Stinner.
7 changes: 7 additions & 0 deletions Misc/stable_abi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2450,3 +2450,10 @@
added = '3.13'
[function.PyDict_GetItemStringRef]
added = '3.13'
[struct.PyResource]
added = '3.13'
struct_abi_kind = 'full-abi'
[function.PyResource_Close]
added = '3.13'
[function.PyBytes_AsStringRes]
added = '3.13'
28 changes: 28 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3654,6 +3654,33 @@ test_dict_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
}


static PyObject *
test_resource_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
const char *src = "test_resource_capi";
PyObject *bytes = PyBytes_FromString(src);
if (bytes == NULL) {
return NULL;
}

// test PyBytes_AsStringRes()
PyResource res;
const char *str = PyBytes_AsStringRes(bytes, &res);
// delete the bytes object, the resource keeps a strong reference to it
Py_CLEAR(bytes);

// test PyResource_Close()
assert(memcmp(str, src, strlen(src)) == 0);
PyResource_Close(&res);

// test PyResource_Close() with NULL callback: must not crash
PyResource res2 = {.close_func = NULL, .data = NULL};
PyResource_Close(&res2);

Py_RETURN_NONE;
}


static PyMethodDef TestMethods[] = {
{"set_errno", set_errno, METH_VARARGS},
{"test_config", test_config, METH_NOARGS},
Expand Down Expand Up @@ -3800,6 +3827,7 @@ static PyMethodDef TestMethods[] = {
{"check_pyimport_addmodule", check_pyimport_addmodule, METH_VARARGS},
{"test_weakref_capi", test_weakref_capi, METH_NOARGS},
{"test_dict_capi", test_dict_capi, METH_NOARGS},
{"test_resource_capi", test_resource_capi, METH_NOARGS},
{NULL, NULL} /* sentinel */
};

Expand Down
15 changes: 14 additions & 1 deletion Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ PyBytes_Size(PyObject *op)
return Py_SIZE(op);
}

char *
char*
PyBytes_AsString(PyObject *op)
{
if (!PyBytes_Check(op)) {
Expand All @@ -1220,6 +1220,19 @@ PyBytes_AsString(PyObject *op)
return ((PyBytesObject *)op)->ob_sval;
}

const char*
PyBytes_AsStringRes(PyObject *op, PyResource *res)
{
if (!PyBytes_Check(op)) {
PyErr_Format(PyExc_TypeError,
"expected bytes, %.200s found", Py_TYPE(op)->tp_name);
return NULL;
}
res->close_func = _PyResource_DECREF;
res->data = Py_NewRef(op);
return ((PyBytesObject *)op)->ob_sval;
}

int
PyBytes_AsStringAndSize(PyObject *obj,
char **s,
Expand Down
16 changes: 16 additions & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2689,6 +2689,22 @@ int Py_IsFalse(PyObject *x)
return Py_Is(x, Py_False);
}

void _PyResource_DECREF(void *data)
{
PyObject *obj = _PyObject_CAST(data);
Py_DECREF(obj);
}

void PyResource_Close(PyResource *res)
{
if (res->close_func == NULL) {
return;
}
res->close_func(res->data);
res->close_func = NULL;
res->data = NULL;
}

#ifdef __cplusplus
}
#endif
2 changes: 2 additions & 0 deletions PC/python3dll.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions PCbuild/pythoncore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@
<ClInclude Include="..\Include\pymath.h" />
<ClInclude Include="..\Include\pymem.h" />
<ClInclude Include="..\Include\pyport.h" />
<ClInclude Include="..\Include\pyresource.h" />
<ClInclude Include="..\Include\pystate.h" />
<ClInclude Include="..\Include\pystats.h" />
<ClInclude Include="..\Include\pystrcmp.h" />
Expand Down
3 changes: 3 additions & 0 deletions PCbuild/pythoncore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@
<ClInclude Include="..\Include\pyport.h">
<Filter>Include</Filter>
</ClInclude>
<ClInclude Include="..\Include\pyresource.h">
<Filter>Include</Filter>
</ClInclude>
<ClInclude Include="..\Include\pystate.h">
<Filter>Include</Filter>
</ClInclude>
Expand Down