Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/MANUAL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ Constructed types are either tuples, introduced by parenthesis, like ``(int,
| argument_type [:,...,3]+ # a ndarray, some dimension fixed
| argument_type:argument_type dict # a dictionary
| argument_type type # a type
| package_name pkg # a package name

basic_type = bool | byte | int | float | str | None | slice
| uint8 | uint16 | uint32 | uint64 | uintp
Expand Down Expand Up @@ -306,6 +307,31 @@ parameter, but also types. It can be useful to write generic functions such as

Container type, as in ``set type`` don't hold any information about their subtype.

Concerning the ``pkg`` specification
************************************

When exporting a function that takes a ``pkg`` argument, as in

.. code-block:: python

#pythran export compute(float[], numpy pkg)
def compute(a, xp):
return xp.cos(a)

Pythran actually transforms your code so that any package passed as ``pkg``
argument is named according to the specification (in our example, the name of
the package has to be ``numpy``).

There currently are several restrictions though:

1. The same function cannot be exported with different package names (*a.k.a.* no
overloads based on the package name). This is valid for exported and non
exported functions.

2. The package argument can only be used as an attribute root, or as a
non-keyword argument to other user function.


When Pythran needs a little help: type annotation
*************************************************

Expand Down
1 change: 1 addition & 0 deletions pythran/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
'openmp',
'optimizations',
'passmanager',
'preprocessor',
'spec',
'syntax',
'tables',
Expand Down
5 changes: 4 additions & 1 deletion pythran/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,10 @@ def visit_Name(self, node):
if node.id in self.local_names:
return cxxid(node.id)
elif node.id in self.global_declarations:
return "{0}()".format(cxxid(node.id))
if isinstance(self.types[node], self.types.builder.PkgRefType):
return f"{self.types[node].generate(None)}{{}}"
else:
return "{0}()".format(cxxid(node.id))
else:
return cxxid(node.id)

Expand Down
15 changes: 15 additions & 0 deletions pythran/cxxtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,21 @@ def generate(self, ctx):
ty = ctx(self.of)
return f"std::integral_constant<{ty}, {str(self.index).lower()}>"

class PkgType(Type):
"""
A type to hold package name
"""
def __init__(self, name):
super(PkgType, self).__init__(name=name)

def generate(self, _):
return f'pythonic::types::pkg::{self.name}'

class PkgRefType(PkgType):
"""
A type to hold reference to package name
"""

class ArgumentType(Type):
"""
A type to hold function arguments
Expand Down
4 changes: 2 additions & 2 deletions pythran/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"""

from pythran.openmp import GatherOMPData
from pythran.syntax import check_syntax
from pythran.syntax import check_syntax, PythranSyntaxError
from pythran.tables import pythran_ward
from pythran.transformations import ExtractDocStrings, HandleImport

import gast as ast
Expand All @@ -16,7 +17,6 @@ def raw_parse(code):

return ast.parse(code)


def parse(pm, code):

# front end
Expand Down
Loading
Loading