[r726]: / trunk / py2exe / source / _memimporter.c  Maximize  Restore  History

Download this file

87 lines (75 with data), 2.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
For the _memimporter compiled into py2exe exe-stubs we need "Python-dynload.h".
For the standalone .pyd we need <Python.h>
*/
#ifdef STANDALONE
# include <Python.h>
# include "Python-version.h"
#else
# include "Python-dynload.h"
# include <stdio.h>
#endif
#include <windows.h>
static char module_doc[] =
"Importer which can load extension modules from memory";
#include "MyLoadLibrary.h"
#include "actctx.h"
static PyObject *
import_module(PyObject *self, PyObject *args)
{
char *initfuncname;
char *modname;
char *pathname;
HMODULE hmem;
FARPROC do_init;
char *oldcontext;
ULONG_PTR cookie = 0;
PyObject *findproc;
/* code, initfuncname, fqmodulename, path */
if (!PyArg_ParseTuple(args, "sssO:import_module",
&modname, &pathname,
&initfuncname,
&findproc))
return NULL;
cookie = _My_ActivateActCtx();//try some windows manifest magic...
hmem = MyLoadLibrary(pathname, NULL, findproc);
_My_DeactivateActCtx(cookie);
if (!hmem) {
PyErr_Format(PyExc_ImportError,
"MemoryLoadLibrary failed loading %s", pathname);
return NULL;
}
do_init = MyGetProcAddress(hmem, initfuncname);
if (!do_init) {
MyFreeLibrary(hmem);
PyErr_Format(PyExc_ImportError,
"Could not find function %s", initfuncname);
return NULL;
}
oldcontext = _Py_PackageContext;
_Py_PackageContext = modname;
do_init();
_Py_PackageContext = oldcontext;
if (PyErr_Occurred())
return NULL;
/* Retrieve from sys.modules */
return PyImport_ImportModule(modname);
}
static PyObject *
get_verbose_flag(PyObject *self, PyObject *args)
{
return PyInt_FromLong(Py_VerboseFlag);
}
static PyMethodDef methods[] = {
{ "import_module", import_module, METH_VARARGS,
"import_module(modname, pathname, initfuncname, finder) -> module" },
{ "get_verbose_flag", get_verbose_flag, METH_NOARGS,
"Return the Py_Verbose flag" },
// { "set_find_proc", set_find_proc, METH_VARARGS },
{ NULL, NULL }, /* Sentinel */
};
DL_EXPORT(void)
init_memimporter(void)
{
Py_InitModule3("_memimporter", methods, module_doc);
}