Visitar URL original
Support PyType_GetModuleName and PyType_GetFullyQualifiedName? · Issue #188 · python/pythoncapi-compat · GitHub
Skip to content

Support PyType_GetModuleName and PyType_GetFullyQualifiedName? #188

Description

@skirpichev

Those currently marked as unsupported, despite being part of the Limited API. And it looks, according to the docs, implementations should be possible. Or I miss something obvious?

PyType_GetModuleName

Return the type's module name. Equivalent to getting the :attr:type.__module__ attribute.

Possible implementation:

PyObject *
PyType_GetModuleName(PyTypeObject *type)
{
    return PyObject_GetAttrString((PyObject *)type, "__module__");
}

PyType_GetFullyQualifiedName

Return the type's fully qualified name. Equivalent to f"{type.__module__}.{type.__qualname__}", or :attr:type.__qualname__ if :attr:type.__module__ is not a string or is equal to "builtins".

(Looking on the actual code, it seems that docs are inaccurate as the alternative happens also if a string is equal to __main__.)

Possible implementation:

PyObject *
PyType_GetFullyQualifiedName(PyTypeObject *type)
{   
    PyObject *qualname = PyType_GetQualName(type);

    if (qualname == NULL) {
        return NULL;
    }

    PyObject *module = PyType_GetModuleName(type);

    if (module == NULL) {
        Py_DECREF(qualname);
        return NULL;
    }

    PyObject *result;

    if (PyUnicode_Check(module)
        && !PyUnicode_EqualToUTF8(module, "builtins")
        && !PyUnicode_EqualToUTF8(module, "__main__"))
    {   
        result = PyUnicode_FromFormat("%U.%U", module, qualname);
    }
    else {
        result = Py_NewRef(qualname);
    }
    Py_DECREF(module);
    Py_DECREF(qualname);
    return result;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions