Open jcelerier opened 7 years ago
It seems like my issue where I have to freeze my import hook while my main program does this:
/*
* komextract_new.c
*
* Entrypoint of the embeded python interpreter for
* running code to extract from kom files.
*
* Note: This code is for WIN32/WIN64 only.
*/
#include <Python.h>
/* for loading python script from the resource
* section.
*/
#include <Windows.h>
#define WITH_ENCRYPTION
#ifdef WITH_ENCRYPTION
#include "encryption.h"
#include "frozenlist.h"
#endif
#include "resource.h"
#ifdef WITH_ENCRYPTION
PyMODINIT_FUNC PyInit_aes(void);
PyMODINIT_FUNC PyInit__zipimport(void);
#endif
int
wmain(int argc, wchar_t **argv)
{
int err, err2;
wchar_t *program = Py_DecodeLocale((char *)argv[0], NULL);
wchar_t **argv_copy = argv;
if (program == NULL) {
LPSTR buffer1[36];
LoadStringA(GetModuleHandle(NULL), IDS_STRING1, (LPSTR)buffer1, 36);
fprintf(stderr, (const char *const)buffer1);
exit(1);
}
#ifdef WITH_ENCRYPTION
PyImport_AppendInittab("aes", PyInit_aes);
PyImport_AppendInittab("_zipimport", PyInit__zipimport);
#endif
Py_SetProgramName(program); /* optional but recommended */
Py_Initialize();
if (Py_IsInitialized() != 0) {
/* allows use of sys.argv to be possible
* with no tracebacks.
*/
argv_copy[0] = L"";
PySys_SetArgvEx(argc, argv_copy, 0);
#ifdef WITH_ENCRYPTION
HRSRC script2_resource = FindResource(
NULL, MAKEINTRESOURCE(IDR_RCDATA2), RT_RCDATA);
unsigned int script2_size = SizeofResource(NULL, script2_resource);
HGLOBAL main2_script = LoadResource(NULL, script2_resource);
void* pmain2_script = LockResource(main2_script);
if (script2_size >= 0) {
err = PyEncryptionExec((const char *)pmain2_script);
} else {
/* python script is empty. */
LPSTR buffer4[62];
LoadStringA(GetModuleHandle(NULL), IDS_STRING4, (LPSTR)buffer4, 62);
fprintf(stderr, (const char *const)buffer4);
}
if (err > 0)
PyErr_Print();
#endif
HRSRC script_resource = FindResource(
NULL, MAKEINTRESOURCE(IDR_RCDATA1), RT_RCDATA);
unsigned int script_size = SizeofResource(NULL, script_resource);
HGLOBAL main_script = LoadResource(NULL, script_resource);
void* pmain_script = LockResource(main_script);
if (script_size >= 0) {
err2 = PyRun_SimpleString((const char *)pmain_script);
} else {
/* python script is empty. */
LPSTR buffer2[38];
LoadStringA(GetModuleHandle(NULL), IDS_STRING2, (LPSTR)buffer2, 38);
fprintf(stderr, (const char *const)buffer2);
}
if (err2 > 0)
PyErr_Print();
Py_Finalize();
} else {
/* python is not initialized. */
LPSTR buffer3[41];
LoadStringA(GetModuleHandle(NULL), IDS_STRING3, (LPSTR)buffer3, 41);
fprintf(stderr, (const char *const)buffer3);
}
PyMem_RawFree(program);
return 0;
}
and frozenlist.h
with this code:
#pragma once
#include <importlib.h>
#include <importlib_external.h>
#include "pyeimport.h"
static const struct _frozen _PyImport_FrozenModules[] = {
/* importlib */
{"_frozen_importlib", _Py_M__importlib, (int)sizeof(_Py_M__importlib)},
{"_frozen_importlib_external", _Py_M__importlib_external,
(int)sizeof(_Py_M__importlib_external)},
/* pye import hook. */
{"pyeimport", M_pyeimport, (int)sizeof(M_pyeimport)},
{0, 0, 0} /* sentinel */
};
const struct _frozen * PyImport_FrozenModules = _PyImport_FrozenModules;
And basically when I compile with frozenlist.h
the following happens and I am not sure why, I tried everything too.
<path trimmed>\bin\x86\Release>kompact_new
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'pyeimport'
Traceback (most recent call last):
File "<string>", line 7, in <module>
ModuleNotFoundError: No module named 'komformat'
What is going on? I told python that pyeimport
is an frozen module though...
I’m interested in the same feature. Is there any way to do this in pybind11?
I have the following case :
dlopen
. This plug-in uses pybind11.So, basically :
When I want to execute some python code that uses scikit_learn from this plug-in, I get the following error:
However, this works if I load the python code from my "main" executable instead.
Here's a minimal example :
CMakeLists.txt:
main.cpp:
lib.cpp:
running: