Skip to content

Commit

Permalink
Try to fix test suite on mac and windows
Browse files Browse the repository at this point in the history
emphasis lies on "Try"
  • Loading branch information
mara004 committed Dec 14, 2023
1 parent bc2c843 commit c3ebf5c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
19 changes: 8 additions & 11 deletions tests/ctypesgentest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,19 @@ def module_from_code(name, python_code):

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

tmp_header = tempfile.NamedTemporaryFile()
tmp_header.write(header_str.encode())
tmp_header.seek(0)
tmp_out = tempfile.NamedTemporaryFile()
ctypesgen_main(["-i", tmp_header.name, "-o", tmp_out.name, "--output-language", lang, *args])
tmp_in = TEST_DIR/"tmp_in.h"
tmp_in.write_text(header_str)
tmp_out = TEST_DIR/"tmp_out.py"
ctypesgen_main(["-i", tmp_in, "-o", tmp_out, "--output-language", lang, *args])
content = tmp_out.read_text()

tmp_header_name = tmp_header.name
tmp_header.close()
tmp_out.seek(0)
content = tmp_out.read().decode()
tmp_out.close()
tmp_in.unlink()
tmp_out.unlink()

if lang.startswith("py"):
return module_from_code("tmp_module", content)
elif lang == "json":
return json.loads(content), tmp_header_name
return json.loads(content), str(tmp_in)
else:
assert False

Expand Down
37 changes: 18 additions & 19 deletions tests/testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
)


if sys.platform.startswith("win32"):
# pick something from %windir%\system32\msvc*dll that includes stdlib
STDLIB_NAME = "msvcrt"
else:
STDLIB_NAME = "c"


def compare_json(test_instance, json, json_ans, verbose=False):
json_helper = JsonHelper()
json_helper.prepare(json)
Expand Down Expand Up @@ -103,12 +110,7 @@ class StdlibTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
header_str = "#include <stdlib.h>\n"
if sys.platform == "win32":
# pick something from %windir%\system32\msvc*dll that include stdlib
library = "msvcrt"
else:
library = "c" # libc
cls.module = generate(header_str, ["-l", library, "--all-headers", "--symbol-rules", r"if_needed=__\w+"])
cls.module = generate(header_str, ["-l", STDLIB_NAME, "--all-headers", "--symbol-rules", r"if_needed=__\w+"])

@classmethod
def tearDownClass(cls):
Expand Down Expand Up @@ -2038,13 +2040,11 @@ def setUpClass(cls):
#include <math.h>
#define sin_plus_y(x,y) (sin(x) + (y))
"""
if sys.platform == "win32":
# pick something from %windir%\system32\msvc*dll that include stdlib
library = "msvcrt"
elif sys.platform.startswith("linux"):
if sys.platform.startswith("linux"):
library = "m" # libm
else:
library = "c" # libc
library = STDLIB_NAME

# math.h contains a macro NAN = (0.0 / 0.0) which triggers a ZeroDivisionError on module import, so exclude the symbol.
# Also exclude unused members starting with __ to avoid garbage in the output.
# TODO consider adding options like --replace-symbol/--add-symbols/--add-imports so the caller could e.g. redefine NAN=math.nan
Expand Down Expand Up @@ -2550,7 +2550,7 @@ class MacromanEncodeTest(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.mac_roman_file = "temp_mac.h"
cls.mac_roman_file = TEST_DIR/"temp_mac.h"
mac_header_str = b"""
#define kICHelper "\xa9\\pHelper\xa5"
Expand All @@ -2559,8 +2559,8 @@ def setUpClass(cls):
with open(cls.mac_roman_file, "wb") as mac_file:
mac_file.write(mac_header_str)

header_str = """
#include "temp_mac.h"
header_str = f"""
#include "{cls.mac_roman_file}"
#define MYSTRING kICHelper
Expand All @@ -2586,20 +2586,19 @@ class VariadicFunctionTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
header_str = "#include <stdio.h>\n"
cls.module = generate(header_str, ["-l", "c", "--all-headers", "--symbol-rules", r"if_needed=__\w+"])
cls.module = generate(header_str, ["-l", STDLIB_NAME, "--all-headers", "--symbol-rules", r"if_needed=__\w+"])

def test_type_error_catch(self):
with self.assertRaises(ctypes.ArgumentError):
self.module.printf(123)

def test_call(self):
tmp = NamedTemporaryFile()
tmp = TEST_DIR/"tmp_testdata.txt"
c_file = self.module.fopen(tmp.name.encode(), b"w")
self.module.fprintf(c_file, b"Test variadic function: %s %d", b"Hello", 123)
self.module.fclose(c_file)
tmp.seek(0)
assert tmp.read() == b"Test variadic function: Hello 123"
tmp.close()
assert tmp.read_bytes() == b"Test variadic function: Hello 123"
tmp.unlink()


def main():
Expand Down

0 comments on commit c3ebf5c

Please sign in to comment.