├── .gitignore ├── .travis.yml ├── CHANGELOG.rst ├── LICENSE ├── README.rst ├── _config.yml ├── reflectivipy ├── __init__.py ├── core.py ├── reflectivity.py ├── reifications.py └── wrappers │ ├── __init__.py │ ├── assign_flatwrapper.py │ ├── call_flatwrapper.py │ ├── cflow_flatwrapper.py │ ├── compare_flatwrapper.py │ ├── expr_flatwrapper.py │ ├── flatwrapper.py │ ├── literal_flatwrapper.py │ ├── method_flatwrapper.py │ └── return_flatwrapper.py ├── setup.py ├── tests ├── ReflectivityExample.py ├── __init__.py ├── test_MetaLink.py ├── test_RFFlatWrapper.py ├── test_ReflectiveMethod.py ├── test_cflow_for.py ├── test_cflow_if.py ├── test_cflow_while.py ├── test_flat_wrap_method.py ├── test_flat_wrap_node.py ├── test_node_wrapping.py └── test_reifications.py └── tox.ini /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | .pypirc 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *,cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # IPython Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # dotenv 80 | .env 81 | 82 | # virtualenv 83 | venv/ 84 | ENV/ 85 | 86 | # Spyder project settings 87 | .spyderproject 88 | 89 | # Rope project settings 90 | .ropeproject 91 | 92 | # Emacs tmp files 93 | *~ 94 | 95 | # Experiments 96 | experiment*.* 97 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | dist: xenial 3 | language: python 4 | python: 5 | - 'pypy3' 6 | - '3.5' 7 | - '3.6' 8 | - '3.7' 9 | 10 | install: 11 | - pip install -U pip setuptools 12 | - pip install tox-travis 13 | script: tox 14 | deploy: 15 | provider: pypi 16 | user: $PYPI_USER 17 | password: $PYPI_PASSWORD 18 | distributions: sdist bdist_wheel 19 | on: 20 | tags: true 21 | python: 3.7 22 | -------------------------------------------------------------------------------- /CHANGELOG.rst: -------------------------------------------------------------------------------- 1 | Changelog 2 | --------- 3 | 4 | 0.1.0 5 | +++++ 6 | 7 | **Features** 8 | 9 | - Add support for Python 3. 10 | 11 | 12 | 0.0.2 13 | +++++ 14 | 15 | **Features** 16 | 17 | - Add support for ``instead`` control for metalinks. This new control allows the 18 | user to remove a node and to place a meta-behavior instead. The new behavior 19 | can be uninstall using the ``uninstall()`` method on the link. 20 | 21 | - Add support for decorators. This new implementation search for the function at the 22 | most "lower" level for methods and gets the function pointed by the decorators. 23 | 24 | - Add support for modules. This new implementation is able to install metalinks 25 | on function at a module level. 26 | 27 | 28 | 0.0.1 29 | +++++ 30 | 31 | **Initial version** 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Steven Costiou 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | ===================================================================================== 2 | Reflectivipy: A Python Implementation of the Reflectivity API from the Pharo language 3 | ===================================================================================== 4 | |Downloads| 5 | 6 | .. |Downloads| image:: https://pepy.tech/badge/reflectivipy 7 | 8 | Reflectivipy is an API inspired by `Reflectivity in Pharo 9 | `_. Reflectivity allows you to deal 10 | with partial behavioral reflection in Python by letting you install ``MetaLink`` 11 | directly on method AST nodes. Moreover, Reflectivity provides object-centric 12 | capabilities and let you install a modified behavior on a dedicated object. 13 | 14 | Let see how to install a link on a method AST towards a meta-object: 15 | 16 | .. code-block:: python 17 | 18 | import reflectivipy 19 | 20 | 21 | # We define a new meta-object that will act as a logger 22 | # each time a dedicated AST node will be "visited/executed" 23 | class MetaLogger(object): 24 | def log_me(self): 25 | print("I'm here") 26 | 27 | 28 | # Here is the class we will instrument 29 | class ExampleClass(object): 30 | def foo(self): 31 | print('Executing foo') 32 | 33 | 34 | # We create a link ('control' is 'before' by default) 35 | link = reflectivipy.MetaLink(MetaLogger(), selector='log_me', control='before') 36 | 37 | # We get the method AST we want to instrument 38 | rf_ast = reflectivipy.reflective_ast_for_method(ExampleClass, 'foo') 39 | 40 | # We select the node that we want to install the link on 41 | # Here we selected the "print 'Executing foo'" AST node. 42 | node = rf_ast.body[0].body[0] 43 | 44 | # We install the link on the node 45 | reflectivipy.link(link, node) 46 | 47 | a = ExampleClass() 48 | a.foo() 49 | 50 | # When we don't need it anymore, we remove it 51 | print('Uninstall Metalink') 52 | link.uninstall() 53 | 54 | a.foo() 55 | 56 | # Produces: 57 | # 58 | # I'm here 59 | # Executing foo 60 | # Uninstall Metalink 61 | # Executing foo 62 | 63 | 64 | This small code example uses the two main Reflectivipy concepts: 65 | 66 | - the meta-object definition, i.e: the object that will own the behavior to add 67 | - the ``MetaLink`` in itself which link the meta-object to the AST node that 68 | must be modified. 69 | 70 | The MetaLink ``link`` is used to install a new behavior ``before`` the code 71 | associated to the AST node on which it will be installed. The method AST is 72 | then gathered using the ``reflective_ast_for_method`` function. The desired AST 73 | node is gathered (here it's the ``print`` node). Finally, the node and the 74 | meta-behavior are linked together using the ``link`` function. Once the new 75 | meta-behavior is not required anymore, the ``uninstall`` method of the created 76 | link is called. This call uninstall the link from every node it could be 77 | installed on. 78 | 79 | On top of that, meta-behavior can be installed for a dedicated instance instead 80 | of a class. To do that, it's just a matter of asking for the 81 | ``reflective_ast_for_method`` of the instance instead of the one from the class. 82 | The code remains then exactly the same. 83 | 84 | 85 | Installation 86 | ============ 87 | 88 | Currently, Reflectivity is now on ``pypi``, so you can install it using 89 | ``pip``. It is recommanded to install it in the virtualenv. 90 | 91 | .. code-block:: bash 92 | 93 | $ pip install reflectivipy 94 | 95 | 96 | Quick Start 97 | =========== 98 | 99 | 100 | Contributors 101 | ============ 102 | 103 | * Steven Costiou (`@StevenCostiou `_), main author of Reflectivipy 104 | * Vincent Aranega (`@aranega `_) 105 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-minimal -------------------------------------------------------------------------------- /reflectivipy/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | Reflectivity implementation for Python base package 3 | """ 4 | from .reflectivity import ( 5 | link, 6 | reflective_ast_for_method, 7 | reflective_method_for, 8 | uninstall_all, 9 | metalinks, 10 | rf_methods, 11 | ) 12 | from .core import MetaLink, ReflectiveMethod 13 | 14 | __version__ = "0.1.0" 15 | 16 | __all__ = [ 17 | "link", 18 | "reflective_ast_for_method", 19 | "reflective_method_for", 20 | "uninstall_all", 21 | "metalinks", 22 | "rf_methods", 23 | "MetaLink", 24 | "ReflectiveMethod", 25 | ] 26 | -------------------------------------------------------------------------------- /reflectivipy/core.py: -------------------------------------------------------------------------------- 1 | import copy 2 | import ast 3 | import inspect 4 | from inspect import isfunction, ismethod, isclass, ismodule 5 | 6 | 7 | # must adapt for Python 3 in the future 8 | def remove_decorators(func): 9 | closure = func.__closure__ 10 | if closure: 11 | for cell in closure: 12 | contents = cell.cell_contents 13 | if contents is func: 14 | continue 15 | 16 | if isfunction(contents) or ismethod(contents) or isclass(contents): 17 | new_func = remove_decorators(contents) 18 | if new_func: 19 | return new_func 20 | else: 21 | return func 22 | else: 23 | return func 24 | 25 | 26 | class MetaLink(object): 27 | def __init__(self, metaobject, selector, control="before", arguments=None): 28 | self.metaobject = metaobject 29 | self.selector = selector 30 | self.control = control 31 | self.nodes = [] 32 | self.arguments = arguments or [] 33 | self.reified_arguments = [] 34 | self.option_arg_as_array = False 35 | self.literal_value_reifications = [] 36 | 37 | def add_node(self, rf_node): 38 | self.nodes.append(rf_node) 39 | 40 | def remove_node(self, rf_node): 41 | self.nodes.remove(rf_node) 42 | rf_node.links.remove(self) 43 | 44 | def uninstall(self, from_node=None): 45 | nodes = [from_node] if from_node else list(self.nodes) 46 | for node in nodes: 47 | self.remove_node(node) 48 | node.method_node.reflective_method.restore() 49 | 50 | def reset_reified_arguments(self): 51 | self.reified_arguments = [] 52 | 53 | 54 | class ReflectiveMethod(object): 55 | def __init__(self, class_or_object, method, method_name): 56 | self.target_entity = class_or_object 57 | self.method_name = method_name 58 | self.original_method = method 59 | self.original_ast = None 60 | self.reflective_ast = None 61 | self.link_registry = dict() 62 | self.init_reflective_method() 63 | self.literal_value_reifications = dict() 64 | 65 | def init_reflective_method(self): 66 | builder = AstBuilder() 67 | self.original_ast = builder.rf_ast_for_method( 68 | self.target_entity, self.original_method, self.method_name 69 | ) 70 | 71 | self.reflective_ast = copy.deepcopy(self.original_ast) 72 | self.original_ast.reflective_method = self 73 | 74 | def lookup_link(self, metalink_id): 75 | return self.link_registry[metalink_id] 76 | 77 | def add_link(self, metalink): 78 | self.link_registry[id(metalink)] = metalink 79 | 80 | def find_node_of_id_in_link(self, node_id, metalink): 81 | return next(node for node in metalink.nodes if node.rf_id == node_id) 82 | 83 | def add_literal_value_reification(self, value): 84 | self.literal_value_reifications[id(value)] = value 85 | 86 | def get_literal_value_reifications(self, value_id): 87 | return self.literal_value_reifications[value_id] 88 | 89 | def compile_rf_method(self, rf_ast, method_name): 90 | locs = {} 91 | compiled_method = compile(rf_ast, "", "exec") 92 | global_vars = { 93 | "__rf_original_method__": self.original_method, 94 | "__rf_method__": self, 95 | } 96 | global_vars.update(self.original_method.__globals__) 97 | 98 | eval(compiled_method, global_vars, locs) 99 | if not (isclass(self.target_entity) or ismodule(self.target_entity)): 100 | method = locs[method_name].__get__(self.target_entity) 101 | else: 102 | method = locs[method_name] 103 | 104 | setattr(self.target_entity, method_name, method) 105 | 106 | def recompile(self): 107 | self.compile_rf_method(self.reflective_ast, self.method_name) 108 | 109 | def invalidate(self): 110 | self.reflective_ast.body[0].body = self.original_ast.wrapper.flat_wrap() 111 | ast.fix_missing_locations(self.reflective_ast) 112 | self.recompile() 113 | 114 | def restore(self): 115 | self.link_registry = dict() 116 | self.init_reflective_method() 117 | self.recompile() 118 | 119 | 120 | class AstBuilder(object): 121 | def __init__(self): 122 | self.method_node = None 123 | self.current_index = 1 124 | self.flattened_nodes = {} 125 | 126 | def ast_load_list(self, args_list): 127 | return ast.List(elts=args_list, ctx=ast.Load()) 128 | 129 | def assign_named_value(self, name, value): 130 | value_store = self.ast_store(name) 131 | return self.ast_assign(value_store, value) 132 | 133 | def ast_assign(self, store_node, value_node): 134 | return ast.Assign(targets=[store_node], value=value_node) 135 | 136 | def ast_store(self, var_name): 137 | return ast.Name(id=var_name, ctx=ast.Store()) 138 | 139 | def ast_load(self, var_name): 140 | load = ast.Name(id=var_name, ctx=ast.Load()) 141 | load.temp_name = var_name 142 | return load 143 | 144 | def ast_expr(self, node): 145 | return ast.Expr(node) 146 | 147 | @staticmethod 148 | def get_method_source(method): 149 | method = remove_decorators(method) 150 | while "__rf_original_method__" in method.__globals__: 151 | method = method.__globals__["__rf_original_method__"] 152 | lines = inspect.getsourcelines(method) 153 | first_line = lines[0][0] 154 | indent = len(first_line) - len(first_line.lstrip()) 155 | src = "" 156 | for line in lines[0]: 157 | src += line[indent:] 158 | return src 159 | 160 | @classmethod 161 | def ast_for_method(cls, method): 162 | return ast.parse(cls.get_method_source(method)) 163 | 164 | def rf_ast_for_method(self, method_class, method, method_name): 165 | method_node = self.ast_for_method(method) 166 | method_node.method_name = method_name 167 | method_node.method_class = method_class 168 | return self.build_rf_ast(method_node) 169 | 170 | def build_rf_ast(self, node): 171 | self.method_node = node 172 | self.method_node.is_method = True 173 | self.visit_node(self.method_node) 174 | self.visit_clear_twins(node) 175 | self.visit_twins(copy.deepcopy(node)) 176 | return node 177 | 178 | # We clear the twins before copying nodes, 179 | # to avoid recursions during deep copies. 180 | # Twins are set back in visit_twins 181 | def visit_clear_twins(self, node): 182 | for node in ast.iter_child_nodes(node): 183 | original_node = self.flattened_nodes[node.rf_id] 184 | original_node.twin = None 185 | self.visit_twins(node) 186 | 187 | def visit_twins(self, copy_node): 188 | for node in ast.iter_child_nodes(copy_node): 189 | original_node = self.flattened_nodes[node.rf_id] 190 | node.method_node = original_node.method_node 191 | original_node.twin = node 192 | self.visit_twins(node) 193 | 194 | def visit_node(self, node): 195 | self.decorate_node(node) 196 | 197 | for child in ast.iter_child_nodes(node): 198 | child.parent = node 199 | node.children.append(child) 200 | self.visit_node(child) 201 | 202 | return node 203 | 204 | def build_rf_node(self, node): 205 | self.decorate_node(node) 206 | return self.decorate_node(node) 207 | 208 | def decorate_node(self, node): 209 | if node != self.method_node: 210 | node.is_method = False 211 | 212 | node.is_generated = False 213 | 214 | node.rf_id = self.current_index 215 | self.flattened_nodes[node.rf_id] = node 216 | self.current_index += 1 217 | 218 | node_class = node.__class__ 219 | node.can_be_wrapped = True 220 | node.temp_name = "temp_{}_{}".format(node_class.__name__, node.rf_id) 221 | 222 | node.method_node = self.method_node 223 | 224 | node.method_class = self.method_node.method_class 225 | node.links = list() 226 | 227 | from .wrappers import flat_wrappers 228 | 229 | if node_class in flat_wrappers: 230 | node.wrapper = flat_wrappers[node_class](node) 231 | else: 232 | node.wrapper = flat_wrappers["generic"](node) 233 | 234 | node.children = [] 235 | return node 236 | -------------------------------------------------------------------------------- /reflectivipy/reflectivity.py: -------------------------------------------------------------------------------- 1 | from weakref import WeakValueDictionary, WeakSet 2 | from .core import ReflectiveMethod 3 | 4 | rf_methods = WeakValueDictionary() 5 | 6 | metalinks = WeakSet() 7 | nodes_with_links = WeakSet() 8 | 9 | 10 | def reflective_ast_for_method(class_or_object, method_name): 11 | """ 12 | Finds the reflective AST for the given method name, or generates it if it 13 | does not exist 14 | """ 15 | return reflective_method_for(class_or_object, method_name).original_ast 16 | 17 | 18 | def reflective_method_for(class_or_object, method_name): 19 | rf_key = (class_or_object, method_name) 20 | try: 21 | return rf_methods[rf_key] 22 | except KeyError: 23 | method = getattr(class_or_object, method_name) 24 | rf_method = ReflectiveMethod(class_or_object, method, method_name) 25 | rf_methods[rf_key] = rf_method 26 | return rf_method 27 | 28 | 29 | def link(metalink, rf_node): 30 | metalinks.add(metalink) 31 | nodes_with_links.add(rf_node) 32 | 33 | metalink.add_node(rf_node) 34 | rf_node.links.append(metalink) 35 | rf_method = rf_node.method_node.reflective_method 36 | rf_method.add_link(metalink) 37 | rf_method.invalidate() 38 | 39 | 40 | def uninstall_all(): 41 | for metalink in metalinks: 42 | for node in metalink.nodes: 43 | metalink.remove_node(node) 44 | 45 | for node in nodes_with_links: 46 | node.links.clear() 47 | 48 | metalinks.clear() 49 | nodes_with_links.clear() 50 | 51 | for rf_method in rf_methods.values(): 52 | rf_method.restore() 53 | -------------------------------------------------------------------------------- /reflectivipy/reifications.py: -------------------------------------------------------------------------------- 1 | import ast 2 | from .core import AstBuilder 3 | 4 | builder = AstBuilder() 5 | 6 | 7 | class Reification(object): 8 | def visit_node(self, rf_node, metalink): 9 | return self.visit_method(rf_node)(rf_node, metalink) 10 | 11 | def visit_method(self, rf_node): 12 | visit_method = "visit_" + rf_node.__class__.__name__ 13 | return getattr(self, visit_method) 14 | 15 | 16 | class LiteralValueReification(Reification): 17 | def __init__(self, value): 18 | self.value = value 19 | 20 | def visit_node(self, rf_node, metalink): 21 | rf_node.method_node.reflective_method.add_literal_value_reification(self.value) 22 | attr_node = ast.Attribute( 23 | value=rf_method_reification(), 24 | attr="get_literal_value_reifications", 25 | ctx=ast.Load(), 26 | ) 27 | return ast.Call(func=attr_node, args=[ast.Num(id(self.value))], keywords=[]) 28 | 29 | 30 | class LinkReification(Reification): 31 | def visit_node(self, rf_node, metalink): 32 | return link_reification(metalink) 33 | 34 | 35 | class ClassReification(Reification): 36 | def visit_node(self, rf_node, metalink): 37 | return ast.Attribute( 38 | value=node_reification(), attr="method_class", ctx=ast.Load() 39 | ) 40 | 41 | 42 | class ObjectReification(Reification): 43 | def visit_node(self, rf_node, metalink): 44 | return ast.Name(id="self", ctx=ast.Load()) 45 | 46 | 47 | class NodeReification(Reification): 48 | def visit_node(self, rf_node, metalink): 49 | attr_node = ast.Attribute( 50 | value=rf_method_reification(), 51 | attr="find_node_of_id_in_link", 52 | ctx=ast.Load(), 53 | ) 54 | return ast.Call( 55 | func=attr_node, 56 | args=[ast.Num(rf_node.rf_id), link_reification(metalink)], 57 | keywords=[], 58 | ) 59 | 60 | 61 | class MethodReification(Reification): 62 | def visit_node(self, rf_node, metalink): 63 | return original_method_reification() 64 | 65 | 66 | class SenderReification(Reification): 67 | def visit_node(self, rf_node, metalink): 68 | method_node_node = ast.Attribute( 69 | value=node_reification(), attr="method_node", ctx=ast.Load() 70 | ) 71 | return ast.Attribute(value=method_node_node, attr="method_name", ctx=ast.Load()) 72 | 73 | 74 | class ReceiverReification(Reification): 75 | def visit_node(self, rf_node, metalink): 76 | return ast.Name(id=rf_node.func.value.temp_name, ctx=ast.Load()) 77 | 78 | 79 | class SelectorReification(Reification): 80 | def visit_node(self, rf_node, metalink): 81 | return ast.Str(rf_node.func.attr) 82 | 83 | 84 | class ValueReification(Reification): 85 | def visit_Assign(self, assign_node, metalink): 86 | return ast.Name(id=assign_node.targets[0].id, ctx=ast.Load()) 87 | 88 | def visit_Name(self, name, metalink): 89 | return name 90 | 91 | 92 | class OldValueReification(ValueReification): 93 | def visit_Assign(self, assign_node, metalink): 94 | return ast.Name(id=assign_node.targets[0].id, ctx=ast.Load()) 95 | 96 | 97 | class NewValueReification(ValueReification): 98 | def visit_Assign(self, assign_node, metalink): 99 | return ast.Name(id=assign_node.temp_name, ctx=ast.Load()) 100 | 101 | 102 | class NameReification(Reification): 103 | def visit_Assign(self, assign_node, metalink): 104 | return self.visit_Name(assign_node.targets[0], metalink) 105 | 106 | def visit_Name(self, name_node, metalink): 107 | return ast.Str(name_node.id) 108 | 109 | 110 | class ArgumentReification(Reification): 111 | def visit_Module(self, rf_node, metalink): 112 | return self.visit_FunctionDef(rf_node.body[0], metalink) 113 | 114 | def visit_FunctionDef(self, rf_node, metalink): 115 | args = [] 116 | for arg in rf_node.args.args: 117 | if not arg.arg == "self": 118 | args.append(ast.Name(id=arg.arg, ctx=ast.Load())) 119 | 120 | return ast.List(elts=args, ctx=ast.Load()) 121 | 122 | def visit_Call(self, rf_node, metalink): 123 | args = [] 124 | for arg in rf_node.args: 125 | args.append(ast.Name(id=arg.id, ctx=ast.Load())) 126 | 127 | return ast.List(elts=args, ctx=ast.Load()) 128 | 129 | 130 | reifications_dict = { 131 | "class": ClassReification, 132 | "node": NodeReification, 133 | "object": ObjectReification, 134 | "method": MethodReification, 135 | "sender": SenderReification, 136 | "receiver": ReceiverReification, 137 | "selector": SelectorReification, 138 | "name": NameReification, 139 | "value": ValueReification, 140 | "old_value": OldValueReification, 141 | "new_value": NewValueReification, 142 | "arguments": ArgumentReification, 143 | "link": LinkReification, 144 | } 145 | 146 | 147 | def reification_for(key, metalink): 148 | if key in reifications_dict: 149 | return reifications_dict[key]() 150 | return LiteralValueReification(key) 151 | 152 | 153 | def rf_method_reification(): 154 | return builder.ast_load("__rf_method__") 155 | 156 | 157 | def link_reification(link): 158 | link_id_node = ast.Num(id(link)) 159 | 160 | link_attr_node = ast.Attribute( 161 | value=rf_method_reification(), attr="lookup_link", ctx=ast.Load() 162 | ) 163 | return ast.Call(func=link_attr_node, args=[link_id_node], keywords=[]) 164 | 165 | 166 | def node_reification(): 167 | return ast.Attribute( 168 | value=rf_method_reification(), attr="reflective_ast", ctx=ast.Load() 169 | ) 170 | 171 | 172 | def original_method_reification(): 173 | return ast.Attribute( 174 | value=rf_method_reification(), attr="original_method", ctx=ast.Load() 175 | ) 176 | 177 | 178 | class ReificationGenerator(object): 179 | def __init__(self): 180 | self.reification_counter = 0 181 | self.arg_list = [] 182 | 183 | def generate_reifications(self, rf_node): 184 | expressions = [] 185 | 186 | for link in rf_node.links: 187 | link.reset_reified_arguments() 188 | 189 | for arg in link.arguments: 190 | reification = reification_for(arg, link).visit_node(rf_node, link) 191 | rf_name = self.rf_name_for_arg(arg, str(rf_node.rf_id)) 192 | expressions.append(builder.assign_named_value(rf_name, reification)) 193 | self.add_reified_argument_to_link(builder.ast_load(rf_name), link) 194 | 195 | if link.option_arg_as_array: 196 | link.reified_arguments.append(builder.ast_load_list(self.arg_list)) 197 | self.arg_list = [] 198 | 199 | return expressions 200 | 201 | def add_reified_argument_to_link(self, arg_node, metalink): 202 | if metalink.option_arg_as_array: 203 | self.arg_list.append(arg_node) 204 | else: 205 | metalink.reified_arguments.append(arg_node) 206 | 207 | def rf_name_for_arg(self, arg, rf_id): 208 | if isinstance(arg, str): 209 | return arg + "_" + rf_id 210 | 211 | self.reification_counter += 1 212 | return "value_{}_{}".format(rf_id, self.reification_counter) 213 | -------------------------------------------------------------------------------- /reflectivipy/wrappers/__init__.py: -------------------------------------------------------------------------------- 1 | import ast 2 | from .flatwrapper import FlatWrapper 3 | from .assign_flatwrapper import AssignFlatWrapper 4 | from .return_flatwrapper import ReturnFlatWrapper 5 | from .method_flatwrapper import MethodFlatWrapper 6 | from .cflow_flatwrapper import CFlowFlatWrapper 7 | from .compare_flatwrapper import CompareFlatWrapper 8 | from .literal_flatwrapper import LiteralFlatWrapper 9 | from .expr_flatwrapper import ExprFlatWrapper 10 | from .call_flatwrapper import CallFlatWrapper 11 | 12 | 13 | flat_wrappers = { 14 | ast.Assign: AssignFlatWrapper, 15 | ast.Return: ReturnFlatWrapper, 16 | ast.Module: MethodFlatWrapper, 17 | ast.If: CFlowFlatWrapper, 18 | ast.While: CFlowFlatWrapper, 19 | ast.For: CFlowFlatWrapper, 20 | ast.Compare: CompareFlatWrapper, 21 | ast.Num: LiteralFlatWrapper, 22 | ast.Str: LiteralFlatWrapper, 23 | ast.Name: LiteralFlatWrapper, 24 | ast.Expr: ExprFlatWrapper, 25 | ast.Call: CallFlatWrapper, 26 | "generic": FlatWrapper, 27 | } 28 | 29 | 30 | __all__ = [ 31 | "AssignFlatWrapper", 32 | "ReturnFlatWrapper", 33 | "MethodFlatWrapper", 34 | "CFlowFlatWrapper", 35 | "CompareFlatWrapper", 36 | "LiteralFlatWrapper", 37 | "ExprFlatWrapper", 38 | "CallFlatWrapper", 39 | "FlatWrapper", 40 | ] 41 | -------------------------------------------------------------------------------- /reflectivipy/wrappers/assign_flatwrapper.py: -------------------------------------------------------------------------------- 1 | import ast 2 | from .flatwrapper import FlatWrapper 3 | 4 | 5 | class AssignFlatWrapper(FlatWrapper): 6 | def flatten_children(self): 7 | value_node = self.original_node.value 8 | 9 | if self.is_node_call_with_links(value_node): 10 | self.flattened_children.append(value_node) 11 | return 12 | 13 | value_assign = self.builder.assign_named_value( 14 | self.original_node.temp_name, value_node 15 | ) 16 | rf_assign = self.builder.build_rf_node(value_assign) 17 | self.flattened_children.append(rf_assign) 18 | 19 | def transform_node(self): 20 | if self.original_node.is_generated: 21 | self.node_transformation.append(self.original_node) 22 | return 23 | 24 | targets = self.original_node.targets 25 | value_node = self.original_node.value 26 | temp_name = self.original_node.temp_name 27 | if self.is_node_call_with_links(value_node): 28 | temp_name = value_node.temp_name 29 | value = self.builder.ast_load(temp_name) 30 | transformed_assign = ast.Assign(targets=targets, value=value) 31 | self.node_transformation.append(transformed_assign) 32 | 33 | def is_node_call_with_links(self, rf_node): 34 | if rf_node.__class__ is not ast.Call: 35 | return False 36 | return rf_node.wrapper.should_wrap(rf_node) 37 | -------------------------------------------------------------------------------- /reflectivipy/wrappers/call_flatwrapper.py: -------------------------------------------------------------------------------- 1 | from .flatwrapper import FlatWrapper 2 | 3 | 4 | class CallFlatWrapper(FlatWrapper): 5 | def flatten_children(self): 6 | self.flattened_children.extend(self.original_node.args) 7 | self.flattened_children.append(self.extract_receiver_node()) 8 | 9 | def transform_node(self): 10 | twin = self.original_node.twin 11 | new_args = [] 12 | for arg in self.original_node.args: 13 | new_args.append(self.builder.ast_load(arg.temp_name)) 14 | 15 | twin.args = new_args 16 | twin.func.value = self.builder.ast_load(twin.func.value.temp_name) 17 | transformed_node = self.builder.assign_named_value( 18 | self.original_node.temp_name, twin 19 | ) 20 | self.node_transformation.append(transformed_node) 21 | 22 | def extract_receiver_node(self): 23 | receiver = self.original_node.func.value 24 | 25 | if receiver.links: 26 | return receiver 27 | 28 | return self.builder.assign_named_value(receiver.temp_name, receiver) 29 | -------------------------------------------------------------------------------- /reflectivipy/wrappers/cflow_flatwrapper.py: -------------------------------------------------------------------------------- 1 | from .flatwrapper import FlatWrapper 2 | 3 | 4 | class CFlowConditionExtractor(object): 5 | def __init__(self): 6 | self.flattened_condition = [] 7 | self.preambles = [] 8 | self.body_supplements = [] 9 | 10 | def visit_node(self, rf_node): 11 | visit_method = "visit_" + rf_node.__class__.__name__ 12 | getattr(self, visit_method)(rf_node) 13 | 14 | def visit_While(self, rf_node): 15 | test = rf_node.test 16 | if not test.wrapper.should_wrap(test): 17 | return 18 | self.extract_test(test) 19 | self.preambles = self.flattened_condition 20 | self.body_supplements = self.flattened_condition 21 | 22 | def visit_For(self, rf_node): 23 | pass 24 | 25 | def visit_If(self, rf_node): 26 | test = rf_node.test 27 | if not test.wrapper.should_wrap(test): 28 | return 29 | self.extract_test(test) 30 | self.preambles = self.flattened_condition 31 | 32 | def extract_test(self, rf_test): 33 | self.flattened_condition = rf_test.wrapper.flat_wrap() 34 | 35 | 36 | class CFlowFlatWrapper(FlatWrapper): 37 | def __init__(self, rf_node): 38 | super(CFlowFlatWrapper, self).__init__(rf_node) 39 | 40 | def extract_body(self): 41 | return self.original_node.body 42 | 43 | def transform_node(self): 44 | # TODO : node transformation here 45 | self.original_node.twin.body = self.transform_body() 46 | self.extract_condition() 47 | self.node_transformation.append(self.original_node.twin) 48 | 49 | def should_wrap(self, rf_node): 50 | return True 51 | 52 | def transform_body(self): 53 | transformations = [] 54 | for node in self.extract_body(): 55 | if hasattr(node, "can_be_wrapped"): 56 | body_transformation = node.wrapper.flat_wrap() 57 | transformations.extend(body_transformation) 58 | return transformations 59 | 60 | def extract_condition(self): 61 | extractor = CFlowConditionExtractor() 62 | extractor.visit_node(self.original_node) 63 | self.flattened_children.extend(extractor.preambles) 64 | self.original_node.twin.body.extend(extractor.body_supplements) 65 | -------------------------------------------------------------------------------- /reflectivipy/wrappers/compare_flatwrapper.py: -------------------------------------------------------------------------------- 1 | from .flatwrapper import FlatWrapper 2 | 3 | 4 | class CompareFlatWrapper(FlatWrapper): 5 | def transform_node(self): 6 | # TODO : node transformation here 7 | self.original_node.twin.left = self.builder.ast_load( 8 | self.original_node.left.temp_name 9 | ) 10 | 11 | comparators = [] 12 | 13 | for node in self.original_node.comparators: 14 | comparators.append(self.builder.ast_load(node.temp_name)) 15 | # TODO : node transformation here 16 | self.original_node.twin.comparators = comparators 17 | 18 | def flatten_children(self): 19 | left = self.original_node.left 20 | self.flattened_children.extend(left.wrapper.flat_wrap()) 21 | 22 | for node in self.original_node.comparators: 23 | self.flattened_children.extend(node.wrapper.flat_wrap()) 24 | -------------------------------------------------------------------------------- /reflectivipy/wrappers/expr_flatwrapper.py: -------------------------------------------------------------------------------- 1 | from .flatwrapper import FlatWrapper 2 | 3 | 4 | class ExprFlatWrapper(FlatWrapper): 5 | def flat_wrap(self): 6 | self.reset_wrapping() 7 | if self.should_wrap_children(self.original_node): 8 | self.body.extend(self.original_node.value.wrapper.flat_wrap()) 9 | else: 10 | self.body.append(self.original_node) 11 | return self.body 12 | -------------------------------------------------------------------------------- /reflectivipy/wrappers/flatwrapper.py: -------------------------------------------------------------------------------- 1 | import ast 2 | from ..core import AstBuilder 3 | from ..reifications import ReificationGenerator, link_reification 4 | 5 | 6 | class FlatWrapper(object): 7 | def __init__(self, rf_node): 8 | self.body = [] 9 | self.preambles = [] 10 | self.flattened_children = [] 11 | self.before_links = [] 12 | self.after_links = [] 13 | self.instead_links = [] 14 | self.original_node = rf_node 15 | self.reifications = set() 16 | self.node_transformation = [] 17 | self.builder = AstBuilder() 18 | self.builder.method_node = self.original_node.method_node 19 | self.reifyer = ReificationGenerator() 20 | 21 | def reset_wrapping(self): 22 | self.body = [] 23 | self.preambles = [] 24 | self.flattened_children = [] 25 | self.before_links = [] 26 | self.after_links = [] 27 | self.instead_links = [] 28 | self.node_transformation = [] 29 | 30 | def gen_preambles(self): 31 | preambles = self.reifyer.generate_reifications(self.original_node) 32 | self.preambles.extend(preambles) 33 | 34 | def flatten_children(self): 35 | pass 36 | 37 | def transform_node(self): 38 | self.node_transformation.append(self.original_node) 39 | 40 | def gen_link_node(self, link): 41 | arguments = [] 42 | arguments.extend(link.reified_arguments) 43 | 44 | metaobject_node = ast.Attribute( 45 | value=link_reification(link), attr="metaobject", ctx=ast.Load() 46 | ) 47 | attr_node = ast.Attribute( 48 | value=metaobject_node, attr=link.selector, ctx=ast.Load() 49 | ) 50 | call_node = ast.Call(func=attr_node, args=arguments, keywords=[]) 51 | 52 | return ast.Expr(call_node) 53 | 54 | def sort_links(self): 55 | for link in self.original_node.links: 56 | if link.control == "before": 57 | self.before_links.append(link) 58 | if link.control == "after": 59 | self.after_links.append(link) 60 | if link.control == "instead": 61 | self.instead_links.append(link) 62 | 63 | def flat_wrap(self): 64 | self.reset_wrapping() 65 | self.gen_preambles() 66 | self.sort_links() 67 | 68 | if not self.should_wrap(self.original_node): 69 | self.body.append(self.original_node) 70 | return self.body 71 | 72 | self.init_wrapping() 73 | return self.basic_wrap() 74 | 75 | def init_wrapping(self): 76 | self.flatten_children() 77 | self.transform_node() 78 | 79 | def basic_wrap(self): 80 | self.append_flattened_children() 81 | self.append_preambles() 82 | self.append_links(self.before_links) 83 | self.append_node_transformation() 84 | self.append_links(self.after_links) 85 | return self.body 86 | 87 | def should_wrap(self, rf_node): 88 | return self.should_wrap_node(rf_node) or self.should_wrap_children(rf_node) 89 | 90 | def should_wrap_node(self, rf_node): 91 | if rf_node.links: 92 | return True 93 | return False 94 | 95 | def should_wrap_children(self, rf_node): 96 | for node in rf_node.children: 97 | if self.should_wrap(node): 98 | return True 99 | return False 100 | 101 | def append_preambles(self): 102 | for node in self.preambles: 103 | self.body.append(node) 104 | 105 | def append_flattened_children(self): 106 | for node in self.flattened_children: 107 | if hasattr(node, "wrapper"): 108 | self.body.extend(node.wrapper.flat_wrap()) 109 | else: 110 | self.body.append(node) 111 | 112 | def append_node_transformation(self): 113 | if self.instead_links: 114 | self.body.append(self.gen_link_node(self.instead_links[0])) 115 | return 116 | 117 | for node in self.node_transformation: 118 | self.body.append(node) 119 | 120 | def append_links(self, links): 121 | for link in links: 122 | self.body.append(self.gen_link_node(link)) 123 | -------------------------------------------------------------------------------- /reflectivipy/wrappers/literal_flatwrapper.py: -------------------------------------------------------------------------------- 1 | from .flatwrapper import FlatWrapper 2 | 3 | 4 | class LiteralFlatWrapper(FlatWrapper): 5 | def transform_node(self): 6 | assign = self.builder.assign_named_value( 7 | self.original_node.temp_name, self.original_node 8 | ) 9 | self.node_transformation.append(self.builder.build_rf_node(assign)) 10 | 11 | def should_wrap(self, rf_node): 12 | return True 13 | -------------------------------------------------------------------------------- /reflectivipy/wrappers/method_flatwrapper.py: -------------------------------------------------------------------------------- 1 | import ast 2 | from .cflow_flatwrapper import CFlowFlatWrapper 3 | 4 | 5 | class MethodFlatWrapper(CFlowFlatWrapper): 6 | def transform_node(self): 7 | self.node_transformation.extend(self.transform_body()) 8 | 9 | def sort_links(self): 10 | super(MethodFlatWrapper, self).sort_links() 11 | 12 | body = self.extract_body() 13 | if not body: 14 | return 15 | 16 | for rf_node in body: 17 | self.inject_after_links_in_method_returns(rf_node) 18 | 19 | if self.is_return(self.last_node()): 20 | self.after_links = [] 21 | 22 | def is_return(self, rf_node): 23 | return rf_node.__class__ is ast.Return 24 | 25 | def extract_body(self): 26 | return self.original_node.body[0].body 27 | 28 | def last_node(self): 29 | body = self.extract_body() 30 | if body: 31 | return body[-1] 32 | return None 33 | 34 | def inject_after_links_in_method_returns(self, rf_node): 35 | if self.is_return(rf_node): 36 | rf_node.wrapper.additional_after_links.extend(self.after_links) 37 | return 38 | 39 | for child in rf_node.children: 40 | self.inject_after_links_in_method_returns(child) 41 | -------------------------------------------------------------------------------- /reflectivipy/wrappers/return_flatwrapper.py: -------------------------------------------------------------------------------- 1 | import ast 2 | from .flatwrapper import FlatWrapper 3 | 4 | 5 | class ReturnFlatWrapper(FlatWrapper): 6 | def __init__(self, rf_node): 7 | super(ReturnFlatWrapper, self).__init__(rf_node) 8 | self.additional_after_links = [] 9 | 10 | def transform_node(self): 11 | original_node = self.original_node 12 | builder = self.builder 13 | value_assign = builder.assign_named_value( 14 | original_node.temp_name, original_node.value 15 | ) 16 | self.node_transformation.append(builder.build_rf_node(value_assign)) 17 | 18 | def basic_wrap(self): 19 | super(ReturnFlatWrapper, self).basic_wrap() 20 | self.append_links(self.additional_after_links) 21 | builder = self.builder 22 | return_node = ast.Return(builder.ast_load(self.original_node.temp_name)) 23 | self.body.append(builder.build_rf_node(return_node)) 24 | return self.body 25 | 26 | def should_wrap(self, rf_node): 27 | return ( 28 | super(ReturnFlatWrapper, self).should_wrap(rf_node) 29 | or self.additional_after_links 30 | ) 31 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from setuptools import setup 4 | import reflectivipy 5 | 6 | 7 | packages = ["reflectivipy", "reflectivipy.wrappers"] 8 | 9 | setup( 10 | name="reflectivipy", 11 | version=reflectivipy.__version__, 12 | description=( 13 | "A Python Implementation of the Reflectivity API from " "the Pharo language" 14 | ), 15 | long_description=open("README.rst").read(), 16 | keywords="object-centric partial-behavior-reflection metaprogramming", 17 | url="https://github.com/StevenCostiou/reflectivipy", 18 | author="Steven Costiou", 19 | author_email="steven.costiou@abc.fr", 20 | packages=packages, 21 | package_data={"": ["README.rst", "LICENCE"]}, 22 | include_package_data=True, 23 | tests_require=["pytest"], 24 | classifiers=[ 25 | "Development Status :: 3 - Alpha", 26 | "Programming Language :: Python", 27 | "Programming Language :: Python :: 2.7", 28 | "Programming Language :: Python :: Implementation :: PyPy", 29 | "Operating System :: OS Independent", 30 | "Intended Audience :: Developers", 31 | "Topic :: Software Development", 32 | "Topic :: Software Development :: Libraries", 33 | "Topic :: Software Development :: Code Generators", 34 | "Topic :: Software Development :: Debuggers", 35 | ], 36 | ) 37 | -------------------------------------------------------------------------------- /tests/ReflectivityExample.py: -------------------------------------------------------------------------------- 1 | import reflectivipy 2 | 3 | 4 | def sample_node(): 5 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_assign') 6 | return rf_ast.original_ast.body[0].body[0] 7 | 8 | 9 | def expr_sample_node(): 10 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'm3') 11 | return rf_ast.original_ast.body[0].body[0] 12 | 13 | 14 | def call_sample_node(): 15 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'm3') 16 | return rf_ast.original_ast.body[0].body[0] 17 | 18 | 19 | def complex_call_sample_node(): 20 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_method') 21 | return rf_ast.original_ast.body[0].body[1] 22 | 23 | 24 | def complex_expr_call_sample_node(): 25 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'm4') 26 | return rf_ast.original_ast.body[0].body[0] 27 | 28 | 29 | def call_with_complex_receiver_sample_node(): 30 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'm5') 31 | return rf_ast.original_ast.body[0].body[0] 32 | 33 | 34 | def value_reification_sample_node(): 35 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_value_reification') 36 | return rf_ast.original_ast.body[0].body[1] 37 | 38 | 39 | def value_call_reification_sample_node(): 40 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_value_call_reification') 41 | return rf_ast.original_ast.body[0].body[1] 42 | 43 | 44 | def value_call_call_reification_sample_node(): 45 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_value_call_call_reification') 46 | return rf_ast.original_ast.body[0].body[1] 47 | 48 | 49 | def value_name_reification_sample_node(): 50 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_value_name_reification') 51 | return rf_ast.original_ast.body[0].body[1].value.args[0] 52 | 53 | 54 | def method_with_args_sample_node(): 55 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'method_with_args') 56 | return rf_ast.original_ast 57 | 58 | 59 | class ReflectivityExample: 60 | def __init__(self): 61 | self.tag = None 62 | self.tagged_reifications = list() 63 | 64 | def tag_exec_(self): 65 | self.tag = 'tag' 66 | 67 | def tag_exec(self, reifications): 68 | self.tag = reifications 69 | 70 | def tag_push(self, reification): 71 | self.tagged_reifications.append(reification) 72 | 73 | def tag_reifications(self, reification_1, reification_2, reification_3, reification_4): 74 | self.tagged_reifications = list() 75 | self.tagged_reifications.append(reification_1) 76 | self.tagged_reifications.append(reification_2) 77 | self.tagged_reifications.append(reification_3) 78 | self.tagged_reifications.append(reification_4) 79 | 80 | def example_method(self): 81 | val = 3 + 4 82 | val = self.m(self.m2(val)) 83 | return val 84 | 85 | def method_with_args(self, i, j, k): 86 | val = self.m(j) 87 | return val + i + k 88 | 89 | def m(self, i): 90 | return i + 1 91 | 92 | def m2(self, j): 93 | return j + 1 94 | 95 | def m3(self): 96 | self.m(0) 97 | 98 | def m4(self): 99 | self.m(self.m2(42)) 100 | 101 | def m5(self): 102 | self.m6().m(0) 103 | 104 | def m6(self): 105 | return self 106 | 107 | def example_assign(self): 108 | a = 1 109 | return a 110 | 111 | def example_call(self): 112 | i = 1 113 | self.m(i) 114 | 115 | def example_assign_call(self): 116 | i = 1 117 | a = self.m(i) 118 | return a 119 | 120 | def example_assign_embedded_calls(self): 121 | i = 1 122 | a = self.m(self.m2(i)) 123 | return a 124 | 125 | def example_return(self): 126 | return 42 127 | 128 | def example_multiple_return(self, i): 129 | if i == 0: 130 | return 42 131 | j = i + 1 132 | return j 133 | 134 | def example_while(self): 135 | i = 0 136 | while i < 10: 137 | i = i + 1 138 | return i 139 | 140 | def example_for(self): 141 | j = 0 142 | for i in range(10): 143 | j = j + 1 144 | return j 145 | 146 | def example_expr_node(self): 147 | self.m(1) 148 | 149 | def example_value_reification(self): 150 | i = 1 151 | i = 2 152 | 153 | def example_value_call_reification(self): 154 | i = 1 155 | i = self.m(i) 156 | 157 | def example_value_call_call_reification(self): 158 | i = self.m(0) 159 | i = self.m(i) 160 | 161 | def example_value_name_reification(self): 162 | i = 1 163 | self.m(i) 164 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /tests/test_MetaLink.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | import reflectivipy 4 | from tests.ReflectivityExample import ReflectivityExample 5 | from reflectivipy import MetaLink 6 | from functools import wraps 7 | 8 | 9 | @pytest.fixture(autouse=True) 10 | def setup(): 11 | reflectivipy.uninstall_all() 12 | 13 | 14 | def test_globals_metalink_registry(): 15 | example = ReflectivityExample() 16 | 17 | link = MetaLink(example, 'tag_exec', 'before', []) 18 | rf_node = reflectivipy.reflective_ast_for_method(ReflectivityExample, 'example_method') 19 | 20 | reflectivipy.link(link, rf_node) 21 | 22 | rf_method = reflectivipy.reflective_method_for(ReflectivityExample, 'example_method') 23 | method_globals = ReflectivityExample.example_method.__globals__ 24 | 25 | assert method_globals["__rf_method__"] is rf_method 26 | assert method_globals["__rf_method__"].lookup_link(id(link)) is link 27 | 28 | 29 | def test_link_to_node(): 30 | example = ReflectivityExample() 31 | 32 | link = MetaLink(example, 'tag_exec', 'before', []) 33 | rf_node = reflectivipy.reflective_ast_for_method(ReflectivityExample, 'example_method') 34 | 35 | reflectivipy.link(link, rf_node) 36 | 37 | assert rf_node.links 38 | assert rf_node.links.pop() is link 39 | 40 | assert link.nodes 41 | assert link.nodes.pop() is rf_node 42 | 43 | 44 | def test_uninstall(): 45 | pass 46 | 47 | 48 | decorator_counter = 0 49 | 50 | 51 | def decorate_with(increment): 52 | def decorator(f): 53 | @wraps(f) 54 | def wrapper(*args, **kwargs): 55 | global decorator_counter 56 | decorator_counter += increment 57 | print(decorator_counter) 58 | return f(*args, **kwargs) 59 | return wrapper 60 | return decorator 61 | 62 | 63 | def test_metalinks_with_wrappers(): 64 | 65 | class MetaBehavior(object): 66 | def __init__(self): 67 | self.called = 0 68 | 69 | def meta(self): 70 | self.called += 1 71 | 72 | 73 | class MyClass(object): 74 | @decorate_with(1) 75 | @decorate_with(2) 76 | @decorate_with(3) 77 | def da_call(self): 78 | return 1234 79 | 80 | m = MyClass() 81 | metabehavior = MetaBehavior() 82 | 83 | link = reflectivipy.MetaLink(metabehavior, selector='meta', control='before') 84 | rf_ast = reflectivipy.reflective_ast_for_method(m, 'da_call') 85 | 86 | node = rf_ast.body[0].body[0] 87 | 88 | reflectivipy.link(link, node) 89 | 90 | result = m.da_call() 91 | 92 | assert result == 1234 93 | assert decorator_counter == 6 94 | assert metabehavior.called == 1 95 | -------------------------------------------------------------------------------- /tests/test_RFFlatWrapper.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | import reflectivipy 4 | import tests.ReflectivityExample as ReflectivityExample 5 | from reflectivipy import MetaLink 6 | from reflectivipy.wrappers import FlatWrapper 7 | 8 | 9 | @pytest.fixture(autouse=True) 10 | def setup(): 11 | reflectivipy.uninstall_all() 12 | 13 | 14 | def test_sort_links(): 15 | before_link = MetaLink(None, None, 'before', []) 16 | after_link = MetaLink(None, None, 'after', []) 17 | instead_link = MetaLink(None, None, 'instead', []) 18 | node = ReflectivityExample.sample_node() 19 | 20 | node.links.append(before_link) 21 | node.links.append(after_link) 22 | node.links.append(instead_link) 23 | 24 | wrapper = FlatWrapper(node) 25 | 26 | assert len(wrapper.before_links) == 0 27 | assert len(wrapper.after_links) == 0 28 | assert len(wrapper.instead_links) == 0 29 | 30 | wrapper.sort_links() 31 | 32 | assert len(wrapper.before_links) == 1 33 | assert wrapper.before_links[0] is before_link 34 | 35 | assert len(wrapper.after_links) == 1 36 | assert wrapper.after_links[0] is after_link 37 | 38 | assert len(wrapper.instead_links) == 1 39 | assert wrapper.instead_links[0] is instead_link 40 | -------------------------------------------------------------------------------- /tests/test_ReflectiveMethod.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | import reflectivipy 4 | from .ReflectivityExample import ReflectivityExample 5 | from reflectivipy import MetaLink 6 | 7 | 8 | @pytest.fixture(autouse=True) 9 | def setup(): 10 | reflectivipy.uninstall_all() 11 | 12 | 13 | def test_original_ast_preservation(): 14 | example = ReflectivityExample() 15 | link = MetaLink(example, 'tag_exec', 'after', ['node']) 16 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_while') 17 | original_body = rf_ast.original_ast.body[0].body 18 | node = original_body[1].test.left 19 | 20 | number_of_nodes = len(original_body) 21 | original_left_id = node.id 22 | 23 | reflectivipy.link(link, node) 24 | 25 | new_body = rf_ast.original_ast.body[0].body 26 | new_left = new_body[1].test.left 27 | reflective_ast_body = rf_ast.reflective_ast.body[0].body 28 | 29 | ReflectivityExample().example_while() 30 | assert example.tag is node 31 | assert new_body is original_body 32 | assert len(new_body) == number_of_nodes 33 | assert original_left_id == new_left.id 34 | 35 | assert reflective_ast_body[1] is not new_body[1] 36 | assert len(reflective_ast_body) > number_of_nodes 37 | 38 | 39 | def test_restore_original(): 40 | example = ReflectivityExample() 41 | link = MetaLink(example, 'tag_exec', 'after', ['node']) 42 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_while') 43 | original_body = rf_ast.original_ast.body[0].body 44 | node = original_body[1].test.left 45 | 46 | reflectivipy.link(link, node) 47 | 48 | example.tag = None 49 | ReflectivityExample().example_while() 50 | assert example.tag is node 51 | 52 | reflectivipy.uninstall_all() 53 | 54 | example.tag = None 55 | ReflectivityExample().example_while() 56 | assert example.tag is None 57 | 58 | 59 | def test_uninstall_all(): 60 | pass 61 | 62 | 63 | def test_metalinks_count(): 64 | example = ReflectivityExample() 65 | link = MetaLink(example, 'tag_exec_', 'before', []) 66 | rf_method = reflectivipy.reflective_method_for(ReflectivityExample, 'example_assign') 67 | node = rf_method.original_ast.body[0].body[0] 68 | 69 | assert len(reflectivipy.metalinks) == 0 70 | 71 | reflectivipy.link(link, node) 72 | 73 | len(reflectivipy.metalinks) == 1 74 | assert reflectivipy.metalinks.pop() is link 75 | -------------------------------------------------------------------------------- /tests/test_cflow_for.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | import reflectivipy 4 | from tests.ReflectivityExample import ReflectivityExample 5 | from reflectivipy import MetaLink 6 | 7 | 8 | @pytest.fixture(autouse=True) 9 | def setup(): 10 | reflectivipy.uninstall_all() 11 | 12 | 13 | def test_link_before_for(): 14 | example = ReflectivityExample() 15 | link = MetaLink(example, 'tag_exec_', 'before', []) 16 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_for') 17 | node = rf_ast.original_ast.body[0].body[1] 18 | 19 | reflectivipy.link(link, node) 20 | 21 | assert example.tag is None 22 | assert ReflectivityExample().example_for() == 10 23 | assert example.tag == 'tag' 24 | 25 | 26 | def test_link_after_for(): 27 | example = ReflectivityExample() 28 | link = MetaLink(example, 'tag_exec_', 'after', []) 29 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_for') 30 | node = rf_ast.original_ast.body[0].body[1] 31 | 32 | reflectivipy.link(link, node) 33 | 34 | assert example.tag is None 35 | assert ReflectivityExample().example_for() == 10 36 | assert example.tag == 'tag' 37 | 38 | 39 | def test_link_before_after_for(): 40 | example = ReflectivityExample() 41 | before_link = MetaLink(example, 'tag_push', 'before', [1]) 42 | after_link = MetaLink(example, 'tag_push', 'after', [2]) 43 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_for') 44 | node = rf_ast.original_ast.body[0].body[1] 45 | 46 | reflectivipy.link(before_link, node) 47 | reflectivipy.link(after_link, node) 48 | 49 | assert len(example.tagged_reifications) == 0 50 | assert ReflectivityExample().example_for() == 10 51 | assert len(example.tagged_reifications) == 2 52 | assert example.tagged_reifications[0] == 1 53 | assert example.tagged_reifications[1] == 2 54 | 55 | 56 | def test_link_inside_for(): 57 | example = ReflectivityExample() 58 | link = MetaLink(example, 'tag_exec_', 'after', []) 59 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_for') 60 | node = rf_ast.original_ast.body[0].body[1].body[0] 61 | 62 | reflectivipy.link(link, node) 63 | 64 | assert example.tag is None 65 | assert ReflectivityExample().example_for() == 10 66 | assert example.tag == 'tag' 67 | -------------------------------------------------------------------------------- /tests/test_cflow_if.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | import reflectivipy 4 | from reflectivipy import MetaLink 5 | from tests.ReflectivityExample import ReflectivityExample 6 | 7 | 8 | @pytest.fixture(autouse=True) 9 | def setup(): 10 | reflectivipy.uninstall_all() 11 | 12 | 13 | def test_link_before_if(): 14 | example = ReflectivityExample() 15 | link = MetaLink(example, 'tag_exec_', 'before', []) 16 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_multiple_return') 17 | node = rf_ast.original_ast.body[0].body[0] 18 | 19 | reflectivipy.link(link, node) 20 | 21 | assert example.tag is None 22 | assert ReflectivityExample().example_multiple_return(0) == 42 23 | assert example.tag == 'tag' 24 | 25 | 26 | def test_link_after_if(): 27 | example = ReflectivityExample() 28 | link = MetaLink(example, 'tag_exec_', 'after', []) 29 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_multiple_return') 30 | node = rf_ast.original_ast.body[0].body[0] 31 | 32 | reflectivipy.link(link, node) 33 | 34 | assert example.tag is None 35 | assert ReflectivityExample().example_multiple_return(1) == 2 36 | assert example.tag == 'tag' 37 | 38 | 39 | def test_link_instead_if(): 40 | example = ReflectivityExample() 41 | link = MetaLink(example, 'tag_exec_', 'instead', []) 42 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_multiple_return') 43 | node = rf_ast.original_ast.body[0].body[0] 44 | 45 | reflectivipy.link(link, node) 46 | 47 | assert example.tag is None 48 | assert ReflectivityExample().example_multiple_return(1) == 2 49 | assert example.tag == 'tag' 50 | 51 | 52 | def test_link_before_after_if(): 53 | example = ReflectivityExample() 54 | before_link = MetaLink(example, 'tag_push', 'before', [1]) 55 | after_link = MetaLink(example, 'tag_push', 'after', [2]) 56 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_multiple_return') 57 | node = rf_ast.original_ast.body[0].body[0] 58 | 59 | reflectivipy.link(before_link, node) 60 | reflectivipy.link(after_link, node) 61 | 62 | assert len(example.tagged_reifications) == 0 63 | assert ReflectivityExample().example_multiple_return(1) == 2 64 | assert len(example.tagged_reifications) == 2 65 | assert example.tagged_reifications[0] == 1 66 | assert example.tagged_reifications[1] == 2 67 | 68 | 69 | def test_link_after_if_that_returns_before_link(): 70 | example = ReflectivityExample() 71 | link = MetaLink(example, 'tag_exec_', 'after', []) 72 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_multiple_return') 73 | node = rf_ast.original_ast.body[0].body[0] 74 | 75 | reflectivipy.link(link, node) 76 | 77 | assert example.tag is None 78 | assert ReflectivityExample().example_multiple_return(0) == 42 79 | assert example.tag is None 80 | -------------------------------------------------------------------------------- /tests/test_cflow_while.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | import reflectivipy 4 | from tests.ReflectivityExample import ReflectivityExample 5 | from reflectivipy import MetaLink 6 | 7 | 8 | @pytest.fixture(autouse=True) 9 | def setup(): 10 | reflectivipy.uninstall_all() 11 | 12 | 13 | def test_link_before_while(): 14 | example = ReflectivityExample() 15 | link = MetaLink(example, 'tag_exec_', 'before', []) 16 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_while') 17 | node = rf_ast.original_ast.body[0].body[1] 18 | 19 | reflectivipy.link(link, node) 20 | 21 | assert example.tag is None 22 | assert ReflectivityExample().example_while() == 10 23 | assert example.tag == 'tag' 24 | 25 | 26 | def test_link_after_while(): 27 | example = ReflectivityExample() 28 | link = MetaLink(example, 'tag_exec_', 'after', []) 29 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_while') 30 | node = rf_ast.original_ast.body[0].body[1] 31 | 32 | reflectivipy.link(link, node) 33 | 34 | assert example.tag is None 35 | assert ReflectivityExample().example_while() == 10 36 | assert example.tag == 'tag' 37 | 38 | 39 | def test_link_before_after_while(): 40 | example = ReflectivityExample() 41 | before_link = MetaLink(example, 'tag_push', 'before', [1]) 42 | after_link = MetaLink(example, 'tag_push', 'after', [2]) 43 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_while') 44 | node = rf_ast.original_ast.body[0].body[1] 45 | 46 | reflectivipy.link(before_link, node) 47 | reflectivipy.link(after_link, node) 48 | 49 | assert len(example.tagged_reifications) == 0 50 | assert ReflectivityExample().example_while() == 10 51 | assert len(example.tagged_reifications) == 2 52 | assert example.tagged_reifications[0] == 1 53 | assert example.tagged_reifications[1] == 2 54 | 55 | 56 | def test_link_inside_while(): 57 | example = ReflectivityExample() 58 | link = MetaLink(example, 'tag_exec_', 'after', []) 59 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_while') 60 | node = rf_ast.original_ast.body[0].body[1].body[0] 61 | 62 | reflectivipy.link(link, node) 63 | 64 | assert example.tag is None 65 | assert ReflectivityExample().example_while() == 10 66 | assert example.tag == 'tag' 67 | 68 | 69 | def test_link_on_while_after_left_test(): 70 | example = ReflectivityExample() 71 | link = MetaLink(example, 'tag_exec', 'after', ['node']) 72 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_while') 73 | node = rf_ast.original_ast.body[0].body[1].test.left 74 | 75 | reflectivipy.link(link, node) 76 | 77 | assert example.tag is None 78 | assert ReflectivityExample().example_while() == 10 79 | assert example.tag is node 80 | 81 | 82 | def test_multiple_links_within_while(): 83 | example = ReflectivityExample() 84 | link_1 = MetaLink(example, 'tag_push', 'after', [0]) 85 | link_2 = MetaLink(example, 'tag_push', 'after', [1]) 86 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_while') 87 | node_1 = rf_ast.original_ast.body[0].body[1].test.left 88 | node_2 = rf_ast.original_ast.body[0].body[1].body[0] 89 | 90 | reflectivipy.link(link_1, node_1) 91 | reflectivipy.link(link_2, node_2) 92 | 93 | assert len(example.tagged_reifications) == 0 94 | assert ReflectivityExample().example_while() == 10 95 | assert len(example.tagged_reifications) == 21 96 | 97 | for i, reif_value in enumerate(example.tagged_reifications): 98 | assert reif_value == i % 2 99 | -------------------------------------------------------------------------------- /tests/test_flat_wrap_method.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | import reflectivipy 4 | from tests.ReflectivityExample import ReflectivityExample 5 | from reflectivipy import MetaLink 6 | 7 | 8 | @pytest.fixture(autouse=True) 9 | def setup(): 10 | reflectivipy.uninstall_all() 11 | 12 | 13 | def test_wrap_method_node(): 14 | example = ReflectivityExample() 15 | 16 | link = MetaLink(example, 'tag_exec_', 'before', []) 17 | ast = reflectivipy.reflective_ast_for_method(ReflectivityExample, 'example_method') 18 | 19 | reflectivipy.link(link, ast) 20 | 21 | assert example.tag is None 22 | assert ReflectivityExample().example_method() == 9 23 | assert example.tag == 'tag' 24 | -------------------------------------------------------------------------------- /tests/test_flat_wrap_node.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | import reflectivipy 4 | from tests.ReflectivityExample import ReflectivityExample 5 | from reflectivipy import MetaLink 6 | 7 | 8 | @pytest.fixture(autouse=True) 9 | def setup(): 10 | reflectivipy.uninstall_all() 11 | 12 | 13 | def test_wrap_assign(): 14 | example = ReflectivityExample() 15 | link = MetaLink(example, 'tag_exec_', 'before', []) 16 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_assign') 17 | node = rf_ast.original_ast.body[0].body[0] 18 | 19 | reflectivipy.link(link, node) 20 | 21 | assert example.tag is None 22 | assert ReflectivityExample().example_assign() == 1 23 | assert example.tag == 'tag' 24 | 25 | 26 | def test_wrap_assign_with_children(): 27 | example = ReflectivityExample() 28 | link = MetaLink(example, 'tag_exec_', 'before', []) 29 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_assign_call') 30 | node = rf_ast.original_ast.body[0].body[1] 31 | 32 | reflectivipy.link(link, node) 33 | 34 | assert example.tag is None 35 | assert ReflectivityExample().example_assign_call() == 2 36 | assert example.tag == 'tag' 37 | 38 | 39 | def test_after_return(): 40 | example = ReflectivityExample() 41 | link = MetaLink(example, 'tag_exec_', 'after', []) 42 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_return') 43 | node = rf_ast.original_ast.body[0].body[0] 44 | 45 | reflectivipy.link(link, node) 46 | 47 | assert example.tag is None 48 | assert ReflectivityExample().example_return() == 42 49 | assert example.tag == 'tag' 50 | 51 | 52 | def test_after_returns_wraps_in_method(): 53 | example = ReflectivityExample() 54 | link = MetaLink(example, 'tag_exec_', 'after', []) 55 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_multiple_return') 56 | node = rf_ast.original_ast 57 | 58 | reflectivipy.link(link, node) 59 | 60 | assert example.tag is None 61 | assert ReflectivityExample().example_multiple_return(0) == 42 62 | assert example.tag == 'tag' 63 | 64 | example.tag = None 65 | assert example.tag is None 66 | assert ReflectivityExample().example_multiple_return(1) == 2 67 | assert example.tag == 'tag' 68 | -------------------------------------------------------------------------------- /tests/test_node_wrapping.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import ast 3 | from .ReflectivityExample import * 4 | import reflectivipy 5 | from reflectivipy import MetaLink 6 | 7 | 8 | @pytest.fixture(autouse=True) 9 | def setup(): 10 | reflectivipy.uninstall_all() 11 | 12 | 13 | def test_wrap_expr(): 14 | node = expr_sample_node() 15 | 16 | assert type(node) is ast.Expr 17 | 18 | transformation = node.wrapper.flat_wrap() 19 | assert len(transformation) == 1 20 | assert transformation[0] is node 21 | 22 | link = MetaLink(ReflectivityExample(), 'tag_exec_', 'before', []) 23 | node.links.append(link) 24 | 25 | assert type(node) is ast.Expr 26 | transformation = node.wrapper.flat_wrap() 27 | assert len(transformation) == 1 28 | assert transformation[0] is node 29 | 30 | 31 | def test_wrap_call(): 32 | node = call_sample_node().value 33 | 34 | assert type(node) is ast.Call 35 | 36 | transformation = node.wrapper.flat_wrap() 37 | assert len(transformation) == 1 38 | assert transformation[0] is node 39 | 40 | link = MetaLink(ReflectivityExample(), 'tag_exec_', 'before', []) 41 | node.links.append(link) 42 | 43 | assert type(node) is ast.Call 44 | 45 | transformation = node.wrapper.flat_wrap() 46 | assert len(transformation) == 4 47 | assert type(transformation[0]) is ast.Assign 48 | assert transformation[0].value is node.args[0] 49 | assert transformation[0] is not node 50 | 51 | assert len(transformation[3].value.args) == 1 52 | assert type(transformation[3].value.args[0]) is ast.Name 53 | assert transformation[3].value.args[0].id is node.args[0].temp_name 54 | assert transformation[3].value.func.value.id is node.func.value.temp_name 55 | 56 | 57 | def test_wrap_call_in_assign(): 58 | node = method_with_args_sample_node().body[0].body[0] 59 | 60 | link = MetaLink(ReflectivityExample(), 'tag_exec_', 'before', []) 61 | node.value.links.append(link) 62 | 63 | assert type(node) is ast.Assign 64 | assert type(node.value) is ast.Call 65 | 66 | transformation = node.wrapper.flat_wrap() 67 | assert len(transformation) == 5 68 | assert type(transformation[0]) == ast.Assign 69 | assert transformation[0].value is node.value.args[0] 70 | assert type(transformation[1]) is ast.Assign 71 | assert transformation[1].value.id == 'self' 72 | assert transformation[0] is not node 73 | assert len(transformation[3].value.args) == 1 74 | assert type(transformation[3].value.args[0]) is ast.Name 75 | assert transformation[3].value.args[0].id is node.value.args[0].temp_name 76 | assert transformation[3].value.func.value.id is node.value.func.value.temp_name 77 | 78 | 79 | def test_wrap_complex_expr_call(): 80 | node = complex_expr_call_sample_node() 81 | 82 | link = MetaLink(ReflectivityExample(), 'tag_exec_', 'before', []) 83 | node.value.args[0].links.append(link) 84 | 85 | transformation = node.wrapper.flat_wrap() 86 | assert len(transformation) == 6 87 | assert type(transformation[3]) is ast.Assign 88 | assert transformation[3].value.rf_id is node.value.args[0].rf_id 89 | assert transformation[3] is not node 90 | 91 | 92 | def test_call_receiver_flattening(): 93 | node = call_with_complex_receiver_sample_node() 94 | link = MetaLink(ReflectivityExample(), 'tag_exec_', 'before', []) 95 | node.value.links.append(link) 96 | 97 | transformation = node.wrapper.flat_wrap() 98 | assert len(transformation) == 4 99 | assert transformation[1].value.func.value.id == 'self' 100 | assert transformation[3].value.func.value.id == transformation[1].targets[0].id 101 | 102 | 103 | def test_call_flattening(): 104 | node = call_with_complex_receiver_sample_node() 105 | link = MetaLink(ReflectivityExample(), 'tag_exec_', 'before', []) 106 | node.value.func.value.links.append(link) 107 | 108 | transformation = node.wrapper.flat_wrap() 109 | assert len(transformation) == 5 110 | assert transformation[1].value.id == 'self' 111 | assert transformation[3].value.func.value.id == transformation[1].targets[0].id 112 | assert transformation[4].value.func.value.id == transformation[3].targets[0].id 113 | 114 | 115 | def test_wrap_assign(): 116 | node = sample_node() 117 | 118 | assert type(node) is ast.Assign 119 | 120 | transformation = node.wrapper.flat_wrap() 121 | assert len(transformation) == 1 122 | assert transformation[0] is node 123 | 124 | link = MetaLink(ReflectivityExample(), 'tag_exec_', 'before', []) 125 | node.links.append(link) 126 | 127 | assert type(node) is ast.Assign 128 | 129 | transformation = node.wrapper.flat_wrap() 130 | assert len(transformation) == 3 131 | assert type(transformation[0]) is ast.Assign 132 | assert transformation[0].value is node.value 133 | assert transformation[0] is not node 134 | 135 | 136 | def test_flatten_children(): 137 | pass 138 | -------------------------------------------------------------------------------- /tests/test_reifications.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | import reflectivipy 4 | from reflectivipy import MetaLink 5 | 6 | from .ReflectivityExample import ReflectivityExample 7 | from .ReflectivityExample import call_with_complex_receiver_sample_node 8 | from .ReflectivityExample import value_reification_sample_node 9 | from .ReflectivityExample import value_call_reification_sample_node 10 | from .ReflectivityExample import value_call_call_reification_sample_node 11 | from .ReflectivityExample import value_name_reification_sample_node 12 | from .ReflectivityExample import method_with_args_sample_node 13 | 14 | 15 | @pytest.fixture(autouse=True) 16 | def setup(): 17 | reflectivipy.uninstall_all() 18 | 19 | 20 | def test_class_reification(): 21 | example = ReflectivityExample() 22 | link = MetaLink(example, 'tag_exec', 'before', ['class']) 23 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_assign_call') 24 | node = rf_ast.original_ast.body[0].body[1] 25 | 26 | reflectivipy.link(link, node) 27 | 28 | assert example.tag is None 29 | 30 | ReflectivityExample().example_assign_call() == 2 31 | assert example.tag is ReflectivityExample 32 | 33 | 34 | def test_literal_value_reification(): 35 | example = ReflectivityExample() 36 | an_object = ReflectivityExample() 37 | link = MetaLink(example, 'tag_reifications', 'before', [an_object, 42, "hello", None]) 38 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_assign_call') 39 | node = rf_ast.original_ast.body[0].body[1] 40 | 41 | reflectivipy.link(link, node) 42 | 43 | assert len(example.tagged_reifications) is 0 44 | assert ReflectivityExample().example_assign_call() == 2 45 | assert example.tagged_reifications[0] is an_object 46 | assert example.tagged_reifications[1] is 42 47 | assert example.tagged_reifications[2] is "hello" 48 | assert example.tagged_reifications[3]is None 49 | 50 | 51 | def test_object_reification(): 52 | example = ReflectivityExample() 53 | working_example = ReflectivityExample() 54 | link = MetaLink(example, 'tag_exec', 'before', ['object']) 55 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_assign_call') 56 | node = rf_ast.original_ast.body[0].body[1] 57 | 58 | reflectivipy.link(link, node) 59 | 60 | assert example.tag is None 61 | assert working_example.example_assign_call() == 2 62 | assert example.tag is working_example 63 | 64 | 65 | def test_node_reification(): 66 | example = ReflectivityExample() 67 | link = MetaLink(example, 'tag_exec', 'before', ['node']) 68 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_assign_call') 69 | node = rf_ast.original_ast.body[0].body[1] 70 | 71 | reflectivipy.link(link, node) 72 | 73 | assert example.tag is None 74 | assert ReflectivityExample().example_assign_call() == 2 75 | assert example.tag is node 76 | 77 | 78 | def test_method_reification(): 79 | example = ReflectivityExample() 80 | working_example = ReflectivityExample() 81 | link = MetaLink(example, 'tag_exec', 'before', ['method']) 82 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_assign_call') 83 | node = rf_ast.original_ast.body[0].body[1] 84 | 85 | reflectivipy.link(link, node) 86 | 87 | assert example.tag is None 88 | assert working_example.example_assign_call() == 2 89 | assert example.tag.__name__, getattr(ReflectivityExample, 'example_assign_call').__name__ 90 | 91 | 92 | def test_multiple_reifications(): 93 | example = ReflectivityExample() 94 | an_object = ReflectivityExample() 95 | working_example = ReflectivityExample() 96 | link = MetaLink(example, 'tag_reifications', 'after', ['class', 'object', 'node', an_object]) 97 | rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_assign_call') 98 | node = rf_ast.original_ast.body[0].body[1] 99 | 100 | reflectivipy.link(link, node) 101 | 102 | assert len(example.tagged_reifications) == 0 103 | assert working_example.example_assign_call() == 2 104 | assert len(example.tagged_reifications) == 4 105 | assert example.tagged_reifications[0] is ReflectivityExample 106 | assert example.tagged_reifications[1] is working_example 107 | assert example.tagged_reifications[2] is node 108 | assert example.tagged_reifications[3] is an_object 109 | 110 | 111 | def test_receiver_reification(): 112 | example = ReflectivityExample() 113 | node = call_with_complex_receiver_sample_node().value.func.value 114 | 115 | working_example = ReflectivityExample() 116 | link = MetaLink(example, 'tag_exec', 'before', ['receiver']) 117 | 118 | reflectivipy.link(link, node) 119 | assert example.tag is None 120 | 121 | working_example.m5() 122 | assert example.tag is working_example 123 | 124 | 125 | def test_call_receiver_reification(): 126 | example = ReflectivityExample() 127 | node = call_with_complex_receiver_sample_node().value 128 | 129 | working_example = ReflectivityExample() 130 | link = MetaLink(example, 'tag_exec', 'before', ['receiver']) 131 | 132 | reflectivipy.link(link, node) 133 | assert example.tag is None 134 | 135 | working_example.m5() 136 | assert example.tag is working_example 137 | 138 | 139 | def test_selector_reification(): 140 | example = ReflectivityExample() 141 | node = call_with_complex_receiver_sample_node().value 142 | 143 | working_example = ReflectivityExample() 144 | link = MetaLink(example, 'tag_exec', 'before', ['selector']) 145 | 146 | reflectivipy.link(link, node) 147 | assert example.tag is None 148 | 149 | working_example.m5() 150 | assert example.tag == 'm' 151 | 152 | 153 | def test_sender_reification(): 154 | example = ReflectivityExample() 155 | node = call_with_complex_receiver_sample_node().value 156 | 157 | working_example = ReflectivityExample() 158 | link = MetaLink(example, 'tag_exec', 'before', ['sender']) 159 | 160 | reflectivipy.link(link, node) 161 | assert example.tag is None 162 | 163 | working_example.m5() 164 | assert example.tag == 'm5' 165 | 166 | 167 | def test_assignment_values_reification(): 168 | example = ReflectivityExample() 169 | node = call_with_complex_receiver_sample_node().value.func.value 170 | 171 | working_example = ReflectivityExample() 172 | link = MetaLink(example, 'tag_exec', 'before', ['receiver']) 173 | 174 | reflectivipy.link(link, node) 175 | assert example.tag is None 176 | 177 | working_example.m5() 178 | assert example.tag is working_example 179 | 180 | 181 | def test_read_values_reification(): 182 | example = ReflectivityExample() 183 | node = call_with_complex_receiver_sample_node().value.func.value 184 | 185 | working_example = ReflectivityExample() 186 | link = MetaLink(example, 'tag_exec', 'before', ['receiver']) 187 | 188 | reflectivipy.link(link, node) 189 | assert example.tag is None 190 | 191 | working_example.m5() 192 | assert example.tag is working_example 193 | 194 | 195 | def test_assignment_name_reification(): 196 | example = ReflectivityExample() 197 | node = value_reification_sample_node() 198 | 199 | working_example = ReflectivityExample() 200 | link = MetaLink(example, 'tag_exec', 'before', ['name']) 201 | 202 | reflectivipy.link(link, node) 203 | assert example.tag is None 204 | 205 | working_example.example_value_reification() 206 | assert example.tag == 'i' 207 | 208 | 209 | def test_read_name_reification(): 210 | example = ReflectivityExample() 211 | node = value_reification_sample_node() 212 | 213 | working_example = ReflectivityExample() 214 | link = MetaLink(example, 'tag_exec', 'before', ['name']) 215 | 216 | reflectivipy.link(link, node) 217 | assert example.tag is None 218 | 219 | working_example.example_value_reification() 220 | assert example.tag == 'i' 221 | 222 | 223 | def test_arguments_reification(): 224 | example = ReflectivityExample() 225 | node = call_with_complex_receiver_sample_node().value.func.value 226 | 227 | working_example = ReflectivityExample() 228 | link = MetaLink(example, 'tag_exec', 'before', ['receiver']) 229 | 230 | reflectivipy.link(link, node) 231 | assert example.tag is None 232 | 233 | working_example.m5() 234 | assert example.tag is working_example 235 | 236 | 237 | def test_old_value_reification_assign(): 238 | example = ReflectivityExample() 239 | node = value_reification_sample_node() 240 | 241 | working_example = ReflectivityExample() 242 | link = MetaLink(example, 'tag_exec', 'before', ['old_value']) 243 | 244 | reflectivipy.link(link, node) 245 | assert example.tag is None 246 | 247 | working_example.example_value_reification() 248 | assert example.tag == 1 249 | 250 | 251 | def test_new_value_reification_assign(): 252 | example = ReflectivityExample() 253 | node = value_reification_sample_node() 254 | 255 | working_example = ReflectivityExample() 256 | link = MetaLink(example, 'tag_exec', 'before', ['new_value']) 257 | 258 | reflectivipy.link(link, node) 259 | assert example.tag is None 260 | 261 | working_example.example_value_reification() 262 | assert example.tag == 2 263 | 264 | 265 | def test_value_reification_assign(): 266 | example = ReflectivityExample() 267 | node = value_reification_sample_node() 268 | 269 | working_example = ReflectivityExample() 270 | link = MetaLink(example, 'tag_exec', 'before', ['value']) 271 | 272 | reflectivipy.link(link, node) 273 | assert example.tag is None 274 | 275 | working_example.example_value_reification() 276 | assert example.tag == 1 277 | 278 | 279 | def test_value_reification_name(): 280 | example = ReflectivityExample() 281 | node = value_name_reification_sample_node() 282 | 283 | working_example = ReflectivityExample() 284 | link_1 = MetaLink(example, 'tag_push', 'before', ['value']) 285 | link_2 = MetaLink(example, 'tag_push', 'before', ['old_value']) 286 | link_3 = MetaLink(example, 'tag_push', 'before', ['new_value']) 287 | 288 | reflectivipy.link(link_1, node) 289 | reflectivipy.link(link_2, node) 290 | reflectivipy.link(link_3, node) 291 | 292 | assert example.tag is None 293 | 294 | working_example.example_value_name_reification() 295 | assert len(example.tagged_reifications) == 3 296 | assert example.tagged_reifications[0] == 1 297 | assert example.tagged_reifications[1] == 1 298 | assert example.tagged_reifications[2] == 1 299 | 300 | 301 | def test_old_value_with_call_reification(): 302 | example = ReflectivityExample() 303 | node = value_call_reification_sample_node() 304 | 305 | working_example = ReflectivityExample() 306 | link = MetaLink(example, 'tag_exec', 'before', ['old_value']) 307 | 308 | reflectivipy.link(link, node) 309 | assert example.tag is None 310 | 311 | working_example.example_value_call_reification() 312 | assert example.tag == 1 313 | 314 | 315 | def test_new_value_with_call_reification(): 316 | example = ReflectivityExample() 317 | node = value_call_reification_sample_node() 318 | 319 | working_example = ReflectivityExample() 320 | link = MetaLink(example, 'tag_exec', 'before', ['new_value']) 321 | 322 | reflectivipy.link(link, node) 323 | assert example.tag is None 324 | 325 | working_example.example_value_call_reification() 326 | assert example.tag == 2 327 | 328 | 329 | def test_value_with_call_reification(): 330 | example = ReflectivityExample() 331 | node = value_call_reification_sample_node() 332 | 333 | working_example = ReflectivityExample() 334 | link = MetaLink(example, 'tag_exec', 'before', ['value']) 335 | 336 | reflectivipy.link(link, node) 337 | assert example.tag is None 338 | 339 | working_example.example_value_call_reification() 340 | assert example.tag == 1 341 | 342 | 343 | def test_value_with_call_call_reification(): 344 | example = ReflectivityExample() 345 | node = value_call_call_reification_sample_node() 346 | 347 | working_example = ReflectivityExample() 348 | 349 | link_1 = MetaLink(example, 'tag_push', 'before', ['value']) 350 | link_2 = MetaLink(example, 'tag_push', 'before', ['old_value']) 351 | link_3 = MetaLink(example, 'tag_push', 'before', ['new_value']) 352 | 353 | reflectivipy.link(link_1, node) 354 | reflectivipy.link(link_2, node) 355 | reflectivipy.link(link_3, node) 356 | 357 | assert example.tag is None 358 | 359 | working_example.example_value_call_call_reification() 360 | assert len(example.tagged_reifications) == 3 361 | print(example.tagged_reifications) 362 | assert example.tagged_reifications[0] == 1 363 | assert example.tagged_reifications[1] == 1 364 | assert example.tagged_reifications[2] == 2 365 | 366 | 367 | def test_named_argument_reification_method(): 368 | pass 369 | 370 | 371 | def test_argument_as_array_reification_method(): 372 | example = ReflectivityExample() 373 | method_node = method_with_args_sample_node() 374 | 375 | link = MetaLink(example, 'tag_exec', 'before', ['arguments']) 376 | reflectivipy.link(link, method_node) 377 | 378 | assert example.tag is None 379 | 380 | res = ReflectivityExample().method_with_args(1, 2, 3) 381 | assert res == 7 382 | 383 | tab = example.tag 384 | assert len(tab) == 3 385 | assert tab[0] == 1 386 | assert tab[1] == 2 387 | assert tab[2] == 3 388 | 389 | 390 | def test_named_argument_reification_call(): 391 | pass 392 | 393 | 394 | def test_argument_as_array_reification_call(): 395 | example = ReflectivityExample() 396 | call_node = method_with_args_sample_node().body[0].body[0].value 397 | 398 | link = MetaLink(example, 'tag_exec', 'before', ['arguments']) 399 | reflectivipy.link(link, call_node) 400 | 401 | assert example.tag is None 402 | 403 | res = ReflectivityExample().method_with_args(1, 2, 3) 404 | assert res == 7 405 | 406 | tab = example.tag 407 | assert len(tab) == 1 408 | assert tab[0] == 2 409 | 410 | 411 | def test_option_args_as_array_reification_call(): 412 | example = ReflectivityExample() 413 | call_node = method_with_args_sample_node().body[0].body[0].value 414 | 415 | link = MetaLink(example, 'tag_exec', 'before', ['arguments', 'link', 'class']) 416 | link.option_arg_as_array = True 417 | reflectivipy.link(link, call_node) 418 | 419 | assert example.tag is None 420 | 421 | res = ReflectivityExample().method_with_args(1, 2, 3) 422 | assert res == 7 423 | 424 | tab = example.tag 425 | assert len(tab) == 3 426 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = 3 | py3 4 | flake8 5 | 6 | [testenv] 7 | commands = 8 | coverage run --source reflectivipy -m pytest -v --capture=sys --pdb 9 | coverage report -m 10 | deps = 11 | coverage 12 | pytest 13 | setuptools 14 | 15 | [testenv:flake8] 16 | commands = 17 | flake8 --max-line-length=100 reflectivipy setup.py 18 | deps = 19 | flake8 20 | --------------------------------------------------------------------------------