summaryrefslogtreecommitdiffstats
path: root/Demo/embed
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1994-10-08 19:30:50 (GMT)
committerGuido van Rossum <guido@python.org>1994-10-08 19:30:50 (GMT)
commit705d517e3f4df74b8c04edf54ac73b087c982f32 (patch)
tree7b0543f1165fc1bdfc4717277f67d8e699dcbeb4 /Demo/embed
parent3dc44aba71305cd9be8cba7703a96b6528f34169 (diff)
downloadcpython-705d517e3f4df74b8c04edf54ac73b087c982f32.zip
cpython-705d517e3f4df74b8c04edf54ac73b087c982f32.tar.gz
cpython-705d517e3f4df74b8c04edf54ac73b087c982f32.tar.bz2
initial checkin
Diffstat (limited to 'Demo/embed')
-rw-r--r--Demo/embed/Makefile42
-rw-r--r--Demo/embed/README12
-rw-r--r--Demo/embed/demo.c46
3 files changed, 100 insertions, 0 deletions
diff --git a/Demo/embed/Makefile b/Demo/embed/Makefile
new file mode 100644
index 0000000..d63a9d4
--- /dev/null
+++ b/Demo/embed/Makefile
@@ -0,0 +1,42 @@
+# Makefile for embedded Python use demo
+
+# Top of the build tree and source tree
+blddir= ../..
+srcdir= ../..
+
+# Compiler flags
+OPT= -g
+INCLUDES= -I$(srcdir)/Include -I$(blddir)
+DEFINES= -DHAVE_CONFIG_H
+CFLAGS= $(OPT) $(DEFINES) $(INCLUDES)
+
+# Libraries
+# XXX edit MODLIBS, LIBS and SYSLIBS to match $(blddir)/Modules/Makefile
+MYLIBS= $(blddir)/Modules/libModules.a \
+ $(blddir)/Python/libPython.a \
+ $(blddir)/Objects/libObjects.a \
+ $(blddir)/Parser/libParser.a
+MODLIBS=
+LIBS=
+SYSLIBS= -lm
+ALLLIBS= $(MYLIBS) $(MODLIBS) $(LIBS) $(SYSLIBS)
+
+# Build the demo application
+all: demo
+demo: demo.o config.o
+ $(CC) demo.o config.o $(ALLLIBS) -o demo
+
+# Build config.o, suppressing the main() function
+config.o: $(blddir)/Modules/config.c
+ $(CC) $(CFLAGS) -DNO_MAIN -c $(blddir)/Modules/config.c
+
+# Administrative targets
+
+test: demo
+ ./demo
+
+clean:
+ -rm -f *.o core
+
+clobber: clean
+ -rm -f *~ @* '#'* demo
diff --git a/Demo/embed/README b/Demo/embed/README
new file mode 100644
index 0000000..62b6513
--- /dev/null
+++ b/Demo/embed/README
@@ -0,0 +1,12 @@
+This directory show how easy it is to embed the Python interpreter in
+your own application. The file demo.c shows you all that is needed in
+your C code.
+
+To build it, you may have to edit the Makefile:
+
+1) set blddir to the directory where you built Python, if it isn't in
+the source directory (../..)
+
+2) change the variables that together define the list of libraries
+(MODLIBS, LIBS, SYSLIBS) to link with, to match their definitions in
+$(blddir)/Modules/Makefile
diff --git a/Demo/embed/demo.c b/Demo/embed/demo.c
new file mode 100644
index 0000000..b1adde2
--- /dev/null
+++ b/Demo/embed/demo.c
@@ -0,0 +1,46 @@
+/* Example of embedding Python in another program */
+
+#include "allobjects.h"
+
+static char *argv0;
+
+main(argc, argv)
+ int argc;
+ char **argv;
+{
+ /* 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 */
+ printf("Hello, brave new world\n\n");
+
+ /* 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");
+
+ /* Note that you can call any public function of the Python
+ interpreter here, e.g. call_object(). */
+
+ /* Some more application specific code */
+ printf("\nGoodbye, cruel world\n");
+
+ /* Exit, cleaning up the interpreter */
+ goaway(0);
+ /*NOTREACHED*/
+}
+
+char *
+getprogramname()
+{
+ return argv0;
+}