By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,646 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,646 IT Pros & Developers. It's quick & easy.

C API eqivalent of object.__new__

P: n/a
How would one write object._new__(SomeType) in the C API ?

.... and what lies behind the following behaviour:
object.__new__(T)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object.__new__(T) is not safe, use T.__new__()

where T refers to almost any fundamental Python type ?
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Jacek Generowicz <ja**************@cern.ch> writes:
How would one write object._new__(SomeType) in the C API ?


PyInstance_NewRaw(PyObject *class, [PyObject* dict])

seems to be the way to do this for classic classes. How would one do
it for new-style classes?

Also, how would one create a new-style class "dynamically" in the C
API? IOW, what's the new-style equivalent of PyClass_New? or, put
another way, how would you write

type(name, (), dict)

in the C API ?
Jul 18 '05 #2

P: n/a
Jacek Generowicz <ja**************@cern.ch> wrote:
Jacek Generowicz <ja**************@cern.ch> writes:
How would one write object._new__(SomeType) in the C API ?
PyInstance_NewRaw(PyObject *class, [PyObject* dict])

seems to be the way to do this for classic classes. How would one do
it for new-style classes?


You mean something like:
PyObject_CallMethod( (PyObject*)&PyBaseObject_Type,
"__new__", "O", clas);
?
Also, how would one create a new-style class "dynamically" in the C
API? IOW, what's the new-style equivalent of PyClass_New? or, put
another way, how would you write

type(name, (), dict)

in the C API ?


You mean something like:
PyObject_CallFunctionObjArgs(
(PyObject*)&PyType_Type,
classname, classbases, classdict,
NULL);
?

There may be faster ways, but I generally start writing extensions by
keeping the C code as "close" to equivalent Python code as I can, which
means lots of use of PyObject_... level functions. If and when I need
to squeeze out the last few cycles, I may dig into those for
optimization purposes, but what was that quip about premature
optimization, again....?-)
Alex
Jul 18 '05 #3

P: n/a
al*****@yahoo.com (Alex Martelli) writes:
Jacek Generowicz <ja**************@cern.ch> wrote:
Jacek Generowicz <ja**************@cern.ch> writes:
How would one write object._new__(SomeType) in the C API ?


PyInstance_NewRaw(PyObject *class, [PyObject* dict])

seems to be the way to do this for classic classes. How would one do
it for new-style classes?


You mean something like:
PyObject_CallMethod( (PyObject*)&PyBaseObject_Type,
"__new__", "O", clas);
?
Also, how would one create a new-style class "dynamically" in the C
API? IOW, what's the new-style equivalent of PyClass_New? or, put
another way, how would you write

type(name, (), dict)

in the C API ?


You mean something like:
PyObject_CallFunctionObjArgs(
(PyObject*)&PyType_Type,
classname, classbases, classdict,
NULL);
?


I was indeed starting to think about PyObject_Call* approaches to
this, but didn't want to go down that path without making sure that I
haven't missed some function that was specifically designed to do it.

If _you_ don't know of one, then I'll take that to mean that I
shouldn't waste more time looking for it :-)

Thanks.
Jul 18 '05 #4

P: n/a
Jacek Generowicz <ja**************@cern.ch> wrote:
...
I was indeed starting to think about PyObject_Call* approaches to
this, but didn't want to go down that path without making sure that I
haven't missed some function that was specifically designed to do it.

If _you_ don't know of one, then I'll take that to mean that I
shouldn't waste more time looking for it :-)

Thanks.


You're welcome, but wrong (even though very kind!-) in assuming I
necessarily know every nook and cranny of Python's C API -- more often
than not, I focus on getting the job done, which means using the easiest
way (generally PyObject_somethingorother), then possibly go back looking
for potential acceleration, if and only if I need to optimize. "Make it
work, make it right, make it fast", as Kent Beck says!-). I've never
needed to optimize this specific case, so I wouldn't be surprised to
learn about better and faster approaches...
Alex
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.