Skip to content

Instantly share code, notes, and snippets.

@devdanzin
Created March 18, 2026 10:06
Show Gist options
  • Select an option

  • Save devdanzin/d602d174ce2b8959759c94729a35c54e to your computer and use it in GitHub Desktop.

Select an option

Save devdanzin/d602d174ce2b8959759c94729a35c54e to your computer and use it in GitHub Desktop.
codeobject.c: code_richcompare swallows __eq__ errors

codeobject.c: code_richcompare swallows eq errors

Summary

At line 2608, !(-1) == 0 when PyObject_RichCompareBool returns -1 (error), so the error falls through instead of jumping to unequal. Exception left dangling on thread state → assertion failure.

Fix

Change if (!eq) to if (eq <= 0) at line 2609.

Reproducer

class BadStr(str):
    _armed = False
    def __eq__(self, other):
        if BadStr._armed and isinstance(other, str) and str.__eq__(self, other):
            raise RuntimeError("Poison!")
        return str.__eq__(self, other)
    def __hash__(self): return str.__hash__(self)

c1 = compile("pass", "test", "exec")
c2 = c1.replace(co_name=BadStr("poison"))
c3 = compile("pass", "poison", "exec")
BadStr._armed = True
c2 == c3  # assertion: !_PyErr_Occurred(tstate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment