@@ -101,10 +101,6 @@ For removing nodes:
101
101
* _odict_find_node(od, key)
102
102
* _odict_keys_equal(od1, od2)
103
103
104
- Used, but specific to the linked-list implementation:
105
-
106
- * _odict_free_fast_nodes(od)
107
-
108
104
And here's a look at how the linked-list relates to the OrderedDict API:
109
105
110
106
============ === === ==== ==== ==== === ==== ===== ==== ==== === ==== === ===
@@ -378,7 +374,6 @@ tp_iter odict_iter
378
374
tp_dictoffset (offset)
379
375
tp_init odict_init
380
376
tp_alloc (repeated)
381
- tp_new odict_new
382
377
================= ================
383
378
384
379
================= ================
@@ -530,15 +525,6 @@ struct _odictnode {
530
525
#define _odict_FOREACH (od , node ) \
531
526
for (node = _odict_FIRST(od); node != NULL; node = _odictnode_NEXT(node))
532
527
533
- #define _odict_FAST_SIZE (od ) ((PyDictObject *)od)->ma_keys->dk_size
534
-
535
- static void
536
- _odict_free_fast_nodes (PyODictObject * od ) {
537
- if (od -> od_fast_nodes ) {
538
- PyMem_FREE (od -> od_fast_nodes );
539
- }
540
- }
541
-
542
528
/* Return the index into the hash table, regardless of a valid node. */
543
529
static Py_ssize_t
544
530
_odict_get_index_raw (PyODictObject * od , PyObject * key , Py_hash_t hash )
@@ -559,7 +545,8 @@ _odict_get_index_raw(PyODictObject *od, PyObject *key, Py_hash_t hash)
559
545
560
546
/* Replace od->od_fast_nodes with a new table matching the size of dict's. */
561
547
static int
562
- _odict_resize (PyODictObject * od ) {
548
+ _odict_resize (PyODictObject * od )
549
+ {
563
550
Py_ssize_t size , i ;
564
551
_ODictNode * * fast_nodes , * node ;
565
552
@@ -585,7 +572,7 @@ _odict_resize(PyODictObject *od) {
585
572
}
586
573
587
574
/* Replace the old fast nodes table. */
588
- _odict_free_fast_nodes (od );
575
+ PyMem_FREE (od -> od_fast_nodes );
589
576
od -> od_fast_nodes = fast_nodes ;
590
577
od -> od_fast_nodes_size = size ;
591
578
od -> od_resize_sentinel = ((PyDictObject * )od )-> ma_keys ;
@@ -623,6 +610,7 @@ _odict_find_node_hash(PyODictObject *od, PyObject *key, Py_hash_t hash)
623
610
index = _odict_get_index (od , key , hash );
624
611
if (index < 0 )
625
612
return NULL ;
613
+ assert (od -> od_fast_nodes != NULL );
626
614
return od -> od_fast_nodes [index ];
627
615
}
628
616
@@ -640,6 +628,7 @@ _odict_find_node(PyODictObject *od, PyObject *key)
640
628
index = _odict_get_index (od , key , hash );
641
629
if (index < 0 )
642
630
return NULL ;
631
+ assert (od -> od_fast_nodes != NULL );
643
632
return od -> od_fast_nodes [index ];
644
633
}
645
634
@@ -684,7 +673,8 @@ _odict_add_new_node(PyODictObject *od, PyObject *key, Py_hash_t hash)
684
673
Py_DECREF (key );
685
674
return -1 ;
686
675
}
687
- else if (od -> od_fast_nodes [i ] != NULL ) {
676
+ assert (od -> od_fast_nodes != NULL );
677
+ if (od -> od_fast_nodes [i ] != NULL ) {
688
678
/* We already have a node for the key so there's no need to add one. */
689
679
Py_DECREF (key );
690
680
return 0 ;
@@ -763,6 +753,7 @@ _odict_clear_node(PyODictObject *od, _ODictNode *node, PyObject *key,
763
753
if (i < 0 )
764
754
return PyErr_Occurred () ? -1 : 0 ;
765
755
756
+ assert (od -> od_fast_nodes != NULL );
766
757
if (node == NULL )
767
758
node = od -> od_fast_nodes [i ];
768
759
assert (node == od -> od_fast_nodes [i ]);
@@ -783,8 +774,10 @@ _odict_clear_nodes(PyODictObject *od)
783
774
{
784
775
_ODictNode * node , * next ;
785
776
786
- _odict_free_fast_nodes (od );
777
+ PyMem_FREE (od -> od_fast_nodes );
787
778
od -> od_fast_nodes = NULL ;
779
+ od -> od_fast_nodes_size = 0 ;
780
+ od -> od_resize_sentinel = NULL ;
788
781
789
782
no
F438
de = _odict_FIRST (od );
790
783
_odict_FIRST (od ) = NULL ;
@@ -887,7 +880,7 @@ static PyObject *
887
880
odict_sizeof (PyODictObject * od )
888
881
{
889
882
Py_ssize_t res = _PyDict_SizeOf ((PyDictObject * )od );
890
- res += sizeof (_ODictNode * ) * _odict_FAST_SIZE ( od ) ; /* od_fast_nodes */
883
+ res += sizeof (_ODictNode * ) * od -> od_fast_nodes_size ; /* od_fast_nodes */
891
884
if (!_odict_EMPTY (od )) {
892
885
res += sizeof (_ODictNode ) * PyODict_SIZE (od ); /* linked-list */
893
886
}
@@ -1175,12 +1168,10 @@ PyDoc_STRVAR(odict_clear__doc__,
1175
1168
"od.clear() -> None. Remove all items from od." );
1176
1169
1177
1170
static PyObject *
1178
- odict_clear (register PyODictObject * od )
1171
+ odict_clear (register PyODictObject * od , PyObject * Py_UNUSED ( ignored ) )
1179
1172
{
1180
1173
PyDict_Clear ((PyObject * )od );
1181
1174
_odict_clear_nodes (od );
1182
- if (_odict_resize (od ) < 0 )
1183
- return NULL ;
1184
1175
Py_RETURN_NONE ;
1185
1176
}
1186
1177
@@ -1484,13 +1475,10 @@ odict_traverse(PyODictObject *od, visitproc visit, void *arg)
1484
1475
static int
1485
1476
odict_tp_clear (PyODictObject * od )
1486
1477
{
1487
- PyObject * res ;
1488
1478
Py_CLEAR (od -> od_inst_dict );
1489
1479
Py_CLEAR (od -> od_weakreflist );
1490
- res = odict_clear (od );
1491
- if (res == NULL )
1492
- return -1 ;
1493
- Py_DECREF (res );
1480
+ PyDict_Clear ((PyObject * )od );
1481
+ _odict_clear_nodes (od );
1494
1482
return 0 ;
1495
1483
}
1496
1484
@@ -1565,27 +1553,6 @@ odict_init(PyObject *self, PyObject *args, PyObject *kwds)
1565
1553
}
1566
1554
}
1567
1555
1568
- /* tp_new */
1569
-
1570
- static PyObject *
1571
- odict_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
1572
- {
1573
- PyODictObject * od ;
1574
-
1575
- od = (PyODictObject * )PyDict_Type .tp_new (type , args , kwds );
1576
- if (od == NULL )
1577
- return NULL ;
1578
-
1579
- /* type constructor fills the memory with zeros (see
1580
- PyType_GenericAlloc()), there is no need to set them to zero again */
1581
- if (_odict_resize (od ) < 0 ) {
1582
- Py_DECREF (od );
1583
- return NULL ;
1584
- }
1585
-
1586
- return (PyObject * )od ;
1587
- }
1588
-
1589
1556
/* PyODict_Type */
1590
1557
1591
1558
PyTypeObject PyODict_Type = {
@@ -1626,7 +1593,7 @@ PyTypeObject PyODict_Type = {
1626
1593
offsetof(PyODictObject , od_inst_dict ), /* tp_dictoffset */
1627
1594
(initproc )odict_init , /* tp_init */
1628
1595
PyType_GenericAlloc , /* tp_alloc */
1629
- ( newfunc ) odict_new , /* tp_new */
1596
+ 0 , /* tp_new */
1630
1597
0 , /* tp_free */
1631
1598
};
1632
1599
@@ -1636,8 +1603,9 @@ PyTypeObject PyODict_Type = {
1636
1603
*/
1637
1604
1638
1605
PyObject *
1639
- PyODict_New (void ) {
1640
- return odict_new (& PyODict_Type , NULL , NULL );
1606
+ PyODict_New (void )
1607
+ {
1608
+ return PyDict_Type .tp_new (& PyODict_Type , NULL , NULL );
1641
1609
}
1642
1610
1643
1611
static int
0 commit comments