Need help with embedded python

cecilm@tsms.tandem.com (cecilm@.tsms.tandem.com)
Thu, 26 May 1994 00:47:41 GMT

Have found and read the "Extending and Embedding the Python Interpreter"
doc but have found it to be confusing and/or lacking. Perhaps an example or two
would help.

I am in need of accessing a Python structure that is created in Python
and then accessed in C/C++. This particular application will perform
interactive drawing and build network interfaces based on the Python
system configuration list andas well as lots of other interfaces.

To restrict the scope let's use points and rectangles. rect.py defines
a list of rectangles based on points and runs a simple test script.

I need to take the resulting list and display the rectangles.
How do I access the list of user-defined rectangles from C/C++?

point.py: ------------------------------------------------------------

#
# point - Python point class
#
# Use of point:
# p=point.Point(10,20)
# q=point.Point(11,12)
# p.show()
# r = p + q
# r.show()
#
class Point:
def __init__(self, x, y): # constructor
self.x = x
self.y = y

def __repr__(self):
return 'Point(' + `self.x` + ', ' + `self.y` + ')'

def __coerce__(self, other):
if type(other) == type(self):
if other.__class__ == self.__class__:
return self, other
else:
raise TypeError, 'cannot coerce to Point type'
else:
# The cast to float() may raise an exception!
return self, Point(float(other), 0.0)

def __add__(aPt1, aPt2): # add to another point
return Point(aPt1.x+aPt2.x, aPt1.y+aPt2.y)

def __sub__(aPt1, aPt2): # aPt2 - aPt1sub from another point
return Point(aPt1.x-aPt2.x, aPt1.y-aPt2.y)

def __neg__(pt):
return Point(-pt.x, -pt.y)

def __abs__(pt):
return Point(abs(pt.x),abs(pt.y))

# test above stuff, not very good test
def test():
p = Point(10,20)
q = Point(11,12)
print p
print q
print p+q
print p-q
print p + Point(100, 200)
print p - Point(100, 200)
print 'p == q : ' + `p == q`

del test # delete the test module
#test()

rect.py: ------------------------------------------------------------

#
# rect - Python point and rectangle classes
#
import point
class Rectangle:
# Overloaded constructor:
# Point(upperLeftPoint, lowerRightPoint)
# Point(upperLeftPoint, width, height)
def __init__(self, ulPt, lrPtOrWidth, height = None): # constructor
self.ulPt = ulPt
if height == None:
self.lrPt = lrPtOrWidth
else:
self.lrPt = ulPt + point.Point(lrPtOrWidth, height)

def __repr__(self):
return 'Rectangle(' + `self.ulPt` + ', ' + `self.lrPt` + ')'

####################################
### global list of returned rects
####################################
global rectList
def getRectList():
return rectList

def test():
global rectList
r1 = Rectangle(point.Point(1, 2), point.Point(11, 12))
r2 = Rectangle(point.Point(10, 20), point.Point(100,200))
r3 = Rectangle(point.Point(10,15), 5, 4)
rectList = [r1, r2, r3]
print 'r3 = point(10, 15), 5, 4) = ' + `r3`
r3 = Rectangle(point.Point(10,15), -5, 4)
print 'r3 = point(10, 15), -5, 4) = ' + `r3`
print 'r1 = ' + `r1`
print 'r2 = ' + `r2`

#del test()
test()

main.cpp: ------------------------------------------------------------

/* Example of embedding Python in another program */
/* Hacked from demo.c */

#include <iostream.h>
#include "allobjects.h"

static char *argv0;

extern "C" void initall();
extern "C" void *run_command(char*);
extern "C" void goaway(int);
extern "C" void setpythonargv(int,char**);

main(int argc, char **argv)
{
object *result; // for calling python func

/* Save a copy of argv0 */
argv0 = argv[0];

/* Initialize the Python interpreter. Required. */
initall();

/* Define sys.argv. It is up to the application if you
want this; you can also let it undefined (since the Python
code is generally not a main program it has no business
touching sys.argv...) */
setpythonargv(argc, argv);

/* Do some application specific code */
cout << "Hello, brave new world" << endl;

/* Execute some Python statements (in module __main__) */
run_command("import sys\n");
run_command("print sys.builtin_module_names\n");
run_command("print sys.argv\n");
run_command("import rect");

/* Note that you can call any public function of the Python
interpreter here, e.g. call_object(). */

/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
This is where I get confused. I would like to
get a list of rectangles and do things like
display them. Below is TOTALLY wrong...
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
result = call_object((object *)"rect.rectList", NULL); /* ??? */
result = call_object((object *)"rect.getRectList", NULL); /* ??? */
/* step thru rectangle list ... and other stuff */

/* Some more application specific code */
cout << "\nGoodbye, cruel world" << endl;

/* Exit, cleaning up the interpreter */
goaway(0);
/*NOTREACHED*/
}

char *
getprogramname()
{
return argv0;
}

-----------------end of code----------------

thanks a lot, cecil

-- 

------------------------------------------------------------------------------ Cecil McGregor |\/\/\/| Tandem Corp, MS 3-06 | |