473,396 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to list the global functions from a C program

Hi all,
I'm a Python newbie and I'm trying to add to my C++ program a limited
support for scripts written in python.
In particular, I'd like to load user scripts written in python, list all the
functions
he defined in the script file and then call them.

To begin I wrote into my C++ program (correctly linked to python 2.3.2):

==============================================
/* create the main module */
m_pModule = PyImport_AddModule("__main__");
m_pDict = PyModule_GetDict(m_pModule);
m_pGlobals = m_pDict;
m_pLocals = m_pDict; // is this right (globals==locals) ??

/* to try out python, I want just to force the creation of
a simple function and then call it from C */
PyRun_StringFlags("def donothing():\n\treturn 'hello'\n",
Py_file_input, m_pGlobals, m_pLocals, 0);

/* scan all the contents of the __main__ module... */
PyObject *list = PyObject_Dir(m_pGlobals);
if (!list || PyList_Check(list) == FALSE)
return;

for (int i=0,max=PyList_Size(list); i<max; i++) {

PyObject *elem = PyList_GetItem(list, i);
if (PyCallable_Check(elem) != 0) {

/* this should be a function.. */
/* HERE IS THE PROBLEM: this code is never reached */
PyObject *str = PyObject_GetAttrString(elem, "func_name");
}
}
==============================================

Everything seems to work but then when scanning the list returned
by PyObject_Dir() I never find any callable object....
what am I doing wrong ?

Thanks indeed,
Francesco Montorsi
================================================== ============
The perverse principle of programming: there is always another bug. (Murphy)
================================================== ============

Jul 18 '05 #1
0 900

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

Similar topics

88
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from...
33
by: abs | last post by:
Hi all. My list: <ul> <li id="a" onclick="show(this)">Aaaaaaaa</li> <li id="b" onclick="show(this)">Bbbbbbbb</li> <li id="c" onclick="show(this)">Cccccccc <ul> <li id="d"...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
5
by: Little | last post by:
I have this program and I need to work on the test portion, which tests if a Val is in the list. It returns false no matter what could you look at the part and see what might need to be done to fix...
7
by: zeecanvas | last post by:
Hi, First of all: Yes, I know global variables are bad, but I've a huge amount of legacy code, and I've to maintain it _as_is_. I'm maintaining a big program. I moved all (program-wide scope)...
2
by: Phoe6 | last post by:
Hi all, Part of my script is to check for pre-requisite rpms to be installed. If its installed, I just display the rpm version as in rpm database, otherwise I output a message saying: ' rpm is...
17
by: 2005 | last post by:
Hi In C++, are the following considered best practices or not? - passing aguments to functions (ie functions do not take any arguments ) - returning values using return statement Anything...
4
by: peo_leo | last post by:
I have a simple implementation of double linked list this code is crashing if i enter values not present in the linked list(during insertion) or if i try to delete values not present in the list...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
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...
0
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...
0
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,...

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.