Commit dcc68cef authored by Pierre Augier's avatar Pierre Augier
Browse files

Use a decorator for functions

parent ccc03e767733
from fluidpythran import create_pythran_file
path = "example_class.py"
path = "example_classic.py"
create_pythran_file(path)
\ No newline at end of file
......@@ -12,14 +12,11 @@ fp = FluidPythran()
# pythran def func(float[][], float[][])
# pythran def func(int[][], float[][])
@fp.pythranize
def func(a, b):
return (a * np.log(b)).max()
func = fp.monkey_patch_function(func)
if __name__ == "__main__":
n0, n1 = 100, 200
......
import inspect
import importlib
import os
from ._version import __version__
......@@ -17,7 +18,7 @@ __all__ = [
class FluidPythran:
def __init__(self, use_pythran=True):
if not use_pythran:
if not use_pythran or "FLUIDPYTHRAN_COMPILING" in os.environ:
self.is_pythranized = False
return
......@@ -35,8 +36,8 @@ class FluidPythran:
self.module_pythran, "arguments_blocks"
)
def monkey_patch_function(self, func):
def pythranize(self, func):
"""Decorator used for functions"""
if self.is_pythranized:
return getattr(self.module_pythran, func.__name__)
else:
......
from tokenize import tokenize, untokenize, COMMENT, INDENT, DEDENT, STRING, NAME
from token import tok_name
from io import BytesIO
import os
# from token import tok_name
from io import BytesIO
import inspect
from runpy import run_path
from pathlib import Path
import black
......@@ -119,7 +120,10 @@ def create_pythran_code(path_py):
code_pythran = "\n" + "\n".join(imports) + "\n"
os.environ["FLUIDPYTHRAN_COMPILING"] = "1"
mod = run_path(path_py)
del os.environ["FLUIDPYTHRAN_COMPILING"]
for name_func in functions:
signatures = signatures_func[name_func]
for signature in signatures:
......@@ -128,6 +132,10 @@ def create_pythran_code(path_py):
func = mod[name_func]
code = inspect.getsource(func)
code = "\n".join(
line for line in code.split("\n") if ".pythranize" not in line
)
code_pythran += f"\n{code}\n\n"
variables_types_block = {}
......
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