python - Embed Python2 and Python3 interpreters, choose which one to use at runtime -
is there way embed both python2 , python3 interpreter c program , running either 1 or other decision occurring @ runtime?
here's example attempt:
makefile:
all: main main: main.c librun_in_py2.so librun_in_py3.so g++ main.c -lrun_in_py2 -lrun_in_py3 -l. -wl,-rpath -wl,$$origin -o main librun_in_py2.so: run_in_py2.c g++ $$(python2.7-config --cflags --ldflags) -shared -fpic $< -o $@ librun_in_py3.so: run_in_py3.c g++ $$(python3.4-config --cflags --ldflags) -shared -fpic $< -o $@ clean: @-rm main *.so
main.c
void run_in_py2(const char* const str); void run_in_py3(const char* const str); static const char str2[] = "from time import time,ctime\n" "import sys\n" "print sys.version_info\n" "print 'today is',ctime(time())\n"; static const char str3[] = "from time import time,ctime\n" "import sys\n" "print(sys.version_info)\n" "print('today is', ctime(time()))\n"; int main(int argc, char* []) { if (argc == 2) run_in_py2(str2); else run_in_py3(str3); }
run_in_py2.c
#include <python.h> void run_in_py2(const char* const str) { py_initialize(); pyrun_simplestring(str); py_finalize(); }
run_in_py3.c:
#include <python.h> void run_in_py3(const char* const str) { py_initialize(); pyrun_simplestring(str); py_finalize(); }
because of order of library linking result same:
$ ./main sys.version_info(major=2, minor=7, micro=9, releaselevel='final', serial=0) ('today is', 'thu jun 4 10:59:29 2015')
since names same looks linker resolves python 2 interpreter. there way isolate names or encourage linker lazier in resolving them? if possible, it'd ideal linker confirm names can resolved , put off symbol resolution until appropriate linker can chosen.
a highly related question asks running 2 independent embedded interpreters @ same time:
the suggestion there use 2 separate processes suspect there's simpler answer question.
the reason behind question thought understood conversation way when there program did this. , i'm curious how done.
Comments
Post a Comment