From e8adb97228a0638fc9d9872671c3a491868b61c5 Mon Sep 17 00:00:00 2001 From: Robert Chisholm Date: Fri, 1 Dec 2023 12:20:33 +0000 Subject: [PATCH] BugFix: AgentPython was missing ID->id_t type conversion. (#1153) * BugFix: AgentPython was missing ID->id_t type conversion. Improved detail in various codgen exceptions too. * BugFix: #1147 broke Python 3.8.13 This variant has been tested on both 3.8.13 and 3.10.12 --- swig/python/codegen/__init__.py | 4 ++-- swig/python/codegen/codegen.py | 13 +++++++------ tests/python/codegen/test_codegen.py | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/swig/python/codegen/__init__.py b/swig/python/codegen/__init__.py index cf27dd11d..e8ec7912f 100644 --- a/swig/python/codegen/__init__.py +++ b/swig/python/codegen/__init__.py @@ -2,7 +2,7 @@ from io import StringIO from .codegen import CodeGenerator from .codegen import CodeGenException -from typing import Callable, Union +from typing import Callable, Union, _SpecialForm import ast import inspect @@ -51,7 +51,7 @@ def translate(function: Union[str, Callable]) -> str: prepend_c_source = "" # Find all annotated variables for key, val in module_annontations.items(): - if val.__name__ == "Final" or val.__name__ == "constant": + if (isinstance(val, _SpecialForm) and val._name == "Final") or val.__name__ == "constant": # Locate the literal for that variable (Python will precompute anything e.g. math.sqrt(12.5)) for mem in module_members: if key == mem[0]: diff --git a/swig/python/codegen/codegen.py b/swig/python/codegen/codegen.py index 4c04359e9..09864c024 100644 --- a/swig/python/codegen/codegen.py +++ b/swig/python/codegen/codegen.py @@ -102,7 +102,8 @@ class CodeGenerator: "Int32": "int_32", "UInt32": "uint_32", "Int64": "int_64", - "UInt64": "uint_64" + "UInt64": "uint_64", + "ID": "flamegpu::id_t" } @@ -197,7 +198,7 @@ def RaiseWarning(self, tree, str): def RaiseError(self, tree, str): raise CodeGenException(f"Error ({tree.lineno}, {tree.col_offset}): {str}") - ############### Cutsom Unparsing methods ############### + ############### Custom Unparsing methods ############### # These are special versions of the ast unparsing # # dispatch functions. # ######################################################## @@ -282,15 +283,15 @@ def dispatchType(self, tree): """ if isinstance(tree, ast.Name): if tree.id not in self.basic_arg_types: - self.RaiseError(tree, "Not a supported type") + self.RaiseError(tree, "%s is not a supported type"%(tree.id)) self.write(tree.id) elif isinstance(tree, ast.Attribute): if not isinstance(tree.value, ast.Name) : self.RaiseError(tree, "Not a supported type") if not (tree.value.id == "numpy" or tree.value.id == "np"): - self.RaiseError(tree, "Not a supported type") + self.RaiseError(tree, "%s.%s is not a supported type"%(tree.value.id, tree.attr)) if tree.attr not in self.numpytypes: - self.RaiseError(tree, "Not a supported numpy type") + self.RaiseError(tree, "%s.%s is not a supported numpy type"%(tree.value.id, tree.attr)) self.write(self.numpytypes[tree.attr]) else: self.RaiseError(tree, "Not a supported type") @@ -450,7 +451,7 @@ def dispatchMemberFunction(self, t, t_parent): self.write(py_func) t_parent.call_type = "AgentOut" else: - self.RaiseError(t, f"Unknown or unsupported nested attribute in {t.value.value.id}") + self.RaiseError(t, f"Unknown or unsupported nested attribute {t.value.attr} in {t.value.value.id}") # Non nested member functions (e.g. x.y()) elif isinstance(t.value, ast.Name): # pyflamegpu singleton diff --git a/tests/python/codegen/test_codegen.py b/tests/python/codegen/test_codegen.py index 5a0cfc8cd..7175590fb 100644 --- a/tests/python/codegen/test_codegen.py +++ b/tests/python/codegen/test_codegen.py @@ -799,7 +799,7 @@ def test_fgpu_supported_types(self): self._checkExpected("a: numpy.float32", "float a;") self._checkExpected("a: numpy.float64", "double a;") # check unsupported - self._checkException("a: numpy.unsupported", "Not a supported numpy type") + self._checkException("a: numpy.unsupported", "numpy.unsupported is not a supported numpy type") def test_fgpu_constexpr(self):