cpython: d195ff5c44f4 (original) (raw)
Mercurial > cpython
changeset 69553:d195ff5c44f4 3.2
Issue #10914: Add a minimal embedding test to test_capi. [#10914]
Antoine Pitrou solipsis@pitrou.net | |
---|---|
date | Mon, 25 Apr 2011 21:21:07 +0200 |
parents | 66ef5e844e6c |
children | 77cf9e4b144b |
files | .hgignore Lib/test/test_capi.py Makefile.pre.in Modules/_testembed.c |
diffstat | 4 files changed, 89 insertions(+), 4 deletions(-)[+] [-] .hgignore 1 Lib/test/test_capi.py 35 Makefile.pre.in 5 Modules/_testembed.c 52 |
line wrap: on
line diff
--- a/.hgignore +++ b/.hgignore @@ -64,3 +64,4 @@ PCbuild/.ncb PCbuild/.bsc PCbuild/Win32-temp-* pycache +Modules/_testembed
--- a/Lib/test/test_capi.py +++ b/Lib/test/test_capi.py @@ -2,6 +2,7 @@
these are all functions testcapi exports whose name begins with 'test'.
from future import with_statement +import os import random import subprocess import sys @@ -141,8 +142,38 @@ class Test6012(unittest.TestCase): def test(self): self.assertEqual(_testcapi.argparsing("Hello", "World"), 1) + +class EmbeddingTest(unittest.TestCase): +
- def test_subinterps(self):
# XXX only tested under Unix checkouts[](#l2.19)
basepath = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))[](#l2.20)
oldcwd = os.getcwd()[](#l2.21)
# This is needed otherwise we get a fatal error:[](#l2.22)
# "Py_Initialize: Unable to get the locale encoding[](#l2.23)
# LookupError: no codec search functions registered: can't find encoding"[](#l2.24)
os.chdir(basepath)[](#l2.25)
try:[](#l2.26)
exe = os.path.join(basepath, "Modules", "_testembed")[](#l2.27)
if not os.path.exists(exe):[](#l2.28)
self.skipTest("%r doesn't exist" % exe)[](#l2.29)
p = subprocess.Popen([exe],[](#l2.30)
stdout=subprocess.PIPE,[](#l2.31)
stderr=subprocess.PIPE)[](#l2.32)
(out, err) = p.communicate()[](#l2.33)
self.assertEqual(p.returncode, 0,[](#l2.34)
"bad returncode %d, stderr is %r" %[](#l2.35)
(p.returncode, err))[](#l2.36)
if support.verbose:[](#l2.37)
print()[](#l2.38)
print(out.decode('latin1'))[](#l2.39)
print(err.decode('latin1'))[](#l2.40)
finally:[](#l2.41)
os.chdir(oldcwd)[](#l2.42)
for name in dir(testcapi): if name.startswith('test'): @@ -177,8 +208,6 @@ def test_main(): t.start() t.join()
- if name == "main": test_main()
--- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -394,7 +394,7 @@ LIBRARY_OBJS= [](#l3.3)
Default target
all: build_all -build_all: $(BUILDPYTHON) oldsharedmods sharedmods gdbhooks +build_all: $(BUILDPYTHON) oldsharedmods sharedmods gdbhooks Modules/_testembed
Compile a binary with gcc profile guided optimization.
profile-opt: @@ -539,6 +539,9 @@ Modules/Setup: $(srcdir)/Modules/Setup.d echo "-----------------------------------------------"; [](#l3.13) fi +Modules/_testembed: Modules/_testembed.o (LIBRARY)(LIBRARY) (LIBRARY)(LDLIBRARY) $(PY3LIBRARY)
- (LINKCC)(LINKCC) (LINKCC)(PY_LDFLAGS) (LINKFORSHARED)−o(LINKFORSHARED) -o (LINKFORSHARED)−o@ Modules/_testembed.o (BLDLIBRARY)(BLDLIBRARY) (BLDLIBRARY)(LIBS) (MODLIBS)(MODLIBS) (MODLIBS)(SYSLIBS) $(LDLAST) +
############################################################################
Special rules for object files
new file mode 100644 --- /dev/null +++ b/Modules/_testembed.c @@ -0,0 +1,52 @@ +#include <Python.h> +#include <stdio.h> + +void print_subinterp(void) +{
- /* Just output some debug stuff */
- PyThreadState *ts = PyThreadState_Get();
- printf("interp %p, thread state %p: ", ts->interp, ts);
- fflush(stdout);
- PyRun_SimpleString(
"import sys;"[](#l4.15)
"print('id(modules) =', id(sys.modules));"[](#l4.16)
"sys.stdout.flush()"[](#l4.17)
- );
+} + +int main(int argc, char *argv[]) +{
- for (i=0; i<3; i++) {
printf("--- Pass %d ---\n", i);[](#l4.28)
/* HACK: the "./" at front avoids a search along the PATH in[](#l4.29)
Modules/getpath.c */[](#l4.30)
Py_SetProgramName(L"./_testembed");[](#l4.31)
Py_Initialize();[](#l4.32)
mainstate = PyThreadState_Get();[](#l4.33)
PyEval_InitThreads();[](#l4.35)
PyEval_ReleaseThread(mainstate);[](#l4.36)
gilstate = PyGILState_Ensure();[](#l4.38)
print_subinterp();[](#l4.39)
PyThreadState_Swap(NULL);[](#l4.40)
for (j=0; j<3; j++) {[](#l4.42)
substate = Py_NewInterpreter();[](#l4.43)
print_subinterp();[](#l4.44)
Py_EndInterpreter(substate);[](#l4.45)
}[](#l4.46)
PyThreadState_Swap(mainstate);[](#l4.48)
print_subinterp();[](#l4.49)
PyGILState_Release(gilstate);[](#l4.50)