|
1 | 1 | import math |
2 | 2 | import json |
3 | | -import weakref |
4 | 3 |
|
5 | 4 | from jmespath import exceptions |
6 | 5 | from jmespath.compat import string_type as STRING_TYPE |
@@ -60,24 +59,6 @@ class RuntimeFunctions(object): |
60 | 59 | FUNCTION_TABLE = { |
61 | 60 | } |
62 | 61 |
|
63 | | - def __init__(self): |
64 | | - self._interpreter = None |
65 | | - |
66 | | - @property |
67 | | - def interpreter(self): |
68 | | - if self._interpreter is None: |
69 | | - return None |
70 | | - else: |
71 | | - return self._interpreter() |
72 | | - |
73 | | - @interpreter.setter |
74 | | - def interpreter(self, value): |
75 | | - # A weakref is used because we have |
76 | | - # a cyclic reference and we want to allow |
77 | | - # for the memory to be properly freed when |
78 | | - # the objects are no longer needed. |
79 | | - self._interpreter = weakref.ref(value) |
80 | | - |
81 | 62 | def call_function(self, function_name, resolved_args): |
82 | 63 | try: |
83 | 64 | spec = self.FUNCTION_TABLE[function_name] |
@@ -255,7 +236,7 @@ def _func_join(self, separator, array): |
255 | 236 | def _func_map(self, expref, arg): |
256 | 237 | result = [] |
257 | 238 | for element in arg: |
258 | | - result.append(self.interpreter.visit(expref.expression, element)) |
| 239 | + result.append(expref.visit(expref.expression, element)) |
259 | 240 | return result |
260 | 241 |
|
261 | 242 | @builtin_function({"types": ['array-number', 'array-string']}) |
@@ -323,34 +304,32 @@ def _func_sort_by(self, array, expref): |
323 | 304 | # that validates that type, which requires that remaining array |
324 | 305 | # elements resolve to the same type as the first element. |
325 | 306 | required_type = self._convert_to_jmespath_type( |
326 | | - type(self.interpreter.visit(expref.expression, array[0])).__name__) |
| 307 | + type(expref.visit(expref.expression, array[0])).__name__) |
327 | 308 | if required_type not in ['number', 'string']: |
328 | 309 | raise exceptions.JMESPathTypeError( |
329 | 310 | 'sort_by', array[0], required_type, ['string', 'number']) |
330 | | - keyfunc = self._create_key_func(expref.expression, |
| 311 | + keyfunc = self._create_key_func(expref, |
331 | 312 | [required_type], |
332 | 313 | 'sort_by') |
333 | 314 | return list(sorted(array, key=keyfunc)) |
334 | 315 |
|
335 | 316 | @builtin_function({'types': ['array']}, {'types': ['expref']}) |
336 | 317 | def _func_min_by(self, array, expref): |
337 | | - keyfunc = self._create_key_func(expref.expression, |
| 318 | + keyfunc = self._create_key_func(expref, |
338 | 319 | ['number', 'string'], |
339 | 320 | 'min_by') |
340 | 321 | return min(array, key=keyfunc) |
341 | 322 |
|
342 | 323 | @builtin_function({'types': ['array']}, {'types': ['expref']}) |
343 | 324 | def _func_max_by(self, array, expref): |
344 | | - keyfunc = self._create_key_func(expref.expression, |
| 325 | + keyfunc = self._create_key_func(expref, |
345 | 326 | ['number', 'string'], |
346 | 327 | 'min_by') |
347 | 328 | return max(array, key=keyfunc) |
348 | 329 |
|
349 | | - def _create_key_func(self, expr_node, allowed_types, function_name): |
350 | | - interpreter = self.interpreter |
351 | | - |
| 330 | + def _create_key_func(self, expref, allowed_types, function_name): |
352 | 331 | def keyfunc(x): |
353 | | - result = interpreter.visit(expr_node, x) |
| 332 | + result = expref.visit(expref.expression, x) |
354 | 333 | actual_typename = type(result).__name__ |
355 | 334 | jmespath_type = self._convert_to_jmespath_type(actual_typename) |
356 | 335 | # allowed_types is in term of jmespath types, not python types. |
|
0 commit comments