DebugClients/Python3/DebugVariables.py

changeset 5190
65a2234c6789
parent 5175
9db0b0f15d12
equal deleted inserted replaced
5189:8fa3e3d379d1 5190:65a2234c6789
472 d["___len___"] = len(var) 472 d["___len___"] = len(var)
473 473
474 return d 474 return d
475 475
476 476
477 ############################################################
478 ## Resolver for array.array
479 ############################################################
480
481
482 class ArrayResolver(BaseResolver):
483 """
484 Class used to resolve from array.array including some meta data.
485 """
486 TypeCodeMap = {
487 "b": "int (signed char)",
488 "B": "int (unsigned char)",
489 "u": "Unicode character (Py_UNICODE)",
490 "h": "int (signed short)",
491 "H": "int (unsigned short)",
492 "i": "int (signed int)",
493 "I": "int (unsigned int)",
494 "l": "int (signed long)",
495 "L": "int (unsigned long)",
496 "q": "int (signed long long)",
497 "Q": "int (unsigned long long)",
498 "f": "float (float)",
499 "d": "float (double)",
500 }
501 def resolve(self, var, attribute):
502 """
503 Public method to get an attribute from a variable.
504
505 @param var variable to extract an attribute or value from
506 @type tuple or list
507 @param attribute id of the value to extract
508 @type str
509 @return value of the attribute
510 @rtype any
511 """
512 if attribute == 'itemsize':
513 return var.itemsize
514
515 if attribute == 'typecode':
516 return var.typecode
517
518 if attribute == 'type':
519 if var.typecode in ArrayResolver.TypeCodeMap:
520 return ArrayResolver.TypeCodeMap[var.typecode]
521 else:
522 return 'illegal type'
523
524 if attribute.startswith('['):
525 container = ArrayItemsContainer()
526 count = 0
527 for element in var:
528 setattr(container, str(count), element)
529 count += 1
530 if count > MaxItemsToHandle:
531 setattr(container, TooLargeAttribute, TooLargeMessage)
532 break
533 return container
534
535 return None
536
537 def getDictionary(self, var):
538 """
539 Public method to get the attributes of a variable as a dictionary.
540
541 @param var variable to be converted
542 @type any
543 @return dictionary containing the variable attributes
544 @rtype dict
545 """
546 d = {}
547 d['typecode'] = var.typecode
548 if var.typecode in ArrayResolver.TypeCodeMap:
549 d['type'] = ArrayResolver.TypeCodeMap[var.typecode]
550 else:
551 d['type'] = 'illegal type'
552 d['itemsize'] = var.itemsize
553 d['[0:{0}]'.format(len(var) - 1)] = var.tolist()[0:MaxItemsToHandle]
554 return d
555
556
557 class ArrayItemsContainer:
558 """
559 Class to store array.array items.
560 """
561 pass
562
563
477 defaultResolver = DefaultResolver() 564 defaultResolver = DefaultResolver()
478 dictResolver = DictResolver() 565 dictResolver = DictResolver()
479 listResolver = ListResolver() 566 listResolver = ListResolver()
480 setResolver = SetResolver() 567 setResolver = SetResolver()
481 ndarrayResolver = NdArrayResolver() 568 ndarrayResolver = NdArrayResolver()
482 multiValueDictResolver = MultiValueDictResolver() 569 multiValueDictResolver = MultiValueDictResolver()
570 arrayResolver = ArrayResolver()
483 571
484 ############################################################ 572 ############################################################
485 ## Methods to determine the type of a variable and the 573 ## Methods to determine the type of a variable and the
486 ## resolver class to use 574 ## resolver class to use
487 ############################################################ 575 ############################################################
525 _TypeMap.append((frozenset, setResolver)) # __IGNORE_WARNING__ 613 _TypeMap.append((frozenset, setResolver)) # __IGNORE_WARNING__
526 except Exception: 614 except Exception:
527 pass # not available on all python versions 615 pass # not available on all python versions
528 616
529 try: 617 try:
618 import array
619 _TypeMap.append((array.array, arrayResolver))
620 except ImportError:
621 pass # array.array may not be available
622
623 try:
530 import numpy 624 import numpy
531 _TypeMap.append((numpy.ndarray, ndarrayResolver)) 625 _TypeMap.append((numpy.ndarray, ndarrayResolver))
532 except ImportError: 626 except ImportError:
533 pass # numpy may not be installed 627 pass # numpy may not be installed
534 628

eric ide

mercurial