Skip to content

gh-132776: Minor Fixes for XIBufferViewType #132779

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

Merged
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
93 changes: 72 additions & 21 deletions Modules/_interpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,41 +76,60 @@ is_running_main(PyInterpreterState *interp)
// XXX Release when the original interpreter is destroyed.

typedef struct {
PyObject_HEAD
PyObject base;
Py_buffer *view;
int64_t interpid;
} XIBufferViewObject;

#define XIBufferViewObject_CAST(op) ((XIBufferViewObject *)(op))

static PyObject *
xibufferview_from_xid(PyTypeObject *cls, _PyXIData_t *data)
xibufferview_from_buffer(PyTypeObject *cls, Py_buffer *view, int64_t interpid)
{
assert(_PyXIData_DATA(data) != NULL);
assert(_PyXIData_OBJ(data) == NULL);
assert(_PyXIData_INTERPID(data) >= 0);
assert(interpid >= 0);

Py_buffer *copied = PyMem_RawMalloc(sizeof(Py_buffer));
if (copied == NULL) {
return NULL;
}
/* This steals the view->obj reference */
*copied = *view;

XIBufferViewObject *self = PyObject_Malloc(sizeof(XIBufferViewObject));
if (self == NULL) {
PyMem_RawFree(copied);
return NULL;
}
PyObject_Init((PyObject *)self, cls);
self->view = (Py_buffer *)_PyXIData_DATA(data);
self->interpid = _PyXIData_INTERPID(data);
PyObject_Init(&self->base, cls);
*self = (XIBufferViewObject){
.base = self->base,
.view = copied,
.interpid = interpid,
};
return (PyObject *)self;
}

static void
xibufferview_dealloc(PyObject *op)
{
XIBufferViewObject *self = XIBufferViewObject_CAST(op);
PyInterpreterState *interp = _PyInterpreterState_LookUpID(self->interpid);
/* If the interpreter is no longer alive then we have problems,
since other objects may be using the buffer still. */
assert(interp != NULL);

if (_PyBuffer_ReleaseInInterpreterAndRawFree(interp, self->view) < 0) {
// XXX Emit a warning?
PyErr_Clear();
if (self->view != NULL) {
PyInterpreterState *interp =
_PyInterpreterState_LookUpID(self->interpid);
if (interp == NULL) {
/* The interpreter is no longer alive. */
PyErr_Clear();
PyMem_RawFree(self->view);
}
else {
if (_PyBuffer_ReleaseInInterpreterAndRawFree(interp,
self->view) < 0)
{
// XXX Emit a warning?
PyErr_Clear();
}
}
}

PyTypeObject *tp = Py_TYPE(self);
Expand Down Expand Up @@ -155,32 +174,64 @@ static PyType_Spec XIBufferViewType_spec = {

static PyTypeObject * _get_current_xibufferview_type(void);


struct xibuffer {
Py_buffer view;
int used;
};

static PyObject *
_memoryview_from_xid(_PyXIData_t *data)
{
assert(_PyXIData_DATA(data) != NULL);
assert(_PyXIData_OBJ(data) == NULL);
assert(_PyXIData_INTERPID(data) >= 0);
struct xibuffer *view = (struct xibuffer *)_PyXIData_DATA(data);
assert(!view->used);

PyTypeObject *cls = _get_current_xibufferview_type();
if (cls == NULL) {
return NULL;
}
PyObject *obj = xibufferview_from_xid(cls, data);

PyObject *obj = xibufferview_from_buffer(
cls, &view->view, _PyXIData_INTERPID(data));
if (obj == NULL) {
return NULL;
}
return PyMemoryView_FromObject(obj);
PyObject *res = PyMemoryView_FromObject(obj);
if (res == NULL) {
Py_DECREF(obj);
return NULL;
}
view->used = 1;
return res;
}

static void
_pybuffer_shared_free(void* data)
{
struct xibuffer *view = (struct xibuffer *)data;
if (!view->used) {
PyBuffer_Release(&view->view);
}
PyMem_RawFree(data);
}

static int
_memoryview_shared(PyThreadState *tstate, PyObject *obj, _PyXIData_t *data)
_pybuffer_shared(PyThreadState *tstate, PyObject *obj, _PyXIData_t *data)
{
Py_buffer *view = PyMem_RawMalloc(sizeof(Py_buffer));
struct xibuffer *view = PyMem_RawMalloc(sizeof(struct xibuffer));
if (view == NULL) {
return -1;
}
if (PyObject_GetBuffer(obj, view, PyBUF_FULL_RO) < 0) {
view->used = 0;
if (PyObject_GetBuffer(obj, &view->view, PyBUF_FULL_RO) < 0) {
PyMem_RawFree(view);
return -1;
}
_PyXIData_Init(data, tstate->interp, view, NULL, _memoryview_from_xid);
data->free = _pybuffer_shared_free;
return 0;
}

Expand All @@ -201,7 +252,7 @@ register_memoryview_xid(PyObject *mod, PyTypeObject **p_state)
*p_state = cls;

// Register XID for the builtin memoryview type.
if (ensure_xid_class(&PyMemoryView_Type, _memoryview_shared) < 0) {
if (ensure_xid_class(&PyMemoryView_Type, _pybuffer_shared) < 0) {
return -1;
}
// We don't ever bother un-registering memoryview.
Expand Down
Loading