Read about our upcoming Code of Conduct on this issue

This instance will be upgraded to Heptapod 0.28.1 on 2022-01-26 at 16:00 UTC+1 (a few minutes of down time)

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

Deprecate mocked_modules argument + documentation

parent 6b2445d7461b
......@@ -360,7 +360,7 @@ setup.py like this:
here = Path(__file__).parent.absolute()
paths = ["fluidsim/base/time_stepping/pseudo_spect.py"]
make_backend_files([here / path for path in paths], mocked_modules=["h5py"])
make_backend_files([here / path for path in paths])
Note that the function :code:`make_backend_files` does not use compile the file
produced. The compilation has to be done after the call of this function (see
......
......@@ -34,6 +34,8 @@ modules, classes and functions.
.. autosummary::
:toctree: generated/
transonic.analyses
transonic.backends
transonic.aheadoftime
transonic.transpiler
transonic.annotation
......
"""Code analyses
================
.. autosummary::
:toctree:
blocks_if
capturex
extast
justintime
parser
util
"""
from pprint import pformat
import gast as ast
......@@ -20,6 +35,7 @@ __all__ = ["print_dumped", "print_unparsed"]
def compute_ancestors_chains(module_node):
"""Create Beniget objects"""
ancestors = beniget.Ancestors()
ancestors.visit(module_node)
......@@ -33,6 +49,7 @@ def compute_ancestors_chains(module_node):
def get_boosted_dicts(module, ancestors, duc, decorator="boost"):
"""Get the definitions of the decorated functions and classes"""
kinds = ("functions", "methods", "classes")
boosted_dicts = {kind: {} for kind in kinds}
......@@ -83,7 +100,7 @@ def get_boosted_dicts(module, ancestors, duc, decorator="boost"):
def analyse_aot(code):
"""Gather the informations for ``@boost`` and blocks"""
debug = logger.debug
debug("extast.parse")
......
"""Analyses for "if blocks"
===========================
"""
import gast as ast
from transonic.analyses.util import gather_rawcode_comments
class BlockDefinition:
"""Represent a block definition"""
def __init__(self, **kwargs):
self.kwargs = kwargs
self.__dict__.update(**kwargs)
......@@ -16,7 +23,7 @@ class BlockDefinition:
def get_block_definitions(code, module, ancestors, duc, udc):
"""Get all "if" block definitions"""
blocks = []
node_transonic_obj = None
for node in module.body:
......@@ -91,6 +98,7 @@ def get_block_definitions(code, module, ancestors, duc, udc):
def find_index_closing_parenthesis(string: str):
"""Find the index of the closing parenthesis"""
assert string.startswith("("), "string has to start with '('"
stack = []
for index, letter in enumerate(string):
......@@ -105,6 +113,7 @@ def find_index_closing_parenthesis(string: str):
def get_signatures_from_comments(comments, namespace=None):
"""Get the blocks signatures for a block"""
if namespace is None:
namespace = {}
......
"""Capture the external nodes used in functions
===============================================
"""
import gast as ast
import beniget
......@@ -5,6 +9,8 @@ from transonic.analyses import extast
class CaptureX(ast.NodeVisitor):
"""Capture the external nodes used in functions, classes and blocks"""
def __init__(
self,
functions,
......
......@@ -12,6 +12,8 @@ import beniget
class CommentLine(ast.AST):
""""New AST node representing a comment line"""
_fields = ("s",)
def __init__(self, s, lineno=None):
......@@ -22,6 +24,8 @@ class CommentLine(ast.AST):
class UnparserExtended(astunparse.Unparser):
"""Unparser for extented AST"""
def __init__(self, tree, file, with_comments=True):
self.with_comments = with_comments
super().__init__(tree, file=file)
......@@ -32,18 +36,22 @@ class UnparserExtended(astunparse.Unparser):
def parse(code, *args, **kwargs):
"""Parse a code and produce the extended AST"""
tree = ast.parse(code, *args, **kwargs)
CommentInserter(tree, code)
return tree
def unparse(tree, with_comments=True):
"""Unparse the extended AST"""
v = StringIO()
UnparserExtended(tree, file=v, with_comments=with_comments)
return v.getvalue()
class CommentInserter(ast.NodeVisitor):
"""Insert the CommentLine nodes in an AST tree"""
def __init__(self, tree, code):
self.tree = tree
......
"""Analyses for ``@jit``
========================
"""
from transonic.log import logger
from transonic.analyses import extast
......@@ -6,6 +11,7 @@ from transonic.analyses.capturex import CaptureX
def analysis_jit(code):
"""Gather the informations for ``@jit`` with an ast analysis"""
debug = logger.debug
......
"""Parser using tokenize
========================
"""
from tokenize import tokenize, COMMENT
from io import BytesIO
......
"""Utilities for the analyses
=============================
"""
from textwrap import dedent
import gast as ast
......@@ -6,6 +11,7 @@ import astunparse
def print_dumped(source):
"""Pretty print the AST"""
if isinstance(source, str):
module = extast.parse(source)
if len(module.body) == 1:
......@@ -18,6 +24,7 @@ def print_dumped(source):
def print_unparsed(node):
"""Print the code corresponding to a tree or a node"""
print(extast.unparse(node))
......@@ -49,6 +56,7 @@ def _fill_ast_annotations_class(class_def, ast_annotations):
def get_annotations(object_def, namespace):
"""Create the annotations from a definition node"""
# print_dump(object_def)
......@@ -77,6 +85,7 @@ def get_annotations(object_def, namespace):
def filter_code_typevars(module, duc, ancestors):
"""Create a filtered code with what is needed to create the annotations"""
module_filtered = ast.Module()
kept = module_filtered.body = []
......@@ -110,6 +119,8 @@ def filter_code_typevars(module, duc, ancestors):
class AnalyseLines(ast.NodeVisitor):
"""Analyse to find the last line of a node"""
def __init__(self, main_node):
if isinstance(main_node, ast.Module):
self.line_start = 1
......@@ -151,6 +162,7 @@ class AnalyseLines(ast.NodeVisitor):
def gather_rawcode_comments(node, code_module):
"""Get the comments in a node"""
analysis = AnalyseLines(node)
rawcode = dedent(analysis.get_code(code_module))
comments = dedent(
......
"""Backends for different accelerators
======================================
.. autosummary::
:toctree:
pythran
"""
"""Pythran Backend
==================
"""
from tokenize import tokenize, untokenize, NAME, OP
from io import BytesIO
from token import tok_name
......
......@@ -38,10 +38,7 @@ def test_detect_pythran_extensions():
"mixed_classic_type_hint.py",
]
make_backend_files(
(path_data_tests / name for name in names),
mocked_modules=("toto.titi", "numpy"),
)
make_backend_files((path_data_tests / name for name in names))
ext_names = detect_pythran_extensions(path_data_tests)
assert len(ext_names) == len(names) - 1
......
......@@ -63,6 +63,7 @@ def test_jit():
def fib():
from .for_test_justintime import fib, use_fib
fib2 = fib(2)
result = use_fib()
wait_for_all_extensions()
......
......@@ -27,7 +27,7 @@ from tokenize import (
from token import tok_name
import inspect
from io import BytesIO
import sys
from warnings import warn
from unittest.mock import MagicMock as Mock
from contextlib import contextmanager
from pathlib import Path
......@@ -223,12 +223,7 @@ def produce_code_class_func(cls, func_name, jit=False):
return python_code
def make_pythran_file(
path_py: Path,
force=False,
log_level=None,
mocked_modules: Optional[Iterable] = None,
):
def make_pythran_file(path_py: Path, force=False, log_level=None):
"""Create a Python file from a Python file (if necessary)"""
if log_level is not None:
logger.set_level(log_level)
......@@ -289,14 +284,19 @@ def make_backend_files(
assert backend is None
if mocked_modules is not None:
warn(
"The argument mocked_modules is deprecated. "
"It is now useless for Transonic.",
DeprecationWarning,
)
if log_level is not None:
logger.set_level(log_level)
paths_out = []
for path in paths:
path_out = make_pythran_file(
path, force=force, mocked_modules=mocked_modules
)
path_out = make_pythran_file(path, force=force)
if path_out:
paths_out.append(path_out)
......
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