-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chapel-py: check for invalid instance of bridged classes to avoid int…
…ernal errors (#26309) Closes #25937. This PR improves the situation in which AST or other bridged objects are incorrectly constructed (specifically, when they are constructed explicitly by the user). In this case, their internal representations can contain `null` values, which causes exception. This PR introduces checking for such cases, raising Python exceptions. Now, none of the following operations produce internal errors, and raise instead: <details> <summary>Expand console output</summary> ```console >>> import chapel >>> c = chapel.Context() >>> id = chapel.Identifier(c) >>> id.unique_id() Traceback (most recent call last): File "<python-input-3>", line 1, in <module> id.unique_id() ~~~~~~~~~~~~^^ RuntimeError: invalid instance of class 'AstNode'; please do not manually construct instances of this class. >>> for node in id: ... print(node) ... Traceback (most recent call last): File "<python-input-4>", line 1, in <module> for node in id: ^^ RuntimeError: invalid instance of class 'AstNode'; please do not manually construct instances of this class. >>> tmp = BoolParam() Traceback (most recent call last): File "<python-input-5>", line 1, in <module> tmp = BoolParam() ^^^^^^^^^ NameError: name 'BoolParam' is not defined >>> tmp = chapel.BoolParam() Traceback (most recent call last): File "<python-input-6>", line 1, in <module> tmp = chapel.BoolParam() TypeError: function takes exactly 1 argument (0 given) >>> tmp = chapel.BoolParam(c) >>> print(tmp) Traceback (most recent call last): File "<python-input-8>", line 1, in <module> print(tmp) ~~~~~^^^^^ RuntimeError: invalid instance of class 'Param'; please do not manually construct instances of this class. ``` </details> The main change in this PR is that, to signal an incorrectly-constructed type, we need an "empty value". This has to happen for all objects in `method-tables.h`, because all those methods are generated using the same body. Some objects defined in `method-tables.h` contain non-pointer values and would return them by reference; however, references cannot be used to signal an empty value, and cannot be stored in `std::optional`. Thus, this PR switches the implementation to always return pointers from `.unwrap()`, using `nullptr` to signal failure. This requires an SFINAE-based re-implementation of `unwrap` for `PythonClass` and subtypes. The generated types do not use `PythonClass`, and always use pointers, so their implementation is only adjusted to raise. Reviewed by @jabraham17 -- thanks! ## Testing - [x] `make test-cls` works
- Loading branch information
Showing
7 changed files
with
101 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.