From 897cc2a83b395613a5406749ebdc2e2307036d14 Mon Sep 17 00:00:00 2001 From: Wu ChenXu Date: Mon, 24 Jun 2024 16:16:31 +0800 Subject: [PATCH] Revert "Remove python2 leftovers (#2378)" This reverts commit 60d5b7ec2f62e0115cb0833e6429fb9057f5867a. --- .github/workflows/CITest.yml | 4 ++-- bindings/python/BUILDING.txt | 11 +++++++--- bindings/python/Makefile | 29 ++++++++++++++++++++++++- bindings/python/capstone/__init__.py | 20 +++++++++++++---- bindings/python/setup.py | 10 +++++++-- bindings/python/test_aarch64.py | 1 + bindings/python/test_alpha.py | 1 + bindings/python/test_arm.py | 1 + bindings/python/test_basic.py | 10 ++++++++- bindings/python/test_bpf.py | 1 + bindings/python/test_customized_mnem.py | 1 + bindings/python/test_detail.py | 1 + bindings/python/test_evm.py | 4 ++++ bindings/python/test_hppa.py | 1 + bindings/python/test_iter.py | 1 + bindings/python/test_lite.py | 1 + bindings/python/test_m680x.py | 8 ++++++- bindings/python/test_m68k.py | 1 + bindings/python/test_mips.py | 1 + bindings/python/test_mos65xx.py | 1 + bindings/python/test_ppc.py | 1 + bindings/python/test_riscv.py | 1 + bindings/python/test_sh.py | 1 + bindings/python/test_skipdata.py | 2 ++ bindings/python/test_sparc.py | 1 + bindings/python/test_systemz.py | 1 + bindings/python/test_tms320c64x.py | 1 + bindings/python/test_tricore.py | 1 + bindings/python/test_wasm.py | 1 + bindings/python/test_x86.py | 3 ++- bindings/python/test_xcore.py | 1 + bindings/python/xprint.py | 21 ++++++++++++++---- 32 files changed, 124 insertions(+), 19 deletions(-) diff --git a/.github/workflows/CITest.yml b/.github/workflows/CITest.yml index e37375b08b..cfb781228a 100644 --- a/.github/workflows/CITest.yml +++ b/.github/workflows/CITest.yml @@ -179,7 +179,7 @@ jobs: run: | cp libcapstone.* bindings/python/prebuilt cd bindings/python - make install + make install3 cd .. BUILD_TESTS=no make tests @@ -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 diff --git a/bindings/python/BUILDING.txt b/bindings/python/BUILDING.txt index 8b84b44927..e527b153eb 100644 --- a/bindings/python/BUILDING.txt +++ b/bindings/python/BUILDING.txt @@ -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: @@ -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. @@ -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 diff --git a/bindings/python/Makefile b/bindings/python/Makefile index 4fc8b8f49e..10b9af3134 100644 --- a/bindings/python/Makefile +++ b/bindings/python/Makefile @@ -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}"; \ @@ -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}"; \ @@ -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 diff --git a/bindings/python/capstone/__init__.py b/bindings/python/capstone/__init__.py index 288b705688..154e6f1105 100755 --- a/bindings/python/capstone/__init__.py +++ b/bindings/python/capstone/__init__.py @@ -1,6 +1,9 @@ # Capstone Python bindings, by Nguyen Anh Quynnh -import os -import sys +import os, sys +from platform import system +_python2 = sys.version_info[0] < 3 +if _python2: + range = xrange __all__ = [ 'Cs', @@ -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 @@ -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) diff --git a/bindings/python/setup.py b/bindings/python/setup.py index 1f69803f85..55314760fc 100755 --- a/bindings/python/setup.py +++ b/bindings/python/setup.py @@ -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 @@ -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, diff --git a/bindings/python/test_aarch64.py b/bindings/python/test_aarch64.py index 12f8ee083a..00d32009fe 100755 --- a/bindings/python/test_aarch64.py +++ b/bindings/python/test_aarch64.py @@ -2,6 +2,7 @@ # Capstone Python bindings, by Nguyen Anh Quynnh +from __future__ import print_function from capstone import * from capstone.aarch64 import * from xprint import to_hex, to_x, to_x_32 diff --git a/bindings/python/test_alpha.py b/bindings/python/test_alpha.py index 056288ae2d..404371ea31 100755 --- a/bindings/python/test_alpha.py +++ b/bindings/python/test_alpha.py @@ -2,6 +2,7 @@ # Capstone Python bindings, by Dmitry Sibirtsev +from __future__ import print_function from capstone import * from capstone.alpha import * from xprint import to_x, to_hex diff --git a/bindings/python/test_arm.py b/bindings/python/test_arm.py index 3056744628..f220884cb0 100755 --- a/bindings/python/test_arm.py +++ b/bindings/python/test_arm.py @@ -2,6 +2,7 @@ # Capstone Python bindings, by Nguyen Anh Quynnh +from __future__ import print_function from capstone import * from capstone.arm import * from xprint import to_hex, to_x_32 diff --git a/bindings/python/test_basic.py b/bindings/python/test_basic.py index a114b1f0c5..9e893ba982 100755 --- a/bindings/python/test_basic.py +++ b/bindings/python/test_basic.py @@ -1,10 +1,15 @@ #!/usr/bin/env python3 # Capstone Python bindings, by Nguyen Anh Quynnh +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" @@ -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 ( diff --git a/bindings/python/test_bpf.py b/bindings/python/test_bpf.py index f7f5538a07..721a2b2ecb 100755 --- a/bindings/python/test_bpf.py +++ b/bindings/python/test_bpf.py @@ -3,6 +3,7 @@ # Capstone Python bindings # BPF tests by david942j , 2019 +from __future__ import print_function from capstone import * from capstone.bpf import * from xprint import to_hex, to_x, to_x_32 diff --git a/bindings/python/test_customized_mnem.py b/bindings/python/test_customized_mnem.py index 157e6d096d..2efc0a99bc 100755 --- a/bindings/python/test_customized_mnem.py +++ b/bindings/python/test_customized_mnem.py @@ -2,6 +2,7 @@ # Capstone Python bindings, by Nguyen Anh Quynnh +from __future__ import print_function from capstone import * from capstone.x86 import * from xprint import to_hex diff --git a/bindings/python/test_detail.py b/bindings/python/test_detail.py index 4f966109fd..f57797bc0e 100755 --- a/bindings/python/test_detail.py +++ b/bindings/python/test_detail.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 # Capstone Python bindings, by Nguyen Anh Quynnh +from __future__ import print_function from capstone import * from xprint import to_hex diff --git a/bindings/python/test_evm.py b/bindings/python/test_evm.py index 3fd90fdfc5..86dffab696 100755 --- a/bindings/python/test_evm.py +++ b/bindings/python/test_evm.py @@ -2,10 +2,14 @@ # Capstone Python bindings, by Nguyen Anh Quynnh +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" diff --git a/bindings/python/test_hppa.py b/bindings/python/test_hppa.py index dfbe08fa3d..15e0e6017e 100755 --- a/bindings/python/test_hppa.py +++ b/bindings/python/test_hppa.py @@ -2,6 +2,7 @@ # Capstone Python bindings, by Dmitry Sibirtsev +from __future__ import print_function from capstone import * from capstone.hppa import * from xprint import to_x, to_hex diff --git a/bindings/python/test_iter.py b/bindings/python/test_iter.py index 26ae93bc43..89b9656248 100755 --- a/bindings/python/test_iter.py +++ b/bindings/python/test_iter.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 # Capstone Python bindings, by Nguyen Anh Quynnh +from __future__ import print_function from capstone import * from xprint import to_hex diff --git a/bindings/python/test_lite.py b/bindings/python/test_lite.py index 524372043f..44569a038f 100755 --- a/bindings/python/test_lite.py +++ b/bindings/python/test_lite.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 # Capstone Python bindings, by Nguyen Anh Quynnh +from __future__ import print_function from capstone import * from xprint import to_hex diff --git a/bindings/python/test_m680x.py b/bindings/python/test_m680x.py index 8512ff0ccb..07dbfc683b 100755 --- a/bindings/python/test_m680x.py +++ b/bindings/python/test_m680x.py @@ -2,8 +2,11 @@ # Capstone Python bindings, by Wolfgang Schwotzer +from __future__ import print_function +import sys from capstone import * from capstone.m680x import * +_python3 = sys.version_info.major == 3 s_access = ( @@ -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): diff --git a/bindings/python/test_m68k.py b/bindings/python/test_m68k.py index 6efdfaedbd..124bf357a3 100755 --- a/bindings/python/test_m68k.py +++ b/bindings/python/test_m68k.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 # Capstone Python bindings, by Nicolas PLANEL +from __future__ import print_function from capstone import * from capstone.m68k import * from xprint import to_hex diff --git a/bindings/python/test_mips.py b/bindings/python/test_mips.py index b6a2502995..7de53749ad 100755 --- a/bindings/python/test_mips.py +++ b/bindings/python/test_mips.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 # Capstone Python bindings, by Nguyen Anh Quynnh +from __future__ import print_function from capstone import * from capstone.mips import * from xprint import to_hex, to_x diff --git a/bindings/python/test_mos65xx.py b/bindings/python/test_mos65xx.py index ae5db53c54..51c12b9444 100755 --- a/bindings/python/test_mos65xx.py +++ b/bindings/python/test_mos65xx.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 # Capstone Python bindings, by Sebastian Macke +from __future__ import print_function from capstone import * from capstone.mos65xx import * from xprint import to_hex, to_x diff --git a/bindings/python/test_ppc.py b/bindings/python/test_ppc.py index 9f2b493865..87442c6b77 100755 --- a/bindings/python/test_ppc.py +++ b/bindings/python/test_ppc.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 # Capstone Python bindings, by Nguyen Anh Quynnh +from __future__ import print_function from capstone import * from capstone.ppc import * from xprint import to_hex, to_x, to_x_32 diff --git a/bindings/python/test_riscv.py b/bindings/python/test_riscv.py index 7f7acae25e..892de005f3 100755 --- a/bindings/python/test_riscv.py +++ b/bindings/python/test_riscv.py @@ -2,6 +2,7 @@ # Capstone Python bindings, by Nguyen Anh Quynnh +from __future__ import print_function from capstone import * from capstone.riscv import * from xprint import to_x, to_hex diff --git a/bindings/python/test_sh.py b/bindings/python/test_sh.py index e5c2f3514e..03dfed9011 100755 --- a/bindings/python/test_sh.py +++ b/bindings/python/test_sh.py @@ -2,6 +2,7 @@ # Capstone Python bindings, by Peace-Maker +from __future__ import print_function from capstone import * from capstone.sh import * from xprint import to_x, to_hex diff --git a/bindings/python/test_skipdata.py b/bindings/python/test_skipdata.py index 0fd7d7fca1..98f027bf4f 100755 --- a/bindings/python/test_skipdata.py +++ b/bindings/python/test_skipdata.py @@ -2,7 +2,9 @@ # Capstone Python bindings, by Nguyen Anh Quynnh +from __future__ import print_function from capstone import * +import binascii from xprint import to_hex diff --git a/bindings/python/test_sparc.py b/bindings/python/test_sparc.py index e1b1344ab3..214a16eff6 100755 --- a/bindings/python/test_sparc.py +++ b/bindings/python/test_sparc.py @@ -2,6 +2,7 @@ # Capstone Python bindings, by Nguyen Anh Quynnh +from __future__ import print_function from capstone import * from capstone.sparc import * from xprint import to_hex, to_x_32 diff --git a/bindings/python/test_systemz.py b/bindings/python/test_systemz.py index a628494fd8..175cda4179 100755 --- a/bindings/python/test_systemz.py +++ b/bindings/python/test_systemz.py @@ -2,6 +2,7 @@ # Capstone Python bindings, by Nguyen Anh Quynnh +from __future__ import print_function from capstone import * from capstone.systemz import * from xprint import to_x, to_hex diff --git a/bindings/python/test_tms320c64x.py b/bindings/python/test_tms320c64x.py index 1bb64b0c11..c6851d2ddb 100755 --- a/bindings/python/test_tms320c64x.py +++ b/bindings/python/test_tms320c64x.py @@ -2,6 +2,7 @@ # Capstone Python bindings, by Fotis Loukos +from __future__ import print_function from capstone import * from capstone.tms320c64x import * from xprint import to_x, to_hex, to_x_32 diff --git a/bindings/python/test_tricore.py b/bindings/python/test_tricore.py index 632a026ada..e49720dfb9 100755 --- a/bindings/python/test_tricore.py +++ b/bindings/python/test_tricore.py @@ -2,6 +2,7 @@ # Capstone Python bindings, by Nguyen Anh Quynnh +from __future__ import print_function from capstone import * from capstone.tricore import * from xprint import to_hex, to_x diff --git a/bindings/python/test_wasm.py b/bindings/python/test_wasm.py index eea8549827..9aade84c2d 100755 --- a/bindings/python/test_wasm.py +++ b/bindings/python/test_wasm.py @@ -2,6 +2,7 @@ # Capstone Python bindings, by Peace-Maker +from __future__ import print_function from capstone import * from capstone.wasm import * from xprint import to_hex diff --git a/bindings/python/test_x86.py b/bindings/python/test_x86.py index 4bcf4238bd..9684cdf3da 100755 --- a/bindings/python/test_x86.py +++ b/bindings/python/test_x86.py @@ -1,9 +1,10 @@ #!/usr/bin/env python3 # Capstone Python bindings, by Nguyen Anh Quynnh +from __future__ import print_function from capstone import * from capstone.x86 import * -from xprint import to_hex, to_x +from xprint import to_hex, to_x, to_x_32 X86_CODE64 = b"\x55\x48\x8b\x05\xb8\x13\x00\x00\xe9\xea\xbe\xad\xde\xff\x25\x23\x01\x00\x00\xe8\xdf\xbe\xad\xde\x74\xff" diff --git a/bindings/python/test_xcore.py b/bindings/python/test_xcore.py index 628a5a0c8c..d0531a7aca 100755 --- a/bindings/python/test_xcore.py +++ b/bindings/python/test_xcore.py @@ -2,6 +2,7 @@ # Capstone Python bindings, by Nguyen Anh Quynnh +from __future__ import print_function from capstone import * from capstone.xcore import * from xprint import to_x, to_hex diff --git a/bindings/python/xprint.py b/bindings/python/xprint.py index 5e31dcf448..70affaca5c 100755 --- a/bindings/python/xprint.py +++ b/bindings/python/xprint.py @@ -1,15 +1,28 @@ #!/usr/bin/env python # Capstone Python bindings, by Nguyen Anh Quynnh +from __future__ import print_function +import sys +_python3 = sys.version_info.major == 3 + def to_hex(s, prefix_0x = True): - if prefix_0x: - return " ".join("0x{0:02x}".format(c) for c in s) # <-- Python 3 is OK + if _python3: + if prefix_0x: + return " ".join("0x{0:02x}".format(c) for c in s) # <-- Python 3 is OK + else: + return " ".join("{0:02x}".format(c) for c in s) # <-- Python 3 is OK else: - return " ".join("{0:02x}".format(c) for c in s) # <-- Python 3 is OK + if prefix_0x: + return " ".join("0x{0:02x}".format(ord(c)) for c in s) + else: + return " ".join("{0:02x}".format(ord(c)) for c in s) def to_hex2(s): - r = "".join("{0:02x}".format(c) for c in s) # <-- Python 3 is OK + if _python3: + r = "".join("{0:02x}".format(c) for c in s) # <-- Python 3 is OK + else: + r = "".join("{0:02x}".format(ord(c)) for c in s) while r[0] == '0': r = r[1:] return r