473,651 Members | 2,787 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_PQconnec tdb(PyObject *self, PyObject *args)
{
PGconn *conn = PyMem_New(PGcon n, sizeof(PGconn *));
conn = (PGconn *)PQconnectdb(( const char*)
PyString_AS_STR ING(args));

printf("%p", conn);
return PyLong_FromVoid Ptr(conn) ;
}

static PyObject *
libpyq_PQfinish (PyObject *self, PyObject *args)
{
printf("%p", args);
return 1;
}
import libpyq #works fine
conn = libpyq.PQconnec tdb #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 1876
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_PQconnec tdb(PyObject *self, PyObject *args)
{
PGconn *conn = PyMem_New(PGcon n, sizeof(PGconn *));
conn = (PGconn *)PQconnectdb(( const char*)
PyString_AS_STR ING(args));

printf("%p", conn);
return PyLong_FromVoid Ptr(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.PQconnec tdb #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
2093
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 base of them all. Because I don't know what classes I am dealing with, I try to get a reference to the base class. Since the base class is a template instaniation, I cannot find a common type for all classes that
6
2824
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 stumped again with this little love triangle. Here is some background: m_commands.h defines - struct command_callbacks, which contains - a struct conn * reference - struct command, which contains - a struct command_callback reference
2
4883
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 tags then 5 dropdownlist controls each containing the 5 Xml text tags). Problem,when I save the user DropDownList selected items by means of a button click the web form looses the PlaceHolder viewstate and generates the following error..."Object...
5
2761
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 const KeyT first_type;
12
5384
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
4
1586
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
15268
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 typedef'd type like 'foo_t'. The following all fail to compile:
3
9541
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 that be placed?
11
3365
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 array. When I try it with the following code i get these errors 1. Use of possibly unassigned field 'micData' 2. The out parameter 'rem' must be assigned to before control leaves the current method Could someone point out how to do this and what...
0
8697
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8579
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
7297
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
6158
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
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
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
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1587
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.