dictobject.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #ifndef Py_CPYTHON_DICTOBJECT_H
  2. # error "this header file must not be included directly"
  3. #endif
  4. typedef struct _dictkeysobject PyDictKeysObject;
  5. typedef struct _dictvalues PyDictValues;
  6. /* The ma_values pointer is NULL for a combined table
  7. * or points to an array of PyObject* for a split table
  8. */
  9. typedef struct {
  10. PyObject_HEAD
  11. /* Number of items in the dictionary */
  12. Py_ssize_t ma_used;
  13. /* Dictionary version: globally unique, value change each time
  14. the dictionary is modified */
  15. #ifdef Py_BUILD_CORE
  16. uint64_t ma_version_tag;
  17. #else
  18. Py_DEPRECATED(3.12) uint64_t ma_version_tag;
  19. #endif
  20. PyDictKeysObject *ma_keys;
  21. /* If ma_values is NULL, the table is "combined": keys and values
  22. are stored in ma_keys.
  23. If ma_values is not NULL, the table is split:
  24. keys are stored in ma_keys and values are stored in ma_values */
  25. PyDictValues *ma_values;
  26. } PyDictObject;
  27. PyAPI_FUNC(PyObject *) _PyDict_GetItem_KnownHash(PyObject *mp, PyObject *key,
  28. Py_hash_t hash);
  29. PyAPI_FUNC(PyObject *) _PyDict_GetItemWithError(PyObject *dp, PyObject *key);
  30. PyAPI_FUNC(PyObject *) _PyDict_GetItemIdWithError(PyObject *dp,
  31. _Py_Identifier *key);
  32. PyAPI_FUNC(PyObject *) _PyDict_GetItemStringWithError(PyObject *, const char *);
  33. PyAPI_FUNC(PyObject *) PyDict_SetDefault(
  34. PyObject *mp, PyObject *key, PyObject *defaultobj);
  35. PyAPI_FUNC(int) _PyDict_SetItem_KnownHash(PyObject *mp, PyObject *key,
  36. PyObject *item, Py_hash_t hash);
  37. PyAPI_FUNC(int) _PyDict_DelItem_KnownHash(PyObject *mp, PyObject *key,
  38. Py_hash_t hash);
  39. PyAPI_FUNC(int) _PyDict_DelItemIf(PyObject *mp, PyObject *key,
  40. int (*predicate)(PyObject *value));
  41. PyAPI_FUNC(int) _PyDict_Next(
  42. PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value, Py_hash_t *hash);
  43. /* Get the number of items of a dictionary. */
  44. static inline Py_ssize_t PyDict_GET_SIZE(PyObject *op) {
  45. PyDictObject *mp;
  46. assert(PyDict_Check(op));
  47. mp = _Py_CAST(PyDictObject*, op);
  48. return mp->ma_used;
  49. }
  50. #define PyDict_GET_SIZE(op) PyDict_GET_SIZE(_PyObject_CAST(op))
  51. PyAPI_FUNC(int) _PyDict_Contains_KnownHash(PyObject *, PyObject *, Py_hash_t);
  52. PyAPI_FUNC(int) _PyDict_ContainsId(PyObject *, _Py_Identifier *);
  53. PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused);
  54. PyAPI_FUNC(void) _PyDict_MaybeUntrack(PyObject *mp);
  55. PyAPI_FUNC(int) _PyDict_HasOnlyStringKeys(PyObject *mp);
  56. PyAPI_FUNC(Py_ssize_t) _PyDict_SizeOf(PyDictObject *);
  57. PyAPI_FUNC(PyObject *) _PyDict_Pop(PyObject *, PyObject *, PyObject *);
  58. #define _PyDict_HasSplitTable(d) ((d)->ma_values != NULL)
  59. /* Like PyDict_Merge, but override can be 0, 1 or 2. If override is 0,
  60. the first occurrence of a key wins, if override is 1, the last occurrence
  61. of a key wins, if override is 2, a KeyError with conflicting key as
  62. argument is raised.
  63. */
  64. PyAPI_FUNC(int) _PyDict_MergeEx(PyObject *mp, PyObject *other, int override);
  65. PyAPI_FUNC(int) _PyDict_SetItemId(PyObject *dp, _Py_Identifier *key, PyObject *item);
  66. PyAPI_FUNC(int) _PyDict_DelItemId(PyObject *mp, _Py_Identifier *key);
  67. PyAPI_FUNC(void) _PyDict_DebugMallocStats(FILE *out);
  68. /* _PyDictView */
  69. typedef struct {
  70. PyObject_HEAD
  71. PyDictObject *dv_dict;
  72. } _PyDictViewObject;
  73. PyAPI_FUNC(PyObject *) _PyDictView_New(PyObject *, PyTypeObject *);
  74. PyAPI_FUNC(PyObject *) _PyDictView_Intersect(PyObject* self, PyObject *other);
  75. /* Dictionary watchers */
  76. #define PY_FOREACH_DICT_EVENT(V) \
  77. V(ADDED) \
  78. V(MODIFIED) \
  79. V(DELETED) \
  80. V(CLONED) \
  81. V(CLEARED) \
  82. V(DEALLOCATED)
  83. typedef enum {
  84. #define PY_DEF_EVENT(EVENT) PyDict_EVENT_##EVENT,
  85. PY_FOREACH_DICT_EVENT(PY_DEF_EVENT)
  86. #undef PY_DEF_EVENT
  87. } PyDict_WatchEvent;
  88. // Callback to be invoked when a watched dict is cleared, dealloced, or modified.
  89. // In clear/dealloc case, key and new_value will be NULL. Otherwise, new_value will be the
  90. // new value for key, NULL if key is being deleted.
  91. typedef int(*PyDict_WatchCallback)(PyDict_WatchEvent event, PyObject* dict, PyObject* key, PyObject* new_value);
  92. // Register/unregister a dict-watcher callback
  93. PyAPI_FUNC(int) PyDict_AddWatcher(PyDict_WatchCallback callback);
  94. PyAPI_FUNC(int) PyDict_ClearWatcher(int watcher_id);
  95. // Mark given dictionary as "watched" (callback will be called if it is modified)
  96. PyAPI_FUNC(int) PyDict_Watch(int watcher_id, PyObject* dict);
  97. PyAPI_FUNC(int) PyDict_Unwatch(int watcher_id, PyObject* dict);