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