fix tests and replace exec() in compute/_jit.py#8106
fix tests and replace exec() in compute/_jit.py#8106water0615 wants to merge 2 commits intoNVIDIA:mainfrom
Conversation
|
Thanks for the PR, @water0615! By its nature, We don't want to be too restrictive in terms of the contents of the user-provided callables. For instance, the That being said, there are improvements we can make here in terms of validating, e.g., struct members to ensure they are identifiers. For example, I think the following patch would take us quite far in terms of better error reporting if someone passes something other than a identifier for the struct member name: @@ -55,6 +55,13 @@ def gpu_struct(
# At this point, field_dict must be a dict
assert isinstance(field_dict, dict)
+ # Validate field names are valid Python identifiers before use in exec()
+ for key in field_dict:
+ if not isinstance(key, str) or not key.isidentifier():
+ raise ValueError(
+ f"gpu_struct field name {key!r} is not a valid Python identifier"
+ )
+
# Normalize fields for storage on the struct class
field_spec = {}
for key, val in field_dict.items():Do you think we could start there (and possibly some unit tests for the above)? |
| ) | ||
| return namespace["impl"] | ||
| def impl(struct_val, idx): | ||
| return getattr(struct_val, field_name) |
There was a problem hiding this comment.
I believe numba's type system will need to see the attribute access rather than access via getattr; so this may not work as intended.
Instead, we could assert that all field names are valid Python identifiers via str.isidentifier().
|
I'm going to go ahead and close this PR, as #8235 addresses the issue without restricting the contents of user-provided callables. |
Description
closes
fix tests and replace exec() in compute/_jit.py
Checklist