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

Skip to content

Commit c299fc1

Browse files
committed
Add support for restricting access based on restricted execution mode.
Renamed the 'readonly' field to 'flags' and defined some new flag bits: READ_RESTRICTED and WRITE_RESTRICTED, as well as a shortcut RESTRICTED that means both.
1 parent bf80a03 commit c299fc1

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

Include/structmember.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct memberlist {
3131
char *name;
3232
int type;
3333
int offset;
34-
int readonly;
34+
int flags;
3535
};
3636

3737
/* Types */
@@ -58,9 +58,13 @@ struct memberlist {
5858
#define T_PSTRING_INPLACE 15
5959
#endif /* macintosh */
6060

61-
/* Readonly flag */
61+
/* Flags */
6262
#define READONLY 1
6363
#define RO READONLY /* Shorthand */
64+
#define READ_RESTRICTED 2
65+
#define WRITE_RESTRICTED 4
66+
#define RESTRICTED (READ_RESTRICTED | WRITE_RESTRICTED)
67+
6468

6569
DL_IMPORT(PyObject *) PyMember_Get(char *, struct memberlist *, char *);
6670
DL_IMPORT(int) PyMember_Set(char *, struct memberlist *, char *, PyObject *);

Python/structmember.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ PyMember_Get(char *addr, struct memberlist *mlist, char *name)
3838
for (l = mlist; l->name != NULL; l++) {
3939
if (strcmp(l->name, name) == 0) {
4040
PyObject *v;
41+
if ((l->flags & READ_RESTRICTED) &&
42+
PyEval_GetRestricted()) {
43+
PyErr_SetString(PyExc_RuntimeError,
44+
"restricted attribute");
45+
return NULL;
46+
}
4147
addr += l->offset;
4248
switch (l->type) {
4349
case T_BYTE:
@@ -133,17 +139,22 @@ PyMember_Set(char *addr, struct memberlist *mlist, char *name, PyObject *v)
133139

134140
for (l = mlist; l->name != NULL; l++) {
135141
if (strcmp(l->name, name) == 0) {
142+
if ((l->flags & READONLY) || l->type == T_STRING
136143
#ifdef macintosh
137-
if (l->readonly || l->type == T_STRING ||
138-
l->type == T_PSTRING)
144+
|| l->type == T_PSTRING
145+
#endif
146+
)
139147
{
140-
#else
141-
if (l->readonly || l->type == T_STRING ) {
142-
#endif /* macintosh */
143148
PyErr_SetString(PyExc_TypeError,
144149
"readonly attribute");
145150
return -1;
146151
}
152+
if ((l->flags & WRITE_RESTRICTED) &&
153+
PyEval_GetRestricted()) {
154+
PyErr_SetString(PyExc_RuntimeError,
155+
"restricted attribute");
156+
return -1;
157+
}
147158
if (v == NULL && l->type != T_OBJECT) {
148159
PyErr_SetString(PyExc_TypeError,
149160
"can't delete numeric/char attribute");

0 commit comments

Comments
 (0)