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.
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: Alyssa 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: Alyssa 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:30 | admin | set | github: 59147 |
| 2012年05月30日 21:13:10 | barry | set | nosy:
+ barry |
| 2012年05月29日 05:20:52 | eric.snow | set | messages: + msg161855 |
| 2012年05月29日 05:09:59 | ncoghlan | set | messages: + msg161851 |
| 2012年05月29日 04:24:34 | eric.snow | set | messages: + msg161850 |
| 2012年05月29日 04:22:24 | eric.snow | set | messages: + msg161849 |
| 2012年05月29日 02:28:35 | ncoghlan | set | status: open -> closed resolution: rejected messages: + msg161838 |
| 2012年05月29日 01:44:27 | ncoghlan | set | messages: - msg161829 |
| 2012年05月29日 01:44:11 | ncoghlan | set | nosy:
+ ncoghlan messages: + msg161829 |
| 2012年05月28日 23:57:32 | eric.snow | set | type: enhancement components: + Interpreter Core |
| 2012年05月28日 23:56:23 | eric.snow | create | |