Skip to content

Commit

Permalink
fixes #39 . c_char is c_ubyte on that arch.
Browse files Browse the repository at this point in the history
  • Loading branch information
trolldbois committed Mar 8, 2021
1 parent 48c7c81 commit 038336e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
14 changes: 5 additions & 9 deletions ctypeslib/codegen/codegenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def type_name(self, t, generate=True):
return self.type_name(t.typ, generate)
elif isinstance(t, typedesc.PointerType):
pointer_class = self.enable_pointer_type()
if t.typ.name == "c_char":
if t.typ.name in ["c_ubyte", "c_char"]:
self.enable_string_cast()
return "%s(%s)" % (pointer_class, self.type_name(t.typ, generate))
elif isinstance(t, typedesc.FunctionType):
Expand Down Expand Up @@ -384,7 +384,7 @@ def Variable(self, tp):
return
elif isinstance(tp.typ, typedesc.PointerType) or isinstance(tp.typ, typedesc.ArrayType):
if (isinstance(tp.typ.typ, typedesc.FundamentalType) and
(tp.typ.typ.name in ["c_char", "c_wchar"])):
(tp.typ.typ.name in ["c_ubyte", "c_char", "c_wchar"])):
# string
# FIXME a char * is not a python string.
# we should output a cstring() construct.
Expand All @@ -403,12 +403,10 @@ def Variable(self, tp):
self._generate(tp.typ.typ)
init_value = self.type_name(tp.typ, False) + "()"
else:
init_value = tp.init if tp.init is not None else (
self.type_name(tp.typ, False)
) + "()"
init_value = tp.init if tp.init is not None else (self.type_name(tp.typ, False)) + "()"
elif isinstance(tp.typ, typedesc.Structure):
init_value = self.type_name(tp.typ, False)
elif isinstance(tp.typ, typedesc.FundamentalType) and tp.typ.name in ["c_char", "c_wchar"]:
elif isinstance(tp.typ, typedesc.FundamentalType) and tp.typ.name in ["c_ubyte", "c_char", "c_wchar"]:
if tp.init is not None:
init_value = repr(tp.init)
else:
Expand All @@ -428,9 +426,7 @@ def Variable(self, tp):
init_value = 0
#
# print it out
print("%s = %s # Variable %s" % (tp.name,
init_value,
self.type_name(tp.typ, False)), file=self.stream)
print("%s = %s # Variable %s" % (tp.name, init_value, self.type_name(tp.typ, False)), file=self.stream)
#
self.names.append(tp.name)
return
Expand Down
17 changes: 17 additions & 0 deletions test/test_macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,23 @@ def test_char_arrays(self):
self.assertEqual(self.namespace.d, 'before after')
# print(self.text_output)

def test_char_arrays_arm_linux(self):
"""c_char is c_ubyte on arm-linux-gnueabihf"""
self.convert('''
#define PRE "before"
#define POST " after"
#define APREPOST PRE POST
char a[] = "what";
char b[] = "why" " though";
char c[] = PRE POST;
char d[] = APREPOST;''', ['-target', 'arm-linux-gnueabihf'])
self.assertEqual(self.namespace.a, "what")
self.assertEqual(self.namespace.b, "why though")
self.assertEqual(self.namespace.c, 'before after')
self.assertEqual(self.namespace.d, 'before after')
# print(self.text_output)

@unittest.skip
def test_define_wchar_t(self):
"""'L' means wchar_t"""
Expand Down

0 comments on commit 038336e

Please sign in to comment.