You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow homogeneous tuples as function arguments (pyccel#1850)
Allow 1D homogeneous tuples as function arguments. Fixespyccel#738.
Homogeneous tuples as arguments already worked in the low level code as
homogeneous tuples are handled as arrays, however the wrapper was
missing which meant that such functions couldn't be called from Python.
In order to handle multi-level tuples some reorganisation of the wrapper
was necessary. Namely the introduction of the
`_extract_X_FunctionDefArgument` functions. However Python multi-level
tuples may have different lengths in the elements. It proved difficult
to check the size of the elements in a generic way so this was left for
later work (the obvious solution is to use lists to support this).
**Commit Summary**
- Update docs
- Update `BindCFunctionDefArgument` to handle tuples as strideless
arrays
- Add descriptions of more tuple functions from `Python.h`. Group these
functions together in `ast.cwrapper`
- Make `dtype` a searchable attribute of `SyntacticTypeAnnotation`
- Fix filter for used template type annotations so it also searches
through indexed elements to find objects in a tuple annotation.
- Add tests for tuples as arguments
- Rename `_get_check_function`->`_get_type_check_condition`.
- Allow `_get_type_check_condition` to return a condition which is not a
function call (e.g. a Variable)
- Add `body` argument to `_get_type_check_condition` to allow additional
commands to be run before determining if the type is correct.
- Add type checks for homogeneous tuples
- Introduce `_extract_X_FunctionDefArgument` functions to describe
container unpacking recursively
- Add `_extract_HomogeneousTupleType_FunctionDefArgument` function to
unpack 1D homogeneous tuples.
- Ensure errors are raised for multi-D tuples.
The arguments and keyword arguments are unpacked into individual `PyObject` pointers.
378
378
Each of these objects is checked to verify the type. If the type does not match the expected type then an error is raised as described in the [C-API documentation](https://docs.python.org/3/c-api/intro.html#exceptions).
379
-
If the type does match then the value is unpacked into a C object. This is done using custom functions defined in `pyccel/stdlib/cwrapper/` or `pyccel/stdlib/cwrapper_ndarrays/` (see these files for more details).
379
+
If the type does match then the value is unpacked into a C object. This is done using custom functions defined in `pyccel/stdlib/cwrapper/` or `pyccel/stdlib/cwrapper_ndarrays/` (see these files for more details) or using functions provided by `Python.h`.
380
+
381
+
In order to create all the nodes necessary to describe the unpacking of the arguments we use functions named `_extract_X_FunctionDefArgument` where `X` is the type of the object being extracted from the `FunctionDefArgument`. This allows such functions to call each other recursively. This is notably useful for container types (tuples, lists, etc) whose elements may themselves be container types. The types of scalars are checked in the same way regardless of whether they are arguments or elements of a container so this also reduces code duplication.
380
382
381
383
Once C objects have been retrieved the function is called normally.
382
384
383
385
Finally all the arguments are packed into a Python tuple stored in a `PyObject` and are returned.
384
386
385
387
The wrapper is attached to the module via a `PyMethodDef` (see C-API [docs](https://docs.python.org/3/c-api/structures.html#c.PyMethodDef)).
Copy file name to clipboardExpand all lines: docs/type_annotations.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,7 +41,7 @@ In general string type hints must be used to provide Pyccel with information abo
41
41
42
42
## Tuples
43
43
44
-
Currently tuples are supported locally in Pyccel but cannot be passed as arguments or returned. The implementation of the type annotations (as a first step to adding the missing support) is in progress. Currently homogeneous tuple type annotations are supported for local variables. See [Container types in Pyccel](./containers.md#tuples) for more information about tuple handling. When creating multiple dimensional tuples it is therefore important to ensure that all objects have compatible sizes otherwise they will be handled as inhomogeneous tuples.
44
+
Currently Pyccel supports tuples used locally in functions and in certain cases as arguments, but not as returned objects or module variables. The implementation of the type annotations (including adding the missing support) is in progress. Currently homogeneous tuple type annotations are supported for local variables and function arguments (if the tuples contain scalar objects). Internally we handle homogeneous tuples as though they were NumPy arrays. When creating multiple dimensional tuples it is therefore important to ensure that all objects have compatible sizes otherwise they will be handled as inhomogeneous tuples.
45
45
46
46
To declare a homogeneous tuple the syntax is as follows:
0 commit comments