C API eqivalent of object.__new__

  • Thread starter Jacek Generowicz
  • Start date
J

Jacek Generowicz

How would one write object._new__(SomeType) in the C API ?

.... and what lies behind the following behaviour:
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 ?
 
J

Jacek Generowicz

Jacek Generowicz said:
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 ?
 
A

Alex Martelli

Jacek Generowicz said:
Jacek Generowicz said:
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
 
J

Jacek Generowicz

Jacek Generowicz said:
Jacek Generowicz said:
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.
 
A

Alex Martelli

Jacek Generowicz said:
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
 
J

Jacek Generowicz

You're welcome, but wrong (even though very kind!-) in assuming I
necessarily know every nook and cranny of Python's C API

I assume no such thing. I merely think that if you are not aware of
it, then the probability is sufficiently high that I haven't missed
something glaringly obvious just because of my incompetence in the
field of RingTFM ;-)

Also, if the PyObject_Call* approach is good enough for you, then the
chances are fairly high that it will be good enough for me.
"Make it work, make it right, make it fast", as Kent Beck says!-).

I've been quoting (a variation on) this to all and sundry, and
attributing it to KB for a few years now, but I've completely
forgotten where I came across it. Do you know its origin ?
 
A

Alex Martelli

Jacek Generowicz said:
I've been quoting (a variation on) this to all and sundry, and
attributing it to KB for a few years now, but I've completely
forgotten where I came across it. Do you know its origin ?

Yes, I asked Kent (I wanted to make sure I attributed it properly in my
book!) for the right wording and origin: the right (original) wording is
exactly as above, and Kent says that he's heard this motto throughout
his childhood from his father, an engineer (I dunno in which field, it
seemed to me that ``as Kent Beck often writes, quoting his father, ...''
is an excellent attribution, so I didn't check further;-).


Alex
 
J

Jacek Generowicz

Yes, I asked Kent (I wanted to make sure I attributed it properly in my
book!) for the right wording and origin: the right (original) wording is
exactly as above, and Kent says that he's heard this motto throughout
his childhood from his father, an engineer (I dunno in which field, it
seemed to me that ``as Kent Beck often writes, quoting his father, ...''
is an excellent attribution, so I didn't check further;-).

.... which means that I could have picked this up from just about
anything that Kent has written or even the Python Nutshell or
Cookbook?

Indeed, it's right there at the beginning of the _Optimization_
section of the Nutshell.
 
A

Alex Martelli

Jacek Generowicz said:
... which means that I could have picked this up from just about
anything that Kent has written or even the Python Nutshell or
Cookbook?

Indeed, it's right there at the beginning of the _Optimization_
section of the Nutshell.

Oh good -- I _would_ have been worried had it NOT been there;-).


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top