@@ -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
================= ================
@@ -522,15 +517,6 @@ struct _odictnode {
522
517
#define _odict_FOREACH (od , node ) \
523
518
for (node = _odict_FIRST(od); node != NULL; node = _odictnode_NEXT(node))
524
519
525
- #define _odict_FAST_SIZE (od ) ((PyDictObject *)od)->ma_keys->dk_size
526
-
527
- static void
528
- _odict_free_fast_nodes (PyODictObject * od ) {
529
- if (od -> od_fast_nodes ) {
530
- PyMem_FREE (od -> od_fast_nodes );
531
- }
532
- }
533
-
534
520
/* Return the index into the hash table, regardless of a valid node. */
535
521
static Py_ssize_t
536
522
_odict_get_index_raw (PyODictObject * od , PyObject * key , Py_hash_t hash )
@@ -551,7 +537,8 @@ _odict_get_index_raw(PyODictObject *od, PyObject *key, Py_hash_t hash)
551
537
552
538
/* Replace od->od_fast_nodes with a new table matching the size of dict's. */
553
539
static int
554
- _odict_resize (PyODictObject * od ) {
540
+ _odict_resize (PyODictObject * od )
541
+ {
555
542
Py_ssize_t size , i ;
556
543
_ODictNode * * fast_nodes , * node ;
557
544
@@ -577,7 +564,7 @@ _odict_resize(PyODictObject *od) {
577
564
}
578
565
579
566
/* Replace the old fast nodes table. */
580
- _odict_free_fast_nodes (od );
567
+ PyMem_FREE (od -> od_fast_nodes );
581
568
od -> od_fast_nodes = fast_nodes ;
582
569
od -> od_fast_nodes_size = size ;
583
570
od -> od_resize_sentinel = ((PyDictObject * )od )-> ma_keys ;
@@ -615,6 +602,7 @@ _odict_find_node_hash(PyODictObject *od, PyObject *key, Py_hash_t hash)
615
602
index = _odict_get_index (od , key , hash );
616
603
if (index < 0 )
617
604
return NULL ;
605
+ assert (od -> od_fast_nodes != NULL );
618
606
return od -> od_fast_nodes [index ];
619
607
}
620
608
@@ -632,6 +620,7 @@ _odict_find_node(PyODictObject *od, PyObject *key)
632
620
index = _odict_get_index (od , key , hash );
633
621
if (index < 0 )
634
622
return NULL ;
623
+ assert (od -> od_fast_nodes != NULL );
635
624
return od -> od_fast_nodes [index ];
636
625
}
637
626
@@ -676,7 +665,8 @@ _odict_add_new_node(PyODictObject *od, PyObject *key, Py_hash_t hash)
676
665
Py_DECREF (key );
677
666
return -1 ;
678
667
}
679
- else if (od -> od_fast_nodes [i ] != NULL ) {
668
+ assert (od -> od_fast_nodes != NULL );
669
+ if (od -> od_fast_nodes [i ] != NULL ) {
680
670
/* We already have a node for the key so there's no need to add one. */
681
671
Py_DECREF (key );
682
672
return 0 ;
@@ -755,6 +745,7 @@ _odict_clear_node(PyODictObject *od, _ODictNode *node, PyObject *key,
755
745
if (i < 0 )
756
746
return PyErr_Occurred () ? -1 : 0 ;
757
747
748
+ assert (od -> od_fast_nodes != NULL );
758
749
if (node == NULL )
759
750
node = od -> od_fast_nodes [i ];
760
751
assert (node == od -> od_fast_nodes [i ]);
@@ -775,8 +766,10 @@ _odict_clear_nodes(PyODictObject *od)
775
766
{
776
767
_ODictNode * node , * next ;
777
768
778
- _odict_free_fast_nodes (od );
769
+ PyMem_FREE (od -> od_fast_nodes );
779
770
od -> od_fast_nodes = NULL ;
771
+ od -> od_fast_nodes_size = 0 ;
772
+ od -> od_resize_sentinel = NULL ;
780
773
781
774
node = _odict_FIRST (od );
782
775
_odict_FIRST (od ) = NULL ;
@@ -941,7 +934,7 @@ static PyObject *
941
934
odict_sizeof (PyODictObject * od )
942
935
{
943
936
Py_ssize_t res = _PyDict_SizeOf ((PyDictObject * )od );
944
- res += sizeof (_ODictNode * ) * _odict_FAST_SIZE ( od ) ; /* od_fast_nodes */
937
+ res += sizeof (_ODictNode * ) * od -> od_fast_nodes_size ; /* od_fast_nodes */
945
938
if (!_odict_EMPTY (od )) {
946
939
res += sizeof (_ODictNode ) * PyODict_SIZE (od ); /* linked-list */
947
940
}
@@ -1231,12 +1224,10 @@ PyDoc_STRVAR(odict_clear__doc__,
1231
1224
"od.clear() -> None. Remove all items from od." );
1232
1225
1233
1226
static PyObject *
1234
- odict_clear (register PyODictObject * od )
1227
+ odict_clear (register PyODictObject * od , PyObject * Py_UNUSED ( ignored ) )
1235
1228
{
1236
1229
PyDict_Clear ((PyObject * )od );
1237
1230
_odict_clear_nodes (od );
1238
- if (_odict_resize (od ) < 0 )
1239
- return NULL ;
1240
1231
Py_RETURN_NONE ;
1241
1232
}
1242
1233
@@ -1570,13 +1561,10 @@ odict_traverse(PyODictObject *od, visitproc visit, void *arg)
1570
1561
static int
1571
1562
odict_tp_clear (PyODictObject * od )
1572
1563
{
1573
- PyObject * res ;
1574
1564
Py_CLEAR (od -> od_inst_dict );
1575
1565
Py_CLEAR (od -> od_weakreflist );
1576
- res = odict_clear (od );
1577
- if (res == NULL )
1578
- return -1 ;
1579
- Py_DECREF (res );
1566
+ PyDict_Clear ((PyObject * )od );
1567
+ _odict_clear_nodes (od );
1580
1568
return 0 ;
1581
1569
}
1582
1570
@@ -1651,27 +1639,6 @@ odict_init(PyObject *self, PyObject *args, PyObject *kwds)
1651
1639
}
1652
1640
}
1653
1641
1654
- /* tp_new */
1655
-
1656
- static PyObject *
1657
- odict_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
1658
- {
1659
- PyODictObject * od ;
1660
-
1661
- od = (PyODictObject * )PyDict_Type .tp_new (type , args , kwds );
1662
- if (od == NULL )
1663
- return NULL ;
1664
-
1665
- /* type constructor fills the memory with zeros (see
1666
- PyType_GenericAlloc()), there is no need to set them to zero again */
1667
- if (_odict_resize (od ) < 0 ) {
1668
- Py_DECREF (od );
1669
- return NULL ;
1670
- }
1671
-
1672
- return (PyObject * )od ;
1673
- }
1674
-
1675
1642
/* PyODict_Type */
1676
1643
1677
1644
PyTypeObject PyODict_Type = {
@@ -1712,7 +1679,7 @@ PyTypeObject PyODict_Type = {
1712
1679
offsetof(PyODictObject , od_inst_dict ), /* tp_dictoffset */
1713
1680
(initproc )odict_init , /* tp_init */
1714
1681
PyType_GenericAlloc , /* tp_alloc */
1715
- ( newfunc ) odict_new , /* tp_new */
1682
+ 0 , /* tp_new */
1716
1683
0 , /* tp_free */
1717
1684
};
1718
1685
@@ -1722,8 +1689,9 @@ PyTypeObject PyODict_Type = {
1722
1689
*/
1723
1690
1724
1691
PyObject *
1725
- PyODict_New (void ) {
1726
- return odict_new (& PyODict_Type , NULL , NULL );
1692
+ PyODict_New (void )
1693
+ {
1694
+ return PyDict_Type .tp_new (& PyODict_Type , NULL , NULL );
1727
1695
}
1728
1696
1729
1697
static int
0 commit comments