Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
10 changes: 5 additions & 5 deletions 10 Doc/c-api/structures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,11 @@ definition with the same method name.
handles use of the :keyword:`del` statement on that attribute more correctly
than :c:macro:`T_OBJECT`.

:attr:`flags` can be ``0`` for write and read access or :c:macro:`READONLY` for
read-only access. Using :c:macro:`T_STRING` for :attr:`type` implies
:c:macro:`READONLY`. :c:macro:`T_STRING` data is interpreted as UTF-8.
Only :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX`
members can be deleted. (They are set to *NULL*).
:attr:`flags` can be ``0`` for write and read access (also represented by the
:c:macro:`PY_READWRITE`) or :c:macro:`PY_READONLY` for read-only access. Using
:c:macro:`T_STRING` for :attr:`type` implies :c:macro:`PY_READONLY`.
:c:macro:`T_STRING` data is interpreted as UTF-8. Only :c:macro:`T_OBJECT`
and :c:macro:`T_OBJECT_EX` members can be deleted. (They are set to *NULL*).


.. c:type:: PyGetSetDef
Expand Down
33 changes: 18 additions & 15 deletions 33 Doc/extending/newtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -282,23 +282,26 @@ store flags which control how the attribute can be accessed.
The following flag constants are defined in :file:`structmember.h`; they may be
combined using bitwise-OR.

+---------------------------+----------------------------------------------+
| Constant | Meaning |
+===========================+==============================================+
| :const:`READONLY` | Never writable. |
+---------------------------+----------------------------------------------+
| :const:`READ_RESTRICTED` | Not readable in restricted mode. |
+---------------------------+----------------------------------------------+
| :const:`WRITE_RESTRICTED` | Not writable in restricted mode. |
+---------------------------+----------------------------------------------+
| :const:`RESTRICTED` | Not readable or writable in restricted mode. |
+---------------------------+----------------------------------------------+
+------------------------------+----------------------------------------------+---------------------------+
| Constant | Meaning | Former Constant |
+==============================+==============================================+===========================+
| :const:`PY_READWRITE` | Writable. | 0 |
+------------------------------+----------------------------------------------+---------------------------+
| :const:`PY_READONLY` | Never writable. | :const:`READONLY` |
+------------------------------+----------------------------------------------+---------------------------+
| :const:`PY_READ_RESTRICTED` | Not readable in restricted mode. | :const:`READ_RESTRICTED` |
+------------------------------+----------------------------------------------+---------------------------+
| :const:`PY_WRITE_RESTRICTED` | Not writable in restricted mode. | :const:`WRITE_RESTRICTED` |
+------------------------------+----------------------------------------------+---------------------------+
| :const:`PY_RESTRICTED` | Not readable or writable in restricted mode. | :const:`RESTRICTED` |
+------------------------------+----------------------------------------------+---------------------------+

.. index::
single: READONLY
single: READ_RESTRICTED
single: WRITE_RESTRICTED
single: RESTRICTED
single: PY_READWRITE
single: PY_READONLY
single: PY_READ_RESTRICTED
single: PY_WRITE_RESTRICTED
single: PY_RESTRICTED

An interesting advantage of using the :c:member:`~PyTypeObject.tp_members` table to build
descriptors that are used at runtime is that any attribute defined this way can
Expand Down
8 changes: 4 additions & 4 deletions 8 Doc/extending/newtypes_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,11 @@ We want to expose our instance variables as attributes. There are a
number of ways to do that. The simplest way is to define member definitions::

static PyMemberDef Custom_members[] = {
{"first", T_OBJECT_EX, offsetof(CustomObject, first), 0,
{"first", T_OBJECT_EX, offsetof(CustomObject, first), PY_READWRITE,
"first name"},
{"last", T_OBJECT_EX, offsetof(CustomObject, last), 0,
{"last", T_OBJECT_EX, offsetof(CustomObject, last), PY_READWRITE,
"last name"},
{"number", T_INT, offsetof(CustomObject, number), 0,
{"number", T_INT, offsetof(CustomObject, number), PY_READWRITE,
"custom number"},
{NULL} /* Sentinel */
};
Expand Down Expand Up @@ -602,7 +602,7 @@ above. In this case, we aren't using a closure, so we just pass *NULL*.
We also remove the member definitions for these attributes::

static PyMemberDef Custom_members[] = {
{"number", T_INT, offsetof(CustomObject, number), 0,
{"number", T_INT, offsetof(CustomObject, number), PY_READWRITE,
"custom number"},
{NULL} /* Sentinel */
};
Expand Down
6 changes: 3 additions & 3 deletions 6 Doc/includes/custom2.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
}

static PyMemberDef Custom_members[] = {
{"first", T_OBJECT_EX, offsetof(CustomObject, first), 0,
{"first", T_OBJECT_EX, offsetof(CustomObject, first), PY_READWRITE,
"first name"},
{"last", T_OBJECT_EX, offsetof(CustomObject, last), 0,
{"last", T_OBJECT_EX, offsetof(CustomObject, last), PY_READWRITE,
"last name"},
{"number", T_INT, offsetof(CustomObject, number), 0,
{"number", T_INT, offsetof(CustomObject, number), PY_READWRITE,
"custom number"},
{NULL} /* Sentinel */
};
Expand Down
2 changes: 1 addition & 1 deletion 2 Doc/includes/custom3.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
}

static PyMemberDef Custom_members[] = {
{"number", T_INT, offsetof(CustomObject, number), 0,
{"number", T_INT, offsetof(CustomObject, number), PY_READWRITE,
"custom number"},
{NULL} /* Sentinel */
};
Expand Down
2 changes: 1 addition & 1 deletion 2 Doc/includes/custom4.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
}

static PyMemberDef Custom_members[] = {
{"number", T_INT, offsetof(CustomObject, number), 0,
{"number", T_INT, offsetof(CustomObject, number), PY_READWRITE,
"custom number"},
{NULL} /* Sentinel */
};
Expand Down
17 changes: 12 additions & 5 deletions 17 Include/structmember.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern "C" {

/* An array of PyMemberDef structures defines the name, type and offset
of selected members of a C structure. These can be read by
PyMember_GetOne() and set by PyMember_SetOne() (except if their READONLY
PyMember_GetOne() and set by PyMember_SetOne() (except if their PY_READONLY
flag is set). The array must be terminated with an entry whose name
pointer is NULL. */

Expand Down Expand Up @@ -57,10 +57,17 @@ typedef struct PyMemberDef {


/* Flags */
#define READONLY 1
#define READ_RESTRICTED 2
#define PY_WRITE_RESTRICTED 4
#define RESTRICTED (READ_RESTRICTED | PY_WRITE_RESTRICTED)
#define PY_READWRITE 0
#define PY_READONLY 1
#define PY_READ_RESTRICTED 2
#define PY_WRITE_RESTRICTED 4
#define PY_RESTRICTED (PY_READ_RESTRICTED | PY_WRITE_RESTRICTED)

/* Flags defined for the backward compatibility */
#define READWRITE PY_READWRITE
#define READONLY PY_READONLY
#define READ_RESTRICTED PY_READ_RESTRICTED
#define RESTRICTED PY_RESTRICTED


/* Current API, use this */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add :c:macro:`PY_READWRITE` for a more explicit definition of the
:c:type:`PyMemberDef` members and add the prefix ``PY_`` to these constants.
Contributed by Stéphane Wirtel.
6 changes: 3 additions & 3 deletions 6 Modules/_bz2module.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,10 +676,10 @@ PyDoc_STRVAR(BZ2Decompressor_needs_input_doc,

static PyMemberDef BZ2Decompressor_members[] = {
{"eof", T_BOOL, offsetof(BZ2Decompressor, eof),
READONLY, BZ2Decompressor_eof__doc__},
PY_READONLY, BZ2Decompressor_eof__doc__},
{"unused_data", T_OBJECT_EX, offsetof(BZ2Decompressor, unused_data),
READONLY, BZ2Decompressor_unused_data__doc__},
{"needs_input", T_BOOL, offsetof(BZ2Decompressor, needs_input), READONLY,
PY_READONLY, BZ2Decompressor_unused_data__doc__},
{"needs_input", T_BOOL, offsetof(BZ2Decompressor, needs_input), PY_READONLY,
BZ2Decompressor_needs_input_doc},
{NULL}
};
Expand Down
4 changes: 2 additions & 2 deletions 4 Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2072,7 +2072,7 @@ static PyMethodDef defdict_methods[] = {

static PyMemberDef defdict_members[] = {
{"default_factory", T_OBJECT,
offsetof(defdictobject, default_factory), 0,
offsetof(defdictobject, default_factory), PY_READWRITE,
PyDoc_STR("Factory for default value called by __missing__().")},
{NULL}
};
Expand Down Expand Up @@ -2448,7 +2448,7 @@ tuplegetter_reduce(_tuplegetterobject *self, PyObject *Py_UNUSED(ignored))


static PyMemberDef tuplegetter_members[] = {
{"__doc__", T_OBJECT, offsetof(_tuplegetterobject, doc), 0},
{"__doc__", T_OBJECT, offsetof(_tuplegetterobject, doc), PY_READWRITE},
{0}
};

Expand Down
12 changes: 6 additions & 6 deletions 12 Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ dialect_check_quoting(int quoting)
#define D_OFF(x) offsetof(DialectObj, x)

static struct PyMemberDef Dialect_memberlist[] = {
{ "skipinitialspace", T_BOOL, D_OFF(skipinitialspace), READONLY },
{ "doublequote", T_BOOL, D_OFF(doublequote), READONLY },
{ "strict", T_BOOL, D_OFF(strict), READONLY },
{ "skipinitialspace", T_BOOL, D_OFF(skipinitialspace), PY_READONLY },
{ "doublequote", T_BOOL, D_OFF(doublequote), PY_READONLY },
{ "strict", T_BOOL, D_OFF(strict), PY_READONLY },
{ NULL }
};

Expand Down Expand Up @@ -889,8 +889,8 @@ static struct PyMethodDef Reader_methods[] = {
#define R_OFF(x) offsetof(ReaderObj, x)

static struct PyMemberDef Reader_memberlist[] = {
{ "dialect", T_OBJECT, R_OFF(dialect), READONLY },
{ "line_num", T_ULONG, R_OFF(line_num), READONLY },
{ "dialect", T_OBJECT, R_OFF(dialect), PY_READONLY },
{ "line_num", T_ULONG, R_OFF(line_num), PY_READONLY },
{ NULL }
};

Expand Down Expand Up @@ -1285,7 +1285,7 @@ static struct PyMethodDef Writer_methods[] = {
#define W_OFF(x) offsetof(WriterObj, x)

static struct PyMemberDef Writer_memberlist[] = {
{ "dialect", T_OBJECT, W_OFF(dialect), READONLY },
{ "dialect", T_OBJECT, W_OFF(dialect), PY_READONLY },
{ NULL }
};

Expand Down
6 changes: 3 additions & 3 deletions 6 Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2666,13 +2666,13 @@ PyCData_dealloc(PyObject *self)

static PyMemberDef PyCData_members[] = {
{ "_b_base_", T_OBJECT,
offsetof(CDataObject, b_base), READONLY,
offsetof(CDataObject, b_base), PY_READONLY,
"the base object" },
{ "_b_needsfree_", T_INT,
offsetof(CDataObject, b_needsfree), READONLY,
offsetof(CDataObject, b_needsfree), PY_READONLY,
"whether the object owns the memory or not" },
{ "_objects", T_OBJECT,
offsetof(CDataObject, b_objects), READONLY,
offsetof(CDataObject, b_objects), PY_READONLY,
"internal objects tree (NEVER CHANGE THIS OBJECT!)"},
{ NULL },
};
Expand Down
2 changes: 1 addition & 1 deletion 2 Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ PyCArg_repr(PyCArgObject *self)

static PyMemberDef PyCArgType_members[] = {
{ "_obj", T_OBJECT,
offsetof(PyCArgObject, obj), READONLY,
offsetof(PyCArgObject, obj), PY_READONLY,
"the wrapped object" },
{ NULL },
};
Expand Down
6 changes: 3 additions & 3 deletions 6 Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2650,13 +2650,13 @@ delta_reduce(PyDateTime_Delta* self, PyObject *Py_UNUSED(ignored))

static PyMemberDef delta_members[] = {

{"days", T_INT, OFFSET(days), READONLY,
{"days", T_INT, OFFSET(days), PY_READONLY,
PyDoc_STR("Number of days.")},

{"seconds", T_INT, OFFSET(seconds), READONLY,
{"seconds", T_INT, OFFSET(seconds), PY_READONLY,
PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")},

{"microseconds", T_INT, OFFSET(microseconds), READONLY,
{"microseconds", T_INT, OFFSET(microseconds), PY_READONLY,
PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")},
{NULL}
};
Expand Down
4 changes: 2 additions & 2 deletions 4 Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -3753,8 +3753,8 @@ _elementtree_XMLParser__setevents_impl(XMLParserObject *self,
}

static PyMemberDef xmlparser_members[] = {
{"entity", T_OBJECT, offsetof(XMLParserObject, entity), READONLY, NULL},
{"target", T_OBJECT, offsetof(XMLParserObject, target), READONLY, NULL},
{"entity", T_OBJECT, offsetof(XMLParserObject, entity), PY_READONLY, NULL},
{"target", T_OBJECT, offsetof(XMLParserObject, target), PY_READONLY, NULL},
{NULL}
};

Expand Down
8 changes: 4 additions & 4 deletions 8 Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,11 @@ PyDoc_STRVAR(partial_doc,

#define OFF(x) offsetof(partialobject, x)
static PyMemberDef partial_memberlist[] = {
{"func", T_OBJECT, OFF(fn), READONLY,
{"func", T_OBJECT, OFF(fn), PY_READONLY,
"function object to use in future partial calls"},
{"args", T_OBJECT, OFF(args), READONLY,
{"args", T_OBJECT, OFF(args), PY_READONLY,
"tuple of arguments to future partial calls"},
{"keywords", T_OBJECT, OFF(kw), READONLY,
{"keywords", T_OBJECT, OFF(kw), PY_READONLY,
"dictionary of keyword arguments to future partial calls"},
{NULL} /* Sentinel */
};
Expand Down Expand Up @@ -460,7 +460,7 @@ keyobject_clear(keyobject *ko)

static PyMemberDef keyobject_members[] = {
{"obj", T_OBJECT,
offsetof(keyobject, object), 0,
offsetof(keyobject, object), PY_READWRITE,
PyDoc_STR("Value wrapped by a key function.")},
{NULL}
};
Expand Down
2 changes: 1 addition & 1 deletion 2 Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ EVP_get_digest_size(EVPobject *self, void *closure)
}

static PyMemberDef EVP_members[] = {
{"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")},
{"name", T_OBJECT, offsetof(EVPobject, name), PY_READONLY, PyDoc_STR("algorithm name.")},
{NULL} /* Sentinel */
};

Expand Down
12 changes: 6 additions & 6 deletions 12 Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2400,8 +2400,8 @@ static PyMethodDef bufferedreader_methods[] = {
};

static PyMemberDef bufferedreader_members[] = {
{"raw", T_OBJECT, offsetof(buffered, raw), READONLY},
{"_finalizing", T_BOOL, offsetof(buffered, finalizing), 0},
{"raw", T_OBJECT, offsetof(buffered, raw), PY_READONLY},
{"_finalizing", T_BOOL, offsetof(buffered, finalizing), PY_READWRITE},
{NULL}
};

Expand Down Expand Up @@ -2486,8 +2486,8 @@ static PyMethodDef bufferedwriter_methods[] = {
};

static PyMemberDef bufferedwriter_members[] = {
{"raw", T_OBJECT, offsetof(buffered, raw), READONLY},
{"_finalizing", T_BOOL, offsetof(buffered, finalizing), 0},
{"raw", T_OBJECT, offsetof(buffered, raw), PY_READONLY},
{"_finalizing", T_BOOL, offsetof(buffered, finalizing), PY_READWRITE},
{NULL}
};

Expand Down Expand Up @@ -2657,8 +2657,8 @@ static PyMethodDef bufferedrandom_methods[] = {
};

static PyMemberDef bufferedrandom_members[] = {
{"raw", T_OBJECT, offsetof(buffered, raw), READONLY},
{"_finalizing", T_BOOL, offsetof(buffered, finalizing), 0},
{"raw", T_OBJECT, offsetof(buffered, raw), PY_READONLY},
{"_finalizing", T_BOOL, offsetof(buffered, finalizing), PY_READWRITE},
{NULL}
};

Expand Down
4 changes: 2 additions & 2 deletions 4 Modules/_io/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1170,8 +1170,8 @@ static PyGetSetDef fileio_getsetlist[] = {
};

static PyMemberDef fileio_members[] = {
{"_blksize", T_UINT, offsetof(fileio, blksize), 0},
{"_finalizing", T_BOOL, offsetof(fileio, finalizing), 0},
{"_blksize", T_UINT, offsetof(fileio, blksize), PY_READWRITE},
{"_finalizing", T_BOOL, offsetof(fileio, finalizing), PY_READWRITE},
{NULL}
};

Expand Down
10 changes: 5 additions & 5 deletions 10 Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -3143,11 +3143,11 @@ static PyMethodDef textiowrapper_methods[] = {
};

static PyMemberDef textiowrapper_members[] = {
{"encoding", T_OBJECT, offsetof(textio, encoding), READONLY},
{"buffer", T_OBJECT, offsetof(textio, buffer), READONLY},
{"line_buffering", T_BOOL, offsetof(textio, line_buffering), READONLY},
{"write_through", T_BOOL, offsetof(textio, write_through), READONLY},
{"_finalizing", T_BOOL, offsetof(textio, finalizing), 0},
{"encoding", T_OBJECT, offsetof(textio, encoding), PY_READONLY},
{"buffer", T_OBJECT, offsetof(textio, buffer), PY_READONLY},
{"line_buffering", T_BOOL, offsetof(textio, line_buffering), PY_READONLY},
{"write_through", T_BOOL, offsetof(textio, write_through), PY_READONLY},
{"_finalizing", T_BOOL, offsetof(textio, finalizing), PY_READWRITE},
{NULL}
};

Expand Down
4 changes: 2 additions & 2 deletions 4 Modules/_io/winconsoleio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1107,8 +1107,8 @@ static PyGetSetDef winconsoleio_getsetlist[] = {
};

static PyMemberDef winconsoleio_members[] = {
{"_blksize", T_UINT, offsetof(winconsoleio, blksize), 0},
{"_finalizing", T_BOOL, offsetof(winconsoleio, finalizing), 0},
{"_blksize", T_UINT, offsetof(winconsoleio, blksize), PY_READWRITE},
{"_finalizing", T_BOOL, offsetof(winconsoleio, finalizing), PY_READWRITE},
{NULL}
};

Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.