Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code is not matched docs (typeobject.c) #96418

Open
twoone-3 opened this issue Aug 30, 2022 · 5 comments
Open

Code is not matched docs (typeobject.c) #96418

twoone-3 opened this issue Aug 30, 2022 · 5 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@twoone-3
Copy link

twoone-3 commented Aug 30, 2022

typeobject.c

    /* Set type.__module__ */
    r = _PyDict_ContainsId(type->tp_dict, &PyId___module__);
    if (r < 0) {
        goto fail;
    }
    if (r == 0) {
        s = strrchr(spec->name, '.');
        if (s != NULL) {
            modname = PyUnicode_FromStringAndSize(
                    spec->name, (Py_ssize_t)(s - spec->name));
            if (modname == NULL) {
                goto fail;
            }
            r = _PyDict_SetItemId(type->tp_dict, &PyId___module__, modname);
            Py_DECREF(modname);
            if (r != 0)
                goto fail;
        } else {
            if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
                    "builtin type %.200s has no __module__ attribute",
                    spec->name))
                goto fail;
        }
    }

https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_name
Dynamic types are still checkpointed and the document does not match the code

@twoone-3 twoone-3 added the type-bug An unexpected behavior, bug, or error label Aug 30, 2022
@ericvsmith
Copy link
Member

What change are you proposing. It’s not clear to me what the problem is.

@twoone-3
Copy link
Author

twoone-3 commented Aug 30, 2022

When I register a dynamic type and use a plain type name such as 'abc', it reports a warning DepracationWarning,the abc class has not module attribute
image

@ericvsmith
Copy link
Member

Please provide a complete example that we can run.

@twoone-3
Copy link
Author

twoone-3 commented Aug 30, 2022

inline PyObject* makeType() {
  PyType_Slot slots[] = {{0, nullptr}};
  PyType_Spec spec{"static_property", PyProperty_Type.tp_basicsize, PyProperty_Type.tp_itemsize,
                   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE, slots};
  PyObject* type = PyType_FromSpec(&spec);
  return type;
}

@twoone-3
Copy link
Author

From docs, the name should have not '.', but in this example, it throws a DepracationWarning.
So I read the source code, #96418 (comment)
It parse '.' in PyType_FromSpec, but docs says that dynamic_type don't need add module_name. in front of classname

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants