C API: how to replace python number object in place?

S

Stephen Vavasis

If x is a C variable of type PyObject*, and I happen to know already that
the object is of a numeric type, say int, is there a way to change the
value of x in place to a different number? In the C/API documentation I
found routines to increment or decrement it in place, but I didn't find a
routine to simply replace its value by a different value. (I suppose I
could change it to the new value via an increment or decrement, but this
is ugly as well as being susceptible to overflow problems and roundoff
errors in the case of floats.)

Thanks,
Steve Vavasis
 
B

Benjamin Peterson

Stephen Vavasis said:
If x is a C variable of type PyObject*, and I happen to know already that
the object is of a numeric type, say int, is there a way to change the
value of x in place to a different number? In the C/API documentation I
found routines to increment or decrement it in place, but I didn't find a
routine to simply replace its value by a different value. (I suppose I
could change it to the new value via an increment or decrement, but this
is ugly as well as being susceptible to overflow problems and roundoff
errors in the case of floats.)

Even in the C-API, Python ints and longs are immutable. You can convert it to a
C int and work with it, otherwise you have to use the APIs which create new
objects: PyNumber_Add etc...
 
H

Hrvoje Niksic

If x is a C variable of type PyObject*, and I happen to know already
that the object is of a numeric type, say int, is there a way to
change the value of x in place to a different number? In the C/API
documentation I found routines to increment or decrement it in
place, but I didn't find a routine to simply replace its value by a
different value.

The only thing you can do is change x to point to a different object,
with the new desired value. That is how the "in place" number
modification functions work.

Which routines to increment or decrement the number are you referring
to, exactly?
 
S

Stephen Vavasis

In my previous posting, I inquired how to change a python numeric object
in place. Several people responded that this is not possible. Perhaps I
should explain the larger problem that I am trying to solve, and then the
solution will become apparent. I have a C routine R that invokes a Python
routine S repeatedly. This Python routine S takes three arguments, two
floats and an integer. I have read the documentation explaining how R can
use the C API to construct an argument list for invoking S. The issue
that baffles me is that, as far as I can tell, each time R invokes S
again, it needs to deallocate and reallocate the three arguments. It
would seem be much more efficient if R could create the argument list once
and then modify the values inside of it for each subsequent invocation of
S.

-- Steve Vavasis
 
C

Carl Banks

In my previous posting, I inquired how to change a python numeric object
in place.  Several people responded that this is not possible.  Perhaps I
should explain the larger problem that I am trying to solve, and then the
solution will become apparent.  I have a C routine R that invokes a Python
routine S repeatedly.  This Python routine S takes three arguments, two
floats and an integer.  I have read the documentation explaining how R can
use the C API to construct an argument list for invoking S.  The issue
that baffles me is that, as far as I can tell, each time R invokes S
again, it needs to deallocate and reallocate the three arguments.

That's correct.

(Well, not exactly: sometimes PyInt_New can use a pre-existing object
rather than to allocate a new one. Also ints and floats I think both
use arena allocation, meaning that Python gets big chunks and creates
objects out of that. It's not like malloc gets called for every
PyInt_New invokation. But those are implementation details. Just
trust that Python's been pretty well optimized by now to create new
int and float objects.)

It
would seem be much more efficient if R could create the argument list once
and then modify the values inside of it for each subsequent invocation of
S.

It really won't make that much of a difference. Python is creating
and destroying objects all the time. If you were to execute a line
like this in Python:

a = b + 2.0 / (0.5 * m * v * v)

It'd probably create and delete 5 intermediate number objects.
Whatever small gains you could make by eliminating a few object
allocations in your calling code would hardly be noticeable.


Carl Banks
 

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

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top