Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 01a74b2

Browse files
committed
Make CObjects mutable. Fixes #477441.
1 parent 95cf84a commit 01a74b2

4 files changed

Lines changed: 30 additions & 5 deletions

File tree

Doc/api/concrete.tex

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,34 +2479,40 @@ \subsection{CObjects \label{cObjects}}
24792479
\end{ctypedesc}
24802480

24812481
\begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p}
2482-
Returns true if its argument is a \ctype{PyCObject}.
2482+
Return true if its argument is a \ctype{PyCObject}.
24832483
\end{cfuncdesc}
24842484

24852485
\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj,
24862486
void (*destr)(void *)}
2487-
Creates a \ctype{PyCObject} from the \code{void *}\var{cobj}. The
2487+
Create a \ctype{PyCObject} from the \code{void *}\var{cobj}. The
24882488
\var{destr} function will be called when the object is reclaimed,
24892489
unless it is \NULL.
24902490
\end{cfuncdesc}
24912491

24922492
\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj,
24932493
void* desc, void (*destr)(void *, void *)}
2494-
Creates a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
2494+
Create a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
24952495
\var{destr} function will be called when the object is reclaimed.
24962496
The \var{desc} argument can be used to pass extra callback data for
24972497
the destructor function.
24982498
\end{cfuncdesc}
24992499

25002500
\begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self}
2501-
Returns the object \ctype{void *} that the \ctype{PyCObject}
2501+
Return the object \ctype{void *} that the \ctype{PyCObject}
25022502
\var{self} was created with.
25032503
\end{cfuncdesc}
25042504

25052505
\begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self}
2506-
Returns the description \ctype{void *} that the \ctype{PyCObject}
2506+
Return the description \ctype{void *} that the \ctype{PyCObject}
25072507
\var{self} was created with.
25082508
\end{cfuncdesc}
25092509

2510+
\begin{cfuncdesc}{int}{PyCObject_SetVoidPtr}{PyObject* self, void* cobj}
2511+
Set the void pointer inside \var{self} to \var{cobj}.
2512+
The \ctype{PyCObject} must not have an associated destructor.
2513+
Return true on success, false on failure.
2514+
\end{cfuncdesc}
2515+
25102516

25112517
\subsection{Cell Objects \label{cell-objects}}
25122518

Include/cobject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ PyAPI_FUNC(void *) PyCObject_GetDesc(PyObject *);
4545
/* Import a pointer to a C object from a module using a PyCObject. */
4646
PyAPI_FUNC(void *) PyCObject_Import(char *module_name, char *cobject_name);
4747

48+
/* Modify a C object. Fails (==0) if object has a destructor. */
49+
PyAPI_FUNC(int) PyCObject_SetVoidPtr(PyObject *self, void *cobj);
50+
4851
#ifdef __cplusplus
4952
}
5053
#endif

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ What's New in Python 2.4 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- CObjects are now mutable (on the C level) through PyCObject_SetVoidPtr.
16+
1517
- list.sort() now supports three keyword arguments: cmp, key, and reverse.
1618
The key argument can be a function of one argument that extracts a
1719
comparison key from the original record: mylist.sort(key=str.lower).

Objects/cobject.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ PyCObject_Import(char *module_name, char *name)
9999
return r;
100100
}
101101

102+
int
103+
PyCObject_SetVoidPtr(PyObject *_self, void *cobj)
104+
{
105+
PyCObject* self = (PyCObject*)_self;
106+
if (self == NULL || !PyCObject_Check(self) ||
107+
self->destructor != NULL) {
108+
PyErr_SetString(PyExc_TypeError,
109+
"Invalid call to PyCObject_SetVoidPtr");
110+
return 0;
111+
}
112+
self->cobject = cobj;
113+
return 1;
114+
}
115+
102116
static void
103117
PyCObject_dealloc(PyCObject *self)
104118
{

0 commit comments

Comments
 (0)