This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: add PyType_New()
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.3
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: barry, eric.snow, ncoghlan
Priority: normal Keywords: patch

Created on 2012-05-28 23:56 by eric.snow, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
PyType_New.diff eric.snow, 2012-05-28 23:56 review
Messages (6)
msg161820 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2012-05-28 23:56
Nick Coghlan suggested[1] exploring an easier spelling for "type(name, (), {})" in the C API.  I've attached a patch that adds a function that does so: _PyType_New().  It's "private" in the patch, but only because I am reticent to expand the API without solid feedback from the more experienced devs.

Even if I didn't get the implementation quite right (I'm relatively new to the C API), the patch at least demonstrates the concept.  :)


[1] http://mail.python.org/pipermail/python-ideas/2012-May/015281.html
msg161838 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2012-05-29 02:28
I realised that with the addition of types.new_class(), that's fairly easy to invoke from C (no harder than any other Python function, anyway). Thus, no need to duplicate the functionality directly in the C API.
msg161849 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2012-05-29 04:22
Presumably you mean something like this:

<examples>

PyObject *
PyType_New(PyObject *name, PyObject *bases, PyObject *ns)
{
    PyObject *type, *args, *newtype;
    PyInterpreterState *interp = PyThreadState_GET()->interp;
    PyObject *modules = interp->modules;
    PyObject *builtins = PyDict_GetItemString(modules, "builtins");
    _Py_IDENTIFIER(type);

    if (builtins == NULL)
        return NULL;
    type = _PyObject_GetAttrId(builtins, &PyId_type);
    if (type == NULL)
        return NULL;
    args = PyTuple_Pack(3, name, bases, ns);
    if (args == NULL)
        return NULL;
    newtype = PyObject_CallObject(type, args);
    Py_DECREF(args);
    return newtype;
}

or even:

PyObject *
PyType_New(PyObject *meta, PyObject *name, PyObject *bases, PyObject *ns)
{
    PyObject *args, *newtype;

    args = PyTuple_Pack(3, name, bases, ns);
    if (args == NULL)
        return NULL;
    newtype = PyObject_CallObject(type, args);
    Py_DECREF(args);
    return newtype;
}

and called with "PyType_New(&PyTypeObject, name, bases, ns)".

</examples>

If that's what you meant, I'm okay with that.  Otherwise please elaborate.  :)
msg161850 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2012-05-29 04:24
And unless there were some performance reason, I agree that the route I took in the patch is overkill.
msg161851 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2012-05-29 05:09
No, I mean no new C API at all. Anyone that wants to dynamically create a new type from C in 3.3 can already just write their own code to make the appropriate types.new_class() call:

http://docs.python.org/dev/library/types#types.new_class

A simple example, ignoring refcounting:

   types_mod = PyImport_ImportModule("types");
   new_class = PyObject_GetAttrString(types_mod, "new_class");
   new_type = PyObject_CallFunction(new_function, "s", "MyClass")

And assorted variations thereof using the different PyObject_GetAttr* and PyObject_Call* functions.
msg161855 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2012-05-29 05:20
Yeah, I'd meant that as an illustration of what I'd understood, rather than some other proposal for the C API.  types.new_class looks really handy.  Thanks for clarifying.
History
Date User Action Args
2022-04-11 14:57:30adminsetgithub: 59147
2012-05-30 21:13:10barrysetnosy: + barry
2012-05-29 05:20:52eric.snowsetmessages: + msg161855
2012-05-29 05:09:59ncoghlansetmessages: + msg161851
2012-05-29 04:24:34eric.snowsetmessages: + msg161850
2012-05-29 04:22:24eric.snowsetmessages: + msg161849
2012-05-29 02:28:35ncoghlansetstatus: open -> closed
resolution: rejected
messages: + msg161838
2012-05-29 01:44:27ncoghlansetmessages: - msg161829
2012-05-29 01:44:11ncoghlansetnosy: + ncoghlan
messages: + msg161829
2012-05-28 23:57:32eric.snowsettype: enhancement
components: + Interpreter Core
2012-05-28 23:56:23eric.snowcreate