DebugClients/Python3/DebugVariables.py

changeset 5190
65a2234c6789
parent 5175
9db0b0f15d12
--- a/DebugClients/Python3/DebugVariables.py	Fri Sep 30 19:44:01 2016 +0200
+++ b/DebugClients/Python3/DebugVariables.py	Fri Sep 30 19:52:16 2016 +0200
@@ -474,12 +474,100 @@
         return d
 
 
+############################################################
+## Resolver for array.array
+############################################################
+
+
+class ArrayResolver(BaseResolver):
+    """
+    Class used to resolve from array.array including some meta data.
+    """
+    TypeCodeMap = {
+        "b": "int (signed char)",
+        "B": "int (unsigned char)",
+        "u": "Unicode character (Py_UNICODE)",
+        "h": "int (signed short)",
+        "H": "int (unsigned short)",
+        "i": "int (signed int)",
+        "I": "int (unsigned int)",
+        "l": "int (signed long)",
+        "L": "int (unsigned long)",
+        "q": "int (signed long long)",
+        "Q": "int (unsigned long long)",
+        "f": "float (float)",
+        "d": "float (double)",
+    }
+    def resolve(self, var, attribute):
+        """
+        Public method to get an attribute from a variable.
+        
+        @param var variable to extract an attribute or value from
+        @type tuple or list
+        @param attribute id of the value to extract
+        @type str
+        @return value of the attribute
+        @rtype any
+        """
+        if attribute == 'itemsize':
+            return var.itemsize
+        
+        if attribute == 'typecode':
+            return var.typecode
+        
+        if attribute == 'type':
+            if var.typecode in ArrayResolver.TypeCodeMap:
+                return ArrayResolver.TypeCodeMap[var.typecode]
+            else:
+                return 'illegal type'
+        
+        if attribute.startswith('['):
+            container = ArrayItemsContainer()
+            count = 0
+            for element in var:
+                setattr(container, str(count), element)
+                count += 1
+                if count > MaxItemsToHandle:
+                    setattr(container, TooLargeAttribute, TooLargeMessage)
+                    break
+            return container
+        
+        return None
+    
+    def getDictionary(self, var):
+        """
+        Public method to get the attributes of a variable as a dictionary.
+        
+        @param var variable to be converted
+        @type any
+        @return dictionary containing the variable attributes
+        @rtype dict
+        """
+        d = {}
+        d['typecode'] = var.typecode
+        if var.typecode in ArrayResolver.TypeCodeMap:
+            d['type'] = ArrayResolver.TypeCodeMap[var.typecode]
+        else:
+            d['type'] = 'illegal type'
+        d['itemsize'] = var.itemsize
+        d['[0:{0}]'.format(len(var) - 1)] = var.tolist()[0:MaxItemsToHandle]
+        return d
+
+
+class ArrayItemsContainer:
+    """
+    Class to store array.array items.
+    """
+    pass
+
+
 defaultResolver = DefaultResolver()
 dictResolver = DictResolver()
 listResolver = ListResolver()
 setResolver = SetResolver()
 ndarrayResolver = NdArrayResolver()
 multiValueDictResolver = MultiValueDictResolver()
+arrayResolver = ArrayResolver()
 
 ############################################################
 ## Methods to determine the type of a variable and the
@@ -527,6 +615,12 @@
         pass    # not available on all python versions
     
     try:
+        import array
+        _TypeMap.append((array.array, arrayResolver))
+    except ImportError:
+        pass  # array.array may not be available
+    
+    try:
         import numpy
         _TypeMap.append((numpy.ndarray, ndarrayResolver))
     except ImportError:

eric ide

mercurial