annotate Python/structmember.c @ 19942:8522b022f80a legacy-trunk

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.
author Guido van Rossum <guido@python.org>
date Mon, 17 Sep 2001 19:28:08 +0000
parents 7b3476c1382e
children 3faabf54b8b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
1
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Map C struct members to Python object attributes */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4 #include "Python.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #include "structmember.h"
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
8 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
9 listmembers(struct memberlist *mlist)
398
0ac1357cd898 Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
10 {
0ac1357cd898 Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
11 int i, n;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
12 PyObject *v;
398
0ac1357cd898 Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
13 for (n = 0; mlist[n].name != NULL; n++)
0ac1357cd898 Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
14 ;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
15 v = PyList_New(n);
398
0ac1357cd898 Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
16 if (v != NULL) {
0ac1357cd898 Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
17 for (i = 0; i < n; i++)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
18 PyList_SetItem(v, i,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
19 PyString_FromString(mlist[i].name));
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
20 if (PyErr_Occurred()) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
21 Py_DECREF(v);
398
0ac1357cd898 Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
22 v = NULL;
0ac1357cd898 Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
23 }
0ac1357cd898 Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
24 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
25 PyList_Sort(v);
398
0ac1357cd898 Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
26 }
0ac1357cd898 Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
27 }
0ac1357cd898 Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
28 return v;
0ac1357cd898 Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
29 }
0ac1357cd898 Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
30
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
31 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
32 PyMember_Get(char *addr, struct memberlist *mlist, char *name)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 struct memberlist *l;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
398
0ac1357cd898 Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
36 if (strcmp(name, "__members__") == 0)
0ac1357cd898 Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
37 return listmembers(mlist);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 for (l = mlist; l->name != NULL; l++) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 if (strcmp(l->name, name) == 0) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
40 PyObject *v;
19942
8522b022f80a Add support for restricting access based on restricted execution mode.
Guido van Rossum <guido@python.org>
parents: 16295
diff changeset
41 if ((l->flags & READ_RESTRICTED) &&
8522b022f80a Add support for restricting access based on restricted execution mode.
Guido van Rossum <guido@python.org>
parents: 16295
diff changeset
42 PyEval_GetRestricted()) {
8522b022f80a Add support for restricting access based on restricted execution mode.
Guido van Rossum <guido@python.org>
parents: 16295
diff changeset
43 PyErr_SetString(PyExc_RuntimeError,
8522b022f80a Add support for restricting access based on restricted execution mode.
Guido van Rossum <guido@python.org>
parents: 16295
diff changeset
44 "restricted attribute");
8522b022f80a Add support for restricting access based on restricted execution mode.
Guido van Rossum <guido@python.org>
parents: 16295
diff changeset
45 return NULL;
8522b022f80a Add support for restricting access based on restricted execution mode.
Guido van Rossum <guido@python.org>
parents: 16295
diff changeset
46 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 addr += l->offset;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 switch (l->type) {
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
49 case T_BYTE:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
50 v = PyInt_FromLong((long)
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
51 (((*(char*)addr & 0xff)
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
52 ^ 0x80) - 0x80));
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
53 break;
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
54 case T_UBYTE:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
55 v = PyInt_FromLong((long) *(char*)addr & 0xff);
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
56 break;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 case T_SHORT:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
58 v = PyInt_FromLong((long) *(short*)addr);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 break;
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
60 case T_USHORT:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
61 v = PyInt_FromLong((long)
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
62 *(unsigned short*)addr);
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
63 break;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 case T_INT:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
65 v = PyInt_FromLong((long) *(int*)addr);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 break;
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
67 case T_UINT:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
68 v = PyInt_FromLong((long)
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
69 *(unsigned int*)addr);
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
70 break;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 case T_LONG:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
72 v = PyInt_FromLong(*(long*)addr);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 break;
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
74 case T_ULONG:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
75 v = PyLong_FromDouble((double)
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
76 *(unsigned long*)addr);
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
77 break;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 case T_FLOAT:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
79 v = PyFloat_FromDouble((double)*(float*)addr);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 case T_DOUBLE:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
82 v = PyFloat_FromDouble(*(double*)addr);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 case T_STRING:
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 if (*(char**)addr == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
86 Py_INCREF(Py_None);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
87 v = Py_None;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 else
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
90 v = PyString_FromString(*(char**)addr);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 break;
2023
3d431a521132 - Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents: 1848
diff changeset
92 case T_STRING_INPLACE:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
93 v = PyString_FromString((char*)addr);
2023
3d431a521132 - Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents: 1848
diff changeset
94 break;
3d431a521132 - Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents: 1848
diff changeset
95 #ifdef macintosh
3d431a521132 - Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents: 1848
diff changeset
96 case T_PSTRING:
3d431a521132 - Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents: 1848
diff changeset
97 if (*(char**)addr == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
98 Py_INCREF(Py_None);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
99 v = Py_None;
2023
3d431a521132 - Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents: 1848
diff changeset
100 }
3d431a521132 - Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents: 1848
diff changeset
101 else
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
102 v = PyString_FromStringAndSize(
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
103 (*(char**)addr)+1,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
104 **(unsigned char**)addr);
2023
3d431a521132 - Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents: 1848
diff changeset
105 break;
3d431a521132 - Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents: 1848
diff changeset
106 case T_PSTRING_INPLACE:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
107 v = PyString_FromStringAndSize(
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
108 ((char*)addr)+1,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
109 *(unsigned char*)addr);
2023
3d431a521132 - Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents: 1848
diff changeset
110 break;
3d431a521132 - Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents: 1848
diff changeset
111 #endif /* macintosh */
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
112 case T_CHAR:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
113 v = PyString_FromStringAndSize((char*)addr, 1);
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
114 break;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 case T_OBJECT:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
116 v = *(PyObject **)addr;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 if (v == NULL)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
118 v = Py_None;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
119 Py_INCREF(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 default:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
122 PyErr_SetString(PyExc_SystemError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
123 "bad memberlist type");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 v = NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 return v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
129
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
130 PyErr_SetString(PyExc_AttributeError, name);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
133
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
135 PyMember_Set(char *addr, struct memberlist *mlist, char *name, PyObject *v)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 struct memberlist *l;
8398
ff31f2501742 Trivial little change: when setting a member to an object, hold the
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
138 PyObject *oldv;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
139
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 for (l = mlist; l->name != NULL; l++) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 if (strcmp(l->name, name) == 0) {
19942
8522b022f80a Add support for restricting access based on restricted execution mode.
Guido van Rossum <guido@python.org>
parents: 16295
diff changeset
142 if ((l->flags & READONLY) || l->type == T_STRING
2023
3d431a521132 - Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents: 1848
diff changeset
143 #ifdef macintosh
19942
8522b022f80a Add support for restricting access based on restricted execution mode.
Guido van Rossum <guido@python.org>
parents: 16295
diff changeset
144 || l->type == T_PSTRING
8522b022f80a Add support for restricting access based on restricted execution mode.
Guido van Rossum <guido@python.org>
parents: 16295
diff changeset
145 #endif
8522b022f80a Add support for restricting access based on restricted execution mode.
Guido van Rossum <guido@python.org>
parents: 16295
diff changeset
146 )
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
147 {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
148 PyErr_SetString(PyExc_TypeError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
149 "readonly attribute");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 return -1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 }
19942
8522b022f80a Add support for restricting access based on restricted execution mode.
Guido van Rossum <guido@python.org>
parents: 16295
diff changeset
152 if ((l->flags & WRITE_RESTRICTED) &&
8522b022f80a Add support for restricting access based on restricted execution mode.
Guido van Rossum <guido@python.org>
parents: 16295
diff changeset
153 PyEval_GetRestricted()) {
8522b022f80a Add support for restricting access based on restricted execution mode.
Guido van Rossum <guido@python.org>
parents: 16295
diff changeset
154 PyErr_SetString(PyExc_RuntimeError,
8522b022f80a Add support for restricting access based on restricted execution mode.
Guido van Rossum <guido@python.org>
parents: 16295
diff changeset
155 "restricted attribute");
8522b022f80a Add support for restricting access based on restricted execution mode.
Guido van Rossum <guido@python.org>
parents: 16295
diff changeset
156 return -1;
8522b022f80a Add support for restricting access based on restricted execution mode.
Guido van Rossum <guido@python.org>
parents: 16295
diff changeset
157 }
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
158 if (v == NULL && l->type != T_OBJECT) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
159 PyErr_SetString(PyExc_TypeError,
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
160 "can't delete numeric/char attribute");
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
161 return -1;
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
162 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 addr += l->offset;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 switch (l->type) {
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
165 case T_BYTE:
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
166 case T_UBYTE:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
167 if (!PyInt_Check(v)) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
168 PyErr_BadArgument();
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
169 return -1;
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
170 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
171 *(char*)addr = (char) PyInt_AsLong(v);
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
172 break;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 case T_SHORT:
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
174 case T_USHORT:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
175 if (!PyInt_Check(v)) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
176 PyErr_BadArgument();
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 return -1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
179 *(short*)addr = (short) PyInt_AsLong(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 break;
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
181 case T_UINT:
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 case T_INT:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
183 if (!PyInt_Check(v)) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
184 PyErr_BadArgument();
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 return -1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
187 *(int*)addr = (int) PyInt_AsLong(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 case T_LONG:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
190 if (!PyInt_Check(v)) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
191 PyErr_BadArgument();
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 return -1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
194 *(long*)addr = PyInt_AsLong(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 break;
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
196 case T_ULONG:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
197 if (PyInt_Check(v))
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
198 *(long*)addr = PyInt_AsLong(v);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
199 else if (PyLong_Check(v))
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
200 *(long*)addr = PyLong_AsLong(v);
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
201 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
202 PyErr_BadArgument();
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
203 return -1;
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
204 }
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
205 break;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 case T_FLOAT:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
207 if (PyInt_Check(v))
5287
5af766a1b531 Tweaks to keep the Microsoft compiler quier.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
208 *(float*)addr =
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
209 (float) PyInt_AsLong(v);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
210 else if (PyFloat_Check(v))
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
211 *(float*)addr =
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
212 (float) PyFloat_AsDouble(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
214 PyErr_BadArgument();
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 return -1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 case T_DOUBLE:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
219 if (PyInt_Check(v))
5287
5af766a1b531 Tweaks to keep the Microsoft compiler quier.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
220 *(double*)addr =
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
221 (double) PyInt_AsLong(v);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
222 else if (PyFloat_Check(v))
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
223 *(double*)addr = PyFloat_AsDouble(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
225 PyErr_BadArgument();
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 return -1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 case T_OBJECT:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
230 Py_XINCREF(v);
8398
ff31f2501742 Trivial little change: when setting a member to an object, hold the
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
231 oldv = *(PyObject **)addr;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
232 *(PyObject **)addr = v;
8398
ff31f2501742 Trivial little change: when setting a member to an object, hold the
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
233 Py_XDECREF(oldv);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 break;
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
235 case T_CHAR:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
236 if (PyString_Check(v) &&
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
237 PyString_Size(v) == 1) {
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
238 *(char*)addr =
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
239 PyString_AsString(v)[0];
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
240 }
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
241 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
242 PyErr_BadArgument();
888
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
243 return -1;
dd7dfad404cd Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
244 }
16295
7b3476c1382e SF bug http://sourceforge.net/bugs/?func=detailbug&bug_id=130242&group_id=5470
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
245 break;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 default:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
247 PyErr_SetString(PyExc_SystemError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
248 "bad memberlist type");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 return -1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 return 0;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
254
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
255 PyErr_SetString(PyExc_AttributeError, name);
95
a5bf7d5959ce Return -1 for errors.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
256 return -1;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 }