dict.pxd 6.7 KB

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