|
@@ -1,8 +1,10 @@
|
|
|
#!/usr/bin/env python
|
|
|
+from __future__ import print_function
|
|
|
+
|
|
|
|
|
|
import os
|
|
|
import sys
|
|
|
-import test # So we can find the path to local files in the printer package
|
|
|
+from . import test # So we can find the path to local files in the printer package
|
|
|
import time
|
|
|
|
|
|
import ctypesgencore.libraryloader # So we can get the path to it
|
|
@@ -29,13 +31,13 @@ class WrapperPrinter:
|
|
|
self.options.strip_build_path += os.path.sep
|
|
|
|
|
|
self.print_header()
|
|
|
- print >>self.file
|
|
|
+ print(file=self.file)
|
|
|
|
|
|
self.print_preamble()
|
|
|
- print >>self.file
|
|
|
+ print(file=self.file)
|
|
|
|
|
|
self.print_loader()
|
|
|
- print >>self.file
|
|
|
+ print(file=self.file)
|
|
|
|
|
|
self.print_group(self.options.libraries, "libraries", self.print_library)
|
|
|
self.print_group(self.options.modules, "modules", self.print_module)
|
|
@@ -54,36 +56,36 @@ class WrapperPrinter:
|
|
|
for kind, desc in data.output_order:
|
|
|
if desc.included:
|
|
|
method_table[kind](desc)
|
|
|
- print >>self.file
|
|
|
+ print(file=self.file)
|
|
|
|
|
|
self.print_group(self.options.inserted_files, "inserted files",
|
|
|
self.insert_file)
|
|
|
|
|
|
def print_group(self, list, name, function):
|
|
|
if list:
|
|
|
- print >>self.file, "# Begin %s" % name
|
|
|
- print >>self.file
|
|
|
+ print("# Begin %s" % name, file=self.file)
|
|
|
+ print(file=self.file)
|
|
|
for obj in list:
|
|
|
function(obj)
|
|
|
- print >>self.file
|
|
|
- print >>self.file, "# %d %s" % (len(list), name)
|
|
|
- print >>self.file, "# End %s" % name
|
|
|
+ print(file=self.file)
|
|
|
+ print("# %d %s" % (len(list), name), file=self.file)
|
|
|
+ print("# End %s" % name, file=self.file)
|
|
|
else:
|
|
|
- print >>self.file, "# No %s" % name
|
|
|
- print >>self.file
|
|
|
+ print("# No %s" % name, file=self.file)
|
|
|
+ print(file=self.file)
|
|
|
|
|
|
def srcinfo(self, src):
|
|
|
if src is None:
|
|
|
- print >>self.file
|
|
|
+ print(file=self.file)
|
|
|
else:
|
|
|
filename, lineno = src
|
|
|
if filename in ("<built-in>", "<command line>"):
|
|
|
- print >>self.file, "# %s" % filename
|
|
|
+ print("# %s" % filename, file=self.file)
|
|
|
else:
|
|
|
if self.options.strip_build_path and \
|
|
|
filename.startswith(self.options.strip_build_path):
|
|
|
filename = filename[len(self.options.strip_build_path):]
|
|
|
- print >>self.file, "# %s: %s" % (filename, lineno)
|
|
|
+ print("# %s: %s" % (filename, lineno), file=self.file)
|
|
|
|
|
|
def template_subs(self):
|
|
|
template_subs = {
|
|
@@ -92,7 +94,7 @@ class WrapperPrinter:
|
|
|
'name': os.path.basename(self.options.headers[0])
|
|
|
}
|
|
|
|
|
|
- for opt, value in self.options.__dict__.iteritems():
|
|
|
+ for opt, value in self.options.__dict__.items():
|
|
|
if isinstance(value, str):
|
|
|
template_subs[opt] = value
|
|
|
elif isinstance(value, (list, tuple)):
|
|
@@ -125,72 +127,72 @@ class WrapperPrinter:
|
|
|
def print_preamble(self):
|
|
|
path = path_to_local_file("preamble.py")
|
|
|
|
|
|
- print >>self.file, "# Begin preamble"
|
|
|
- print >>self.file
|
|
|
+ print("# Begin preamble", file=self.file)
|
|
|
+ print(file=self.file)
|
|
|
preamble_file = file(path, "r")
|
|
|
self.file.write(preamble_file.read())
|
|
|
preamble_file.close()
|
|
|
- print >>self.file
|
|
|
- print >>self.file, "# End preamble"
|
|
|
+ print(file=self.file)
|
|
|
+ print("# End preamble", file=self.file)
|
|
|
|
|
|
def print_loader(self):
|
|
|
- print >>self.file, "_libs = {}"
|
|
|
- print >>self.file, "_libdirs = %s" % self.options.compile_libdirs
|
|
|
- print >>self.file
|
|
|
- print >>self.file, "# Begin loader"
|
|
|
- print >>self.file
|
|
|
+ print("_libs = {}", file=self.file)
|
|
|
+ print("_libdirs = %s" % self.options.compile_libdirs, file=self.file)
|
|
|
+ print(file=self.file)
|
|
|
+ print("# Begin loader", file=self.file)
|
|
|
+ print(file=self.file)
|
|
|
path = path_to_local_file("libraryloader.py",
|
|
|
ctypesgencore.libraryloader)
|
|
|
loader_file = file(path, "r")
|
|
|
self.file.write(loader_file.read())
|
|
|
loader_file.close()
|
|
|
- print >>self.file
|
|
|
- print >>self.file, "# End loader"
|
|
|
- print >>self.file
|
|
|
- print >>self.file, "add_library_search_dirs([%s])" % \
|
|
|
- ", ".join([repr(d) for d in self.options.runtime_libdirs])
|
|
|
+ print(file=self.file)
|
|
|
+ print("# End loader", file=self.file)
|
|
|
+ print(file=self.file)
|
|
|
+ print("add_library_search_dirs([%s])" % \
|
|
|
+ ", ".join([repr(d) for d in self.options.runtime_libdirs]), file=self.file)
|
|
|
|
|
|
def print_library(self, library):
|
|
|
- print >>self.file, '_libs["%s"] = load_library("%s")' % (library, library)
|
|
|
+ print('_libs["%s"] = load_library("%s")' % (library, library), file=self.file)
|
|
|
|
|
|
def print_module(self, module):
|
|
|
- print >>self.file, 'from %s import *' % name
|
|
|
+ print('from %s import *' % name, file=self.file)
|
|
|
|
|
|
def print_constant(self, constant):
|
|
|
- print >>self.file, '%s = %s' % \
|
|
|
- (constant.name, constant.value.py_string(False)),
|
|
|
+ print('%s = %s' % \
|
|
|
+ (constant.name, constant.value.py_string(False)), end=' ', file=self.file)
|
|
|
self.srcinfo(constant.src)
|
|
|
|
|
|
def print_typedef(self, typedef):
|
|
|
- print >>self.file, '%s = %s' % \
|
|
|
- (typedef.name, typedef.ctype.py_string()),
|
|
|
+ print('%s = %s' % \
|
|
|
+ (typedef.name, typedef.ctype.py_string()), end=' ', file=self.file)
|
|
|
self.srcinfo(typedef.src)
|
|
|
|
|
|
def print_struct(self, struct):
|
|
|
self.srcinfo(struct.src)
|
|
|
base = {'union': 'Union', 'struct': 'Structure'}[struct.variety]
|
|
|
- print >>self.file, 'class %s_%s(%s):' % \
|
|
|
- (struct.variety, struct.tag, base)
|
|
|
- print >>self.file, ' pass'
|
|
|
+ print('class %s_%s(%s):' % \
|
|
|
+ (struct.variety, struct.tag, base), file=self.file)
|
|
|
+ print(' pass', file=self.file)
|
|
|
|
|
|
def print_struct_members(self, struct):
|
|
|
if struct.opaque:
|
|
|
return
|
|
|
- print >>self.file, '%s_%s.__slots__ = [' % (struct.variety, struct.tag)
|
|
|
+ print('%s_%s.__slots__ = [' % (struct.variety, struct.tag), file=self.file)
|
|
|
for name, ctype in struct.members:
|
|
|
- print >>self.file, " '%s'," % name
|
|
|
- print >>self.file, ']'
|
|
|
- print >>self.file, '%s_%s._fields_ = [' % (struct.variety, struct.tag)
|
|
|
+ print(" '%s'," % name, file=self.file)
|
|
|
+ print(']', file=self.file)
|
|
|
+ print('%s_%s._fields_ = [' % (struct.variety, struct.tag), file=self.file)
|
|
|
for name, ctype in struct.members:
|
|
|
if isinstance(ctype, CtypesBitfield):
|
|
|
- print >>self.file, " ('%s', %s, %s)," % \
|
|
|
- (name, ctype.py_string(), ctype.bitfield.py_string(False))
|
|
|
+ print(" ('%s', %s, %s)," % \
|
|
|
+ (name, ctype.py_string(), ctype.bitfield.py_string(False)), file=self.file)
|
|
|
else:
|
|
|
- print >>self.file, " ('%s', %s)," % (name, ctype.py_string())
|
|
|
- print >>self.file, ']'
|
|
|
+ print(" ('%s', %s)," % (name, ctype.py_string()), file=self.file)
|
|
|
+ print(']', file=self.file)
|
|
|
|
|
|
def print_enum(self, enum):
|
|
|
- print >>self.file, 'enum_%s = c_int' % enum.tag,
|
|
|
+ print('enum_%s = c_int' % enum.tag, end=' ', file=self.file)
|
|
|
self.srcinfo(enum.src)
|
|
|
# Values of enumerator are output as constants.
|
|
|
|
|
@@ -203,69 +205,69 @@ class WrapperPrinter:
|
|
|
def print_fixed_function(self, function):
|
|
|
self.srcinfo(function.src)
|
|
|
if function.source_library:
|
|
|
- print >>self.file, "if hasattr(_libs[%r], %r):" % \
|
|
|
- (function.source_library, function.c_name())
|
|
|
- print >>self.file, " %s = _libs[%r].%s" % \
|
|
|
- (function.py_name(), function.source_library, function.c_name())
|
|
|
- print >>self.file, " %s.restype = %s" % \
|
|
|
- (function.py_name(), function.restype.py_string())
|
|
|
- print >>self.file, " %s.argtypes = [%s]" % (
|
|
|
+ print("if hasattr(_libs[%r], %r):" % \
|
|
|
+ (function.source_library, function.c_name()), file=self.file)
|
|
|
+ print(" %s = _libs[%r].%s" % \
|
|
|
+ (function.py_name(), function.source_library, function.c_name()), file=self.file)
|
|
|
+ print(" %s.restype = %s" % \
|
|
|
+ (function.py_name(), function.restype.py_string()), file=self.file)
|
|
|
+ print(" %s.argtypes = [%s]" % (
|
|
|
function.py_name(),
|
|
|
- ', '.join([a.py_string() for a in function.argtypes]))
|
|
|
+ ', '.join([a.py_string() for a in function.argtypes])), file=self.file)
|
|
|
else:
|
|
|
- print >>self.file, "for _lib in _libs.values():"
|
|
|
- print >>self.file, " if hasattr(_lib, %r):" % function.c_name()
|
|
|
- print >>self.file, " %s = _lib.%s" % (function.py_name(), function.c_name())
|
|
|
- print >>self.file, " %s.restype = %s" % (
|
|
|
- function.py_name(), function.restype.py_string())
|
|
|
- print >>self.file, " %s.argtypes = [%s]" % (
|
|
|
- function.py_name(), ', '.join([a.py_string() for a in function.argtypes]))
|
|
|
- print >>self.file, " break"
|
|
|
+ print("for _lib in _libs.values():", file=self.file)
|
|
|
+ print(" if hasattr(_lib, %r):" % function.c_name(), file=self.file)
|
|
|
+ print(" %s = _lib.%s" % (function.py_name(), function.c_name()), file=self.file)
|
|
|
+ print(" %s.restype = %s" % (
|
|
|
+ function.py_name(), function.restype.py_string()), file=self.file)
|
|
|
+ print(" %s.argtypes = [%s]" % (
|
|
|
+ function.py_name(), ', '.join([a.py_string() for a in function.argtypes])), file=self.file)
|
|
|
+ print(" break", file=self.file)
|
|
|
|
|
|
def print_variadic_function(self, function):
|
|
|
self.srcinfo(function.src)
|
|
|
if function.source_library:
|
|
|
- print >>self.file, "if hasattr(_libs[%r], %r):" % \
|
|
|
- (function.source_library, function.c_name())
|
|
|
- print >>self.file, " _func = _libs[%r].%s" % \
|
|
|
- (function.source_library, function.c_name())
|
|
|
- print >>self.file, " _restype = %s" % function.restype.py_string()
|
|
|
- print >>self.file, " _argtypes = [%s]" % \
|
|
|
- ', '.join([a.py_string() for a in function.argtypes])
|
|
|
- print >>self.file, " %s = _variadic_function(_func,_restype,_argtypes)" % \
|
|
|
- function.py_name()
|
|
|
+ print("if hasattr(_libs[%r], %r):" % \
|
|
|
+ (function.source_library, function.c_name()), file=self.file)
|
|
|
+ print(" _func = _libs[%r].%s" % \
|
|
|
+ (function.source_library, function.c_name()), file=self.file)
|
|
|
+ print(" _restype = %s" % function.restype.py_string(), file=self.file)
|
|
|
+ print(" _argtypes = [%s]" % \
|
|
|
+ ', '.join([a.py_string() for a in function.argtypes]), file=self.file)
|
|
|
+ print(" %s = _variadic_function(_func,_restype,_argtypes)" % \
|
|
|
+ function.py_name(), file=self.file)
|
|
|
else:
|
|
|
- print >>self.file, "for _lib in _libs.values():"
|
|
|
- print >>self.file, " if hasattr(_lib, %r):" % function.c_name()
|
|
|
- print >>self.file, " _func = _lib.%s" % \
|
|
|
- (function.c_name())
|
|
|
- print >>self.file, " _restype = %s" % function.restype.py_string()
|
|
|
- print >>self.file, " _argtypes = [%s]" % \
|
|
|
- ', '.join([a.py_string() for a in function.argtypes])
|
|
|
- print >>self.file, " %s = _variadic_function(_func,_restype,_argtypes)" % \
|
|
|
- function.py_name()
|
|
|
+ print("for _lib in _libs.values():", file=self.file)
|
|
|
+ print(" if hasattr(_lib, %r):" % function.c_name(), file=self.file)
|
|
|
+ print(" _func = _lib.%s" % \
|
|
|
+ (function.c_name()), file=self.file)
|
|
|
+ print(" _restype = %s" % function.restype.py_string(), file=self.file)
|
|
|
+ print(" _argtypes = [%s]" % \
|
|
|
+ ', '.join([a.py_string() for a in function.argtypes]), file=self.file)
|
|
|
+ print(" %s = _variadic_function(_func,_restype,_argtypes)" % \
|
|
|
+ function.py_name(), file=self.file)
|
|
|
|
|
|
def print_variable(self, variable):
|
|
|
self.srcinfo(variable.src)
|
|
|
if variable.source_library:
|
|
|
- print >>self.file, 'try:'
|
|
|
- print >>self.file, ' %s = (%s).in_dll(_libs[%r], %r)' % \
|
|
|
+ print('try:', file=self.file)
|
|
|
+ print(' %s = (%s).in_dll(_libs[%r], %r)' % \
|
|
|
(variable.py_name(),
|
|
|
variable.ctype.py_string(),
|
|
|
variable.source_library,
|
|
|
- variable.c_name())
|
|
|
- print >>self.file, 'except:'
|
|
|
- print >>self.file, ' pass'
|
|
|
+ variable.c_name()), file=self.file)
|
|
|
+ print('except:', file=self.file)
|
|
|
+ print(' pass', file=self.file)
|
|
|
else:
|
|
|
- print >>self.file, "for _lib in _libs.values():"
|
|
|
- print >>self.file, ' try:'
|
|
|
- print >>self.file, ' %s = (%s).in_dll(_lib, %r)' % \
|
|
|
+ print("for _lib in _libs.values():", file=self.file)
|
|
|
+ print(' try:', file=self.file)
|
|
|
+ print(' %s = (%s).in_dll(_lib, %r)' % \
|
|
|
(variable.py_name(),
|
|
|
variable.ctype.py_string(),
|
|
|
- variable.c_name())
|
|
|
- print >>self.file, " break"
|
|
|
- print >>self.file, ' except:'
|
|
|
- print >>self.file, ' pass'
|
|
|
+ variable.c_name()), file=self.file)
|
|
|
+ print(" break", file=self.file)
|
|
|
+ print(' except:', file=self.file)
|
|
|
+ print(' pass', file=self.file)
|
|
|
|
|
|
def print_macro(self, macro):
|
|
|
if macro.params:
|
|
@@ -278,16 +280,16 @@ class WrapperPrinter:
|
|
|
# We want to contain the failures as much as possible.
|
|
|
# Hence the try statement.
|
|
|
self.srcinfo(macro.src)
|
|
|
- print >>self.file, "try:"
|
|
|
- print >>self.file, " %s = %s" % (macro.name, macro.expr.py_string(True))
|
|
|
- print >>self.file, "except:"
|
|
|
- print >>self.file, " pass"
|
|
|
+ print("try:", file=self.file)
|
|
|
+ print(" %s = %s" % (macro.name, macro.expr.py_string(True)), file=self.file)
|
|
|
+ print("except:", file=self.file)
|
|
|
+ print(" pass", file=self.file)
|
|
|
|
|
|
def print_func_macro(self, macro):
|
|
|
self.srcinfo(macro.src)
|
|
|
- print >>self.file, "def %s(%s):" % \
|
|
|
- (macro.name, ", ".join(macro.params))
|
|
|
- print >>self.file, " return %s" % macro.expr.py_string(True)
|
|
|
+ print("def %s(%s):" % \
|
|
|
+ (macro.name, ", ".join(macro.params)), file=self.file)
|
|
|
+ print(" return %s" % macro.expr.py_string(True), file=self.file)
|
|
|
|
|
|
def insert_file(self, filename):
|
|
|
try:
|
|
@@ -296,10 +298,10 @@ class WrapperPrinter:
|
|
|
error_message("Cannot open file \"%s\". Skipped it." % filename,
|
|
|
cls='missing-file')
|
|
|
|
|
|
- print >>self.file, "# Begin \"%s\"" % filename
|
|
|
- print >>self.file
|
|
|
+ print("# Begin \"%s\"" % filename, file=self.file)
|
|
|
+ print(file=self.file)
|
|
|
self.file.write(inserted_file.read())
|
|
|
- print >>self.file
|
|
|
- print >>self.file, "# End \"%s\"" % filename
|
|
|
+ print(file=self.file)
|
|
|
+ print("# End \"%s\"" % filename, file=self.file)
|
|
|
|
|
|
inserted_file.close()
|