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

Skip to content

Commit 36eef3c

Browse files
committed
Changes by Greg Stein (code) and GvR (design).
Add a new member to the PyBufferProcs struct, bf_getcharbuffer. For backward compatibility, this member should only be used (this includes testing for NULL!) when the flag Py_TPFLAGS_HAVE_GETCHARBUFFER is set in the type structure, below. Note that if its flag is not set, we may be looking at an extension module compiled for 1.5.1, which will have garbage at the bf_getcharbuffer member (because the struct wasn't as long then). If the flag is one, the pointer may still be NULL. The function found at this member is used in a similar manner as bf_getreadbuffer, but it is known to point to 8-bit character data. (See discussion in getargs.c checked in later.) As a general feature for extending the type structure and the various structures that (may) hang off it in a backwards compatible way, we rename the tp_xxx4 "spare" slot to tp_flags. In 1.5.1 and before, this slot was always zero. In 1.5.1, it may contain various flags indicating extra fields that weren't present in 1.5.1. The only flag defined so far is for the bf_getcharbuffer member of the PyBufferProcs struct. Note that the new spares (tp_xxx5 - tp_xxx8), once they become used, should also be protected by a flag (or flags) in tp_flags.
1 parent 7e1e574 commit 36eef3c

1 file changed

Lines changed: 35 additions & 2 deletions

File tree

Include/object.h

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ typedef int(*objobjargproc) Py_PROTO((PyObject *, PyObject *, PyObject *));
149149
typedef int (*getreadbufferproc) Py_PROTO((PyObject *, int, void **));
150150
typedef int (*getwritebufferproc) Py_PROTO((PyObject *, int, void **));
151151
typedef int (*getsegcountproc) Py_PROTO((PyObject *, int *));
152+
typedef int (*getcharbufferproc) Py_PROTO((PyObject *, int, const char **));
152153

153154
typedef struct {
154155
binaryfunc nb_add;
@@ -196,6 +197,7 @@ typedef struct {
196197
getreadbufferproc bf_getreadbuffer;
197198
getwritebufferproc bf_getwritebuffer;
198199
getsegcountproc bf_getsegcount;
200+
getcharbufferproc bf_getcharbuffer;
199201
} PyBufferProcs;
200202

201203

@@ -240,8 +242,8 @@ typedef struct _typeobject {
240242
/* Functions to access object as input/output buffer */
241243
PyBufferProcs *tp_as_buffer;
242244

243-
/* Space for future expansion */
244-
long tp_xxx4;
245+
/* Flags to define presence of optional/expanded features */
246+
long tp_flags;
245247

246248
char *tp_doc; /* Documentation string */
247249

@@ -289,6 +291,37 @@ extern void Py_ReprLeave Py_PROTO((PyObject *));
289291
/* Flag bits for printing: */
290292
#define Py_PRINT_RAW 1 /* No string quotes etc. */
291293

294+
/*
295+
296+
Type flags (tp_flags)
297+
298+
These flags are used to extend the type structure in a backwards-compatible
299+
fashion. Extensions can use the flags to indicate (and test) when a given
300+
type structure contains a new feature. The Python core will use these when
301+
introducing new functionality between major revisions (to avoid mid-version
302+
changes in the PYTHON_API_VERSION).
303+
304+
Arbitration of the flag bit positions will need to be coordinated among
305+
all extension writers who publically release their extensions (this will
306+
be fewer than you might expect!)..
307+
308+
Python 1.5.2 introduced the bf_getcharbuffer slot into PyBufferProcs.
309+
310+
Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
311+
312+
Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
313+
given type object has a specified feature.
314+
315+
*/
316+
317+
/* PyBufferProcs contains bf_getcharbuffer */
318+
#define Py_TPFLAGS_HAVE_GETCHARBUFFER (1L<<0)
319+
320+
#define Py_TPFLAGS_DEFAULT (Py_TPFLAGS_HAVE_GETCHARBUFFER)
321+
322+
#define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
323+
324+
292325
/*
293326
123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
294327

0 commit comments

Comments
 (0)