Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Objects/dictobject.c
12 views
1
/* Dictionary object implementation using a hash table */
2
3
/* The distribution includes a separate file, Objects/dictnotes.txt,
4
describing explorations into dictionary design and optimization.
5
It covers typical dictionary use patterns, the parameters for
6
tuning dictionaries, and several ideas for possible optimizations.
7
*/
8
9
/* PyDictKeysObject
10
11
This implements the dictionary's hashtable.
12
13
As of Python 3.6, this is compact and ordered. Basic idea is described here:
14
* https://mail.python.org/pipermail/python-dev/2012-December/123028.html
15
* https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html
16
17
layout:
18
19
+---------------------+
20
| dk_refcnt |
21
| dk_log2_size |
22
| dk_log2_index_bytes |
23
| dk_kind |
24
| dk_usable |
25
| dk_nentries |
26
+---------------------+
27
| dk_indices[] |
28
| |
29
+---------------------+
30
| dk_entries[] |
31
| |
32
+---------------------+
33
34
dk_indices is actual hashtable. It holds index in entries, or DKIX_EMPTY(-1)
35
or DKIX_DUMMY(-2).
36
Size of indices is dk_size. Type of each index in indices is vary on dk_size:
37
38
* int8 for dk_size <= 128
39
* int16 for 256 <= dk_size <= 2**15
40
* int32 for 2**16 <= dk_size <= 2**31
41
* int64 for 2**32 <= dk_size
42
43
dk_entries is array of PyDictKeyEntry when dk_kind == DICT_KEYS_GENERAL or
44
PyDictUnicodeEntry otherwise. Its length is USABLE_FRACTION(dk_size).
45
46
NOTE: Since negative value is used for DKIX_EMPTY and DKIX_DUMMY, type of
47
dk_indices entry is signed integer and int16 is used for table which
48
dk_size == 256.
49
*/
50
51
52
/*
53
The DictObject can be in one of two forms.
54
55
Either:
56
A combined table:
57
ma_values == NULL, dk_refcnt == 1.
58
Values are stored in the me_value field of the PyDictKeysObject.
59
Or:
60
A split table:
61
ma_values != NULL, dk_refcnt >= 1
62
Values are stored in the ma_values array.
63
Only string (unicode) keys are allowed.
64
65
There are four kinds of slots in the table (slot is index, and
66
DK_ENTRIES(keys)[index] if index >= 0):
67
68
1. Unused. index == DKIX_EMPTY
69
Does not hold an active (key, value) pair now and never did. Unused can
70
transition to Active upon key insertion. This is each slot's initial state.
71
72
2. Active. index >= 0, me_key != NULL and me_value != NULL
73
Holds an active (key, value) pair. Active can transition to Dummy or
74
Pending upon key deletion (for combined and split tables respectively).
75
This is the only case in which me_value != NULL.
76
77
3. Dummy. index == DKIX_DUMMY (combined only)
78
Previously held an active (key, value) pair, but that was deleted and an
79
active pair has not yet overwritten the slot. Dummy can transition to
80
Active upon key insertion. Dummy slots cannot be made Unused again
81
else the probe sequence in case of collision would have no way to know
82
they were once active.
83
84
4. Pending. index >= 0, key != NULL, and value == NULL (split only)
85
Not yet inserted in split-table.
86
*/
87
88
/*
89
Preserving insertion order
90
91
It's simple for combined table. Since dk_entries is mostly append only, we can
92
get insertion order by just iterating dk_entries.
93
94
One exception is .popitem(). It removes last item in dk_entries and decrement
95
dk_nentries to achieve amortized O(1). Since there are DKIX_DUMMY remains in
96
dk_indices, we can't increment dk_usable even though dk_nentries is
97
decremented.
98
99
To preserve the order in a split table, a bit vector is used to record the
100
insertion order. When a key is inserted the bit vector is shifted up by 4 bits
101
and the index of the key is stored in the low 4 bits.
102
As a consequence of this, split keys have a maximum size of 16.
103
*/
104
105
/* PyDict_MINSIZE is the starting size for any new dict.
106
* 8 allows dicts with no more than 5 active entries; experiments suggested
107
* this suffices for the majority of dicts (consisting mostly of usually-small
108
* dicts created to pass keyword arguments).
109
* Making this 8, rather than 4 reduces the number of resizes for most
110
* dictionaries, without any significant extra memory use.
111
*/
112
#define PyDict_LOG_MINSIZE 3
113
#define PyDict_MINSIZE 8
114
115
#include "Python.h"
116
#include "pycore_bitutils.h" // _Py_bit_length
117
#include "pycore_call.h" // _PyObject_CallNoArgs()
118
#include "pycore_code.h" // stats
119
#include "pycore_dict.h" // PyDictKeysObject
120
#include "pycore_gc.h" // _PyObject_GC_IS_TRACKED()
121
#include "pycore_object.h" // _PyObject_GC_TRACK()
122
#include "pycore_pyerrors.h" // _PyErr_GetRaisedException()
123
#include "pycore_pystate.h" // _PyThreadState_GET()
124
#include "stringlib/eq.h" // unicode_eq()
125
126
#include <stdbool.h>
127
128
/*[clinic input]
129
class dict "PyDictObject *" "&PyDict_Type"
130
[clinic start generated code]*/
131
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
132
133
134
/*
135
To ensure the lookup algorithm terminates, there must be at least one Unused
136
slot (NULL key) in the table.
137
To avoid slowing down lookups on a near-full table, we resize the table when
138
it's USABLE_FRACTION (currently two-thirds) full.
139
*/
140
141
#define PERTURB_SHIFT 5
142
143
/*
144
Major subtleties ahead: Most hash schemes depend on having a "good" hash
145
function, in the sense of simulating randomness. Python doesn't: its most
146
important hash functions (for ints) are very regular in common
147
cases:
148
149
>>>[hash(i) for i in range(4)]
150
[0, 1, 2, 3]
151
152
This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
153
the low-order i bits as the initial table index is extremely fast, and there
154
are no collisions at all for dicts indexed by a contiguous range of ints. So
155
this gives better-than-random behavior in common cases, and that's very
156
desirable.
157
158
OTOH, when collisions occur, the tendency to fill contiguous slices of the
159
hash table makes a good collision resolution strategy crucial. Taking only
160
the last i bits of the hash code is also vulnerable: for example, consider
161
the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
162
their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
163
of every hash code are all 0: they *all* map to the same table index.
164
165
But catering to unusual cases should not slow the usual ones, so we just take
166
the last i bits anyway. It's up to collision resolution to do the rest. If
167
we *usually* find the key we're looking for on the first try (and, it turns
168
out, we usually do -- the table load factor is kept under 2/3, so the odds
169
are solidly in our favor), then it makes best sense to keep the initial index
170
computation dirt cheap.
171
172
The first half of collision resolution is to visit table indices via this
173
recurrence:
174
175
j = ((5*j) + 1) mod 2**i
176
177
For any initial j in range(2**i), repeating that 2**i times generates each
178
int in range(2**i) exactly once (see any text on random-number generation for
179
proof). By itself, this doesn't help much: like linear probing (setting
180
j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
181
order. This would be bad, except that's not the only thing we do, and it's
182
actually *good* in the common cases where hash keys are consecutive. In an
183
example that's really too small to make this entirely clear, for a table of
184
size 2**3 the order of indices is:
185
186
0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
187
188
If two things come in at index 5, the first place we look after is index 2,
189
not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
190
Linear probing is deadly in this case because there the fixed probe order
191
is the *same* as the order consecutive keys are likely to arrive. But it's
192
extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
193
and certain that consecutive hash codes do not.
194
195
The other half of the strategy is to get the other bits of the hash code
196
into play. This is done by initializing a (unsigned) vrbl "perturb" to the
197
full hash code, and changing the recurrence to:
198
199
perturb >>= PERTURB_SHIFT;
200
j = (5*j) + 1 + perturb;
201
use j % 2**i as the next table index;
202
203
Now the probe sequence depends (eventually) on every bit in the hash code,
204
and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
205
because it quickly magnifies small differences in the bits that didn't affect
206
the initial index. Note that because perturb is unsigned, if the recurrence
207
is executed often enough perturb eventually becomes and remains 0. At that
208
point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
209
that's certain to find an empty slot eventually (since it generates every int
210
in range(2**i), and we make sure there's always at least one empty slot).
211
212
Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
213
small so that the high bits of the hash code continue to affect the probe
214
sequence across iterations; but you want it large so that in really bad cases
215
the high-order hash bits have an effect on early iterations. 5 was "the
216
best" in minimizing total collisions across experiments Tim Peters ran (on
217
both normal and pathological cases), but 4 and 6 weren't significantly worse.
218
219
Historical: Reimer Behrends contributed the idea of using a polynomial-based
220
approach, using repeated multiplication by x in GF(2**n) where an irreducible
221
polynomial for each table size was chosen such that x was a primitive root.
222
Christian Tismer later extended that to use division by x instead, as an
223
efficient way to get the high bits of the hash code into play. This scheme
224
also gave excellent collision statistics, but was more expensive: two
225
if-tests were required inside the loop; computing "the next" index took about
226
the same number of operations but without as much potential parallelism
227
(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
228
above, and then shifting perturb can be done while the table index is being
229
masked); and the PyDictObject struct required a member to hold the table's
230
polynomial. In Tim's experiments the current scheme ran faster, produced
231
equally good collision statistics, needed less code & used less memory.
232
233
*/
234
235
static int dictresize(PyInterpreterState *interp, PyDictObject *mp,
236
uint8_t log_newsize, int unicode);
237
238
static PyObject* dict_iter(PyDictObject *dict);
239
240
#include "clinic/dictobject.c.h"
241
242
243
#if PyDict_MAXFREELIST > 0
244
static struct _Py_dict_state *
245
get_dict_state(PyInterpreterState *interp)
246
{
247
return &interp->dict_state;
248
}
249
#endif
250
251
252
void
253
_PyDict_ClearFreeList(PyInterpreterState *interp)
254
{
255
#if PyDict_MAXFREELIST > 0
256
struct _Py_dict_state *state = &interp->dict_state;
257
while (state->numfree) {
258
PyDictObject *op = state->free_list[--state->numfree];
259
assert(PyDict_CheckExact(op));
260
PyObject_GC_Del(op);
261
}
262
while (state->keys_numfree) {
263
PyObject_Free(state->keys_free_list[--state->keys_numfree]);
264
}
265
#endif
266
}
267
268
269
void
270
_PyDict_Fini(PyInterpreterState *interp)
271
{
272
_PyDict_ClearFreeList(interp);
273
#if defined(Py_DEBUG) && PyDict_MAXFREELIST > 0
274
struct _Py_dict_state *state = &interp->dict_state;
275
state->numfree = -1;
276
state->keys_numfree = -1;
277
#endif
278
}
279
280
static inline Py_hash_t
281
unicode_get_hash(PyObject *o)
282
{
283
assert(PyUnicode_CheckExact(o));
284
return _PyASCIIObject_CAST(o)->hash;
285
}
286
287
/* Print summary info about the state of the optimized allocator */
288
void
289
_PyDict_DebugMallocStats(FILE *out)
290
{
291
#if PyDict_MAXFREELIST > 0
292
PyInterpreterState *interp = _PyInterpreterState_GET();
293
struct _Py_dict_state *state = get_dict_state(interp);
294
_PyDebugAllocatorStats(out, "free PyDictObject",
295
state->numfree, sizeof(PyDictObject));
296
#endif
297
}
298
299
#define DK_MASK(dk) (DK_SIZE(dk)-1)
300
301
static void free_keys_object(PyInterpreterState *interp, PyDictKeysObject *keys);
302
303
/* PyDictKeysObject has refcounts like PyObject does, so we have the
304
following two functions to mirror what Py_INCREF() and Py_DECREF() do.
305
(Keep in mind that PyDictKeysObject isn't actually a PyObject.)
306
Likewise a PyDictKeysObject can be immortal (e.g. Py_EMPTY_KEYS),
307
so we apply a naive version of what Py_INCREF() and Py_DECREF() do
308
for immortal objects. */
309
310
static inline void
311
dictkeys_incref(PyDictKeysObject *dk)
312
{
313
if (dk->dk_refcnt == _Py_IMMORTAL_REFCNT) {
314
return;
315
}
316
#ifdef Py_REF_DEBUG
317
_Py_IncRefTotal(_PyInterpreterState_GET());
318
#endif
319
dk->dk_refcnt++;
320
}
321
322
static inline void
323
dictkeys_decref(PyInterpreterState *interp, PyDictKeysObject *dk)
324
{
325
if (dk->dk_refcnt == _Py_IMMORTAL_REFCNT) {
326
return;
327
}
328
assert(dk->dk_refcnt > 0);
329
#ifdef Py_REF_DEBUG
330
_Py_DecRefTotal(_PyInterpreterState_GET());
331
#endif
332
if (--dk->dk_refcnt == 0) {
333
free_keys_object(interp, dk);
334
}
335
}
336
337
/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
338
static inline Py_ssize_t
339
dictkeys_get_index(const PyDictKeysObject *keys, Py_ssize_t i)
340
{
341
int log2size = DK_LOG_SIZE(keys);
342
Py_ssize_t ix;
343
344
if (log2size < 8) {
345
const int8_t *indices = (const int8_t*)(keys->dk_indices);
346
ix = indices[i];
347
}
348
else if (log2size < 16) {
349
const int16_t *indices = (const int16_t*)(keys->dk_indices);
350
ix = indices[i];
351
}
352
#if SIZEOF_VOID_P > 4
353
else if (log2size >= 32) {
354
const int64_t *indices = (const int64_t*)(keys->dk_indices);
355
ix = indices[i];
356
}
357
#endif
358
else {
359
const int32_t *indices = (const int32_t*)(keys->dk_indices);
360
ix = indices[i];
361
}
362
assert(ix >= DKIX_DUMMY);
363
return ix;
364
}
365
366
/* write to indices. */
367
static inline void
368
dictkeys_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
369
{
370
int log2size = DK_LOG_SIZE(keys);
371
372
assert(ix >= DKIX_DUMMY);
373
assert(keys->dk_version == 0);
374
375
if (log2size < 8) {
376
int8_t *indices = (int8_t*)(keys->dk_indices);
377
assert(ix <= 0x7f);
378
indices[i] = (char)ix;
379
}
380
else if (log2size < 16) {
381
int16_t *indices = (int16_t*)(keys->dk_indices);
382
assert(ix <= 0x7fff);
383
indices[i] = (int16_t)ix;
384
}
385
#if SIZEOF_VOID_P > 4
386
else if (log2size >= 32) {
387
int64_t *indices = (int64_t*)(keys->dk_indices);
388
indices[i] = ix;
389
}
390
#endif
391
else {
392
int32_t *indices = (int32_t*)(keys->dk_indices);
393
assert(ix <= 0x7fffffff);
394
indices[i] = (int32_t)ix;
395
}
396
}
397
398
399
/* USABLE_FRACTION is the maximum dictionary load.
400
* Increasing this ratio makes dictionaries more dense resulting in more
401
* collisions. Decreasing it improves sparseness at the expense of spreading
402
* indices over more cache lines and at the cost of total memory consumed.
403
*
404
* USABLE_FRACTION must obey the following:
405
* (0 < USABLE_FRACTION(n) < n) for all n >= 2
406
*
407
* USABLE_FRACTION should be quick to calculate.
408
* Fractions around 1/2 to 2/3 seem to work well in practice.
409
*/
410
#define USABLE_FRACTION(n) (((n) << 1)/3)
411
412
/* Find the smallest dk_size >= minsize. */
413
static inline uint8_t
414
calculate_log2_keysize(Py_ssize_t minsize)
415
{
416
#if SIZEOF_LONG == SIZEOF_SIZE_T
417
minsize = (minsize | PyDict_MINSIZE) - 1;
418
return _Py_bit_length(minsize | (PyDict_MINSIZE-1));
419
#elif defined(_MSC_VER)
420
// On 64bit Windows, sizeof(long) == 4.
421
minsize = (minsize | PyDict_MINSIZE) - 1;
422
unsigned long msb;
423
_BitScanReverse64(&msb, (uint64_t)minsize);
424
return (uint8_t)(msb + 1);
425
#else
426
uint8_t log2_size;
427
for (log2_size = PyDict_LOG_MINSIZE;
428
(((Py_ssize_t)1) << log2_size) < minsize;
429
log2_size++)
430
;
431
return log2_size;
432
#endif
433
}
434
435
/* estimate_keysize is reverse function of USABLE_FRACTION.
436
*
437
* This can be used to reserve enough size to insert n entries without
438
* resizing.
439
*/
440
static inline uint8_t
441
estimate_log2_keysize(Py_ssize_t n)
442
{
443
return calculate_log2_keysize((n*3 + 1) / 2);
444
}
445
446
447
/* GROWTH_RATE. Growth rate upon hitting maximum load.
448
* Currently set to used*3.
449
* This means that dicts double in size when growing without deletions,
450
* but have more head room when the number of deletions is on a par with the
451
* number of insertions. See also bpo-17563 and bpo-33205.
452
*
453
* GROWTH_RATE was set to used*4 up to version 3.2.
454
* GROWTH_RATE was set to used*2 in version 3.3.0
455
* GROWTH_RATE was set to used*2 + capacity/2 in 3.4.0-3.6.0.
456
*/
457
#define GROWTH_RATE(d) ((d)->ma_used*3)
458
459
/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
460
* (which cannot fail and thus can do no allocation).
461
*/
462
static PyDictKeysObject empty_keys_struct = {
463
_Py_IMMORTAL_REFCNT, /* dk_refcnt */
464
0, /* dk_log2_size */
465
0, /* dk_log2_index_bytes */
466
DICT_KEYS_UNICODE, /* dk_kind */
467
1, /* dk_version */
468
0, /* dk_usable (immutable) */
469
0, /* dk_nentries */
470
{DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
471
DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}, /* dk_indices */
472
};
473
474
#define Py_EMPTY_KEYS &empty_keys_struct
475
476
/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
477
// #define DEBUG_PYDICT
478
479
#ifdef DEBUG_PYDICT
480
# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 1))
481
#else
482
# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 0))
483
#endif
484
485
static inline int
486
get_index_from_order(PyDictObject *mp, Py_ssize_t i)
487
{
488
assert(mp->ma_used <= SHARED_KEYS_MAX_SIZE);
489
assert(i < (((char *)mp->ma_values)[-2]));
490
return ((char *)mp->ma_values)[-3-i];
491
}
492
493
#ifdef DEBUG_PYDICT
494
static void
495
dump_entries(PyDictKeysObject *dk)
496
{
497
for (Py_ssize_t i = 0; i < dk->dk_nentries; i++) {
498
if (DK_IS_UNICODE(dk)) {
499
PyDictUnicodeEntry *ep = &DK_UNICODE_ENTRIES(dk)[i];
500
printf("key=%p value=%p\n", ep->me_key, ep->me_value);
501
}
502
else {
503
PyDictKeyEntry *ep = &DK_ENTRIES(dk)[i];
504
printf("key=%p hash=%lx value=%p\n", ep->me_key, ep->me_hash, ep->me_value);
505
}
506
}
507
}
508
#endif
509
510
int
511
_PyDict_CheckConsistency(PyObject *op, int check_content)
512
{
513
#define CHECK(expr) \
514
do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
515
516
assert(op != NULL);
517
CHECK(PyDict_Check(op));
518
PyDictObject *mp = (PyDictObject *)op;
519
520
PyDictKeysObject *keys = mp->ma_keys;
521
int splitted = _PyDict_HasSplitTable(mp);
522
Py_ssize_t usable = USABLE_FRACTION(DK_SIZE(keys));
523
524
CHECK(0 <= mp->ma_used && mp->ma_used <= usable);
525
CHECK(0 <= keys->dk_usable && keys->dk_usable <= usable);
526
CHECK(0 <= keys->dk_nentries && keys->dk_nentries <= usable);
527
CHECK(keys->dk_usable + keys->dk_nentries <= usable);
528
529
if (!splitted) {
530
/* combined table */
531
CHECK(keys->dk_kind != DICT_KEYS_SPLIT);
532
CHECK(keys->dk_refcnt == 1 || keys == Py_EMPTY_KEYS);
533
}
534
else {
535
CHECK(keys->dk_kind == DICT_KEYS_SPLIT);
536
CHECK(mp->ma_used <= SHARED_KEYS_MAX_SIZE);
537
}
538
539
if (check_content) {
540
for (Py_ssize_t i=0; i < DK_SIZE(keys); i++) {
541
Py_ssize_t ix = dictkeys_get_index(keys, i);
542
CHECK(DKIX_DUMMY <= ix && ix <= usable);
543
}
544
545
if (keys->dk_kind == DICT_KEYS_GENERAL) {
546
PyDictKeyEntry *entries = DK_ENTRIES(keys);
547
for (Py_ssize_t i=0; i < usable; i++) {
548
PyDictKeyEntry *entry = &entries[i];
549
PyObject *key = entry->me_key;
550
551
if (key != NULL) {
552
/* test_dict fails if PyObject_Hash() is called again */
553
CHECK(entry->me_hash != -1);
554
CHECK(entry->me_value != NULL);
555
556
if (PyUnicode_CheckExact(key)) {
557
Py_hash_t hash = unicode_get_hash(key);
558
CHECK(entry->me_hash == hash);
559
}
560
}
561
}
562
}
563
else {
564
PyDictUnicodeEntry *entries = DK_UNICODE_ENTRIES(keys);
565
for (Py_ssize_t i=0; i < usable; i++) {
566
PyDictUnicodeEntry *entry = &entries[i];
567
PyObject *key = entry->me_key;
568
569
if (key != NULL) {
570
CHECK(PyUnicode_CheckExact(key));
571
Py_hash_t hash = unicode_get_hash(key);
572
CHECK(hash != -1);
573
if (!splitted) {
574
CHECK(entry->me_value != NULL);
575
}
576
}
577
578
if (splitted) {
579
CHECK(entry->me_value == NULL);
580
}
581
}
582
}
583
584
if (splitted) {
585
CHECK(mp->ma_used <= SHARED_KEYS_MAX_SIZE);
586
/* splitted table */
587
int duplicate_check = 0;
588
for (Py_ssize_t i=0; i < mp->ma_used; i++) {
589
int index = get_index_from_order(mp, i);
590
CHECK((duplicate_check & (1<<index)) == 0);
591
duplicate_check |= (1<<index);
592
CHECK(mp->ma_values->values[index] != NULL);
593
}
594
}
595
}
596
return 1;
597
598
#undef CHECK
599
}
600
601
602
static PyDictKeysObject*
603
new_keys_object(PyInterpreterState *interp, uint8_t log2_size, bool unicode)
604
{
605
PyDictKeysObject *dk;
606
Py_ssize_t usable;
607
int log2_bytes;
608
size_t entry_size = unicode ? sizeof(PyDictUnicodeEntry) : sizeof(PyDictKeyEntry);
609
610
assert(log2_size >= PyDict_LOG_MINSIZE);
611
612
usable = USABLE_FRACTION((size_t)1<<log2_size);
613
if (log2_size < 8) {
614
log2_bytes = log2_size;
615
}
616
else if (log2_size < 16) {
617
log2_bytes = log2_size + 1;
618
}
619
#if SIZEOF_VOID_P > 4
620
else if (log2_size >= 32) {
621
log2_bytes = log2_size + 3;
622
}
623
#endif
624
else {
625
log2_bytes = log2_size + 2;
626
}
627
628
#if PyDict_MAXFREELIST > 0
629
struct _Py_dict_state *state = get_dict_state(interp);
630
#ifdef Py_DEBUG
631
// new_keys_object() must not be called after _PyDict_Fini()
632
assert(state->keys_numfree != -1);
633
#endif
634
if (log2_size == PyDict_LOG_MINSIZE && unicode && state->keys_numfree > 0) {
635
dk = state->keys_free_list[--state->keys_numfree];
636
OBJECT_STAT_INC(from_freelist);
637
}
638
else
639
#endif
640
{
641
dk = PyObject_Malloc(sizeof(PyDictKeysObject)
642
+ ((size_t)1 << log2_bytes)
643
+ entry_size * usable);
644
if (dk == NULL) {
645
PyErr_NoMemory();
646
return NULL;
647
}
648
}
649
#ifdef Py_REF_DEBUG
650
_Py_IncRefTotal(_PyInterpreterState_GET());
651
#endif
652
dk->dk_refcnt = 1;
653
dk->dk_log2_size = log2_size;
654
dk->dk_log2_index_bytes = log2_bytes;
655
dk->dk_kind = unicode ? DICT_KEYS_UNICODE : DICT_KEYS_GENERAL;
656
dk->dk_nentries = 0;
657
dk->dk_usable = usable;
658
dk->dk_version = 0;
659
memset(&dk->dk_indices[0], 0xff, ((size_t)1 << log2_bytes));
660
memset(&dk->dk_indices[(size_t)1 << log2_bytes], 0, entry_size * usable);
661
return dk;
662
}
663
664
static void
665
free_keys_object(PyInterpreterState *interp, PyDictKeysObject *keys)
666
{
667
assert(keys != Py_EMPTY_KEYS);
668
if (DK_IS_UNICODE(keys)) {
669
PyDictUnicodeEntry *entries = DK_UNICODE_ENTRIES(keys);
670
Py_ssize_t i, n;
671
for (i = 0, n = keys->dk_nentries; i < n; i++) {
672
Py_XDECREF(entries[i].me_key);
673
Py_XDECREF(entries[i].me_value);
674
}
675
}
676
else {
677
PyDictKeyEntry *entries = DK_ENTRIES(keys);
678
Py_ssize_t i, n;
679
for (i = 0, n = keys->dk_nentries; i < n; i++) {
680
Py_XDECREF(entries[i].me_key);
681
Py_XDECREF(entries[i].me_value);
682
}
683
}
684
#if PyDict_MAXFREELIST > 0
685
struct _Py_dict_state *state = get_dict_state(interp);
686
#ifdef Py_DEBUG
687
// free_keys_object() must not be called after _PyDict_Fini()
688
assert(state->keys_numfree != -1);
689
#endif
690
if (DK_LOG_SIZE(keys) == PyDict_LOG_MINSIZE
691
&& state->keys_numfree < PyDict_MAXFREELIST
692
&& DK_IS_UNICODE(keys)) {
693
state->keys_free_list[state->keys_numfree++] = keys;
694
OBJECT_STAT_INC(to_freelist);
695
return;
696
}
697
#endif
698
PyObject_Free(keys);
699
}
700
701
static inline PyDictValues*
702
new_values(size_t size)
703
{
704
assert(size >= 1);
705
size_t prefix_size = _Py_SIZE_ROUND_UP(size+2, sizeof(PyObject *));
706
assert(prefix_size < 256);
707
size_t n = prefix_size + size * sizeof(PyObject *);
708
uint8_t *mem = PyMem_Malloc(n);
709
if (mem == NULL) {
710
return NULL;
711
}
712
assert(prefix_size % sizeof(PyObject *) == 0);
713
mem[prefix_size-1] = (uint8_t)prefix_size;
714
return (PyDictValues*)(mem + prefix_size);
715
}
716
717
static inline void
718
free_values(PyDictValues *values)
719
{
720
int prefix_size = ((uint8_t *)values)[-1];
721
PyMem_Free(((char *)values)-prefix_size);
722
}
723
724
/* Consumes a reference to the keys object */
725
static PyObject *
726
new_dict(PyInterpreterState *interp,
727
PyDictKeysObject *keys, PyDictValues *values,
728
Py_ssize_t used, int free_values_on_failure)
729
{
730
PyDictObject *mp;
731
assert(keys != NULL);
732
#if PyDict_MAXFREELIST > 0
733
struct _Py_dict_state *state = get_dict_state(interp);
734
#ifdef Py_DEBUG
735
// new_dict() must not be called after _PyDict_Fini()
736
assert(state->numfree != -1);
737
#endif
738
if (state->numfree) {
739
mp = state->free_list[--state->numfree];
740
assert (mp != NULL);
741
assert (Py_IS_TYPE(mp, &PyDict_Type));
742
OBJECT_STAT_INC(from_freelist);
743
_Py_NewReference((PyObject *)mp);
744
}
745
else
746
#endif
747
{
748
mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
749
if (mp == NULL) {
750
dictkeys_decref(interp, keys);
751
if (free_values_on_failure) {
752
free_values(values);
753
}
754
return NULL;
755
}
756
}
757
mp->ma_keys = keys;
758
mp->ma_values = values;
759
mp->ma_used = used;
760
mp->ma_version_tag = DICT_NEXT_VERSION(interp);
761
ASSERT_CONSISTENT(mp);
762
return (PyObject *)mp;
763
}
764
765
static inline size_t
766
shared_keys_usable_size(PyDictKeysObject *keys)
767
{
768
return (size_t)keys->dk_nentries + (size_t)keys->dk_usable;
769
}
770
771
/* Consumes a reference to the keys object */
772
static PyObject *
773
new_dict_with_shared_keys(PyInterpreterState *interp, PyDictKeysObject *keys)
774
{
775
size_t size = shared_keys_usable_size(keys);
776
PyDictValues *values = new_values(size);
777
if (values == NULL) {
778
dictkeys_decref(interp, keys);
779
return PyErr_NoMemory();
780
}
781
((char *)values)[-2] = 0;
782
for (size_t i = 0; i < size; i++) {
783
values->values[i] = NULL;
784
}
785
return new_dict(interp, keys, values, 0, 1);
786
}
787
788
789
static PyDictKeysObject *
790
clone_combined_dict_keys(PyDictObject *orig)
791
{
792
assert(PyDict_Check(orig));
793
assert(Py_TYPE(orig)->tp_iter == (getiterfunc)dict_iter);
794
assert(orig->ma_values == NULL);
795
assert(orig->ma_keys != Py_EMPTY_KEYS);
796
assert(orig->ma_keys->dk_refcnt == 1);
797
798
size_t keys_size = _PyDict_KeysSize(orig->ma_keys);
799
PyDictKeysObject *keys = PyObject_Malloc(keys_size);
800
if (keys == NULL) {
801
PyErr_NoMemory();
802
return NULL;
803
}
804
805
memcpy(keys, orig->ma_keys, keys_size);
806
807
/* After copying key/value pairs, we need to incref all
808
keys and values and they are about to be co-owned by a
809
new dict object. */
810
PyObject **pkey, **pvalue;
811
size_t offs;
812
if (DK_IS_UNICODE(orig->ma_keys)) {
813
PyDictUnicodeEntry *ep0 = DK_UNICODE_ENTRIES(keys);
814
pkey = &ep0->me_key;
815
pvalue = &ep0->me_value;
816
offs = sizeof(PyDictUnicodeEntry) / sizeof(PyObject*);
817
}
818
else {
819
PyDictKeyEntry *ep0 = DK_ENTRIES(keys);
820
pkey = &ep0->me_key;
821
pvalue = &ep0->me_value;
822
offs = sizeof(PyDictKeyEntry) / sizeof(PyObject*);
823
}
824
825
Py_ssize_t n = keys->dk_nentries;
826
for (Py_ssize_t i = 0; i < n; i++) {
827
PyObject *value = *pvalue;
828
if (value != NULL) {
829
Py_INCREF(value);
830
Py_INCREF(*pkey);
831
}
832
pvalue += offs;
833
pkey += offs;
834
}
835
836
/* Since we copied the keys table we now have an extra reference
837
in the system. Manually call increment _Py_RefTotal to signal that
838
we have it now; calling dictkeys_incref would be an error as
839
keys->dk_refcnt is already set to 1 (after memcpy). */
840
#ifdef Py_REF_DEBUG
841
_Py_IncRefTotal(_PyInterpreterState_GET());
842
#endif
843
return keys;
844
}
845
846
PyObject *
847
PyDict_New(void)
848
{
849
PyInterpreterState *interp = _PyInterpreterState_GET();
850
/* We don't incref Py_EMPTY_KEYS here because it is immortal. */
851
return new_dict(interp, Py_EMPTY_KEYS, NULL, 0, 0);
852
}
853
854
/* Search index of hash table from offset of entry table */
855
static Py_ssize_t
856
lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
857
{
858
size_t mask = DK_MASK(k);
859
size_t perturb = (size_t)hash;
860
size_t i = (size_t)hash & mask;
861
862
for (;;) {
863
Py_ssize_t ix = dictkeys_get_index(k, i);
864
if (ix == index) {
865
return i;
866
}
867
if (ix == DKIX_EMPTY) {
868
return DKIX_EMPTY;
869
}
870
perturb >>= PERTURB_SHIFT;
871
i = mask & (i*5 + perturb + 1);
872
}
873
Py_UNREACHABLE();
874
}
875
876
// Search non-Unicode key from Unicode table
877
static Py_ssize_t
878
unicodekeys_lookup_generic(PyDictObject *mp, PyDictKeysObject* dk, PyObject *key, Py_hash_t hash)
879
{
880
PyDictUnicodeEntry *ep0 = DK_UNICODE_ENTRIES(dk);
881
size_t mask = DK_MASK(dk);
882
size_t perturb = hash;
883
size_t i = (size_t)hash & mask;
884
Py_ssize_t ix;
885
for (;;) {
886
ix = dictkeys_get_index(dk, i);
887
if (ix >= 0) {
888
PyDictUnicodeEntry *ep = &ep0[ix];
889
assert(ep->me_key != NULL);
890
assert(PyUnicode_CheckExact(ep->me_key));
891
if (ep->me_key == key) {
892
return ix;
893
}
894
if (unicode_get_hash(ep->me_key) == hash) {
895
PyObject *startkey = ep->me_key;
896
Py_INCREF(startkey);
897
int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
898
Py_DECREF(startkey);
899
if (cmp < 0) {
900
return DKIX_ERROR;
901
}
902
if (dk == mp->ma_keys && ep->me_key == startkey) {
903
if (cmp > 0) {
904
return ix;
905
}
906
}
907
else {
908
/* The dict was mutated, restart */
909
return DKIX_KEY_CHANGED;
910
}
911
}
912
}
913
else if (ix == DKIX_EMPTY) {
914
return DKIX_EMPTY;
915
}
916
perturb >>= PERTURB_SHIFT;
917
i = mask & (i*5 + perturb + 1);
918
}
919
Py_UNREACHABLE();
920
}
921
922
// Search Unicode key from Unicode table.
923
static Py_ssize_t _Py_HOT_FUNCTION
924
unicodekeys_lookup_unicode(PyDictKeysObject* dk, PyObject *key, Py_hash_t hash)
925
{
926
PyDictUnicodeEntry *ep0 = DK_UNICODE_ENTRIES(dk);
927
size_t mask = DK_MASK(dk);
928
size_t perturb = hash;
929
size_t i = (size_t)hash & mask;
930
Py_ssize_t ix;
931
for (;;) {
932
ix = dictkeys_get_index(dk, i);
933
if (ix >= 0) {
934
PyDictUnicodeEntry *ep = &ep0[ix];
935
assert(ep->me_key != NULL);
936
assert(PyUnicode_CheckExact(ep->me_key));
937
if (ep->me_key == key ||
938
(unicode_get_hash(ep->me_key) == hash && unicode_eq(ep->me_key, key))) {
939
return ix;
940
}
941
}
942
else if (ix == DKIX_EMPTY) {
943
return DKIX_EMPTY;
944
}
945
perturb >>= PERTURB_SHIFT;
946
i = mask & (i*5 + perturb + 1);
947
// Manual loop unrolling
948
ix = dictkeys_get_index(dk, i);
949
if (ix >= 0) {
950
PyDictUnicodeEntry *ep = &ep0[ix];
951
assert(ep->me_key != NULL);
952
assert(PyUnicode_CheckExact(ep->me_key));
953
if (ep->me_key == key ||
954
(unicode_get_hash(ep->me_key) == hash && unicode_eq(ep->me_key, key))) {
955
return ix;
956
}
957
}
958
else if (ix == DKIX_EMPTY) {
959
return DKIX_EMPTY;
960
}
961
perturb >>= PERTURB_SHIFT;
962
i = mask & (i*5 + perturb + 1);
963
}
964
Py_UNREACHABLE();
965
}
966
967
// Search key from Generic table.
968
static Py_ssize_t
969
dictkeys_generic_lookup(PyDictObject *mp, PyDictKeysObject* dk, PyObject *key, Py_hash_t hash)
970
{
971
PyDictKeyEntry *ep0 = DK_ENTRIES(dk);
972
size_t mask = DK_MASK(dk);
973
size_t perturb = hash;
974
size_t i = (size_t)hash & mask;
975
Py_ssize_t ix;
976
for (;;) {
977
ix = dictkeys_get_index(dk, i);
978
if (ix >= 0) {
979
PyDictKeyEntry *ep = &ep0[ix];
980
assert(ep->me_key != NULL);
981
if (ep->me_key == key) {
982
return ix;
983
}
984
if (ep->me_hash == hash) {
985
PyObject *startkey = ep->me_key;
986
Py_INCREF(startkey);
987
int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
988
Py_DECREF(startkey);
989
if (cmp < 0) {
990
return DKIX_ERROR;
991
}
992
if (dk == mp->ma_keys && ep->me_key == startkey) {
993
if (cmp > 0) {
994
return ix;
995
}
996
}
997
else {
998
/* The dict was mutated, restart */
999
return DKIX_KEY_CHANGED;
1000
}
1001
}
1002
}
1003
else if (ix == DKIX_EMPTY) {
1004
return DKIX_EMPTY;
1005
}
1006
perturb >>= PERTURB_SHIFT;
1007
i = mask & (i*5 + perturb + 1);
1008
}
1009
Py_UNREACHABLE();
1010
}
1011
1012
/* Lookup a string in a (all unicode) dict keys.
1013
* Returns DKIX_ERROR if key is not a string,
1014
* or if the dict keys is not all strings.
1015
* If the keys is present then return the index of key.
1016
* If the key is not present then return DKIX_EMPTY.
1017
*/
1018
Py_ssize_t
1019
_PyDictKeys_StringLookup(PyDictKeysObject* dk, PyObject *key)
1020
{
1021
DictKeysKind kind = dk->dk_kind;
1022
if (!PyUnicode_CheckExact(key) || kind == DICT_KEYS_GENERAL) {
1023
return DKIX_ERROR;
1024
}
1025
Py_hash_t hash = unicode_get_hash(key);
1026
if (hash == -1) {
1027
hash = PyUnicode_Type.tp_hash(key);
1028
if (hash == -1) {
1029
PyErr_Clear();
1030
return DKIX_ERROR;
1031
}
1032
}
1033
return unicodekeys_lookup_unicode(dk, key, hash);
1034
}
1035
1036
/*
1037
The basic lookup function used by all operations.
1038
This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
1039
Open addressing is preferred over chaining since the link overhead for
1040
chaining would be substantial (100% with typical malloc overhead).
1041
1042
The initial probe index is computed as hash mod the table size. Subsequent
1043
probe indices are computed as explained earlier.
1044
1045
All arithmetic on hash should ignore overflow.
1046
1047
_Py_dict_lookup() is general-purpose, and may return DKIX_ERROR if (and only if) a
1048
comparison raises an exception.
1049
When the key isn't found a DKIX_EMPTY is returned.
1050
*/
1051
Py_ssize_t
1052
_Py_dict_lookup(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject **value_addr)
1053
{
1054
PyDictKeysObject *dk;
1055
DictKeysKind kind;
1056
Py_ssize_t ix;
1057
1058
start:
1059
dk = mp->ma_keys;
1060
kind = dk->dk_kind;
1061
1062
if (kind != DICT_KEYS_GENERAL) {
1063
if (PyUnicode_CheckExact(key)) {
1064
ix = unicodekeys_lookup_unicode(dk, key, hash);
1065
}
1066
else {
1067
ix = unicodekeys_lookup_generic(mp, dk, key, hash);
1068
if (ix == DKIX_KEY_CHANGED) {
1069
goto start;
1070
}
1071
}
1072
1073
if (ix >= 0) {
1074
if (kind == DICT_KEYS_SPLIT) {
1075
*value_addr = mp->ma_values->values[ix];
1076
}
1077
else {
1078
*value_addr = DK_UNICODE_ENTRIES(dk)[ix].me_value;
1079
}
1080
}
1081
else {
1082
*value_addr = NULL;
1083
}
1084
}
1085
else {
1086
ix = dictkeys_generic_lookup(mp, dk, key, hash);
1087
if (ix == DKIX_KEY_CHANGED) {
1088
goto start;
1089
}
1090
if (ix >= 0) {
1091
*value_addr = DK_ENTRIES(dk)[ix].me_value;
1092
}
1093
else {
1094
*value_addr = NULL;
1095
}
1096
}
1097
1098
return ix;
1099
}
1100
1101
int
1102
_PyDict_HasOnlyStringKeys(PyObject *dict)
1103
{
1104
Py_ssize_t pos = 0;
1105
PyObject *key, *value;
1106
assert(PyDict_Check(dict));
1107
/* Shortcut */
1108
if (((PyDictObject *)dict)->ma_keys->dk_kind != DICT_KEYS_GENERAL)
1109
return 1;
1110
while (PyDict_Next(dict, &pos, &key, &value))
1111
if (!PyUnicode_Check(key))
1112
return 0;
1113
return 1;
1114
}
1115
1116
#define MAINTAIN_TRACKING(mp, key, value) \
1117
do { \
1118
if (!_PyObject_GC_IS_TRACKED(mp)) { \
1119
if (_PyObject_GC_MAY_BE_TRACKED(key) || \
1120
_PyObject_GC_MAY_BE_TRACKED(value)) { \
1121
_PyObject_GC_TRACK(mp); \
1122
} \
1123
} \
1124
} while(0)
1125
1126
void
1127
_PyDict_MaybeUntrack(PyObject *op)
1128
{
1129
PyDictObject *mp;
1130
PyObject *value;
1131
Py_ssize_t i, numentries;
1132
1133
if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
1134
return;
1135
1136
mp = (PyDictObject *) op;
1137
numentries = mp->ma_keys->dk_nentries;
1138
if (_PyDict_HasSplitTable(mp)) {
1139
for (i = 0; i < numentries; i++) {
1140
if ((value = mp->ma_values->values[i]) == NULL)
1141
continue;
1142
if (_PyObject_GC_MAY_BE_TRACKED(value)) {
1143
return;
1144
}
1145
}
1146
}
1147
else {
1148
if (DK_IS_UNICODE(mp->ma_keys)) {
1149
PyDictUnicodeEntry *ep0 = DK_UNICODE_ENTRIES(mp->ma_keys);
1150
for (i = 0; i < numentries; i++) {
1151
if ((value = ep0[i].me_value) == NULL)
1152
continue;
1153
if (_PyObject_GC_MAY_BE_TRACKED(value))
1154
return;
1155
}
1156
}
1157
else {
1158
PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
1159
for (i = 0; i < numentries; i++) {
1160
if ((value = ep0[i].me_value) == NULL)
1161
continue;
1162
if (_PyObject_GC_MAY_BE_TRACKED(value) ||
1163
_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
1164
return;
1165
}
1166
}
1167
}
1168
_PyObject_GC_UNTRACK(op);
1169
}
1170
1171
/* Internal function to find slot for an item from its hash
1172
when it is known that the key is not present in the dict.
1173
1174
The dict must be combined. */
1175
static Py_ssize_t
1176
find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
1177
{
1178
assert(keys != NULL);
1179
1180
const size_t mask = DK_MASK(keys);
1181
size_t i = hash & mask;
1182
Py_ssize_t ix = dictkeys_get_index(keys, i);
1183
for (size_t perturb = hash; ix >= 0;) {
1184
perturb >>= PERTURB_SHIFT;
1185
i = (i*5 + perturb + 1) & mask;
1186
ix = dictkeys_get_index(keys, i);
1187
}
1188
return i;
1189
}
1190
1191
static int
1192
insertion_resize(PyInterpreterState *interp, PyDictObject *mp, int unicode)
1193
{
1194
return dictresize(interp, mp, calculate_log2_keysize(GROWTH_RATE(mp)), unicode);
1195
}
1196
1197
static Py_ssize_t
1198
insert_into_dictkeys(PyDictKeysObject *keys, PyObject *name)
1199
{
1200
assert(PyUnicode_CheckExact(name));
1201
Py_hash_t hash = unicode_get_hash(name);
1202
if (hash == -1) {
1203
hash = PyUnicode_Type.tp_hash(name);
1204
if (hash == -1) {
1205
PyErr_Clear();
1206
return DKIX_EMPTY;
1207
}
1208
}
1209
Py_ssize_t ix = unicodekeys_lookup_unicode(keys, name, hash);
1210
if (ix == DKIX_EMPTY) {
1211
if (keys->dk_usable <= 0) {
1212
return DKIX_EMPTY;
1213
}
1214
/* Insert into new slot. */
1215
keys->dk_version = 0;
1216
Py_ssize_t hashpos = find_empty_slot(keys, hash);
1217
ix = keys->dk_nentries;
1218
PyDictUnicodeEntry *ep = &DK_UNICODE_ENTRIES(keys)[ix];
1219
dictkeys_set_index(keys, hashpos, ix);
1220
assert(ep->me_key == NULL);
1221
ep->me_key = Py_NewRef(name);
1222
keys->dk_usable--;
1223
keys->dk_nentries++;
1224
}
1225
assert (ix < SHARED_KEYS_MAX_SIZE);
1226
return ix;
1227
}
1228
1229
/*
1230
Internal routine to insert a new item into the table.
1231
Used both by the internal resize routine and by the public insert routine.
1232
Returns -1 if an error occurred, or 0 on success.
1233
Consumes key and value references.
1234
*/
1235
static int
1236
insertdict(PyInterpreterState *interp, PyDictObject *mp,
1237
PyObject *key, Py_hash_t hash, PyObject *value)
1238
{
1239
PyObject *old_value;
1240
1241
if (DK_IS_UNICODE(mp->ma_keys) && !PyUnicode_CheckExact(key)) {
1242
if (insertion_resize(interp, mp, 0) < 0)
1243
goto Fail;
1244
assert(mp->ma_keys->dk_kind == DICT_KEYS_GENERAL);
1245
}
1246
1247
Py_ssize_t ix = _Py_dict_lookup(mp, key, hash, &old_value);
1248
if (ix == DKIX_ERROR)
1249
goto Fail;
1250
1251
MAINTAIN_TRACKING(mp, key, value);
1252
1253
if (ix == DKIX_EMPTY) {
1254
uint64_t new_version = _PyDict_NotifyEvent(
1255
interp, PyDict_EVENT_ADDED, mp, key, value);
1256
/* Insert into new slot. */
1257
mp->ma_keys->dk_version = 0;
1258
assert(old_value == NULL);
1259
if (mp->ma_keys->dk_usable <= 0) {
1260
/* Need to resize. */
1261
if (insertion_resize(interp, mp, 1) < 0)
1262
goto Fail;
1263
}
1264
1265
Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
1266
dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
1267
1268
if (DK_IS_UNICODE(mp->ma_keys)) {
1269
PyDictUnicodeEntry *ep;
1270
ep = &DK_UNICODE_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
1271
ep->me_key = key;
1272
if (mp->ma_values) {
1273
Py_ssize_t index = mp->ma_keys->dk_nentries;
1274
_PyDictValues_AddToInsertionOrder(mp->ma_values, index);
1275
assert (mp->ma_values->values[index] == NULL);
1276
mp->ma_values->values[index] = value;
1277
}
1278
else {
1279
ep->me_value = value;
1280
}
1281
}
1282
else {
1283
PyDictKeyEntry *ep;
1284
ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
1285
ep->me_key = key;
1286
ep->me_hash = hash;
1287
ep->me_value = value;
1288
}
1289
mp->ma_used++;
1290
mp->ma_version_tag = new_version;
1291
mp->ma_keys->dk_usable--;
1292
mp->ma_keys->dk_nentries++;
1293
assert(mp->ma_keys->dk_usable >= 0);
1294
ASSERT_CONSISTENT(mp);
1295
return 0;
1296
}
1297
1298
if (old_value != value) {
1299
uint64_t new_version = _PyDict_NotifyEvent(
1300
interp, PyDict_EVENT_MODIFIED, mp, key, value);
1301
if (_PyDict_HasSplitTable(mp)) {
1302
mp->ma_values->values[ix] = value;
1303
if (old_value == NULL) {
1304
_PyDictValues_AddToInsertionOrder(mp->ma_values, ix);
1305
mp->ma_used++;
1306
}
1307
}
1308
else {
1309
assert(old_value != NULL);
1310
if (DK_IS_UNICODE(mp->ma_keys)) {
1311
DK_UNICODE_ENTRIES(mp->ma_keys)[ix].me_value = value;
1312
}
1313
else {
1314
DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
1315
}
1316
}
1317
mp->ma_version_tag = new_version;
1318
}
1319
Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
1320
ASSERT_CONSISTENT(mp);
1321
Py_DECREF(key);
1322
return 0;
1323
1324
Fail:
1325
Py_DECREF(value);
1326
Py_DECREF(key);
1327
return -1;
1328
}
1329
1330
// Same to insertdict but specialized for ma_keys = Py_EMPTY_KEYS.
1331
// Consumes key and value references.
1332
static int
1333
insert_to_emptydict(PyInterpreterState *interp, PyDictObject *mp,
1334
PyObject *key, Py_hash_t hash, PyObject *value)
1335
{
1336
assert(mp->ma_keys == Py_EMPTY_KEYS);
1337
1338
uint64_t new_version = _PyDict_NotifyEvent(
1339
interp, PyDict_EVENT_ADDED, mp, key, value);
1340
1341
int unicode = PyUnicode_CheckExact(key);
1342
PyDictKeysObject *newkeys = new_keys_object(
1343
interp, PyDict_LOG_MINSIZE, unicode);
1344
if (newkeys == NULL) {
1345
Py_DECREF(key);
1346
Py_DECREF(value);
1347
return -1;
1348
}
1349
/* We don't decref Py_EMPTY_KEYS here because it is immortal. */
1350
mp->ma_keys = newkeys;
1351
mp->ma_values = NULL;
1352
1353
MAINTAIN_TRACKING(mp, key, value);
1354
1355
size_t hashpos = (size_t)hash & (PyDict_MINSIZE-1);
1356
dictkeys_set_index(mp->ma_keys, hashpos, 0);
1357
if (unicode) {
1358
PyDictUnicodeEntry *ep = DK_UNICODE_ENTRIES(mp->ma_keys);
1359
ep->me_key = key;
1360
ep->me_value = value;
1361
}
1362
else {
1363
PyDictKeyEntry *ep = DK_ENTRIES(mp->ma_keys);
1364
ep->me_key = key;
1365
ep->me_hash = hash;
1366
ep->me_value = value;
1367
}
1368
mp->ma_used++;
1369
mp->ma_version_tag = new_version;
1370
mp->ma_keys->dk_usable--;
1371
mp->ma_keys->dk_nentries++;
1372
return 0;
1373
}
1374
1375
/*
1376
Internal routine used by dictresize() to build a hashtable of entries.
1377
*/
1378
static void
1379
build_indices_generic(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
1380
{
1381
size_t mask = DK_MASK(keys);
1382
for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1383
Py_hash_t hash = ep->me_hash;
1384
size_t i = hash & mask;
1385
for (size_t perturb = hash; dictkeys_get_index(keys, i) != DKIX_EMPTY;) {
1386
perturb >>= PERTURB_SHIFT;
1387
i = mask & (i*5 + perturb + 1);
1388
}
1389
dictkeys_set_index(keys, i, ix);
1390
}
1391
}
1392
1393
static void
1394
build_indices_unicode(PyDictKeysObject *keys, PyDictUnicodeEntry *ep, Py_ssize_t n)
1395
{
1396
size_t mask = DK_MASK(keys);
1397
for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1398
Py_hash_t hash = unicode_get_hash(ep->me_key);
1399
assert(hash != -1);
1400
size_t i = hash & mask;
1401
for (size_t perturb = hash; dictkeys_get_index(keys, i) != DKIX_EMPTY;) {
1402
perturb >>= PERTURB_SHIFT;
1403
i = mask & (i*5 + perturb + 1);
1404
}
1405
dictkeys_set_index(keys, i, ix);
1406
}
1407
}
1408
1409
/*
1410
Restructure the table by allocating a new table and reinserting all
1411
items again. When entries have been deleted, the new table may
1412
actually be smaller than the old one.
1413
If a table is split (its keys and hashes are shared, its values are not),
1414
then the values are temporarily copied into the table, it is resized as
1415
a combined table, then the me_value slots in the old table are NULLed out.
1416
After resizing a table is always combined.
1417
1418
This function supports:
1419
- Unicode split -> Unicode combined or Generic
1420
- Unicode combined -> Unicode combined or Generic
1421
- Generic -> Generic
1422
*/
1423
static int
1424
dictresize(PyInterpreterState *interp, PyDictObject *mp,
1425
uint8_t log2_newsize, int unicode)
1426
{
1427
PyDictKeysObject *oldkeys;
1428
PyDictValues *oldvalues;
1429
1430
if (log2_newsize >= SIZEOF_SIZE_T*8) {
1431
PyErr_NoMemory();
1432
return -1;
1433
}
1434
assert(log2_newsize >= PyDict_LOG_MINSIZE);
1435
1436
oldkeys = mp->ma_keys;
1437
oldvalues = mp->ma_values;
1438
1439
if (!DK_IS_UNICODE(oldkeys)) {
1440
unicode = 0;
1441
}
1442
1443
/* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1444
* So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1445
* TODO: Try reusing oldkeys when reimplement odict.
1446
*/
1447
1448
/* Allocate a new table. */
1449
mp->ma_keys = new_keys_object(interp, log2_newsize, unicode);
1450
if (mp->ma_keys == NULL) {
1451
mp->ma_keys = oldkeys;
1452
return -1;
1453
}
1454
// New table must be large enough.
1455
assert(mp->ma_keys->dk_usable >= mp->ma_used);
1456
1457
Py_ssize_t numentries = mp->ma_used;
1458
1459
if (oldvalues != NULL) {
1460
PyDictUnicodeEntry *oldentries = DK_UNICODE_ENTRIES(oldkeys);
1461
/* Convert split table into new combined table.
1462
* We must incref keys; we can transfer values.
1463
*/
1464
if (mp->ma_keys->dk_kind == DICT_KEYS_GENERAL) {
1465
// split -> generic
1466
PyDictKeyEntry *newentries = DK_ENTRIES(mp->ma_keys);
1467
1468
for (Py_ssize_t i = 0; i < numentries; i++) {
1469
int index = get_index_from_order(mp, i);
1470
PyDictUnicodeEntry *ep = &oldentries[index];
1471
assert(oldvalues->values[index] != NULL);
1472
newentries[i].me_key = Py_NewRef(ep->me_key);
1473
newentries[i].me_hash = unicode_get_hash(ep->me_key);
1474
newentries[i].me_value = oldvalues->values[index];
1475
}
1476
build_indices_generic(mp->ma_keys, newentries, numentries);
1477
}
1478
else { // split -> combined unicode
1479
PyDictUnicodeEntry *newentries = DK_UNICODE_ENTRIES(mp->ma_keys);
1480
1481
for (Py_ssize_t i = 0; i < numentries; i++) {
1482
int index = get_index_from_order(mp, i);
1483
PyDictUnicodeEntry *ep = &oldentries[index];
1484
assert(oldvalues->values[index] != NULL);
1485
newentries[i].me_key = Py_NewRef(ep->me_key);
1486
newentries[i].me_value = oldvalues->values[index];
1487
}
1488
build_indices_unicode(mp->ma_keys, newentries, numentries);
1489
}
1490
dictkeys_decref(interp, oldkeys);
1491
mp->ma_values = NULL;
1492
free_values(oldvalues);
1493
}
1494
else { // oldkeys is combined.
1495
if (oldkeys->dk_kind == DICT_KEYS_GENERAL) {
1496
// generic -> generic
1497
assert(mp->ma_keys->dk_kind == DICT_KEYS_GENERAL);
1498
PyDictKeyEntry *oldentries = DK_ENTRIES(oldkeys);
1499
PyDictKeyEntry *newentries = DK_ENTRIES(mp->ma_keys);
1500
if (oldkeys->dk_nentries == numentries) {
1501
memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1502
}
1503
else {
1504
PyDictKeyEntry *ep = oldentries;
1505
for (Py_ssize_t i = 0; i < numentries; i++) {
1506
while (ep->me_value == NULL)
1507
ep++;
1508
newentries[i] = *ep++;
1509
}
1510
}
1511
build_indices_generic(mp->ma_keys, newentries, numentries);
1512
}
1513
else { // oldkeys is combined unicode
1514
PyDictUnicodeEntry *oldentries = DK_UNICODE_ENTRIES(oldkeys);
1515
if (unicode) { // combined unicode -> combined unicode
1516
PyDictUnicodeEntry *newentries = DK_UNICODE_ENTRIES(mp->ma_keys);
1517
if (oldkeys->dk_nentries == numentries && mp->ma_keys->dk_kind == DICT_KEYS_UNICODE) {
1518
memcpy(newentries, oldentries, numentries * sizeof(PyDictUnicodeEntry));
1519
}
1520
else {
1521
PyDictUnicodeEntry *ep = oldentries;
1522
for (Py_ssize_t i = 0; i < numentries; i++) {
1523
while (ep->me_value == NULL)
1524
ep++;
1525
newentries[i] = *ep++;
1526
}
1527
}
1528
build_indices_unicode(mp->ma_keys, newentries, numentries);
1529
}
1530
else { // combined unicode -> generic
1531
PyDictKeyEntry *newentries = DK_ENTRIES(mp->ma_keys);
1532
PyDictUnicodeEntry *ep = oldentries;
1533
for (Py_ssize_t i = 0; i < numentries; i++) {
1534
while (ep->me_value == NULL)
1535
ep++;
1536
newentries[i].me_key = ep->me_key;
1537
newentries[i].me_hash = unicode_get_hash(ep->me_key);
1538
newentries[i].me_value = ep->me_value;
1539
ep++;
1540
}
1541
build_indices_generic(mp->ma_keys, newentries, numentries);
1542
}
1543
}
1544
1545
// We can not use free_keys_object here because key's reference
1546
// are moved already.
1547
if (oldkeys != Py_EMPTY_KEYS) {
1548
#ifdef Py_REF_DEBUG
1549
_Py_DecRefTotal(_PyInterpreterState_GET());
1550
#endif
1551
assert(oldkeys->dk_kind != DICT_KEYS_SPLIT);
1552
assert(oldkeys->dk_refcnt == 1);
1553
#if PyDict_MAXFREELIST > 0
1554
struct _Py_dict_state *state = get_dict_state(interp);
1555
#ifdef Py_DEBUG
1556
// dictresize() must not be called after _PyDict_Fini()
1557
assert(state->keys_numfree != -1);
1558
#endif
1559
if (DK_LOG_SIZE(oldkeys) == PyDict_LOG_MINSIZE &&
1560
DK_IS_UNICODE(oldkeys) &&
1561
state->keys_numfree < PyDict_MAXFREELIST)
1562
{
1563
state->keys_free_list[state->keys_numfree++] = oldkeys;
1564
OBJECT_STAT_INC(to_freelist);
1565
}
1566
else
1567
#endif
1568
{
1569
PyObject_Free(oldkeys);
1570
}
1571
}
1572
}
1573
1574
mp->ma_keys->dk_usable -= numentries;
1575
mp->ma_keys->dk_nentries = numentries;
1576
ASSERT_CONSISTENT(mp);
1577
return 0;
1578
}
1579
1580
static PyObject *
1581
dict_new_presized(PyInterpreterState *interp, Py_ssize_t minused, bool unicode)
1582
{
1583
const uint8_t log2_max_presize = 17;
1584
const Py_ssize_t max_presize = ((Py_ssize_t)1) << log2_max_presize;
1585
uint8_t log2_newsize;
1586
PyDictKeysObject *new_keys;
1587
1588
if (minused <= USABLE_FRACTION(PyDict_MINSIZE)) {
1589
return PyDict_New();
1590
}
1591
/* There are no strict guarantee that returned dict can contain minused
1592
* items without resize. So we create medium size dict instead of very
1593
* large dict or MemoryError.
1594
*/
1595
if (minused > USABLE_FRACTION(max_presize)) {
1596
log2_newsize = log2_max_presize;
1597
}
1598
else {
1599
log2_newsize = estimate_log2_keysize(minused);
1600
}
1601
1602
new_keys = new_keys_object(interp, log2_newsize, unicode);
1603
if (new_keys == NULL)
1604
return NULL;
1605
return new_dict(interp, new_keys, NULL, 0, 0);
1606
}
1607
1608
PyObject *
1609
_PyDict_NewPresized(Py_ssize_t minused)
1610
{
1611
PyInterpreterState *interp = _PyInterpreterState_GET();
1612
return dict_new_presized(interp, minused, false);
1613
}
1614
1615
PyObject *
1616
_PyDict_FromItems(PyObject *const *keys, Py_ssize_t keys_offset,
1617
PyObject *const *values, Py_ssize_t values_offset,
1618
Py_ssize_t length)
1619
{
1620
bool unicode = true;
1621
PyObject *const *ks = keys;
1622
PyInterpreterState *interp = _PyInterpreterState_GET();
1623
1624
for (Py_ssize_t i = 0; i < length; i++) {
1625
if (!PyUnicode_CheckExact(*ks)) {
1626
unicode = false;
1627
break;
1628
}
1629
ks += keys_offset;
1630
}
1631
1632
PyObject *dict = dict_new_presized(interp, length, unicode);
1633
if (dict == NULL) {
1634
return NULL;
1635
}
1636
1637
ks = keys;
1638
PyObject *const *vs = values;
1639
1640
for (Py_ssize_t i = 0; i < length; i++) {
1641
PyObject *key = *ks;
1642
PyObject *value = *vs;
1643
if (PyDict_SetItem(dict, key, value) < 0) {
1644
Py_DECREF(dict);
1645
return NULL;
1646
}
1647
ks += keys_offset;
1648
vs += values_offset;
1649
}
1650
1651
return dict;
1652
}
1653
1654
/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1655
* that may occur (originally dicts supported only string keys, and exceptions
1656
* weren't possible). So, while the original intent was that a NULL return
1657
* meant the key wasn't present, in reality it can mean that, or that an error
1658
* (suppressed) occurred while computing the key's hash, or that some error
1659
* (suppressed) occurred when comparing keys in the dict's internal probe
1660
* sequence. A nasty example of the latter is when a Python-coded comparison
1661
* function hits a stack-depth error, which can cause this to return NULL
1662
* even if the key is present.
1663
*/
1664
PyObject *
1665
PyDict_GetItem(PyObject *op, PyObject *key)
1666
{
1667
if (!PyDict_Check(op)) {
1668
return NULL;
1669
}
1670
PyDictObject *mp = (PyDictObject *)op;
1671
1672
Py_hash_t hash;
1673
if (!PyUnicode_CheckExact(key) || (hash = unicode_get_hash(key)) == -1) {
1674
hash = PyObject_Hash(key);
1675
if (hash == -1) {
1676
PyErr_Clear();
1677
return NULL;
1678
}
1679
}
1680
1681
PyThreadState *tstate = _PyThreadState_GET();
1682
#ifdef Py_DEBUG
1683
// bpo-40839: Before Python 3.10, it was possible to call PyDict_GetItem()
1684
// with the GIL released.
1685
_Py_EnsureTstateNotNULL(tstate);
1686
#endif
1687
1688
/* Preserve the existing exception */
1689
PyObject *value;
1690
Py_ssize_t ix; (void)ix;
1691
1692
1693
PyObject *exc = _PyErr_GetRaisedException(tstate);
1694
ix = _Py_dict_lookup(mp, key, hash, &value);
1695
1696
/* Ignore any exception raised by the lookup */
1697
_PyErr_SetRaisedException(tstate, exc);
1698
1699
1700
assert(ix >= 0 || value == NULL);
1701
return value;
1702
}
1703
1704
Py_ssize_t
1705
_PyDict_LookupIndex(PyDictObject *mp, PyObject *key)
1706
{
1707
PyObject *value;
1708
assert(PyDict_CheckExact((PyObject*)mp));
1709
assert(PyUnicode_CheckExact(key));
1710
1711
Py_hash_t hash = unicode_get_hash(key);
1712
if (hash == -1) {
1713
hash = PyObject_Hash(key);
1714
if (hash == -1) {
1715
return -1;
1716
}
1717
}
1718
1719
return _Py_dict_lookup(mp, key, hash, &value);
1720
}
1721
1722
/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1723
This returns NULL *with* an exception set if an exception occurred.
1724
It returns NULL *without* an exception set if the key wasn't present.
1725
*/
1726
PyObject *
1727
_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1728
{
1729
Py_ssize_t ix; (void)ix;
1730
PyDictObject *mp = (PyDictObject *)op;
1731
PyObject *value;
1732
1733
if (!PyDict_Check(op)) {
1734
PyErr_BadInternalCall();
1735
return NULL;
1736
}
1737
1738
ix = _Py_dict_lookup(mp, key, hash, &value);
1739
assert(ix >= 0 || value == NULL);
1740
return value;
1741
}
1742
1743
/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1744
This returns NULL *with* an exception set if an exception occurred.
1745
It returns NULL *without* an exception set if the key wasn't present.
1746
*/
1747
PyObject *
1748
PyDict_GetItemWithError(PyObject *op, PyObject *key)
1749
{
1750
Py_ssize_t ix; (void)ix;
1751
Py_hash_t hash;
1752
PyDictObject*mp = (PyDictObject *)op;
1753
PyObject *value;
1754
1755
if (!PyDict_Check(op)) {
1756
PyErr_BadInternalCall();
1757
return NULL;
1758
}
1759
if (!PyUnicode_CheckExact(key) || (hash = unicode_get_hash(key)) == -1)
1760
{
1761
hash = PyObject_Hash(key);
1762
if (hash == -1) {
1763
return NULL;
1764
}
1765
}
1766
1767
ix = _Py_dict_lookup(mp, key, hash, &value);
1768
assert(ix >= 0 || value == NULL);
1769
return value;
1770
}
1771
1772
PyObject *
1773
_PyDict_GetItemWithError(PyObject *dp, PyObject *kv)
1774
{
1775
assert(PyUnicode_CheckExact(kv));
1776
Py_hash_t hash = kv->ob_type->tp_hash(kv);
1777
if (hash == -1) {
1778
return NULL;
1779
}
1780
return _PyDict_GetItem_KnownHash(dp, kv, hash);
1781
}
1782
1783
PyObject *
1784
_PyDict_GetItemIdWithError(PyObject *dp, _Py_Identifier *key)
1785
{
1786
PyObject *kv;
1787
kv = _PyUnicode_FromId(key); /* borrowed */
1788
if (kv == NULL)
1789
return NULL;
1790
Py_hash_t hash = unicode_get_hash(kv);
1791
assert (hash != -1); /* interned strings have their hash value initialised */
1792
return _PyDict_GetItem_KnownHash(dp, kv, hash);
1793
}
1794
1795
PyObject *
1796
_PyDict_GetItemStringWithError(PyObject *v, const char *key)
1797
{
1798
PyObject *kv, *rv;
1799
kv = PyUnicode_FromString(key);
1800
if (kv == NULL) {
1801
return NULL;
1802
}
1803
rv = PyDict_GetItemWithError(v, kv);
1804
Py_DECREF(kv);
1805
return rv;
1806
}
1807
1808
/* Fast version of global value lookup (LOAD_GLOBAL).
1809
* Lookup in globals, then builtins.
1810
*
1811
*
1812
*
1813
*
1814
* Raise an exception and return NULL if an error occurred (ex: computing the
1815
* key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1816
* exist. Return the value if the key exists.
1817
*/
1818
PyObject *
1819
_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
1820
{
1821
Py_ssize_t ix;
1822
Py_hash_t hash;
1823
PyObject *value;
1824
1825
if (!PyUnicode_CheckExact(key) || (hash = unicode_get_hash(key)) == -1) {
1826
hash = PyObject_Hash(key);
1827
if (hash == -1)
1828
return NULL;
1829
}
1830
1831
/* namespace 1: globals */
1832
ix = _Py_dict_lookup(globals, key, hash, &value);
1833
if (ix == DKIX_ERROR)
1834
return NULL;
1835
if (ix != DKIX_EMPTY && value != NULL)
1836
return value;
1837
1838
/* namespace 2: builtins */
1839
ix = _Py_dict_lookup(builtins, key, hash, &value);
1840
assert(ix >= 0 || value == NULL);
1841
return value;
1842
}
1843
1844
/* Consumes references to key and value */
1845
int
1846
_PyDict_SetItem_Take2(PyDictObject *mp, PyObject *key, PyObject *value)
1847
{
1848
assert(key);
1849
assert(value);
1850
assert(PyDict_Check(mp));
1851
Py_hash_t hash;
1852
if (!PyUnicode_CheckExact(key) || (hash = unicode_get_hash(key)) == -1) {
1853
hash = PyObject_Hash(key);
1854
if (hash == -1) {
1855
Py_DECREF(key);
1856
Py_DECREF(value);
1857
return -1;
1858
}
1859
}
1860
PyInterpreterState *interp = _PyInterpreterState_GET();
1861
if (mp->ma_keys == Py_EMPTY_KEYS) {
1862
return insert_to_emptydict(interp, mp, key, hash, value);
1863
}
1864
/* insertdict() handles any resizing that might be necessary */
1865
return insertdict(interp, mp, key, hash, value);
1866
}
1867
1868
/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1869
* dictionary if it's merely replacing the value for an existing key.
1870
* This means that it's safe to loop over a dictionary with PyDict_Next()
1871
* and occasionally replace a value -- but you can't insert new keys or
1872
* remove them.
1873
*/
1874
int
1875
PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
1876
{
1877
if (!PyDict_Check(op)) {
1878
PyErr_BadInternalCall();
1879
return -1;
1880
}
1881
assert(key);
1882
assert(value);
1883
return _PyDict_SetItem_Take2((PyDictObject *)op,
1884
Py_NewRef(key), Py_NewRef(value));
1885
}
1886
1887
int
1888
_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1889
Py_hash_t hash)
1890
{
1891
PyDictObject *mp;
1892
1893
if (!PyDict_Check(op)) {
1894
PyErr_BadInternalCall();
1895
return -1;
1896
}
1897
assert(key);
1898
assert(value);
1899
assert(hash != -1);
1900
mp = (PyDictObject *)op;
1901
1902
PyInterpreterState *interp = _PyInterpreterState_GET();
1903
if (mp->ma_keys == Py_EMPTY_KEYS) {
1904
return insert_to_emptydict(interp, mp, Py_NewRef(key), hash, Py_NewRef(value));
1905
}
1906
/* insertdict() handles any resizing that might be necessary */
1907
return insertdict(interp, mp, Py_NewRef(key), hash, Py_NewRef(value));
1908
}
1909
1910
static void
1911
delete_index_from_values(PyDictValues *values, Py_ssize_t ix)
1912
{
1913
uint8_t *size_ptr = ((uint8_t *)values)-2;
1914
int size = *size_ptr;
1915
int i;
1916
for (i = 1; size_ptr[-i] != ix; i++) {
1917
assert(i <= size);
1918
}
1919
assert(i <= size);
1920
for (; i < size; i++) {
1921
size_ptr[-i] = size_ptr[-i-1];
1922
}
1923
*size_ptr = size -1;
1924
}
1925
1926
static int
1927
delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
1928
PyObject *old_value, uint64_t new_version)
1929
{
1930
PyObject *old_key;
1931
1932
Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1933
assert(hashpos >= 0);
1934
1935
mp->ma_used--;
1936
mp->ma_version_tag = new_version;
1937
if (mp->ma_values) {
1938
assert(old_value == mp->ma_values->values[ix]);
1939
mp->ma_values->values[ix] = NULL;
1940
assert(ix < SHARED_KEYS_MAX_SIZE);
1941
/* Update order */
1942
delete_index_from_values(mp->ma_values, ix);
1943
ASSERT_CONSISTENT(mp);
1944
}
1945
else {
1946
mp->ma_keys->dk_version = 0;
1947
dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1948
if (DK_IS_UNICODE(mp->ma_keys)) {
1949
PyDictUnicodeEntry *ep = &DK_UNICODE_ENTRIES(mp->ma_keys)[ix];
1950
old_key = ep->me_key;
1951
ep->me_key = NULL;
1952
ep->me_value = NULL;
1953
}
1954
else {
1955
PyDictKeyEntry *ep = &DK_ENTRIES(mp->ma_keys)[ix];
1956
old_key = ep->me_key;
1957
ep->me_key = NULL;
1958
ep->me_value = NULL;
1959
ep->me_hash = 0;
1960
}
1961
Py_DECREF(old_key);
1962
}
1963
Py_DECREF(old_value);
1964
1965
ASSERT_CONSISTENT(mp);
1966
return 0;
1967
}
1968
1969
int
1970
PyDict_DelItem(PyObject *op, PyObject *key)
1971
{
1972
Py_hash_t hash;
1973
assert(key);
1974
if (!PyUnicode_CheckExact(key) || (hash = unicode_get_hash(key)) == -1) {
1975
hash = PyObject_Hash(key);
1976
if (hash == -1)
1977
return -1;
1978
}
1979
1980
return _PyDict_DelItem_KnownHash(op, key, hash);
1981
}
1982
1983
int
1984
_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1985
{
1986
Py_ssize_t ix;
1987
PyDictObject *mp;
1988
PyObject *old_value;
1989
1990
if (!PyDict_Check(op)) {
1991
PyErr_BadInternalCall();
1992
return -1;
1993
}
1994
assert(key);
1995
assert(hash != -1);
1996
mp = (PyDictObject *)op;
1997
ix = _Py_dict_lookup(mp, key, hash, &old_value);
1998
if (ix == DKIX_ERROR)
1999
return -1;
2000
if (ix == DKIX_EMPTY || old_value == NULL) {
2001
_PyErr_SetKeyError(key);
2002
return -1;
2003
}
2004
2005
PyInterpreterState *interp = _PyInterpreterState_GET();
2006
uint64_t new_version = _PyDict_NotifyEvent(
2007
interp, PyDict_EVENT_DELETED, mp, key, NULL);
2008
return delitem_common(mp, hash, ix, old_value, new_version);
2009
}
2010
2011
/* This function promises that the predicate -> deletion sequence is atomic
2012
* (i.e. protected by the GIL), assuming the predicate itself doesn't
2013
* release the GIL.
2014
*/
2015
int
2016
_PyDict_DelItemIf(PyObject *op, PyObject *key,
2017
int (*predicate)(PyObject *value))
2018
{
2019
Py_ssize_t hashpos, ix;
2020
PyDictObject *mp;
2021
Py_hash_t hash;
2022
PyObject *old_value;
2023
int res;
2024
2025
if (!PyDict_Check(op)) {
2026
PyErr_BadInternalCall();
2027
return -1;
2028
}
2029
assert(key);
2030
hash = PyObject_Hash(key);
2031
if (hash == -1)
2032
return -1;
2033
mp = (PyDictObject *)op;
2034
ix = _Py_dict_lookup(mp, key, hash, &old_value);
2035
if (ix == DKIX_ERROR)
2036
return -1;
2037
if (ix == DKIX_EMPTY || old_value == NULL) {
2038
_PyErr_SetKeyError(key);
2039
return -1;
2040
}
2041
2042
res = predicate(old_value);
2043
if (res == -1)
2044
return -1;
2045
2046
hashpos = lookdict_index(mp->ma_keys, hash, ix);
2047
assert(hashpos >= 0);
2048
2049
if (res > 0) {
2050
PyInterpreterState *interp = _PyInterpreterState_GET();
2051
uint64_t new_version = _PyDict_NotifyEvent(
2052
interp, PyDict_EVENT_DELETED, mp, key, NULL);
2053
return delitem_common(mp, hashpos, ix, old_value, new_version);
2054
} else {
2055
return 0;
2056
}
2057
}
2058
2059
2060
void
2061
PyDict_Clear(PyObject *op)
2062
{
2063
PyDictObject *mp;
2064
PyDictKeysObject *oldkeys;
2065
PyDictValues *oldvalues;
2066
Py_ssize_t i, n;
2067
2068
if (!PyDict_Check(op))
2069
return;
2070
mp = ((PyDictObject *)op);
2071
oldkeys = mp->ma_keys;
2072
oldvalues = mp->ma_values;
2073
if (oldkeys == Py_EMPTY_KEYS) {
2074
return;
2075
}
2076
/* Empty the dict... */
2077
PyInterpreterState *interp = _PyInterpreterState_GET();
2078
uint64_t new_version = _PyDict_NotifyEvent(
2079
interp, PyDict_EVENT_CLEARED, mp, NULL, NULL);
2080
dictkeys_incref(Py_EMPTY_KEYS);
2081
mp->ma_keys = Py_EMPTY_KEYS;
2082
mp->ma_values = NULL;
2083
mp->ma_used = 0;
2084
mp->ma_version_tag = new_version;
2085
/* ...then clear the keys and values */
2086
if (oldvalues != NULL) {
2087
n = oldkeys->dk_nentries;
2088
for (i = 0; i < n; i++)
2089
Py_CLEAR(oldvalues->values[i]);
2090
free_values(oldvalues);
2091
dictkeys_decref(interp, oldkeys);
2092
}
2093
else {
2094
assert(oldkeys->dk_refcnt == 1);
2095
dictkeys_decref(interp, oldkeys);
2096
}
2097
ASSERT_CONSISTENT(mp);
2098
}
2099
2100
/* Internal version of PyDict_Next that returns a hash value in addition
2101
* to the key and value.
2102
* Return 1 on success, return 0 when the reached the end of the dictionary
2103
* (or if op is not a dictionary)
2104
*/
2105
int
2106
_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
2107
PyObject **pvalue, Py_hash_t *phash)
2108
{
2109
Py_ssize_t i;
2110
PyDictObject *mp;
2111
PyObject *key, *value;
2112
Py_hash_t hash;
2113
2114
if (!PyDict_Check(op))
2115
return 0;
2116
mp = (PyDictObject *)op;
2117
i = *ppos;
2118
if (mp->ma_values) {
2119
assert(mp->ma_used <= SHARED_KEYS_MAX_SIZE);
2120
if (i < 0 || i >= mp->ma_used)
2121
return 0;
2122
int index = get_index_from_order(mp, i);
2123
value = mp->ma_values->values[index];
2124
2125
key = DK_UNICODE_ENTRIES(mp->ma_keys)[index].me_key;
2126
hash = unicode_get_hash(key);
2127
assert(value != NULL);
2128
}
2129
else {
2130
Py_ssize_t n = mp->ma_keys->dk_nentries;
2131
if (i < 0 || i >= n)
2132
return 0;
2133
if (DK_IS_UNICODE(mp->ma_keys)) {
2134
PyDictUnicodeEntry *entry_ptr = &DK_UNICODE_ENTRIES(mp->ma_keys)[i];
2135
while (i < n && entry_ptr->me_value == NULL) {
2136
entry_ptr++;
2137
i++;
2138
}
2139
if (i >= n)
2140
return 0;
2141
key = entry_ptr->me_key;
2142
hash = unicode_get_hash(entry_ptr->me_key);
2143
value = entry_ptr->me_value;
2144
}
2145
else {
2146
PyDictKeyEntry *entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
2147
while (i < n && entry_ptr->me_value == NULL) {
2148
entry_ptr++;
2149
i++;
2150
}
2151
if (i >= n)
2152
return 0;
2153
key = entry_ptr->me_key;
2154
hash = entry_ptr->me_hash;
2155
value = entry_ptr->me_value;
2156
}
2157
}
2158
*ppos = i+1;
2159
if (pkey)
2160
*pkey = key;
2161
if (pvalue)
2162
*pvalue = value;
2163
if (phash)
2164
*phash = hash;
2165
return 1;
2166
}
2167
2168
/*
2169
* Iterate over a dict. Use like so:
2170
*
2171
* Py_ssize_t i;
2172
* PyObject *key, *value;
2173
* i = 0; # important! i should not otherwise be changed by you
2174
* while (PyDict_Next(yourdict, &i, &key, &value)) {
2175
* Refer to borrowed references in key and value.
2176
* }
2177
*
2178
* Return 1 on success, return 0 when the reached the end of the dictionary
2179
* (or if op is not a dictionary)
2180
*
2181
* CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
2182
* mutates the dict. One exception: it is safe if the loop merely changes
2183
* the values associated with the keys (but doesn't insert new keys or
2184
* delete keys), via PyDict_SetItem().
2185
*/
2186
int
2187
PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
2188
{
2189
return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
2190
}
2191
2192
/* Internal version of dict.pop(). */
2193
PyObject *
2194
_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
2195
{
2196
Py_ssize_t ix;
2197
PyObject *old_value;
2198
PyDictObject *mp;
2199
PyInterpreterState *interp = _PyInterpreterState_GET();
2200
2201
assert(PyDict_Check(dict));
2202
mp = (PyDictObject *)dict;
2203
2204
if (mp->ma_used == 0) {
2205
if (deflt) {
2206
return Py_NewRef(deflt);
2207
}
2208
_PyErr_SetKeyError(key);
2209
return NULL;
2210
}
2211
ix = _Py_dict_lookup(mp, key, hash, &old_value);
2212
if (ix == DKIX_ERROR)
2213
return NULL;
2214
if (ix == DKIX_EMPTY || old_value == NULL) {
2215
if (deflt) {
2216
return Py_NewRef(deflt);
2217
}
2218
_PyErr_SetKeyError(key);
2219
return NULL;
2220
}
2221
assert(old_value != NULL);
2222
uint64_t new_version = _PyDict_NotifyEvent(
2223
interp, PyDict_EVENT_DELETED, mp, key, NULL);
2224
delitem_common(mp, hash, ix, Py_NewRef(old_value), new_version);
2225
2226
ASSERT_CONSISTENT(mp);
2227
return old_value;
2228
}
2229
2230
PyObject *
2231
_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
2232
{
2233
Py_hash_t hash;
2234
2235
if (((PyDictObject *)dict)->ma_used == 0) {
2236
if (deflt) {
2237
return Py_NewRef(deflt);
2238
}
2239
_PyErr_SetKeyError(key);
2240
return NULL;
2241
}
2242
if (!PyUnicode_CheckExact(key) || (hash = unicode_get_hash(key)) == -1) {
2243
hash = PyObject_Hash(key);
2244
if (hash == -1)
2245
return NULL;
2246
}
2247
return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
2248
}
2249
2250
/* Internal version of dict.from_keys(). It is subclass-friendly. */
2251
PyObject *
2252
_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
2253
{
2254
PyObject *it; /* iter(iterable) */
2255
PyObject *key;
2256
PyObject *d;
2257
int status;
2258
PyInterpreterState *interp = _PyInterpreterState_GET();
2259
2260
d = _PyObject_CallNoArgs(cls);
2261
if (d == NULL)
2262
return NULL;
2263
2264
if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
2265
if (PyDict_CheckExact(iterable)) {
2266
PyDictObject *mp = (PyDictObject *)d;
2267
PyObject *oldvalue;
2268
Py_ssize_t pos = 0;
2269
PyObject *key;
2270
Py_hash_t hash;
2271
2272
int unicode = DK_IS_UNICODE(((PyDictObject*)iterable)->ma_keys);
2273
if (dictresize(interp, mp,
2274
estimate_log2_keysize(PyDict_GET_SIZE(iterable)),
2275
unicode)) {
2276
Py_DECREF(d);
2277
return NULL;
2278
}
2279
2280
while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
2281
if (insertdict(interp, mp,
2282
Py_NewRef(key), hash, Py_NewRef(value))) {
2283
Py_DECREF(d);
2284
return NULL;
2285
}
2286
}
2287
return d;
2288
}
2289
if (PyAnySet_CheckExact(iterable)) {
2290
PyDictObject *mp = (PyDictObject *)d;
2291
Py_ssize_t pos = 0;
2292
PyObject *key;
2293
Py_hash_t hash;
2294
2295
if (dictresize(interp, mp,
2296
estimate_log2_keysize(PySet_GET_SIZE(iterable)), 0)) {
2297
Py_DECREF(d);
2298
return NULL;
2299
}
2300
2301
while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
2302
if (insertdict(interp, mp, Py_NewRef(key), hash, Py_NewRef(value))) {
2303
Py_DECREF(d);
2304
return NULL;
2305
}
2306
}
2307
return d;
2308
}
2309
}
2310
2311
it = PyObject_GetIter(iterable);
2312
if (it == NULL){
2313
Py_DECREF(d);
2314
return NULL;
2315
}
2316
2317
if (PyDict_CheckExact(d)) {
2318
while ((key = PyIter_Next(it)) != NULL) {
2319
status = PyDict_SetItem(d, key, value);
2320
Py_DECREF(key);
2321
if (status < 0)
2322
goto Fail;
2323
}
2324
} else {
2325
while ((key = PyIter_Next(it)) != NULL) {
2326
status = PyObject_SetItem(d, key, value);
2327
Py_DECREF(key);
2328
if (status < 0)
2329
goto Fail;
2330
}
2331
}
2332
2333
if (PyErr_Occurred())
2334
goto Fail;
2335
Py_DECREF(it);
2336
return d;
2337
2338
Fail:
2339
Py_DECREF(it);
2340
Py_DECREF(d);
2341
return NULL;
2342
}
2343
2344
/* Methods */
2345
2346
static void
2347
dict_dealloc(PyDictObject *mp)
2348
{
2349
PyInterpreterState *interp = _PyInterpreterState_GET();
2350
assert(Py_REFCNT(mp) == 0);
2351
Py_SET_REFCNT(mp, 1);
2352
_PyDict_NotifyEvent(interp, PyDict_EVENT_DEALLOCATED, mp, NULL, NULL);
2353
if (Py_REFCNT(mp) > 1) {
2354
Py_SET_REFCNT(mp, Py_REFCNT(mp) - 1);
2355
return;
2356
}
2357
Py_SET_REFCNT(mp, 0);
2358
PyDictValues *values = mp->ma_values;
2359
PyDictKeysObject *keys = mp->ma_keys;
2360
Py_ssize_t i, n;
2361
2362
/* bpo-31095: UnTrack is needed before calling any callbacks */
2363
PyObject_GC_UnTrack(mp);
2364
Py_TRASHCAN_BEGIN(mp, dict_dealloc)
2365
if (values != NULL) {
2366
for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
2367
Py_XDECREF(values->values[i]);
2368
}
2369
free_values(values);
2370
dictkeys_decref(interp, keys);
2371
}
2372
else if (keys != NULL) {
2373
assert(keys->dk_refcnt == 1 || keys == Py_EMPTY_KEYS);
2374
dictkeys_decref(interp, keys);
2375
}
2376
#if PyDict_MAXFREELIST > 0
2377
struct _Py_dict_state *state = get_dict_state(interp);
2378
#ifdef Py_DEBUG
2379
// new_dict() must not be called after _PyDict_Fini()
2380
assert(state->numfree != -1);
2381
#endif
2382
if (state->numfree < PyDict_MAXFREELIST && Py_IS_TYPE(mp, &PyDict_Type)) {
2383
state->free_list[state->numfree++] = mp;
2384
OBJECT_STAT_INC(to_freelist);
2385
}
2386
else
2387
#endif
2388
{
2389
Py_TYPE(mp)->tp_free((PyObject *)mp);
2390
}
2391
Py_TRASHCAN_END
2392
}
2393
2394
2395
static PyObject *
2396
dict_repr(PyDictObject *mp)
2397
{
2398
Py_ssize_t i;
2399
PyObject *key = NULL, *value = NULL;
2400
_PyUnicodeWriter writer;
2401
int first;
2402
2403
i = Py_ReprEnter((PyObject *)mp);
2404
if (i != 0) {
2405
return i > 0 ? PyUnicode_FromString("{...}") : NULL;
2406
}
2407
2408
if (mp->ma_used == 0) {
2409
Py_ReprLeave((PyObject *)mp);
2410
return PyUnicode_FromString("{}");
2411
}
2412
2413
_PyUnicodeWriter_Init(&writer);
2414
writer.overallocate = 1;
2415
/* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
2416
writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
2417
2418
if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
2419
goto error;
2420
2421
/* Do repr() on each key+value pair, and insert ": " between them.
2422
Note that repr may mutate the dict. */
2423
i = 0;
2424
first = 1;
2425
while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
2426
PyObject *s;
2427
int res;
2428
2429
/* Prevent repr from deleting key or value during key format. */
2430
Py_INCREF(key);
2431
Py_INCREF(value);
2432
2433
if (!first) {
2434
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
2435
goto error;
2436
}
2437
first = 0;
2438
2439
s = PyObject_Repr(key);
2440
if (s == NULL)
2441
goto error;
2442
res = _PyUnicodeWriter_WriteStr(&writer, s);
2443
Py_DECREF(s);
2444
if (res < 0)
2445
goto error;
2446
2447
if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2448
goto error;
2449
2450
s = PyObject_Repr(value);
2451
if (s == NULL)
2452
goto error;
2453
res = _PyUnicodeWriter_WriteStr(&writer, s);
2454
Py_DECREF(s);
2455
if (res < 0)
2456
goto error;
2457
2458
Py_CLEAR(key);
2459
Py_CLEAR(value);
2460
}
2461
2462
writer.overallocate = 0;
2463
if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2464
goto error;
2465
2466
Py_ReprLeave((PyObject *)mp);
2467
2468
return _PyUnicodeWriter_Finish(&writer);
2469
2470
error:
2471
Py_ReprLeave((PyObject *)mp);
2472
_PyUnicodeWriter_Dealloc(&writer);
2473
Py_XDECREF(key);
2474
Py_XDECREF(value);
2475
return NULL;
2476
}
2477
2478
static Py_ssize_t
2479
dict_length(PyDictObject *mp)
2480
{
2481
return mp->ma_used;
2482
}
2483
2484
static PyObject *
2485
dict_subscript(PyDictObject *mp, PyObject *key)
2486
{
2487
Py_ssize_t ix;
2488
Py_hash_t hash;
2489
PyObject *value;
2490
2491
if (!PyUnicode_CheckExact(key) || (hash = unicode_get_hash(key)) == -1) {
2492
hash = PyObject_Hash(key);
2493
if (hash == -1)
2494
return NULL;
2495
}
2496
ix = _Py_dict_lookup(mp, key, hash, &value);
2497
if (ix == DKIX_ERROR)
2498
return NULL;
2499
if (ix == DKIX_EMPTY || value == NULL) {
2500
if (!PyDict_CheckExact(mp)) {
2501
/* Look up __missing__ method if we're a subclass. */
2502
PyObject *missing, *res;
2503
missing = _PyObject_LookupSpecial(
2504
(PyObject *)mp, &_Py_ID(__missing__));
2505
if (missing != NULL) {
2506
res = PyObject_CallOneArg(missing, key);
2507
Py_DECREF(missing);
2508
return res;
2509
}
2510
else if (PyErr_Occurred())
2511
return NULL;
2512
}
2513
_PyErr_SetKeyError(key);
2514
return NULL;
2515
}
2516
return Py_NewRef(value);
2517
}
2518
2519
static int
2520
dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
2521
{
2522
if (w == NULL)
2523
return PyDict_DelItem((PyObject *)mp, v);
2524
else
2525
return PyDict_SetItem((PyObject *)mp, v, w);
2526
}
2527
2528
static PyMappingMethods dict_as_mapping = {
2529
(lenfunc)dict_length, /*mp_length*/
2530
(binaryfunc)dict_subscript, /*mp_subscript*/
2531
(objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
2532
};
2533
2534
static PyObject *
2535
dict_keys(PyDictObject *mp)
2536
{
2537
PyObject *v;
2538
Py_ssize_t n;
2539
2540
again:
2541
n = mp->ma_used;
2542
v = PyList_New(n);
2543
if (v == NULL)
2544
return NULL;
2545
if (n != mp->ma_used) {
2546
/* Durnit. The allocations caused the dict to resize.
2547
* Just start over, this shouldn't normally happen.
2548
*/
2549
Py_DECREF(v);
2550
goto again;
2551
}
2552
2553
/* Nothing we do below makes any function calls. */
2554
Py_ssize_t j = 0, pos = 0;
2555
PyObject *key;
2556
while (_PyDict_Next((PyObject*)mp, &pos, &key, NULL, NULL)) {
2557
assert(j < n);
2558
PyList_SET_ITEM(v, j, Py_NewRef(key));
2559
j++;
2560
}
2561
assert(j == n);
2562
return v;
2563
}
2564
2565
static PyObject *
2566
dict_values(PyDictObject *mp)
2567
{
2568
PyObject *v;
2569
Py_ssize_t n;
2570
2571
again:
2572
n = mp->ma_used;
2573
v = PyList_New(n);
2574
if (v == NULL)
2575
return NULL;
2576
if (n != mp->ma_used) {
2577
/* Durnit. The allocations caused the dict to resize.
2578
* Just start over, this shouldn't normally happen.
2579
*/
2580
Py_DECREF(v);
2581
goto again;
2582
}
2583
2584
/* Nothing we do below makes any function calls. */
2585
Py_ssize_t j = 0, pos = 0;
2586
PyObject *value;
2587
while (_PyDict_Next((PyObject*)mp, &pos, NULL, &value, NULL)) {
2588
assert(j < n);
2589
PyList_SET_ITEM(v, j, Py_NewRef(value));
2590
j++;
2591
}
2592
assert(j == n);
2593
return v;
2594
}
2595
2596
static PyObject *
2597
dict_items(PyDictObject *mp)
2598
{
2599
PyObject *v;
2600
Py_ssize_t i, n;
2601
PyObject *item;
2602
2603
/* Preallocate the list of tuples, to avoid allocations during
2604
* the loop over the items, which could trigger GC, which
2605
* could resize the dict. :-(
2606
*/
2607
again:
2608
n = mp->ma_used;
2609
v = PyList_New(n);
2610
if (v == NULL)
2611
return NULL;
2612
for (i = 0; i < n; i++) {
2613
item = PyTuple_New(2);
2614
if (item == NULL) {
2615
Py_DECREF(v);
2616
return NULL;
2617
}
2618
PyList_SET_ITEM(v, i, item);
2619
}
2620
if (n != mp->ma_used) {
2621
/* Durnit. The allocations caused the dict to resize.
2622
* Just start over, this shouldn't normally happen.
2623
*/
2624
Py_DECREF(v);
2625
goto again;
2626
}
2627
2628
/* Nothing we do below makes any function calls. */
2629
Py_ssize_t j = 0, pos = 0;
2630
PyObject *key, *value;
2631
while (_PyDict_Next((PyObject*)mp, &pos, &key, &value, NULL)) {
2632
assert(j < n);
2633
PyObject *item = PyList_GET_ITEM(v, j);
2634
PyTuple_SET_ITEM(item, 0, Py_NewRef(key));
2635
PyTuple_SET_ITEM(item, 1, Py_NewRef(value));
2636
j++;
2637
}
2638
assert(j == n);
2639
return v;
2640
}
2641
2642
/*[clinic input]
2643
@classmethod
2644
dict.fromkeys
2645
iterable: object
2646
value: object=None
2647
/
2648
2649
Create a new dictionary with keys from iterable and values set to value.
2650
[clinic start generated code]*/
2651
2652
static PyObject *
2653
dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
2654
/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
2655
{
2656
return _PyDict_FromKeys((PyObject *)type, iterable, value);
2657
}
2658
2659
/* Single-arg dict update; used by dict_update_common and operators. */
2660
static int
2661
dict_update_arg(PyObject *self, PyObject *arg)
2662
{
2663
if (PyDict_CheckExact(arg)) {
2664
return PyDict_Merge(self, arg, 1);
2665
}
2666
PyObject *func;
2667
if (_PyObject_LookupAttr(arg, &_Py_ID(keys), &func) < 0) {
2668
return -1;
2669
}
2670
if (func != NULL) {
2671
Py_DECREF(func);
2672
return PyDict_Merge(self, arg, 1);
2673
}
2674
return PyDict_MergeFromSeq2(self, arg, 1);
2675
}
2676
2677
static int
2678
dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2679
const char *methname)
2680
{
2681
PyObject *arg = NULL;
2682
int result = 0;
2683
2684
if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
2685
result = -1;
2686
}
2687
else if (arg != NULL) {
2688
result = dict_update_arg(self, arg);
2689
}
2690
2691
if (result == 0 && kwds != NULL) {
2692
if (PyArg_ValidateKeywordArguments(kwds))
2693
result = PyDict_Merge(self, kwds, 1);
2694
else
2695
result = -1;
2696
}
2697
return result;
2698
}
2699
2700
/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
2701
Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2702
slower, see the issue #29312. */
2703
static PyObject *
2704
dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2705
{
2706
if (dict_update_common(self, args, kwds, "update") != -1)
2707
Py_RETURN_NONE;
2708
return NULL;
2709
}
2710
2711
/* Update unconditionally replaces existing items.
2712
Merge has a 3rd argument 'override'; if set, it acts like Update,
2713
otherwise it leaves existing items unchanged.
2714
2715
PyDict_{Update,Merge} update/merge from a mapping object.
2716
2717
PyDict_MergeFromSeq2 updates/merges from any iterable object
2718
producing iterable objects of length 2.
2719
*/
2720
2721
int
2722
PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2723
{
2724
PyObject *it; /* iter(seq2) */
2725
Py_ssize_t i; /* index into seq2 of current element */
2726
PyObject *item; /* seq2[i] */
2727
PyObject *fast; /* item as a 2-tuple or 2-list */
2728
2729
assert(d != NULL);
2730
assert(PyDict_Check(d));
2731
assert(seq2 != NULL);
2732
2733
it = PyObject_GetIter(seq2);
2734
if (it == NULL)
2735
return -1;
2736
2737
for (i = 0; ; ++i) {
2738
PyObject *key, *value;
2739
Py_ssize_t n;
2740
2741
fast = NULL;
2742
item = PyIter_Next(it);
2743
if (item == NULL) {
2744
if (PyErr_Occurred())
2745
goto Fail;
2746
break;
2747
}
2748
2749
/* Convert item to sequence, and verify length 2. */
2750
fast = PySequence_Fast(item, "");
2751
if (fast == NULL) {
2752
if (PyErr_ExceptionMatches(PyExc_TypeError))
2753
PyErr_Format(PyExc_TypeError,
2754
"cannot convert dictionary update "
2755
"sequence element #%zd to a sequence",
2756
i);
2757
goto Fail;
2758
}
2759
n = PySequence_Fast_GET_SIZE(fast);
2760
if (n != 2) {
2761
PyErr_Format(PyExc_ValueError,
2762
"dictionary update sequence element #%zd "
2763
"has length %zd; 2 is required",
2764
i, n);
2765
goto Fail;
2766
}
2767
2768
/* Update/merge with this (key, value) pair. */
2769
key = PySequence_Fast_GET_ITEM(fast, 0);
2770
value = PySequence_Fast_GET_ITEM(fast, 1);
2771
Py_INCREF(key);
2772
Py_INCREF(value);
2773
if (override) {
2774
if (PyDict_SetItem(d, key, value) < 0) {
2775
Py_DECREF(key);
2776
Py_DECREF(value);
2777
goto Fail;
2778
}
2779
}
2780
else {
2781
if (PyDict_SetDefault(d, key, value) == NULL) {
2782
Py_DECREF(key);
2783
Py_DECREF(value);
2784
goto Fail;
2785
}
2786
}
2787
2788
Py_DECREF(key);
2789
Py_DECREF(value);
2790
Py_DECREF(fast);
2791
Py_DECREF(item);
2792
}
2793
2794
i = 0;
2795
ASSERT_CONSISTENT(d);
2796
goto Return;
2797
Fail:
2798
Py_XDECREF(item);
2799
Py_XDECREF(fast);
2800
i = -1;
2801
Return:
2802
Py_DECREF(it);
2803
return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
2804
}
2805
2806
static int
2807
dict_merge(PyInterpreterState *interp, PyObject *a, PyObject *b, int override)
2808
{
2809
PyDictObject *mp, *other;
2810
2811
assert(0 <= override && override <= 2);
2812
2813
/* We accept for the argument either a concrete dictionary object,
2814
* or an abstract "mapping" object. For the former, we can do
2815
* things quite efficiently. For the latter, we only require that
2816
* PyMapping_Keys() and PyObject_GetItem() be supported.
2817
*/
2818
if (a == NULL || !PyDict_Check(a) || b == NULL) {
2819
PyErr_BadInternalCall();
2820
return -1;
2821
}
2822
mp = (PyDictObject*)a;
2823
if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
2824
other = (PyDictObject*)b;
2825
if (other == mp || other->ma_used == 0)
2826
/* a.update(a) or a.update({}); nothing to do */
2827
return 0;
2828
if (mp->ma_used == 0) {
2829
/* Since the target dict is empty, PyDict_GetItem()
2830
* always returns NULL. Setting override to 1
2831
* skips the unnecessary test.
2832
*/
2833
override = 1;
2834
PyDictKeysObject *okeys = other->ma_keys;
2835
2836
// If other is clean, combined, and just allocated, just clone it.
2837
if (other->ma_values == NULL &&
2838
other->ma_used == okeys->dk_nentries &&
2839
(DK_LOG_SIZE(okeys) == PyDict_LOG_MINSIZE ||
2840
USABLE_FRACTION(DK_SIZE(okeys)/2) < other->ma_used)) {
2841
uint64_t new_version = _PyDict_NotifyEvent(
2842
interp, PyDict_EVENT_CLONED, mp, b, NULL);
2843
PyDictKeysObject *keys = clone_combined_dict_keys(other);
2844
if (keys == NULL) {
2845
return -1;
2846
}
2847
2848
dictkeys_decref(interp, mp->ma_keys);
2849
mp->ma_keys = keys;
2850
if (mp->ma_values != NULL) {
2851
free_values(mp->ma_values);
2852
mp->ma_values = NULL;
2853
}
2854
2855
mp->ma_used = other->ma_used;
2856
mp->ma_version_tag = new_version;
2857
ASSERT_CONSISTENT(mp);
2858
2859
if (_PyObject_GC_IS_TRACKED(other) && !_PyObject_GC_IS_TRACKED(mp)) {
2860
/* Maintain tracking. */
2861
_PyObject_GC_TRACK(mp);
2862
}
2863
2864
return 0;
2865
}
2866
}
2867
/* Do one big resize at the start, rather than
2868
* incrementally resizing as we insert new items. Expect
2869
* that there will be no (or few) overlapping keys.
2870
*/
2871
if (USABLE_FRACTION(DK_SIZE(mp->ma_keys)) < other->ma_used) {
2872
int unicode = DK_IS_UNICODE(other->ma_keys);
2873
if (dictresize(interp, mp,
2874
estimate_log2_keysize(mp->ma_used + other->ma_used),
2875
unicode)) {
2876
return -1;
2877
}
2878
}
2879
2880
Py_ssize_t orig_size = other->ma_keys->dk_nentries;
2881
Py_ssize_t pos = 0;
2882
Py_hash_t hash;
2883
PyObject *key, *value;
2884
2885
while (_PyDict_Next((PyObject*)other, &pos, &key, &value, &hash)) {
2886
int err = 0;
2887
Py_INCREF(key);
2888
Py_INCREF(value);
2889
if (override == 1) {
2890
err = insertdict(interp, mp,
2891
Py_NewRef(key), hash, Py_NewRef(value));
2892
}
2893
else {
2894
err = _PyDict_Contains_KnownHash(a, key, hash);
2895
if (err == 0) {
2896
err = insertdict(interp, mp,
2897
Py_NewRef(key), hash, Py_NewRef(value));
2898
}
2899
else if (err > 0) {
2900
if (override != 0) {
2901
_PyErr_SetKeyError(key);
2902
Py_DECREF(value);
2903
Py_DECREF(key);
2904
return -1;
2905
}
2906
err = 0;
2907
}
2908
}
2909
Py_DECREF(value);
2910
Py_DECREF(key);
2911
if (err != 0)
2912
return -1;
2913
2914
if (orig_size != other->ma_keys->dk_nentries) {
2915
PyErr_SetString(PyExc_RuntimeError,
2916
"dict mutated during update");
2917
return -1;
2918
}
2919
}
2920
}
2921
else {
2922
/* Do it the generic, slower way */
2923
PyObject *keys = PyMapping_Keys(b);
2924
PyObject *iter;
2925
PyObject *key, *value;
2926
int status;
2927
2928
if (keys == NULL)
2929
/* Docstring says this is equivalent to E.keys() so
2930
* if E doesn't have a .keys() method we want
2931
* AttributeError to percolate up. Might as well
2932
* do the same for any other error.
2933
*/
2934
return -1;
2935
2936
iter = PyObject_GetIter(keys);
2937
Py_DECREF(keys);
2938
if (iter == NULL)
2939
return -1;
2940
2941
for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
2942
if (override != 1) {
2943
status = PyDict_Contains(a, key);
2944
if (status != 0) {
2945
if (status > 0) {
2946
if (override == 0) {
2947
Py_DECREF(key);
2948
continue;
2949
}
2950
_PyErr_SetKeyError(key);
2951
}
2952
Py_DECREF(key);
2953
Py_DECREF(iter);
2954
return -1;
2955
}
2956
}
2957
value = PyObject_GetItem(b, key);
2958
if (value == NULL) {
2959
Py_DECREF(iter);
2960
Py_DECREF(key);
2961
return -1;
2962
}
2963
status = PyDict_SetItem(a, key, value);
2964
Py_DECREF(key);
2965
Py_DECREF(value);
2966
if (status < 0) {
2967
Py_DECREF(iter);
2968
return -1;
2969
}
2970
}
2971
Py_DECREF(iter);
2972
if (PyErr_Occurred())
2973
/* Iterator completed, via error */
2974
return -1;
2975
}
2976
ASSERT_CONSISTENT(a);
2977
return 0;
2978
}
2979
2980
int
2981
PyDict_Update(PyObject *a, PyObject *b)
2982
{
2983
PyInterpreterState *interp = _PyInterpreterState_GET();
2984
return dict_merge(interp, a, b, 1);
2985
}
2986
2987
int
2988
PyDict_Merge(PyObject *a, PyObject *b, int override)
2989
{
2990
PyInterpreterState *interp = _PyInterpreterState_GET();
2991
/* XXX Deprecate override not in (0, 1). */
2992
return dict_merge(interp, a, b, override != 0);
2993
}
2994
2995
int
2996
_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2997
{
2998
PyInterpreterState *interp = _PyInterpreterState_GET();
2999
return dict_merge(interp, a, b, override);
3000
}
3001
3002
static PyObject *
3003
dict_copy(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
3004
{
3005
return PyDict_Copy((PyObject*)mp);
3006
}
3007
3008
PyObject *
3009
PyDict_Copy(PyObject *o)
3010
{
3011
PyObject *copy;
3012
PyDictObject *mp;
3013
PyInterpreterState *interp = _PyInterpreterState_GET();
3014
3015
if (o == NULL || !PyDict_Check(o)) {
3016
PyErr_BadInternalCall();
3017
return NULL;
3018
}
3019
3020
mp = (PyDictObject *)o;
3021
if (mp->ma_used == 0) {
3022
/* The dict is empty; just return a new dict. */
3023
return PyDict_New();
3024
}
3025
3026
if (_PyDict_HasSplitTable(mp)) {
3027
PyDictObject *split_copy;
3028
size_t size = shared_keys_usable_size(mp->ma_keys);
3029
PyDictValues *newvalues = new_values(size);
3030
if (newvalues == NULL)
3031
return PyErr_NoMemory();
3032
split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
3033
if (split_copy == NULL) {
3034
free_values(newvalues);
3035
return NULL;
3036
}
3037
size_t prefix_size = ((uint8_t *)newvalues)[-1];
3038
memcpy(((char *)newvalues)-prefix_size, ((char *)mp->ma_values)-prefix_size, prefix_size-1);
3039
split_copy->ma_values = newvalues;
3040
split_copy->ma_keys = mp->ma_keys;
3041
split_copy->ma_used = mp->ma_used;
3042
split_copy->ma_version_tag = DICT_NEXT_VERSION(interp);
3043
dictkeys_incref(mp->ma_keys);
3044
for (size_t i = 0; i < size; i++) {
3045
PyObject *value = mp->ma_values->values[i];
3046
split_copy->ma_values->values[i] = Py_XNewRef(value);
3047
}
3048
if (_PyObject_GC_IS_TRACKED(mp))
3049
_PyObject_GC_TRACK(split_copy);
3050
return (PyObject *)split_copy;
3051
}
3052
3053
if (Py_TYPE(mp)->tp_iter == (getiterfunc)dict_iter &&
3054
mp->ma_values == NULL &&
3055
(mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
3056
{
3057
/* Use fast-copy if:
3058
3059
(1) type(mp) doesn't override tp_iter; and
3060
3061
(2) 'mp' is not a split-dict; and
3062
3063
(3) if 'mp' is non-compact ('del' operation does not resize dicts),
3064
do fast-copy only if it has at most 1/3 non-used keys.
3065
3066
The last condition (3) is important to guard against a pathological
3067
case when a large dict is almost emptied with multiple del/pop
3068
operations and copied after that. In cases like this, we defer to
3069
PyDict_Merge, which produces a compacted copy.
3070
*/
3071
PyDictKeysObject *keys = clone_combined_dict_keys(mp);
3072
if (keys == NULL) {
3073
return NULL;
3074
}
3075
PyDictObject *new = (PyDictObject *)new_dict(interp, keys, NULL, 0, 0);
3076
if (new == NULL) {
3077
/* In case of an error, `new_dict()` takes care of
3078
cleaning up `keys`. */
3079
return NULL;
3080
}
3081
3082
new->ma_used = mp->ma_used;
3083
ASSERT_CONSISTENT(new);
3084
if (_PyObject_GC_IS_TRACKED(mp)) {
3085
/* Maintain tracking. */
3086
_PyObject_GC_TRACK(new);
3087
}
3088
3089
return (PyObject *)new;
3090
}
3091
3092
copy = PyDict_New();
3093
if (copy == NULL)
3094
return NULL;
3095
if (dict_merge(interp, copy, o, 1) == 0)
3096
return copy;
3097
Py_DECREF(copy);
3098
return NULL;
3099
}
3100
3101
Py_ssize_t
3102
PyDict_Size(PyObject *mp)
3103
{
3104
if (mp == NULL || !PyDict_Check(mp)) {
3105
PyErr_BadInternalCall();
3106
return -1;
3107
}
3108
return ((PyDictObject *)mp)->ma_used;
3109
}
3110
3111
PyObject *
3112
PyDict_Keys(PyObject *mp)
3113
{
3114
if (mp == NULL || !PyDict_Check(mp)) {
3115
PyErr_BadInternalCall();
3116
return NULL;
3117
}
3118
return dict_keys((PyDictObject *)mp);
3119
}
3120
3121
PyObject *
3122
PyDict_Values(PyObject *mp)
3123
{
3124
if (mp == NULL || !PyDict_Check(mp)) {
3125
PyErr_BadInternalCall();
3126
return NULL;
3127
}
3128
return dict_values((PyDictObject *)mp);
3129
}
3130
3131
PyObject *
3132
PyDict_Items(PyObject *mp)
3133
{
3134
if (mp == NULL || !PyDict_Check(mp)) {
3135
PyErr_BadInternalCall();
3136
return NULL;
3137
}
3138
return dict_items((PyDictObject *)mp);
3139
}
3140
3141
/* Return 1 if dicts equal, 0 if not, -1 if error.
3142
* Gets out as soon as any difference is detected.
3143
* Uses only Py_EQ comparison.
3144
*/
3145
static int
3146
dict_equal(PyDictObject *a, PyDictObject *b)
3147
{
3148
Py_ssize_t i;
3149
3150
if (a->ma_used != b->ma_used)
3151
/* can't be equal if # of entries differ */
3152
return 0;
3153
/* Same # of entries -- check all of 'em. Exit early on any diff. */
3154
for (i = 0; i < a->ma_keys->dk_nentries; i++) {
3155
PyObject *key, *aval;
3156
Py_hash_t hash;
3157
if (DK_IS_UNICODE(a->ma_keys)) {
3158
PyDictUnicodeEntry *ep = &DK_UNICODE_ENTRIES(a->ma_keys)[i];
3159
key = ep->me_key;
3160
if (key == NULL) {
3161
continue;
3162
}
3163
hash = unicode_get_hash(key);
3164
if (a->ma_values)
3165
aval = a->ma_values->values[i];
3166
else
3167
aval = ep->me_value;
3168
}
3169
else {
3170
PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
3171
key = ep->me_key;
3172
aval = ep->me_value;
3173
hash = ep->me_hash;
3174
}
3175
if (aval != NULL) {
3176
int cmp;
3177
PyObject *bval;
3178
/* temporarily bump aval's refcount to ensure it stays
3179
alive until we're done with it */
3180
Py_INCREF(aval);
3181
/* ditto for key */
3182
Py_INCREF(key);
3183
/* reuse the known hash value */
3184
_Py_dict_lookup(b, key, hash, &bval);
3185
if (bval == NULL) {
3186
Py_DECREF(key);
3187
Py_DECREF(aval);
3188
if (PyErr_Occurred())
3189
return -1;
3190
return 0;
3191
}
3192
Py_INCREF(bval);
3193
cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
3194
Py_DECREF(key);
3195
Py_DECREF(aval);
3196
Py_DECREF(bval);
3197
if (cmp <= 0) /* error or not equal */
3198
return cmp;
3199
}
3200
}
3201
return 1;
3202
}
3203
3204
static PyObject *
3205
dict_richcompare(PyObject *v, PyObject *w, int op)
3206
{
3207
int cmp;
3208
PyObject *res;
3209
3210
if (!PyDict_Check(v) || !PyDict_Check(w)) {
3211
res = Py_NotImplemented;
3212
}
3213
else if (op == Py_EQ || op == Py_NE) {
3214
cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
3215
if (cmp < 0)
3216
return NULL;
3217
res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
3218
}
3219
else
3220
res = Py_NotImplemented;
3221
return Py_NewRef(res);
3222
}
3223
3224
/*[clinic input]
3225
3226
@coexist
3227
dict.__contains__
3228
3229
key: object
3230
/
3231
3232
True if the dictionary has the specified key, else False.
3233
[clinic start generated code]*/
3234
3235
static PyObject *
3236
dict___contains__(PyDictObject *self, PyObject *key)
3237
/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
3238
{
3239
register PyDictObject *mp = self;
3240
Py_hash_t hash;
3241
Py_ssize_t ix;
3242
PyObject *value;
3243
3244
if (!PyUnicode_CheckExact(key) || (hash = unicode_get_hash(key)) == -1) {
3245
hash = PyObject_Hash(key);
3246
if (hash == -1)
3247
return NULL;
3248
}
3249
ix = _Py_dict_lookup(mp, key, hash, &value);
3250
if (ix == DKIX_ERROR)
3251
return NULL;
3252
if (ix == DKIX_EMPTY || value == NULL)
3253
Py_RETURN_FALSE;
3254
Py_RETURN_TRUE;
3255
}
3256
3257
/*[clinic input]
3258
dict.get
3259
3260
key: object
3261
default: object = None
3262
/
3263
3264
Return the value for key if key is in the dictionary, else default.
3265
[clinic start generated code]*/
3266
3267
static PyObject *
3268
dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
3269
/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
3270
{
3271
PyObject *val = NULL;
3272
Py_hash_t hash;
3273
Py_ssize_t ix;
3274
3275
if (!PyUnicode_CheckExact(key) || (hash = unicode_get_hash(key)) == -1) {
3276
hash = PyObject_Hash(key);
3277
if (hash == -1)
3278
return NULL;
3279
}
3280
ix = _Py_dict_lookup(self, key, hash, &val);
3281
if (ix == DKIX_ERROR)
3282
return NULL;
3283
if (ix == DKIX_EMPTY || val == NULL) {
3284
val = default_value;
3285
}
3286
return Py_NewRef(val);
3287
}
3288
3289
PyObject *
3290
PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
3291
{
3292
PyDictObject *mp = (PyDictObject *)d;
3293
PyObject *value;
3294
Py_hash_t hash;
3295
PyInterpreterState *interp = _PyInterpreterState_GET();
3296
3297
if (!PyDict_Check(d)) {
3298
PyErr_BadInternalCall();
3299
return NULL;
3300
}
3301
3302
if (!PyUnicode_CheckExact(key) || (hash = unicode_get_hash(key)) == -1) {
3303
hash = PyObject_Hash(key);
3304
if (hash == -1)
3305
return NULL;
3306
}
3307
3308
if (mp->ma_keys == Py_EMPTY_KEYS) {
3309
if (insert_to_emptydict(interp, mp, Py_NewRef(key), hash,
3310
Py_NewRef(defaultobj)) < 0) {
3311
return NULL;
3312
}
3313
return defaultobj;
3314
}
3315
3316
if (!PyUnicode_CheckExact(key) && DK_IS_UNICODE(mp->ma_keys)) {
3317
if (insertion_resize(interp, mp, 0) < 0) {
3318
return NULL;
3319
}
3320
}
3321
3322
Py_ssize_t ix = _Py_dict_lookup(mp, key, hash, &value);
3323
if (ix == DKIX_ERROR)
3324
return NULL;
3325
3326
if (ix == DKIX_EMPTY) {
3327
uint64_t new_version = _PyDict_NotifyEvent(
3328
interp, PyDict_EVENT_ADDED, mp, key, defaultobj);
3329
mp->ma_keys->dk_version = 0;
3330
value = defaultobj;
3331
if (mp->ma_keys->dk_usable <= 0) {
3332
if (insertion_resize(interp, mp, 1) < 0) {
3333
return NULL;
3334
}
3335
}
3336
Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
3337
dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
3338
if (DK_IS_UNICODE(mp->ma_keys)) {
3339
assert(PyUnicode_CheckExact(key));
3340
PyDictUnicodeEntry *ep = &DK_UNICODE_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
3341
ep->me_key = Py_NewRef(key);
3342
if (_PyDict_HasSplitTable(mp)) {
3343
Py_ssize_t index = (int)mp->ma_keys->dk_nentries;
3344
assert(index < SHARED_KEYS_MAX_SIZE);
3345
assert(mp->ma_values->values[index] == NULL);
3346
mp->ma_values->values[index] = Py_NewRef(value);
3347
_PyDictValues_AddToInsertionOrder(mp->ma_values, index);
3348
}
3349
else {
3350
ep->me_value = Py_NewRef(value);
3351
}
3352
}
3353
else {
3354
PyDictKeyEntry *ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
3355
ep->me_key = Py_NewRef(key);
3356
ep->me_hash = hash;
3357
ep->me_value = Py_NewRef(value);
3358
}
3359
MAINTAIN_TRACKING(mp, key, value);
3360
mp->ma_used++;
3361
mp->ma_version_tag = new_version;
3362
mp->ma_keys->dk_usable--;
3363
mp->ma_keys->dk_nentries++;
3364
assert(mp->ma_keys->dk_usable >= 0);
3365
}
3366
else if (value == NULL) {
3367
uint64_t new_version = _PyDict_NotifyEvent(
3368
interp, PyDict_EVENT_ADDED, mp, key, defaultobj);
3369
value = defaultobj;
3370
assert(_PyDict_HasSplitTable(mp));
3371
assert(mp->ma_values->values[ix] == NULL);
3372
MAINTAIN_TRACKING(mp, key, value);
3373
mp->ma_values->values[ix] = Py_NewRef(value);
3374
_PyDictValues_AddToInsertionOrder(mp->ma_values, ix);
3375
mp->ma_used++;
3376
mp->ma_version_tag = new_version;
3377
}
3378
3379
ASSERT_CONSISTENT(mp);
3380
return value;
3381
}
3382
3383
/*[clinic input]
3384
dict.setdefault
3385
3386
key: object
3387
default: object = None
3388
/
3389
3390
Insert key with a value of default if key is not in the dictionary.
3391
3392
Return the value for key if key is in the dictionary, else default.
3393
[clinic start generated code]*/
3394
3395
static PyObject *
3396
dict_setdefault_impl(PyDictObject *self, PyObject *key,
3397
PyObject *default_value)
3398
/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
3399
{
3400
PyObject *val;
3401
3402
val = PyDict_SetDefault((PyObject *)self, key, default_value);
3403
return Py_XNewRef(val);
3404
}
3405
3406
static PyObject *
3407
dict_clear(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
3408
{
3409
PyDict_Clear((PyObject *)mp);
3410
Py_RETURN_NONE;
3411
}
3412
3413
/*[clinic input]
3414
dict.pop
3415
3416
key: object
3417
default: object = NULL
3418
/
3419
3420
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
3421
3422
If the key is not found, return the default if given; otherwise,
3423
raise a KeyError.
3424
[clinic start generated code]*/
3425
3426
static PyObject *
3427
dict_pop_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
3428
/*[clinic end generated code: output=3abb47b89f24c21c input=e221baa01044c44c]*/
3429
{
3430
return _PyDict_Pop((PyObject*)self, key, default_value);
3431
}
3432
3433
/*[clinic input]
3434
dict.popitem
3435
3436
Remove and return a (key, value) pair as a 2-tuple.
3437
3438
Pairs are returned in LIFO (last-in, first-out) order.
3439
Raises KeyError if the dict is empty.
3440
[clinic start generated code]*/
3441
3442
static PyObject *
3443
dict_popitem_impl(PyDictObject *self)
3444
/*[clinic end generated code: output=e65fcb04420d230d input=1c38a49f21f64941]*/
3445
{
3446
Py_ssize_t i, j;
3447
PyObject *res;
3448
uint64_t new_version;
3449
PyInterpreterState *interp = _PyInterpreterState_GET();
3450
3451
/* Allocate the result tuple before checking the size. Believe it
3452
* or not, this allocation could trigger a garbage collection which
3453
* could empty the dict, so if we checked the size first and that
3454
* happened, the result would be an infinite loop (searching for an
3455
* entry that no longer exists). Note that the usual popitem()
3456
* idiom is "while d: k, v = d.popitem()". so needing to throw the
3457
* tuple away if the dict *is* empty isn't a significant
3458
* inefficiency -- possible, but unlikely in practice.
3459
*/
3460
res = PyTuple_New(2);
3461
if (res == NULL)
3462
return NULL;
3463
if (self->ma_used == 0) {
3464
Py_DECREF(res);
3465
PyErr_SetString(PyExc_KeyError, "popitem(): dictionary is empty");
3466
return NULL;
3467
}
3468
/* Convert split table to combined table */
3469
if (self->ma_keys->dk_kind == DICT_KEYS_SPLIT) {
3470
if (dictresize(interp, self, DK_LOG_SIZE(self->ma_keys), 1)) {
3471
Py_DECREF(res);
3472
return NULL;
3473
}
3474
}
3475
self->ma_keys->dk_version = 0;
3476
3477
/* Pop last item */
3478
PyObject *key, *value;
3479
Py_hash_t hash;
3480
if (DK_IS_UNICODE(self->ma_keys)) {
3481
PyDictUnicodeEntry *ep0 = DK_UNICODE_ENTRIES(self->ma_keys);
3482
i = self->ma_keys->dk_nentries - 1;
3483
while (i >= 0 && ep0[i].me_value == NULL) {
3484
i--;
3485
}
3486
assert(i >= 0);
3487
3488
key = ep0[i].me_key;
3489
new_version = _PyDict_NotifyEvent(
3490
interp, PyDict_EVENT_DELETED, self, key, NULL);
3491
hash = unicode_get_hash(key);
3492
value = ep0[i].me_value;
3493
ep0[i].me_key = NULL;
3494
ep0[i].me_value = NULL;
3495
}
3496
else {
3497
PyDictKeyEntry *ep0 = DK_ENTRIES(self->ma_keys);
3498
i = self->ma_keys->dk_nentries - 1;
3499
while (i >= 0 && ep0[i].me_value == NULL) {
3500
i--;
3501
}
3502
assert(i >= 0);
3503
3504
key = ep0[i].me_key;
3505
new_version = _PyDict_NotifyEvent(
3506
interp, PyDict_EVENT_DELETED, self, key, NULL);
3507
hash = ep0[i].me_hash;
3508
value = ep0[i].me_value;
3509
ep0[i].me_key = NULL;
3510
ep0[i].me_hash = -1;
3511
ep0[i].me_value = NULL;
3512
}
3513
3514
j = lookdict_index(self->ma_keys, hash, i);
3515
assert(j >= 0);
3516
assert(dictkeys_get_index(self->ma_keys, j) == i);
3517
dictkeys_set_index(self->ma_keys, j, DKIX_DUMMY);
3518
3519
PyTuple_SET_ITEM(res, 0, key);
3520
PyTuple_SET_ITEM(res, 1, value);
3521
/* We can't dk_usable++ since there is DKIX_DUMMY in indices */
3522
self->ma_keys->dk_nentries = i;
3523
self->ma_used--;
3524
self->ma_version_tag = new_version;
3525
ASSERT_CONSISTENT(self);
3526
return res;
3527
}
3528
3529
static int
3530
dict_traverse(PyObject *op, visitproc visit, void *arg)
3531
{
3532
PyDictObject *mp = (PyDictObject *)op;
3533
PyDictKeysObject *keys = mp->ma_keys;
3534
Py_ssize_t i, n = keys->dk_nentries;
3535
3536
if (DK_IS_UNICODE(keys)) {
3537
if (mp->ma_values != NULL) {
3538
for (i = 0; i < n; i++) {
3539
Py_VISIT(mp->ma_values->values[i]);
3540
}
3541
}
3542
else {
3543
PyDictUnicodeEntry *entries = DK_UNICODE_ENTRIES(keys);
3544
for (i = 0; i < n; i++) {
3545
Py_VISIT(entries[i].me_value);
3546
}
3547
}
3548
}
3549
else {
3550
PyDictKeyEntry *entries = DK_ENTRIES(keys);
3551
for (i = 0; i < n; i++) {
3552
if (entries[i].me_value != NULL) {
3553
Py_VISIT(entries[i].me_value);
3554
Py_VISIT(entries[i].me_key);
3555
}
3556
}
3557
}
3558
return 0;
3559
}
3560
3561
static int
3562
dict_tp_clear(PyObject *op)
3563
{
3564
PyDict_Clear(op);
3565
return 0;
3566
}
3567
3568
static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
3569
3570
Py_ssize_t
3571
_PyDict_SizeOf(PyDictObject *mp)
3572
{
3573
size_t res = _PyObject_SIZE(Py_TYPE(mp));
3574
if (mp->ma_values) {
3575
res += shared_keys_usable_size(mp->ma_keys) * sizeof(PyObject*);
3576
}
3577
/* If the dictionary is split, the keys portion is accounted-for
3578
in the type object. */
3579
if (mp->ma_keys->dk_refcnt == 1) {
3580
res += _PyDict_KeysSize(mp->ma_keys);
3581
}
3582
assert(res <= (size_t)PY_SSIZE_T_MAX);
3583
return (Py_ssize_t)res;
3584
}
3585
3586
size_t
3587
_PyDict_KeysSize(PyDictKeysObject *keys)
3588
{
3589
size_t es = (keys->dk_kind == DICT_KEYS_GENERAL
3590
? sizeof(PyDictKeyEntry) : sizeof(PyDictUnicodeEntry));
3591
size_t size = sizeof(PyDictKeysObject);
3592
size += (size_t)1 << keys->dk_log2_index_bytes;
3593
size += USABLE_FRACTION((size_t)DK_SIZE(keys)) * es;
3594
return size;
3595
}
3596
3597
static PyObject *
3598
dict_sizeof(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
3599
{
3600
return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3601
}
3602
3603
static PyObject *
3604
dict_or(PyObject *self, PyObject *other)
3605
{
3606
if (!PyDict_Check(self) || !PyDict_Check(other)) {
3607
Py_RETURN_NOTIMPLEMENTED;
3608
}
3609
PyObject *new = PyDict_Copy(self);
3610
if (new == NULL) {
3611
return NULL;
3612
}
3613
if (dict_update_arg(new, other)) {
3614
Py_DECREF(new);
3615
return NULL;
3616
}
3617
return new;
3618
}
3619
3620
static PyObject *
3621
dict_ior(PyObject *self, PyObject *other)
3622
{
3623
if (dict_update_arg(self, other)) {
3624
return NULL;
3625
}
3626
return Py_NewRef(self);
3627
}
3628
3629
PyDoc_STRVAR(getitem__doc__,
3630
"__getitem__($self, key, /)\n--\n\nReturn self[key].");
3631
3632
PyDoc_STRVAR(sizeof__doc__,
3633
"D.__sizeof__() -> size of D in memory, in bytes");
3634
3635
PyDoc_STRVAR(update__doc__,
3636
"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3637
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3638
If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3639
In either case, this is followed by: for k in F: D[k] = F[k]");
3640
3641
PyDoc_STRVAR(clear__doc__,
3642
"D.clear() -> None. Remove all items from D.");
3643
3644
PyDoc_STRVAR(copy__doc__,
3645
"D.copy() -> a shallow copy of D");
3646
3647
/* Forward */
3648
static PyObject *dictkeys_new(PyObject *, PyObject *);
3649
static PyObject *dictitems_new(PyObject *, PyObject *);
3650
static PyObject *dictvalues_new(PyObject *, PyObject *);
3651
3652
PyDoc_STRVAR(keys__doc__,
3653
"D.keys() -> a set-like object providing a view on D's keys");
3654
PyDoc_STRVAR(items__doc__,
3655
"D.items() -> a set-like object providing a view on D's items");
3656
PyDoc_STRVAR(values__doc__,
3657
"D.values() -> an object providing a view on D's values");
3658
3659
static PyMethodDef mapp_methods[] = {
3660
DICT___CONTAINS___METHODDEF
3661
{"__getitem__", _PyCFunction_CAST(dict_subscript), METH_O | METH_COEXIST,
3662
getitem__doc__},
3663
{"__sizeof__", _PyCFunction_CAST(dict_sizeof), METH_NOARGS,
3664
sizeof__doc__},
3665
DICT_GET_METHODDEF
3666
DICT_SETDEFAULT_METHODDEF
3667
DICT_POP_METHODDEF
3668
DICT_POPITEM_METHODDEF
3669
{"keys", dictkeys_new, METH_NOARGS,
3670
keys__doc__},
3671
{"items", dictitems_new, METH_NOARGS,
3672
items__doc__},
3673
{"values", dictvalues_new, METH_NOARGS,
3674
values__doc__},
3675
{"update", _PyCFunction_CAST(dict_update), METH_VARARGS | METH_KEYWORDS,
3676
update__doc__},
3677
DICT_FROMKEYS_METHODDEF
3678
{"clear", (PyCFunction)dict_clear, METH_NOARGS,
3679
clear__doc__},
3680
{"copy", (PyCFunction)dict_copy, METH_NOARGS,
3681
copy__doc__},
3682
DICT___REVERSED___METHODDEF
3683
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
3684
{NULL, NULL} /* sentinel */
3685
};
3686
3687
/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
3688
int
3689
PyDict_Contains(PyObject *op, PyObject *key)
3690
{
3691
Py_hash_t hash;
3692
Py_ssize_t ix;
3693
PyDictObject *mp = (PyDictObject *)op;
3694
PyObject *value;
3695
3696
if (!PyUnicode_CheckExact(key) || (hash = unicode_get_hash(key)) == -1) {
3697
hash = PyObject_Hash(key);
3698
if (hash == -1)
3699
return -1;
3700
}
3701
ix = _Py_dict_lookup(mp, key, hash, &value);
3702
if (ix == DKIX_ERROR)
3703
return -1;
3704
return (ix != DKIX_EMPTY && value != NULL);
3705
}
3706
3707
/* Internal version of PyDict_Contains used when the hash value is already known */
3708
int
3709
_PyDict_Contains_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
3710
{
3711
PyDictObject *mp = (PyDictObject *)op;
3712
PyObject *value;
3713
Py_ssize_t ix;
3714
3715
ix = _Py_dict_lookup(mp, key, hash, &value);
3716
if (ix == DKIX_ERROR)
3717
return -1;
3718
return (ix != DKIX_EMPTY && value != NULL);
3719
}
3720
3721
int
3722
_PyDict_ContainsId(PyObject *op, _Py_Identifier *key)
3723
{
3724
PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3725
if (kv == NULL) {
3726
return -1;
3727
}
3728
return PyDict_Contains(op, kv);
3729
}
3730
3731
/* Hack to implement "key in dict" */
3732
static PySequenceMethods dict_as_sequence = {
3733
0, /* sq_length */
3734
0, /* sq_concat */
3735
0, /* sq_repeat */
3736
0, /* sq_item */
3737
0, /* sq_slice */
3738
0, /* sq_ass_item */
3739
0, /* sq_ass_slice */
3740
PyDict_Contains, /* sq_contains */
3741
0, /* sq_inplace_concat */
3742
0, /* sq_inplace_repeat */
3743
};
3744
3745
static PyNumberMethods dict_as_number = {
3746
.nb_or = dict_or,
3747
.nb_inplace_or = dict_ior,
3748
};
3749
3750
static PyObject *
3751
dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3752
{
3753
assert(type != NULL);
3754
assert(type->tp_alloc != NULL);
3755
// dict subclasses must implement the GC protocol
3756
assert(_PyType_IS_GC(type));
3757
3758
PyObject *self = type->tp_alloc(type, 0);
3759
if (self == NULL) {
3760
return NULL;
3761
}
3762
PyDictObject *d = (PyDictObject *)self;
3763
3764
d->ma_used = 0;
3765
d->ma_version_tag = DICT_NEXT_VERSION(
3766
_PyInterpreterState_GET());
3767
dictkeys_incref(Py_EMPTY_KEYS);
3768
d->ma_keys = Py_EMPTY_KEYS;
3769
d->ma_values = NULL;
3770
ASSERT_CONSISTENT(d);
3771
3772
if (type != &PyDict_Type) {
3773
// Don't track if a subclass tp_alloc is PyType_GenericAlloc()
3774
if (!_PyObject_GC_IS_TRACKED(d)) {
3775
_PyObject_GC_TRACK(d);
3776
}
3777
}
3778
else {
3779
// _PyType_AllocNoTrack() does not track the created object
3780
assert(!_PyObject_GC_IS_TRACKED(d));
3781
}
3782
return self;
3783
}
3784
3785
static int
3786
dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3787
{
3788
return dict_update_common(self, args, kwds, "dict");
3789
}
3790
3791
static PyObject *
3792
dict_vectorcall(PyObject *type, PyObject * const*args,
3793
size_t nargsf, PyObject *kwnames)
3794
{
3795
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
3796
if (!_PyArg_CheckPositional("dict", nargs, 0, 1)) {
3797
return NULL;
3798
}
3799
3800
PyObject *self = dict_new(_PyType_CAST(type), NULL, NULL);
3801
if (self == NULL) {
3802
return NULL;
3803
}
3804
if (nargs == 1) {
3805
if (dict_update_arg(self, args[0]) < 0) {
3806
Py_DECREF(self);
3807
return NULL;
3808
}
3809
args++;
3810
}
3811
if (kwnames != NULL) {
3812
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwnames); i++) {
3813
if (PyDict_SetItem(self, PyTuple_GET_ITEM(kwnames, i), args[i]) < 0) {
3814
Py_DECREF(self);
3815
return NULL;
3816
}
3817
}
3818
}
3819
return self;
3820
}
3821
3822
static PyObject *
3823
dict_iter(PyDictObject *dict)
3824
{
3825
return dictiter_new(dict, &PyDictIterKey_Type);
3826
}
3827
3828
PyDoc_STRVAR(dictionary_doc,
3829
"dict() -> new empty dictionary\n"
3830
"dict(mapping) -> new dictionary initialized from a mapping object's\n"
3831
" (key, value) pairs\n"
3832
"dict(iterable) -> new dictionary initialized as if via:\n"
3833
" d = {}\n"
3834
" for k, v in iterable:\n"
3835
" d[k] = v\n"
3836
"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3837
" in the keyword argument list. For example: dict(one=1, two=2)");
3838
3839
PyTypeObject PyDict_Type = {
3840
PyVarObject_HEAD_INIT(&PyType_Type, 0)
3841
"dict",
3842
sizeof(PyDictObject),
3843
0,
3844
(destructor)dict_dealloc, /* tp_dealloc */
3845
0, /* tp_vectorcall_offset */
3846
0, /* tp_getattr */
3847
0, /* tp_setattr */
3848
0, /* tp_as_async */
3849
(reprfunc)dict_repr, /* tp_repr */
3850
&dict_as_number, /* tp_as_number */
3851
&dict_as_sequence, /* tp_as_sequence */
3852
&dict_as_mapping, /* tp_as_mapping */
3853
PyObject_HashNotImplemented, /* tp_hash */
3854
0, /* tp_call */
3855
0, /* tp_str */
3856
PyObject_GenericGetAttr, /* tp_getattro */
3857
0, /* tp_setattro */
3858
0, /* tp_as_buffer */
3859
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3860
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS |
3861
_Py_TPFLAGS_MATCH_SELF | Py_TPFLAGS_MAPPING, /* tp_flags */
3862
dictionary_doc, /* tp_doc */
3863
dict_traverse, /* tp_traverse */
3864
dict_tp_clear, /* tp_clear */
3865
dict_richcompare, /* tp_richcompare */
3866
0, /* tp_weaklistoffset */
3867
(getiterfunc)dict_iter, /* tp_iter */
3868
0, /* tp_iternext */
3869
mapp_methods, /* tp_methods */
3870
0, /* tp_members */
3871
0, /* tp_getset */
3872
0, /* tp_base */
3873
0, /* tp_dict */
3874
0, /* tp_descr_get */
3875
0, /* tp_descr_set */
3876
0, /* tp_dictoffset */
3877
dict_init, /* tp_init */
3878
_PyType_AllocNoTrack, /* tp_alloc */
3879
dict_new, /* tp_new */
3880
PyObject_GC_Del, /* tp_free */
3881
.tp_vectorcall = dict_vectorcall,
3882
};
3883
3884
/* For backward compatibility with old dictionary interface */
3885
3886
PyObject *
3887
PyDict_GetItemString(PyObject *v, const char *key)
3888
{
3889
PyObject *kv, *rv;
3890
kv = PyUnicode_FromString(key);
3891
if (kv == NULL) {
3892
PyErr_Clear();
3893
return NULL;
3894
}
3895
rv = PyDict_GetItem(v, kv);
3896
Py_DECREF(kv);
3897
return rv;
3898
}
3899
3900
int
3901
_PyDict_SetItemId(PyObject *v, _Py_Identifier *key, PyObject *item)
3902
{
3903
PyObject *kv;
3904
kv = _PyUnicode_FromId(key); /* borrowed */
3905
if (kv == NULL)
3906
return -1;
3907
return PyDict_SetItem(v, kv, item);
3908
}
3909
3910
int
3911
PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
3912
{
3913
PyObject *kv;
3914
int err;
3915
kv = PyUnicode_FromString(key);
3916
if (kv == NULL)
3917
return -1;
3918
PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3919
err = PyDict_SetItem(v, kv, item);
3920
Py_DECREF(kv);
3921
return err;
3922
}
3923
3924
int
3925
_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3926
{
3927
PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3928
if (kv == NULL)
3929
return -1;
3930
return PyDict_DelItem(v, kv);
3931
}
3932
3933
int
3934
PyDict_DelItemString(PyObject *v, const char *key)
3935
{
3936
PyObject *kv;
3937
int err;
3938
kv = PyUnicode_FromString(key);
3939
if (kv == NULL)
3940
return -1;
3941
err = PyDict_DelItem(v, kv);
3942
Py_DECREF(kv);
3943
return err;
3944
}
3945
3946
/* Dictionary iterator types */
3947
3948
typedef struct {
3949
PyObject_HEAD
3950
PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3951
Py_ssize_t di_used;
3952
Py_ssize_t di_pos;
3953
PyObject* di_result; /* reusable result tuple for iteritems */
3954
Py_ssize_t len;
3955
} dictiterobject;
3956
3957
static PyObject *
3958
dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
3959
{
3960
dictiterobject *di;
3961
di = PyObject_GC_New(dictiterobject, itertype);
3962
if (di == NULL) {
3963
return NULL;
3964
}
3965
di->di_dict = (PyDictObject*)Py_NewRef(dict);
3966
di->di_used = dict->ma_used;
3967
di->len = dict->ma_used;
3968
if (itertype == &PyDictRevIterKey_Type ||
3969
itertype == &PyDictRevIterItem_Type ||
3970
itertype == &PyDictRevIterValue_Type) {
3971
if (dict->ma_values) {
3972
di->di_pos = dict->ma_used - 1;
3973
}
3974
else {
3975
di->di_pos = dict->ma_keys->dk_nentries - 1;
3976
}
3977
}
3978
else {
3979
di->di_pos = 0;
3980
}
3981
if (itertype == &PyDictIterItem_Type ||
3982
itertype == &PyDictRevIterItem_Type) {
3983
di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3984
if (di->di_result == NULL) {
3985
Py_DECREF(di);
3986
return NULL;
3987
}
3988
}
3989
else {
3990
di->di_result = NULL;
3991
}
3992
_PyObject_GC_TRACK(di);
3993
return (PyObject *)di;
3994
}
3995
3996
static void
3997
dictiter_dealloc(dictiterobject *di)
3998
{
3999
/* bpo-31095: UnTrack is needed before calling any callbacks */
4000
_PyObject_GC_UNTRACK(di);
4001
Py_XDECREF(di->di_dict);
4002
Py_XDECREF(di->di_result);
4003
PyObject_GC_Del(di);
4004
}
4005
4006
static int
4007
dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
4008
{
4009
Py_VISIT(di->di_dict);
4010
Py_VISIT(di->di_result);
4011
return 0;
4012
}
4013
4014
static PyObject *
4015
dictiter_len(dictiterobject *di, PyObject *Py_UNUSED(ignored))
4016
{
4017
Py_ssize_t len = 0;
4018
if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
4019
len = di->len;
4020
return PyLong_FromSize_t(len);
4021
}
4022
4023
PyDoc_STRVAR(length_hint_doc,
4024
"Private method returning an estimate of len(list(it)).");
4025
4026
static PyObject *
4027
dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored));
4028
4029
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
4030
4031
static PyMethodDef dictiter_methods[] = {
4032
{"__length_hint__", _PyCFunction_CAST(dictiter_len), METH_NOARGS,
4033
length_hint_doc},
4034
{"__reduce__", _PyCFunction_CAST(dictiter_reduce), METH_NOARGS,
4035
reduce_doc},
4036
{NULL, NULL} /* sentinel */
4037
};
4038
4039
static PyObject*
4040
dictiter_iternextkey(dictiterobject *di)
4041
{
4042
PyObject *key;
4043
Py_ssize_t i;
4044
PyDictKeysObject *k;
4045
PyDictObject *d = di->di_dict;
4046
4047
if (d == NULL)
4048
return NULL;
4049
assert (PyDict_Check(d));
4050
4051
if (di->di_used != d->ma_used) {
4052
PyErr_SetString(PyExc_RuntimeError,
4053
"dictionary changed size during iteration");
4054
di->di_used = -1; /* Make this state sticky */
4055
return NULL;
4056
}
4057
4058
i = di->di_pos;
4059
k = d->ma_keys;
4060
assert(i >= 0);
4061
if (d->ma_values) {
4062
if (i >= d->ma_used)
4063
goto fail;
4064
int index = get_index_from_order(d, i);
4065
key = DK_UNICODE_ENTRIES(k)[index].me_key;
4066
assert(d->ma_values->values[index] != NULL);
4067
}
4068
else {
4069
Py_ssize_t n = k->dk_nentries;
4070
if (DK_IS_UNICODE(k)) {
4071
PyDictUnicodeEntry *entry_ptr = &DK_UNICODE_ENTRIES(k)[i];
4072
while (i < n && entry_ptr->me_value == NULL) {
4073
entry_ptr++;
4074
i++;
4075
}
4076
if (i >= n)
4077
goto fail;
4078
key = entry_ptr->me_key;
4079
}
4080
else {
4081
PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
4082
while (i < n && entry_ptr->me_value == NULL) {
4083
entry_ptr++;
4084
i++;
4085
}
4086
if (i >= n)
4087
goto fail;
4088
key = entry_ptr->me_key;
4089
}
4090
}
4091
// We found an element (key), but did not expect it
4092
if (di->len == 0) {
4093
PyErr_SetString(PyExc_RuntimeError,
4094
"dictionary keys changed during iteration");
4095
goto fail;
4096
}
4097
di->di_pos = i+1;
4098
di->len--;
4099
return Py_NewRef(key);
4100
4101
fail:
4102
di->di_dict = NULL;
4103
Py_DECREF(d);
4104
return NULL;
4105
}
4106
4107
PyTypeObject PyDictIterKey_Type = {
4108
PyVarObject_HEAD_INIT(&PyType_Type, 0)
4109
"dict_keyiterator", /* tp_name */
4110
sizeof(dictiterobject), /* tp_basicsize */
4111
0, /* tp_itemsize */
4112
/* methods */
4113
(destructor)dictiter_dealloc, /* tp_dealloc */
4114
0, /* tp_vectorcall_offset */
4115
0, /* tp_getattr */
4116
0, /* tp_setattr */
4117
0, /* tp_as_async */
4118
0, /* tp_repr */
4119
0, /* tp_as_number */
4120
0, /* tp_as_sequence */
4121
0, /* tp_as_mapping */
4122
0, /* tp_hash */
4123
0, /* tp_call */
4124
0, /* tp_str */
4125
PyObject_GenericGetAttr, /* tp_getattro */
4126
0, /* tp_setattro */
4127
0, /* tp_as_buffer */
4128
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4129
0, /* tp_doc */
4130
(traverseproc)dictiter_traverse, /* tp_traverse */
4131
0, /* tp_clear */
4132
0, /* tp_richcompare */
4133
0, /* tp_weaklistoffset */
4134
PyObject_SelfIter, /* tp_iter */
4135
(iternextfunc)dictiter_iternextkey, /* tp_iternext */
4136
dictiter_methods, /* tp_methods */
4137
0,
4138
};
4139
4140
static PyObject *
4141
dictiter_iternextvalue(dictiterobject *di)
4142
{
4143
PyObject *value;
4144
Py_ssize_t i;
4145
PyDictObject *d = di->di_dict;
4146
4147
if (d == NULL)
4148
return NULL;
4149
assert (PyDict_Check(d));
4150
4151
if (di->di_used != d->ma_used) {
4152
PyErr_SetString(PyExc_RuntimeError,
4153
"dictionary changed size during iteration");
4154
di->di_used = -1; /* Make this state sticky */
4155
return NULL;
4156
}
4157
4158
i = di->di_pos;
4159
assert(i >= 0);
4160
if (d->ma_values) {
4161
if (i >= d->ma_used)
4162
goto fail;
4163
int index = get_index_from_order(d, i);
4164
value = d->ma_values->values[index];
4165
assert(value != NULL);
4166
}
4167
else {
4168
Py_ssize_t n = d->ma_keys->dk_nentries;
4169
if (DK_IS_UNICODE(d->ma_keys)) {
4170
PyDictUnicodeEntry *entry_ptr = &DK_UNICODE_ENTRIES(d->ma_keys)[i];
4171
while (i < n && entry_ptr->me_value == NULL) {
4172
entry_ptr++;
4173
i++;
4174
}
4175
if (i >= n)
4176
goto fail;
4177
value = entry_ptr->me_value;
4178
}
4179
else {
4180
PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
4181
while (i < n && entry_ptr->me_value == NULL) {
4182
entry_ptr++;
4183
i++;
4184
}
4185
if (i >= n)
4186
goto fail;
4187
value = entry_ptr->me_value;
4188
}
4189
}
4190
// We found an element, but did not expect it
4191
if (di->len == 0) {
4192
PyErr_SetString(PyExc_RuntimeError,
4193
"dictionary keys changed during iteration");
4194
goto fail;
4195
}
4196
di->di_pos = i+1;
4197
di->len--;
4198
return Py_NewRef(value);
4199
4200
fail:
4201
di->di_dict = NULL;
4202
Py_DECREF(d);
4203
return NULL;
4204
}
4205
4206
PyTypeObject PyDictIterValue_Type = {
4207
PyVarObject_HEAD_INIT(&PyType_Type, 0)
4208
"dict_valueiterator", /* tp_name */
4209
sizeof(dictiterobject), /* tp_basicsize */
4210
0, /* tp_itemsize */
4211
/* methods */
4212
(destructor)dictiter_dealloc, /* tp_dealloc */
4213
0, /* tp_vectorcall_offset */
4214
0, /* tp_getattr */
4215
0, /* tp_setattr */
4216
0, /* tp_as_async */
4217
0, /* tp_repr */
4218
0, /* tp_as_number */
4219
0, /* tp_as_sequence */
4220
0, /* tp_as_mapping */
4221
0, /* tp_hash */
4222
0, /* tp_call */
4223
0, /* tp_str */
4224
PyObject_GenericGetAttr, /* tp_getattro */
4225
0, /* tp_setattro */
4226
0, /* tp_as_buffer */
4227
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
4228
0, /* tp_doc */
4229
(traverseproc)dictiter_traverse, /* tp_traverse */
4230
0, /* tp_clear */
4231
0, /* tp_richcompare */
4232
0, /* tp_weaklistoffset */
4233
PyObject_SelfIter, /* tp_iter */
4234
(iternextfunc)dictiter_iternextvalue, /* tp_iternext */
4235
dictiter_methods, /* tp_methods */
4236
0,
4237
};
4238
4239
static PyObject *
4240
dictiter_iternextitem(dictiterobject *di)
4241
{
4242
PyObject *key, *value, *result;
4243
Py_ssize_t i;
4244
PyDictObject *d = di->di_dict;
4245
4246
if (d == NULL)
4247
return NULL;
4248
assert (PyDict_Check(d));
4249
4250
if (di->di_used != d->ma_used) {
4251
PyErr_SetString(PyExc_RuntimeError,
4252
"dictionary changed size during iteration");
4253
di->di_used = -1; /* Make this state sticky */
4254
return NULL;
4255
}
4256
4257
i = di->di_pos;
4258
assert(i >= 0);
4259
if (d->ma_values) {
4260
if (i >= d->ma_used)
4261
goto fail;
4262
int index = get_index_from_order(d, i);
4263
key = DK_UNICODE_ENTRIES(d->ma_keys)[index].me_key;
4264
value = d->ma_values->values[index];
4265
assert(value != NULL);
4266
}
4267
else {
4268
Py_ssize_t n = d->ma_keys->dk_nentries;
4269
if (DK_IS_UNICODE(d->ma_keys)) {
4270
PyDictUnicodeEntry *entry_ptr = &DK_UNICODE_ENTRIES(d->ma_keys)[i];
4271
while (i < n && entry_ptr->me_value == NULL) {
4272
entry_ptr++;
4273
i++;
4274
}
4275
if (i >= n)
4276
goto fail;
4277
key = entry_ptr->me_key;
4278
value = entry_ptr->me_value;
4279
}
4280
else {
4281
PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
4282
while (i < n && entry_ptr->me_value == NULL) {
4283
entry_ptr++;
4284
i++;
4285
}
4286
if (i >= n)
4287
goto fail;
4288
key = entry_ptr->me_key;
4289
value = entry_ptr->me_value;
4290
}
4291
}
4292
// We found an element, but did not expect it
4293
if (di->len == 0) {
4294
PyErr_SetString(PyExc_RuntimeError,
4295
"dictionary keys changed during iteration");
4296
goto fail;
4297
}
4298
di->di_pos = i+1;
4299
di->len--;
4300
result = di->di_result;
4301
if (Py_REFCNT(result) == 1) {
4302
PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
4303
PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
4304
PyTuple_SET_ITEM(result, 0, Py_NewRef(key));
4305
PyTuple_SET_ITEM(result, 1, Py_NewRef(value));
4306
Py_INCREF(result);
4307
Py_DECREF(oldkey);
4308
Py_DECREF(oldvalue);
4309
// bpo-42536: The GC may have untracked this result tuple. Since we're
4310
// recycling it, make sure it's tracked again:
4311
if (!_PyObject_GC_IS_TRACKED(result)) {
4312
_PyObject_GC_TRACK(result);
4313
}
4314
}
4315
else {
4316
result = PyTuple_New(2);
4317
if (result == NULL)
4318
return NULL;
4319
PyTuple_SET_ITEM(result, 0, Py_NewRef(key));
4320
PyTuple_SET_ITEM(result, 1, Py_NewRef(value));
4321
}
4322
return result;
4323
4324
fail:
4325
di->di_dict = NULL;
4326
Py_DECREF(d);
4327
return NULL;
4328
}
4329
4330
PyTypeObject PyDictIterItem_Type = {
4331
PyVarObject_HEAD_INIT(&PyType_Type, 0)
4332
"dict_itemiterator", /* tp_name */
4333
sizeof(dictiterobject), /* tp_basicsize */
4334
0, /* tp_itemsize */
4335
/* methods */
4336
(destructor)dictiter_dealloc, /* tp_dealloc */
4337
0, /* tp_vectorcall_offset */
4338
0, /* tp_getattr */
4339
0, /* tp_setattr */
4340
0, /* tp_as_async */
4341
0, /* tp_repr */
4342
0, /* tp_as_number */
4343
0, /* tp_as_sequence */
4344
0, /* tp_as_mapping */
4345
0, /* tp_hash */
4346
0, /* tp_call */
4347
0, /* tp_str */
4348
PyObject_GenericGetAttr, /* tp_getattro */
4349
0, /* tp_setattro */
4350
0, /* tp_as_buffer */
4351
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4352
0, /* tp_doc */
4353
(traverseproc)dictiter_traverse, /* tp_traverse */
4354
0, /* tp_clear */
4355
0, /* tp_richcompare */
4356
0, /* tp_weaklistoffset */
4357
PyObject_SelfIter, /* tp_iter */
4358
(iternextfunc)dictiter_iternextitem, /* tp_iternext */
4359
dictiter_methods, /* tp_methods */
4360
0,
4361
};
4362
4363
4364
/* dictreviter */
4365
4366
static PyObject *
4367
dictreviter_iternext(dictiterobject *di)
4368
{
4369
PyDictObject *d = di->di_dict;
4370
4371
if (d == NULL) {
4372
return NULL;
4373
}
4374
assert (PyDict_Check(d));
4375
4376
if (di->di_used != d->ma_used) {
4377
PyErr_SetString(PyExc_RuntimeError,
4378
"dictionary changed size during iteration");
4379
di->di_used = -1; /* Make this state sticky */
4380
return NULL;
4381
}
4382
4383
Py_ssize_t i = di->di_pos;
4384
PyDictKeysObject *k = d->ma_keys;
4385
PyObject *key, *value, *result;
4386
4387
if (i < 0) {
4388
goto fail;
4389
}
4390
if (d->ma_values) {
4391
int index = get_index_from_order(d, i);
4392
key = DK_UNICODE_ENTRIES(k)[index].me_key;
4393
value = d->ma_values->values[index];
4394
assert (value != NULL);
4395
}
4396
else {
4397
if (DK_IS_UNICODE(k)) {
4398
PyDictUnicodeEntry *entry_ptr = &DK_UNICODE_ENTRIES(k)[i];
4399
while (entry_ptr->me_value == NULL) {
4400
if (--i < 0) {
4401
goto fail;
4402
}
4403
entry_ptr--;
4404
}
4405
key = entry_ptr->me_key;
4406
value = entry_ptr->me_value;
4407
}
4408
else {
4409
PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
4410
while (entry_ptr->me_value == NULL) {
4411
if (--i < 0) {
4412
goto fail;
4413
}
4414
entry_ptr--;
4415
}
4416
key = entry_ptr->me_key;
4417
value = entry_ptr->me_value;
4418
}
4419
}
4420
di->di_pos = i-1;
4421
di->len--;
4422
4423
if (Py_IS_TYPE(di, &PyDictRevIterKey_Type)) {
4424
return Py_NewRef(key);
4425
}
4426
else if (Py_IS_TYPE(di, &PyDictRevIterValue_Type)) {
4427
return Py_NewRef(value);
4428
}
4429
else if (Py_IS_TYPE(di, &PyDictRevIterItem_Type)) {
4430
result = di->di_result;
4431
if (Py_REFCNT(result) == 1) {
4432
PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
4433
PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
4434
PyTuple_SET_ITEM(result, 0, Py_NewRef(key));
4435
PyTuple_SET_ITEM(result, 1, Py_NewRef(value));
4436
Py_INCREF(result);
4437
Py_DECREF(oldkey);
4438
Py_DECREF(oldvalue);
4439
// bpo-42536: The GC may have untracked this result tuple. Since
4440
// we're recycling it, make sure it's tracked again:
4441
if (!_PyObject_GC_IS_TRACKED(result)) {
4442
_PyObject_GC_TRACK(result);
4443
}
4444
}
4445
else {
4446
result = PyTuple_New(2);
4447
if (result == NULL) {
4448
return NULL;
4449
}
4450
PyTuple_SET_ITEM(result, 0, Py_NewRef(key));
4451
PyTuple_SET_ITEM(result, 1, Py_NewRef(value));
4452
}
4453
return result;
4454
}
4455
else {
4456
Py_UNREACHABLE();
4457
}
4458
4459
fail:
4460
di->di_dict = NULL;
4461
Py_DECREF(d);
4462
return NULL;
4463
}
4464
4465
PyTypeObject PyDictRevIterKey_Type = {
4466
PyVarObject_HEAD_INIT(&PyType_Type, 0)
4467
"dict_reversekeyiterator",
4468
sizeof(dictiterobject),
4469
.tp_dealloc = (destructor)dictiter_dealloc,
4470
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4471
.tp_traverse = (traverseproc)dictiter_traverse,
4472
.tp_iter = PyObject_SelfIter,
4473
.tp_iternext = (iternextfunc)dictreviter_iternext,
4474
.tp_methods = dictiter_methods
4475
};
4476
4477
4478
/*[clinic input]
4479
dict.__reversed__
4480
4481
Return a reverse iterator over the dict keys.
4482
[clinic start generated code]*/
4483
4484
static PyObject *
4485
dict___reversed___impl(PyDictObject *self)
4486
/*[clinic end generated code: output=e674483336d1ed51 input=23210ef3477d8c4d]*/
4487
{
4488
assert (PyDict_Check(self));
4489
return dictiter_new(self, &PyDictRevIterKey_Type);
4490
}
4491
4492
static PyObject *
4493
dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
4494
{
4495
/* copy the iterator state */
4496
dictiterobject tmp = *di;
4497
Py_XINCREF(tmp.di_dict);
4498
PyObject *list = PySequence_List((PyObject*)&tmp);
4499
Py_XDECREF(tmp.di_dict);
4500
if (list == NULL) {
4501
return NULL;
4502
}
4503
return Py_BuildValue("N(N)", _PyEval_GetBuiltin(&_Py_ID(iter)), list);
4504
}
4505
4506
PyTypeObject PyDictRevIterItem_Type = {
4507
PyVarObject_HEAD_INIT(&PyType_Type, 0)
4508
"dict_reverseitemiterator",
4509
sizeof(dictiterobject),
4510
.tp_dealloc = (destructor)dictiter_dealloc,
4511
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4512
.tp_traverse = (traverseproc)dictiter_traverse,
4513
.tp_iter = PyObject_SelfIter,
4514
.tp_iternext = (iternextfunc)dictreviter_iternext,
4515
.tp_methods = dictiter_methods
4516
};
4517
4518
PyTypeObject PyDictRevIterValue_Type = {
4519
PyVarObject_HEAD_INIT(&PyType_Type, 0)
4520
"dict_reversevalueiterator",
4521
sizeof(dictiterobject),
4522
.tp_dealloc = (destructor)dictiter_dealloc,
4523
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4524
.tp_traverse = (traverseproc)dictiter_traverse,
4525
.tp_iter = PyObject_SelfIter,
4526
.tp_iternext = (iternextfunc)dictreviter_iternext,
4527
.tp_methods = dictiter_methods
4528
};
4529
4530
/***********************************************/
4531
/* View objects for keys(), items(), values(). */
4532
/***********************************************/
4533
4534
/* The instance lay-out is the same for all three; but the type differs. */
4535
4536
static void
4537
dictview_dealloc(_PyDictViewObject *dv)
4538
{
4539
/* bpo-31095: UnTrack is needed before calling any callbacks */
4540
_PyObject_GC_UNTRACK(dv);
4541
Py_XDECREF(dv->dv_dict);
4542
PyObject_GC_Del(dv);
4543
}
4544
4545
static int
4546
dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
4547
{
4548
Py_VISIT(dv->dv_dict);
4549
return 0;
4550
}
4551
4552
static Py_ssize_t
4553
dictview_len(_PyDictViewObject *dv)
4554
{
4555
Py_ssize_t len = 0;
4556
if (dv->dv_dict != NULL)
4557
len = dv->dv_dict->ma_used;
4558
return len;
4559
}
4560
4561
PyObject *
4562
_PyDictView_New(PyObject *dict, PyTypeObject *type)
4563
{
4564
_PyDictViewObject *dv;
4565
if (dict == NULL) {
4566
PyErr_BadInternalCall();
4567
return NULL;
4568
}
4569
if (!PyDict_Check(dict)) {
4570
/* XXX Get rid of this restriction later */
4571
PyErr_Format(PyExc_TypeError,
4572
"%s() requires a dict argument, not '%s'",
4573
type->tp_name, Py_TYPE(dict)->tp_name);
4574
return NULL;
4575
}
4576
dv = PyObject_GC_New(_PyDictViewObject, type);
4577
if (dv == NULL)
4578
return NULL;
4579
dv->dv_dict = (PyDictObject *)Py_NewRef(dict);
4580
_PyObject_GC_TRACK(dv);
4581
return (PyObject *)dv;
4582
}
4583
4584
static PyObject *
4585
dictview_mapping(PyObject *view, void *Py_UNUSED(ignored)) {
4586
assert(view != NULL);
4587
assert(PyDictKeys_Check(view)
4588
|| PyDictValues_Check(view)
4589
|| PyDictItems_Check(view));
4590
PyObject *mapping = (PyObject *)((_PyDictViewObject *)view)->dv_dict;
4591
return PyDictProxy_New(mapping);
4592
}
4593
4594
static PyGetSetDef dictview_getset[] = {
4595
{"mapping", dictview_mapping, (setter)NULL,
4596
"dictionary that this view refers to", NULL},
4597
{0}
4598
};
4599
4600
/* TODO(guido): The views objects are not complete:
4601
4602
* support more set operations
4603
* support arbitrary mappings?
4604
- either these should be static or exported in dictobject.h
4605
- if public then they should probably be in builtins
4606
*/
4607
4608
/* Return 1 if self is a subset of other, iterating over self;
4609
0 if not; -1 if an error occurred. */
4610
static int
4611
all_contained_in(PyObject *self, PyObject *other)
4612
{
4613
PyObject *iter = PyObject_GetIter(self);
4614
int ok = 1;
4615
4616
if (iter == NULL)
4617
return -1;
4618
for (;;) {
4619
PyObject *next = PyIter_Next(iter);
4620
if (next == NULL) {
4621
if (PyErr_Occurred())
4622
ok = -1;
4623
break;
4624
}
4625
ok = PySequence_Contains(other, next);
4626
Py_DECREF(next);
4627
if (ok <= 0)
4628
break;
4629
}
4630
Py_DECREF(iter);
4631
return ok;
4632
}
4633
4634
static PyObject *
4635
dictview_richcompare(PyObject *self, PyObject *other, int op)
4636
{
4637
Py_ssize_t len_self, len_other;
4638
int ok;
4639
PyObject *result;
4640
4641
assert(self != NULL);
4642
assert(PyDictViewSet_Check(self));
4643
assert(other != NULL);
4644
4645
if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
4646
Py_RETURN_NOTIMPLEMENTED;
4647
4648
len_self = PyObject_Size(self);
4649
if (len_self < 0)
4650
return NULL;
4651
len_other = PyObject_Size(other);
4652
if (len_other < 0)
4653
return NULL;
4654
4655
ok = 0;
4656
switch(op) {
4657
4658
case Py_NE:
4659
case Py_EQ:
4660
if (len_self == len_other)
4661
ok = all_contained_in(self, other);
4662
if (op == Py_NE && ok >= 0)
4663
ok = !ok;
4664
break;
4665
4666
case Py_LT:
4667
if (len_self < len_other)
4668
ok = all_contained_in(self, other);
4669
break;
4670
4671
case Py_LE:
4672
if (len_self <= len_other)
4673
ok = all_contained_in(self, other);
4674
break;
4675
4676
case Py_GT:
4677
if (len_self > len_other)
4678
ok = all_contained_in(other, self);
4679
break;
4680
4681
case Py_GE:
4682
if (len_self >= len_other)
4683
ok = all_contained_in(other, self);
4684
break;
4685
4686
}
4687
if (ok < 0)
4688
return NULL;
4689
result = ok ? Py_True : Py_False;
4690
return Py_NewRef(result);
4691
}
4692
4693
static PyObject *
4694
dictview_repr(_PyDictViewObject *dv)
4695
{
4696
PyObject *seq;
4697
PyObject *result = NULL;
4698
Py_ssize_t rc;
4699
4700
rc = Py_ReprEnter((PyObject *)dv);
4701
if (rc != 0) {
4702
return rc > 0 ? PyUnicode_FromString("...") : NULL;
4703
}
4704
seq = PySequence_List((PyObject *)dv);
4705
if (seq == NULL) {
4706
goto Done;
4707
}
4708
result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
4709
Py_DECREF(seq);
4710
4711
Done:
4712
Py_ReprLeave((PyObject *)dv);
4713
return result;
4714
}
4715
4716
/*** dict_keys ***/
4717
4718
static PyObject *
4719
dictkeys_iter(_PyDictViewObject *dv)
4720
{
4721
if (dv->dv_dict == NULL) {
4722
Py_RETURN_NONE;
4723
}
4724
return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
4725
}
4726
4727
static int
4728
dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
4729
{
4730
if (dv->dv_dict == NULL)
4731
return 0;
4732
return PyDict_Contains((PyObject *)dv->dv_dict, obj);
4733
}
4734
4735
static PySequenceMethods dictkeys_as_sequence = {
4736
(lenfunc)dictview_len, /* sq_length */
4737
0, /* sq_concat */
4738
0, /* sq_repeat */
4739
0, /* sq_item */
4740
0, /* sq_slice */
4741
0, /* sq_ass_item */
4742
0, /* sq_ass_slice */
4743
(objobjproc)dictkeys_contains, /* sq_contains */
4744
};
4745
4746
// Create a set object from dictviews object.
4747
// Returns a new reference.
4748
// This utility function is used by set operations.
4749
static PyObject*
4750
dictviews_to_set(PyObject *self)
4751
{
4752
PyObject *left = self;
4753
if (PyDictKeys_Check(self)) {
4754
// PySet_New() has fast path for the dict object.
4755
PyObject *dict = (PyObject *)((_PyDictViewObject *)self)->dv_dict;
4756
if (PyDict_CheckExact(dict)) {
4757
left = dict;
4758
}
4759
}
4760
return PySet_New(left);
4761
}
4762
4763
static PyObject*
4764
dictviews_sub(PyObject *self, PyObject *other)
4765
{
4766
PyObject *result = dictviews_to_set(self);
4767
if (result == NULL) {
4768
return NULL;
4769
}
4770
4771
PyObject *tmp = PyObject_CallMethodOneArg(
4772
result, &_Py_ID(difference_update), other);
4773
if (tmp == NULL) {
4774
Py_DECREF(result);
4775
return NULL;
4776
}
4777
4778
Py_DECREF(tmp);
4779
return result;
4780
}
4781
4782
static int
4783
dictitems_contains(_PyDictViewObject *dv, PyObject *obj);
4784
4785
PyObject *
4786
_PyDictView_Intersect(PyObject* self, PyObject *other)
4787
{
4788
PyObject *result;
4789
PyObject *it;
4790
PyObject *key;
4791
Py_ssize_t len_self;
4792
int rv;
4793
int (*dict_contains)(_PyDictViewObject *, PyObject *);
4794
4795
/* Python interpreter swaps parameters when dict view
4796
is on right side of & */
4797
if (!PyDictViewSet_Check(self)) {
4798
PyObject *tmp = other;
4799
other = self;
4800
self = tmp;
4801
}
4802
4803
len_self = dictview_len((_PyDictViewObject *)self);
4804
4805
/* if other is a set and self is smaller than other,
4806
reuse set intersection logic */
4807
if (PySet_CheckExact(other) && len_self <= PyObject_Size(other)) {
4808
return PyObject_CallMethodObjArgs(
4809
other, &_Py_ID(intersection), self, NULL);
4810
}
4811
4812
/* if other is another dict view, and it is bigger than self,
4813
swap them */
4814
if (PyDictViewSet_Check(other)) {
4815
Py_ssize_t len_other = dictview_len((_PyDictViewObject *)other);
4816
if (len_other > len_self) {
4817
PyObject *tmp = other;
4818
other = self;
4819
self = tmp;
4820
}
4821
}
4822
4823
/* at this point, two things should be true
4824
1. self is a dictview
4825
2. if other is a dictview then it is smaller than self */
4826
result = PySet_New(NULL);
4827
if (result == NULL)
4828
return NULL;
4829
4830
it = PyObject_GetIter(other);
4831
if (it == NULL) {
4832
Py_DECREF(result);
4833
return NULL;
4834
}
4835
4836
if (PyDictKeys_Check(self)) {
4837
dict_contains = dictkeys_contains;
4838
}
4839
/* else PyDictItems_Check(self) */
4840
else {
4841
dict_contains = dictitems_contains;
4842
}
4843
4844
while ((key = PyIter_Next(it)) != NULL) {
4845
rv = dict_contains((_PyDictViewObject *)self, key);
4846
if (rv < 0) {
4847
goto error;
4848
}
4849
if (rv) {
4850
if (PySet_Add(result, key)) {
4851
goto error;
4852
}
4853
}
4854
Py_DECREF(key);
4855
}
4856
Py_DECREF(it);
4857
if (PyErr_Occurred()) {
4858
Py_DECREF(result);
4859
return NULL;
4860
}
4861
return result;
4862
4863
error:
4864
Py_DECREF(it);
4865
Py_DECREF(result);
4866
Py_DECREF(key);
4867
return NULL;
4868
}
4869
4870
static PyObject*
4871
dictviews_or(PyObject* self, PyObject *other)
4872
{
4873
PyObject *result = dictviews_to_set(self);
4874
if (result == NULL) {
4875
return NULL;
4876
}
4877
4878
if (_PySet_Update(result, other) < 0) {
4879
Py_DECREF(result);
4880
return NULL;
4881
}
4882
return result;
4883
}
4884
4885
static PyObject *
4886
dictitems_xor(PyObject *self, PyObject *other)
4887
{
4888
assert(PyDictItems_Check(self));
4889
assert(PyDictItems_Check(other));
4890
PyObject *d1 = (PyObject *)((_PyDictViewObject *)self)->dv_dict;
4891
PyObject *d2 = (PyObject *)((_PyDictViewObject *)other)->dv_dict;
4892
4893
PyObject *temp_dict = PyDict_Copy(d1);
4894
if (temp_dict == NULL) {
4895
return NULL;
4896
}
4897
PyObject *result_set = PySet_New(NULL);
4898
if (result_set == NULL) {
4899
Py_CLEAR(temp_dict);
4900
return NULL;
4901
}
4902
4903
PyObject *key = NULL, *val1 = NULL, *val2 = NULL;
4904
Py_ssize_t pos = 0;
4905
Py_hash_t hash;
4906
4907
while (_PyDict_Next(d2, &pos, &key, &val2, &hash)) {
4908
Py_INCREF(key);
4909
Py_INCREF(val2);
4910
val1 = _PyDict_GetItem_KnownHash(temp_dict, key, hash);
4911
4912
int to_delete;
4913
if (val1 == NULL) {
4914
if (PyErr_Occurred()) {
4915
goto error;
4916
}
4917
to_delete = 0;
4918
}
4919
else {
4920
Py_INCREF(val1);
4921
to_delete = PyObject_RichCompareBool(val1, val2, Py_EQ);
4922
if (to_delete < 0) {
4923
goto error;
4924
}
4925
}
4926
4927
if (to_delete) {
4928
if (_PyDict_DelItem_KnownHash(temp_dict, key, hash) < 0) {
4929
goto error;
4930
}
4931
}
4932
else {
4933
PyObject *pair = PyTuple_Pack(2, key, val2);
4934
if (pair == NULL) {
4935
goto error;
4936
}
4937
if (PySet_Add(result_set, pair) < 0) {
4938
Py_DECREF(pair);
4939
goto error;
4940
}
4941
Py_DECREF(pair);
4942
}
4943
Py_DECREF(key);
4944
Py_XDECREF(val1);
4945
Py_DECREF(val2);
4946
}
4947
key = val1 = val2 = NULL;
4948
4949
PyObject *remaining_pairs = PyObject_CallMethodNoArgs(
4950
temp_dict, &_Py_ID(items));
4951
if (remaining_pairs == NULL) {
4952
goto error;
4953
}
4954
if (_PySet_Update(result_set, remaining_pairs) < 0) {
4955
Py_DECREF(remaining_pairs);
4956
goto error;
4957
}
4958
Py_DECREF(temp_dict);
4959
Py_DECREF(remaining_pairs);
4960
return result_set;
4961
4962
error:
4963
Py_XDECREF(temp_dict);
4964
Py_XDECREF(result_set);
4965
Py_XDECREF(key);
4966
Py_XDECREF(val1);
4967
Py_XDECREF(val2);
4968
return NULL;
4969
}
4970
4971
static PyObject*
4972
dictviews_xor(PyObject* self, PyObject *other)
4973
{
4974
if (PyDictItems_Check(self) && PyDictItems_Check(other)) {
4975
return dictitems_xor(self, other);
4976
}
4977
PyObject *result = dictviews_to_set(self);
4978
if (result == NULL) {
4979
return NULL;
4980
}
4981
4982
PyObject *tmp = PyObject_CallMethodOneArg(
4983
result, &_Py_ID(symmetric_difference_update), other);
4984
if (tmp == NULL) {
4985
Py_DECREF(result);
4986
return NULL;
4987
}
4988
4989
Py_DECREF(tmp);
4990
return result;
4991
}
4992
4993
static PyNumberMethods dictviews_as_number = {
4994
0, /*nb_add*/
4995
(binaryfunc)dictviews_sub, /*nb_subtract*/
4996
0, /*nb_multiply*/
4997
0, /*nb_remainder*/
4998
0, /*nb_divmod*/
4999
0, /*nb_power*/
5000
0, /*nb_negative*/
5001
0, /*nb_positive*/
5002
0, /*nb_absolute*/
5003
0, /*nb_bool*/
5004
0, /*nb_invert*/
5005
0, /*nb_lshift*/
5006
0, /*nb_rshift*/
5007
(binaryfunc)_PyDictView_Intersect, /*nb_and*/
5008
(binaryfunc)dictviews_xor, /*nb_xor*/
5009
(binaryfunc)dictviews_or, /*nb_or*/
5010
};
5011
5012
static PyObject*
5013
dictviews_isdisjoint(PyObject *self, PyObject *other)
5014
{
5015
PyObject *it;
5016
PyObject *item = NULL;
5017
5018
if (self == other) {
5019
if (dictview_len((_PyDictViewObject *)self) == 0)
5020
Py_RETURN_TRUE;
5021
else
5022
Py_RETURN_FALSE;
5023
}
5024
5025
/* Iterate over the shorter object (only if other is a set,
5026
* because PySequence_Contains may be expensive otherwise): */
5027
if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
5028
Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
5029
Py_ssize_t len_other = PyObject_Size(other);
5030
if (len_other == -1)
5031
return NULL;
5032
5033
if ((len_other > len_self)) {
5034
PyObject *tmp = other;
5035
other = self;
5036
self = tmp;
5037
}
5038
}
5039
5040
it = PyObject_GetIter(other);
5041
if (it == NULL)
5042
return NULL;
5043
5044
while ((item = PyIter_Next(it)) != NULL) {
5045
int contains = PySequence_Contains(self, item);
5046
Py_DECREF(item);
5047
if (contains == -1) {
5048
Py_DECREF(it);
5049
return NULL;
5050
}
5051
5052
if (contains) {
5053
Py_DECREF(it);
5054
Py_RETURN_FALSE;
5055
}
5056
}
5057
Py_DECREF(it);
5058
if (PyErr_Occurred())
5059
return NULL; /* PyIter_Next raised an exception. */
5060
Py_RETURN_TRUE;
5061
}
5062
5063
PyDoc_STRVAR(isdisjoint_doc,
5064
"Return True if the view and the given iterable have a null intersection.");
5065
5066
static PyObject* dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored));
5067
5068
PyDoc_STRVAR(reversed_keys_doc,
5069
"Return a reverse iterator over the dict keys.");
5070
5071
static PyMethodDef dictkeys_methods[] = {
5072
{"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
5073
isdisjoint_doc},
5074
{"__reversed__", _PyCFunction_CAST(dictkeys_reversed), METH_NOARGS,
5075
reversed_keys_doc},
5076
{NULL, NULL} /* sentinel */
5077
};
5078
5079
PyTypeObject PyDictKeys_Type = {
5080
PyVarObject_HEAD_INIT(&PyType_Type, 0)
5081
"dict_keys", /* tp_name */
5082
sizeof(_PyDictViewObject), /* tp_basicsize */
5083
0, /* tp_itemsize */
5084
/* methods */
5085
(destructor)dictview_dealloc, /* tp_dealloc */
5086
0, /* tp_vectorcall_offset */
5087
0, /* tp_getattr */
5088
0, /* tp_setattr */
5089
0, /* tp_as_async */
5090
(reprfunc)dictview_repr, /* tp_repr */
5091
&dictviews_as_number, /* tp_as_number */
5092
&dictkeys_as_sequence, /* tp_as_sequence */
5093
0, /* tp_as_mapping */
5094
0, /* tp_hash */
5095
0, /* tp_call */
5096
0, /* tp_str */
5097
PyObject_GenericGetAttr, /* tp_getattro */
5098
0, /* tp_setattro */
5099
0, /* tp_as_buffer */
5100
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
5101
0, /* tp_doc */
5102
(traverseproc)dictview_traverse, /* tp_traverse */
5103
0, /* tp_clear */
5104
dictview_richcompare, /* tp_richcompare */
5105
0, /* tp_weaklistoffset */
5106
(getiterfunc)dictkeys_iter, /* tp_iter */
5107
0, /* tp_iternext */
5108
dictkeys_methods, /* tp_methods */
5109
.tp_getset = dictview_getset,
5110
};
5111
5112
static PyObject *
5113
dictkeys_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
5114
{
5115
return _PyDictView_New(dict, &PyDictKeys_Type);
5116
}
5117
5118
static PyObject *
5119
dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
5120
{
5121
if (dv->dv_dict == NULL) {
5122
Py_RETURN_NONE;
5123
}
5124
return dictiter_new(dv->dv_dict, &PyDictRevIterKey_Type);
5125
}
5126
5127
/*** dict_items ***/
5128
5129
static PyObject *
5130
dictitems_iter(_PyDictViewObject *dv)
5131
{
5132
if (dv->dv_dict == NULL) {
5133
Py_RETURN_NONE;
5134
}
5135
return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
5136
}
5137
5138
static int
5139
dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
5140
{
5141
int result;
5142
PyObject *key, *value, *found;
5143
if (dv->dv_dict == NULL)
5144
return 0;
5145
if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
5146
return 0;
5147
key = PyTuple_GET_ITEM(obj, 0);
5148
value = PyTuple_GET_ITEM(obj, 1);
5149
found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
5150
if (found == NULL) {
5151
if (PyErr_Occurred())
5152
return -1;
5153
return 0;
5154
}
5155
Py_INCREF(found);
5156
result = PyObject_RichCompareBool(found, value, Py_EQ);
5157
Py_DECREF(found);
5158
return result;
5159
}
5160
5161
static PySequenceMethods dictitems_as_sequence = {
5162
(lenfunc)dictview_len, /* sq_length */
5163
0, /* sq_concat */
5164
0, /* sq_repeat */
5165
0, /* sq_item */
5166
0, /* sq_slice */
5167
0, /* sq_ass_item */
5168
0, /* sq_ass_slice */
5169
(objobjproc)dictitems_contains, /* sq_contains */
5170
};
5171
5172
static PyObject* dictitems_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored));
5173
5174
PyDoc_STRVAR(reversed_items_doc,
5175
"Return a reverse iterator over the dict items.");
5176
5177
static PyMethodDef dictitems_methods[] = {
5178
{"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
5179
isdisjoint_doc},
5180
{"__reversed__", (PyCFunction)dictitems_reversed, METH_NOARGS,
5181
reversed_items_doc},
5182
{NULL, NULL} /* sentinel */
5183
};
5184
5185
PyTypeObject PyDictItems_Type = {
5186
PyVarObject_HEAD_INIT(&PyType_Type, 0)
5187
"dict_items", /* tp_name */
5188
sizeof(_PyDictViewObject), /* tp_basicsize */
5189
0, /* tp_itemsize */
5190
/* methods */
5191
(destructor)dictview_dealloc, /* tp_dealloc */
5192
0, /* tp_vectorcall_offset */
5193
0, /* tp_getattr */
5194
0, /* tp_setattr */
5195
0, /* tp_as_async */
5196
(reprfunc)dictview_repr, /* tp_repr */
5197
&dictviews_as_number, /* tp_as_number */
5198
&dictitems_as_sequence, /* tp_as_sequence */
5199
0, /* tp_as_mapping */
5200
0, /* tp_hash */
5201
0, /* tp_call */
5202
0, /* tp_str */
5203
PyObject_GenericGetAttr, /* tp_getattro */
5204
0, /* tp_setattro */
5205
0, /* tp_as_buffer */
5206
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
5207
0, /* tp_doc */
5208
(traverseproc)dictview_traverse, /* tp_traverse */
5209
0, /* tp_clear */
5210
dictview_richcompare, /* tp_richcompare */
5211
0, /* tp_weaklistoffset */
5212
(getiterfunc)dictitems_iter, /* tp_iter */
5213
0, /* tp_iternext */
5214
dictitems_methods, /* tp_methods */
5215
.tp_getset = dictview_getset,
5216
};
5217
5218
static PyObject *
5219
dictitems_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
5220
{
5221
return _PyDictView_New(dict, &PyDictItems_Type);
5222
}
5223
5224
static PyObject *
5225
dictitems_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
5226
{
5227
if (dv->dv_dict == NULL) {
5228
Py_RETURN_NONE;
5229
}
5230
return dictiter_new(dv->dv_dict, &PyDictRevIterItem_Type);
5231
}
5232
5233
/*** dict_values ***/
5234
5235
static PyObject *
5236
dictvalues_iter(_PyDictViewObject *dv)
5237
{
5238
if (dv->dv_dict == NULL) {
5239
Py_RETURN_NONE;
5240
}
5241
return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
5242
}
5243
5244
static PySequenceMethods dictvalues_as_sequence = {
5245
(lenfunc)dictview_len, /* sq_length */
5246
0, /* sq_concat */
5247
0, /* sq_repeat */
5248
0, /* sq_item */
5249
0, /* sq_slice */
5250
0, /* sq_ass_item */
5251
0, /* sq_ass_slice */
5252
(objobjproc)0, /* sq_contains */
5253
};
5254
5255
static PyObject* dictvalues_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored));
5256
5257
PyDoc_STRVAR(reversed_values_doc,
5258
"Return a reverse iterator over the dict values.");
5259
5260
static PyMethodDef dictvalues_methods[] = {
5261
{"__reversed__", (PyCFunction)dictvalues_reversed, METH_NOARGS,
5262
reversed_values_doc},
5263
{NULL, NULL} /* sentinel */
5264
};
5265
5266
PyTypeObject PyDictValues_Type = {
5267
PyVarObject_HEAD_INIT(&PyType_Type, 0)
5268
"dict_values", /* tp_name */
5269
sizeof(_PyDictViewObject), /* tp_basicsize */
5270
0, /* tp_itemsize */
5271
/* methods */
5272
(destructor)dictview_dealloc, /* tp_dealloc */
5273
0, /* tp_vectorcall_offset */
5274
0, /* tp_getattr */
5275
0, /* tp_setattr */
5276
0, /* tp_as_async */
5277
(reprfunc)dictview_repr, /* tp_repr */
5278
0, /* tp_as_number */
5279
&dictvalues_as_sequence, /* tp_as_sequence */
5280
0, /* tp_as_mapping */
5281
0, /* tp_hash */
5282
0, /* tp_call */
5283
0, /* tp_str */
5284
PyObject_GenericGetAttr, /* tp_getattro */
5285
0, /* tp_setattro */
5286
0, /* tp_as_buffer */
5287
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
5288
0, /* tp_doc */
5289
(traverseproc)dictview_traverse, /* tp_traverse */
5290
0, /* tp_clear */
5291
0, /* tp_richcompare */
5292
0, /* tp_weaklistoffset */
5293
(getiterfunc)dictvalues_iter, /* tp_iter */
5294
0, /* tp_iternext */
5295
dictvalues_methods, /* tp_methods */
5296
.tp_getset = dictview_getset,
5297
};
5298
5299
static PyObject *
5300
dictvalues_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
5301
{
5302
return _PyDictView_New(dict, &PyDictValues_Type);
5303
}
5304
5305
static PyObject *
5306
dictvalues_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
5307
{
5308
if (dv->dv_dict == NULL) {
5309
Py_RETURN_NONE;
5310
}
5311
return dictiter_new(dv->dv_dict, &PyDictRevIterValue_Type);
5312
}
5313
5314
5315
/* Returns NULL if cannot allocate a new PyDictKeysObject,
5316
but does not set an error */
5317
PyDictKeysObject *
5318
_PyDict_NewKeysForClass(void)
5319
{
5320
PyInterpreterState *interp = _PyInterpreterState_GET();
5321
PyDictKeysObject *keys = new_keys_object(
5322
interp, NEXT_LOG2_SHARED_KEYS_MAX_SIZE, 1);
5323
if (keys == NULL) {
5324
PyErr_Clear();
5325
}
5326
else {
5327
assert(keys->dk_nentries == 0);
5328
/* Set to max size+1 as it will shrink by one before each new object */
5329
keys->dk_usable = SHARED_KEYS_MAX_SIZE;
5330
keys->dk_kind = DICT_KEYS_SPLIT;
5331
}
5332
return keys;
5333
}
5334
5335
#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
5336
5337
int
5338
_PyObject_InitInlineValues(PyObject *obj, PyTypeObject *tp)
5339
{
5340
assert(tp->tp_flags & Py_TPFLAGS_HEAPTYPE);
5341
assert(tp->tp_flags & Py_TPFLAGS_MANAGED_DICT);
5342
PyDictKeysObject *keys = CACHED_KEYS(tp);
5343
assert(keys != NULL);
5344
if (keys->dk_usable > 1) {
5345
keys->dk_usable--;
5346
}
5347
size_t size = shared_keys_usable_size(keys);
5348
PyDictValues *values = new_values(size);
5349
if (values == NULL) {
5350
PyErr_NoMemory();
5351
return -1;
5352
}
5353
assert(((uint8_t *)values)[-1] >= (size + 2));
5354
((uint8_t *)values)[-2] = 0;
5355
for (size_t i = 0; i < size; i++) {
5356
values->values[i] = NULL;
5357
}
5358
_PyDictOrValues_SetValues(_PyObject_DictOrValuesPointer(obj), values);
5359
return 0;
5360
}
5361
5362
int
5363
_PyObject_InitializeDict(PyObject *obj)
5364
{
5365
PyInterpreterState *interp = _PyInterpreterState_GET();
5366
PyTypeObject *tp = Py_TYPE(obj);
5367
if (tp->tp_dictoffset == 0) {
5368
return 0;
5369
}
5370
if (tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) {
5371
OBJECT_STAT_INC(new_values);
5372
return _PyObject_InitInlineValues(obj, tp);
5373
}
5374
PyObject *dict;
5375
if (_PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
5376
dictkeys_incref(CACHED_KEYS(tp));
5377
dict = new_dict_with_shared_keys(interp, CACHED_KEYS(tp));
5378
}
5379
else {
5380
dict = PyDict_New();
5381
}
5382
if (dict == NULL) {
5383
return -1;
5384
}
5385
PyObject **dictptr = _PyObject_ComputedDictPointer(obj);
5386
*dictptr = dict;
5387
return 0;
5388
}
5389
5390
static PyObject *
5391
make_dict_from_instance_attributes(PyInterpreterState *interp,
5392
PyDictKeysObject *keys, PyDictValues *values)
5393
{
5394
dictkeys_incref(keys);
5395
Py_ssize_t used = 0;
5396
Py_ssize_t track = 0;
5397
size_t size = shared_keys_usable_size(keys);
5398
for (size_t i = 0; i < size; i++) {
5399
PyObject *val = values->values[i];
5400
if (val != NULL) {
5401
used += 1;
5402
track += _PyObject_GC_MAY_BE_TRACKED(val);
5403
}
5404
}
5405
PyObject *res = new_dict(interp, keys, values, used, 0);
5406
if (track && res) {
5407
_PyObject_GC_TRACK(res);
5408
}
5409
return res;
5410
}
5411
5412
PyObject *
5413
_PyObject_MakeDictFromInstanceAttributes(PyObject *obj, PyDictValues *values)
5414
{
5415
PyInterpreterState *interp = _PyInterpreterState_GET();
5416
PyDictKeysObject *keys = CACHED_KEYS(Py_TYPE(obj));
5417
OBJECT_STAT_INC(dict_materialized_on_request);
5418
return make_dict_from_instance_attributes(interp, keys, values);
5419
}
5420
5421
int
5422
_PyObject_StoreInstanceAttribute(PyObject *obj, PyDictValues *values,
5423
PyObject *name, PyObject *value)
5424
{
5425
PyInterpreterState *interp = _PyInterpreterState_GET();
5426
PyDictKeysObject *keys = CACHED_KEYS(Py_TYPE(obj));
5427
assert(keys != NULL);
5428
assert(values != NULL);
5429
assert(Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT);
5430
Py_ssize_t ix = DKIX_EMPTY;
5431
if (PyUnicode_CheckExact(name)) {
5432
ix = insert_into_dictkeys(keys, name);
5433
}
5434
if (ix == DKIX_EMPTY) {
5435
#ifdef Py_STATS
5436
if (PyUnicode_CheckExact(name)) {
5437
if (shared_keys_usable_size(keys) == SHARED_KEYS_MAX_SIZE) {
5438
OBJECT_STAT_INC(dict_materialized_too_big);
5439
}
5440
else {
5441
OBJECT_STAT_INC(dict_materialized_new_key);
5442
}
5443
}
5444
else {
5445
OBJECT_STAT_INC(dict_materialized_str_subclass);
5446
}
5447
#endif
5448
PyObject *dict = make_dict_from_instance_attributes(
5449
interp, keys, values);
5450
if (dict == NULL) {
5451
return -1;
5452
}
5453
_PyObject_DictOrValuesPointer(obj)->dict = dict;
5454
if (value == NULL) {
5455
return PyDict_DelItem(dict, name);
5456
}
5457
else {
5458
return PyDict_SetItem(dict, name, value);
5459
}
5460
}
5461
PyObject *old_value = values->values[ix];
5462
values->values[ix] = Py_XNewRef(value);
5463
if (old_value == NULL) {
5464
if (value == NULL) {
5465
PyErr_Format(PyExc_AttributeError,
5466
"'%.100s' object has no attribute '%U'",
5467
Py_TYPE(obj)->tp_name, name);
5468
return -1;
5469
}
5470
_PyDictValues_AddToInsertionOrder(values, ix);
5471
}
5472
else {
5473
if (value == NULL) {
5474
delete_index_from_values(values, ix);
5475
}
5476
Py_DECREF(old_value);
5477
}
5478
return 0;
5479
}
5480
5481
/* Sanity check for managed dicts */
5482
#if 0
5483
#define CHECK(val) assert(val); if (!(val)) { return 0; }
5484
5485
int
5486
_PyObject_ManagedDictValidityCheck(PyObject *obj)
5487
{
5488
PyTypeObject *tp = Py_TYPE(obj);
5489
CHECK(tp->tp_flags & Py_TPFLAGS_MANAGED_DICT);
5490
PyDictOrValues *dorv_ptr = _PyObject_DictOrValuesPointer(obj);
5491
if (_PyDictOrValues_IsValues(*dorv_ptr)) {
5492
PyDictValues *values = _PyDictOrValues_GetValues(*dorv_ptr);
5493
int size = ((uint8_t *)values)[-2];
5494
int count = 0;
5495
PyDictKeysObject *keys = CACHED_KEYS(tp);
5496
for (Py_ssize_t i = 0; i < keys->dk_nentries; i++) {
5497
if (values->values[i] != NULL) {
5498
count++;
5499
}
5500
}
5501
CHECK(size == count);
5502
}
5503
else {
5504
if (dorv_ptr->dict != NULL) {
5505
CHECK(PyDict_Check(dorv_ptr->dict));
5506
}
5507
}
5508
return 1;
5509
}
5510
#endif
5511
5512
PyObject *
5513
_PyObject_GetInstanceAttribute(PyObject *obj, PyDictValues *values,
5514
PyObject *name)
5515
{
5516
assert(PyUnicode_CheckExact(name));
5517
PyDictKeysObject *keys = CACHED_KEYS(Py_TYPE(obj));
5518
assert(keys != NULL);
5519
Py_ssize_t ix = _PyDictKeys_StringLookup(keys, name);
5520
if (ix == DKIX_EMPTY) {
5521
return NULL;
5522
}
5523
PyObject *value = values->values[ix];
5524
return Py_XNewRef(value);
5525
}
5526
5527
int
5528
_PyObject_IsInstanceDictEmpty(PyObject *obj)
5529
{
5530
PyTypeObject *tp = Py_TYPE(obj);
5531
if (tp->tp_dictoffset == 0) {
5532
return 1;
5533
}
5534
PyObject *dict;
5535
if (tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) {
5536
PyDictOrValues dorv = *_PyObject_DictOrValuesPointer(obj);
5537
if (_PyDictOrValues_IsValues(dorv)) {
5538
PyDictKeysObject *keys = CACHED_KEYS(tp);
5539
for (Py_ssize_t i = 0; i < keys->dk_nentries; i++) {
5540
if (_PyDictOrValues_GetValues(dorv)->values[i] != NULL) {
5541
return 0;
5542
}
5543
}
5544
return 1;
5545
}
5546
dict = _PyDictOrValues_GetDict(dorv);
5547
}
5548
else {
5549
PyObject **dictptr = _PyObject_ComputedDictPointer(obj);
5550
dict = *dictptr;
5551
}
5552
if (dict == NULL) {
5553
return 1;
5554
}
5555
return ((PyDictObject *)dict)->ma_used == 0;
5556
}
5557
5558
void
5559
_PyObject_FreeInstanceAttributes(PyObject *self)
5560
{
5561
PyTypeObject *tp = Py_TYPE(self);
5562
assert(Py_TYPE(self)->tp_flags & Py_TPFLAGS_MANAGED_DICT);
5563
PyDictOrValues dorv = *_PyObject_DictOrValuesPointer(self);
5564
if (!_PyDictOrValues_IsValues(dorv)) {
5565
return;
5566
}
5567
PyDictValues *values = _PyDictOrValues_GetValues(dorv);
5568
PyDictKeysObject *keys = CACHED_KEYS(tp);
5569
for (Py_ssize_t i = 0; i < keys->dk_nentries; i++) {
5570
Py_XDECREF(values->values[i]);
5571
}
5572
free_values(values);
5573
}
5574
5575
int
5576
_PyObject_VisitManagedDict(PyObject *obj, visitproc visit, void *arg)
5577
{
5578
PyTypeObject *tp = Py_TYPE(obj);
5579
if((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0) {
5580
return 0;
5581
}
5582
assert(tp->tp_dictoffset);
5583
PyDictOrValues dorv = *_PyObject_DictOrValuesPointer(obj);
5584
if (_PyDictOrValues_IsValues(dorv)) {
5585
PyDictValues *values = _PyDictOrValues_GetValues(dorv);
5586
PyDictKeysObject *keys = CACHED_KEYS(tp);
5587
for (Py_ssize_t i = 0; i < keys->dk_nentries; i++) {
5588
Py_VISIT(values->values[i]);
5589
}
5590
}
5591
else {
5592
PyObject *dict = _PyDictOrValues_GetDict(dorv);
5593
Py_VISIT(dict);
5594
}
5595
return 0;
5596
}
5597
5598
void
5599
_PyObject_ClearManagedDict(PyObject *obj)
5600
{
5601
PyTypeObject *tp = Py_TYPE(obj);
5602
if((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0) {
5603
return;
5604
}
5605
PyDictOrValues *dorv_ptr = _PyObject_DictOrValuesPointer(obj);
5606
if (_PyDictOrValues_IsValues(*dorv_ptr)) {
5607
PyDictValues *values = _PyDictOrValues_GetValues(*dorv_ptr);
5608
PyDictKeysObject *keys = CACHED_KEYS(tp);
5609
for (Py_ssize_t i = 0; i < keys->dk_nentries; i++) {
5610
Py_CLEAR(values->values[i]);
5611
}
5612
dorv_ptr->dict = NULL;
5613
free_values(values);
5614
}
5615
else {
5616
PyObject *dict = dorv_ptr->dict;
5617
if (dict) {
5618
dorv_ptr->dict = NULL;
5619
Py_DECREF(dict);
5620
}
5621
}
5622
}
5623
5624
PyObject *
5625
PyObject_GenericGetDict(PyObject *obj, void *context)
5626
{
5627
PyObject *dict;
5628
PyInterpreterState *interp = _PyInterpreterState_GET();
5629
PyTypeObject *tp = Py_TYPE(obj);
5630
if (_PyType_HasFeature(tp, Py_TPFLAGS_MANAGED_DICT)) {
5631
PyDictOrValues *dorv_ptr = _PyObject_DictOrValuesPointer(obj);
5632
if (_PyDictOrValues_IsValues(*dorv_ptr)) {
5633
PyDictValues *values = _PyDictOrValues_GetValues(*dorv_ptr);
5634
OBJECT_STAT_INC(dict_materialized_on_request);
5635
dict = make_dict_from_instance_attributes(
5636
interp, CACHED_KEYS(tp), values);
5637
if (dict != NULL) {
5638
dorv_ptr->dict = dict;
5639
}
5640
}
5641
else {
5642
dict = _PyDictOrValues_GetDict(*dorv_ptr);
5643
if (dict == NULL) {
5644
dictkeys_incref(CACHED_KEYS(tp));
5645
dict = new_dict_with_shared_keys(interp, CACHED_KEYS(tp));
5646
dorv_ptr->dict = dict;
5647
}
5648
}
5649
}
5650
else {
5651
PyObject **dictptr = _PyObject_ComputedDictPointer(obj);
5652
if (dictptr == NULL) {
5653
PyErr_SetString(PyExc_AttributeError,
5654
"This object has no __dict__");
5655
return NULL;
5656
}
5657
dict = *dictptr;
5658
if (dict == NULL) {
5659
PyTypeObject *tp = Py_TYPE(obj);
5660
if (_PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
5661
dictkeys_incref(CACHED_KEYS(tp));
5662
*dictptr = dict = new_dict_with_shared_keys(
5663
interp, CACHED_KEYS(tp));
5664
}
5665
else {
5666
*dictptr = dict = PyDict_New();
5667
}
5668
}
5669
}
5670
return Py_XNewRef(dict);
5671
}
5672
5673
int
5674
_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
5675
PyObject *key, PyObject *value)
5676
{
5677
PyObject *dict;
5678
int res;
5679
PyDictKeysObject *cached;
5680
PyInterpreterState *interp = _PyInterpreterState_GET();
5681
5682
assert(dictptr != NULL);
5683
if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
5684
assert(dictptr != NULL);
5685
dict = *dictptr;
5686
if (dict == NULL) {
5687
dictkeys_incref(cached);
5688
dict = new_dict_with_shared_keys(interp, cached);
5689
if (dict == NULL)
5690
return -1;
5691
*dictptr = dict;
5692
}
5693
if (value == NULL) {
5694
res = PyDict_DelItem(dict, key);
5695
}
5696
else {
5697
res = PyDict_SetItem(dict, key, value);
5698
}
5699
} else {
5700
dict = *dictptr;
5701
if (dict == NULL) {
5702
dict = PyDict_New();
5703
if (dict == NULL)
5704
return -1;
5705
*dictptr = dict;
5706
}
5707
if (value == NULL) {
5708
res = PyDict_DelItem(dict, key);
5709
} else {
5710
res = PyDict_SetItem(dict, key, value);
5711
}
5712
}
5713
ASSERT_CONSISTENT(dict);
5714
return res;
5715
}
5716
5717
void
5718
_PyDictKeys_DecRef(PyDictKeysObject *keys)
5719
{
5720
PyInterpreterState *interp = _PyInterpreterState_GET();
5721
dictkeys_decref(interp, keys);
5722
}
5723
5724
uint32_t _PyDictKeys_GetVersionForCurrentState(PyInterpreterState *interp,
5725
PyDictKeysObject *dictkeys)
5726
{
5727
if (dictkeys->dk_version != 0) {
5728
return dictkeys->dk_version;
5729
}
5730
if (interp->dict_state.next_keys_version == 0) {
5731
return 0;
5732
}
5733
uint32_t v = interp->dict_state.next_keys_version++;
5734
dictkeys->dk_version = v;
5735
return v;
5736
}
5737
5738
static inline int
5739
validate_watcher_id(PyInterpreterState *interp, int watcher_id)
5740
{
5741
if (watcher_id < 0 || watcher_id >= DICT_MAX_WATCHERS) {
5742
PyErr_Format(PyExc_ValueError, "Invalid dict watcher ID %d", watcher_id);
5743
return -1;
5744
}
5745
if (!interp->dict_state.watchers[watcher_id]) {
5746
PyErr_Format(PyExc_ValueError, "No dict watcher set for ID %d", watcher_id);
5747
return -1;
5748
}
5749
return 0;
5750
}
5751
5752
int
5753
PyDict_Watch(int watcher_id, PyObject* dict)
5754
{
5755
if (!PyDict_Check(dict)) {
5756
PyErr_SetString(PyExc_ValueError, "Cannot watch non-dictionary");
5757
return -1;
5758
}
5759
PyInterpreterState *interp = _PyInterpreterState_GET();
5760
if (validate_watcher_id(interp, watcher_id)) {
5761
return -1;
5762
}
5763
((PyDictObject*)dict)->ma_version_tag |= (1LL << watcher_id);
5764
return 0;
5765
}
5766
5767
int
5768
PyDict_Unwatch(int watcher_id, PyObject* dict)
5769
{
5770
if (!PyDict_Check(dict)) {
5771
PyErr_SetString(PyExc_ValueError, "Cannot watch non-dictionary");
5772
return -1;
5773
}
5774
PyInterpreterState *interp = _PyInterpreterState_GET();
5775
if (validate_watcher_id(interp, watcher_id)) {
5776
return -1;
5777
}
5778
((PyDictObject*)dict)->ma_version_tag &= ~(1LL << watcher_id);
5779
return 0;
5780
}
5781
5782
int
5783
PyDict_AddWatcher(PyDict_WatchCallback callback)
5784
{
5785
PyInterpreterState *interp = _PyInterpreterState_GET();
5786
5787
for (int i = 0; i < DICT_MAX_WATCHERS; i++) {
5788
if (!interp->dict_state.watchers[i]) {
5789
interp->dict_state.watchers[i] = callback;
5790
return i;
5791
}
5792
}
5793
5794
PyErr_SetString(PyExc_RuntimeError, "no more dict watcher IDs available");
5795
return -1;
5796
}
5797
5798
int
5799
PyDict_ClearWatcher(int watcher_id)
5800
{
5801
PyInterpreterState *interp = _PyInterpreterState_GET();
5802
if (validate_watcher_id(interp, watcher_id)) {
5803
return -1;
5804
}
5805
interp->dict_state.watchers[watcher_id] = NULL;
5806
return 0;
5807
}
5808
5809
static const char *
5810
dict_event_name(PyDict_WatchEvent event) {
5811
switch (event) {
5812
#define CASE(op) \
5813
case PyDict_EVENT_##op: \
5814
return "PyDict_EVENT_" #op;
5815
PY_FOREACH_DICT_EVENT(CASE)
5816
#undef CASE
5817
}
5818
Py_UNREACHABLE();
5819
}
5820
5821
void
5822
_PyDict_SendEvent(int watcher_bits,
5823
PyDict_WatchEvent event,
5824
PyDictObject *mp,
5825
PyObject *key,
5826
PyObject *value)
5827
{
5828
PyInterpreterState *interp = _PyInterpreterState_GET();
5829
for (int i = 0; i < DICT_MAX_WATCHERS; i++) {
5830
if (watcher_bits & 1) {
5831
PyDict_WatchCallback cb = interp->dict_state.watchers[i];
5832
if (cb && (cb(event, (PyObject*)mp, key, value) < 0)) {
5833
// We don't want to resurrect the dict by potentially having an
5834
// unraisablehook keep a reference to it, so we don't pass the
5835
// dict as context, just an informative string message. Dict
5836
// repr can call arbitrary code, so we invent a simpler version.
5837
PyObject *context = PyUnicode_FromFormat(
5838
"%s watcher callback for <dict at %p>",
5839
dict_event_name(event), mp);
5840
if (context == NULL) {
5841
context = Py_NewRef(Py_None);
5842
}
5843
PyErr_WriteUnraisable(context);
5844
Py_DECREF(context);
5845
}
5846
}
5847
watcher_bits >>= 1;
5848
}
5849
}
5850
5851