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

Get code of functions without importing the module

parent 38270890194f
......@@ -6,7 +6,7 @@ black:
black -l 82 fluidpythran
tests:
pytest -s
pytest fluidpythran
tests_coverage:
mkdir -p .coverage
......
from fluidpythran import FluidPythran
fp = FluidPythran()
class MyClass:
def _time_step_RK2_fluidpythran(self):
dt = self.deltat
diss, diss2 = self.exact_linear_coefs.get_updated_coefs()
compute_tendencies = self.sim.tendencies_nonlin
state_spect = self.sim.state.state_spect
tendencies_n = compute_tendencies()
state_spect_n12 = self._state_spect_tmp
if fp.is_pythranized:
fp.use_pythranized_block("rk2_step0")
else:
# pythran block (
# complex128[][][] state_spect_n12, state_spect, tendencies_n;
# float64[][] diss2;
# float dt
# )
state_spect_n12[:] = (state_spect + dt / 2 * tendencies_n) * diss2
print("hello!")
\ No newline at end of file
__version__ = "0.0.1"
__version__ = "0.0.2"
try:
from pyfiglet import figlet_format
__about__ = figlet_format("fluidpythran v" + __version__, font="big")
__about__ = (
figlet_format("fluidpythran", font="big") + 51 * " " + "v. " + __version__
)
except ImportError:
__about__ = r"""
......
......@@ -6,8 +6,6 @@ from logging import DEBUG
from token import tok_name
from io import BytesIO
import inspect
from runpy import run_path
from pathlib import Path
......@@ -90,11 +88,16 @@ def parse_py(path):
signatures_blocks[name_block].append(signature)
if has_to_find_code_block == "in block":
code_blocks[name_block].append((toknum, tokval))
if toknum == DEDENT:
logger.debug(
f"code_blocks[name_block]: {code_blocks[name_block]}"
)
code_blocks[name_block] = untokenize(code_blocks[name_block])
has_to_find_code_block = False
else:
code_blocks[name_block].append((toknum, tokval))
if (
has_to_find_code_block == "after use_pythranized_block"
......@@ -115,6 +118,46 @@ def parse_py(path):
)
def get_code_functions(path_py, func_names):
"""Get the code of function from a path and function names"""
with open(path_py) as file:
code = file.read()
indent = 0
in_def = False
codes = {}
g = tokenize(BytesIO(code.encode("utf-8")).readline)
for toknum, tokval, a, b, c in g:
if toknum == INDENT:
indent += 1
if toknum == DEDENT:
indent -= 1
if in_def == "def" and tokval in func_names:
in_def = tokval
codes[in_def] = [(NAME, "def")]
if indent == 0 and toknum == NAME and tokval == "def":
in_def = "def"
if in_def and in_def != "def":
if indent == 0 and toknum == DEDENT:
print("codes[in_def]", codes[in_def])
codes[in_def] = untokenize(codes[in_def])
in_def = False
else:
codes[in_def].append((toknum, tokval))
logger.debug((indent, tok_name[toknum], tokval))
return codes
def create_pythran_code(path_py):
"""Create a pythran code from a Python file"""
......@@ -135,26 +178,14 @@ imports: {imports}\n"""
code_pythran = "\n" + "\n".join(imports) + "\n"
# it is not optimal. We should be able to get the code of the function
# without running the module
os.environ["FLUIDPYTHRAN_COMPILING"] = "1"
mod = run_path(str(path_py))
try:
del os.environ["FLUIDPYTHRAN_COMPILING"]
except KeyError:
pass
codes_functions = get_code_functions(path_py, functions)
for name_func in functions:
signatures = signatures_func[name_func]
for signature in signatures:
code_pythran += f"# pythran export {signature}\n"
func = mod[name_func]
code = inspect.getsource(func)
code = "\n".join(
line for line in code.split("\n") if ".pythranize" not in line
)
code = codes_functions[name_func]
code_pythran += f"\n{code}\n\n"
......
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