[issue28040] compact dict : SystemError: returned NULL without setting an error.

INADA Naoki report at bugs.python.org
Mon Sep 12 20:20:38 EDT 2016


INADA Naoki added the comment:

> So what if we delete mp->ma_used == ix or use mp->ma_keys->dk_nentries == ix? Do we still have any case breaking the order?

Yes.  `mp->ma_used == ix` means no more guard about key ordering.

class C:
    ...

a, b = C()
a.a, a.b = 1, 2  # shared key order is [a, b]

# b has [a, b] pending slot too, and no guard when inserting  pending slot.
b.b, b.a = 3, 4  # continue to using sharing key

assert list(b.__dict__.keys()) == ['b', 'a'])  # AssertionError!

---
`mp->ma_keys->dk_nentries == ix` is nonsense.  It prohibits to inserting pending slot:

a, b = C()
a.a, a.b = 1, 2  # shared key order is [a, b]

# Since ma_keys is shared, b's dk_nentries == 2
b.a = 3  # ix = 0, dk_nentries = 2; stop using sharing keys.

---

To keep using sharing key, my idea is adding one more member to dict: mp->ma_values_end.

When inserting to pending or empty slot, check that `ix >= mp_ma_values_end` to ensure ordering
and `set mp->ma_values_end = ix+1` if it's OK.

a, b = C()  # Both of ma_values_end = 0
a.a, a.b = 1, 2  # shared key order is [a, b], a's ma_values_end = 2
b.b = 3          # ma_values_end (=0) <= ix (=1); OK; set ma_values_end = 2
b.a = 4          # ma_values_end (=2) >  ix (=0); NG; convert to combined table

But testing such an implementation detail is hard from pure Python. (No API for checking ma_values_end,
the dict is split or combined, and two dict share keys).

I don't know adding such hack is worth enough.
Dict is made by tons of hacks, you know.

I think new OrderedDict implementation based on new dict implementation is more worth.
If dict is far more efficient than OrderedDict, people may use dict even when OrderedDict should be used.
But I don't know it can be done after beta1.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28040>
_______________________________________


More information about the Python-bugs-list mailing list