The second time we get to here (in com_test, compile.c, and when running python_d -O blah/blah/test_builtin.py, and test_b2.pyo doesn't exist): co = (PyObject *) icompile(CHILD(n, 0), c); if (co == NULL) { c->c_errors++; return; } symtable_exit_scope(c->c_symtable); if (co == NULL) { c->c_errors++; i = 255; closure = 0; } else { i = com_addconst(c, co); Py_DECREF(co); ************** HERE ********* closure = com_make_closure(c, (PyCodeObject *)co); } the refcount of co is 1 before we do the Py_DECREF. Everything else follows from that. In the failing 2nd time thru this code, com_addconst finds the thing already, so com_addconst doesn't boost the refcount above 1. The code appears a bit confused regardless (e.g., it checks for co==NULL twice, but it looks impossible for the second test to succeed).