Skip to content

Remove zero or negative reference counter check in PyObject_Print #98928

Closed as not planned
@MonadChains

Description

@MonadChains

Feature or enhancement

Remove the section of the PyObject_Print function that handles input objects that have a negative reference counter:

cpython/Objects/object.c

Lines 274 to 278 in 88297e2

if (Py_REFCNT(op) <= 0) {
Py_BEGIN_ALLOW_THREADS
fprintf(fp, "<refcnt %zd at %p>", Py_REFCNT(op), (void *)op);
Py_END_ALLOW_THREADS
}

Previous discussion

The PR #98749 that improves the coverage of PyObject_Print is blocked due to a failing pipeline. After investigation, the test that is failing is associated with the Address Sanitizer. In particular, it fails the test that covers the aforementioned section. It consists in passing an object (specifically a Python string) with its reference counter set to zero and comparing the output string to the expected result: https://github.com/MonadChains/cpython/blob/562b9d26db7e766741c954146d4423eda414d2f6/Modules/_testcapimodule.c#L2053-L2080
However, when the reference counter of the object goes to zero, it is deallocated, and the Address Sanitizer does not like it.
After the discussion in https://mail.python.org/archives/list/core-mentorship@python.org/thread/7BP55KL6VB3H2ZZ5JZP2NV6RX72HBUCC/, a possible solution to this is to clean up the function and remove this section entirely. Normally this kind of check is performed only in specific situations like for example _Py_ForgetReference or Py_DECREF so it seems a bit weird to have it in PyObject_Print.
Alternatively, another solution could be to remove the failing test from the PR and don't cover this section of the function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions