Mercurial > cpython-lru_cache-weakref
annotate Python/structmember.c @ 5758:bfe9ba74c19e legacy-trunk
Fix problem discovered by Greg McFarlane: when an imported module
replaces its own entry in sys.module, reference count errors ensue;
even if there is no reference count problem, it would be preferable
for the import to yield the new thing in sys.modules anyway (if only
because that's what later imports will yield). This opens the road to
an official hack to implement a __getattr__ like feature for modules:
stick an instance in sys.modules[__name__].
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 10 Jul 1997 18:00:45 +0000 |
| parents | 5880384d7c71 |
| children | ff31f2501742 |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
2062
d0d0a69b5ca3
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2023
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
d0d0a69b5ca3
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2023
diff
changeset
|
3 The Netherlands. |
| 145 | 4 |
| 5 All Rights Reserved | |
| 6 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 145 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 145 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
15 permission. |
| 145 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 145 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
| 86 | 32 /* Map C struct members to Python object attributes */ |
| 33 | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
34 #include "Python.h" |
| 86 | 35 |
| 36 #include "structmember.h" | |
| 37 | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
38 static PyObject * |
|
398
0ac1357cd898
Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
39 listmembers(mlist) |
|
0ac1357cd898
Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
40 struct memberlist *mlist; |
|
0ac1357cd898
Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
41 { |
|
0ac1357cd898
Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
42 int i, n; |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
43 PyObject *v; |
|
398
0ac1357cd898
Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
44 for (n = 0; mlist[n].name != NULL; n++) |
|
0ac1357cd898
Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
45 ; |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
46 v = PyList_New(n); |
|
398
0ac1357cd898
Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
47 if (v != NULL) { |
|
0ac1357cd898
Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
48 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
|
49 PyList_SetItem(v, i, |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
50 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
|
51 if (PyErr_Occurred()) { |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
52 Py_DECREF(v); |
|
398
0ac1357cd898
Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
53 v = NULL; |
|
0ac1357cd898
Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
54 } |
|
0ac1357cd898
Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
55 else { |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
56 PyList_Sort(v); |
|
398
0ac1357cd898
Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
57 } |
|
0ac1357cd898
Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
58 } |
|
0ac1357cd898
Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
59 return v; |
|
0ac1357cd898
Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
60 } |
|
0ac1357cd898
Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
61 |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
62 PyObject * |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
63 PyMember_Get(addr, mlist, name) |
| 86 | 64 char *addr; |
| 65 struct memberlist *mlist; | |
| 66 char *name; | |
| 67 { | |
| 68 struct memberlist *l; | |
| 69 | |
|
398
0ac1357cd898
Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
70 if (strcmp(name, "__members__") == 0) |
|
0ac1357cd898
Added __members__ secret attribute
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
71 return listmembers(mlist); |
| 86 | 72 for (l = mlist; l->name != NULL; l++) { |
| 73 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
|
74 PyObject *v; |
| 86 | 75 addr += l->offset; |
| 76 switch (l->type) { | |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
77 case T_BYTE: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
78 v = PyInt_FromLong((long) |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
79 (((*(char*)addr & 0xff) |
|
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
80 ^ 0x80) - 0x80)); |
|
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
81 break; |
|
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
82 case T_UBYTE: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
83 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
|
84 break; |
| 86 | 85 case T_SHORT: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
86 v = PyInt_FromLong((long) *(short*)addr); |
| 86 | 87 break; |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
88 case T_USHORT: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
89 v = PyInt_FromLong((long) |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
90 *(unsigned short*)addr); |
|
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
91 break; |
| 86 | 92 case T_INT: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
93 v = PyInt_FromLong((long) *(int*)addr); |
| 86 | 94 break; |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
95 case T_UINT: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
96 v = PyInt_FromLong((long) |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
97 *(unsigned int*)addr); |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
98 break; |
| 86 | 99 case T_LONG: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
100 v = PyInt_FromLong(*(long*)addr); |
| 86 | 101 break; |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
102 case T_ULONG: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
103 v = PyLong_FromDouble((double) |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
104 *(unsigned long*)addr); |
|
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
105 break; |
| 86 | 106 case T_FLOAT: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
107 v = PyFloat_FromDouble((double)*(float*)addr); |
| 86 | 108 break; |
| 109 case T_DOUBLE: | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
110 v = PyFloat_FromDouble(*(double*)addr); |
| 86 | 111 break; |
| 112 case T_STRING: | |
| 113 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
|
114 Py_INCREF(Py_None); |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
115 v = Py_None; |
| 86 | 116 } |
| 117 else | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
118 v = PyString_FromString(*(char**)addr); |
| 86 | 119 break; |
|
2023
3d431a521132
- Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents:
1848
diff
changeset
|
120 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
|
121 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
|
122 break; |
|
3d431a521132
- Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents:
1848
diff
changeset
|
123 #ifdef macintosh |
|
3d431a521132
- Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents:
1848
diff
changeset
|
124 case T_PSTRING: |
|
3d431a521132
- Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents:
1848
diff
changeset
|
125 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
|
126 Py_INCREF(Py_None); |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
127 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
|
128 } |
|
3d431a521132
- Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents:
1848
diff
changeset
|
129 else |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
130 v = PyString_FromStringAndSize( |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
131 (*(char**)addr)+1, |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
132 **(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
|
133 break; |
|
3d431a521132
- Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents:
1848
diff
changeset
|
134 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
|
135 v = PyString_FromStringAndSize( |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
136 ((char*)addr)+1, |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
137 *(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
|
138 break; |
|
3d431a521132
- Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents:
1848
diff
changeset
|
139 #endif /* macintosh */ |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
140 case T_CHAR: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
141 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
|
142 break; |
| 86 | 143 case T_OBJECT: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
144 v = *(PyObject **)addr; |
| 86 | 145 if (v == NULL) |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
146 v = Py_None; |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
147 Py_INCREF(v); |
| 86 | 148 break; |
| 149 default: | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
150 PyErr_SetString(PyExc_SystemError, |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
151 "bad memberlist type"); |
| 86 | 152 v = NULL; |
| 153 } | |
| 154 return v; | |
| 155 } | |
| 156 } | |
| 157 | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
158 PyErr_SetString(PyExc_AttributeError, name); |
| 86 | 159 return NULL; |
| 160 } | |
| 161 | |
| 162 int | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
163 PyMember_Set(addr, mlist, name, v) |
| 86 | 164 char *addr; |
| 165 struct memberlist *mlist; | |
| 166 char *name; | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
167 PyObject *v; |
| 86 | 168 { |
| 169 struct memberlist *l; | |
| 170 | |
| 171 for (l = mlist; l->name != NULL; l++) { | |
| 172 if (strcmp(l->name, name) == 0) { | |
|
2023
3d431a521132
- Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents:
1848
diff
changeset
|
173 #ifdef macintosh |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
174 if (l->readonly || l->type == T_STRING || |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
175 l->type == T_PSTRING) |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
176 { |
|
2023
3d431a521132
- Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents:
1848
diff
changeset
|
177 #else |
|
3d431a521132
- Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents:
1848
diff
changeset
|
178 if (l->readonly || l->type == T_STRING ) { |
|
3d431a521132
- Added ability to get at strings embedded in the struct
Jack Jansen <jack.jansen@cwi.nl>
parents:
1848
diff
changeset
|
179 #endif /* macintosh */ |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
180 PyErr_SetString(PyExc_TypeError, |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
181 "readonly attribute"); |
| 86 | 182 return -1; |
| 183 } | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
184 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
|
185 PyErr_SetString(PyExc_TypeError, |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
186 "can't delete numeric/char attribute"); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
187 return -1; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
188 } |
| 86 | 189 addr += l->offset; |
| 190 switch (l->type) { | |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
191 case T_BYTE: |
|
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
192 case T_UBYTE: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
193 if (!PyInt_Check(v)) { |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
194 PyErr_BadArgument(); |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
195 return -1; |
|
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
196 } |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
197 *(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
|
198 break; |
| 86 | 199 case T_SHORT: |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
200 case T_USHORT: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
201 if (!PyInt_Check(v)) { |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
202 PyErr_BadArgument(); |
| 86 | 203 return -1; |
| 204 } | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
205 *(short*)addr = (short) PyInt_AsLong(v); |
| 86 | 206 break; |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
207 case T_UINT: |
| 86 | 208 case T_INT: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
209 if (!PyInt_Check(v)) { |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
210 PyErr_BadArgument(); |
| 86 | 211 return -1; |
| 212 } | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
213 *(int*)addr = (int) PyInt_AsLong(v); |
| 86 | 214 break; |
| 215 case T_LONG: | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
216 if (!PyInt_Check(v)) { |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
217 PyErr_BadArgument(); |
| 86 | 218 return -1; |
| 219 } | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
220 *(long*)addr = PyInt_AsLong(v); |
| 86 | 221 break; |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
222 case T_ULONG: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
223 if (PyInt_Check(v)) |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
224 *(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
|
225 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
|
226 *(long*)addr = PyLong_AsLong(v); |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
227 else { |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
228 PyErr_BadArgument(); |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
229 return -1; |
|
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
230 } |
|
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
231 break; |
| 86 | 232 case T_FLOAT: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
233 if (PyInt_Check(v)) |
|
5287
5af766a1b531
Tweaks to keep the Microsoft compiler quier.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
234 *(float*)addr = |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
235 (float) PyInt_AsLong(v); |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
236 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
|
237 *(float*)addr = |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
238 (float) PyFloat_AsDouble(v); |
| 86 | 239 else { |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
240 PyErr_BadArgument(); |
| 86 | 241 return -1; |
| 242 } | |
| 243 break; | |
| 244 case T_DOUBLE: | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
245 if (PyInt_Check(v)) |
|
5287
5af766a1b531
Tweaks to keep the Microsoft compiler quier.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
246 *(double*)addr = |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
247 (double) PyInt_AsLong(v); |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
248 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
|
249 *(double*)addr = PyFloat_AsDouble(v); |
| 86 | 250 else { |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
251 PyErr_BadArgument(); |
| 86 | 252 return -1; |
| 253 } | |
| 254 break; | |
| 255 case T_OBJECT: | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
256 Py_XDECREF(*(PyObject **)addr); |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
257 Py_XINCREF(v); |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
258 *(PyObject **)addr = v; |
| 86 | 259 break; |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
260 case T_CHAR: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
261 if (PyString_Check(v) && |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
262 PyString_Size(v) == 1) { |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
263 *(char*)addr = |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
264 PyString_AsString(v)[0]; |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
265 } |
|
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
266 else { |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
267 PyErr_BadArgument(); |
|
888
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
268 return -1; |
|
dd7dfad404cd
Added new types to setattr() as well...
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
269 } |
| 86 | 270 default: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
271 PyErr_SetString(PyExc_SystemError, |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
272 "bad memberlist type"); |
| 86 | 273 return -1; |
| 274 } | |
| 275 return 0; | |
| 276 } | |
| 277 } | |
| 278 | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
279 PyErr_SetString(PyExc_AttributeError, name); |
| 95 | 280 return -1; |
| 86 | 281 } |
