diff options
author | Daniel Holth <dholth@fastmail.fm> | 2012-08-27 18:54:29 -0400 |
---|---|---|
committer | Daniel Holth <dholth@fastmail.fm> | 2012-08-27 18:54:29 -0400 |
commit | 29a8409a283da240a25e0eb525b9234a585837f5 (patch) | |
tree | 95e75c7fa6fba12bc76e732668c6d8aed4796d26 /_markerlib | |
parent | d86645946ee23972d2ab298970d3069bd2e24eeb (diff) | |
download | python-setuptools-bitbucket-29a8409a283da240a25e0eb525b9234a585837f5.tar.gz |
impractical to support _markerlib on Python < 2.6 (no compile(ast))
Diffstat (limited to '_markerlib')
-rw-r--r-- | _markerlib/__init__.py | 1 | ||||
-rw-r--r-- | _markerlib/_markers_ast.py | 132 | ||||
-rw-r--r-- | _markerlib/markers.py | 12 |
3 files changed, 4 insertions, 141 deletions
diff --git a/_markerlib/__init__.py b/_markerlib/__init__.py index ef8c994c..a7b26037 100644 --- a/_markerlib/__init__.py +++ b/_markerlib/__init__.py @@ -1 +1,2 @@ +"""Used by pkg_resources to interpret PEP 345 environment markers.""" from _markerlib.markers import default_environment, compile, interpret, as_function diff --git a/_markerlib/_markers_ast.py b/_markerlib/_markers_ast.py deleted file mode 100644 index b7a5e0b9..00000000 --- a/_markerlib/_markers_ast.py +++ /dev/null @@ -1,132 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Just enough of ast.py for markers.py -""" - -from _ast import AST, PyCF_ONLY_AST - -def parse(source, filename='<unknown>', mode='exec'): - """ - Parse the source into an AST node. - Equivalent to compile(source, filename, mode, PyCF_ONLY_AST). - """ - return compile(source, filename, mode, PyCF_ONLY_AST) - -def copy_location(new_node, old_node): - """ - Copy source location (`lineno` and `col_offset` attributes) from - *old_node* to *new_node* if possible, and return *new_node*. - """ - for attr in 'lineno', 'col_offset': - if attr in old_node._attributes and attr in new_node._attributes \ - and hasattr(old_node, attr): - setattr(new_node, attr, getattr(old_node, attr)) - return new_node - -def iter_fields(node): - """ - Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields`` - that is present on *node*. - """ - for field in node._fields: - try: - yield field, getattr(node, field) - except AttributeError: - pass - -class NodeVisitor(object): - """ - A node visitor base class that walks the abstract syntax tree and calls a - visitor function for every node found. This function may return a value - which is forwarded by the `visit` method. - - This class is meant to be subclassed, with the subclass adding visitor - methods. - - Per default the visitor functions for the nodes are ``'visit_'`` + - class name of the node. So a `TryFinally` node visit function would - be `visit_TryFinally`. This behavior can be changed by overriding - the `visit` method. If no visitor function exists for a node - (return value `None`) the `generic_visit` visitor is used instead. - - Don't use the `NodeVisitor` if you want to apply changes to nodes during - traversing. For this a special visitor exists (`NodeTransformer`) that - allows modifications. - """ - - def visit(self, node): - """Visit a node.""" - method = 'visit_' + node.__class__.__name__ - visitor = getattr(self, method, self.generic_visit) - return visitor(node) - -# def generic_visit(self, node): -# """Called if no explicit visitor function exists for a node.""" -# for field, value in iter_fields(node): -# if isinstance(value, list): -# for item in value: -# if isinstance(item, AST): -# self.visit(item) -# elif isinstance(value, AST): -# self.visit(value) - - -class NodeTransformer(NodeVisitor): - """ - A :class:`NodeVisitor` subclass that walks the abstract syntax tree and - allows modification of nodes. - - The `NodeTransformer` will walk the AST and use the return value of the - visitor methods to replace or remove the old node. If the return value of - the visitor method is ``None``, the node will be removed from its location, - otherwise it is replaced with the return value. The return value may be the - original node in which case no replacement takes place. - - Here is an example transformer that rewrites all occurrences of name lookups - (``foo``) to ``data['foo']``:: - - class RewriteName(NodeTransformer): - - def visit_Name(self, node): - return copy_location(Subscript( - value=Name(id='data', ctx=Load()), - slice=Index(value=Str(s=node.id)), - ctx=node.ctx - ), node) - - Keep in mind that if the node you're operating on has child nodes you must - either transform the child nodes yourself or call the :meth:`generic_visit` - method for the node first. - - For nodes that were part of a collection of statements (that applies to all - statement nodes), the visitor may also return a list of nodes rather than - just a single node. - - Usually you use the transformer like this:: - - node = YourTransformer().visit(node) - """ - - def generic_visit(self, node): - for field, old_value in iter_fields(node): - old_value = getattr(node, field, None) - if isinstance(old_value, list): - new_values = [] - for value in old_value: - if isinstance(value, AST): - value = self.visit(value) - if value is None: - continue - elif not isinstance(value, AST): - new_values.extend(value) - continue - new_values.append(value) - old_value[:] = new_values - elif isinstance(old_value, AST): - new_node = self.visit(old_value) - if new_node is None: - delattr(node, field) - else: - setattr(node, field, new_node) - return node -
\ No newline at end of file diff --git a/_markerlib/markers.py b/_markerlib/markers.py index 293adf72..54c2828a 100644 --- a/_markerlib/markers.py +++ b/_markerlib/markers.py @@ -12,17 +12,13 @@ where EXPR belongs to any of those: platform.version = platform.version() platform.machine = platform.machine() platform.python_implementation = platform.python_implementation() - a free string, like '2.4', or 'win32' + a free string, like '2.6', or 'win32' """ __all__ = ['default_environment', 'compile', 'interpret'] -# Would import from ast but for Python 2.5 -from _ast import Compare, BoolOp, Attribute, Name, Load, Str, cmpop, boolop -try: - from ast import parse, copy_location, NodeTransformer -except ImportError: # pragma no coverage - from markerlib._markers_ast import parse, copy_location, NodeTransformer +from ast import Compare, BoolOp, Attribute, Name, Load, Str, cmpop, boolop +from ast import parse, copy_location, NodeTransformer import os import platform @@ -104,7 +100,5 @@ def compile(marker): _cache[marker] = marker_fn return _cache[marker] -as_function = compile # bw compat - def interpret(marker, environment=None): return compile(marker)(environment) |