annotate Objects/namespaceobject.c @ 87488:124e51c19e4f

Issue #19734: Also run pip version check in isolated mode
author Nick Coghlan <ncoghlan@gmail.com>
date Sun, 24 Nov 2013 16:49:20 +1000
parents 28f71af02b69
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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)
84028
93f4b32fc95c Issue #9566: Fix a compiler warning on Windows 64-bit in namespace_init()
Victor Stinner <victor.stinner@gmail.com>
parents: 82235
diff changeset
47 return -1;
77339
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 *
82232
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
69 namespace_repr(PyObject *ns)
77339
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;
82232
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
75 const char * name;
77339
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
76
82232
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
77 name = (Py_TYPE(ns) == &_PyNamespace_Type) ? "namespace"
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
78 : ns->ob_type->tp_name;
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
79
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
80 i = Py_ReprEnter(ns);
77339
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
81 if (i != 0) {
82232
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
82 return i > 0 ? PyUnicode_FromFormat("%s(...)", name) : NULL;
77339
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
83 }
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 pairs = PyList_New(0);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
86 if (pairs == NULL)
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
87 goto error;
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 d = ((_PyNamespaceObject *)ns)->ns_dict;
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
90 assert(d != NULL);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
91 Py_INCREF(d);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
92
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
93 keys = PyDict_Keys(d);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
94 if (keys == NULL)
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
95 goto error;
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
96 if (PyList_Sort(keys) != 0)
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 keys_iter = PyObject_GetIter(keys);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
100 if (keys_iter == NULL)
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
101 goto error;
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 while ((key = PyIter_Next(keys_iter)) != NULL) {
87083
28f71af02b69 Don't use deprecated function PyUnicode_GET_SIZE()
Victor Stinner <victor.stinner@gmail.com>
parents: 85157
diff changeset
104 if (PyUnicode_Check(key) && PyUnicode_GET_LENGTH(key) > 0) {
77339
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
105 PyObject *value, *item;
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
106
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
107 value = PyDict_GetItem(d, key);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
108 assert(value != NULL);
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 item = PyUnicode_FromFormat("%S=%R", key, value);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
111 if (item == NULL) {
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
112 loop_error = 1;
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 else {
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
115 loop_error = PyList_Append(pairs, item);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
116 Py_DECREF(item);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
117 }
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
118 }
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 Py_DECREF(key);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
121 if (loop_error)
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
122 goto error;
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
123 }
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 separator = PyUnicode_FromString(", ");
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
126 if (separator == NULL)
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
127 goto error;
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
128
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
129 pairsrepr = PyUnicode_Join(separator, pairs);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
130 Py_DECREF(separator);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
131 if (pairsrepr == NULL)
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
132 goto error;
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
133
82232
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
134 repr = PyUnicode_FromFormat("%s(%S)", name, pairsrepr);
77339
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
135 Py_DECREF(pairsrepr);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
136
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
137 error:
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
138 Py_XDECREF(pairs);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
139 Py_XDECREF(d);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
140 Py_XDECREF(keys);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
141 Py_XDECREF(keys_iter);
82232
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
142 Py_ReprLeave(ns);
77339
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 return repr;
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
145 }
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
146
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 static int
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
149 namespace_traverse(_PyNamespaceObject *ns, visitproc visit, void *arg)
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 Py_VISIT(ns->ns_dict);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
152 return 0;
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
153 }
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
154
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 static int
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
157 namespace_clear(_PyNamespaceObject *ns)
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 Py_CLEAR(ns->ns_dict);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
160 return 0;
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
161 }
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
162
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
163
82232
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
164 static PyObject *
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
165 namespace_richcompare(PyObject *self, PyObject *other, int op)
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
166 {
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
167 if (PyObject_IsInstance(self, (PyObject *)&_PyNamespace_Type) &&
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
168 PyObject_IsInstance(other, (PyObject *)&_PyNamespace_Type))
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
169 return PyObject_RichCompare(((_PyNamespaceObject *)self)->ns_dict,
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
170 ((_PyNamespaceObject *)other)->ns_dict, op);
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
171 Py_INCREF(Py_NotImplemented);
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
172 return Py_NotImplemented;
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
173 }
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
174
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
175
82235
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
176 PyDoc_STRVAR(namespace_reduce__doc__, "Return state information for pickling");
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
177
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
178 static PyObject *
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84028
diff changeset
179 namespace_reduce(_PyNamespaceObject *ns)
82235
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
180 {
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
181 PyObject *result, *args = PyTuple_New(0);
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
182
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
183 if (!args)
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
184 return NULL;
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
185
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
186 result = PyTuple_Pack(3, (PyObject *)Py_TYPE(ns), args, ns->ns_dict);
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
187 Py_DECREF(args);
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
188 return result;
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
189 }
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
190
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
191
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
192 static PyMethodDef namespace_methods[] = {
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
193 {"__reduce__", (PyCFunction)namespace_reduce, METH_NOARGS,
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
194 namespace_reduce__doc__},
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
195 {NULL, NULL} /* sentinel */
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
196 };
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
197
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
198
77339
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
199 PyDoc_STRVAR(namespace_doc,
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
200 "A simple attribute-based namespace.\n\
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
201 \n\
82232
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
202 SimpleNamespace(**kwargs)");
77339
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
203
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
204 PyTypeObject _PyNamespace_Type = {
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
205 PyVarObject_HEAD_INIT(&PyType_Type, 0)
82232
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
206 "types.SimpleNamespace", /* tp_name */
77339
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
207 sizeof(_PyNamespaceObject), /* tp_size */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
208 0, /* tp_itemsize */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
209 (destructor)namespace_dealloc, /* tp_dealloc */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
210 0, /* tp_print */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
211 0, /* tp_getattr */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
212 0, /* tp_setattr */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
213 0, /* tp_reserved */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
214 (reprfunc)namespace_repr, /* tp_repr */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
215 0, /* tp_as_number */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
216 0, /* tp_as_sequence */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
217 0, /* tp_as_mapping */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
218 0, /* tp_hash */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
219 0, /* tp_call */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
220 0, /* tp_str */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
221 PyObject_GenericGetAttr, /* tp_getattro */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
222 PyObject_GenericSetAttr, /* tp_setattro */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
223 0, /* tp_as_buffer */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
224 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
225 Py_TPFLAGS_BASETYPE, /* tp_flags */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
226 namespace_doc, /* tp_doc */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
227 (traverseproc)namespace_traverse, /* tp_traverse */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
228 (inquiry)namespace_clear, /* tp_clear */
82232
3b93ab8c9c20 Issue #15022: Add pickle and comparison support to types.SimpleNamespace.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 79776
diff changeset
229 namespace_richcompare, /* tp_richcompare */
77339
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
230 0, /* tp_weaklistoffset */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
231 0, /* tp_iter */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
232 0, /* tp_iternext */
82235
e4c065b2db49 Issue #15022: Ensure all pickle protocols are supported.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 82232
diff changeset
233 namespace_methods, /* tp_methods */
77339
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
234 namespace_members, /* tp_members */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
235 0, /* tp_getset */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
236 0, /* tp_base */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
237 0, /* tp_dict */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
238 0, /* tp_descr_get */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
239 0, /* tp_descr_set */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
240 offsetof(_PyNamespaceObject, ns_dict), /* tp_dictoffset */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
241 (initproc)namespace_init, /* tp_init */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
242 PyType_GenericAlloc, /* tp_alloc */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
243 (newfunc)namespace_new, /* tp_new */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
244 PyObject_GC_Del, /* tp_free */
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
245 };
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
246
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
247
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
248 PyObject *
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
249 _PyNamespace_New(PyObject *kwds)
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
250 {
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
251 PyObject *ns = namespace_new(&_PyNamespace_Type, NULL, NULL);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
252 if (ns == NULL)
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
253 return NULL;
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
254
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
255 if (kwds == NULL)
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
256 return ns;
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
257 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
258 Py_DECREF(ns);
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
259 return NULL;
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
260 }
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
261
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
262 return (PyObject *)ns;
9c445f4695c1 Eric Snow's implementation of PEP 421.
Barry Warsaw <barry@python.org>
parents:
diff changeset
263 }