1from .object cimport PyObject 2from .pyport cimport uint64_t 3 4cdef extern from "Python.h": 5 # On Python 2, PyDict_GetItemWithError is called _PyDict_GetItemWithError 6 """ 7 #if PY_MAJOR_VERSION <= 2 8 #define PyDict_GetItemWithError _PyDict_GetItemWithError 9 #endif 10 """ 11 12 ############################################################################ 13 # 7.4.1 Dictionary Objects 14 ############################################################################ 15 16 # PyDictObject 17 # 18 # This subtype of PyObject represents a Python dictionary object 19 # (i.e. the 'dict' type). 20 21 # PyTypeObject PyDict_Type 22 # 23 # This instance of PyTypeObject represents the Python dictionary 24 # type. This is exposed to Python programs as dict and 25 # types.DictType. 26 27 bint PyDict_Check(object p) 28 # Return true if p is a dict object or an instance of a subtype of 29 # the dict type. 30 31 bint PyDict_CheckExact(object p) 32 # Return true if p is a dict object, but not an instance of a 33 # subtype of the dict type. 34 35 dict PyDict_New() 36 # Return value: New reference. 37 # Return a new empty dictionary, or NULL on failure. 38 39 object PyDictProxy_New(object dict) 40 # Return value: New reference. 41 # Return a proxy object for a mapping which enforces read-only 42 # behavior. This is normally used to create a proxy to prevent 43 # modification of the dictionary for non-dynamic class types. 44 45 void PyDict_Clear(object p) 46 # Empty an existing dictionary of all key-value pairs. 47 48 int PyDict_Contains(object p, object key) except -1 49 # Determine if dictionary p contains key. If an item in p is 50 # matches key, return 1, otherwise return 0. On error, return 51 # -1. This is equivalent to the Python expression "key in p". 52 53 dict PyDict_Copy(object p) 54 # Return value: New reference. 55 # Return a new dictionary that contains the same key-value pairs as p. 56 57 int PyDict_SetItem(object p, object key, object val) except -1 58 # Insert value into the dictionary p with a key of key. key must 59 # be hashable; if it isn't, TypeError will be raised. Return 0 on 60 # success or -1 on failure. 61 62 int PyDict_SetItemString(object p, const char *key, object val) except -1 63 # Insert value into the dictionary p using key as a key. key 64 # should be a char*. The key object is created using 65 # PyString_FromString(key). Return 0 on success or -1 on failure. 66 67 int PyDict_DelItem(object p, object key) except -1 68 # Remove the entry in dictionary p with key key. key must be 69 # hashable; if it isn't, TypeError is raised. Return 0 on success 70 # or -1 on failure. 71 72 int PyDict_DelItemString(object p, const char *key) except -1 73 # Remove the entry in dictionary p which has a key specified by 74 # the string key. Return 0 on success or -1 on failure. 75 76 PyObject* PyDict_GetItem(object p, object key) 77 # Return value: Borrowed reference. 78 # Return the object from dictionary p which has a key key. Return 79 # NULL if the key key is not present, but without setting an 80 # exception. 81 82 PyObject* PyDict_GetItemWithError(object p, object key) except? NULL 83 # Return value: Borrowed reference. 84 # Variant of PyDict_GetItem() that does not suppress exceptions. Return 85 # NULL with an exception set if an exception occurred. Return NULL 86 # without an exception set if the key wasn’t present. 87 88 PyObject* PyDict_GetItemString(object p, const char *key) 89 # Return value: Borrowed reference. 90 # This is the same as PyDict_GetItem(), but key is specified as a 91 # char*, rather than a PyObject*. 92 93 PyObject* PyDict_SetDefault(object p, object key, object default) except NULL 94 # Return value: Borrowed reference. 95 # This is the same as the Python-level dict.setdefault(). If present, it 96 # returns the value corresponding to key from the dictionary p. If the key 97 # is not in the dict, it is inserted with value defaultobj and defaultobj 98 # is returned. This function evaluates the hash function of key only once, 99 # instead of evaluating it independently for the lookup and the insertion. 100 101 list PyDict_Items(object p) 102 # Return value: New reference. 103 # Return a PyListObject containing all the items from the 104 # dictionary, as in the dictionary method items() (see the Python 105 # Library Reference). 106 107 list PyDict_Keys(object p) 108 # Return value: New reference. 109 # Return a PyListObject containing all the keys from the 110 # dictionary, as in the dictionary method keys() (see the Python 111 # Library Reference). 112 113 list PyDict_Values(object p) 114 # Return value: New reference. 115 # Return a PyListObject containing all the values from the 116 # dictionary p, as in the dictionary method values() (see the 117 # Python Library Reference). 118 119 Py_ssize_t PyDict_Size(object p) except -1 120 # Return the number of items in the dictionary. This is equivalent 121 # to "len(p)" on a dictionary. 122 123 int PyDict_Next(object p, Py_ssize_t *ppos, PyObject* *pkey, PyObject* *pvalue) 124 # Iterate over all key-value pairs in the dictionary p. The int 125 # referred to by ppos must be initialized to 0 prior to the first 126 # call to this function to start the iteration; the function 127 # returns true for each pair in the dictionary, and false once all 128 # pairs have been reported. The parameters pkey and pvalue should 129 # either point to PyObject* variables that will be filled in with 130 # each key and value, respectively, or may be NULL. Any references 131 # returned through them are borrowed. ppos should not be altered 132 # during iteration. Its value represents offsets within the 133 # internal dictionary structure, and since the structure is 134 # sparse, the offsets are not consecutive. 135 # For example: 136 # 137 #object key, *value; 138 #int pos = 0; 139 # 140 #while (PyDict_Next(self->dict, &pos, &key, &value)) { 141 # /* do something interesting with the values... */ 142 # ... 143 #} 144 # The dictionary p should not be mutated during iteration. It is 145 # safe (since Python 2.1) to modify the values of the keys as you 146 # iterate over the dictionary, but only so long as the set of keys 147 # does not change. For example: 148 # object key, *value; 149 # int pos = 0; 150 # while (PyDict_Next(self->dict, &pos, &key, &value)) { 151 # int i = PyInt_AS_LONG(value) + 1; 152 # object o = PyInt_FromLong(i); 153 # if (o == NULL) 154 # return -1; 155 # if (PyDict_SetItem(self->dict, key, o) < 0) { 156 # Py_DECREF(o); 157 # return -1; 158 # } 159 # Py_DECREF(o); 160 # } 161 162 int PyDict_Merge(object a, object b, int override) except -1 163 # Iterate over mapping object b adding key-value pairs to 164 # dictionary a. b may be a dictionary, or any object supporting 165 # PyMapping_Keys() and PyObject_GetItem(). If override is true, 166 # existing pairs in a will be replaced if a matching key is found 167 # in b, otherwise pairs will only be added if there is not a 168 # matching key in a. Return 0 on success or -1 if an exception was 169 # raised. 170 171 int PyDict_Update(object a, object b) except -1 172 # This is the same as PyDict_Merge(a, b, 1) in C, or a.update(b) 173 # in Python. Return 0 on success or -1 if an exception was raised. 174 175 int PyDict_MergeFromSeq2(object a, object seq2, int override) except -1 176 # Update or merge into dictionary a, from the key-value pairs in 177 # seq2. seq2 must be an iterable object producing iterable objects 178 # of length 2, viewed as key-value pairs. In case of duplicate 179 # keys, the last wins if override is true, else the first 180 # wins. Return 0 on success or -1 if an exception was 181 # raised. Equivalent Python (except for the return value): 182 # 183 #def PyDict_MergeFromSeq2(a, seq2, override): 184 # for key, value in seq2: 185 # if override or key not in a: 186 # a[key] = value 187