473,503 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retain reference to a struct

Hi All,

A C library I'm using has a number of functions that all require a
struct as an argument. The example module shows how to make a new
Python Object from C code and I've seen other posts that recommend this
way of doing it.

In this case though, it would seem easier if I could create the object
in the Python code. This would require storing a pointer to an
instance of the struct until a certain function is called.

I can get the pointer into the python code, but whenever I try to use
it to call another function, the module segfaults. Can anyone suggest
why this is?

static PyObject *
libpyq_PQconnectdb(PyObject *self, PyObject *args)
{
PGconn *conn = PyMem_New(PGconn, sizeof(PGconn *));
conn = (PGconn *)PQconnectdb((const char*)
PyString_AS_STRING(args));

printf("%p", conn);
return PyLong_FromVoidPtr(conn) ;
}

static PyObject *
libpyq_PQfinish(PyObject *self, PyObject *args)
{
printf("%p", args);
return 1;
}
import libpyq #works fine
conn = libpyq.PQconnectdb #conn now a pointer
libpyq.PQfinish(conn) #segfaults


I'm new to C but relatively experienced with Python. I have a sneaky
suspiscion there's a good reason for not doing it this way but I
haven't seen a good explanation of why not yet. If you know one,
please tell me.

Thanks,
Andy

Nov 2 '05 #1
1 1870
On Wed, 2005-11-02 at 16:23, an**************@yahoo.co.uk wrote:
Hi All,

A C library I'm using has a number of functions that all require a
struct as an argument. The example module shows how to make a new
Python Object from C code and I've seen other posts that recommend this
way of doing it.

In this case though, it would seem easier if I could create the object
in the Python code. This would require storing a pointer to an
instance of the struct until a certain function is called.

I can get the pointer into the python code, but whenever I try to use
it to call another function, the module segfaults. Can anyone suggest
why this is?

static PyObject *
libpyq_PQconnectdb(PyObject *self, PyObject *args)
{
PGconn *conn = PyMem_New(PGconn, sizeof(PGconn *));
conn = (PGconn *)PQconnectdb((const char*)
PyString_AS_STRING(args));

printf("%p", conn);
return PyLong_FromVoidPtr(conn) ;
}

static PyObject *
libpyq_PQfinish(PyObject *self, PyObject *args)
{
printf("%p", args);
return 1;
What exactly do you think you're returning here? The function
declaration says that you're supposed to return a pointer to a PyObject.
'1' is not likely to be a valid pointer to anything.
}
import libpyq #works fine
conn = libpyq.PQconnectdb #conn now a pointer
Are you sure that's the correct example code? As written, that line
doesn't call the PQconnectdb function. It assigns "conn" to be an
alternate name for the PQconnectdb function.
libpyq.PQfinish(conn) #segfaults

That's probably due to the fact that the python interpreter wants to
look up your return value for printing it, but you're returning a bogus
pointer.
I'm new to C but relatively experienced with Python. I have a sneaky
suspiscion there's a good reason for not doing it this way but I
haven't seen a good explanation of why not yet. If you know one,
please tell me.


The idea of passing around pointers as numbers is very unpythonic. There
is no guarantee that the number that's passed into PQfinish actually
came from a call to PQconnectdb. The user could pass in any integer and
(probably successfully) attempt to crash the system. You should really
wrap the C struct (or the pointer to the C struct) into a Python object
instead.

By the way, it looks like you're trying to write some sort of database
access module. The 'pq' looks suspiciously like it's for PostgreSQL. If
that's the case, can't you just use an existing module for connecting to
PostgreSQL?

HTH,

Carsten Haese.
Nov 2 '05 #2

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

Similar topics

2
2086
by: Agent Mulder | last post by:
Hi group, I try to get a reference to a template. In the template, a virtual function is declared. Later in the program, I inherit from classes created by the template. So the template is the...
6
2812
by: T Koster | last post by:
After a few years of programming C, I had come to believe that I finally knew how to correctly organise my structure definitions in header files for mutually dependent structures, but I find myself...
2
4870
by: theo | last post by:
Hi... I wish to extract the text content of an Xml file and assign it to DropDownList controls at runtime.I can get the Xml file text content into the DropDownList controls (Ex...if 5 Xml text...
5
2755
by: homsan toft | last post by:
Hi, I'm (still) trying to return a pair<const Key, T> from iterator dereference. So I defined a proxy class in the obvious way: template<class KeyT, class DataT> struct ref_proxy { typedef...
12
5367
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
4
1573
by: Bit byte | last post by:
Can I have a struct reference (i.e. a reference to a struct)?. I tried it and it compiles, but are there any gotchas I need to be aware of ?
7
15245
by: Michael B Allen | last post by:
I have a forward reference like: struct foo; int some_fn(struct foo *param); Because the parameter is a pointer the compiler is satisfied. But now I wan to change 'struct foo' to a...
3
9508
by: DaTurk | last post by:
I was just curious. I know that a struct is value type by definition, and will be placed on the stack, but what if you have a reference type in the struct, say a string, or an array. Where will...
11
3350
by: abhiM | last post by:
I have a struct that has an array in it. I need to assign space to the array in a function and pass the corresponding struct by reference to another function so that it can store values into the...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7084
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...
0
7278
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,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5013
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...
0
3167
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...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.