PageRenderTime 43ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/cpyext/dictobject.py

https://bitbucket.org/pypy/pypy/
Python | 270 lines | 161 code | 24 blank | 85 comment | 25 complexity | c625649e39a46fb839df7c5ffd478f7b MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. from rpython.rtyper.lltypesystem import rffi, lltype
  2. from pypy.module.cpyext.api import (
  3. cpython_api, CANNOT_FAIL, build_type_checkers, Py_ssize_t,
  4. Py_ssize_tP, CONST_STRING)
  5. from pypy.module.cpyext.pyobject import PyObject, PyObjectP, as_pyobj
  6. from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall
  7. from pypy.interpreter.error import OperationError
  8. from rpython.rlib.objectmodel import specialize
  9. @cpython_api([], PyObject)
  10. def PyDict_New(space):
  11. return space.newdict()
  12. PyDict_Check, PyDict_CheckExact = build_type_checkers("Dict")
  13. @cpython_api([PyObject, PyObject], PyObject, error=CANNOT_FAIL,
  14. result_borrowed=True)
  15. def PyDict_GetItem(space, w_dict, w_key):
  16. try:
  17. w_res = space.getitem(w_dict, w_key)
  18. except:
  19. return None
  20. # NOTE: this works so far because all our dict strategies store
  21. # *values* as full objects, which stay alive as long as the dict is
  22. # alive and not modified. So we can return a borrowed ref.
  23. # XXX this is wrong with IntMutableCell. Hope it works...
  24. return w_res
  25. @cpython_api([PyObject, PyObject, PyObject], rffi.INT_real, error=-1)
  26. def PyDict_SetItem(space, w_dict, w_key, w_obj):
  27. if PyDict_Check(space, w_dict):
  28. space.setitem(w_dict, w_key, w_obj)
  29. return 0
  30. else:
  31. PyErr_BadInternalCall(space)
  32. @cpython_api([PyObject, PyObject], rffi.INT_real, error=-1)
  33. def PyDict_DelItem(space, w_dict, w_key):
  34. if PyDict_Check(space, w_dict):
  35. space.delitem(w_dict, w_key)
  36. return 0
  37. else:
  38. PyErr_BadInternalCall(space)
  39. @cpython_api([PyObject, CONST_STRING, PyObject], rffi.INT_real, error=-1)
  40. def PyDict_SetItemString(space, w_dict, key_ptr, w_obj):
  41. if PyDict_Check(space, w_dict):
  42. key = rffi.charp2str(key_ptr)
  43. space.setitem_str(w_dict, key, w_obj)
  44. return 0
  45. else:
  46. PyErr_BadInternalCall(space)
  47. @cpython_api([PyObject, CONST_STRING], PyObject, error=CANNOT_FAIL,
  48. result_borrowed=True)
  49. def PyDict_GetItemString(space, w_dict, key):
  50. """This is the same as PyDict_GetItem(), but key is specified as a
  51. char*, rather than a PyObject*."""
  52. try:
  53. w_res = space.finditem_str(w_dict, rffi.charp2str(key))
  54. except:
  55. w_res = None
  56. # NOTE: this works so far because all our dict strategies store
  57. # *values* as full objects, which stay alive as long as the dict is
  58. # alive and not modified. So we can return a borrowed ref.
  59. # XXX this is wrong with IntMutableCell. Hope it works...
  60. return w_res
  61. @cpython_api([PyObject, CONST_STRING], rffi.INT_real, error=-1)
  62. def PyDict_DelItemString(space, w_dict, key_ptr):
  63. """Remove the entry in dictionary p which has a key specified by the string
  64. key. Return 0 on success or -1 on failure."""
  65. if PyDict_Check(space, w_dict):
  66. key = rffi.charp2str(key_ptr)
  67. # our dicts dont have a standardized interface, so we need
  68. # to go through the space
  69. space.delitem(w_dict, space.wrap(key))
  70. return 0
  71. else:
  72. PyErr_BadInternalCall(space)
  73. @cpython_api([PyObject], Py_ssize_t, error=-1)
  74. def PyDict_Size(space, w_obj):
  75. """
  76. Return the number of items in the dictionary. This is equivalent to
  77. len(p) on a dictionary."""
  78. return space.len_w(w_obj)
  79. @cpython_api([PyObject, PyObject], rffi.INT_real, error=-1)
  80. def PyDict_Contains(space, w_obj, w_value):
  81. """Determine if dictionary p contains key. If an item in p is matches
  82. key, return 1, otherwise return 0. On error, return -1.
  83. This is equivalent to the Python expression key in p.
  84. """
  85. w_res = space.contains(w_obj, w_value)
  86. return space.int_w(w_res)
  87. @cpython_api([PyObject], lltype.Void)
  88. def PyDict_Clear(space, w_obj):
  89. """Empty an existing dictionary of all key-value pairs."""
  90. space.call_method(space.w_dict, "clear", w_obj)
  91. @cpython_api([PyObject], PyObject)
  92. def PyDict_Copy(space, w_obj):
  93. """Return a new dictionary that contains the same key-value pairs as p.
  94. """
  95. return space.call_method(space.w_dict, "copy", w_obj)
  96. def _has_val(space, w_dict, w_key):
  97. try:
  98. w_val = space.getitem(w_dict, w_key)
  99. except OperationError as e:
  100. if e.match(space, space.w_KeyError):
  101. return False
  102. else:
  103. raise
  104. return True
  105. @cpython_api([PyObject, PyObject, rffi.INT_real], rffi.INT_real, error=-1)
  106. def PyDict_Merge(space, w_a, w_b, override):
  107. """Iterate over mapping object b adding key-value pairs to dictionary a.
  108. b may be a dictionary, or any object supporting PyMapping_Keys()
  109. and PyObject_GetItem(). If override is true, existing pairs in a
  110. will be replaced if a matching key is found in b, otherwise pairs will
  111. only be added if there is not a matching key in a. Return 0 on
  112. success or -1 if an exception was raised.
  113. """
  114. override = rffi.cast(lltype.Signed, override)
  115. w_keys = space.call_method(w_b, "keys")
  116. for w_key in space.iteriterable(w_keys):
  117. if not _has_val(space, w_a, w_key) or override != 0:
  118. space.setitem(w_a, w_key, space.getitem(w_b, w_key))
  119. return 0
  120. @cpython_api([PyObject, PyObject], rffi.INT_real, error=-1)
  121. def PyDict_Update(space, w_obj, w_other):
  122. """This is the same as PyDict_Merge(a, b, 1) in C, or a.update(b) in
  123. Python. Return 0 on success or -1 if an exception was raised.
  124. """
  125. space.call_method(space.w_dict, "update", w_obj, w_other)
  126. return 0
  127. @cpython_api([PyObject], PyObject)
  128. def PyDict_Keys(space, w_obj):
  129. """Return a PyListObject containing all the keys from the dictionary,
  130. as in the dictionary method dict.keys()."""
  131. return space.call_method(space.w_dict, "keys", w_obj)
  132. @cpython_api([PyObject], PyObject)
  133. def PyDict_Values(space, w_obj):
  134. """Return a PyListObject containing all the values from the
  135. dictionary p, as in the dictionary method dict.values()."""
  136. return space.call_method(space.w_dict, "values", w_obj)
  137. @cpython_api([PyObject], PyObject)
  138. def PyDict_Items(space, w_obj):
  139. """Return a PyListObject containing all the items from the
  140. dictionary, as in the dictionary method dict.items()."""
  141. return space.call_method(space.w_dict, "items", w_obj)
  142. @cpython_api([PyObject, Py_ssize_tP, PyObjectP, PyObjectP], rffi.INT_real, error=CANNOT_FAIL)
  143. def PyDict_Next(space, w_dict, ppos, pkey, pvalue):
  144. """Iterate over all key-value pairs in the dictionary p. The
  145. Py_ssize_t referred to by ppos must be initialized to 0
  146. prior to the first call to this function to start the iteration; the
  147. function returns true for each pair in the dictionary, and false once all
  148. pairs have been reported. The parameters pkey and pvalue should either
  149. point to PyObject* variables that will be filled in with each key
  150. and value, respectively, or may be NULL. Any references returned through
  151. them are borrowed. ppos should not be altered during iteration. Its
  152. value represents offsets within the internal dictionary structure, and
  153. since the structure is sparse, the offsets are not consecutive.
  154. For example:
  155. PyObject *key, *value;
  156. Py_ssize_t pos = 0;
  157. while (PyDict_Next(self->dict, &pos, &key, &value)) {
  158. /* do something interesting with the values... */
  159. ...
  160. }
  161. The dictionary p should not be mutated during iteration. It is safe
  162. (since Python 2.1) to modify the values of the keys as you iterate over the
  163. dictionary, but only so long as the set of keys does not change. For
  164. example:
  165. PyObject *key, *value;
  166. Py_ssize_t pos = 0;
  167. while (PyDict_Next(self->dict, &pos, &key, &value)) {
  168. int i = PyInt_AS_LONG(value) + 1;
  169. PyObject *o = PyInt_FromLong(i);
  170. if (o == NULL)
  171. return -1;
  172. if (PyDict_SetItem(self->dict, key, o) < 0) {
  173. Py_DECREF(o);
  174. return -1;
  175. }
  176. Py_DECREF(o);
  177. }"""
  178. if w_dict is None:
  179. return 0
  180. # XXX XXX PyDict_Next is not efficient. Storing an iterator would probably
  181. # work, but we can't work out how to not leak it if iteration does
  182. # not complete. Alternatively, we could add some RPython-only
  183. # dict-iterator method to move forward by N steps.
  184. w_dict.ensure_object_strategy() # make sure both keys and values can
  185. # be borrwed
  186. try:
  187. w_iter = space.call_method(space.w_dict, "iteritems", w_dict)
  188. pos = ppos[0]
  189. while pos:
  190. space.call_method(w_iter, "next")
  191. pos -= 1
  192. w_item = space.call_method(w_iter, "next")
  193. w_key, w_value = space.fixedview(w_item, 2)
  194. if pkey:
  195. pkey[0] = as_pyobj(space, w_key)
  196. if pvalue:
  197. pvalue[0] = as_pyobj(space, w_value)
  198. ppos[0] += 1
  199. except OperationError as e:
  200. if not e.match(space, space.w_StopIteration):
  201. raise
  202. return 0
  203. return 1
  204. @specialize.memo()
  205. def make_frozendict(space):
  206. if space not in _frozendict_cache:
  207. _frozendict_cache[space] = _make_frozendict(space)
  208. return _frozendict_cache[space]
  209. _frozendict_cache = {}
  210. def _make_frozendict(space):
  211. return space.appexec([], '''():
  212. import _abcoll
  213. class FrozenDict(_abcoll.Mapping):
  214. def __init__(self, *args, **kwargs):
  215. self._d = dict(*args, **kwargs)
  216. def __iter__(self):
  217. return iter(self._d)
  218. def __len__(self):
  219. return len(self._d)
  220. def __getitem__(self, key):
  221. return self._d[key]
  222. return FrozenDict''')
  223. @cpython_api([PyObject], PyObject)
  224. def PyDictProxy_New(space, w_dict):
  225. w_frozendict = make_frozendict(space)
  226. return space.call_function(w_frozendict, w_dict)
  227. @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
  228. def PyDictProxy_Check(space, w_obj):
  229. w_typ = make_frozendict(space)
  230. #print 'check', w_typ, space.type(w_obj)
  231. return space.isinstance_w(w_obj, w_typ)
  232. @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
  233. def PyDictProxy_CheckExact(space, w_obj):
  234. w_typ = make_frozendict(space)
  235. #print 'exact', w_typ, w_obj
  236. return space.is_w(space.type(w_obj), w_typ)