[PATCH 0/7] misc ctypesgen additions and fixes

35 views
Skip to first unread message

Lars Munch

unread,
Jan 2, 2014, 4:26:31 PM1/2/14
to ctyp...@googlegroups.com, Lars Munch
The following patches are minor stuff that I have been using locally for almost
2 years now. Please apply.

Lars Munch (7):
issue 27: automatically typedef enums
issue 31: Support several include/exclude symbol options
issue 26: fix pointer argument parsing
Issue 33: String argument issue
use raw strings for lib
support spaces in include path
On win32 add __intX types explicitly

ctypesgen.py | 6 ++++--
ctypesgencore/options.py | 4 ++--
ctypesgencore/parser/preprocessor.py | 9 +++++++-
ctypesgencore/printer_python/preamble.py | 19 ++++------------
ctypesgencore/printer_python/printer.py | 2 +-
ctypesgencore/processor/operations.py | 37 +++++++++++++++++---------------
ctypesgencore/processor/pipeline.py | 2 +-
7 files changed, 40 insertions(+), 39 deletions(-)

--
1.8.5.2

Lars Munch

unread,
Jan 2, 2014, 4:26:32 PM1/2/14
to ctyp...@googlegroups.com, Lars Munch
Check if struct/enum has already been type defined in the C code and
automatically typedef enums in same fashion as structs and unions.
---
ctypesgencore/processor/operations.py | 29 ++++++++++++++++-------------
ctypesgencore/processor/pipeline.py | 2 +-
2 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/ctypesgencore/processor/operations.py b/ctypesgencore/processor/operations.py
index 0e0bae4..b1839eb 100644
--- a/ctypesgencore/processor/operations.py
+++ b/ctypesgencore/processor/operations.py
@@ -13,20 +13,23 @@ import ctypesgencore.libraryloader

# Processor functions

-def automatically_typedef_structs(data,options):
- """automatically_typedef_structs() aliases "struct_<tag>" to "<tag>" for
- every struct and union."""
- # XXX Check if it has already been aliased in the C code.
-
- for struct in data.structs:
- if not struct.ctype.anonymous: # Don't alias anonymous structs
- typedef=TypedefDescription(struct.tag,
- struct.ctype,
- src=struct.src)
- typedef.add_requirements(set([struct]))
-
+def automatically_typedef_structs_and_enums(data,options):
+ """automatically_typedef_structs_and_enums() aliases "struct_<tag>" to
+ "<tag>" for every struct and union and "enum_<tag>" to <tag> for every
+ enum."""
+
+ for obj in data.structs + data.enums:
+ # Check if it has already been aliased in the C code.
+ if obj.tag in [typedef.name for typedef in data.typedefs]:
+ continue
+ # Don't alias anonymous structs or enums
+ if not obj.ctype.anonymous:
+ typedef=TypedefDescription(obj.tag,
+ obj.ctype,
+ src=obj.src)
+ typedef.add_requirements(set([obj]))
data.typedefs.append(typedef)
- data.all.insert(data.all.index(struct)+1,typedef)
+ data.all.insert(data.all.index(obj)+1,typedef)
data.output_order.append(("typedef", typedef))

def remove_NULL(data, options):
diff --git a/ctypesgencore/processor/pipeline.py b/ctypesgencore/processor/pipeline.py
index 5322efa..0417623 100644
--- a/ctypesgencore/processor/pipeline.py
+++ b/ctypesgencore/processor/pipeline.py
@@ -43,7 +43,7 @@ def process(data,options):

find_dependencies(data,options)

- automatically_typedef_structs(data,options)
+ automatically_typedef_structs_and_enums(data,options)
remove_NULL(data, options)
remove_descriptions_in_system_headers(data,options)
filter_by_regexes_exclude(data,options)
--
1.8.5.2

Lars Munch

