eric6/DebugClients/Python/DebugVariables.py

branch
maintenance
changeset 8273
698ae46f40a4
parent 8043
0acf98cd089a
parent 8243
cc717c2ae956
child 8576
fe1957c69854
equal deleted inserted replaced
8190:fb0ef164f536 8273:698ae46f40a4
5 5
6 """ 6 """
7 Module implementing classes and functions to dump variable contents. 7 Module implementing classes and functions to dump variable contents.
8 """ 8 """
9 9
10 import contextlib
11
10 from DebugConfig import ConfigQtNames, ConfigKnownQtTypes, BatchSize 12 from DebugConfig import ConfigQtNames, ConfigKnownQtTypes, BatchSize
11 13
12 # 14 #
13 # This code was inspired by pydevd. 15 # This code was inspired by pydevd.
14 # 16 #
16 ############################################################ 18 ############################################################
17 ## Classes implementing resolvers for various compund types 19 ## Classes implementing resolvers for various compund types
18 ############################################################ 20 ############################################################
19 21
20 22
21 class BaseResolver(object): 23 class BaseResolver:
22 """ 24 """
23 Base class of the resolver class tree. 25 Base class of the resolver class tree.
24 """ 26 """
25 def resolve(self, var, attribute): 27 def resolve(self, var, attribute):
26 """ 28 """
48 if not names and hasattr(var, "__members__"): 50 if not names and hasattr(var, "__members__"):
49 names = var.__members__ 51 names = var.__members__
50 52
51 d = {} 53 d = {}
52 for name in names: 54 for name in names:
53 try: 55 with contextlib.suppress(Exception):
54 attribute = getattr(var, name) 56 attribute = getattr(var, name)
55 d[name] = attribute 57 d[name] = attribute
56 except Exception: # secok
57 pass # if we can't get it, simply ignore it
58 58
59 return d 59 return d
60 60
61 61
62 ############################################################ 62 ############################################################
82 if not names and hasattr(var, "__members__"): 82 if not names and hasattr(var, "__members__"):
83 names = var.__members__ 83 names = var.__members__
84 84
85 d = {} 85 d = {}
86 for name in names: 86 for name in names:
87 try: 87 with contextlib.suppress(Exception):
88 attribute = getattr(var, name) 88 attribute = getattr(var, name)
89 d[name] = attribute 89 d[name] = attribute
90 except Exception: # secok
91 pass # if we can't get it, simply ignore it
92 90
93 yield -1, d 91 yield -1, d
94 while True: 92 while True:
95 yield -2, {} 93 yield -2, {}
96 94
178 176
179 if d: 177 if d:
180 yield start, d 178 yield start, d
181 179
182 # in case it has additional fields 180 # in case it has additional fields
183 d = super(DictResolver, self).getDictionary(var) 181 d = super().getDictionary(var)
184 yield -1, d 182 yield -1, d
185 183
186 while True: 184 while True:
187 yield -2, {} 185 yield -2, {}
188 186
235 233
236 if d: 234 if d:
237 yield start, d 235 yield start, d
238 236
239 # in case it has additional fields 237 # in case it has additional fields
240 d = super(ListResolver, self).getDictionary(var) 238 d = super().getDictionary(var)
241 yield -1, d 239 yield -1, d
242 240
243 while True: 241 while True:
244 yield -2, {} 242 yield -2, {}
245 243
262 @param attribute id of the value to extract 260 @param attribute id of the value to extract
263 @type str 261 @type str
264 @return value of the attribute 262 @return value of the attribute
265 @rtype any 263 @rtype any
266 """ 264 """
267 return super(DictViewResolver, self).resolve(list(var), attribute) 265 return super().resolve(list(var), attribute)
268 266
269 def getDictionary(self, var): 267 def getDictionary(self, var):
270 """ 268 """
271 Public method to get the attributes of a variable as a dictionary. 269 Public method to get the attributes of a variable as a dictionary.
272 270
273 @param var variable to be converted 271 @param var variable to be converted
274 @type any 272 @type any
275 @return dictionary containing the variable attributes 273 @return dictionary containing the variable attributes
276 @rtype dict 274 @rtype dict
277 """ 275 """
278 return super(DictViewResolver, self).getDictionary(list(var)) 276 return super().getDictionary(list(var))
279 277
280 278
281 ############################################################ 279 ############################################################
282 ## Resolver for Sets and Frozensets 280 ## Resolver for Sets and Frozensets
283 ############################################################ 281 ############################################################
334 332
335 if d: 333 if d:
336 yield start, d 334 yield start, d
337 335
338 # in case it has additional fields 336 # in case it has additional fields
339 additionals = super(SetResolver, self).getDictionary(var) 337 additionals = super().getDictionary(var)
340 yield -1, additionals 338 yield -1, additionals
341 339
342 while True: 340 while True:
343 yield -2, {} 341 yield -2, {}
344 342
431 429
432 if d: 430 if d:
433 yield start, d 431 yield start, d
434 432
435 # in case it has additional fields 433 # in case it has additional fields
436 d = super(NdArrayResolver, self).getDictionary(var) 434 d = super().getDictionary(var)
437 435
438 if var.size > 1024 * 1024: 436 if var.size > 1024 * 1024:
439 d['min'] = ( 437 d['min'] = (
440 'ndarray too big, calculating min would slow down debugging') 438 'ndarray too big, calculating min would slow down debugging')
441 d['max'] = ( 439 d['max'] = (
487 return var[attribute] 485 return var[attribute]
488 except Exception: 486 except Exception:
489 return getattr(var, attribute, None) 487 return getattr(var, attribute, None)
490 488
491 expectedID = int(attribute.split(" (ID:")[-1][:-1]) 489 expectedID = int(attribute.split(" (ID:")[-1][:-1])
492 for key in var.keys(): 490 for key in var:
493 if id(key) == expectedID: 491 if id(key) == expectedID:
494 return var.getlist(key) 492 return var.getlist(key)
495 493
496 return None 494 return None
497 495
527 525
528 if d: 526 if d:
529 yield start, d 527 yield start, d
530 528
531 # in case it has additional fields 529 # in case it has additional fields
532 d = super(DictResolver, self).getDictionary(var) 530 d = super().getDictionary(var)
533 yield -1, d 531 yield -1, d
534 532
535 while True: 533 while True:
536 yield -2, {} 534 yield -2, {}
537 535
604 602
605 if d: 603 if d:
606 yield start, d 604 yield start, d
607 605
608 # in case it has additional fields 606 # in case it has additional fields
609 d = super(ArrayResolver, self).getDictionary(var) 607 d = super().getDictionary(var)
610 608
611 # Special data for array type: convert typecode to readable text 609 # Special data for array type: convert typecode to readable text
612 d['type'] = self.TypeCodeMap.get(var.typecode, 'illegal type') 610 d['type'] = self.TypeCodeMap.get(var.typecode, 'illegal type')
613 611
614 yield -1, d 612 yield -1, d
651 (dict, dictResolver), 649 (dict, dictResolver),
652 (set, setResolver), 650 (set, setResolver),
653 (frozenset, setResolver), 651 (frozenset, setResolver),
654 ] 652 ]
655 653
656 try: 654 with contextlib.suppress(Exception):
657 _TypeMap.append((long, None)) # __IGNORE_WARNING__ 655 _TypeMap.append((long, None)) # __IGNORE_WARNING__
658 except Exception: # secok 656
659 pass # not available on all Python versions 657 with contextlib.suppress(ImportError):
660
661 try:
662 import array 658 import array
663 _TypeMap.append((array.array, arrayResolver)) 659 _TypeMap.append((array.array, arrayResolver))
664 except ImportError: 660 # array.array may not be available
665 pass # array.array may not be available 661
666 662 with contextlib.suppress(ImportError):
667 try:
668 import numpy 663 import numpy
669 _TypeMap.append((numpy.ndarray, ndarrayResolver)) 664 _TypeMap.append((numpy.ndarray, ndarrayResolver))
670 except ImportError: 665 # numpy may not be installed
671 pass # numpy may not be installed 666
672 667 with contextlib.suppress(ImportError):
673 try:
674 from django.utils.datastructures import MultiValueDict 668 from django.utils.datastructures import MultiValueDict
675 # it should go before dict 669 # it should go before dict
676 _TypeMap.insert(0, (MultiValueDict, multiValueDictResolver)) 670 _TypeMap.insert(0, (MultiValueDict, multiValueDictResolver))
677 except ImportError: 671 # django may not be installed
678 pass # django may not be installed 672
679 673 with contextlib.suppress(ImportError):
680 try:
681 from collections.abc import ItemsView, KeysView, ValuesView 674 from collections.abc import ItemsView, KeysView, ValuesView
682 _TypeMap.append((ItemsView, dictViewResolver)) 675 _TypeMap.append((ItemsView, dictViewResolver))
683 _TypeMap.append((KeysView, dictViewResolver)) 676 _TypeMap.append((KeysView, dictViewResolver))
684 _TypeMap.append((ValuesView, dictViewResolver)) 677 _TypeMap.append((ValuesView, dictViewResolver))
685 except ImportError: 678 # not available on all Python versions
686 pass # not available on all Python versions
687 679
688 680
689 def getType(obj): 681 def getType(obj):
690 """ 682 """
691 Public method to get the type information for an object. 683 Public method to get the type information for an object.
715 break 707 break
716 else: 708 else:
717 resolver = defaultResolver 709 resolver = defaultResolver
718 710
719 return typeName, typeStr, resolver 711 return typeName, typeStr, resolver
712
713 #
714 # eflag: noqa = Y113

eric ide

mercurial