mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-08-07 09:01:38 -04:00
Do not put C extensions that use exceptions into the zip file as an exception will then cause a crash. Also update the memory dll loader tosupport 64 bit dlls
This commit is contained in:
parent
ed57ee77d6
commit
c8398e295f
File diff suppressed because it is too large
Load Diff
@ -1,58 +1,48 @@
|
|||||||
/*
|
/*
|
||||||
* Memory DLL loading code
|
* Memory DLL loading code
|
||||||
* Version 0.0.2
|
* Version 0.0.3
|
||||||
*
|
*
|
||||||
* Copyright (c) 2004-2005 by Joachim Bauch / mail@joachim-bauch.de
|
* Copyright (c) 2004-2012 by Joachim Bauch / mail@joachim-bauch.de
|
||||||
* http://www.joachim-bauch.de
|
* http://www.joachim-bauch.de
|
||||||
*
|
*
|
||||||
* The contents of this file are subject to the Mozilla Public License Version
|
* The contents of this file are subject to the Mozilla Public License Version
|
||||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
* 2.0 (the "License"); you may not use this file except in compliance with
|
||||||
* the License. You may obtain a copy of the License at
|
* the License. You may obtain a copy of the License at
|
||||||
* http://www.mozilla.org/MPL/
|
* http://www.mozilla.org/MPL/
|
||||||
*
|
*
|
||||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||||
* for the specific language governing rights and limitations under the
|
* for the specific language governing rights and limitations under the
|
||||||
* License.
|
* License.
|
||||||
*
|
*
|
||||||
* The Original Code is MemoryModule.h
|
* The Original Code is MemoryModule.h
|
||||||
*
|
*
|
||||||
* The Initial Developer of the Original Code is Joachim Bauch.
|
* The Initial Developer of the Original Code is Joachim Bauch.
|
||||||
*
|
*
|
||||||
* Portions created by Joachim Bauch are Copyright (C) 2004-2005
|
* Portions created by Joachim Bauch are Copyright (C) 2004-2012
|
||||||
* Joachim Bauch. All Rights Reserved.
|
* Joachim Bauch. All Rights Reserved.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __MEMORY_MODULE_HEADER
|
#ifndef __MEMORY_MODULE_HEADER
|
||||||
#define __MEMORY_MODULE_HEADER
|
#define __MEMORY_MODULE_HEADER
|
||||||
|
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
|
|
||||||
typedef void *HMEMORYMODULE;
|
typedef void *HMEMORYMODULE;
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef void *(*FINDPROC)();
|
HMEMORYMODULE MemoryLoadLibrary(const void *);
|
||||||
|
|
||||||
extern FINDPROC findproc;
|
FARPROC MemoryGetProcAddress(HMEMORYMODULE, const char *);
|
||||||
extern void *findproc_data;
|
|
||||||
|
void MemoryFreeLibrary(HMEMORYMODULE);
|
||||||
HMEMORYMODULE MemoryLoadLibrary(char *, const void *);
|
|
||||||
|
#ifdef __cplusplus
|
||||||
FARPROC MemoryGetProcAddress(HMEMORYMODULE, const char *);
|
}
|
||||||
|
#endif
|
||||||
void MemoryFreeLibrary(HMEMORYMODULE);
|
|
||||||
|
#endif // __MEMORY_MODULE_HEADER
|
||||||
BOOL MyFreeLibrary(HMODULE hModule);
|
|
||||||
HMODULE MyLoadLibrary(char *lpFileName);
|
|
||||||
FARPROC MyGetProcAddress(HMODULE hModule, LPCSTR lpProcName);
|
|
||||||
HMODULE MyGetModuleHandle(LPCTSTR lpModuleName);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif // __MEMORY_MODULE_HEADER
|
|
||||||
|
@ -111,7 +111,7 @@ class Win32Freeze(Command, WixMixIn):
|
|||||||
self.info('Removing CRT dependency from manifest of: %s'%bn)
|
self.info('Removing CRT dependency from manifest of: %s'%bn)
|
||||||
# Blank out the bytes corresponding to the dependency specification
|
# Blank out the bytes corresponding to the dependency specification
|
||||||
nraw = repl_pat.sub(lambda m: b' '*len(m.group()), raw)
|
nraw = repl_pat.sub(lambda m: b' '*len(m.group()), raw)
|
||||||
if len(nraw) != len(raw):
|
if len(nraw) != len(raw) or nraw == raw:
|
||||||
raise Exception('Something went wrong with %s'%bn)
|
raise Exception('Something went wrong with %s'%bn)
|
||||||
with open(dll, 'wb') as f:
|
with open(dll, 'wb') as f:
|
||||||
f.write(nraw)
|
f.write(nraw)
|
||||||
@ -133,12 +133,13 @@ class Win32Freeze(Command, WixMixIn):
|
|||||||
# used instead
|
# used instead
|
||||||
shutil.copy2(f, tgt)
|
shutil.copy2(f, tgt)
|
||||||
|
|
||||||
def fix_pyd_bootstraps_in(self, crypto_dir):
|
def fix_pyd_bootstraps_in(self, folder):
|
||||||
for dirpath, dirnames, filenames in os.walk(crypto_dir):
|
for dirpath, dirnames, filenames in os.walk(folder):
|
||||||
for f in filenames:
|
for f in filenames:
|
||||||
name, ext = os.path.splitext(f)
|
name, ext = os.path.splitext(f)
|
||||||
if ext == '.pyd':
|
bpy = self.j(dirpath, name + '.py')
|
||||||
with open(self.j(dirpath, name+'.py')) as f:
|
if ext == '.pyd' and os.path.exists(bpy):
|
||||||
|
with open(bpy, 'rb') as f:
|
||||||
raw = f.read().strip()
|
raw = f.read().strip()
|
||||||
if (not raw.startswith('def __bootstrap__') or not
|
if (not raw.startswith('def __bootstrap__') or not
|
||||||
raw.endswith('__bootstrap__()')):
|
raw.endswith('__bootstrap__()')):
|
||||||
@ -203,9 +204,10 @@ class Win32Freeze(Command, WixMixIn):
|
|||||||
|
|
||||||
# Fix PyCrypto and Pillow, removing the bootstrap .py modules that load
|
# Fix PyCrypto and Pillow, removing the bootstrap .py modules that load
|
||||||
# the .pyd modules, since they do not work when in a zip file
|
# the .pyd modules, since they do not work when in a zip file
|
||||||
for crypto_dir in glob.glob(self.j(sp_dir, 'pycrypto-*', 'Crypto')
|
for folder in os.listdir(sp_dir):
|
||||||
) + glob.glob(self.j(sp_dir, 'Pillow-*')):
|
folder = self.j(sp_dir, folder)
|
||||||
self.fix_pyd_bootstraps_in(crypto_dir)
|
if os.path.isdir(folder):
|
||||||
|
self.fix_pyd_bootstraps_in(folder)
|
||||||
|
|
||||||
for pat in (r'PyQt4\uic\port_v3', ):
|
for pat in (r'PyQt4\uic\port_v3', ):
|
||||||
x = glob.glob(self.j(self.lib_dir, 'site-packages', pat))[0]
|
x = glob.glob(self.j(self.lib_dir, 'site-packages', pat))[0]
|
||||||
@ -570,13 +572,18 @@ class Win32Freeze(Command, WixMixIn):
|
|||||||
for x in (self.plugins_dir, self.dll_dir):
|
for x in (self.plugins_dir, self.dll_dir):
|
||||||
for pyd in os.listdir(x):
|
for pyd in os.listdir(x):
|
||||||
if pyd.endswith('.pyd') and pyd not in {
|
if pyd.endswith('.pyd') and pyd not in {
|
||||||
'unrar.pyd', 'sqlite_custom.pyd', 'calibre_style.pyd'}:
|
|
||||||
# sqlite_custom has to be a file for
|
# sqlite_custom has to be a file for
|
||||||
# sqlite_load_extension to work
|
# sqlite_load_extension to work
|
||||||
# For some reason unrar.pyd crashes when processing
|
'sqlite_custom.pyd',
|
||||||
# password protected RAR files if loaded from inside
|
# calibre_style has to be loaded by Qt therefore it
|
||||||
# pylib.zip. Probably because of this bug:
|
# must be a file
|
||||||
# https://github.com/fancycode/MemoryModule/issues/4
|
'calibre_style.pyd',
|
||||||
|
# Because of https://github.com/fancycode/MemoryModule/issues/4
|
||||||
|
# any extensions that use C++ exceptions must be loaded
|
||||||
|
# from files
|
||||||
|
'unrar.pyd', 'wpd.pyd', 'podofo.pyd',
|
||||||
|
'progress_indicator.pyd',
|
||||||
|
}:
|
||||||
self.add_to_zipfile(zf, pyd, x)
|
self.add_to_zipfile(zf, pyd, x)
|
||||||
os.remove(self.j(x, pyd))
|
os.remove(self.j(x, pyd))
|
||||||
|
|
||||||
|
@ -63,17 +63,6 @@ static void* FindLibrary(char *name, PyObject *callback)
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *set_find_proc(PyObject *self, PyObject *args)
|
|
||||||
{
|
|
||||||
PyObject *callback = NULL;
|
|
||||||
if (!PyArg_ParseTuple(args, "|O:set_find_proc", &callback))
|
|
||||||
return NULL;
|
|
||||||
Py_DECREF((PyObject *)findproc_data);
|
|
||||||
Py_INCREF(callback);
|
|
||||||
findproc_data = (void *)callback;
|
|
||||||
return Py_BuildValue("i", 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
import_module(PyObject *self, PyObject *args)
|
import_module(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
@ -92,7 +81,7 @@ import_module(PyObject *self, PyObject *args)
|
|||||||
&data, &size,
|
&data, &size,
|
||||||
&initfuncname, &modname, &pathname))
|
&initfuncname, &modname, &pathname))
|
||||||
return NULL;
|
return NULL;
|
||||||
hmem = MemoryLoadLibrary(pathname, data);
|
hmem = MemoryLoadLibrary(data);
|
||||||
if (!hmem) {
|
if (!hmem) {
|
||||||
PyErr_Format(*DLL_ImportError,
|
PyErr_Format(*DLL_ImportError,
|
||||||
"MemoryLoadLibrary() failed loading %s", pathname);
|
"MemoryLoadLibrary() failed loading %s", pathname);
|
||||||
@ -119,14 +108,14 @@ import_module(PyObject *self, PyObject *args)
|
|||||||
static PyMethodDef methods[] = {
|
static PyMethodDef methods[] = {
|
||||||
{ "import_module", import_module, METH_VARARGS,
|
{ "import_module", import_module, METH_VARARGS,
|
||||||
"import_module(code, initfunc, dllname[, finder]) -> module" },
|
"import_module(code, initfunc, dllname[, finder]) -> module" },
|
||||||
{ "set_find_proc", set_find_proc, METH_VARARGS },
|
|
||||||
{ NULL, NULL }, /* Sentinel */
|
{ NULL, NULL }, /* Sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
static int _show_error(const wchar_t *preamble, const wchar_t *msg, const int code) {
|
static int _show_error(const wchar_t *preamble, const wchar_t *msg, const int code) {
|
||||||
wchar_t *buf, *cbuf;
|
wchar_t *buf;
|
||||||
|
char *cbuf;
|
||||||
buf = (wchar_t*)LocalAlloc(LMEM_ZEROINIT, sizeof(wchar_t)*
|
buf = (wchar_t*)LocalAlloc(LMEM_ZEROINIT, sizeof(wchar_t)*
|
||||||
(wcslen(msg) + wcslen(preamble) + 80));
|
(wcslen(msg) + wcslen(preamble) + 80));
|
||||||
|
|
||||||
@ -372,7 +361,6 @@ void initialize_interpreter(wchar_t *outr, wchar_t *errr,
|
|||||||
}
|
}
|
||||||
PySys_SetObject("argv", argv);
|
PySys_SetObject("argv", argv);
|
||||||
|
|
||||||
findproc = FindLibrary;
|
|
||||||
Py_InitModule3("_memimporter", methods, module_doc);
|
Py_InitModule3("_memimporter", methods, module_doc);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user