From 011810aa420f751492395c7251cf6e730b945b74 Mon Sep 17 00:00:00 2001 From: Fabio Zadrozny Date: Sun, 19 Apr 2020 13:42:27 -0300 Subject: [PATCH] Change __len__ to len(). --- .../pydevd/_pydevd_bundle/pydevd_constants.py | 1 + .../pydevd/_pydevd_bundle/pydevd_resolver.py | 36 +++++++++---------- .../_pydevd_bundle/pydevd_suspended_frames.py | 4 +-- .../pydevd/_pydevd_bundle/pydevd_utils.py | 6 ++-- .../pydevd/tests_python/test_debugger_json.py | 10 +++--- .../pydevd/tests_python/test_resolvers.py | 32 +++++++---------- .../test_suspended_frames_manager.py | 10 +++--- tests/debug/config.py | 7 +++- tests/debugpy/test_evaluate.py | 24 ++++++------- 9 files changed, 64 insertions(+), 66 deletions(-) diff --git a/src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_constants.py b/src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_constants.py index 254273b2..fbdea662 100644 --- a/src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_constants.py +++ b/src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_constants.py @@ -71,6 +71,7 @@ except AttributeError: MAXIMUM_VARIABLE_REPRESENTATION_SIZE = 1000 # Prefix for saving functions return values in locals RETURN_VALUES_DICT = '__pydevd_ret_val_dict' +GENERATED_LEN_ATTR_NAME = 'len()' import os diff --git a/src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_resolver.py b/src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_resolver.py index 0bcb7962..9279f262 100644 --- a/src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_resolver.py +++ b/src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_resolver.py @@ -9,7 +9,7 @@ from os.path import basename from functools import partial from _pydevd_bundle.pydevd_constants import dict_iter_items, dict_keys, xrange, IS_PY36_OR_GREATER, \ - MethodWrapperType, RETURN_VALUES_DICT, DebugInfoHolder, IS_PYPY + MethodWrapperType, RETURN_VALUES_DICT, DebugInfoHolder, IS_PYPY, GENERATED_LEN_ATTR_NAME from _pydevd_bundle.pydevd_safe_repr import SafeRepr # Note: 300 is already a lot to see in the outline (after that the user should really use the shell to get things) @@ -223,7 +223,7 @@ class DictResolver: sort_keys = not IS_PY36_OR_GREATER def resolve(self, dict, key): - if key in ('__len__', TOO_LARGE_ATTR): + if key in (GENERATED_LEN_ATTR_NAME, TOO_LARGE_ATTR): return None if '(' not in key: @@ -276,17 +276,17 @@ class DictResolver: ret.append((TOO_LARGE_ATTR, TOO_LARGE_MSG, None)) break - ret.append(('__len__', len(dct), partial(_apply_evaluate_name, evaluate_name='len(%s)'))) # in case the class extends built-in type and has some additional fields from_default_resolver = defaultResolver.get_contents_debug_adapter_protocol(dct, fmt) if from_default_resolver: ret = from_default_resolver + ret - if not self.sort_keys: - return ret + if self.sort_keys: + ret = sorted(ret, key=lambda tup: sorted_attributes_key(tup[0])) - return sorted(ret, key=lambda tup: sorted_attributes_key(tup[0])) + ret.append((GENERATED_LEN_ATTR_NAME, len(dct), partial(_apply_evaluate_name, evaluate_name='len(%s)'))) + return ret def get_dictionary(self, dict): ret = self.init_dict() @@ -304,7 +304,7 @@ class DictResolver: # in case if the class extends built-in type and has some additional fields additional_fields = defaultResolver.get_dictionary(dict) ret.update(additional_fields) - ret['__len__'] = len(dict) + ret[GENERATED_LEN_ATTR_NAME] = len(dict) return ret @@ -322,7 +322,7 @@ class TupleResolver: # to enumerate tuples and lists @param var: that's the original attribute @param attribute: that's the key passed in the dict (as a string) ''' - if attribute in ('__len__', TOO_LARGE_ATTR): + if attribute in (GENERATED_LEN_ATTR_NAME, TOO_LARGE_ATTR): return None try: return var[int(attribute)] @@ -353,11 +353,12 @@ class TupleResolver: # to enumerate tuples and lists ret.append((TOO_LARGE_ATTR, TOO_LARGE_MSG, None)) break - ret.append(('__len__', len(lst), partial(_apply_evaluate_name, evaluate_name='len(%s)'))) # Needed in case the class extends the built-in type and has some additional fields. from_default_resolver = defaultResolver.get_contents_debug_adapter_protocol(lst, fmt=fmt) if from_default_resolver: ret = from_default_resolver + ret + + ret.append((GENERATED_LEN_ATTR_NAME, len(lst), partial(_apply_evaluate_name, evaluate_name='len(%s)'))) return ret def get_dictionary(self, var, fmt={}): @@ -378,7 +379,7 @@ class TupleResolver: # to enumerate tuples and lists # in case if the class extends built-in type and has some additional fields additional_fields = defaultResolver.get_dictionary(var) d.update(additional_fields) - d['__len__'] = len(var) + d[GENERATED_LEN_ATTR_NAME] = len(var) return d @@ -400,15 +401,15 @@ class SetResolver: ret.append((TOO_LARGE_ATTR, TOO_LARGE_MSG, None)) break - ret.append(('__len__', len(obj), partial(_apply_evaluate_name, evaluate_name='len(%s)'))) # Needed in case the class extends the built-in type and has some additional fields. from_default_resolver = defaultResolver.get_contents_debug_adapter_protocol(obj, fmt=fmt) if from_default_resolver: ret = from_default_resolver + ret + ret.append((GENERATED_LEN_ATTR_NAME, len(obj), partial(_apply_evaluate_name, evaluate_name='len(%s)'))) return ret def resolve(self, var, attribute): - if attribute in ('__len__', TOO_LARGE_ATTR): + if attribute in (GENERATED_LEN_ATTR_NAME, TOO_LARGE_ATTR): return None try: @@ -434,7 +435,7 @@ class SetResolver: # in case if the class extends built-in type and has some additional fields additional_fields = defaultResolver.get_dictionary(var) d.update(additional_fields) - d['__len__'] = len(var) + d[GENERATED_LEN_ATTR_NAME] = len(var) return d def change_var_from_name(self, container, name, new_value): @@ -490,7 +491,7 @@ class JyArrayResolver: ''' def resolve(self, var, attribute): - if attribute == '__len__': + if attribute == GENERATED_LEN_ATTR_NAME: return None return var[int(attribute)] @@ -500,7 +501,7 @@ class JyArrayResolver: for i in xrange(len(obj)): ret[ i ] = obj[i] - ret['__len__'] = len(obj) + ret[GENERATED_LEN_ATTR_NAME] = len(obj) return ret @@ -510,7 +511,7 @@ class JyArrayResolver: class MultiValueDictResolver(DictResolver): def resolve(self, dict, key): - if key in ('__len__', TOO_LARGE_ATTR): + if key in (GENERATED_LEN_ATTR_NAME, TOO_LARGE_ATTR): return None # ok, we have to iterate over the items to find the one that matches the id, because that's the only way @@ -660,8 +661,7 @@ def get_var_scope(attr_name, attr_value, evaluate_name, handle_return_values): if handle_return_values and attr_name == RETURN_VALUES_DICT: return '' - elif attr_name == '__len__' and evaluate_name != '.__len__': - # Treat the __len__ we generate internally separate from the __len__ function + elif attr_name == GENERATED_LEN_ATTR_NAME: return '' if attr_name.startswith('__') and attr_name.endswith('__'): diff --git a/src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_suspended_frames.py b/src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_suspended_frames.py index 6271a204..6edbed9a 100644 --- a/src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_suspended_frames.py +++ b/src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_suspended_frames.py @@ -2,7 +2,7 @@ from contextlib import contextmanager import sys from _pydevd_bundle.pydevd_constants import get_frame, dict_items, RETURN_VALUES_DICT, \ - dict_iter_items, ForkSafeLock + dict_iter_items, ForkSafeLock, GENERATED_LEN_ATTR_NAME from _pydevd_bundle.pydevd_xml import get_variable_details, get_type from _pydev_bundle.pydev_override import overrides from _pydevd_bundle.pydevd_resolver import sorted_attributes_key, TOO_LARGE_ATTR, get_var_scope @@ -61,7 +61,7 @@ class _AbstractVariable(object): attributes.append('readOnly') name = '(return) %s' % (name,) - elif name in (TOO_LARGE_ATTR, '__len__'): + elif name in (TOO_LARGE_ATTR, GENERATED_LEN_ATTR_NAME): attributes.append('readOnly') var_data = { diff --git a/src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_utils.py b/src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_utils.py index e9a1ffe9..725dacda 100644 --- a/src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_utils.py +++ b/src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_utils.py @@ -11,7 +11,7 @@ except: import inspect import sys from _pydevd_bundle.pydevd_constants import IS_PY3K, USE_CUSTOM_SYS_CURRENT_FRAMES, IS_PYPY, SUPPORT_GEVENT, \ - GEVENT_SUPPORT_NOT_SET_MSG + GEVENT_SUPPORT_NOT_SET_MSG, GENERATED_LEN_ATTR_NAME from _pydev_imps._pydev_saved_modules import threading @@ -76,11 +76,11 @@ def to_number(x): def compare_object_attrs_key(x): - if '__len__' == x: + if GENERATED_LEN_ATTR_NAME == x: as_number = to_number(x) if as_number is None: as_number = 99999999 - # __len__ should appear after other attributes in a list. + # len() should appear after other attributes in a list. return (1, as_number) else: return (-1, to_string(x)) diff --git a/src/debugpy/_vendored/pydevd/tests_python/test_debugger_json.py b/src/debugpy/_vendored/pydevd/tests_python/test_debugger_json.py index 56e4f059..583fd2f7 100644 --- a/src/debugpy/_vendored/pydevd/tests_python/test_debugger_json.py +++ b/src/debugpy/_vendored/pydevd/tests_python/test_debugger_json.py @@ -17,7 +17,7 @@ from _pydevd_bundle._debug_adapter.pydevd_schema import (ThreadEvent, ModuleEven from _pydevd_bundle.pydevd_comm_constants import file_system_encoding from _pydevd_bundle.pydevd_constants import (int_types, IS_64BIT_PROCESS, PY_VERSION_STR, PY_IMPL_VERSION_STR, PY_IMPL_NAME, IS_PY36_OR_GREATER, IS_PY39_OR_GREATER, - IS_PY37_OR_GREATER, IS_PYPY) + IS_PY37_OR_GREATER, IS_PYPY, GENERATED_LEN_ATTR_NAME) from tests_python import debugger_unittest from tests_python.debug_constants import TEST_CHERRYPY, IS_PY2, TEST_DJANGO, TEST_FLASK, IS_PY26, \ IS_PY27, IS_CPYTHON, TEST_GEVENT @@ -1223,7 +1223,7 @@ def test_dict_ordered(case_setup): variables_response = json_facade.get_variables_response(ref) assert [(d['name'], d['value']) for d in variables_response.body.variables if (not d['name'].startswith('_OrderedDict')) and (d['name'] not in DAPGrouper.SCOPES_SORTED)] == [ - ('4', "'first'"), ('3', "'second'"), ('2', "'last'"), ('__len__', '3')] + ('4', "'first'"), ('3', "'second'"), ('2', "'last'"), (GENERATED_LEN_ATTR_NAME, '3')] json_facade.write_continue() writer.finished_ok = True @@ -1276,7 +1276,7 @@ def test_stack_and_variables_dict(case_setup): assert check == [ {'name': "'a'", 'value': '30', 'type': 'int', 'evaluateName': "variable_for_test_3['a']", 'variablesReference': 0 }, {'name': "'b'", 'value': '20', 'type': 'int', 'evaluateName': "variable_for_test_3['b']", 'variablesReference': 0}, - {'name': '__len__', 'value': '2', 'type': 'int', 'evaluateName': 'len(variable_for_test_3)', 'variablesReference': 0, 'presentationHint': {'attributes': ['readOnly']}} + {'name': GENERATED_LEN_ATTR_NAME, 'value': '2', 'type': 'int', 'evaluateName': 'len(variable_for_test_3)', 'variablesReference': 0, 'presentationHint': {'attributes': ['readOnly']}} ] json_facade.write_continue() @@ -1480,7 +1480,7 @@ def test_stack_and_variables_set_and_list(case_setup): u'variablesReference': 0, }, { - u'name': u'__len__', + u'name': GENERATED_LEN_ATTR_NAME, u'type': u'int', u'value': u'2', u'evaluateName': u'len(variable_for_test_1)', @@ -1683,7 +1683,7 @@ def test_evaluate_variable_references(case_setup): 'variablesReference': 0, }, { - 'name': '__len__', + 'name': GENERATED_LEN_ATTR_NAME, 'type': 'int', 'value': '1', 'presentationHint': {'attributes': ['readOnly']}, diff --git a/src/debugpy/_vendored/pydevd/tests_python/test_resolvers.py b/src/debugpy/_vendored/pydevd/tests_python/test_resolvers.py index 35fb7fea..033012e2 100644 --- a/src/debugpy/_vendored/pydevd/tests_python/test_resolvers.py +++ b/src/debugpy/_vendored/pydevd/tests_python/test_resolvers.py @@ -1,5 +1,5 @@ from tests_python.debug_constants import IS_PY2 -from _pydevd_bundle.pydevd_constants import IS_PY36_OR_GREATER +from _pydevd_bundle.pydevd_constants import IS_PY36_OR_GREATER, GENERATED_LEN_ATTR_NAME def check_len_entry(len_entry, first_2_params): @@ -14,7 +14,7 @@ def test_dict_resolver(): dct = {(1, 2): 2, u'22': 22} contents_debug_adapter_protocol = clear_contents_debug_adapter_protocol(dict_resolver.get_contents_debug_adapter_protocol(dct)) len_entry = contents_debug_adapter_protocol.pop(-1) - check_len_entry(len_entry, ('__len__', 2)) + check_len_entry(len_entry, (GENERATED_LEN_ATTR_NAME, 2)) if IS_PY36_OR_GREATER: assert contents_debug_adapter_protocol == [ ('(1, 2)', 2, '[(1, 2)]'), ("'22'", 22, "['22']")] @@ -34,7 +34,7 @@ def test_dict_resolver_hex(): contents_debug_adapter_protocol = clear_contents_debug_adapter_protocol( dict_resolver.get_contents_debug_adapter_protocol(dct, fmt={'hex': True})) len_entry = contents_debug_adapter_protocol.pop(-1) - check_len_entry(len_entry, ('__len__', 1)) + check_len_entry(len_entry, (GENERATED_LEN_ATTR_NAME, 1)) assert contents_debug_adapter_protocol == [ ('(0x1, 0xa, 0x64)', (10000, 100000, 100000), '[(1, 10, 100)]'), ] @@ -166,12 +166,6 @@ def test_django_forms_resolver(): def clear_contents_debug_adapter_protocol(contents_debug_adapter_protocol): lst = [] for x in contents_debug_adapter_protocol: - if x[0] == '__len__': - if x[2] == '.__len__': - # i.e.: remove a builtin __len__ method, but not the __len__ we add with the length. - continue - lst.append(x) - if not x[0].startswith('__'): if '= (3, 6): - # Note that the special __len__ we manually create is not added to special variables. + # Note that the special len() we manually create is not added to special variables. expected = [ "special variables", "function variables", "'spam'", "'eggs'", "'abcd'", - "__len__", + "len()", ] else: expected = [ @@ -195,7 +195,7 @@ def test_variable_sort(pyfile, target, run): "'abcd'", "'eggs'", "'spam'", - "__len__", + "len()", ] assert var_names == expected @@ -207,8 +207,8 @@ def test_variable_sort(pyfile, target, run): "variables", {"variablesReference": c_test["variablesReference"]} )["variables"] var_names = [v["name"] for v in c_test_vars] - # Note that the special __len__ we manually create is not added to special variables. - expected = ["1", "2", "10", "__len__"] + # Note that the special len() we manually create is not added to special variables. + expected = ["1", "2", "10", "len()"] assert var_names == expected @@ -417,7 +417,7 @@ def test_hex_numbers(pyfile, target, run): ), some.dict.containing( { - "name": "__len__", + "name": "len()", "value": "0x3", "type": "int", "evaluateName": "len(b)", @@ -463,7 +463,7 @@ def test_hex_numbers(pyfile, target, run): ), some.dict.containing( { - "name": "__len__", + "name": "len()", "value": "0x3", "type": "int", "evaluateName": "len(c)", @@ -504,7 +504,7 @@ def test_hex_numbers(pyfile, target, run): ), some.dict.containing( { - "name": "__len__", + "name": "len()", "value": "0x3", "type": "int", "evaluateName": "len(c)", @@ -530,7 +530,7 @@ def test_hex_numbers(pyfile, target, run): ), some.dict.containing( { - "name": "__len__", + "name": "len()", "value": "0x1", "type": "int", "evaluateName": "len(d)", @@ -578,7 +578,7 @@ def test_hex_numbers(pyfile, target, run): ), some.dict.containing( { - "name": "__len__", + "name": "len()", "value": "0x3", "type": "int", "evaluateName": "len(d[(1, 10, 100)])",