473,809 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C Extension - return an array of longs or pointer?

Hi,
I have been posting about writing a C extension for Python...so far,
so good. At least for the "simple" functions that I need to wrap.

Ok, my c function looks like...

MY_NUM *doNumberStuff( const char *in, const char *x) { ... }

MY_NUM is defined as, typedef unsigned long MY_NUM; (not sure if that
matters, or can i just create a wrapper which handles longs?)

anyhow..for my wrapper I have this..

static PyObject *wrap_doNumberS tuff(PyObject *self, PyObject args) {
char *in = 0;
char *x = 0;
long *result = 0;
int ok = PyArg_ParseTupl e(args, "ss", &in, &x);
if (!ok) return 0;

result = doNumberStuff(i n, x);

return Py_BuildValue(" l", result);
}

....my question is...in the c code, result is a pointer to an array of
longs, how can I get the returned result to be a list or something
similar to an array in Python?

....I also have a function which returns a character array (denoted by a
char *)...would it work the same as the previous question?

Thanks!!

Oct 12 '05 #1
6 6325
All the veteran programmers out there can correct me, but the way I did
it in my extension was this:

static PyObject *wrap_doNumberS tuff(PyObject* self, PyObject* args)
{
char* in = 0;
char* x = 0;
long* result = 0;
int i = 0;
PyObject* py = PyTuple_New()
int ok = PyArg_ParseTupl e(args,"ss",&in , &x);
if(!ok) return NULL;

result = doNumberStuff(i n,x):
len = sizeof(result)/sizeof(long)
for(i;i < len; i++)
PyTuple_SET_ITE M(py, i,Py_BuildValue ("l",*result[i])
}

Simple enough idea...i'm not quite sure if I've done everything
correctly with the pointers, but I'm sure you can figure that out, the
algorithm is simple enough.
Hi,
I have been posting about writing a C extension for Python...so far,
so good. At least for the "simple" functions that I need to wrap.

Ok, my c function looks like...

MY_NUM *doNumberStuff( const char *in, const char *x) { ... }

MY_NUM is defined as, typedef unsigned long MY_NUM; (not sure if that
matters, or can i just create a wrapper which handles longs?)

anyhow..for my wrapper I have this..

static PyObject *wrap_doNumberS tuff(PyObject *self, PyObject args) {
char *in = 0;
char *x = 0;
long *result = 0;
int ok = PyArg_ParseTupl e(args, "ss", &in, &x);
if (!ok) return 0;

result = doNumberStuff(i n, x);

return Py_BuildValue(" l", result);
}

...my question is...in the c code, result is a pointer to an array of
longs, how can I get the returned result to be a list or something
similar to an array in Python?

...I also have a function which returns a character array (denoted by a
char *)...would it work the same as the previous question?

Thanks!!

----== Posted via Newsgroups.com - Usenet Access to over 100,000 Newsgroups ==----
Get Anonymous, Uncensored, Access to West and East Coast Server Farms!
----== Highest Retention and Completion Rates! HTTP://WWW.NEWSGROUPS.COM ==----
Oct 12 '05 #2
I'm sorry...I just woke up and forgot my C...must have left it in the
Coffee...Anyway , i made a few mistakes (can't initialize blank
tuple...functio n should return a value, lol).

static PyObject* wrap_doNumberSt uff(PyObject* self, PyObject* args)
{
char* in = 0;
char* x = 0;
long* result = 0;
int i = 0;
PyObject* py = NULL;
if(!PyArg_Parse Tuple(args,"ss" ,&in,&x) return NULL;

result = doNumberStuff(i n,x);
len = sizeof(result)/sizeof(long);
py = PyTuple_New(len );
for(i; i < len; i++)
PyTuple_SET_ITE M(py, i, Py_BuildValue(" l",*result[i]);

return py;
}

Additionally, the Python/C api in the docs tells you all of these nifty
little abstract layer functions that you can call from your extension.
All the veteran programmers out there can correct me, but the way I did
it in my extension was this:

static PyObject *wrap_doNumberS tuff(PyObject* self, PyObject* args)
{
char* in = 0;
char* x = 0;
long* result = 0;
int i = 0;
PyObject* py = PyTuple_New()
int ok = PyArg_ParseTupl e(args,"ss",&in , &x);
if(!ok) return NULL;

result = doNumberStuff(i n,x):
len = sizeof(result)/sizeof(long)
for(i;i < len; i++)
PyTuple_SET_ITE M(py, i,Py_BuildValue ("l",*result[i])
}

Simple enough idea...i'm not quite sure if I've done everything
correctly with the pointers, but I'm sure you can figure that out, the
algorithm is simple enough.

----== Posted via Newsgroups.com - Usenet Access to over 100,000 Newsgroups ==----
Get Anonymous, Uncensored, Access to West and East Coast Server Farms!
----== Highest Retention and Completion Rates! HTTP://WWW.NEWSGROUPS.COM ==----
Oct 12 '05 #3
Interesting...t hanks. Any good tutorials out there, other than the
python doc for ext?
thanks.

Brandon K wrote:
All the veteran programmers out there can correct me, but the way I did
it in my extension was this:

static PyObject *wrap_doNumberS tuff(PyObject* self, PyObject* args)
{
char* in = 0;
char* x = 0;
long* result = 0;
int i = 0;
PyObject* py = PyTuple_New()
int ok = PyArg_ParseTupl e(args,"ss",&in , &x);
if(!ok) return NULL;

result = doNumberStuff(i n,x):
len = sizeof(result)/sizeof(long)
for(i;i < len; i++)
PyTuple_SET_ITE M(py, i,Py_BuildValue ("l",*result[i])
}

Simple enough idea...i'm not quite sure if I've done everything
correctly with the pointers, but I'm sure you can figure that out, the
algorithm is simple enough.
Hi,
I have been posting about writing a C extension for Python...so far,
so good. At least for the "simple" functions that I need to wrap.

Ok, my c function looks like...

MY_NUM *doNumberStuff( const char *in, const char *x) { ... }

MY_NUM is defined as, typedef unsigned long MY_NUM; (not sure if that
matters, or can i just create a wrapper which handles longs?)

anyhow..for my wrapper I have this..

static PyObject *wrap_doNumberS tuff(PyObject *self, PyObject args) {
char *in = 0;
char *x = 0;
long *result = 0;
int ok = PyArg_ParseTupl e(args, "ss", &in, &x);
if (!ok) return 0;

result = doNumberStuff(i n, x);

return Py_BuildValue(" l", result);
}

...my question is...in the c code, result is a pointer to an array of
longs, how can I get the returned result to be a list or something
similar to an array in Python?

...I also have a function which returns a character array (denoted by a
char *)...would it work the same as the previous question?

Thanks!!

----== Posted via Newsgroups.com - Usenet Access to over 100,000 Newsgroups ==----
Get Anonymous, Uncensored, Access to West and East Coast Server Farms!
----== Highest Retention and Completion Rates! HTTP://WWW.NEWSGROUPS.COM ==----


Oct 12 '05 #4
Brandon K <pr***********@ yahoo.com> writes:
long* result = 0; [...] result = doNumberStuff(i n,x);
len = sizeof(result)/sizeof(long);


I don't think this will do what you appear to expect it to do.

Bernhard

--
Intevation GmbH http://intevation.de/
Skencil http://skencil.org/
Thuban http://thuban.intevation.org/
Oct 12 '05 #5
On 12 Oct 2005 04:46:34 -0700, Java and Swing <co*******@gmai l.com> wrote:
....
...my question is...in the c code, result is a pointer to an array of
longs, how can I get the returned result to be a list or something
similar to an array in Python?
RTFM. There are API functions to create an empty list [] and to append
objects to a list. PyList_somethin g(), I think.
...I also have a function which returns a character array (denoted by a
char *)...would it work the same as the previous question?


You might want to return this as a Python string, even if it's just random
octet data garbage. Depends on what that data really represents, but it's a
common idiom, and modules like struct and array expect their data to be
strings. Note that Python strings may contain NUL bytes.

/Jorgen

--
// Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn!
Oct 12 '05 #6
Brandon K wrote:
PyTuple_SET_ITE M(py, i, Py_BuildValue(" l",*result[i]);


Using Py_BuildValue is overkill here. PyInt_FromLong will do just
as well.

Regards,
Martin
Oct 12 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1749
by: Letbetter, Jason | last post by:
I'm creating Python extensions for several c/c++ components. I'm using swig to create the extensions. The biggest challenge so far is working with the c args between Python and the Python extension. Is there a 3rd party library of extension helpers that assist in representing c-types between Python and c? Are there any tips and tricks? Here are some specific scenarios I am running into. 1) Refrencing and derefrencing: For...
8
1798
by: Russell E. Owen | last post by:
I'm writing a C extension for numarray and am puzzled about the idiom for error handling. The documentation seems to say one should always decref an array after calling NA_InputArray, etc., to convert numarray args to C arrays. However, the example and also the numarray code suggests that it's OK to return early via (for example) PyErr_Format without doing the DECREF. For example, I have appended a very abbreviated version of the...
1
2263
by: sugaray | last post by:
I don't get why the correct way to allocate memory space for 3d-array should be like this in C++: long (*ptr); ptr=new long; and when deallocation delete ptr;
10
2248
by: Java and Swing | last post by:
I need to write an extension for a C function so that I can call it from python. C code (myapp.c) ====== typedef unsigned long MY_LONG; char *DoStuff(char *input, MY_LONG *x) { ... } so you pass in a string and an array of MY_LONGS...such as
2
2883
by: Richard L Rosenheim | last post by:
I'm converting some routines from C to C#. The code involves having arrays of bytes which are passed to a function as an array of longs. Well, technically the C code has a pointer to an array of char which is passed to the function which treats the pointer as a pointer to an array of longs. I would appreciate if someone could direct me to an example, article, etc. that discuss how to convert such code to C#. TIA,
3
1447
by: rimmer | last post by:
I'm writing an extension module in C in which I'm passing an array of floats from C to python. The code below illustrates a simple C function designed to output an array of floats. --------- extTest.c --------- #include <stdio.h> double *testArray(int nsamp) {
4
1957
by: Philip Semanchuk | last post by:
I'm writing a Python extension in C that wraps a function which takes a void * as a parameter. (The function is shmat() which attaches a chunk of shared memory to the process at the address supplied by the caller.) I would like to expose this function to Python, but I don't know how to define the interface. Specifically, when calling PyArg_ParseTuple(), what letter should I use to represent the pointer in the format string? The best idea...
0
1407
by: Philip Semanchuk | last post by:
On Oct 22, 2008, at 8:33 PM, Robert Kern wrote: I agree. To deny users of this module access to this param of shmat() would be design arrogance on my part. To do so would be to claim that I know better than everyone who might want to use my module. Using values other than NULL might be unwise (the man page I'm looking at states that clearly) but isn't one of the core design philosophies of Python "we're all consenting adults"?
3
3047
by: Keith Thompson | last post by:
Victor <vhnguyenn@yahoo.comwrites: You're declaring an array of pointers to unsigned long long, but you're initializing the pointers with integer values. This is actually a constraint violation, and your compiler should have warned you about it. Your problem is either that you're invoking your compiler in some non-standard mode that inhibits the warnings, or getting warnings and not bothering to tell us about them.
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10633
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.