Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make bindings.py 2-3 compatible #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 42 additions & 41 deletions fcd/python/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# (feed me a preprocessed llvm-c/Core.h in stdin)
#

from __future__ import print_function
import re
import os
import string
Expand All @@ -28,7 +29,7 @@
class CParameter(object):
@staticmethod
def parse(paramString):
wordCharset = string.letters + string.digits
wordCharset = string.ascii_letters + string.digits
result = []
for param in paramString.split(","):
param = param.strip()
Expand Down Expand Up @@ -128,7 +129,7 @@ def __init__(self, cFunction):
if cFunction.name == "LLVMConstIntGetZExtValue":
sys.stderr.write("got zextvalue\n")
if cFunction.returnType in callbacks:
raise ValueError, "callback type %s" % cFunction.returnType
raise ValueError("callback type %s" % cFunction.returnType)

self.function = cFunction
self.name = self.function.name[4:]
Expand All @@ -146,7 +147,7 @@ def convertParamList(list):
prevPointerType = None
for param in list:
if param in callbacks:
raise ValueError, "of callback type %s" % cFunction.returnType
raise ValueError("of callback type %s" % cFunction.returnType)

if prevPointerType != None:
lowerName = param.name.lower()
Expand All @@ -156,14 +157,14 @@ def convertParamList(list):
prevPointerType = None
break
else:
raise ValueError, "of uncertain bounds for double-pointer type '%s'" % prevPointerType
raise ValueError("of uncertain bounds for double-pointer type '%s'" % prevPointerType)
elif param.type == "const char *" or param.type == "const char*":
params.append(PythonParameter("string"))
else:
refType = param.getRefType()
if param.isDoublePointer():
if refType == None:
raise ValueError, "of non-ref double-pointer type '%s'" % param
raise ValueError("of non-ref double-pointer type '%s'" % param)
prevPointerType = refType
elif refType != None:
params.append(PythonParameter("object", refType))
Expand All @@ -178,10 +179,10 @@ def convertParamList(list):
elif param.type == "LLVMBool":
params.append(PythonParameter("bool"))
else:
raise ValueError, "of unhandled type '%s'" % param
raise ValueError("of unhandled type '%s'" % param)

if prevPointerType != None:
raise ValueError, "of uncertain bounds for double-pointer type %s" % prevPointerType
raise ValueError("of uncertain bounds for double-pointer type %s" % prevPointerType)

return params

Expand All @@ -190,10 +191,10 @@ def inferSelf(self):
return self.selfType

if len(self.params) == 0:
raise ValueError, "cannot infer self type from empty parameter list"
raise ValueError("cannot infer self type from empty parameter list")

if self.params[0].type != "object":
raise ValueError, "cannot infer self type as non-reference type %s" % self.params[0]
raise ValueError("cannot infer self type as non-reference type %s" % self.params[0])

self.selfType = self.params[0].generic
self.params = self.params[1:]
Expand Down Expand Up @@ -247,7 +248,7 @@ def addMethod(self, method):
if classType not in classes:
classes[classType] = PythonClass(classType)
classes[classType].addMethod(method)
except ValueError, message:
except ValueError as message:
sys.stderr.write("cannot use %s because %s\n" % (p, message))

#
Expand Down Expand Up @@ -284,13 +285,13 @@ def addMethod(self, method):
# code generation starts here
#

print "#pragma clang diagnostic push"
print "#pragma clang diagnostic ignored \"-Wshorten-64-to-32\""
print
print "#include \"bindings.h\""
print "#include <llvm-c/Core.h>"
print "#include <memory>"
print
print("#pragma clang diagnostic push")
print("#pragma clang diagnostic ignored \"-Wshorten-64-to-32\"")
print()
print("#include \"bindings.h\"")
print("#include <llvm-c/Core.h>")
print("#include <memory>")
print()

methodNoArgsPrototypeTemplate = """static PyObject* %s(Py_LLVM_Wrapped<%s>* self)"""
methodArgsPrototypeTemplate = """static PyObject* %s(Py_LLVM_Wrapped<%s>* self, PyObject* args)"""
Expand Down Expand Up @@ -328,11 +329,11 @@ def addMethod(self, method):
if len(method.params) == 0:
args = "NOARGS"
prototype = methodNoArgsPrototypeTemplate % (methodCName, llvmName)
print prototype + ";"
print(prototype + ";")
else:
args = "VARARGS"
prototype = methodArgsPrototypeTemplate % (methodCName, llvmName)
print prototype + ";"
print(prototype + ";")

