Mercurial > cpython-lru_cache-weakref
annotate Objects/namespaceobject.c @ 83447:678564fc0962 3.3
#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 19 Apr 2013 05:45:27 +0300 |
| parents | c5124145e79e |
| children | 3b93ab8c9c20 |
| rev | line source |
|---|---|
|
77339
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
1 /* namespace object implementation */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
2 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
3 #include "Python.h" |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
4 #include "structmember.h" |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
5 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
6 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
7 typedef struct { |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
8 PyObject_HEAD |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
9 PyObject *ns_dict; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
10 } _PyNamespaceObject; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
11 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
12 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
13 static PyMemberDef namespace_members[] = { |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
14 {"__dict__", T_OBJECT, offsetof(_PyNamespaceObject, ns_dict), READONLY}, |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
15 {NULL} |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
16 }; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
17 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
18 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
19 /* Methods */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
20 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
21 static PyObject * |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
22 namespace_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
23 { |
|
79776
c5124145e79e
Close #16160: Subclass support now works for types.SimpleNamespace. Thanks to RDM for noticing.
Eric Snow <ericsnowcurrently@gmail.com>
parents:
77339
diff
changeset
|
24 PyObject *self; |
|
77339
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
25 |
|
79776
c5124145e79e
Close #16160: Subclass support now works for types.SimpleNamespace. Thanks to RDM for noticing.
Eric Snow <ericsnowcurrently@gmail.com>
parents:
77339
diff
changeset
|
26 assert(type != NULL && type->tp_alloc != NULL); |
|
c5124145e79e
Close #16160: Subclass support now works for types.SimpleNamespace. Thanks to RDM for noticing.
Eric Snow <ericsnowcurrently@gmail.com>
parents:
77339
diff
changeset
|
27 self = type->tp_alloc(type, 0); |
|
c5124145e79e
Close #16160: Subclass support now works for types.SimpleNamespace. Thanks to RDM for noticing.
Eric Snow <ericsnowcurrently@gmail.com>
parents:
77339
diff
changeset
|
28 if (self != NULL) { |
|
c5124145e79e
Close #16160: Subclass support now works for types.SimpleNamespace. Thanks to RDM for noticing.
Eric Snow <ericsnowcurrently@gmail.com>
parents:
77339
diff
changeset
|
29 _PyNamespaceObject *ns = (_PyNamespaceObject *)self; |
|
c5124145e79e
Close #16160: Subclass support now works for types.SimpleNamespace. Thanks to RDM for noticing.
Eric Snow <ericsnowcurrently@gmail.com>
parents:
77339
diff
changeset
|
30 ns->ns_dict = PyDict_New(); |
|
c5124145e79e
Close #16160: Subclass support now works for types.SimpleNamespace. Thanks to RDM for noticing.
Eric Snow <ericsnowcurrently@gmail.com>
parents:
77339
diff
changeset
|
31 if (ns->ns_dict == NULL) { |
|
c5124145e79e
Close #16160: Subclass support now works for types.SimpleNamespace. Thanks to RDM for noticing.
Eric Snow <ericsnowcurrently@gmail.com>
parents:
77339
diff
changeset
|
32 Py_DECREF(ns); |
|
c5124145e79e
Close #16160: Subclass support now works for types.SimpleNamespace. Thanks to RDM for noticing.
Eric Snow <ericsnowcurrently@gmail.com>
parents:
77339
diff
changeset
|
33 return NULL; |
|
c5124145e79e
Close #16160: Subclass support now works for types.SimpleNamespace. Thanks to RDM for noticing.
Eric Snow <ericsnowcurrently@gmail.com>
parents:
77339
diff
changeset
|
34 } |
|
77339
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
35 } |
|
79776
c5124145e79e
Close #16160: Subclass support now works for types.SimpleNamespace. Thanks to RDM for noticing.
Eric Snow <ericsnowcurrently@gmail.com>
parents:
77339
diff
changeset
|
36 return self; |
|
77339
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
37 } |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
38 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
39 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
40 static int |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
41 namespace_init(_PyNamespaceObject *ns, PyObject *args, PyObject *kwds) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
42 { |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
43 /* ignore args if it's NULL or empty */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
44 if (args != NULL) { |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
45 Py_ssize_t argcount = PyObject_Size(args); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
46 if (argcount < 0) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
47 return argcount; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
48 else if (argcount > 0) { |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
49 PyErr_Format(PyExc_TypeError, "no positional arguments expected"); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
50 return -1; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
51 } |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
52 } |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
53 if (kwds == NULL) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
54 return 0; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
55 return PyDict_Update(ns->ns_dict, kwds); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
56 } |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
57 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
58 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
59 static void |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
60 namespace_dealloc(_PyNamespaceObject *ns) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
61 { |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
62 PyObject_GC_UnTrack(ns); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
63 Py_CLEAR(ns->ns_dict); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
64 Py_TYPE(ns)->tp_free((PyObject *)ns); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
65 } |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
66 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
67 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
68 static PyObject * |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
69 namespace_repr(_PyNamespaceObject *ns) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
70 { |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
71 int i, loop_error = 0; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
72 PyObject *pairs = NULL, *d = NULL, *keys = NULL, *keys_iter = NULL; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
73 PyObject *key; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
74 PyObject *separator, *pairsrepr, *repr = NULL; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
75 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
76 i = Py_ReprEnter((PyObject *)ns); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
77 if (i != 0) { |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
78 return i > 0 ? PyUnicode_FromString("namespace(...)") : NULL; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
79 } |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
80 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
81 pairs = PyList_New(0); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
82 if (pairs == NULL) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
83 goto error; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
84 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
85 d = ((_PyNamespaceObject *)ns)->ns_dict; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
86 assert(d != NULL); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
87 Py_INCREF(d); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
88 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
89 keys = PyDict_Keys(d); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
90 if (keys == NULL) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
91 goto error; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
92 if (PyList_Sort(keys) != 0) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
93 goto error; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
94 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
95 keys_iter = PyObject_GetIter(keys); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
96 if (keys_iter == NULL) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
97 goto error; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
98 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
99 while ((key = PyIter_Next(keys_iter)) != NULL) { |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
100 if (PyUnicode_Check(key) && PyUnicode_GET_SIZE(key) > 0) { |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
101 PyObject *value, *item; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
102 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
103 value = PyDict_GetItem(d, key); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
104 assert(value != NULL); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
105 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
106 item = PyUnicode_FromFormat("%S=%R", key, value); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
107 if (item == NULL) { |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
108 loop_error = 1; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
109 } |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
110 else { |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
111 loop_error = PyList_Append(pairs, item); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
112 Py_DECREF(item); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
113 } |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
114 } |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
115 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
116 Py_DECREF(key); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
117 if (loop_error) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
118 goto error; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
119 } |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
120 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
121 separator = PyUnicode_FromString(", "); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
122 if (separator == NULL) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
123 goto error; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
124 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
125 pairsrepr = PyUnicode_Join(separator, pairs); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
126 Py_DECREF(separator); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
127 if (pairsrepr == NULL) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
128 goto error; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
129 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
130 repr = PyUnicode_FromFormat("%s(%S)", |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
131 ((PyObject *)ns)->ob_type->tp_name, pairsrepr); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
132 Py_DECREF(pairsrepr); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
133 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
134 error: |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
135 Py_XDECREF(pairs); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
136 Py_XDECREF(d); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
137 Py_XDECREF(keys); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
138 Py_XDECREF(keys_iter); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
139 Py_ReprLeave((PyObject *)ns); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
140 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
141 return repr; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
142 } |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
143 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
144 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
145 static int |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
146 namespace_traverse(_PyNamespaceObject *ns, visitproc visit, void *arg) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
147 { |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
148 Py_VISIT(ns->ns_dict); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
149 return 0; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
150 } |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
151 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
152 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
153 static int |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
154 namespace_clear(_PyNamespaceObject *ns) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
155 { |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
156 Py_CLEAR(ns->ns_dict); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
157 return 0; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
158 } |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
159 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
160 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
161 PyDoc_STRVAR(namespace_doc, |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
162 "A simple attribute-based namespace.\n\ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
163 \n\ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
164 namespace(**kwargs)"); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
165 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
166 PyTypeObject _PyNamespace_Type = { |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
167 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
168 "namespace", /* tp_name */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
169 sizeof(_PyNamespaceObject), /* tp_size */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
170 0, /* tp_itemsize */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
171 (destructor)namespace_dealloc, /* tp_dealloc */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
172 0, /* tp_print */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
173 0, /* tp_getattr */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
174 0, /* tp_setattr */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
175 0, /* tp_reserved */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
176 (reprfunc)namespace_repr, /* tp_repr */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
177 0, /* tp_as_number */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
178 0, /* tp_as_sequence */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
179 0, /* tp_as_mapping */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
180 0, /* tp_hash */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
181 0, /* tp_call */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
182 0, /* tp_str */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
183 PyObject_GenericGetAttr, /* tp_getattro */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
184 PyObject_GenericSetAttr, /* tp_setattro */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
185 0, /* tp_as_buffer */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
186 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
187 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
188 namespace_doc, /* tp_doc */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
189 (traverseproc)namespace_traverse, /* tp_traverse */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
190 (inquiry)namespace_clear, /* tp_clear */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
191 0, /* tp_richcompare */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
192 0, /* tp_weaklistoffset */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
193 0, /* tp_iter */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
194 0, /* tp_iternext */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
195 0, /* tp_methods */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
196 namespace_members, /* tp_members */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
197 0, /* tp_getset */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
198 0, /* tp_base */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
199 0, /* tp_dict */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
200 0, /* tp_descr_get */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
201 0, /* tp_descr_set */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
202 offsetof(_PyNamespaceObject, ns_dict), /* tp_dictoffset */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
203 (initproc)namespace_init, /* tp_init */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
204 PyType_GenericAlloc, /* tp_alloc */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
205 (newfunc)namespace_new, /* tp_new */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
206 PyObject_GC_Del, /* tp_free */ |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
207 }; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
208 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
209 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
210 PyObject * |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
211 _PyNamespace_New(PyObject *kwds) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
212 { |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
213 PyObject *ns = namespace_new(&_PyNamespace_Type, NULL, NULL); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
214 if (ns == NULL) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
215 return NULL; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
216 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
217 if (kwds == NULL) |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
218 return ns; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
219 if (PyDict_Update(((_PyNamespaceObject *)ns)->ns_dict, kwds) != 0) { |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
220 Py_DECREF(ns); |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
221 return NULL; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
222 } |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
223 |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
224 return (PyObject *)ns; |
|
9c445f4695c1
Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
225 } |