unread,
Jan 2, 2014, 4:26:33 PM1/2/14
to ctyp...@googlegroups.com, Lars Munch
Support several include/exclude symbols option using the -i or -x options.
---
ctypesgen.py | 6 ++++--
ctypesgencore/options.py | 4 ++--
ctypesgencore/processor/operations.py | 8 ++++----
3 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/ctypesgen.py b/ctypesgen.py
index 7fb8a49..ae1f579 100755
--- a/ctypesgen.py
+++ b/ctypesgen.py
@@ -87,9 +87,11 @@ if __name__=="__main__":
op.add_option('', '--no-macros', action='store_false', dest='include_macros',
default=True, help="Don't output macros.")
op.add_option('-i', '--include-symbols', dest='include_symbols',
- default=None, help='regular expression for symbols to always include')
+ action='append', default=[],
+ help='regular expression for symbols to always include')
op.add_option('-x', '--exclude-symbols', dest='exclude_symbols',
- default=None, help='regular expression for symbols to exclude')
+ action='append', default=[],
+ help='regular expression for symbols to exclude')
op.add_option('', '--no-stddef-types', action='store_true',
dest='no_stddef_types', default=False,
help='Do not support extra C types from stddef.h')
diff --git a/ctypesgencore/options.py b/ctypesgencore/options.py
index ad19417..242b81d 100644
--- a/ctypesgencore/options.py
+++ b/ctypesgencore/options.py
@@ -20,8 +20,8 @@ default_values={
"save_preprocessed_headers": None,
"all_headers": False,
"builtin_symbols": False,
- "include_symbols": None,
- "exclude_symbols": None,
+ "include_symbols": [],
+ "exclude_symbols": [],
"show_all_errors": False,
"show_long_errors": False,
"show_macro_warnings": True,
diff --git a/ctypesgencore/processor/operations.py b/ctypesgencore/processor/operations.py
index b1839eb..a92db37 100644
--- a/ctypesgencore/processor/operations.py
+++ b/ctypesgencore/processor/operations.py
@@ -68,8 +68,8 @@ def remove_macros(data,opts):
def filter_by_regexes_exclude(data,opts):
"""filter_by_regexes_exclude() uses regular expressions specified by options
dictionary to filter symbols."""
- if opts.exclude_symbols:
- expr=re.compile(opts.exclude_symbols)
+ for symbols in opts.exclude_symbols:
+ expr=re.compile(symbols)
for object in data.all:
if expr.match(object.py_name()):
object.include_rule="never"
@@ -77,8 +77,8 @@ def filter_by_regexes_exclude(data,opts):
def filter_by_regexes_include(data,opts):
"""filter_by_regexes_include() uses regular expressions specified by options
dictionary to re-include symbols previously rejected by other operations."""
- if opts.include_symbols:
- expr=re.compile(opts.include_symbols)
+ for symbols in opts.include_symbols:
+ expr=re.compile(symbols)
for object in data.all:
if object.include_rule!="never":
if expr.match(object.py_name()):
--
1.8.5.2

Lars Munch

unread,
Jan 2, 2014, 4:26:34 PM1/2/14
to ctyp...@googlegroups.com, Lars Munch
This patch reverts a workaround introduced in svn commit 50. The workaround
fixes (I think) this issue http://bugs.python.org/issue1703286 but also breaks
pointer argument parsing so that byref is always needed. Since ctypesgen needs
python 2.6 or above, this should be safe to revert.
---
ctypesgencore/printer_python/preamble.py | 15 ---------------
1 file changed, 15 deletions(-)

diff --git a/ctypesgencore/printer_python/preamble.py b/ctypesgencore/printer_python/preamble.py
index af38f90..b5da49d 100644
--- a/ctypesgencore/printer_python/preamble.py
+++ b/ctypesgencore/printer_python/preamble.py
@@ -19,21 +19,6 @@ class c_void(Structure):
# POINTER(c_void), so it can be treated as a real pointer.
_fields_ = [('dummy', c_int)]

-def POINTER(obj):
- p = ctypes.POINTER(obj)
-
- # Convert None to a real NULL pointer to work around bugs
- # in how ctypes handles None on 64-bit platforms
- if not isinstance(p.from_param, classmethod):
- def from_param(cls, x):
- if x is None:
- return cls()
- else:
- return x
- p.from_param = classmethod(from_param)
-
- return p
-
class UserString:
def __init__(self, seq):
if isinstance(seq, basestring):
--
1.8.5.2

Lars Munch

unread,
Jan 2, 2014, 4:26:35 PM1/2/14
to ctyp...@googlegroups.com, Lars Munch
I have a function which takes "String" as argument. If I create a string buffer
using create_string_buffer and parse that as the argument to the function, it
fails. The attached patch adds the conversion for c_char arrays which fixes
this issue.
---
ctypesgencore/printer_python/preamble.py | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/ctypesgencore/printer_python/preamble.py b/ctypesgencore/printer_python/preamble.py
index b5da49d..5669e0a 100644
--- a/ctypesgencore/printer_python/preamble.py
+++ b/ctypesgencore/printer_python/preamble.py
@@ -232,6 +232,10 @@ class String(MutableString, Union):
elif isinstance(obj, int):
return cls(cast(obj, POINTER(c_char)))

+ # Convert from c_char array
+ elif isinstance(obj, c_char*len(obj)):
+ return obj
+
# Convert from object
else:
return String.from_param(obj._as_parameter_)
--
1.8.5.2

Lars Munch

unread,
Jan 2, 2014, 4:26:36 PM1/2/14
to ctyp...@googlegroups.com, Lars Munch
use raw strings for lib otherwise c:\\xyz will be c:\xyz
---
ctypesgencore/printer_python/printer.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/ctypesgencore/printer_python/printer.py b/ctypesgencore/printer_python/printer.py
index 022f06d..ade04cc 100644
--- a/ctypesgencore/printer_python/printer.py
+++ b/ctypesgencore/printer_python/printer.py
@@ -146,7 +146,7 @@ class WrapperPrinter:
", ".join([repr(d) for d in self.options.runtime_libdirs])

def print_library(self,library):
- print >>self.file, '_libs["%s"] = load_library("%s")'%(library,library)
+ print >>self.file, "_libs[%r] = load_library(%r)"%(library,library)

def print_module(self,module):
print >>self.file, 'from %s import *' % name
--
1.8.5.2

Reply all
Reply to author
Forward
0 new messages