From b6c6d6bbf67e5ef4492ad76d6a62ce936528d1a5 Mon Sep 17 00:00:00 2001 From: Nicolas Kruse Date: Mon, 3 Nov 2025 22:49:31 +0100 Subject: [PATCH] code style fixed and type hints in get_binaries.py fixed --- notes/crash_windows.md | 2 +- src/copapy/_basic_types.py | 2 +- src/copapy/_compiler.py | 10 +++++----- src/copapy/_math.py | 20 ++++++++++---------- src/copapy/_stencils.py | 8 ++++---- src/copapy/_target.py | 6 +++--- src/coparun/runmem.c | 20 ++++++++++---------- stencils/generate_stencils.py | 2 +- tests/test_compile.py | 2 +- tests/test_compile_aarch64.py | 4 ++-- tests/test_math.py | 2 +- tests/test_readme_example.py | 2 +- tools/get_binaries.py | 5 +++-- tools/make_example.py | 1 - 14 files changed, 43 insertions(+), 43 deletions(-) diff --git a/notes/crash_windows.md b/notes/crash_windows.md index df094ba..a05d0ab 100644 --- a/notes/crash_windows.md +++ b/notes/crash_windows.md @@ -42,7 +42,7 @@ case RUN_PROG: rel_entr_point = *(uint32_t*)bytes; bytes += 4; printf("RUN_PROG rel_entr_point=%i\n", rel_entr_point); entr_point = (int (*)())(executable_memory + rel_entr_point); - + mark_mem_executable(executable_memory, executable_memory_len); int ret = entr_point(); printf("Return value: %i\n", ret); diff --git a/src/copapy/_basic_types.py b/src/copapy/_basic_types.py index bd2812a..c5ced93 100644 --- a/src/copapy/_basic_types.py +++ b/src/copapy/_basic_types.py @@ -32,7 +32,7 @@ def transl_type(t: str) -> str: class Node: - """A Node represents an computational operation like ADD or other operations + """A Node represents an computational operation like ADD or other operations like read and write from or to the memory or IOs. In the computation graph Nodes are connected via Nets. diff --git a/src/copapy/_compiler.py b/src/copapy/_compiler.py index b9beb1a..5fb90ee 100644 --- a/src/copapy/_compiler.py +++ b/src/copapy/_compiler.py @@ -159,7 +159,7 @@ def get_nets(*inputs: Iterable[Iterable[Any]]) -> list[Net]: def get_data_layout(variable_list: Iterable[Net], sdb: stencil_database, offset: int = 0) -> tuple[list[tuple[Net, int, int]], int]: """Get memory layout for the provided variables - + Arguments: variable_list: Variables to layout sdb: Stencil database for size lookup @@ -184,7 +184,7 @@ def get_data_layout(variable_list: Iterable[Net], sdb: stencil_database, offset: def get_section_layout(section_indexes: Iterable[int], sdb: stencil_database, offset: int = 0) -> tuple[list[tuple[int, int, int]], int]: """Get memory layout for the provided sections - + Arguments: section_indexes: Sections (by index) to layout sdb: Stencil database for size lookup @@ -210,7 +210,7 @@ def get_aux_function_mem_layout(function_names: Iterable[str], sdb: stencil_data function_names: Function names to layout sdb: Stencil database for size lookup offset: Starting offset for layout - + Returns: Tuple of list of (function_name, start_offset, length) and total length """ @@ -320,7 +320,7 @@ def compile_to_dag(node_list: Iterable[Node], sdb: stencil_database) -> tuple[bi #print(patch.type, patch.addr, binw.Command.PATCH_FUNC, node.name, '->', patch.target_symbol_name) else: raise ValueError(f"Unsupported: {node.name} {reloc.target_symbol_info} {reloc.target_symbol_name}") - + patch_list.append(patch) offset += len(data) @@ -358,7 +358,7 @@ def compile_to_dag(node_list: Iterable[Node], sdb: stencil_database) -> tuple[bi else: raise ValueError(f"Unsupported: {name=} {reloc.target_symbol_info=} {reloc.target_symbol_name=} {reloc.target_section_index}") - + patch_list.append(patch) #assert False, aux_function_mem_layout diff --git a/src/copapy/_math.py b/src/copapy/_math.py index 6dbbc57..0446693 100644 --- a/src/copapy/_math.py +++ b/src/copapy/_math.py @@ -12,10 +12,10 @@ def sqrt(x: float | int) -> float: ... def sqrt(x: variable[Any]) -> variable[float]: ... def sqrt(x: NumLike) -> variable[float] | float: """Square root function - + Arguments: x: Input value - + Returns: Square root of x """ @@ -30,10 +30,10 @@ def sin(x: float | int) -> float: ... def sin(x: variable[Any]) -> variable[float]: ... def sin(x: NumLike) -> variable[float] | float: """Sine function - + Arguments: x: Input value - + Returns: Square root of x """ @@ -47,10 +47,10 @@ def cos(x: float | int) -> float: ... def cos(x: variable[Any]) -> variable[float]: ... def cos(x: NumLike) -> variable[float] | float: """Cosine function - + Arguments: x: Input value - + Returns: Cosine of x """ @@ -64,10 +64,10 @@ def tan(x: float | int) -> float: ... def tan(x: variable[Any]) -> variable[float]: ... def tan(x: NumLike) -> variable[float] | float: """Tangent function - + Arguments: x: Input value - + Returns: Tangent of x """ @@ -83,10 +83,10 @@ def get_42() -> variable[float]: def abs(x: T) -> T: """Absolute value function - + Arguments: x: Input value - + Returns: Absolute value of x """ diff --git a/src/copapy/_stencils.py b/src/copapy/_stencils.py index 9e1ec9a..771cffb 100644 --- a/src/copapy/_stencils.py +++ b/src/copapy/_stencils.py @@ -143,7 +143,7 @@ class stencil_database(): end_index = symbol.fields['st_size'] for reloc in symbol.relocations: - + # address to fist byte to patch relative to the start of the symbol patch_offset = reloc.fields['r_offset'] - symbol.fields['st_value'] - start_index @@ -175,7 +175,7 @@ class stencil_database(): patch_offset = pr.fields['r_offset'] - relocation.function_offset - relocation.start + function_offset #print(f"xx {pr.fields['r_offset'] - relocation.function_offset} {relocation.target_symbol_name=} {pr.fields['r_offset']=} {relocation.function_offset=} {relocation.start=} {function_offset=}") scale = 1 - + if pr.type.endswith('_PLT32') or pr.type.endswith('_PC32'): # S + A - P mask = 0xFFFFFFFF # 32 bit @@ -233,7 +233,7 @@ class stencil_database(): """Return recursively all functions called by stencils or by other functions Args: names: function or stencil names - + Returns: set of all sub function names """ @@ -266,7 +266,7 @@ class stencil_database(): def get_function_code(self, name: str, part: Literal['full', 'start', 'end'] = 'full') -> bytes: """Returns machine code for a specified function name. - + Args: name: function name part: part of the function to return ('full', 'start', 'end') diff --git a/src/copapy/_target.py b/src/copapy/_target.py index a30ffe4..75bc2a2 100644 --- a/src/copapy/_target.py +++ b/src/copapy/_target.py @@ -20,7 +20,7 @@ class Target(): """ def __init__(self, arch: str = 'native', optimization: str = 'O3') -> None: """Initialize Target object - + Arguments: arch: Target architecture optimization: Optimization level @@ -30,7 +30,7 @@ class Target(): def compile(self, *variables: int | float | variable[int] | variable[float] | variable[bool] | Iterable[int | float | variable[int] | variable[float] | variable[bool]]) -> None: """Compiles the code to compute the given variables. - + Arguments: variables: Variables to compute """ @@ -76,7 +76,7 @@ class Target(): Arguments: net: Variable to read - + Returns: Value of the variable """ diff --git a/src/coparun/runmem.c b/src/coparun/runmem.c index 78d22e6..6307c64 100644 --- a/src/coparun/runmem.c +++ b/src/coparun/runmem.c @@ -23,7 +23,7 @@ uint8_t *executable_memory = NULL; uint32_t executable_memory_len = 0; entry_point_t entr_point = NULL; int data_offs = 0; - + void patch(uint8_t *patch_addr, uint32_t patch_mask, int32_t value) { uint32_t *val_ptr = (uint32_t*)patch_addr; uint32_t original = *val_ptr; @@ -84,7 +84,7 @@ int parse_commands(uint8_t *bytes) { uint32_t size; int end_flag = 0; uint32_t rel_entr_point = 0; - + while(!end_flag) { command = *(uint32_t*)bytes; bytes += 4; @@ -96,14 +96,14 @@ int parse_commands(uint8_t *bytes) { LOG("ALLOCATE_DATA size=%i mem_addr=%p\n", size, (void*)data_memory); if (!update_data_offs()) end_flag = -4; break; - + case COPY_DATA: offs = *(uint32_t*)bytes; bytes += 4; size = *(uint32_t*)bytes; bytes += 4; LOG("COPY_DATA offs=%i size=%i\n", offs, size); memcpy(data_memory + offs, bytes, size); bytes += size; break; - + case ALLOCATE_CODE: size = *(uint32_t*)bytes; bytes += 4; executable_memory = allocate_executable_memory(size); @@ -112,14 +112,14 @@ int parse_commands(uint8_t *bytes) { //LOG("# d %i c %i off %i\n", data_memory, executable_memory, data_offs); if (!update_data_offs()) end_flag = -4; break; - + case COPY_CODE: offs = *(uint32_t*)bytes; bytes += 4; size = *(uint32_t*)bytes; bytes += 4; LOG("COPY_CODE offs=%i size=%i\n", offs, size); memcpy(executable_memory + offs, bytes, size); bytes += size; break; - + case PATCH_FUNC: offs = *(uint32_t*)bytes; bytes += 4; patch_mask = *(uint32_t*)bytes; bytes += 4; @@ -129,7 +129,7 @@ int parse_commands(uint8_t *bytes) { offs, patch_mask, patch_scale, value); patch(executable_memory + offs, patch_mask, value / patch_scale); break; - + case PATCH_OBJECT: offs = *(uint32_t*)bytes; bytes += 4; patch_mask = *(uint32_t*)bytes; bytes += 4; @@ -166,13 +166,13 @@ int parse_commands(uint8_t *bytes) { entr_point = (entry_point_t)(executable_memory + rel_entr_point); mark_mem_executable(executable_memory, executable_memory_len); break; - + case RUN_PROG: LOG("RUN_PROG\n"); int ret = entr_point(); BLOG("Return value: %i\n", ret); break; - + case READ_DATA: offs = *(uint32_t*)bytes; bytes += 4; size = *(uint32_t*)bytes; bytes += 4; @@ -197,7 +197,7 @@ int parse_commands(uint8_t *bytes) { LOG("END_COM\n"); end_flag = 1; break; - + default: LOG("Unknown command\n"); end_flag = -1; diff --git a/stencils/generate_stencils.py b/stencils/generate_stencils.py index ddd2292..98b1cd4 100644 --- a/stencils/generate_stencils.py +++ b/stencils/generate_stencils.py @@ -216,7 +216,7 @@ if __name__ == "__main__": # Scalar arithmetic: types = ['int', 'float'] ops = ['add', 'sub', 'mul', 'div', 'floordiv', 'gt', 'ge', 'eq', 'ne', 'pow'] - int_ops = ['bwand', 'bwor', 'bwxor', 'lshift', 'rshift'] + int_ops = ['bwand', 'bwor', 'bwxor', 'lshift', 'rshift'] for t1 in types: code += get_result_stubs1(t1) diff --git a/tests/test_compile.py b/tests/test_compile.py index 44c101b..7f4fc08 100644 --- a/tests/test_compile.py +++ b/tests/test_compile.py @@ -1,4 +1,4 @@ -from copapy import variable, NumLike +from copapy import NumLike from copapy.backend import Write, compile_to_dag, add_read_command import copapy as cp import subprocess diff --git a/tests/test_compile_aarch64.py b/tests/test_compile_aarch64.py index 32516ea..b7a941d 100644 --- a/tests/test_compile_aarch64.py +++ b/tests/test_compile_aarch64.py @@ -1,4 +1,4 @@ -from copapy import variable, NumLike +from copapy import NumLike from copapy.backend import Write, compile_to_dag, add_read_command import subprocess from copapy import _binwrite @@ -26,7 +26,7 @@ def check_for_qemu() -> bool: command = qemu_command + ['--version'] try: result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf8', check=False) - except: + except Exception: return False return result.returncode == 0 diff --git a/tests/test_math.py b/tests/test_math.py index 8241b3e..8c3fa82 100644 --- a/tests/test_math.py +++ b/tests/test_math.py @@ -43,7 +43,7 @@ def test_fine(): cp.sin(c_f), cp.cos(c_f), cp.tan(c_f)) # , c_i & 3) - + ret_refe = (a_f ** 2, a_i ** -1, cp.sqrt(a_i), diff --git a/tests/test_readme_example.py b/tests/test_readme_example.py index ac92450..cc5f5f4 100644 --- a/tests/test_readme_example.py +++ b/tests/test_readme_example.py @@ -27,4 +27,4 @@ def test_readme_example(): assert tg.read_value(e) == pytest.approx(cp.sqrt(0.87), 0.001) # pyright: ignore[reportUnknownMemberType] if __name__ == "__main__": - test_readme_example() \ No newline at end of file + test_readme_example() diff --git a/tools/get_binaries.py b/tools/get_binaries.py index 89fdae3..a5d228b 100644 --- a/tools/get_binaries.py +++ b/tools/get_binaries.py @@ -1,3 +1,4 @@ +from typing import Any import os import json from urllib.request import urlopen, Request @@ -5,13 +6,13 @@ from urllib.request import urlopen, Request OWNER = "Nonannet" REPO = "copapy" -def fetch_json(url: str): +def fetch_json(url: str) -> Any: req = Request(url, headers={"User-Agent": "Python"}) with urlopen(req, timeout=10) as resp: assert resp.status == 200 return json.load(resp) -def download_file(url: str, dest_path: str): +def download_file(url: str, dest_path: str) -> None: req = Request(url, headers={"User-Agent": "Python"}) with urlopen(req, timeout=30) as resp, open(dest_path, "wb") as f: f.write(resp.read()) diff --git a/tools/make_example.py b/tools/make_example.py index 8dc8de2..dddaad6 100644 --- a/tools/make_example.py +++ b/tools/make_example.py @@ -1,6 +1,5 @@ from copapy import variable from copapy.backend import Write, compile_to_dag, stencil_db_from_package -import copapy as cp from copapy._binwrite import Command