| 1 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 2 |
from rpython.rlib.objectmodel import specialize |
| 3 |
from pypy.interpreter.error import OperationError |
| 4 |
from pypy.objspace.std.classdict import ClassDictStrategy |
| 5 |
from pypy.objspace.std.dictmultiobject import W_DictMultiObject |
| 6 |
from pypy.interpreter.typedef import GetSetProperty |
| 7 |
from pypy.module.cpyext.api import ( |
| 8 |
cpython_api, CANNOT_FAIL, build_type_checkers_flags, Py_ssize_t, cts, |
| 9 |
Py_ssize_tP, CONST_STRING, PyObjectFields, cpython_struct, |
| 10 |
bootstrap_function, slot_function) |
| 11 |
from pypy.module.cpyext.pyobject import (PyObject, PyObjectP, as_pyobj, |
| 12 |
make_typedescr, track_reference, create_ref, from_ref, decref, |
| 13 |
incref) |
| 14 |
from pypy.module.cpyext.object import _dealloc |
| 15 |
from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall |
| 16 |
|
| 17 |
PyDictObjectStruct = lltype.ForwardReference() |
| 18 |
PyDictObject = lltype.Ptr(PyDictObjectStruct) |
| 19 |
PyDictObjectFields = PyObjectFields + \ |
| 20 |
(("_tmpkeys", PyObject),) |
| 21 |
cpython_struct("PyDictObject", PyDictObjectFields, PyDictObjectStruct) |
| 22 |
|
| 23 |
@bootstrap_function |
| 24 |
def init_dictobject(space): |
| 25 |
"Type description of PyDictObject" |
| 26 |
make_typedescr(space.w_dict.layout.typedef, |
| 27 |
basestruct=PyDictObject.TO, |
| 28 |
attach=dict_attach, |
| 29 |
dealloc=dict_dealloc, |
| 30 |
realize=dict_realize) |
| 31 |
|
| 32 |
def dict_attach(space, py_obj, w_obj, w_userdata=None): |
| 33 |
""" |
| 34 |
Fills a newly allocated PyDictObject with the given dict object. |
| 35 |
""" |
| 36 |
py_dict = rffi.cast(PyDictObject, py_obj) |
| 37 |
py_dict.c__tmpkeys = lltype.nullptr(PyObject.TO) |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
if not space.is_w(w_userdata, space.gettypefor(GetSetProperty)): |
| 44 |
|
| 45 |
w_vals = space.call_method(space.w_dict, "values", w_obj) |
| 46 |
vals = space.listview(w_vals) |
| 47 |
for w_v in vals: |
| 48 |
if isinstance(w_v, GetSetProperty): |
| 49 |
pyobj = as_pyobj(space, w_v, w_userdata) |
| 50 |
|
| 51 |
|
| 52 |
def dict_realize(space, py_obj): |
| 53 |
""" |
| 54 |
Creates the dict in the interpreter |
| 55 |
""" |
| 56 |
w_obj = space.newdict() |
| 57 |
track_reference(space, py_obj, w_obj) |
| 58 |
|
| 59 |
@slot_function([PyObject], lltype.Void) |
| 60 |
def dict_dealloc(space, py_obj): |
| 61 |
py_dict = rffi.cast(PyDictObject, py_obj) |
| 62 |
decref(space, py_dict.c__tmpkeys) |
| 63 |
py_dict.c__tmpkeys = lltype.nullptr(PyObject.TO) |
| 64 |
_dealloc(space, py_obj) |
| 65 |
|
| 66 |
@cpython_api([], PyObject) |
| 67 |
def PyDict_New(space): |
| 68 |
return space.newdict() |
| 69 |
|
| 70 |
PyDict_Check, PyDict_CheckExact = build_type_checkers_flags("Dict") |
| 71 |
|
| 72 |
@cpython_api([PyObject, PyObject], PyObject, error=CANNOT_FAIL, |
| 73 |
result_borrowed=True) |
| 74 |
def PyDict_GetItem(space, w_dict, w_key): |
| 75 |
if not isinstance(w_dict, W_DictMultiObject): |
| 76 |
return None |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
try: |
| 82 |
return w_dict.getitem(w_key) |
| 83 |
except OperationError: |
| 84 |
return None |
| 85 |
|
| 86 |
@cpython_api([PyObject, PyObject], PyObject, result_borrowed=True) |
| 87 |
def PyDict_GetItemWithError(space, w_dict, w_key): |
| 88 |
"""Variant of PyDict_GetItem() that does not suppress |
| 89 |
exceptions. Return NULL with an exception set if an exception |
| 90 |
occurred. Return NULL without an exception set if the key |
| 91 |
wasn't present.""" |
| 92 |
if not isinstance(w_dict, W_DictMultiObject): |
| 93 |
PyErr_BadInternalCall(space) |
| 94 |
return w_dict.getitem(w_key) |
| 95 |
|
| 96 |
@cpython_api([PyObject, PyObject, PyObject], rffi.INT_real, error=-1) |
| 97 |
def PyDict_SetItem(space, w_dict, w_key, w_obj): |
| 98 |
if not isinstance(w_dict, W_DictMultiObject): |
| 99 |
PyErr_BadInternalCall(space) |
| 100 |
w_dict.setitem(w_key, w_obj) |
| 101 |
return 0 |
| 102 |
|
| 103 |
@cpython_api([PyObject, PyObject], rffi.INT_real, error=-1) |
| 104 |
def PyDict_DelItem(space, w_dict, w_key): |
| 105 |
if not isinstance(w_dict, W_DictMultiObject): |
| 106 |
PyErr_BadInternalCall(space) |
| 107 |
w_dict.descr_delitem(space, w_key) |
| 108 |
return 0 |
| 109 |
|
| 110 |
@cpython_api([PyObject, CONST_STRING, PyObject], rffi.INT_real, error=-1) |
| 111 |
def PyDict_SetItemString(space, w_dict, key_ptr, w_obj): |
| 112 |
w_key = space.newtext(rffi.charp2str(key_ptr)) |
| 113 |
if not isinstance(w_dict, W_DictMultiObject): |
| 114 |
PyErr_BadInternalCall(space) |
| 115 |
w_dict.setitem(w_key, w_obj) |
| 116 |
return 0 |
| 117 |
|
| 118 |
@cpython_api([PyObject, CONST_STRING], PyObject, error=CANNOT_FAIL, |
| 119 |
result_borrowed=True) |
| 120 |
def PyDict_GetItemString(space, w_dict, key): |
| 121 |
"""This is the same as PyDict_GetItem(), but key is specified as a |
| 122 |
char*, rather than a PyObject*.""" |
| 123 |
w_key = space.newtext(rffi.charp2str(key)) |
| 124 |
if not isinstance(w_dict, W_DictMultiObject): |
| 125 |
return None |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
return w_dict.getitem(w_key) |
| 131 |
|
| 132 |
|
| 133 |
@cpython_api([PyObject, CONST_STRING], PyObject, |
| 134 |
result_borrowed=True) |
| 135 |
def _PyDict_GetItemStringWithError(space, w_dict, key): |
| 136 |
w_key = space.newtext(rffi.charp2str(key)) |
| 137 |
if not isinstance(w_dict, W_DictMultiObject): |
| 138 |
PyErr_BadInternalCall(space) |
| 139 |
return w_dict.getitem(w_key) |
| 140 |
|
| 141 |
@cpython_api([PyObject, CONST_STRING], rffi.INT_real, error=-1) |
| 142 |
def PyDict_DelItemString(space, w_dict, key_ptr): |
| 143 |
"""Remove the entry in dictionary p which has a key specified by the string |
| 144 |
key. Return 0 on success or -1 on failure.""" |
| 145 |
w_key = space.newtext(rffi.charp2str(key_ptr)) |
| 146 |
if not isinstance(w_dict, W_DictMultiObject): |
| 147 |
raise PyErr_BadInternalCall(space) |
| 148 |
w_dict.descr_delitem(space, w_key) |
| 149 |
return 0 |
| 150 |
|
| 151 |
@cpython_api([PyObject], Py_ssize_t, error=-1) |
| 152 |
def PyDict_Size(space, w_obj): |
| 153 |
""" |
| 154 |
Return the number of items in the dictionary. This is equivalent to |
| 155 |
len(p) on a dictionary.""" |
| 156 |
return space.len_w(w_obj) |
| 157 |
|
| 158 |
@cpython_api([PyObject, PyObject], rffi.INT_real, error=-1) |
| 159 |
def PyDict_Contains(space, w_dict, w_key): |
| 160 |
"""Determine if dictionary p contains key. If an item in p is matches |
| 161 |
key, return 1, otherwise return 0. On error, return -1. |
| 162 |
This is equivalent to the Python expression key in p. |
| 163 |
""" |
| 164 |
if not isinstance(w_dict, W_DictMultiObject): |
| 165 |
raise PyErr_BadInternalCall(space) |
| 166 |
try: |
| 167 |
w_obj = w_dict.getitem(w_key) |
| 168 |
if (w_obj is None): |
| 169 |
return 0 |
| 170 |
return 1 |
| 171 |
except OperationError as e: |
| 172 |
if e.match(space, space.w_KeyError): |
| 173 |
return 0 |
| 174 |
raise e |
| 175 |
|
| 176 |
@cpython_api([PyObject], lltype.Void) |
| 177 |
def PyDict_Clear(space, w_obj): |
| 178 |
"""Empty an existing dictionary of all key-value pairs.""" |
| 179 |
space.call_method(space.w_dict, "clear", w_obj) |
| 180 |
|
| 181 |
@cts.decl("""PyObject * |
| 182 |
PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)""") |
| 183 |
def PyDict_SetDefault(space, w_dict, w_key, w_defaultobj): |
| 184 |
if not PyDict_Check(space, w_dict): |
| 185 |
PyErr_BadInternalCall(space) |
| 186 |
else: |
| 187 |
return space.call_method( |
| 188 |
space.w_dict, "setdefault", w_dict, w_key, w_defaultobj) |
| 189 |
|
| 190 |
@cpython_api([PyObject], PyObject) |
| 191 |
def PyDict_Copy(space, w_obj): |
| 192 |
"""Return a new dictionary that contains the same key-value pairs as p. |
| 193 |
""" |
| 194 |
return space.call_method(space.w_dict, "copy", w_obj) |
| 195 |
|
| 196 |
def _has_val(space, w_dict, w_key): |
| 197 |
try: |
| 198 |
w_val = space.getitem(w_dict, w_key) |
| 199 |
except OperationError as e: |
| 200 |
if e.match(space, space.w_KeyError): |
| 201 |
return False |
| 202 |
else: |
| 203 |
raise |
| 204 |
return True |
| 205 |
|
| 206 |
@cpython_api([PyObject, PyObject, rffi.INT_real], rffi.INT_real, error=-1) |
| 207 |
def PyDict_Merge(space, w_a, w_b, override): |
| 208 |
"""Iterate over mapping object b adding key-value pairs to dictionary a. |
| 209 |
b may be a dictionary, or any object supporting PyMapping_Keys() |
| 210 |
and PyObject_GetItem(). If override is true, existing pairs in a |
| 211 |
will be replaced if a matching key is found in b, otherwise pairs will |
| 212 |
only be added if there is not a matching key in a. Return 0 on |
| 213 |
success or -1 if an exception was raised. |
| 214 |
""" |
| 215 |
override = rffi.cast(lltype.Signed, override) |
| 216 |
w_keys = space.call_method(w_b, "keys") |
| 217 |
w_iter = space.iter(w_keys) |
| 218 |
while 1: |
| 219 |
try: |
| 220 |
w_key = space.next(w_iter) |
| 221 |
except OperationError as e: |
| 222 |
if not e.match(space, space.w_StopIteration): |
| 223 |
raise |
| 224 |
break |
| 225 |
if not _has_val(space, w_a, w_key) or override != 0: |
| 226 |
space.setitem(w_a, w_key, space.getitem(w_b, w_key)) |
| 227 |
return 0 |
| 228 |
|
| 229 |
@cpython_api([PyObject, PyObject], rffi.INT_real, error=-1) |
| 230 |
def PyDict_Update(space, w_obj, w_other): |
| 231 |
"""This is the same as PyDict_Merge(a, b, 1) in C, or a.update(b) in |
| 232 |
Python. Return 0 on success or -1 if an exception was raised. |
| 233 |
""" |
| 234 |
return PyDict_Merge(space, w_obj, w_other, 1) |
| 235 |
|
| 236 |
@cpython_api([PyObject], PyObject) |
| 237 |
def PyDict_Keys(space, w_obj): |
| 238 |
"""Return a PyListObject containing all the keys from the dictionary, |
| 239 |
as in the dictionary method dict.keys().""" |
| 240 |
return space.call_function(space.w_list, space.call_method(space.w_dict, "keys", w_obj)) |
| 241 |
|
| 242 |
@cpython_api([PyObject], PyObject) |
| 243 |
def PyDict_Values(space, w_obj): |
| 244 |
"""Return a PyListObject containing all the values from the |
| 245 |
dictionary p, as in the dictionary method dict.values().""" |
| 246 |
return space.call_function(space.w_list, space.call_method(space.w_dict, "values", w_obj)) |
| 247 |
|
| 248 |
@cpython_api([PyObject], PyObject) |
| 249 |
def PyDict_Items(space, w_obj): |
| 250 |
"""Return a PyListObject containing all the items from the |
| 251 |
dictionary, as in the dictionary method dict.items().""" |
| 252 |
return space.call_function(space.w_list, space.call_method(space.w_dict, "items", w_obj)) |
| 253 |
|
| 254 |
@cpython_api([PyObject, Py_ssize_tP, PyObjectP, PyObjectP], rffi.INT_real, error=CANNOT_FAIL) |
| 255 |
def PyDict_Next(space, w_dict, ppos, pkey, pvalue): |
| 256 |
"""Iterate over all key-value pairs in the dictionary p. The |
| 257 |
Py_ssize_t referred to by ppos must be initialized to 0 |
| 258 |
prior to the first call to this function to start the iteration; the |
| 259 |
function returns true for each pair in the dictionary, and false once all |
| 260 |
pairs have been reported. The parameters pkey and pvalue should either |
| 261 |
point to PyObject* variables that will be filled in with each key |
| 262 |
and value, respectively, or may be NULL. Any references returned through |
| 263 |
them are borrowed. ppos should not be altered during iteration. Its |
| 264 |
value represents offsets within the internal dictionary structure, and |
| 265 |
since the structure is sparse, the offsets are not consecutive. |
| 266 |
|
| 267 |
For example: |
| 268 |
|
| 269 |
PyObject *key, *value; |
| 270 |
Py_ssize_t pos = 0; |
| 271 |
|
| 272 |
while (PyDict_Next(self->dict, &pos, &key, &value)) { |
| 273 |
/* do something interesting with the values... */ |
| 274 |
... |
| 275 |
} |
| 276 |
|
| 277 |
The dictionary p should not be mutated during iteration. It is safe |
| 278 |
(since Python 2.1) to modify the values but not the keys as you iterate |
| 279 |
over the dictionary, the keys must not change. |
| 280 |
For example: |
| 281 |
|
| 282 |
PyObject *key, *value; |
| 283 |
Py_ssize_t pos = 0; |
| 284 |
|
| 285 |
while (PyDict_Next(self->dict, &pos, &key, &value)) { |
| 286 |
int i = PyLong_AS_LONG(value) + 1; |
| 287 |
PyObject *o = PyLong_FromLong(i); |
| 288 |
if (o == NULL) |
| 289 |
return -1; |
| 290 |
if (PyDict_SetItem(self->dict, key, o) < 0) { |
| 291 |
Py_DECREF(o); |
| 292 |
return -1; |
| 293 |
} |
| 294 |
Py_DECREF(o); |
| 295 |
}""" |
| 296 |
|
| 297 |
if w_dict is None: |
| 298 |
return 0 |
| 299 |
if not space.isinstance_w(w_dict, space.w_dict): |
| 300 |
return 0 |
| 301 |
pos = ppos[0] |
| 302 |
py_obj = as_pyobj(space, w_dict) |
| 303 |
py_dict = rffi.cast(PyDictObject, py_obj) |
| 304 |
if pos == 0: |
| 305 |
|
| 306 |
w_keyview = space.call_method(space.w_dict, "keys", w_dict) |
| 307 |
|
| 308 |
w_keys = space.newlist(space.listview(w_keyview)) |
| 309 |
w_keys.switch_to_object_strategy() |
| 310 |
oldkeys = py_dict.c__tmpkeys |
| 311 |
py_dict.c__tmpkeys = create_ref(space, w_keys) |
| 312 |
incref(space, py_dict.c__tmpkeys) |
| 313 |
decref(space, oldkeys) |
| 314 |
else: |
| 315 |
if not py_dict.c__tmpkeys: |
| 316 |
|
| 317 |
return 0; |
| 318 |
w_keys = from_ref(space, py_dict.c__tmpkeys) |
| 319 |
if pos >= space.len_w(w_keys): |
| 320 |
decref(space, py_dict.c__tmpkeys) |
| 321 |
py_dict.c__tmpkeys = lltype.nullptr(PyObject.TO) |
| 322 |
return 0 |
| 323 |
ppos[0] += 1 |
| 324 |
w_key = space.listview(w_keys)[pos] |
| 325 |
w_value = space.getitem(w_dict, w_key) |
| 326 |
if pkey: |
| 327 |
pkey[0] = as_pyobj(space, w_key) |
| 328 |
if pvalue: |
| 329 |
pvalue[0] = as_pyobj(space, w_value) |
| 330 |
return 1 |
| 331 |
|
| 332 |
@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL) |
| 333 |
def _PyDict_HasOnlyStringKeys(space, w_dict): |
| 334 |
keys_w = space.unpackiterable(w_dict) |
| 335 |
for w_key in keys_w: |
| 336 |
if not space.isinstance_w(w_key, space.w_unicode): |
| 337 |
return 0 |
| 338 |
return 1 |
| 339 |
|
| 340 |
|
| 341 |
|