tableEntries += methodTableEntryTemplate % (method.name, methodCName, args, method.function.name)

Expand Down Expand Up @@ -427,56 +428,56 @@ def addMethod(self, method):
else:
methodImplementations += "#error Implement return type %s" % method.returnType.type
methodImplementations += "}\n\n"
print
print()

sys.stderr.write("\n")

# method table
print methodTableTemplate % (typeName, tableEntries)
print typeObjectTemplate % (typeName, classKey, "sizeof(Py_LLVM_Wrapped<%s>)" % llvmName, llvmName, typeName)
print(methodTableTemplate % (typeName, tableEntries))
print(typeObjectTemplate % (typeName, classKey, "sizeof(Py_LLVM_Wrapped<%s>)" % llvmName, llvmName, typeName))

# enum types here
print methodTableTemplate % ("no", "")
print(methodTableTemplate % ("no", ""))
for enumKey in enums:
typeName = "%s%s" % (prefix, enumKey)
print typeObjectTemplate % (typeName, enumKey[4:], "sizeof(PyObject)", "enum " + enumKey, "no")
print(typeObjectTemplate % (typeName, enumKey[4:], "sizeof(PyObject)", "enum " + enumKey, "no"))

print methodImplementations
print(methodImplementations)

print "PyMODINIT_FUNC initLlvmModule(PyObject** module)"
print "{"
print("PyMODINIT_FUNC initLlvmModule(PyObject** module)")
print("{")
for classKey in classes:
typeObjName = "%sLLVM%s_Type" % (prefix, classKey)
print "\tif (PyType_Ready(&%s) < 0) return;" % typeObjName
print("\tif (PyType_Ready(&%s) < 0) return;" % typeObjName)

for enumKey in enums:
typeObjName = "%s%s_Type" % (prefix, enumKey)
print "\tif (PyType_Ready(&%s) < 0) return;" % typeObjName
print("\tif (PyType_Ready(&%s) < 0) return;" % typeObjName)

print
print()
for enumKey in enums:
typeObjName = "%s%s_Type" % (prefix, enumKey)
enum = enums[enumKey]
for key in enum.cases:
print "\tPyDict_SetItemString(%s.tp_dict, \"%s\", (TAKEREF PyInt_FromLong(%i)).get());" % (typeObjName, key[4:], enum.cases[key])
print
print("\tPyDict_SetItemString(%s.tp_dict, \"%s\", (TAKEREF PyInt_FromLong(%i)).get());" % (typeObjName, key[4:], enum.cases[key]))
print()

print
print "\t*module = Py_InitModule(\"llvm\", nullptr);"
print()
print("\t*module = Py_InitModule(\"llvm\", nullptr);")
for classKey in classes:
typeObjName = "%sLLVM%s_Type" % (prefix, classKey)
print "\tPy_INCREF(&%s);" % typeObjName
print("\tPy_INCREF(&%s);" % typeObjName)
for enumKey in enums:
typeObjName = "%s%s_Type" % (prefix, enumKey)
print "\tPy_INCREF(&%s);" % typeObjName
print("\tPy_INCREF(&%s);" % typeObjName)

for classKey in classes:
typeObjName = "%sLLVM%s_Type" % (prefix, classKey)
print "\tPyModule_AddObject(*module, \"%s\", (PyObject*)&%s);" % (classKey, typeObjName)
print("\tPyModule_AddObject(*module, \"%s\", (PyObject*)&%s);" % (classKey, typeObjName))
for enumKey in enums:
typeObjName = "%s%s_Type" % (prefix, enumKey)
print "\tPyModule_AddObject(*module, \"%s\", (PyObject*)&%s);" % (enumKey[4:], typeObjName)
print("\tPyModule_AddObject(*module, \"%s\", (PyObject*)&%s);" % (enumKey[4:], typeObjName))

print "}"
print
print "#pragma clang diagnostic pop"
print("}")
print()
print("#pragma clang diagnostic pop")