Skip to content

_testcapi has invalid usage of PyType_GetName #136288

@ZeroIntensity

Description

@ZeroIntensity

Bug report

Bug description:

Spotted by @wsfulton in GH-136258. I'm making an issue so I can attach it to his PR.

PyType_GetName is used incorrectly in two places:

return PyErr_Format(
PyExc_TypeError,
"expected %s instance",
PyType_GetName(type));

return PyErr_Format(
PyExc_TypeError,
"type %s has no vectorcall offset",
PyType_GetName(type));

There are two problems:

  1. PyType_GetName returns a PyObject *, not a string (so the %s conversion is invalid).
  2. It also returns a strong reference, which isn't consumed by PyErr_Format and thus is leaked.

Fixes to tests generally don't matter, but I think that this one is worth fixing. People look to the source for examples, and considering this is one of our only examples using it, I'd rather not leave people to think that PyType_GetName returns a string or borrowed reference.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    testsTests in the Lib/test dirtopic-C-APItype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions