Skip to content

Commit

Permalink
Fix refcounting bug in module initialization
Browse files Browse the repository at this point in the history
PyModule_AddObject steals a reference,
so we have to Py_INCREF the values that we are passing to it
but also owned by the _zic_module_state.
  • Loading branch information
davisagli committed Aug 26, 2024
1 parent a44f50e commit f9a7458
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
7.0.2 (unreleased)
==================

- TBD
- Fix reference-counting bug in C module initialization (broken in 7.0).
(`#316 <https://github.com/zopefoundation/zope.interface/issues/316>`_)


7.0.1 (2024-08-06)
Expand Down
7 changes: 7 additions & 0 deletions src/zope/interface/_zope_interface_coptimizations.c
Original file line number Diff line number Diff line change
Expand Up @@ -2517,6 +2517,7 @@ _zic_module_exec(PyObject* module)
_zic_module_state* rec = _zic_state_init(module);

rec->adapter_hooks = PyList_New(0);
Py_INCREF(rec->adapter_hooks);
if (rec->adapter_hooks == NULL)
return -1;

Expand Down Expand Up @@ -2569,26 +2570,32 @@ _zic_module_exec(PyObject* module)
*/
sb_class = PyType_FromModuleAndSpec(module, &SB_type_spec, NULL);
if (sb_class == NULL) { return -1; }
Py_INCREF(sb_class);
rec->specification_base_class = TYPE(sb_class);

osd_class = PyType_FromModuleAndSpec(module, &OSD_type_spec, NULL);
if (osd_class == NULL) { return -1; }
Py_INCREF(osd_class);
rec->object_specification_descriptor_class = TYPE(osd_class);

cpb_class = PyType_FromModuleAndSpec(module, &CPB_type_spec, sb_class);
if (cpb_class == NULL) { return -1; }
Py_INCREF(cpb_class);
rec->class_provides_base_class = TYPE(cpb_class);

ib_class = PyType_FromModuleAndSpec(module, &IB_type_spec, sb_class);
if (ib_class == NULL) { return -1; }
Py_INCREF(ib_class);
rec->interface_base_class = TYPE(ib_class);

lb_class = PyType_FromModuleAndSpec(module, &LB_type_spec, NULL);
if (lb_class == NULL) { return -1; }
Py_INCREF(lb_class);
rec->lookup_base_class = TYPE(lb_class);

vb_class = PyType_FromModuleAndSpec(module, &VB_type_spec, lb_class);
if (vb_class == NULL) { return -1; }
Py_INCREF(vb_class);
rec->verifying_base_class = TYPE(vb_class);

#endif
Expand Down

0 comments on commit f9a7458

Please sign in to comment.