pyccel.ast.cwrapper module#
Module representing objects (functions/variables etc) required for the interface between Python code and C code (using Python/C Api and cwrapper.c). This file contains classes but also many FunctionDef/Variable instances representing objects defined in Python.h.
- class pyccel.ast.cwrapper.PyArgKeywords(name, arg_names)[source]#
Bases:
PyccelAstNode
Represents the list containing the names of all arguments to a function. This information allows the function to be called by keyword
- Parameters:
name (str) – The name of the variable in which the list is stored
arg_names (list of str) – A list of the names of the function arguments
- property arg_names#
The names of the arguments to the function which are contained in the PyArgKeywords list
- property name#
The name of the variable in which the list of all arguments to the function is stored
- class pyccel.ast.cwrapper.PyArg_ParseTupleNode(python_func_args, python_func_kwargs, c_func_args, parse_args, arg_names)[source]#
Bases:
PyccelAstNode
Represents a call to the function PyArg_ParseTupleNode.
Represents a call to the function PyArg_ParseTupleNode from Python.h. This function collects the expected arguments from self, args, kwargs and packs them into variables with datatype PyccelPyObject.
- Parameters:
python_func_args (Variable) – Args provided to the function in Python.
python_func_kwargs (Variable) – Kwargs provided to the function in Python.
c_func_args (list of Variable) – List of expected arguments. This helps determine the expected output types.
parse_args (list of Variable) – List of arguments into which the result will be collected.
arg_names (list of str) – A list of the names of the function arguments.
- property arg_names#
The PyArgKeywords object which contains all the names of the function’s arguments
- property args#
The arguments into which the python args and kwargs are collected
- property flags#
The flags indicating the types of the objects.
The flags indicating the types of the objects to be collected from the Python arguments passed to the function.
- property pyarg#
The variable containing all positional arguments passed to the function
- property pykwarg#
The variable containing all keyword arguments passed to the function
- class pyccel.ast.cwrapper.PyArgumentError(error_type, error_msg: str, **kwargs)[source]#
Bases:
PyccelAstNode
Class to display errors related to arguments.
Class to display errors related to arguments. This class helps format the arguments to display the type of the received argument.
- Parameters:
- property args#
The arguments whose types are printed in the error message.
The arguments whose types are printed in the error message. These arguments are displayed in the order they appear in the error message.
- property error_msg#
The error message that should be formatted.
The error message that should be formatted.
- property error_type#
The error type that should be raised.
The error type that should be raised.
- class pyccel.ast.cwrapper.PyBuildValueNode(result_args=())[source]#
Bases:
PyccelFunction
Represents a call to the function PyBuildValueNode.
The function PyBuildValueNode can be found in Python.h. It describes the creation of a new Python object based on a format string. More details can be found in Python’s docs.
- Parameters:
result_args (list of Variable) – List of arguments which the result will be built from.
- property args#
The arguments passed to the function.
Tuple containing all the arguments passed to the function call.
- property flags#
- class pyccel.ast.cwrapper.PyCapsule_Import(module_name)[source]#
Bases:
PyccelFunction
Represents a call to the function PyCapsule_Import.
The function PyCapsule_Import can be found in Python.h. It describes the initialisation of a capsule by importing the information from another module. A capsule contains all information from a module which should be exposed to other modules that import this module. See https://docs.python.org/3/extending/extending.html#using-capsules for a tutorial involving capsules. See https://docs.python.org/3/c-api/capsule.html#c.PyCapsule_Import for the API docstrings for this method.
- Parameters:
module_name (str) – The name of the module being retrieved.
- property capsule_name#
Get the name of the capsule being retrieved.
Get the name of the capsule being retrieved.
- class pyccel.ast.cwrapper.PyCapsule_New(API_var, module_name)[source]#
Bases:
PyccelFunction
Represents a call to the function PyCapsule_New.
The function PyCapsule_New can be found in Python.h. It describes the creation of a capsule. A capsule contains all information from a module which should be exposed to other modules that import this module. See https://docs.python.org/3/extending/extending.html#using-capsules for a tutorial involving capsules. See https://docs.python.org/3/c-api/capsule.html#c.PyCapsule_New for the API docstrings for this method.
- Parameters:
API_var (Variable) – The variable which contains all elements of the API which should be exposed.
module_name (str) – The name of the module being exposed.
- property API_var#
Get the variable describing the API.
Get the variable which contains all elements of the API which should be exposed.
- property capsule_name#
Get the name of the capsule being created.
Get the name of the capsule being created.
- class pyccel.ast.cwrapper.PyClassDef(original_class, struct_name, type_name, scope, **kwargs)[source]#
Bases:
ClassDef
Class to hold a class definition which is accessible from Python.
Class to hold a class definition which is accessible from Python.
- Parameters:
original_class (ClassDef) – The original class being wrapped.
struct_name (str) – The name of the structure which will hold the Python-compatible class definition.
type_name (str) – The name of the instance of the Python-compatible class definition structure. This object is necessary to add the class to the module.
scope (Scope) – The scope for the class contents.
**kwargs (dict) – See ClassDef.
See also
ClassDef
The class from which PyClassDef inherits. This is also the object being wrapped.
- add_alloc_method(f)[source]#
Add the wrapper for __new__ to the class definition.
Add the wrapper for __new__ which allocates the memory for the class instance.
- Parameters:
f (PyFunctionDef) – The wrapper for the __new__ function.
- add_new_magic_method(method)[source]#
Add a new magic method to the current class.
Add a new magic method to the current ClassDef.
- Parameters:
method (FunctionDef) – The Method that will be added.
- add_property(p)[source]#
Add a class property which has been wrapped.
Add a class property which has been wrapped.
- Parameters:
p (PyccelAstNode) – The new wrapped property which is added to the class.
- property magic_methods#
Get the magic methods describing methods.
Get the magic methods describing methods such as __add__.
- property new_func#
Get the wrapper for __new__.
Get the wrapper for __new__ which allocates the memory for the class instance.
- property original_class#
The class which is wrapped by this PyClassDef.
The original class which would be printed in pure C which is not compatible with Python.
- property properties#
Get all wrapped class properties.
Get all wrapped class properties.
- property struct_name#
The name of the structure which will hold the Python-compatible class definition.
The name of the structure which will hold the Python-compatible class definition.
- property type_name#
The name of the Python-compatible class definition instance.
The name of the instance of the Python-compatible class definition structure. This object is necessary to add the class to the module.
- property type_object#
The Python-compatible class definition instance.
The Variable describing the instance of the Python-compatible class definition structure. This object is necessary to add the class to the module.
- class pyccel.ast.cwrapper.PyFunctionDef(*args, original_function, **kwargs)[source]#
Bases:
FunctionDef
Class to hold a FunctionDef which is accessible from Python.
Contains the Python-compatible version of the function which is used for the wrapper. As compared to a normal FunctionDef, this version contains arguments for the shape of arrays. It should be generated by calling codegen.wrapper.CToPythonWrapper.wrap.
- Parameters:
*args (list) – See FunctionDef.
original_function (FunctionDef) – The function from which the Python-compatible version was created.
**kwargs (dict) – See FunctionDef.
See also
pyccel.ast.core.FunctionDef
The class from which BindCFunctionDef inherits which contains all details about the args and kwargs.
- property original_function#
The function which is wrapped by this PyFunctionDef.
The original function which would be printed in pure C which is not compatible with Python.
- class pyccel.ast.cwrapper.PyGetSetDefElement(python_name, getter, setter, docstring)[source]#
Bases:
PyccelAstNode
A class representing a PyGetSetDef object.
A class representing an element of the list of PyGetSetDef objects which are used to add attributes/properties to classes. See https://docs.python.org/3/c-api/structures.html#c.PyGetSetDef .
- Parameters:
python_name (str) – The name of the attribute/property in the original Python code.
getter (FunctionDef) – The function which collects the value of the class attribute.
setter (FunctionDef) – The function which modifies the value of the class attribute.
docstring (LiteralString) – The docstring of the property.
- property docstring#
The docstring of the property being wrapped.
The docstring of the property being wrapped.
- property getter#
The BindCFunctionDef describing the getter function.
The BindCFunctionDef describing the function which allows the user to collect the value of the property.
- property python_name#
The name of the attribute/property in the original Python code.
The name of the attribute/property in the original Python code.
- property setter#
The BindCFunctionDef describing the setter function.
The BindCFunctionDef describing the function which allows the user to modify the value of the property.
- class pyccel.ast.cwrapper.PyInterface(name, functions, interface_func, type_check_func, original_interface, **kwargs)[source]#
Bases:
Interface
Class to hold an Interface which is accessible from Python.
A class which holds the Python-compatible Interface. It contains functions for determining the type of the arguments passed to the Interface and the functions called through the interface.
- Parameters:
name (str) – The name of the interface. See Interface.
functions (iterable of FunctionDef) – The functions of the interface. See Interface.
interface_func (FunctionDef) – The function which Python will call to access the interface.
type_check_func (FunctionDef) – The helper function which will determine the types of the arguments passed.
original_interface (Interface) – The interface being wrapped.
**kwargs (dict) – See Interface.
See also
Interface
The super class.
- property interface_func#
The function which is exposed to Python.
The function which receives the Python arguments self, args, and kwargs and calls the appropriate function.
- property original_function#
The Interface which is wrapped by this PyInterface.
The original interface which would be printed in C.
- property type_check_func#
The function which determines the types which were passed to the Interface.
The function which takes the arguments passed to the function and returns an integer indicating which function was called.
- class pyccel.ast.cwrapper.PyList_Clear(list_obj)[source]#
Bases:
TypedAstNode
A class representing a call to list.clear() in the wrapper.
A class representing a call to list.clear() in the wrapper. There is no simple method to describe this operation before Python 3.13.
- Parameters:
list_obj (TypedAstNode) – The list that must be emptied.
- property list_obj#
The list that must be emptied.
The list that must be emptied.
- class pyccel.ast.cwrapper.PyModInitFunc(name, body, static_vars, scope)[source]#
Bases:
FunctionDef
A class representing the PyModInitFunc function def.
A class representing the PyModInitFunc function def. This function returns the macro PyModInitFunc, takes no arguments and initialises a module.
- Parameters:
name (str) – The name of the function.
body (list[PyccelAstNode]) – The code executed in the function.
static_vars (list[Variable]) – A list of variables which should be declared as static objects.
scope (Scope) – The scope of the function.
- property declarations#
Returns the declarations of the variables.
Returns the declarations of the variables.
- class pyccel.ast.cwrapper.PyModule(name, *args, external_funcs=(), declarations=(), init_func=None, import_func=None, **kwargs)[source]#
Bases:
Module
Class to hold a module which is accessible from Python.
Class to hold a module which is accessible from Python. This class adds external functions and external declarations to the basic Module. However its main utility is in order to differentiate itself such that a different _print function can be implemented to handle it.
- Parameters:
name (str) – Name of the module.
*args (tuple) – See Module.
external_funcs (iterable of FunctionDef) – A list of external functions.
declarations (iterable) – Any declarations of (external) variables which should be made in the module.
init_func (FunctionDef, optional) – The function which is executed when a module is initialised. See: https://docs.python.org/3/c-api/module.html#multi-phase-initialization .
import_func (FunctionDef, optional) – The function which allows types from this module to be imported in other modules. See: https://docs.python.org/3/extending/extending.html .
**kwargs (dict) – See Module.
See also
Module
The super class from which the class inherits.
- property declarations#
All declarations that need printing in the module.
All declarations that need printing in the module. This usually includes any variables coming from a non-C language for which compatibility with C exists.
- property external_funcs#
A list of external functions.
The external functions which should be declared at the start of the module. This is useful for declaring the existence of Fortran functions whose definition and declaration is inaccessible from C.
- property import_func#
The function which allows types from this module to be imported in other modules.
The function which allows types from this module to be imported in other modules. See https://docs.python.org/3/extending/extending.html to understand how this is done.
- class pyccel.ast.cwrapper.PyModule_AddObject(mod_name, name, variable)[source]#
Bases:
PyccelFunction
Represents a call to the PyModule_AddObject function.
The PyModule_AddObject function can be found in Python.h. It adds a PythonObject to a module. More information about this function can be found in Python’s documentation.
- Parameters:
mod_name (str) – The name of the variable containing the module.
name (str) – The name of the variable being added to the module.
variable (Variable) – The variable containing the PythonObject.
- property mod_name#
The name of the variable containing the module
- property name#
The name of the variable being added to the module
- property variable#
The variable containing the PythonObject
- class pyccel.ast.cwrapper.PyModule_Create(module_def_name)[source]#
Bases:
PyccelFunction
Represents a call to the PyModule_Create function.
The PyModule_Create function can be found in Python.h. It acts as a constructor for a module. More information about this function can be found in Python’s documentation. See https://docs.python.org/3/c-api/module.html#c.PyModule_Create .
- Parameters:
module_def_name (str) – The name of the structure which defined the module.
- property module_def_name#
Get the name of the structure which defined the module.
Get the name of the structure which defined the module.
- class pyccel.ast.cwrapper.PyTuple_Pack(*args)[source]#
Bases:
PyccelFunction
A class representing a call to Python’s PyTuple_Pack function.
A class representing a call to Python’s PyTuple_Pack function. A class is used instead of a FunctionDef as the number of arguments is variable. A PyTuple_Pack is described here: https://docs.python.org/3/c-api/tuple.html#c.PyTuple_Pack
- Parameters:
*args (PyccelAstNode) – The arguments that should be packed into the tuple.
- class pyccel.ast.cwrapper.Py_ssize_t(self)[source]#
Bases:
FixedSizeType
Class representing Python’s Py_ssize_t type.
Class representing Python’s Py_ssize_t type.
- class pyccel.ast.cwrapper.Py_ssize_t_Cast(arg)[source]#
Bases:
PythonInt
A class for casting integers to Python’s Py_ssize_t type.
A class for casting integers to Python’s Py_ssize_t type.
- Parameters:
arg (TypedAstNode) – The argument passed to the function.
- name = 'Py_ssize_t'#
- class pyccel.ast.cwrapper.PyccelPyClassType(self)[source]#
Bases:
FixedSizeType
Datatype representing a subclass of PyObject.
Datatype representing a subclass of PyObject. This is the datatype of a class which is compatible with Python.
- class pyccel.ast.cwrapper.PyccelPyObject(self)[source]#
Bases:
FixedSizeType
Datatype representing a PyObject.
Datatype representing a PyObject which is the class used to hold Python objects in Python.h.
- class pyccel.ast.cwrapper.PyccelPyTypeObject(self)[source]#
Bases:
FixedSizeType
Datatype representing a PyTypeObject.
Datatype representing a PyTypeObject which is the class used to hold Python class objects in Python.h.
- class pyccel.ast.cwrapper.WrapperCustomDataType(self)[source]#
Bases:
CustomDataType
Datatype representing a subclass of PyObject.
Datatype representing a subclass of PyObject. This is the datatype of a class which is compatible with Python.