Skip to content

Commit

Permalink
Specify utf-8 explicitly on behalf of windows
Browse files Browse the repository at this point in the history
  • Loading branch information
mara004 committed Dec 18, 2023
1 parent 0066f70 commit 91267b2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
16 changes: 7 additions & 9 deletions ctypesgen/printer_python/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class WrapperPrinter:
def __init__(self, outpath, options, data, argv):
outpath = Path(outpath).resolve()
status_message(f"Writing to {outpath}.")
self.file = outpath.open("w")
self.file = outpath.open("w", encoding="utf-8")

try:
self.options = options
Expand Down Expand Up @@ -122,9 +122,7 @@ def print_info(self, argv):
# TODO(py38) consider shlex.join()
argv_str = ' '.join([f'"{a}"' if ' ' in a else a for a in argv])
argv_str = self._strip_private_paths(argv_str)
self.file.write(
f"# Auto-generated by: ctypesgen {argv_str}\n"
)
self.file.write(f"# Auto-generated by: ctypesgen {argv_str}\n")


def print_preamble(self):
Expand Down Expand Up @@ -267,16 +265,16 @@ def print_enum(self, enum):
self.file.write("enum_%s = c_int" % enum.tag)


def print_typedef(self, typedef):
self.srcinfo(typedef.src, wants_nl=False)
self.file.write("%s = %s" % (typedef.name, typedef.ctype.py_string()))


def print_constant(self, constant):
self.srcinfo(constant.src, wants_nl=False)
self.file.write("%s = %s" % (constant.name, constant.value.py_string(False)))


def print_typedef(self, typedef):
self.srcinfo(typedef.src, wants_nl=False)
self.file.write("%s = %s" % (typedef.name, typedef.ctype.py_string()))


def print_variable(self, variable):
assert self.options.library, "Binary symbol requires --library LIBNAME"
self.srcinfo(variable.src)
Expand Down
10 changes: 6 additions & 4 deletions tests/ctypesgentest.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,20 @@ def module_from_code(name, python_code):

def generate(header_str, args=[], lang="py"):

# use custom tempfiles scoping so we may retain data for inspection
# also note that python stdlib tempfiles don't play well with windows
# Use custom tempfiles scoping so we may retain data for inspection
# Windows notes:
# - Avoid stdlib tempfiles, they're not usable by anyone except the direct creator, otherwise you'll get permission errors.
# - The default file encoding seems to be cp1252, which is problematic with special chars (such as the banana in the constants test). Need to specify UTF-8 explicitly. PEP 686 should hopefully improve this.

global COUNTER
COUNTER += 1

tmp_in = TMP_DIR/f"in_header_{COUNTER:02d}.h"
tmp_in.write_text(header_str.strip() + "\n")
tmp_in.write_text(header_str.strip() + "\n", encoding="utf-8")
try:
tmp_out = TMP_DIR/f"out_bindings_{COUNTER:02d}.{lang}"
ctypesgen_main(["-i", tmp_in, "-o", tmp_out, "--output-language", lang, *args])
content = tmp_out.read_text()
content = tmp_out.read_text(encoding="utf-8")
finally:
if CLEANUP_OK:
tmp_in.unlink()
Expand Down

0 comments on commit 91267b2

Please sign in to comment.