Read about our upcoming Code of Conduct on this issue

Commit 39db1f56 authored by Pierre Augier's avatar Pierre Augier
Browse files

New decorator include

parent 7e30956fd2e9
......@@ -8,7 +8,7 @@ except ImportError:
from .annotation import Array, NDim, Type, Shape
from .aheadoftime import FluidPythran, pythran_def, boost, make_signature
from .aheadoftime import FluidPythran, pythran_def, boost, make_signature, include
from .util import set_pythranize_at_import
from .pythranizer import wait_for_all_extensions
......@@ -18,6 +18,7 @@ __all__ = [
"FluidPythran",
"pythran_def",
"boost",
"include",
"make_signature",
"path_data_tests",
"Array",
......
......@@ -39,7 +39,6 @@ from .util import (
has_to_build,
modification_date,
is_method,
path_root,
path_cachedjit_classes,
)
......@@ -231,6 +230,7 @@ class FluidPythran:
self.functions = {}
self.classes = {}
self.signatures_func = {}
self.included_functions = []
modules[module_name] = self
self.is_transpiled = False
return
......@@ -549,6 +549,9 @@ class FluidPythran:
self.signatures_func[func.__name__] = []
self.signatures_func[func.__name__].extend(signatures)
def include(self, func):
self.included_functions.append(func)
class FluidPythranTemporaryMethod:
def __init__(self, func):
......@@ -561,7 +564,54 @@ class FluidPythranTemporaryMethod:
)
# FIXME: @include and @include(used_by=("func0", "func1")
def include(func=None, used_by=None):
"""Decorator to record that the function has to be included in a Pythran file
"""
if func is not None and used_by is None:
if not callable(func):
func, used_by = used_by, func
decor = Include(used_by=used_by)
if callable(func):
decor._decorator_no_arg = True
return decor(func)
else:
return decor
class Include:
"""Decorator used internally by the public include decorator
@include
@include(used_by=("func0", "func1")
"""
def __init__(self, used_by=None):
self.used_by = used_by
self._decorator_no_arg = False
def __call__(self, func):
if self._decorator_no_arg:
index_frame = 3
else:
index_frame = 2
if self.used_by is not None:
from .justintime import _get_module_cachedjit
jit_mod = _get_module_cachedjit(index_frame)
jit_mod.record_used_function(func, self.used_by)
if not is_transpiling:
return func
fp = _get_fluidpythran_calling_module(index_frame)
fp.include(func)
return func
class FluidPythranTemporaryJITMethod:
......
......@@ -2,7 +2,7 @@ import numpy as np
# pythran import numpy as np
from fluidpythran import FluidPythran, boost
from fluidpythran import FluidPythran, boost, include
# pythran def func(int, float)
......@@ -13,12 +13,14 @@ def func(a, b):
return a + b
# pythran def func2(int, float)
@include
def func_tmp(arg):
return arg ** 2
@boost
def func2(a, b):
return a - b
def func2(a: int, b: float):
return a - func_tmp(b)
fp = FluidPythran()
......
......@@ -2,16 +2,16 @@ import numpy as np
# pythran import numpy as np
from fluidpythran import cachedjit, used_by_cachedjit, boost
from fluidpythran import cachedjit, include, boost
from .mpi import Path
@used_by_cachedjit("func1")
@include(used_by="func1")
def func0(a):
return 2 * a
@used_by_cachedjit("func1")
@include(used_by="func1")
def func():
return 1
......
......@@ -60,6 +60,7 @@ import sys
import time
from distutils.util import strtobool
from functools import wraps
from warnings import warn
try:
import numpy as np
......@@ -181,6 +182,10 @@ class used_by_cachedjit:
self.names = names
def __call__(self, func):
warn(
"used_by_cachedjit is deprecated, use include instead",
DeprecationWarning,
)
mod = _get_module_cachedjit()
mod.record_used_function(func, self.names)
return func
......
......@@ -389,9 +389,7 @@ def produce_code_class_func(cls, func_name, jit=False):
)
if jit:
new_code = (
"from fluidpythran import cachedjit\n\n@cachedjit\n" + new_code
)
new_code = "from fluidpythran import cachedjit\n\n@cachedjit\n" + new_code
python_code = pythran_signatures + "\n" + new_code
......@@ -499,6 +497,9 @@ imports: {imports}\n"""
for cls in fp.classes.values():
code_pythran += produce_code_class(cls)
for func in fp.included_functions:
code_pythran += "\n" + get_source_without_decorator(func) + "\n"
else:
if "# FLUIDPYTHRAN_NO_IMPORT" not in code:
logger.warning(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment