From 45e70333fd70ec2449a0a95a7a7f4f68c47a36d5 Mon Sep 17 00:00:00 2001 From: Siu Kwan Lam Date: Fri, 22 Feb 2013 19:17:56 -0600 Subject: [PATCH] Adapt to python 3.3 --- llvmpy/capsule.cpp | 1 + llvmpy/capsule.py | 12 ++- llvmpy/extra.py | 2 +- llvmpy/gen/binding.py | 2 +- llvmpy/gen/codegen.py | 2 +- llvmpy/gen/gen.py | 1 + llvmpy/include/llvm_binding/conversion.h | 23 ++++++ llvmpy/include/llvm_binding/extra.h | 8 +- llvmpy/include/python3adapt.h | 32 ++++++++ llvmpy/src/ADT/Triple.py | 2 +- llvmpy/src/Argument.py | 6 +- llvmpy/src/Attributes.py | 4 +- llvmpy/src/BasicBlock.py | 10 +-- llvmpy/src/Bitcode/ReaderWriter.py | 2 +- llvmpy/src/CallingConv.py | 2 +- llvmpy/src/Constant.py | 20 ++--- llvmpy/src/DataLayout.py | 16 ++-- llvmpy/src/DerivedTypes.py | 8 +- llvmpy/src/EngineBuilder.py | 16 ++-- llvmpy/src/ExecutionEngine/ExecutionEngine.py | 2 +- llvmpy/src/Function.py | 16 ++-- llvmpy/src/GenericValue.py | 4 +- llvmpy/src/GlobalValue.py | 8 +- llvmpy/src/GlobalVariable.py | 12 +-- llvmpy/src/IRBuilder.py | 28 +++---- llvmpy/src/InlineAsm.py | 8 +- llvmpy/src/Instruction.py | 12 +-- llvmpy/src/Intrinsics.py | 6 +- llvmpy/src/JITMemoryManager.py | 2 +- llvmpy/src/LLVMContext.py | 2 +- llvmpy/src/Linker.py | 8 +- llvmpy/src/Metadata.py | 16 ++-- llvmpy/src/Module.py | 22 +++--- llvmpy/src/Pass.py | 8 +- llvmpy/src/PassManager.py | 8 +- llvmpy/src/PassRegistry.py | 2 +- llvmpy/src/PassSupport.py | 2 +- llvmpy/src/Support/FormattedStream.py | 2 +- llvmpy/src/Type.py | 8 +- llvmpy/src/User.py | 4 +- llvmpy/src/Value.py | 12 +-- llvmpy/src/__init__.py | 5 +- llvmpy/test_binding.py | 73 ++++++++++--------- 43 files changed, 251 insertions(+), 188 deletions(-) create mode 100644 llvmpy/include/python3adapt.h diff --git a/llvmpy/capsule.cpp b/llvmpy/capsule.cpp index b7fa81b..561b28a 100644 --- a/llvmpy/capsule.cpp +++ b/llvmpy/capsule.cpp @@ -1,4 +1,5 @@ #include +#include #include #include diff --git a/llvmpy/capsule.py b/llvmpy/capsule.py index 0b1f203..ec6fdde 100644 --- a/llvmpy/capsule.py +++ b/llvmpy/capsule.py @@ -32,7 +32,7 @@ def _capsule_weakref_dtor(item): class Capsule(object): "Wraps PyCapsule so that we can build weakref of it." - from _capsule import check, getClassName, getName, getPointer + from ._capsule import check, getClassName, getName, getPointer def __init__(self, capsule): assert Capsule.valid(capsule) @@ -75,6 +75,9 @@ class Capsule(object): else: return False + def __hash__(self): + return super(Capsule, self).__hash__() + def __ne__(self, other): return not (self == other) @@ -122,7 +125,7 @@ def wrap(cap, owned=False): ''' if not Capsule.valid(cap): if isinstance(cap, list): - return map(wrap, cap) + return list(map(wrap, cap)) return cap # bypass if cap is not a PyCapsule and not a list cap = Capsule(cap) @@ -170,6 +173,9 @@ class Wrapper(object): def _ptr(self): return self._capsule.capsule + def __hash__(self): + return super(Wrapper, self).__hash__() + def __eq__(self, other): return self._capsule == other._capsule @@ -184,7 +190,7 @@ class Wrapper(object): return hasattr(cls, '_delete_') def downcast(obj, cls): - import _api + from . import _api if type(obj) is cls: return obj fromty = obj._llvm_type_ diff --git a/llvmpy/extra.py b/llvmpy/extra.py index a2e744f..99035a7 100644 --- a/llvmpy/extra.py +++ b/llvmpy/extra.py @@ -11,7 +11,7 @@ from llvmpy import _api def _wrapper(func): "Wrap the re-exported functions" def _core(*args): - unwrapped = map(capsule.unwrap, args) + unwrapped = list(map(capsule.unwrap, args)) ret = func(*unwrapped) return capsule.wrap(ret) return _core diff --git a/llvmpy/gen/binding.py b/llvmpy/gen/binding.py index 4454d72..0f5c4fd 100644 --- a/llvmpy/gen/binding.py +++ b/llvmpy/gen/binding.py @@ -348,7 +348,7 @@ class Enum(object): def __init__(self, *value_names): self.parent = None if len(value_names) == 1: - value_names = filter(bool, value_names[0].replace(',', ' ').split()) + value_names = list(filter(bool, value_names[0].replace(',', ' ').split())) self.value_names = value_names self.includes = set() diff --git a/llvmpy/gen/codegen.py b/llvmpy/gen/codegen.py index abde437..f9585fd 100644 --- a/llvmpy/gen/codegen.py +++ b/llvmpy/gen/codegen.py @@ -259,7 +259,7 @@ class PyCodeWriter(CodeWriterBase): def unwrap_many(self, args): unwrapped = self.new_symbol('unwrapped') - self.println('%(unwrapped)s = map(capsule.unwrap, %(args)s)' % locals()) + self.println('%(unwrapped)s = list(map(capsule.unwrap, %(args)s))' % locals()) return unwrapped def unwrap(self, val): diff --git a/llvmpy/gen/gen.py b/llvmpy/gen/gen.py index 066ed23..263c289 100644 --- a/llvmpy/gen/gen.py +++ b/llvmpy/gen/gen.py @@ -41,6 +41,7 @@ def populate_headers(println): includes = [ 'cstring', 'Python.h', + 'python3adapt.h', 'capsulethunk.h', 'llvm_binding/conversion.h', 'llvm_binding/binding.h', diff --git a/llvmpy/include/llvm_binding/conversion.h b/llvmpy/include/llvm_binding/conversion.h index 7b8e6df..abc3749 100644 --- a/llvmpy/include/llvm_binding/conversion.h +++ b/llvmpy/include/llvm_binding/conversion.h @@ -4,6 +4,29 @@ // python object unwrapper +static +int py_bytes_to(PyObject *bytesobj, llvm::StringRef &strref){ + // type check + if (!PyBytes_Check(bytesobj)) { + // raises TypeError + PyErr_SetString(PyExc_TypeError, "Expecting a str"); + return 0; + } + // get len and buffer + const Py_ssize_t len = PyBytes_Size(bytesobj); + const char * buf = PyBytes_AsString(bytesobj); + if (!buf) { + // raises TypeError + return 0; + } + // set output + strref = llvm::StringRef(buf, len); + // success + return 1; +} + + + static int py_str_to(PyObject *strobj, llvm::StringRef &strref){ // type check diff --git a/llvmpy/include/llvm_binding/extra.h b/llvmpy/include/llvm_binding/extra.h index 76c8a16..3874bb3 100644 --- a/llvmpy/include/llvm_binding/extra.h +++ b/llvmpy/include/llvm_binding/extra.h @@ -497,11 +497,9 @@ PyObject* llvm_WriteBitcodeToFile(const llvm::Module *M, PyObject* FObj) llvm::WriteBitcodeToFile(M, rso); rso.flush(); StringRef ref = rso.str(); - PyObject* buf = PyString_FromStringAndSize(ref.data(), ref.size()); - if (-1 == PyFile_WriteObject(buf, FObj, Py_PRINT_RAW)){ - return NULL; - } - Py_RETURN_NONE; + PyObject* buf = PyBytes_FromStringAndSize(ref.data(), ref.size()); + puts(PyString_AsString(PyObject_Str(PyObject_Type(buf)))); + return PyObject_CallMethod(FObj, "write", "O", buf); } static diff --git a/llvmpy/include/python3adapt.h b/llvmpy/include/python3adapt.h new file mode 100644 index 0000000..5b19ebe --- /dev/null +++ b/llvmpy/include/python3adapt.h @@ -0,0 +1,32 @@ +#ifndef PYTHON3ADAPT_H +#define PYTHON3ADAPT_H + +#if (PY_VERSION_HEX < 0x03000000) + +#define PyBytes_Check PyString_Check +#define PyBytes_Size PyString_Size +#define PyBytes_AsString PyString_AsString +#define PyBytes_FromStringAndSize PyString_FromStringAndSize +#define PyBytes_FromString PyString_FromString + +#endif + +#if (PY_VERSION_HEX >= 0x03000000) + +#define PyString_Check PyUnicode_Check +#define PyString_Size PyUnicode_GET_SIZE +#define PyString_AsString PyUnicode_AsUTF8 +#define PyString_FromStringAndSize PyUnicode_FromStringAndSize +#define PyString_FromString PyUnicode_FromString + +#define PyInt_Check PyLong_Check +#define PyInt_FromLong PyLong_FromLong +#define PyInt_AsLong PyLong_AsLong +#define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask +#define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask + +#define PyFile_Check(x) (1) + +#endif + +#endif //PYTHON3ADAPT_H \ No newline at end of file diff --git a/llvmpy/src/ADT/Triple.py b/llvmpy/src/ADT/Triple.py index 82de57e..1615c1c 100644 --- a/llvmpy/src/ADT/Triple.py +++ b/llvmpy/src/ADT/Triple.py @@ -1,6 +1,6 @@ from binding import * from ..namespace import llvm -from StringRef import StringRef +from .StringRef import StringRef Triple = llvm.Class() diff --git a/llvmpy/src/Argument.py b/llvmpy/src/Argument.py index 64db072..4f0b7a4 100644 --- a/llvmpy/src/Argument.py +++ b/llvmpy/src/Argument.py @@ -1,7 +1,7 @@ from binding import * -from namespace import llvm -from Value import Argument, Value -from Attributes import Attributes +from .namespace import llvm +from .Value import Argument, Value +from .Attributes import Attributes @Argument class Argument: diff --git a/llvmpy/src/Attributes.py b/llvmpy/src/Attributes.py index c6a8d97..4fe7651 100644 --- a/llvmpy/src/Attributes.py +++ b/llvmpy/src/Attributes.py @@ -1,6 +1,6 @@ from binding import * -from namespace import llvm -from LLVMContext import LLVMContext +from .namespace import llvm +from .LLVMContext import LLVMContext llvm.includes.add('llvm/Attributes.h') diff --git a/llvmpy/src/BasicBlock.py b/llvmpy/src/BasicBlock.py index 4ed4f66..77eaf01 100644 --- a/llvmpy/src/BasicBlock.py +++ b/llvmpy/src/BasicBlock.py @@ -1,9 +1,9 @@ from binding import * -from namespace import llvm -from Value import Function, BasicBlock -from Instruction import Instruction, TerminatorInst -from LLVMContext import LLVMContext -from ADT.StringRef import StringRef +from .namespace import llvm +from .Value import Function, BasicBlock +from .Instruction import Instruction, TerminatorInst +from .LLVMContext import LLVMContext +from .ADT.StringRef import StringRef @BasicBlock class BasicBlock: diff --git a/llvmpy/src/Bitcode/ReaderWriter.py b/llvmpy/src/Bitcode/ReaderWriter.py index a8e8c43..b950ba4 100644 --- a/llvmpy/src/Bitcode/ReaderWriter.py +++ b/llvmpy/src/Bitcode/ReaderWriter.py @@ -9,7 +9,7 @@ llvm.includes.add('llvm/Bitcode/ReaderWriter.h') ParseBitCodeFile = llvm.CustomFunction('ParseBitCodeFile', 'llvm_ParseBitCodeFile', PyObjectPtr, # returns Module* - cast(str, StringRef), + cast(bytes, StringRef), ref(LLVMContext), PyObjectPtr, # file-like object ).require_only(2) diff --git a/llvmpy/src/CallingConv.py b/llvmpy/src/CallingConv.py index 71b4ad3..efca1fc 100644 --- a/llvmpy/src/CallingConv.py +++ b/llvmpy/src/CallingConv.py @@ -1,5 +1,5 @@ from binding import * -from namespace import llvm +from .namespace import llvm CallingConv = llvm.Namespace('CallingConv') ID = CallingConv.Enum('ID', ''' diff --git a/llvmpy/src/Constant.py b/llvmpy/src/Constant.py index 3cbb40b..70dc3bf 100644 --- a/llvmpy/src/Constant.py +++ b/llvmpy/src/Constant.py @@ -1,14 +1,14 @@ from binding import * -from namespace import llvm -from Value import Value -from Value import Constant, UndefValue, ConstantInt, ConstantFP, ConstantArray -from Value import ConstantStruct, ConstantVector, ConstantVector -from Value import ConstantDataSequential, ConstantDataArray, ConstantExpr -from LLVMContext import LLVMContext -from ADT.StringRef import StringRef -from ADT.SmallVector import SmallVector_Value, SmallVector_Unsigned -from Type import Type, IntegerType, ArrayType, StructType -from Instruction import CmpInst +from .namespace import llvm +from .Value import Value +from .Value import Constant, UndefValue, ConstantInt, ConstantFP, ConstantArray +from .Value import ConstantStruct, ConstantVector, ConstantVector +from .Value import ConstantDataSequential, ConstantDataArray, ConstantExpr +from .LLVMContext import LLVMContext +from .ADT.StringRef import StringRef +from .ADT.SmallVector import SmallVector_Value, SmallVector_Unsigned +from .Type import Type, IntegerType, ArrayType, StructType +from .Instruction import CmpInst @Constant class Constant: diff --git a/llvmpy/src/DataLayout.py b/llvmpy/src/DataLayout.py index 9582d53..7085f52 100644 --- a/llvmpy/src/DataLayout.py +++ b/llvmpy/src/DataLayout.py @@ -1,16 +1,16 @@ from binding import * -from namespace import llvm -from Pass import ImmutablePass +from .namespace import llvm +from .Pass import ImmutablePass DataLayout = llvm.Class(ImmutablePass) StructLayout = llvm.Class() -from LLVMContext import LLVMContext -from ADT.StringRef import StringRef -from Module import Module -from Type import Type, IntegerType, StructType -from ADT.SmallVector import SmallVector_Value -from GlobalVariable import GlobalVariable +from .LLVMContext import LLVMContext +from .ADT.StringRef import StringRef +from .Module import Module +from .Type import Type, IntegerType, StructType +from .ADT.SmallVector import SmallVector_Value +from .GlobalVariable import GlobalVariable @DataLayout diff --git a/llvmpy/src/DerivedTypes.py b/llvmpy/src/DerivedTypes.py index 40724e0..02eec8c 100644 --- a/llvmpy/src/DerivedTypes.py +++ b/llvmpy/src/DerivedTypes.py @@ -1,8 +1,8 @@ from binding import * -from namespace import llvm -from LLVMContext import LLVMContext -from Type import Type -from ADT.SmallVector import SmallVector_Type +from .namespace import llvm +from .LLVMContext import LLVMContext +from .Type import Type +from .ADT.SmallVector import SmallVector_Type FunctionType = llvm.Class(Type) diff --git a/llvmpy/src/EngineBuilder.py b/llvmpy/src/EngineBuilder.py index c3feafe..4e8ed12 100644 --- a/llvmpy/src/EngineBuilder.py +++ b/llvmpy/src/EngineBuilder.py @@ -1,12 +1,12 @@ from binding import * -from namespace import llvm -from Module import Module -from JITMemoryManager import JITMemoryManager -from Support.CodeGen import CodeGenOpt, Reloc, CodeModel -from ADT.StringRef import StringRef -from ExecutionEngine.ExecutionEngine import ExecutionEngine -from Target.TargetMachine import TargetMachine -from ADT.Triple import Triple +from .namespace import llvm +from .Module import Module +from .JITMemoryManager import JITMemoryManager +from .Support.CodeGen import CodeGenOpt, Reloc, CodeModel +from .ADT.StringRef import StringRef +from .ExecutionEngine.ExecutionEngine import ExecutionEngine +from .Target.TargetMachine import TargetMachine +from .ADT.Triple import Triple EngineBuilder = llvm.Class() diff --git a/llvmpy/src/ExecutionEngine/ExecutionEngine.py b/llvmpy/src/ExecutionEngine/ExecutionEngine.py index bcf66fd..038d344 100644 --- a/llvmpy/src/ExecutionEngine/ExecutionEngine.py +++ b/llvmpy/src/ExecutionEngine/ExecutionEngine.py @@ -98,6 +98,6 @@ class ExecutionEngine: @CustomPythonMethod def runFunction(self, fn, args): from llvmpy import capsule - unwrapped = map(capsule.unwrap, args) + unwrapped = list(map(capsule.unwrap, args)) return self._runFunction(fn, tuple(unwrapped)) diff --git a/llvmpy/src/Function.py b/llvmpy/src/Function.py index 52db622..55a50d9 100644 --- a/llvmpy/src/Function.py +++ b/llvmpy/src/Function.py @@ -1,12 +1,12 @@ from binding import * -from namespace import llvm -from Value import GlobalValue, Constant, Function, Argument, Value -from BasicBlock import BasicBlock -from Attributes import Attributes -from Type import Type -from DerivedTypes import FunctionType -from LLVMContext import LLVMContext -from CallingConv import CallingConv +from .namespace import llvm +from .Value import GlobalValue, Constant, Function, Argument, Value +from .BasicBlock import BasicBlock +from .Attributes import Attributes +from .Type import Type +from .DerivedTypes import FunctionType +from .LLVMContext import LLVMContext +from .CallingConv import CallingConv @Function class Function: diff --git a/llvmpy/src/GenericValue.py b/llvmpy/src/GenericValue.py index 73a3709..e587b5f 100644 --- a/llvmpy/src/GenericValue.py +++ b/llvmpy/src/GenericValue.py @@ -1,6 +1,6 @@ from binding import * -from namespace import llvm -from Type import Type +from .namespace import llvm +from .Type import Type GenericValue = llvm.Class() diff --git a/llvmpy/src/GlobalValue.py b/llvmpy/src/GlobalValue.py index f21d5af..2583c6a 100644 --- a/llvmpy/src/GlobalValue.py +++ b/llvmpy/src/GlobalValue.py @@ -1,8 +1,8 @@ from binding import * -from namespace import llvm -from Value import GlobalValue -from Module import Module -from ADT.StringRef import StringRef +from .namespace import llvm +from .Value import GlobalValue +from .Module import Module +from .ADT.StringRef import StringRef @GlobalValue class GlobalValue: diff --git a/llvmpy/src/GlobalVariable.py b/llvmpy/src/GlobalVariable.py index 2280e24..c38df27 100644 --- a/llvmpy/src/GlobalVariable.py +++ b/llvmpy/src/GlobalVariable.py @@ -1,13 +1,13 @@ from binding import * -from namespace import llvm +from .namespace import llvm -from GlobalValue import GlobalValue +from .GlobalValue import GlobalValue GlobalVariable = llvm.Class(GlobalValue) -from Module import Module -from Type import Type -from ADT.StringRef import StringRef -from Value import Constant +from .Module import Module +from .Type import Type +from .ADT.StringRef import StringRef +from .Value import Constant @GlobalVariable class GlobalVariable: diff --git a/llvmpy/src/IRBuilder.py b/llvmpy/src/IRBuilder.py index efdf453..afd78a0 100644 --- a/llvmpy/src/IRBuilder.py +++ b/llvmpy/src/IRBuilder.py @@ -1,18 +1,18 @@ from binding import * -from namespace import llvm -from LLVMContext import LLVMContext -from BasicBlock import BasicBlock -from Instruction import Instruction -from Instruction import ReturnInst, CallInst, BranchInst, SwitchInst -from Instruction import IndirectBrInst, InvokeInst, ResumeInst, PHINode -from Instruction import UnreachableInst, AllocaInst, LoadInst, StoreInst -from Instruction import FenceInst, AtomicCmpXchgInst, AtomicRMWInst, CmpInst -from Instruction import LandingPadInst, VAArgInst -from Instruction import AtomicOrdering, SynchronizationScope -from ADT.SmallVector import SmallVector_Value, SmallVector_Unsigned -from ADT.StringRef import StringRef -from Value import Value, MDNode -from Type import Type, IntegerType +from .namespace import llvm +from .LLVMContext import LLVMContext +from .BasicBlock import BasicBlock +from .Instruction import Instruction +from .Instruction import ReturnInst, CallInst, BranchInst, SwitchInst +from .Instruction import IndirectBrInst, InvokeInst, ResumeInst, PHINode +from .Instruction import UnreachableInst, AllocaInst, LoadInst, StoreInst +from .Instruction import FenceInst, AtomicCmpXchgInst, AtomicRMWInst, CmpInst +from .Instruction import LandingPadInst, VAArgInst +from .Instruction import AtomicOrdering, SynchronizationScope +from .ADT.SmallVector import SmallVector_Value, SmallVector_Unsigned +from .ADT.StringRef import StringRef +from .Value import Value, MDNode +from .Type import Type, IntegerType IRBuilder = llvm.Class() diff --git a/llvmpy/src/InlineAsm.py b/llvmpy/src/InlineAsm.py index a4ce16b..8793343 100644 --- a/llvmpy/src/InlineAsm.py +++ b/llvmpy/src/InlineAsm.py @@ -1,8 +1,8 @@ from binding import * -from namespace import llvm -from Value import Value -from DerivedTypes import FunctionType -from ADT.StringRef import StringRef +from .namespace import llvm +from .Value import Value +from .DerivedTypes import FunctionType +from .ADT.StringRef import StringRef llvm.includes.add('llvm/InlineAsm.h') diff --git a/llvmpy/src/Instruction.py b/llvmpy/src/Instruction.py index 4761e16..1974aad 100644 --- a/llvmpy/src/Instruction.py +++ b/llvmpy/src/Instruction.py @@ -1,6 +1,6 @@ from binding import * -from namespace import llvm -from Value import Value, MDNode, User, BasicBlock, Function, ConstantInt +from .namespace import llvm +from .Value import Value, MDNode, User, BasicBlock, Function, ConstantInt Instruction = llvm.Class(User) @@ -63,10 +63,10 @@ SynchronizationScope = llvm.Enum('SynchronizationScope', -from ADT.StringRef import StringRef -from CallingConv import CallingConv -from Attributes import Attributes -from Type import Type +from .ADT.StringRef import StringRef +from .CallingConv import CallingConv +from .Attributes import Attributes +from .Type import Type diff --git a/llvmpy/src/Intrinsics.py b/llvmpy/src/Intrinsics.py index 5314112..7e1decd 100644 --- a/llvmpy/src/Intrinsics.py +++ b/llvmpy/src/Intrinsics.py @@ -1,8 +1,8 @@ from binding import * -from namespace import llvm +from .namespace import llvm -from Module import Module -from Function import Function +from .Module import Module +from .Function import Function Intrinsic = llvm.Namespace('Intrinsic') diff --git a/llvmpy/src/JITMemoryManager.py b/llvmpy/src/JITMemoryManager.py index f134735..e34e678 100644 --- a/llvmpy/src/JITMemoryManager.py +++ b/llvmpy/src/JITMemoryManager.py @@ -1,5 +1,5 @@ from binding import * -from namespace import llvm +from .namespace import llvm @llvm.Class() class JITMemoryManager: diff --git a/llvmpy/src/LLVMContext.py b/llvmpy/src/LLVMContext.py index f2526db..4a6b49a 100644 --- a/llvmpy/src/LLVMContext.py +++ b/llvmpy/src/LLVMContext.py @@ -1,5 +1,5 @@ from binding import * -from namespace import llvm +from .namespace import llvm @llvm.Class() class LLVMContext: diff --git a/llvmpy/src/Linker.py b/llvmpy/src/Linker.py index acca503..960bd90 100644 --- a/llvmpy/src/Linker.py +++ b/llvmpy/src/Linker.py @@ -1,8 +1,8 @@ from binding import * -from namespace import llvm -from ADT.StringRef import StringRef -from Module import Module -from LLVMContext import LLVMContext +from .namespace import llvm +from .ADT.StringRef import StringRef +from .Module import Module +from .LLVMContext import LLVMContext llvm.includes.add('llvm/Linker.h') diff --git a/llvmpy/src/Metadata.py b/llvmpy/src/Metadata.py index d5cc4b1..cfeb6ce 100644 --- a/llvmpy/src/Metadata.py +++ b/llvmpy/src/Metadata.py @@ -1,12 +1,12 @@ from binding import * -from namespace import llvm -from Value import Value, MDNode, MDString -from LLVMContext import LLVMContext -from ADT.StringRef import StringRef -from Module import Module -from Function import Function -from Support.raw_ostream import raw_ostream -from Assembly.AssemblyAnnotationWriter import AssemblyAnnotationWriter +from .namespace import llvm +from .Value import Value, MDNode, MDString +from .LLVMContext import LLVMContext +from .ADT.StringRef import StringRef +from .Module import Module +from .Function import Function +from .Support.raw_ostream import raw_ostream +from .Assembly.AssemblyAnnotationWriter import AssemblyAnnotationWriter @MDNode class MDNode: diff --git a/llvmpy/src/Module.py b/llvmpy/src/Module.py index d68be36..46fad5a 100644 --- a/llvmpy/src/Module.py +++ b/llvmpy/src/Module.py @@ -1,18 +1,18 @@ from binding import * -from namespace import llvm +from .namespace import llvm Module = llvm.Class() -from LLVMContext import LLVMContext -from ADT.StringRef import StringRef -from Constant import Constant -from GlobalVariable import GlobalVariable -from Function import Function -from DerivedTypes import FunctionType -from Support.raw_ostream import raw_ostream -from Assembly.AssemblyAnnotationWriter import AssemblyAnnotationWriter -from Type import Type, StructType -from Metadata import NamedMDNode +from .LLVMContext import LLVMContext +from .ADT.StringRef import StringRef +from .Constant import Constant +from .GlobalVariable import GlobalVariable +from .Function import Function +from .DerivedTypes import FunctionType +from .Support.raw_ostream import raw_ostream +from .Assembly.AssemblyAnnotationWriter import AssemblyAnnotationWriter +from .Type import Type, StructType +from .Metadata import NamedMDNode @Module class Module: diff --git a/llvmpy/src/Pass.py b/llvmpy/src/Pass.py index f36856c..ad03d9a 100644 --- a/llvmpy/src/Pass.py +++ b/llvmpy/src/Pass.py @@ -1,14 +1,14 @@ from binding import * -from namespace import llvm +from .namespace import llvm Pass = llvm.Class() ModulePass = llvm.Class(Pass) FunctionPass = llvm.Class(Pass) ImmutablePass = llvm.Class(ModulePass) -from ADT.StringRef import StringRef -from Module import Module -from Value import Function +from .ADT.StringRef import StringRef +from .Module import Module +from .Value import Function @Pass class Pass: diff --git a/llvmpy/src/PassManager.py b/llvmpy/src/PassManager.py index 91a98f4..4d59e9f 100644 --- a/llvmpy/src/PassManager.py +++ b/llvmpy/src/PassManager.py @@ -1,8 +1,8 @@ from binding import * -from namespace import llvm -from Pass import Pass -from Module import Module -from Value import Function +from .namespace import llvm +from .Pass import Pass +from .Module import Module +from .Value import Function PassManagerBase = llvm.Class() PassManager = llvm.Class(PassManagerBase) diff --git a/llvmpy/src/PassRegistry.py b/llvmpy/src/PassRegistry.py index 495de51..ede026c 100644 --- a/llvmpy/src/PassRegistry.py +++ b/llvmpy/src/PassRegistry.py @@ -1,5 +1,5 @@ from binding import * -from namespace import llvm +from .namespace import llvm from src.ADT.StringRef import StringRef PassRegistry = llvm.Class() diff --git a/llvmpy/src/PassSupport.py b/llvmpy/src/PassSupport.py index 8dba872..d239a6d 100644 --- a/llvmpy/src/PassSupport.py +++ b/llvmpy/src/PassSupport.py @@ -1,5 +1,5 @@ from binding import * -from namespace import llvm +from .namespace import llvm PassInfo = llvm.Class() diff --git a/llvmpy/src/Support/FormattedStream.py b/llvmpy/src/Support/FormattedStream.py index 26f906d..3e9159b 100644 --- a/llvmpy/src/Support/FormattedStream.py +++ b/llvmpy/src/Support/FormattedStream.py @@ -1,6 +1,6 @@ from binding import * from ..namespace import llvm -from raw_ostream import raw_ostream +from .raw_ostream import raw_ostream @llvm.Class(raw_ostream) class formatted_raw_ostream: diff --git a/llvmpy/src/Type.py b/llvmpy/src/Type.py index 8e62137..ea1f0e5 100644 --- a/llvmpy/src/Type.py +++ b/llvmpy/src/Type.py @@ -1,8 +1,8 @@ from binding import * -from namespace import llvm -from LLVMContext import LLVMContext -from Support.raw_ostream import raw_ostream -from ADT.StringRef import StringRef +from .namespace import llvm +from .LLVMContext import LLVMContext +from .Support.raw_ostream import raw_ostream +from .ADT.StringRef import StringRef Type = llvm.Class() IntegerType = llvm.Class(Type) diff --git a/llvmpy/src/User.py b/llvmpy/src/User.py index 9a077c1..1d3cc1f 100644 --- a/llvmpy/src/User.py +++ b/llvmpy/src/User.py @@ -1,6 +1,6 @@ from binding import * -from namespace import llvm -from Value import Value, User +from .namespace import llvm +from .Value import Value, User @User class User: diff --git a/llvmpy/src/Value.py b/llvmpy/src/Value.py index b10807a..669ed80 100644 --- a/llvmpy/src/Value.py +++ b/llvmpy/src/Value.py @@ -1,5 +1,5 @@ from binding import * -from namespace import llvm +from .namespace import llvm # forward declarations Value = llvm.Class() @@ -21,11 +21,11 @@ ConstantDataSequential = llvm.Class(Constant) ConstantDataArray = llvm.Class(ConstantDataSequential) ConstantExpr = llvm.Class(Constant) -from Support.raw_ostream import raw_ostream -from Assembly.AssemblyAnnotationWriter import AssemblyAnnotationWriter -from Type import Type -from LLVMContext import LLVMContext -from ADT.StringRef import StringRef +from .Support.raw_ostream import raw_ostream +from .Assembly.AssemblyAnnotationWriter import AssemblyAnnotationWriter +from .Type import Type +from .LLVMContext import LLVMContext +from .ADT.StringRef import StringRef @Value diff --git a/llvmpy/src/__init__.py b/llvmpy/src/__init__.py index ed34608..36bc12f 100644 --- a/llvmpy/src/__init__.py +++ b/llvmpy/src/__init__.py @@ -3,11 +3,12 @@ import os.path def _init(root=__name__, file=__file__): base = os.path.dirname(file) for fname in os.listdir(base): - print fname is_python_script = fname.endswith('.py') or fname.endswith('.pyc') is_init_script = fname.startswith('__init__') is_directory = os.path.isdir(os.path.join(base, fname)) - if (is_directory or is_python_script) and not is_init_script : + is_python_module = is_directory and not fname.startswith('__') + if (is_python_module or is_python_script) and not is_init_script: + print(fname) modname = os.path.basename(fname).rsplit('.', 1)[0] #importlib.import_module('.' + modname, __name__) __import__('.'.join([root, modname])) diff --git a/llvmpy/test_binding.py b/llvmpy/test_binding.py index 8eb56f6..c7ceb49 100644 --- a/llvmpy/test_binding.py +++ b/llvmpy/test_binding.py @@ -1,9 +1,10 @@ -from StringIO import StringIO +from io import BytesIO, StringIO from llvmpy.api import llvm from llvmpy import extra from llvmpy import _capsule import llvmpy.capsule +import collections llvmpy.capsule.set_debug(True) @@ -14,19 +15,19 @@ def test_basic_jit_use(): context = llvm.getGlobalContext() m = llvm.Module.new("modname", context) - print m.getModuleIdentifier() + print(m.getModuleIdentifier()) m.setModuleIdentifier('modname2') - print m.getModuleIdentifier() - print 'endianness', m.getEndianness() + print(m.getModuleIdentifier()) + print('endianness', m.getEndianness()) assert m.getEndianness() == llvm.Module.Endianness.AnyEndianness - print 'pointer-size', m.getPointerSize() + print('pointer-size', m.getPointerSize()) assert m.getPointerSize() == llvm.Module.PointerSize.AnyPointerSize m.dump() os = extra.make_raw_ostream_for_printing() m.print_(os, None) - print os.str() + print(os.str()) int1ty = llvm.Type.getInt1Ty(context) @@ -40,11 +41,11 @@ def test_basic_jit_use(): types = [llvm.Type.getIntNTy(context, 8), llvm.Type.getIntNTy(context, 32)] fnty = llvm.FunctionType.get(llvm.Type.getIntNTy(context, 8), types, False) - print fnty + print(fnty) const = m.getOrInsertFunction("foo", fnty) fn = const._downcast(llvm.Function) - print fn + print(fn) assert fn.hasName() assert 'foo' == fn.getName() fn.setName('bar') @@ -63,7 +64,7 @@ def test_basic_jit_use(): assert len(fn_uselist) == 0 builder = llvm.IRBuilder.new(context) - print builder + print(builder) bb = llvm.BasicBlock.Create(context, "entry", fn, None) assert bb.empty() @@ -72,19 +73,19 @@ def test_basic_jit_use(): assert bb.getTerminator() is None arg0, arg1 = fn.getArgumentList() - print arg0, arg1 + print(arg0, arg1) extended = builder.CreateZExt(arg0, arg1.getType()) result = builder.CreateAdd(extended, arg1) ret = builder.CreateTrunc(result, fn.getReturnType()) builder.CreateRet(ret) - print arg0.list_use() + print(arg0.list_use()) - print fn + print(fn) errio = StringIO() - print m + print(m) # verifier action = llvm.VerifierFailureAction.ReturnStatusAction @@ -92,7 +93,7 @@ def test_basic_jit_use(): corrupted = llvm.verifyFunction(fn, action) assert not corrupted corrupted = llvm.verifyModule(m, action, errio) - print corrupted + print(corrupted) assert not corrupted, errio.getvalue() # build pass manager @@ -114,13 +115,13 @@ def test_basic_jit_use(): pm.run(m) - print m + print(m) # build engine ee = llvm.ExecutionEngine.createJIT(m, errio) - print ee, errio.getvalue() - print ee.getDataLayout().getStringRepresentation() + print(ee, errio.getvalue()) + print(ee.getDataLayout().getStringRepresentation()) datalayout_str = 'e-p:64:64:64-S128-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f16:16:16-f32:32:32-f64:64:64-f128:128:128-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64' @@ -149,7 +150,7 @@ def test_basic_jit_use(): assert 44 == gvR.toUnsignedInt() # write bitcode - bc_buffer = StringIO() + bc_buffer = BytesIO() llvm.WriteBitcodeToFile(m, bc_buffer) bc = bc_buffer.getvalue() bc_buffer.close() @@ -187,7 +188,7 @@ def test_engine_builder(): builder.SetInsertPoint(bb) builder.CreateRet(fn.getArgumentList()[0]) - print fn + print(fn) eb = llvm.EngineBuilder.new(m) eb2 = eb.setEngineKind(llvm.EngineKind.Kind.JIT) @@ -196,13 +197,13 @@ def test_engine_builder(): tm = eb.selectTarget() - print 'target triple:', tm.getTargetTriple() - print 'target cpu:', tm.getTargetCPU() - print 'target feature string:', tm.getTargetFeatureString() + print('target triple:', tm.getTargetTriple()) + print('target cpu:', tm.getTargetCPU()) + print('target feature string:', tm.getTargetFeatureString()) target = tm.getTarget() - print 'target name:', target.getName() - print 'target short description:', target.getShortDescription() + print('target name:', target.getName()) + print('target short description:', target.getShortDescription()) assert target.hasJIT() assert target.hasTargetMachine() @@ -218,7 +219,7 @@ def test_engine_builder(): triple_32variant = triple.get32BitArchVariant() assert triple_32variant.isArch32Bit() - print tm.getDataLayout() + print(tm.getDataLayout()) pm = llvm.PassManager.new() pm.add(llvm.DataLayout.new(str(tm.getDataLayout()))) @@ -240,7 +241,7 @@ def test_engine_builder(): formatted.flush() raw.flush() asm = raw.str() - print asm + print(asm) assert 'foo' in asm @@ -301,7 +302,7 @@ def test_globalvariable(): gvar2 = m.getNamedGlobal('apple') assert gvar2 is gvar - print m.list_globals() + print(m.list_globals()) def test_sequentialtypes(): @@ -345,9 +346,9 @@ def test_passregistry(): passinfo = passreg.getPassInfo("dce") dcepass = passinfo.createPass() - print dcepass.getPassName() + print(dcepass.getPassName()) - print passreg.enumerate() + print(passreg.enumerate()) def test_targetregistry(): llvm.TargetRegistry.printRegisteredTargetsForVersion() @@ -356,15 +357,15 @@ def test_targetregistry(): target = llvm.TargetRegistry.getClosestTargetForJIT(errmsg) errmsg.close() - print target.getName() - print target.getShortDescription() + print(target.getName()) + print(target.getShortDescription()) assert target.hasJIT() assert target.hasTargetMachine() next = target.getNext() if next: - print next.getName() - print next.getShortDescription() + print(next.getName()) + print(next.getShortDescription()) triple = llvm.sys.getDefaultTargetTriple() cpu = llvm.sys.getHostCPUName() @@ -375,9 +376,9 @@ def test_targetregistry(): tm = target.createTargetMachine(triple, cpu, "", targetoptions) def main(): - for name, value in globals().items(): - if name.startswith('test_') and callable(value): - print name.center(80, '-') + for name, value in list(globals().items()): + if name.startswith('test_') and isinstance(value, collections.Callable): + print(name.center(80, '-')) value()