Skip to content

Commit

Permalink
Revert "Remove python2 leftovers (#2378)" (#2391)
Browse files Browse the repository at this point in the history
This reverts commit 60d5b7e.
  • Loading branch information
kabeor authored Jun 24, 2024
1 parent 33f24cd commit a3fd6ac
Show file tree
Hide file tree
Showing 32 changed files with 124 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/CITest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ jobs:
run: |
cp libcapstone.* bindings/python/prebuilt
cd bindings/python
make install
make install3
cd ..
BUILD_TESTS=no make tests
Expand All @@ -188,7 +188,7 @@ jobs:
run: |
pip install cython
cd bindings/python
make install_cython
make install3_cython
cd ..
python -c "import capstone;print(capstone.debug())" | grep Cython
BUILD_TESTS=no make tests
Expand Down
11 changes: 8 additions & 3 deletions bindings/python/BUILDING.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

$ sudo make install

To install Capstone for Python 3, run the command below:
(Note: this requires python3 installed in your machine)

$ sudo make install3

To control the install destination, set the DESTDIR environment variable.

2. For better Python performance, install cython-based binding with:
Expand All @@ -14,7 +19,7 @@

Note that this requires Cython installed first. To install Cython, see
below.

3. To install Cython, you have to ensure that the header files
and the static library for Python are installed beforehand.

Expand All @@ -33,13 +38,13 @@
install the required Cython version using your repository.

E.g. on Ubuntu, do:

$ sudo apt-get install cython

However, our cython-based binding requires Cython version 0.19 or newer,
but sometimes distributions only provide older version. Make sure to
verify the current installed version before going into section 2 above.

E.g, on Ubuntu, you can verify the current Cython version with:

$ apt-cache policy cython
Expand Down
29 changes: 28 additions & 1 deletion bindings/python/Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
PYTHON2 ?= python2
PYTHON3 ?= python3

.PHONY: gen_const install install_cython sdist bdist clean check
.PHONY: gen_const install install3 install_cython sdist sdist3 bdist bdist3 clean check

gen_const:
cd .. && $(PYTHON3) const_generator.py python

install:
rm -rf src/
if test -n "${DESTDIR}"; then \
$(PYTHON2) setup.py build install --root="${DESTDIR}"; \
else \
$(PYTHON2) setup.py build install; \
fi

install3:
rm -rf src/
if test -n "${DESTDIR}"; then \
$(PYTHON3) setup.py build install --root="${DESTDIR}"; \
Expand All @@ -15,6 +24,14 @@ install:

# NOTE: Newer cython can be installed by: sudo pip install --upgrade cython
install_cython:
rm -rf src/
if test -n "${DESTDIR}"; then \
$(PYTHON2) setup_cython.py build install --root="${DESTDIR}"; \
else \
$(PYTHON2) setup_cython.py build install; \
fi

install3_cython:
rm -rf src/
if test -n "${DESTDIR}"; then \
$(PYTHON3) setup_cython.py build install --root="${DESTDIR}"; \
Expand All @@ -24,11 +41,21 @@ install_cython:

# build & upload PyPi package with source code of the core
sdist:
rm -rf src/ dist/
$(PYTHON2) setup.py sdist register upload

# build & upload PyPi package with source code of the core
sdist3:
rm -rf src/ dist/
$(PYTHON3) setup.py sdist register upload

# build & upload PyPi package with prebuilt core
bdist:
rm -rf src/ dist/
$(PYTHON2) setup.py bdist_wheel register upload

# build & upload PyPi package with prebuilt core
bdist3:
rm -rf src/ dist/
$(PYTHON3) setup.py bdist_wheel register upload

Expand Down
20 changes: 16 additions & 4 deletions bindings/python/capstone/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
import os
import sys
import os, sys
from platform import system
_python2 = sys.version_info[0] < 3
if _python2:
range = xrange

__all__ = [
'Cs',
Expand Down Expand Up @@ -546,8 +549,13 @@ class CsError(Exception):
def __init__(self, errno):
self.errno = errno

def __str__(self):
return _cs.cs_strerror(self.errno).decode()
if _python2:
def __str__(self):
return _cs.cs_strerror(self.errno)

else:
def __str__(self):
return _cs.cs_strerror(self.errno).decode()


# return the core's version
Expand Down Expand Up @@ -1207,6 +1215,10 @@ def group_name(self, group_id, default=None):
# Disassemble binary & return disassembled instructions in CsInsn objects
def disasm(self, code, offset, count=0):
all_insn = ctypes.POINTER(_cs_insn)()
'''if not _python2:
print(code)
code = code.encode()
print(code)'''
# Pass a bytearray by reference
size = len(code)
view = memoryview(code)
Expand Down
10 changes: 8 additions & 2 deletions bindings/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
from distutils.command.sdist import sdist
from setuptools.command.bdist_egg import bdist_egg

PYTHON2 = sys.version_info[0] == 2
if PYTHON2:
import io

SYSTEM = sys.platform

# adapted from commit e504b81 of Nguyen Tan Cong
Expand Down Expand Up @@ -216,11 +220,13 @@ def run(self):
author_email='aquynh@gmail.com',
description='Capstone disassembly engine',
url='https://www.capstone-engine.org',
long_description=open('README.txt', encoding="utf8").read(),
long_description=io.open('README.txt', encoding="utf8").read() if PYTHON2 else open('README.txt', encoding="utf8").read(),
long_description_content_type='text/markdown',
python_requires='>=3.6',
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*',
classifiers=[
'License :: OSI Approved :: BSD License',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
],
cmdclass=cmdclass,
Expand Down
1 change: 1 addition & 0 deletions bindings/python/test_aarch64.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>

from __future__ import print_function
from capstone import *
from capstone.aarch64 import *
from xprint import to_hex, to_x, to_x_32
Expand Down
1 change: 1 addition & 0 deletions bindings/python/test_alpha.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Capstone Python bindings, by Dmitry Sibirtsev <sibirtsev_dl@gmail.com>

from __future__ import print_function
from capstone import *
from capstone.alpha import *
from xprint import to_x, to_hex
Expand Down
1 change: 1 addition & 0 deletions bindings/python/test_arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>

from __future__ import print_function
from capstone import *
from capstone.arm import *
from xprint import to_hex, to_x_32
Expand Down
10 changes: 9 additions & 1 deletion bindings/python/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#!/usr/bin/env python3
# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>

from __future__ import print_function
from capstone import *
import binascii
import sys

from xprint import to_hex

_python3 = sys.version_info.major == 3


X86_CODE16 = b"\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00"
X86_CODE32 = b"\xba\xcd\xab\x00\x00\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00"
Expand Down Expand Up @@ -97,7 +102,10 @@ def test_cs_disasm_quick():


def test_different_data_formats():
data = bytes.fromhex('4831C948F7E1043B48BB0A2F62696E2F2F736852530A545F5257545E0F05')
if _python3:
data = bytes.fromhex('4831C948F7E1043B48BB0A2F62696E2F2F736852530A545F5257545E0F05')
else:
data = bytes(bytearray.fromhex('4831C948F7E1043B48BB0A2F62696E2F2F736852530A545F5257545E0F05'))
mnemonics = ['xor', 'mul', 'add', 'movabs', 'push', 'pop', 'push', 'push', 'push', 'pop', 'syscall']
disassembler = Cs(CS_ARCH_X86, CS_MODE_64)
for name, code in (
Expand Down
1 change: 1 addition & 0 deletions bindings/python/test_bpf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Capstone Python bindings
# BPF tests by david942j <david942j@gmail.com>, 2019

from __future__ import print_function
from capstone import *
from capstone.bpf import *
from xprint import to_hex, to_x, to_x_32
Expand Down
1 change: 1 addition & 0 deletions bindings/python/test_customized_mnem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>

from __future__ import print_function
from capstone import *
from capstone.x86 import *
from xprint import to_hex
Expand Down
1 change: 1 addition & 0 deletions bindings/python/test_detail.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
from __future__ import print_function
from capstone import *

from xprint import to_hex
Expand Down
4 changes: 4 additions & 0 deletions bindings/python/test_evm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>

from __future__ import print_function
from capstone import *
import sys

from xprint import to_hex

_python3 = sys.version_info.major == 3


EVM_CODE = b"\x60\x61\x50"

Expand Down
1 change: 1 addition & 0 deletions bindings/python/test_hppa.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Capstone Python bindings, by Dmitry Sibirtsev <sibirtsev_dl@gmail.com>

from __future__ import print_function
from capstone import *
from capstone.hppa import *
from xprint import to_x, to_hex
Expand Down
1 change: 1 addition & 0 deletions bindings/python/test_iter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
from __future__ import print_function
from capstone import *
from xprint import to_hex

Expand Down
1 change: 1 addition & 0 deletions bindings/python/test_lite.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
from __future__ import print_function
from capstone import *
from xprint import to_hex

Expand Down
8 changes: 7 additions & 1 deletion bindings/python/test_m680x.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

# Capstone Python bindings, by Wolfgang Schwotzer <wolfgang.schwotzer@gmx.net>

from __future__ import print_function
import sys
from capstone import *
from capstone.m680x import *
_python3 = sys.version_info.major == 3


s_access = (
Expand Down Expand Up @@ -37,7 +40,10 @@

# print hex dump from string all upper case
def to_hex_uc(string):
return " ".join("0x%02x" % c for c in string)
if _python3:
return " ".join("0x%02x" % c for c in string)
else:
return " ".join("0x%02x" % ord(c) for c in string)

# print short hex dump from byte array all upper case
def to_hex_short_uc(byte_array):
Expand Down
1 change: 1 addition & 0 deletions bindings/python/test_m68k.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

# Capstone Python bindings, by Nicolas PLANEL <nplanel@gmail.com>
from __future__ import print_function
from capstone import *
from capstone.m68k import *
from xprint import to_hex
Expand Down
1 change: 1 addition & 0 deletions bindings/python/test_mips.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
from __future__ import print_function
from capstone import *
from capstone.mips import *
from xprint import to_hex, to_x
Expand Down
1 change: 1 addition & 0 deletions bindings/python/test_mos65xx.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

# Capstone Python bindings, by Sebastian Macke <Sebastian Macke>
from __future__ import print_function
from capstone import *
from capstone.mos65xx import *
from xprint import to_hex, to_x
Expand Down
1 change: 1 addition & 0 deletions bindings/python/test_ppc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
from __future__ import print_function
from capstone import *
from capstone.ppc import *
from xprint import to_hex, to_x, to_x_32
Expand Down
1 change: 1 addition & 0 deletions bindings/python/test_riscv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>

from __future__ import print_function
from capstone import *
from capstone.riscv import *
from xprint import to_x, to_hex
Expand Down
1 change: 1 addition & 0 deletions bindings/python/test_sh.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Capstone Python bindings, by Peace-Maker <peacemakerctf@gmail.com>

from __future__ import print_function
from capstone import *
from capstone.sh import *
from xprint import to_x, to_hex
Expand Down
2 changes: 2 additions & 0 deletions bindings/python/test_skipdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>

from __future__ import print_function
from capstone import *
import binascii
from xprint import to_hex


Expand Down
1 change: 1 addition & 0 deletions bindings/python/test_sparc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>

from __future__ import print_function
from capstone import *
from capstone.sparc import *
from xprint import to_hex, to_x_32
Expand Down
1 change: 1 addition & 0 deletions bindings/python/test_systemz.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>

from __future__ import print_function
from capstone import *
from capstone.systemz import *
from xprint import to_x, to_hex
Expand Down
Loading

0 comments on commit a3fd6ac

Please sign in to comment.