|
8 | 8 | import ast
|
9 | 9 | from six import string_types
|
10 | 10 | from six.moves import builtins
|
11 |
| -from numbers import Number |
12 | 11 |
|
13 | 12 | from bpython import line as line_properties
|
14 | 13 | from bpython._py3compat import py3
|
@@ -136,14 +135,16 @@ def find_attribute_with_name(node, name):
|
136 | 135 | return r
|
137 | 136 |
|
138 | 137 |
|
139 |
| -def evaluate_current_expression(cursor_offset, line, namespace={}): |
| 138 | +def evaluate_current_expression(cursor_offset, line, namespace=None): |
140 | 139 | """
|
141 |
| - Return evaluted expression to the right of the dot of current attribute. |
| 140 | + Return evaluated expression to the right of the dot of current attribute. |
142 | 141 |
|
143 | 142 | build asts from with increasing numbers of characters.
|
144 | 143 | Find the biggest valid ast.
|
145 | 144 | Once our attribute access is a subtree, stop
|
146 | 145 | """
|
| 146 | + if namespace is None: |
| 147 | + namespace = {} |
147 | 148 |
|
148 | 149 | # in case attribute is blank, e.g. foo.| -> foo.xxx|
|
149 | 150 | temp_line = line[:cursor_offset] + 'xxx' + line[cursor_offset:]
|
@@ -174,3 +175,17 @@ def parse_trees(cursor_offset, line):
|
174 | 175 | return simple_eval(largest_ast, namespace)
|
175 | 176 | except ValueError:
|
176 | 177 | raise EvaluationError("Could not safely evaluate")
|
| 178 | + |
| 179 | + |
| 180 | +def evaluate_current_attribute(cursor_offset, line, namespace=None): |
| 181 | + # this function runs user code in case of custom descriptors, |
| 182 | + # so could fail in any way |
| 183 | + |
| 184 | + obj = evaluate_current_expression(cursor_offset, line, namespace) |
| 185 | + attr = line_properties.current_expression_attribute(cursor_offset, line) |
| 186 | + if attr is None: |
| 187 | + raise EvaluationError("No attribute found to look up") |
| 188 | + try: |
| 189 | + return getattr(obj, attr.word) |
| 190 | + except AttributeError: |
| 191 | + raise EvaluationError("can't lookup attribute %s on %r" % (attr.word, obj)) |
0 commit comments