annotate cos/python/Objects/object.c @ 210:67b0feafe5ae

Added missing file
author Windel Bouwman
date Sat, 29 Jun 2013 10:09:50 +0200
parents 7f74363f4c82
children
rev   line source
27
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2 /* Generic object operations; and implementation of None */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4 #include "Python.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
5 #include "frameobject.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
6
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
7 /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
8 These are used by the individual routines for object creation.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
9 Do not call them otherwise, they do not initialize the object! */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
10
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
11 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
12 Py_IncRef(PyObject *o)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
13 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
14 Py_XINCREF(o);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
15 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
16
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
17 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
18 Py_DecRef(PyObject *o)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
19 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
20 Py_XDECREF(o);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
21 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
22
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
23 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
24 PyObject_Init(PyObject *op, PyTypeObject *tp)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
25 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
26 if (op == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
27 return PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
28 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
29 Py_TYPE(op) = tp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
30 _Py_NewReference(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
31 return op;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
32 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
33
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
34 PyVarObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
35 PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
36 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
37 if (op == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
38 return (PyVarObject *) PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
39 /* Any changes should be reflected in PyObject_INIT_VAR */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
40 op->ob_size = size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
41 Py_TYPE(op) = tp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
42 _Py_NewReference((PyObject *)op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
43 return op;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
44 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
45
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
46 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
47 _PyObject_New(PyTypeObject *tp)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
48 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
49 PyObject *op;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
50 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
51 if (op == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
52 return PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
53 return PyObject_INIT(op, tp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
54 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
55
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
56 PyVarObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
57 _PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
58 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
59 PyVarObject *op;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
60 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
61 op = (PyVarObject *) PyObject_MALLOC(size);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
62 if (op == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
63 return (PyVarObject *)PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
64 return PyObject_INIT_VAR(op, tp, nitems);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
65 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
66
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
67 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
68 PyObject_Print(PyObject *op, FILE *fp, int flags)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
69 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
70 int ret = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
71 if (PyErr_CheckSignals())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
72 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
73 #ifdef USE_STACKCHECK
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
74 if (PyOS_CheckStack()) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
75 PyErr_SetString(PyExc_MemoryError, "stack overflow");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
76 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
77 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
78 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
79 clearerr(fp); /* Clear any previous error condition */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
80 if (op == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
81 Py_BEGIN_ALLOW_THREADS
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
82 fprintf(fp, "<nil>");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
83 Py_END_ALLOW_THREADS
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
84 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
85 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
86 if (op->ob_refcnt <= 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
87 /* XXX(twouters) cast refcount to long until %zd is
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
88 universally available */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
89 Py_BEGIN_ALLOW_THREADS
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
90 fprintf(fp, "<refcnt %ld at %p>",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
91 (long)op->ob_refcnt, op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
92 Py_END_ALLOW_THREADS
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
93 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
94 PyObject *s;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
95 if (flags & Py_PRINT_RAW)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
96 s = PyObject_Str(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
97 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
98 s = PyObject_Repr(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
99 if (s == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
100 ret = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
101 else if (PyBytes_Check(s)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
102 fwrite(PyBytes_AS_STRING(s), 1,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
103 PyBytes_GET_SIZE(s), fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
104 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
105 else if (PyUnicode_Check(s)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
106 PyObject *t;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
107 t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
108 if (t == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
109 ret = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
110 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
111 fwrite(PyBytes_AS_STRING(t), 1,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
112 PyBytes_GET_SIZE(t), fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
113 Py_DECREF(t);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
114 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
115 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
116 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
117 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
118 "str() or repr() returned '%.100s'",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
119 s->ob_type->tp_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
120 ret = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
121 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
122 Py_XDECREF(s);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
123 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
124 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
125 if (ret == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
126 if (ferror(fp)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
127 PyErr_SetFromErrno(PyExc_IOError);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
128 clearerr(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
129 ret = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
130 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
131 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
132 return ret;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
133 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
134
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
135 /* For debugging convenience. Set a breakpoint here and call it from your DLL */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
136 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
137 _Py_BreakPoint(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
138 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
139 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
140
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
141
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
142 /* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
143 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
144 _PyObject_Dump(PyObject* op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
145 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
146 if (op == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
147 fprintf(stderr, "NULL\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
148 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
149 #ifdef WITH_THREAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
150 PyGILState_STATE gil;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
151 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
152 fprintf(stderr, "object : ");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
153 #ifdef WITH_THREAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
154 gil = PyGILState_Ensure();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
155 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
156 (void)PyObject_Print(op, stderr, 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
157 #ifdef WITH_THREAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
158 PyGILState_Release(gil);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
159 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
160 /* XXX(twouters) cast refcount to long until %zd is
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
161 universally available */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
162 fprintf(stderr, "\n"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
163 "type : %s\n"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
164 "refcount: %ld\n"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
165 "address : %p\n",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
166 Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
167 (long)op->ob_refcnt,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
168 op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
169 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
170 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
171
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
172 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
173 PyObject_Repr(PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
174 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
175 PyObject *res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
176 if (PyErr_CheckSignals())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
177 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
178 #ifdef USE_STACKCHECK
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
179 if (PyOS_CheckStack()) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
180 PyErr_SetString(PyExc_MemoryError, "stack overflow");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
181 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
182 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
183 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
184 if (v == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
185 return PyUnicode_FromString("<NULL>");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
186 if (Py_TYPE(v)->tp_repr == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
187 return PyUnicode_FromFormat("<%s object at %p>",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
188 v->ob_type->tp_name, v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
189 res = (*v->ob_type->tp_repr)(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
190 if (res == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
191 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
192 if (!PyUnicode_Check(res)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
193 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
194 "__repr__ returned non-string (type %.200s)",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
195 res->ob_type->tp_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
196 Py_DECREF(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
197 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
198 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
199 #ifndef Py_DEBUG
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
200 if (PyUnicode_READY(res) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
201 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
202 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
203 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
204 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
205
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
206 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
207 PyObject_Str(PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
208 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
209 PyObject *res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
210 if (PyErr_CheckSignals())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
211 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
212 #ifdef USE_STACKCHECK
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
213 if (PyOS_CheckStack()) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
214 PyErr_SetString(PyExc_MemoryError, "stack overflow");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
215 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
216 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
217 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
218 if (v == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
219 return PyUnicode_FromString("<NULL>");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
220 if (PyUnicode_CheckExact(v)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
221 #ifndef Py_DEBUG
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
222 if (PyUnicode_READY(v) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
223 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
224 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
225 Py_INCREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
226 return v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
227 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
228 if (Py_TYPE(v)->tp_str == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
229 return PyObject_Repr(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
230
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
231 /* It is possible for a type to have a tp_str representation that loops
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
232 infinitely. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
233 if (Py_EnterRecursiveCall(" while getting the str of an object"))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
234 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
235 res = (*Py_TYPE(v)->tp_str)(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
236 Py_LeaveRecursiveCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
237 if (res == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
238 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
239 if (!PyUnicode_Check(res)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
240 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
241 "__str__ returned non-string (type %.200s)",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
242 Py_TYPE(res)->tp_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
243 Py_DECREF(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
244 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
245 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
246 #ifndef Py_DEBUG
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
247 if (PyUnicode_READY(res) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
248 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
249 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
250 assert(_PyUnicode_CheckConsistency(res, 1));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
251 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
252 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
253
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
254 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
255 PyObject_ASCII(PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
256 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
257 PyObject *repr, *ascii, *res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
258
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
259 repr = PyObject_Repr(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
260 if (repr == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
261 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
262
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
263 /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
264 ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
265 Py_DECREF(repr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
266 if (ascii == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
267 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
268
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
269 res = PyUnicode_DecodeASCII(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
270 PyBytes_AS_STRING(ascii),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
271 PyBytes_GET_SIZE(ascii),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
272 NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
273
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
274 Py_DECREF(ascii);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
275 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
276 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
277
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
278 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
279 PyObject_Bytes(PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
280 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
281 PyObject *result, *func;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
282 static PyObject *bytesstring = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
283
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
284 if (v == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
285 return PyBytes_FromString("<NULL>");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
286
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
287 if (PyBytes_CheckExact(v)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
288 Py_INCREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
289 return v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
290 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
291
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
292 func = _PyObject_LookupSpecial(v, "__bytes__", &bytesstring);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
293 if (func != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
294 result = PyObject_CallFunctionObjArgs(func, NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
295 Py_DECREF(func);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
296 if (result == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
297 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
298 if (!PyBytes_Check(result)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
299 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
300 "__bytes__ returned non-bytes (type %.200s)",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
301 Py_TYPE(result)->tp_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
302 Py_DECREF(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
303 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
304 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
305 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
306 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
307 else if (PyErr_Occurred())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
308 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
309 return PyBytes_FromObject(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
310 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
311
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
312 /* For Python 3.0.1 and later, the old three-way comparison has been
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
313 completely removed in favour of rich comparisons. PyObject_Compare() and
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
314 PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
315 The old tp_compare slot has been renamed to tp_reserved, and should no
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
316 longer be used. Use tp_richcompare instead.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
317
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
318 See (*) below for practical amendments.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
319
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
320 tp_richcompare gets called with a first argument of the appropriate type
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
321 and a second object of an arbitrary type. We never do any kind of
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
322 coercion.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
323
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
324 The tp_richcompare slot should return an object, as follows:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
325
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
326 NULL if an exception occurred
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
327 NotImplemented if the requested comparison is not implemented
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
328 any other false value if the requested comparison is false
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
329 any other true value if the requested comparison is true
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
330
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
331 The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
332 NotImplemented.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
333
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
334 (*) Practical amendments:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
335
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
336 - If rich comparison returns NotImplemented, == and != are decided by
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
337 comparing the object pointer (i.e. falling back to the base object
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
338 implementation).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
339
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
340 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
341
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
342 /* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
343 int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
344
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
345 static char *opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
346
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
347 /* Perform a rich comparison, raising TypeError when the requested comparison
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
348 operator is not supported. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
349 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
350 do_richcompare(PyObject *v, PyObject *w, int op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
351 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
352 richcmpfunc f;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
353 PyObject *res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
354 int checked_reverse_op = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
355
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
356 if (v->ob_type != w->ob_type &&
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
357 PyType_IsSubtype(w->ob_type, v->ob_type) &&
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
358 (f = w->ob_type->tp_richcompare) != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
359 checked_reverse_op = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
360 res = (*f)(w, v, _Py_SwappedOp[op]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
361 if (res != Py_NotImplemented)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
362 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
363 Py_DECREF(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
364 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
365 if ((f = v->ob_type->tp_richcompare) != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
366 res = (*f)(v, w, op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
367 if (res != Py_NotImplemented)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
368 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
369 Py_DECREF(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
370 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
371 if (!checked_reverse_op && (f = w->ob_type->tp_richcompare) != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
372 res = (*f)(w, v, _Py_SwappedOp[op]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
373 if (res != Py_NotImplemented)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
374 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
375 Py_DECREF(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
376 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
377 /* If neither object implements it, provide a sensible default
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
378 for == and !=, but raise an exception for ordering. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
379 switch (op) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
380 case Py_EQ:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
381 res = (v == w) ? Py_True : Py_False;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
382 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
383 case Py_NE:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
384 res = (v != w) ? Py_True : Py_False;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
385 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
386 default:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
387 /* XXX Special-case None so it doesn't show as NoneType() */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
388 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
389 "unorderable types: %.100s() %s %.100s()",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
390 v->ob_type->tp_name,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
391 opstrings[op],
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
392 w->ob_type->tp_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
393 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
394 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
395 Py_INCREF(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
396 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
397 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
398
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
399 /* Perform a rich comparison with object result. This wraps do_richcompare()
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
400 with a check for NULL arguments and a recursion check. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
401
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
402 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
403 PyObject_RichCompare(PyObject *v, PyObject *w, int op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
404 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
405 PyObject *res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
406
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
407 assert(Py_LT <= op && op <= Py_GE);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
408 if (v == NULL || w == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
409 if (!PyErr_Occurred())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
410 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
411 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
412 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
413 if (Py_EnterRecursiveCall(" in comparison"))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
414 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
415 res = do_richcompare(v, w, op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
416 Py_LeaveRecursiveCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
417 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
418 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
419
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
420 /* Perform a rich comparison with integer result. This wraps
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
421 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
422 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
423 PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
424 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
425 PyObject *res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
426 int ok;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
427
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
428 /* Quick result when objects are the same.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
429 Guarantees that identity implies equality. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
430 if (v == w) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
431 if (op == Py_EQ)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
432 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
433 else if (op == Py_NE)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
434 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
435 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
436
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
437 res = PyObject_RichCompare(v, w, op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
438 if (res == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
439 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
440 if (PyBool_Check(res))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
441 ok = (res == Py_True);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
442 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
443 ok = PyObject_IsTrue(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
444 Py_DECREF(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
445 return ok;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
446 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
447
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
448 /* Set of hash utility functions to help maintaining the invariant that
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
449 if a==b then hash(a)==hash(b)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
450
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
451 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
452 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
453
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
454 /* For numeric types, the hash of a number x is based on the reduction
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
455 of x modulo the prime P = 2**_PyHASH_BITS - 1. It's designed so that
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
456 hash(x) == hash(y) whenever x and y are numerically equal, even if
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
457 x and y have different types.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
458
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
459 A quick summary of the hashing strategy:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
460
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
461 (1) First define the 'reduction of x modulo P' for any rational
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
462 number x; this is a standard extension of the usual notion of
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
463 reduction modulo P for integers. If x == p/q (written in lowest
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
464 terms), the reduction is interpreted as the reduction of p times
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
465 the inverse of the reduction of q, all modulo P; if q is exactly
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
466 divisible by P then define the reduction to be infinity. So we've
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
467 got a well-defined map
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
468
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
469 reduce : { rational numbers } -> { 0, 1, 2, ..., P-1, infinity }.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
470
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
471 (2) Now for a rational number x, define hash(x) by:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
472
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
473 reduce(x) if x >= 0
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
474 -reduce(-x) if x < 0
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
475
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
476 If the result of the reduction is infinity (this is impossible for
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
477 integers, floats and Decimals) then use the predefined hash value
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
478 _PyHASH_INF for x >= 0, or -_PyHASH_INF for x < 0, instead.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
479 _PyHASH_INF, -_PyHASH_INF and _PyHASH_NAN are also used for the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
480 hashes of float and Decimal infinities and nans.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
481
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
482 A selling point for the above strategy is that it makes it possible
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
483 to compute hashes of decimal and binary floating-point numbers
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
484 efficiently, even if the exponent of the binary or decimal number
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
485 is large. The key point is that
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
486
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
487 reduce(x * y) == reduce(x) * reduce(y) (modulo _PyHASH_MODULUS)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
488
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
489 provided that {reduce(x), reduce(y)} != {0, infinity}. The reduction of a
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
490 binary or decimal float is never infinity, since the denominator is a power
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
491 of 2 (for binary) or a divisor of a power of 10 (for decimal). So we have,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
492 for nonnegative x,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
493
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
494 reduce(x * 2**e) == reduce(x) * reduce(2**e) % _PyHASH_MODULUS
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
495
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
496 reduce(x * 10**e) == reduce(x) * reduce(10**e) % _PyHASH_MODULUS
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
497
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
498 and reduce(10**e) can be computed efficiently by the usual modular
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
499 exponentiation algorithm. For reduce(2**e) it's even better: since
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
500 P is of the form 2**n-1, reduce(2**e) is 2**(e mod n), and multiplication
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
501 by 2**(e mod n) modulo 2**n-1 just amounts to a rotation of bits.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
502
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
503 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
504
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
505 Py_hash_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
506 _Py_HashDouble(double v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
507 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
508 int e, sign;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
509 double m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
510 Py_uhash_t x, y;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
511
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
512 if (!Py_IS_FINITE(v)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
513 if (Py_IS_INFINITY(v))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
514 return v > 0 ? _PyHASH_INF : -_PyHASH_INF;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
515 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
516 return _PyHASH_NAN;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
517 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
518
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
519 m = frexp(v, &e);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
520
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
521 sign = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
522 if (m < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
523 sign = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
524 m = -m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
525 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
526
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
527 /* process 28 bits at a time; this should work well both for binary
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
528 and hexadecimal floating point. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
529 x = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
530 while (m) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
531 x = ((x << 28) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - 28);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
532 m *= 268435456.0; /* 2**28 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
533 e -= 28;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
534 y = (Py_uhash_t)m; /* pull out integer part */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
535 m -= y;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
536 x += y;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
537 if (x >= _PyHASH_MODULUS)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
538 x -= _PyHASH_MODULUS;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
539 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
540
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
541 /* adjust for the exponent; first reduce it modulo _PyHASH_BITS */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
542 e = e >= 0 ? e % _PyHASH_BITS : _PyHASH_BITS-1-((-1-e) % _PyHASH_BITS);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
543 x = ((x << e) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - e);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
544
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
545 x = x * sign;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
546 if (x == (Py_uhash_t)-1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
547 x = (Py_uhash_t)-2;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
548 return (Py_hash_t)x;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
549 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
550
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
551 Py_hash_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
552 _Py_HashPointer(void *p)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
553 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
554 Py_hash_t x;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
555 size_t y = (size_t)p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
556 /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
557 excessive hash collisions for dicts and sets */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
558 y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
559 x = (Py_hash_t)y;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
560 if (x == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
561 x = -2;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
562 return x;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
563 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
564
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
565 Py_hash_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
566 _Py_HashBytes(unsigned char *p, Py_ssize_t len)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
567 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
568 Py_uhash_t x;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
569 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
570
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
571 x = (Py_uhash_t) *p << 7;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
572 for (i = 0; i < len; i++)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
573 x = (1000003U * x) ^ (Py_uhash_t) *p++;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
574 x ^= (Py_uhash_t) len;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
575 if (x == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
576 x = -2;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
577 return x;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
578 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
579
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
580 Py_hash_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
581 PyObject_HashNotImplemented(PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
582 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
583 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
584 Py_TYPE(v)->tp_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
585 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
586 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
587
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
588 Py_hash_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
589 PyObject_Hash(PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
590 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
591 PyTypeObject *tp = Py_TYPE(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
592 if (tp->tp_hash != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
593 return (*tp->tp_hash)(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
594 /* To keep to the general practice that inheriting
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
595 * solely from object in C code should work without
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
596 * an explicit call to PyType_Ready, we implicitly call
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
597 * PyType_Ready here and then check the tp_hash slot again
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
598 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
599 if (tp->tp_dict == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
600 if (PyType_Ready(tp) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
601 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
602 if (tp->tp_hash != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
603 return (*tp->tp_hash)(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
604 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
605 /* Otherwise, the object can't be hashed */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
606 return PyObject_HashNotImplemented(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
607 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
608
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
609 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
610 PyObject_GetAttrString(PyObject *v, const char *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
611 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
612 PyObject *w, *res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
613
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
614 if (Py_TYPE(v)->tp_getattr != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
615 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
616 w = PyUnicode_InternFromString(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
617 if (w == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
618 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
619 res = PyObject_GetAttr(v, w);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
620 Py_XDECREF(w);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
621 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
622 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
623
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
624 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
625 PyObject_HasAttrString(PyObject *v, const char *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
626 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
627 PyObject *res = PyObject_GetAttrString(v, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
628 if (res != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
629 Py_DECREF(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
630 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
631 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
632 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
633 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
634 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
635
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
636 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
637 PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
638 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
639 PyObject *s;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
640 int res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
641
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
642 if (Py_TYPE(v)->tp_setattr != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
643 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
644 s = PyUnicode_InternFromString(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
645 if (s == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
646 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
647 res = PyObject_SetAttr(v, s, w);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
648 Py_XDECREF(s);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
649 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
650 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
651
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
652 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
653 _PyObject_IsAbstract(PyObject *obj)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
654 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
655 int res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
656 PyObject* isabstract;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
657 _Py_IDENTIFIER(__isabstractmethod__);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
658
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
659 if (obj == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
660 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
661
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
662 isabstract = _PyObject_GetAttrId(obj, &PyId___isabstractmethod__);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
663 if (isabstract == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
664 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
665 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
666 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
667 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
668 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
669 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
670 res = PyObject_IsTrue(isabstract);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
671 Py_DECREF(isabstract);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
672 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
673 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
674
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
675 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
676 _PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
677 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
678 PyObject *result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
679 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
680 if (!oname)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
681 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
682 result = PyObject_GetAttr(v, oname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
683 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
684 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
685
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
686 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
687 _PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
688 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
689 int result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
690 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
691 if (!oname)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
692 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
693 result = PyObject_HasAttr(v, oname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
694 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
695 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
696
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
697 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
698 _PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
699 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
700 int result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
701 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
702 if (!oname)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
703 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
704 result = PyObject_SetAttr(v, oname, w);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
705 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
706 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
707
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
708 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
709 PyObject_GetAttr(PyObject *v, PyObject *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
710 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
711 PyTypeObject *tp = Py_TYPE(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
712
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
713 if (!PyUnicode_Check(name)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
714 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
715 "attribute name must be string, not '%.200s'",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
716 name->ob_type->tp_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
717 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
718 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
719 if (tp->tp_getattro != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
720 return (*tp->tp_getattro)(v, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
721 if (tp->tp_getattr != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
722 char *name_str = _PyUnicode_AsString(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
723 if (name_str == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
724 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
725 return (*tp->tp_getattr)(v, name_str);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
726 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
727 PyErr_Format(PyExc_AttributeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
728 "'%.50s' object has no attribute '%U'",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
729 tp->tp_name, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
730 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
731 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
732
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
733 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
734 PyObject_HasAttr(PyObject *v, PyObject *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
735 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
736 PyObject *res = PyObject_GetAttr(v, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
737 if (res != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
738 Py_DECREF(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
739 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
740 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
741 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
742 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
743 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
744
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
745 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
746 PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
747 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
748 PyTypeObject *tp = Py_TYPE(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
749 int err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
750
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
751 if (!PyUnicode_Check(name)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
752 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
753 "attribute name must be string, not '%.200s'",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
754 name->ob_type->tp_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
755 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
756 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
757 Py_INCREF(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
758
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
759 PyUnicode_InternInPlace(&name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
760 if (tp->tp_setattro != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
761 err = (*tp->tp_setattro)(v, name, value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
762 Py_DECREF(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
763 return err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
764 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
765 if (tp->tp_setattr != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
766 char *name_str = _PyUnicode_AsString(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
767 if (name_str == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
768 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
769 err = (*tp->tp_setattr)(v, name_str, value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
770 Py_DECREF(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
771 return err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
772 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
773 Py_DECREF(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
774 assert(name->ob_refcnt >= 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
775 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
776 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
777 "'%.100s' object has no attributes "
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
778 "(%s .%U)",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
779 tp->tp_name,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
780 value==NULL ? "del" : "assign to",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
781 name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
782 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
783 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
784 "'%.100s' object has only read-only attributes "
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
785 "(%s .%U)",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
786 tp->tp_name,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
787 value==NULL ? "del" : "assign to",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
788 name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
789 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
790 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
791
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
792 /* Helper to get a pointer to an object's __dict__ slot, if any */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
793
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
794 PyObject **
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
795 _PyObject_GetDictPtr(PyObject *obj)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
796 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
797 Py_ssize_t dictoffset;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
798 PyTypeObject *tp = Py_TYPE(obj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
799
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
800 dictoffset = tp->tp_dictoffset;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
801 if (dictoffset == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
802 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
803 if (dictoffset < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
804 Py_ssize_t tsize;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
805 size_t size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
806
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
807 tsize = ((PyVarObject *)obj)->ob_size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
808 if (tsize < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
809 tsize = -tsize;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
810 size = _PyObject_VAR_SIZE(tp, tsize);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
811
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
812 dictoffset += (long)size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
813 assert(dictoffset > 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
814 assert(dictoffset % SIZEOF_VOID_P == 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
815 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
816 return (PyObject **) ((char *)obj + dictoffset);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
817 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
818
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
819 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
820 PyObject_SelfIter(PyObject *obj)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
821 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
822 Py_INCREF(obj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
823 return obj;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
824 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
825
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
826 /* Helper used when the __next__ method is removed from a type:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
827 tp_iternext is never NULL and can be safely called without checking
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
828 on every iteration.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
829 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
830
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
831 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
832 _PyObject_NextNotImplemented(PyObject *self)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
833 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
834 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
835 "'%.200s' object is not iterable",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
836 Py_TYPE(self)->tp_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
837 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
838 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
839
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
840 /* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
841
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
842 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
843 _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
844 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
845 PyTypeObject *tp = Py_TYPE(obj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
846 PyObject *descr = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
847 PyObject *res = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
848 descrgetfunc f;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
849 Py_ssize_t dictoffset;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
850 PyObject **dictptr;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
851
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
852 if (!PyUnicode_Check(name)){
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
853 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
854 "attribute name must be string, not '%.200s'",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
855 name->ob_type->tp_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
856 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
857 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
858 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
859 Py_INCREF(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
860
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
861 if (tp->tp_dict == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
862 if (PyType_Ready(tp) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
863 goto done;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
864 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
865
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
866 descr = _PyType_Lookup(tp, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
867 Py_XINCREF(descr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
868
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
869 f = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
870 if (descr != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
871 f = descr->ob_type->tp_descr_get;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
872 if (f != NULL && PyDescr_IsData(descr)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
873 res = f(descr, obj, (PyObject *)obj->ob_type);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
874 Py_DECREF(descr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
875 goto done;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
876 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
877 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
878
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
879 if (dict == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
880 /* Inline _PyObject_GetDictPtr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
881 dictoffset = tp->tp_dictoffset;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
882 if (dictoffset != 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
883 if (dictoffset < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
884 Py_ssize_t tsize;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
885 size_t size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
886
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
887 tsize = ((PyVarObject *)obj)->ob_size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
888 if (tsize < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
889 tsize = -tsize;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
890 size = _PyObject_VAR_SIZE(tp, tsize);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
891
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
892 dictoffset += (long)size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
893 assert(dictoffset > 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
894 assert(dictoffset % SIZEOF_VOID_P == 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
895 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
896 dictptr = (PyObject **) ((char *)obj + dictoffset);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
897 dict = *dictptr;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
898 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
899 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
900 if (dict != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
901 Py_INCREF(dict);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
902 res = PyDict_GetItem(dict, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
903 if (res != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
904 Py_INCREF(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
905 Py_XDECREF(descr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
906 Py_DECREF(dict);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
907 goto done;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
908 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
909 Py_DECREF(dict);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
910 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
911
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
912 if (f != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
913 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
914 Py_DECREF(descr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
915 goto done;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
916 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
917
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
918 if (descr != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
919 res = descr;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
920 /* descr was already increfed above */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
921 goto done;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
922 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
923
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
924 PyErr_Format(PyExc_AttributeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
925 "'%.50s' object has no attribute '%U'",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
926 tp->tp_name, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
927 done:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
928 Py_DECREF(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
929 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
930 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
931
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
932 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
933 PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
934 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
935 return _PyObject_GenericGetAttrWithDict(obj, name, NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
936 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
937
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
938 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
939 _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
940 PyObject *value, PyObject *dict)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
941 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
942 PyTypeObject *tp = Py_TYPE(obj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
943 PyObject *descr;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
944 descrsetfunc f;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
945 PyObject **dictptr;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
946 int res = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
947
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
948 if (!PyUnicode_Check(name)){
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
949 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
950 "attribute name must be string, not '%.200s'",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
951 name->ob_type->tp_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
952 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
953 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
954 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
955 Py_INCREF(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
956
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
957 if (tp->tp_dict == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
958 if (PyType_Ready(tp) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
959 goto done;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
960 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
961
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
962 descr = _PyType_Lookup(tp, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
963 f = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
964 if (descr != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
965 f = descr->ob_type->tp_descr_set;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
966 if (f != NULL && PyDescr_IsData(descr)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
967 res = f(descr, obj, value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
968 goto done;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
969 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
970 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
971
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
972 if (dict == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
973 dictptr = _PyObject_GetDictPtr(obj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
974 if (dictptr != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
975 dict = *dictptr;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
976 if (dict == NULL && value != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
977 dict = PyDict_New();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
978 if (dict == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
979 goto done;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
980 *dictptr = dict;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
981 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
982 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
983 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
984 if (dict != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
985 Py_INCREF(dict);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
986 if (value == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
987 res = PyDict_DelItem(dict, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
988 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
989 res = PyDict_SetItem(dict, name, value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
990 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
991 PyErr_SetObject(PyExc_AttributeError, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
992 Py_DECREF(dict);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
993 goto done;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
994 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
995
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
996 if (f != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
997 res = f(descr, obj, value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
998 goto done;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
999 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1000
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1001 if (descr == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1002 PyErr_Format(PyExc_AttributeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1003 "'%.100s' object has no attribute '%U'",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1004 tp->tp_name, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1005 goto done;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1006 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1007
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1008 PyErr_Format(PyExc_AttributeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1009 "'%.50s' object attribute '%U' is read-only",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1010 tp->tp_name, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1011 done:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1012 Py_DECREF(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1013 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1014 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1015
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1016 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1017 PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1018 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1019 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1020 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1021
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1022
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1023 /* Test a value used as condition, e.g., in a for or if statement.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1024 Return -1 if an error occurred */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1025
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1026 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1027 PyObject_IsTrue(PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1028 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1029 Py_ssize_t res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1030 if (v == Py_True)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1031 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1032 if (v == Py_False)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1033 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1034 if (v == Py_None)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1035 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1036 else if (v->ob_type->tp_as_number != NULL &&
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1037 v->ob_type->tp_as_number->nb_bool != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1038 res = (*v->ob_type->tp_as_number->nb_bool)(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1039 else if (v->ob_type->tp_as_mapping != NULL &&
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1040 v->ob_type->tp_as_mapping->mp_length != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1041 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1042 else if (v->ob_type->tp_as_sequence != NULL &&
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1043 v->ob_type->tp_as_sequence->sq_length != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1044 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1045 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1046 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1047 /* if it is negative, it should be either -1 or -2 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1048 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1049 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1050
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1051 /* equivalent of 'not v'
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1052 Return -1 if an error occurred */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1053
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1054 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1055 PyObject_Not(PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1056 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1057 int res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1058 res = PyObject_IsTrue(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1059 if (res < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1060 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1061 return res == 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1062 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1063
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1064 /* Test whether an object can be called */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1065
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1066 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1067 PyCallable_Check(PyObject *x)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1068 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1069 if (x == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1070 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1071 return x->ob_type->tp_call != NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1072 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1073
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1074
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1075 /* Helper for PyObject_Dir without arguments: returns the local scope. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1076 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1077 _dir_locals(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1078 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1079 PyObject *names;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1080 PyObject *locals = PyEval_GetLocals();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1081
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1082 if (locals == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1083 PyErr_SetString(PyExc_SystemError, "frame does not exist");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1084 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1085 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1086
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1087 names = PyMapping_Keys(locals);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1088 if (!names)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1089 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1090 if (!PyList_Check(names)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1091 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1092 "dir(): expected keys() of locals to be a list, "
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1093 "not '%.200s'", Py_TYPE(names)->tp_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1094 Py_DECREF(names);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1095 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1096 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1097 if (PyList_Sort(names)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1098 Py_DECREF(names);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1099 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1100 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1101 /* the locals don't need to be DECREF'd */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1102 return names;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1103 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1104
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1105 /* Helper for PyObject_Dir: object introspection. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1106 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1107 _dir_object(PyObject *obj)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1108 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1109 PyObject *result, *sorted;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1110 static PyObject *dir_str = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1111 PyObject *dirfunc = _PyObject_LookupSpecial(obj, "__dir__", &dir_str);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1112
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1113 assert(obj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1114 if (dirfunc == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1115 if (!PyErr_Occurred())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1116 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1117 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1118 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1119 /* use __dir__ */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1120 result = PyObject_CallFunctionObjArgs(dirfunc, NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1121 Py_DECREF(dirfunc);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1122 if (result == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1123 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1124 /* return sorted(result) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1125 sorted = PySequence_List(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1126 Py_DECREF(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1127 if (sorted == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1128 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1129 if (PyList_Sort(sorted)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1130 Py_DECREF(sorted);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1131 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1132 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1133 return sorted;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1134 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1135
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1136 /* Implementation of dir() -- if obj is NULL, returns the names in the current
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1137 (local) scope. Otherwise, performs introspection of the object: returns a
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1138 sorted list of attribute names (supposedly) accessible from the object
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1139 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1140 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1141 PyObject_Dir(PyObject *obj)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1142 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1143 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1144 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1145
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1146 /*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1147 None is a non-NULL undefined value.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1148 There is (and should be!) no way to create other objects of this type,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1149 so there is exactly one (which is indestructible, by the way).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1150 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1151
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1152 /* ARGSUSED */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1153 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1154 none_repr(PyObject *op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1155 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1156 return PyUnicode_FromString("None");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1157 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1158
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1159 /* ARGUSED */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1160 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1161 none_dealloc(PyObject* ignore)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1162 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1163 /* This should never get called, but we also don't want to SEGV if
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1164 * we accidentally decref None out of existence.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1165 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1166 Py_FatalError("deallocating None");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1167 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1168
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1169 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1170 none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1171 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1172 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1173 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1174 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1175 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1176 Py_RETURN_NONE;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1177 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1178
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1179 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1180 none_bool(PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1181 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1182 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1183 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1184
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1185 static PyNumberMethods none_as_number = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1186 0, /* nb_add */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1187 0, /* nb_subtract */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1188 0, /* nb_multiply */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1189 0, /* nb_remainder */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1190 0, /* nb_divmod */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1191 0, /* nb_power */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1192 0, /* nb_negative */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1193 0, /* nb_positive */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1194 0, /* nb_absolute */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1195 (inquiry)none_bool, /* nb_bool */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1196 0, /* nb_invert */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1197 0, /* nb_lshift */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1198 0, /* nb_rshift */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1199 0, /* nb_and */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1200 0, /* nb_xor */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1201 0, /* nb_or */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1202 0, /* nb_int */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1203 0, /* nb_reserved */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1204 0, /* nb_float */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1205 0, /* nb_inplace_add */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1206 0, /* nb_inplace_subtract */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1207 0, /* nb_inplace_multiply */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1208 0, /* nb_inplace_remainder */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1209 0, /* nb_inplace_power */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1210 0, /* nb_inplace_lshift */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1211 0, /* nb_inplace_rshift */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1212 0, /* nb_inplace_and */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1213 0, /* nb_inplace_xor */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1214 0, /* nb_inplace_or */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1215 0, /* nb_floor_divide */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1216 0, /* nb_true_divide */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1217 0, /* nb_inplace_floor_divide */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1218 0, /* nb_inplace_true_divide */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1219 0, /* nb_index */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1220 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1221
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1222 static PyTypeObject PyNone_Type = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1223 PyVarObject_HEAD_INIT(&PyType_Type, 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1224 "NoneType",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1225 0,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1226 0,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1227 none_dealloc, /*tp_dealloc*/ /*never called*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1228 0, /*tp_print*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1229 0, /*tp_getattr*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1230 0, /*tp_setattr*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1231 0, /*tp_reserved*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1232 none_repr, /*tp_repr*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1233 &none_as_number, /*tp_as_number*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1234 0, /*tp_as_sequence*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1235 0, /*tp_as_mapping*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1236 0, /*tp_hash */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1237 0, /*tp_call */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1238 0, /*tp_str */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1239 0, /*tp_getattro */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1240 0, /*tp_setattro */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1241 0, /*tp_as_buffer */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1242 Py_TPFLAGS_DEFAULT, /*tp_flags */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1243 0, /*tp_doc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1244 0, /*tp_traverse */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1245 0, /*tp_clear */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1246 0, /*tp_richcompare */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1247 0, /*tp_weaklistoffset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1248 0, /*tp_iter */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1249 0, /*tp_iternext */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1250 0, /*tp_methods */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1251 0, /*tp_members */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1252 0, /*tp_getset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1253 0, /*tp_base */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1254 0, /*tp_dict */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1255 0, /*tp_descr_get */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1256 0, /*tp_descr_set */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1257 0, /*tp_dictoffset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1258 0, /*tp_init */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1259 0, /*tp_alloc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1260 none_new, /*tp_new */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1261 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1262
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1263 PyObject _Py_NoneStruct = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1264 _PyObject_EXTRA_INIT
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1265 1, &PyNone_Type
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1266 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1267
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1268 /* NotImplemented is an object that can be used to signal that an
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1269 operation is not implemented for the given type combination. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1270
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1271 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1272 NotImplemented_repr(PyObject *op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1273 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1274 return PyUnicode_FromString("NotImplemented");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1275 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1276
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1277 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1278 notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1279 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1280 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1281 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1282 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1283 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1284 Py_RETURN_NOTIMPLEMENTED;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1285 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1286
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1287 static PyTypeObject PyNotImplemented_Type = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1288 PyVarObject_HEAD_INIT(&PyType_Type, 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1289 "NotImplementedType",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1290 0,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1291 0,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1292 none_dealloc, /*tp_dealloc*/ /*never called*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1293 0, /*tp_print*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1294 0, /*tp_getattr*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1295 0, /*tp_setattr*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1296 0, /*tp_reserved*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1297 NotImplemented_repr, /*tp_repr*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1298 0, /*tp_as_number*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1299 0, /*tp_as_sequence*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1300 0, /*tp_as_mapping*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1301 0, /*tp_hash */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1302 0, /*tp_call */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1303 0, /*tp_str */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1304 0, /*tp_getattro */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1305 0, /*tp_setattro */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1306 0, /*tp_as_buffer */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1307 Py_TPFLAGS_DEFAULT, /*tp_flags */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1308 0, /*tp_doc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1309 0, /*tp_traverse */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1310 0, /*tp_clear */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1311 0, /*tp_richcompare */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1312 0, /*tp_weaklistoffset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1313 0, /*tp_iter */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1314 0, /*tp_iternext */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1315 0, /*tp_methods */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1316 0, /*tp_members */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1317 0, /*tp_getset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1318 0, /*tp_base */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1319 0, /*tp_dict */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1320 0, /*tp_descr_get */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1321 0, /*tp_descr_set */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1322 0, /*tp_dictoffset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1323 0, /*tp_init */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1324 0, /*tp_alloc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1325 notimplemented_new, /*tp_new */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1326 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1327
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1328 PyObject _Py_NotImplementedStruct = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1329 _PyObject_EXTRA_INIT
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1330 1, &PyNotImplemented_Type
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1331 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1332
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1333 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1334 _Py_ReadyTypes(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1335 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1336 if (PyType_Ready(&PyType_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1337 Py_FatalError("Can't initialize type type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1338
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1339 if (PyType_Ready(&_PyWeakref_RefType) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1340 Py_FatalError("Can't initialize weakref type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1341
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1342 if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1343 Py_FatalError("Can't initialize callable weakref proxy type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1344
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1345 if (PyType_Ready(&_PyWeakref_ProxyType) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1346 Py_FatalError("Can't initialize weakref proxy type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1347
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1348 if (PyType_Ready(&PyBool_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1349 Py_FatalError("Can't initialize bool type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1350
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1351 if (PyType_Ready(&PyByteArray_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1352 Py_FatalError("Can't initialize bytearray type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1353
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1354 if (PyType_Ready(&PyBytes_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1355 Py_FatalError("Can't initialize 'str'");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1356
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1357 if (PyType_Ready(&PyList_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1358 Py_FatalError("Can't initialize list type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1359
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1360 if (PyType_Ready(&PyNone_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1361 Py_FatalError("Can't initialize None type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1362
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1363 if (PyType_Ready(&PyNotImplemented_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1364 Py_FatalError("Can't initialize NotImplemented type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1365
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1366 if (PyType_Ready(&PyTraceBack_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1367 Py_FatalError("Can't initialize traceback type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1368
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1369 if (PyType_Ready(&PySuper_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1370 Py_FatalError("Can't initialize super type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1371
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1372 if (PyType_Ready(&PyBaseObject_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1373 Py_FatalError("Can't initialize object type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1374
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1375 if (PyType_Ready(&PyRange_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1376 Py_FatalError("Can't initialize range type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1377
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1378 if (PyType_Ready(&PyDict_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1379 Py_FatalError("Can't initialize dict type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1380
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1381 if (PyType_Ready(&PySet_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1382 Py_FatalError("Can't initialize set type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1383
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1384 if (PyType_Ready(&PyUnicode_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1385 Py_FatalError("Can't initialize str type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1386
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1387 if (PyType_Ready(&PySlice_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1388 Py_FatalError("Can't initialize slice type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1389
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1390 if (PyType_Ready(&PyStaticMethod_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1391 Py_FatalError("Can't initialize static method type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1392
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1393 if (PyType_Ready(&PyComplex_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1394 Py_FatalError("Can't initialize complex type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1395
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1396 if (PyType_Ready(&PyFloat_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1397 Py_FatalError("Can't initialize float type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1398
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1399 if (PyType_Ready(&PyLong_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1400 Py_FatalError("Can't initialize int type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1401
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1402 if (PyType_Ready(&PyFrozenSet_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1403 Py_FatalError("Can't initialize frozenset type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1404
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1405 if (PyType_Ready(&PyProperty_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1406 Py_FatalError("Can't initialize property type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1407
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1408 if (PyType_Ready(&PyMemoryView_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1409 Py_FatalError("Can't initialize memoryview type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1410
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1411 if (PyType_Ready(&PyTuple_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1412 Py_FatalError("Can't initialize tuple type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1413
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1414 if (PyType_Ready(&PyEnum_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1415 Py_FatalError("Can't initialize enumerate type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1416
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1417 if (PyType_Ready(&PyReversed_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1418 Py_FatalError("Can't initialize reversed type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1419
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1420 if (PyType_Ready(&PyStdPrinter_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1421 Py_FatalError("Can't initialize StdPrinter");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1422
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1423 if (PyType_Ready(&PyCode_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1424 Py_FatalError("Can't initialize code type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1425
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1426 if (PyType_Ready(&PyFrame_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1427 Py_FatalError("Can't initialize frame type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1428
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1429 if (PyType_Ready(&PyCFunction_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1430 Py_FatalError("Can't initialize builtin function type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1431
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1432 if (PyType_Ready(&PyMethod_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1433 Py_FatalError("Can't initialize method type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1434
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1435 if (PyType_Ready(&PyFunction_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1436 Py_FatalError("Can't initialize function type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1437
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1438 if (PyType_Ready(&PyDictProxy_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1439 Py_FatalError("Can't initialize dict proxy type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1440
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1441 if (PyType_Ready(&PyGen_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1442 Py_FatalError("Can't initialize generator type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1443
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1444 if (PyType_Ready(&PyGetSetDescr_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1445 Py_FatalError("Can't initialize get-set descriptor type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1446
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1447 if (PyType_Ready(&PyWrapperDescr_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1448 Py_FatalError("Can't initialize wrapper type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1449
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1450 if (PyType_Ready(&_PyMethodWrapper_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1451 Py_FatalError("Can't initialize method wrapper type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1452
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1453 if (PyType_Ready(&PyEllipsis_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1454 Py_FatalError("Can't initialize ellipsis type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1455
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1456 if (PyType_Ready(&PyMemberDescr_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1457 Py_FatalError("Can't initialize member descriptor type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1458
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1459 if (PyType_Ready(&PyFilter_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1460 Py_FatalError("Can't initialize filter type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1461
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1462 if (PyType_Ready(&PyMap_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1463 Py_FatalError("Can't initialize map type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1464
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1465 if (PyType_Ready(&PyZip_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1466 Py_FatalError("Can't initialize zip type");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1467 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1468
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1469
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1470 #ifdef Py_TRACE_REFS
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1471
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1472 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1473 _Py_NewReference(PyObject *op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1474 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1475 _Py_INC_REFTOTAL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1476 op->ob_refcnt = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1477 _Py_AddToAllObjects(op, 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1478 _Py_INC_TPALLOCS(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1479 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1480
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1481 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1482 _Py_ForgetReference(register PyObject *op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1483 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1484 #ifdef SLOW_UNREF_CHECK
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1485 register PyObject *p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1486 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1487 if (op->ob_refcnt < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1488 Py_FatalError("UNREF negative refcnt");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1489 if (op == &refchain ||
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1490 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1491 fprintf(stderr, "* ob\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1492 _PyObject_Dump(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1493 fprintf(stderr, "* op->_ob_prev->_ob_next\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1494 _PyObject_Dump(op->_ob_prev->_ob_next);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1495 fprintf(stderr, "* op->_ob_next->_ob_prev\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1496 _PyObject_Dump(op->_ob_next->_ob_prev);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1497 Py_FatalError("UNREF invalid object");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1498 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1499 #ifdef SLOW_UNREF_CHECK
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1500 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1501 if (p == op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1502 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1503 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1504 if (p == &refchain) /* Not found */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1505 Py_FatalError("UNREF unknown object");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1506 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1507 op->_ob_next->_ob_prev = op->_ob_prev;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1508 op->_ob_prev->_ob_next = op->_ob_next;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1509 op->_ob_next = op->_ob_prev = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1510 _Py_INC_TPFREES(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1511 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1512
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1513 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1514 _Py_Dealloc(PyObject *op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1515 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1516 destructor dealloc = Py_TYPE(op)->tp_dealloc;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1517 _Py_ForgetReference(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1518 (*dealloc)(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1519 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1520
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1521 /* Print all live objects. Because PyObject_Print is called, the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1522 * interpreter must be in a healthy state.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1523 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1524 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1525 _Py_PrintReferences(FILE *fp)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1526 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1527 PyObject *op;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1528 fprintf(fp, "Remaining objects:\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1529 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1530 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1531 if (PyObject_Print(op, fp, 0) != 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1532 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1533 putc('\n', fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1534 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1535 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1536
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1537 /* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1538 * doesn't make any calls to the Python C API, so is always safe to call.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1539 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1540 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1541 _Py_PrintReferenceAddresses(FILE *fp)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1542 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1543 PyObject *op;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1544 fprintf(fp, "Remaining object addresses:\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1545 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1546 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1547 op->ob_refcnt, Py_TYPE(op)->tp_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1548 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1549
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1550 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1551 _Py_GetObjects(PyObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1552 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1553 int i, n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1554 PyObject *t = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1555 PyObject *res, *op;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1556
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1557 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1558 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1559 op = refchain._ob_next;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1560 res = PyList_New(0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1561 if (res == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1562 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1563 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1564 while (op == self || op == args || op == res || op == t ||
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1565 (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1566 op = op->_ob_next;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1567 if (op == &refchain)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1568 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1569 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1570 if (PyList_Append(res, op) < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1571 Py_DECREF(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1572 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1573 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1574 op = op->_ob_next;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1575 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1576 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1577 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1578
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1579 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1580
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1581 /* Hack to force loading of pycapsule.o */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1582 PyTypeObject *_PyCapsule_hack = &PyCapsule_Type;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1583
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1584
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1585 /* Hack to force loading of abstract.o */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1586 Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1587
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1588
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1589 /* Python's malloc wrappers (see pymem.h) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1590
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1591 void *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1592 PyMem_Malloc(size_t nbytes)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1593 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1594 return PyMem_MALLOC(nbytes);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1595 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1596
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1597 void *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1598 PyMem_Realloc(void *p, size_t nbytes)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1599 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1600 return PyMem_REALLOC(p, nbytes);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1601 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1602
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1603 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1604 PyMem_Free(void *p)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1605 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1606 PyMem_FREE(p);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1607 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1608
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1609
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1610 /* These methods are used to control infinite recursion in repr, str, print,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1611 etc. Container objects that may recursively contain themselves,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1612 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1613 Py_ReprLeave() to avoid infinite recursion.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1614
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1615 Py_ReprEnter() returns 0 the first time it is called for a particular
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1616 object and 1 every time thereafter. It returns -1 if an exception
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1617 occurred. Py_ReprLeave() has no return value.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1618
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1619 See dictobject.c and listobject.c for examples of use.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1620 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1621
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1622 #define KEY "Py_Repr"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1623
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1624 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1625 Py_ReprEnter(PyObject *obj)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1626 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1627 PyObject *dict;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1628 PyObject *list;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1629 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1630
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1631 dict = PyThreadState_GetDict();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1632 if (dict == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1633 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1634 list = PyDict_GetItemString(dict, KEY);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1635 if (list == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1636 list = PyList_New(0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1637 if (list == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1638 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1639 if (PyDict_SetItemString(dict, KEY, list) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1640 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1641 Py_DECREF(list);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1642 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1643 i = PyList_GET_SIZE(list);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1644 while (--i >= 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1645 if (PyList_GET_ITEM(list, i) == obj)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1646 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1647 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1648 PyList_Append(list, obj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1649 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1650 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1651
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1652 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1653 Py_ReprLeave(PyObject *obj)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1654 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1655 PyObject *dict;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1656 PyObject *list;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1657 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1658
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1659 dict = PyThreadState_GetDict();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1660 if (dict == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1661 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1662 list = PyDict_GetItemString(dict, KEY);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1663 if (list == NULL || !PyList_Check(list))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1664 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1665 i = PyList_GET_SIZE(list);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1666 /* Count backwards because we always expect obj to be list[-1] */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1667 while (--i >= 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1668 if (PyList_GET_ITEM(list, i) == obj) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1669 PyList_SetSlice(list, i, i + 1, NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1670 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1671 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1672 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1673 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1674
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1675 /* Trashcan support. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1676
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1677 /* Current call-stack depth of tp_dealloc calls. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1678 int _PyTrash_delete_nesting = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1679
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1680 /* List of objects that still need to be cleaned up, singly linked via their
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1681 * gc headers' gc_prev pointers.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1682 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1683 PyObject *_PyTrash_delete_later = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1684
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1685 /* Add op to the _PyTrash_delete_later list. Called when the current
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1686 * call-stack depth gets large. op must be a currently untracked gc'ed
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1687 * object, with refcount 0. Py_DECREF must already have been called on it.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1688 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1689 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1690 _PyTrash_deposit_object(PyObject *op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1691 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1692 assert(PyObject_IS_GC(op));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1693 assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1694 assert(op->ob_refcnt == 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1695 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1696 _PyTrash_delete_later = op;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1697 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1698
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1699 /* Dealloccate all the objects in the _PyTrash_delete_later list. Called when
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1700 * the call-stack unwinds again.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1701 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1702 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1703 _PyTrash_destroy_chain(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1704 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1705 while (_PyTrash_delete_later) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1706 PyObject *op = _PyTrash_delete_later;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1707 destructor dealloc = Py_TYPE(op)->tp_dealloc;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1708
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1709 _PyTrash_delete_later =
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1710 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1711
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1712 /* Call the deallocator directly. This used to try to
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1713 * fool Py_DECREF into calling it indirectly, but
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1714 * Py_DECREF was already called on this object, and in
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1715 * assorted non-release builds calling Py_DECREF again ends
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1716 * up distorting allocation statistics.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1717 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1718 assert(op->ob_refcnt == 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1719 ++_PyTrash_delete_nesting;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1720 (*dealloc)(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1721 --_PyTrash_delete_nesting;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1722 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1723 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1724
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1725 #ifndef Py_TRACE_REFS
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1726 /* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1727 Define this here, so we can undefine the macro. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1728 #undef _Py_Dealloc
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1729 PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1730 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1731 _Py_Dealloc(PyObject *op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1732 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1733 _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1734 (*Py_TYPE(op)->tp_dealloc)(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1735 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1736 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1737