469,609 Members | 1,455 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

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_AddModule 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_AddModule("gc");
if (module != NULL)
{
// get dictionary of methods for this module

PyObject *dict;

dict = PyModule_GetDict(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(dict);

// this works, good, that is expected

func = PyDict_GetItemString(dict, "__name__");

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

func = PyDict_GetItemString(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 1426
In message <aX**************@objmedia.demon.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_AddModule("gc");


But to use

module = PyImport_ImportModule("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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by eq | last post: by
14 posts views Thread by Axel Straschil | last post: by
4 posts views Thread by Matthew Thorley | last post: by
2 posts views Thread by ajikoe | last post: by
6 posts views Thread by TPJ | last post: by
4 posts views Thread by amidzic.branko | last post: by
reply views Thread by Solution2021 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.