473,578 Members | 2,997 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem calling Python methods from C

Hello everyone,

I'm trying to do something in C calling Python and its failing. I'd be
grateful if you could take a look and hopefully you have an answer.

What I'm trying to do is determine the address of the "collect" function
in the "gc" module. I want to do this so that we can determine when a
garbage collection happens, regardless of how it is triggered
(explicitly by a user call, or implicitly by the behaviour of the
program allocating lots of data).

As far as I can tell, the gc.collect function is not exposed as a C API,
so I cannot hook that to do the monitoring I want. Therefore the only
way to get the address to hook is to inspect the Python module function
dictionaries, well I think its the only way :-)

The approach is to get the module that holds "gc". You can do this by
calling PyImport_AddMod ule for an existing module - it will return it.
Then you get the dictionary of methods from the module and lookup the
method in there. Well, that was my theory after looking at the Python
source code.
I've shown the source code below.

// get "collect" method from GC module
// this module should always be present
// as it is a fundamental part of Python

PyObject *module;
DWORD funcAddress = NULL;

module = PyImport_AddMod ule("gc");
if (module != NULL)
{
// get dictionary of methods for this module

PyObject *dict;

dict = PyModule_GetDic t(module);
if (dict != NULL)
{
// lookup "collect" in the dictionary

PyObject *func;
int numItems;

// check we have some elements,
// strangely there are only 2

numItems = PyDict_Size(dic t);

// this works, good, that is expected

func = PyDict_GetItemS tring(dict, "__name__") ;

// this fails, why? "collect" is a method in "gc"
// I would expect it to work

func = PyDict_GetItemS tring(dict, "collect");
}
}

Hopefully someone can shed some light on why this doesn't work, or
explain what the correct method should be if I am doing the wrong thing.

Answers as descriptions, Python or C most welcome.

Cheers

Stephen
--
Stephen Kellett
Object Media Limited http://www.objmedia.demon.co.uk/software.html
Computer Consultancy, Software Development
Windows C++, Java, Assembler, Performance Analysis, Troubleshooting
Jul 19 '05 #1
1 1572
In message <aX************ **@objmedia.dem on.co.uk>, Stephen Kellett
<sn***@objmedia .demon.co.uk> writes

Following my posting with the solution for anyone else that wants to
know the answer.

The solution appears to be not to use:
module = PyImport_AddMod ule("gc");


But to use

module = PyImport_Import Module("gc");

Stephen
--
Stephen Kellett
Object Media Limited http://www.objmedia.demon.co.uk/software.html
Computer Consultancy, Software Development
Windows C++, Java, Assembler, Performance Analysis, Troubleshooting
Jul 19 '05 #2

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

Similar topics

7
1355
by: Doug Rosser | last post by:
I'm writing a fairly complicated test framework and keeping configuration data inside ini files that are parsed at runtime by the ConfigParser module. For example, there would be a section similar to the following server1:{'hostname':'alpha','os':'posix'} server2:{'hostname':'beta','os':'win'}
5
2273
by: eq | last post by:
Hi, I'm trying to create a program(written in C) that does the following things using embedded Python: 1. Create a module(say, "MyModule") 2. Create a class in that module(say, "MyClass") 3. Create a function in that module(say, "MyFunction") The Python-code would look like this: MyClass.py:
14
6388
by: Axel Straschil | last post by:
Hello! Im working with new (object) classes and normaly call init of ther motherclass with callin super(...), workes fine. No, I've got a case with multiple inherance and want to ask if this is the right and common case to call init: class Mother(object): def __init__(self, param_mother): print 'Mother'
4
1836
by: Matthew Thorley | last post by:
I am trying to inherit from ElementTree so I can add some methods. This is the code I am trying to make work, and the following is the error I am getting. from elementtree import ElementTree class AcidTree(ElementTree): def write_string(self): .... File "/home/hope/var/proj/acid/server/mgacparse.py", line 22, in ?
25
7737
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official Python tutorial 8 months ago, have spent 30 minutes with Python 3 times a week since, have 14 years of computing experience, 8 years in mathematical computing and 4 years in unix admin and perl, i...
2
4437
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c */ #include <time.h>
6
2335
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any solution. My code can be downloaded from here: http://www.tprimke.net/konto/PyObject-problem.tar.bz2. There are some scripts for GNU/Linux system...
4
2005
by: amidzic.branko | last post by:
I'm trying to solve a problem using inheritance and polymorphism in python 2.4.2 I think it's easier to explain the problem using simple example: class shortList:
3
6373
by: ShambhuHubli | last post by:
Hi all!! .. I am New member to this group. And also new to C/PYTHON API coding. I am trying to have two way communication i,e i am calling from python to C and then from C to python. I have no problem calling c func from python.... But problem is for calling C to Python I am trying to call a python function multiple times by passing one...
0
7850
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7779
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...
0
8291
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...
1
7874
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...
0
6524
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...
1
5664
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...
0
5343
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...
1
2293
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 we have to send another system
0
1114
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...

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.