diff options
author | Georg Brandl <georg@python.org> | 2010-12-30 21:33:07 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-12-30 21:33:07 (GMT) |
commit | 7fafbc95c0c963438197c9a43fe893c4ea6fe759 (patch) | |
tree | 5c9ceda4bdc5260236a230554b9ed56b8c0cdbd3 /Demo/embed | |
parent | 6f17e2df29a865a29447531e89fb22be710e382d (diff) | |
download | cpython-7fafbc95c0c963438197c9a43fe893c4ea6fe759.zip cpython-7fafbc95c0c963438197c9a43fe893c4ea6fe759.tar.gz cpython-7fafbc95c0c963438197c9a43fe893c4ea6fe759.tar.bz2 |
More cleanup: Move some demos into a dedicated Tools/demo dir, move 2to3 demo to Tools, and remove all the other Demo content.
Diffstat (limited to 'Demo/embed')
-rw-r--r-- | Demo/embed/Makefile | 57 | ||||
-rw-r--r-- | Demo/embed/README | 19 | ||||
-rw-r--r-- | Demo/embed/demo.c | 89 | ||||
-rw-r--r-- | Demo/embed/importexc.c | 23 | ||||
-rw-r--r-- | Demo/embed/loop.c | 33 |
5 files changed, 0 insertions, 221 deletions
diff --git a/Demo/embed/Makefile b/Demo/embed/Makefile deleted file mode 100644 index b02772f..0000000 --- a/Demo/embed/Makefile +++ /dev/null @@ -1,57 +0,0 @@ -# Makefile for embedded Python use demo. -# (This version originally written on Red Hat Linux 6.1; -# edit lines marked with XXX.) - -# XXX The compiler you are using -CC= gcc - -# XXX Top of the build tree and source tree -blddir= ../.. -srcdir= ../.. - -# Python version -VERSION= 3.2 - -# Compiler flags -OPT= -g -INCLUDES= -I$(srcdir)/Include -I$(blddir) -CFLAGS= $(OPT) -CPPFLAGS= $(INCLUDES) - -# The Python library -LIBPYTHON= $(blddir)/libpython$(VERSION).a - -# XXX edit LIBS (in particular) to match $(blddir)/Makefile -LIBS= -lnsl -ldl -lreadline -lieee -lpthread -lutil -LDFLAGS= -Xlinker -export-dynamic -SYSLIBS= -lm -MODLIBS= -ALLLIBS= $(LIBPYTHON) $(MODLIBS) $(LIBS) $(SYSLIBS) - -# Build the demo applications -all: demo loop importexc -demo: demo.o - $(CC) $(LDFLAGS) demo.o $(ALLLIBS) -o demo - -loop: loop.o - $(CC) $(LDFLAGS) loop.o $(ALLLIBS) -o loop - -importexc: importexc.o - $(CC) $(LDFLAGS) importexc.o $(ALLLIBS) -o importexc - -# Administrative targets - -test: demo - ./demo - -COMMAND="print 'hello world'" -looptest: loop - ./loop $(COMMAND) - -clean: - -rm -f *.o core - -clobber: clean - -rm -f *~ @* '#'* demo loop importexc - -realclean: clobber diff --git a/Demo/embed/README b/Demo/embed/README deleted file mode 100644 index a0f7af8..0000000 --- a/Demo/embed/README +++ /dev/null @@ -1,19 +0,0 @@ -This directory show how 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 - -An additional test program, loop.c, is used to experiment with memory -leakage caused by repeated initialization and finalization of the -interpreter. It can be build by saying "make loop" and tested with -"make looptest". Command line usage is "./loop <python-command>", -e.g. "./loop 'print 2+2'" should spit out an endless number of lines -containing the number 4. diff --git a/Demo/embed/demo.c b/Demo/embed/demo.c deleted file mode 100644 index 8d92820..0000000 --- a/Demo/embed/demo.c +++ /dev/null @@ -1,89 +0,0 @@ -/* Example of embedding Python in another program */ - -#include "Python.h" - -PyObject* PyInit_xyzzy(void); /* Forward */ - -main(int argc, char **argv) -{ - /* Ignore passed-in argc/argv. If desired, conversion - should use mbstowcs to convert them. */ - wchar_t *args[] = {L"embed", L"hello", 0}; - - /* Pass argv[0] to the Python interpreter */ - Py_SetProgramName(args[0]); - - /* Add a static module */ - PyImport_AppendInittab("xyzzy", PyInit_xyzzy); - - /* Initialize the Python interpreter. Required. */ - Py_Initialize(); - - /* Define sys.argv. It is up to the application if you - want this; you can also leave it undefined (since the Python - code is generally not a main program it has no business - touching sys.argv...) - - If the third argument is true, sys.path is modified to include - either the directory containing the script named by argv[0], or - the current working directory. This can be risky; if you run - an application embedding Python in a directory controlled by - someone else, attackers could put a Trojan-horse module in the - directory (say, a file named os.py) that your application would - then import and run. - */ - PySys_SetArgvEx(2, args, 0); - - /* Do some application specific code */ - printf("Hello, brave new world\n\n"); - - /* Execute some Python statements (in module __main__) */ - PyRun_SimpleString("import sys\n"); - PyRun_SimpleString("print(sys.builtin_module_names)\n"); - PyRun_SimpleString("print(sys.modules.keys())\n"); - PyRun_SimpleString("print(sys.executable)\n"); - PyRun_SimpleString("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 */ - Py_Exit(0); - /*NOTREACHED*/ -} - -/* A static module */ - -/* 'self' is not used */ -static PyObject * -xyzzy_foo(PyObject *self, PyObject* args) -{ - return PyLong_FromLong(42L); -} - -static PyMethodDef xyzzy_methods[] = { - {"foo", xyzzy_foo, METH_NOARGS, - "Return the meaning of everything."}, - {NULL, NULL} /* sentinel */ -}; - -static struct PyModuleDef xyzzymodule = { - {}, /* m_base */ - "xyzzy", /* m_name */ - 0, /* m_doc */ - 0, /* m_size */ - xyzzy_methods, /* m_methods */ - 0, /* m_reload */ - 0, /* m_traverse */ - 0, /* m_clear */ - 0, /* m_free */ -}; - -PyObject* -PyInit_xyzzy(void) -{ - return PyModule_Create(&xyzzymodule); -} diff --git a/Demo/embed/importexc.c b/Demo/embed/importexc.c deleted file mode 100644 index 59b1d01..0000000 --- a/Demo/embed/importexc.c +++ /dev/null @@ -1,23 +0,0 @@ -#include <Python.h> - -#if 0 -char* cmd = "import codecs, encodings.utf_8, types; print(types)"; -#else -char* cmd = "import types; print(types)"; -#endif - -int main() -{ - printf("Initialize interpreter\n"); - Py_Initialize(); - PyEval_InitThreads(); - PyRun_SimpleString(cmd); - Py_EndInterpreter(PyThreadState_Get()); - - printf("\nInitialize subinterpreter\n"); - Py_NewInterpreter(); - PyRun_SimpleString(cmd); - Py_Finalize(); - - return 0; -} diff --git a/Demo/embed/loop.c b/Demo/embed/loop.c deleted file mode 100644 index 4a341fd..0000000 --- a/Demo/embed/loop.c +++ /dev/null @@ -1,33 +0,0 @@ -/* Simple program that repeatedly calls Py_Initialize(), does something, and - then calls Py_Finalize(). This should help finding leaks related to - initialization. */ - -#include "Python.h" - -main(int argc, char **argv) -{ - int count = -1; - char *command; - - if (argc < 2 || argc > 3) { - fprintf(stderr, "usage: loop <python-command> [count]\n"); - exit(2); - } - command = argv[1]; - - if (argc == 3) { - count = atoi(argv[2]); - } - - Py_SetProgramName(L"loop"); - - /* uncomment this if you don't want to load site.py */ - /* Py_NoSiteFlag = 1; */ - - while (count == -1 || --count >= 0 ) { - Py_Initialize(); - PyRun_SimpleString(command); - Py_Finalize(); - } - return 0; -} |