Skip to content

Commit

Permalink
Replace custom distutils hacks with a simple Extension
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Jan 14, 2020
1 parent 1ccf8d2 commit 34374b0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 323 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
*.egg-info
*.py[cod]
*.so
build
dist
2 changes: 1 addition & 1 deletion MANIFEST.in
@@ -1,5 +1,5 @@
include COPYING setup.cfg setup.py
include make\win32\cdistorm.vcxproj make\win32\cdistorm.vcxproj.filters make\win32\distorm.sln make\win32\resource.h make\win32\Resource.rc
recursive-include src *.c *.h
recursive-include include *.c *.h
recursive-include include *.h
recursive-include . *.py
54 changes: 21 additions & 33 deletions python/distorm3/__init__.py
Expand Up @@ -26,8 +26,7 @@
]

from ctypes import *
from os.path import split, join
from os import name as os_name
import os
import sys

if sys.version_info[0] >= 3:
Expand All @@ -36,33 +35,25 @@
#==============================================================================
# Load the diStorm DLL

# Guess the DLL filename and load the library.
_distorm_path = None
try:
_distorm_path = split(__file__)[0]
except NameError:
# In some containers __file__ isn't set.
pass
if hasattr(sys, "oxidized"):
_distorm_path = ""
if hasattr(sys, '_MEIPASS'):
_distorm_path = sys._MEIPASS
potential_libs = ['libdistorm3.so', 'libdistorm3.dylib']
if os_name == 'nt':
potential_libs = ['distorm3.dll', 'libdistorm3.dll']
lib_was_found = False
for i in potential_libs:
try:
_distorm_file = join(_distorm_path, i)
_distorm = cdll.LoadLibrary(_distorm_file)
lib_was_found = True
break
except OSError:
pass

if lib_was_found == False:
def _load_distorm():
if sys.version_info[0] == 3:
try:
import _distorm3
return cdll.LoadLibrary(_distorm3.__spec__.origin)
except ImportError:
pass

dll_ext = ('.dll' if sys.platform == 'win32' else '.so')
libnames = ['_distorm3' + dll_ext]
for dir in sys.path:
for name in libnames:
_distorm_file = os.path.join(dir, name)
if os.path.isfile(_distorm_file):
return cdll.LoadLibrary(_distorm_file)
raise ImportError("Error loading the diStorm dynamic library (or cannot load library into process).")

_distorm = _load_distorm()

# Get the decode C function (try 64 bits version first, only then 32 bits).
SUPPORT_64BIT_OFFSET = False
try:
Expand All @@ -71,12 +62,9 @@
internal_format = _distorm.distorm_format64
SUPPORT_64BIT_OFFSET = True
except AttributeError:
try:
internal_decode = _distorm.distorm_decode32
internal_decompose = _distorm.distorm_decompose32
internal_format = _distorm.distorm_format32
except AttributeError:
raise ImportError("Error loading distorm")
internal_decode = _distorm.distorm_decode32
internal_decompose = _distorm.distorm_decompose32
internal_format = _distorm.distorm_format32

#==============================================================================
# diStorm C interface
Expand Down
30 changes: 30 additions & 0 deletions python/python_module_init.c
@@ -0,0 +1,30 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>

#if PY_MAJOR_VERSION == 2
PyMODINIT_FUNC
init_distorm3(void)
{
(void) Py_InitModule("_distorm3", NULL);
}
#else
static struct PyModuleDef _distorm3_module = {
PyModuleDef_HEAD_INIT,
"_distorm3",
NULL,
-1,
NULL,
};

PyMODINIT_FUNC
PyInit__distorm3(void)
{
PyObject *m;

m = PyModule_Create(&_distorm3_module);
if (m == NULL)
return NULL;

return m;
}
#endif
3 changes: 0 additions & 3 deletions setup.cfg
@@ -1,6 +1,3 @@
[wheel]
universal = 1

[install]
force=1
compile=1
Expand Down

0 comments on commit 34374b0

Please sign in to comment.