Index: Lib/test/test_capi.py =================================================================== --- Lib/test/test_capi.py (révision 88008) +++ Lib/test/test_capi.py (copie de travail) @@ -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 @@ -139,6 +140,36 @@ def test(self): self.assertEqual(_testcapi.argparsing("Hello", "World"), 1) + +class EmbeddingTest(unittest.TestCase): + + def test_subinterps(self): + # XXX only tested under Unix checkouts + basepath = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) + oldcwd = os.getcwd() + # This is needed otherwise we get a fatal error: + # "Py_Initialize: Unable to get the locale encoding + # LookupError: no codec search functions registered: can't find encoding" + os.chdir(basepath) + try: + exe = os.path.join(basepath, "Modules", "_testembed") + if not os.path.exists(exe): + self.skipTest("%r doesn't exist" % exe) + p = subprocess.Popen([exe], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + (out, err) = p.communicate() + self.assertEqual(p.returncode, 0, + "bad returncode %d, stderr is %r" % + (p.returncode, err)) + if support.verbose: + print() + print(out.decode('latin1')) + print(err.decode('latin1')) + finally: + os.chdir(oldcwd) + + def test_main(): support.run_unittest(CAPITest) @@ -175,7 +206,7 @@ t.start() t.join() - support.run_unittest(TestPendingCalls, Test6012) + support.run_unittest(TestPendingCalls, Test6012, EmbeddingTest) if __name__ == "__main__": Index: Makefile.pre.in =================================================================== --- Makefile.pre.in (révision 88008) +++ Makefile.pre.in (copie de travail) @@ -391,7 +391,7 @@ # 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: @@ -542,6 +542,9 @@ echo "-----------------------------------------------"; \ fi +Modules/_testembed: Modules/_testembed.o $(LIBRARY) $(LDLIBRARY) $(PY3LIBRARY) + $(LINKCC) $(PY_LDFLAGS) $(LINKFORSHARED) -o $@ Modules/_testembed.o $(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS) $(LDLAST) + ############################################################################ # Special rules for object files Index: Modules/_testembed.c =================================================================== --- Modules/_testembed.c (révision 0) +++ Modules/_testembed.c (révision 0) @@ -0,0 +1,53 @@ +#include +#include + +void print_subinterp() +{ + /* Just output some debug stuff */ + PyThreadState *ts = PyThreadState_Get(); + printf("interp %p : ", ts); + fflush(stdout); + PyRun_SimpleString( + "import sys;" + "print('id(modules) =', id(sys.modules));" + "sys.stdout.flush()" + ); +} + +int main(int argc, char *argv[]) +{ + PyThreadState *main, *sub; + PyGILState_STATE gilstate; + int i, j; + + for (i=0; i<3; i++) + { + + printf("--- Pass %d ---\n", i); + Py_SetProgramName(L"subembed"); + Py_Initialize(); + main = PyThreadState_Get(); + + PyEval_InitThreads(); + PyEval_ReleaseThread(main); + + gilstate = PyGILState_Ensure(); + print_subinterp(); + PyThreadState_Swap(NULL); + + for (j=0; j<3; j++) { + sub = Py_NewInterpreter(); + print_subinterp(); + Py_EndInterpreter(sub); + } + + PyThreadState_Swap(main); + print_subinterp(); + PyGILState_Release(gilstate); + + PyEval_RestoreThread(main); + Py_Finalize(); + + } + return 0; +} Modification de propriétés sur Modules/_testembed.c ___________________________________________________________________ Ajouté : svn:eol-style + native