Skip to content
Permalink
Browse files

Replace custom distutils hacks with a simple Extension

  • Loading branch information
akx committed Jan 14, 2020
1 parent 1ccf8d2 commit 34374b00c2c320d27273f6be69dfe7bd571367fb
Showing with 65 additions and 323 deletions.
  1. +5 −0 .gitignore
  2. +1 −1 MANIFEST.in
  3. +21 −33 python/distorm3/__init__.py
  4. +30 −0 python/python_module_init.c
  5. +0 −3 setup.cfg
  6. +8 −286 setup.py
@@ -0,0 +1,5 @@
*.egg-info
*.py[cod]
*.so
build
dist
@@ -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
@@ -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:
@@ -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:
@@ -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
@@ -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
@@ -1,6 +1,3 @@
[wheel]
universal = 1

[install]
force=1
compile=1

0 comments on commit 34374b0

Please sign in to comment.
You can’t perform that action at this time.