Commit bb75624
authored
Implement cell comparison and repr (RustPython#8458)
The cell type filled neither the richcompare nor the repr slot, so
object's address-based defaults showed through: cell(1) == cell(1) was
False, ordering raised TypeError, and repr rendered
<cell object at 0x...> rather than <cell at 0x...: int object at 0x...>.
Compare cells by contents, with empty cells ordering before everything
else, and render CPython's repr for both the filled and empty cases.
The comparison fills the richcompare slot directly rather than going
through Comparable, whose cmp() can only answer with a bool. CPython
returns whatever PyObject_RichCompare produced, so a contained __eq__
that yields a non-bool must pass through untouched; coercing it would
also call __bool__ and surface exceptions CPython never raises. The
empty-cell branch still answers with a bool, so both arms are needed and
the slot returns Either.
The repr truncates the contained type name the way "%.80s" does: at most
80 bytes, dropping a character the cut would leave incomplete.
Mark the type unhashable. Content-based equality combined with the
inherited identity hash would break the hash/eq contract, and hashing the
contents is not possible either because cell_contents is writable.
CPython gets this implicitly, because defining tp_richcompare suppresses
tp_hash inheritance.
Reference: CPython Objects/cellobject.c, cell_richcompare and cell_repr.
Assisted-by: Claude Code:claude-opus-51 parent 9bab06c commit bb75624
3 files changed
Lines changed: 45 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
432 | 432 | | |
433 | 433 | | |
434 | 434 | | |
435 | | - | |
436 | 435 | | |
437 | 436 | | |
438 | 437 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
237 | 237 | | |
238 | 238 | | |
239 | 239 | | |
240 | | - | |
241 | 240 | | |
242 | 241 | | |
243 | 242 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| |||
1507 | 1507 | | |
1508 | 1508 | | |
1509 | 1509 | | |
1510 | | - | |
| 1510 | + | |
1511 | 1511 | | |
1512 | 1512 | | |
1513 | 1513 | | |
| |||
1530 | 1530 | | |
1531 | 1531 | | |
1532 | 1532 | | |
1533 | | - | |
| 1533 | + | |
1534 | 1534 | | |
| 1535 | + | |
| 1536 | + | |
| 1537 | + | |
| 1538 | + | |
| 1539 | + | |
| 1540 | + | |
| 1541 | + | |
| 1542 | + | |
| 1543 | + | |
| 1544 | + | |
| 1545 | + | |
| 1546 | + | |
| 1547 | + | |
| 1548 | + | |
| 1549 | + | |
| 1550 | + | |
| 1551 | + | |
| 1552 | + | |
1535 | 1553 | | |
1536 | 1554 | | |
1537 | 1555 | | |
| |||
1561 | 1579 | | |
1562 | 1580 | | |
1563 | 1581 | | |
| 1582 | + | |
| 1583 | + | |
| 1584 | + | |
| 1585 | + | |
| 1586 | + | |
| 1587 | + | |
| 1588 | + | |
| 1589 | + | |
| 1590 | + | |
| 1591 | + | |
| 1592 | + | |
| 1593 | + | |
| 1594 | + | |
| 1595 | + | |
| 1596 | + | |
| 1597 | + | |
| 1598 | + | |
| 1599 | + | |
| 1600 | + | |
| 1601 | + | |
| 1602 | + | |
| 1603 | + | |
| 1604 | + | |
| 1605 | + | |
1564 | 1606 | | |
1565 | 1607 | | |
1566 | 1608 | | |
| |||
0 commit comments