Added a resolver for array.array types to the debuggers.

Fri, 30 Sep 2016 19:52:16 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 30 Sep 2016 19:52:16 +0200
changeset 5190
65a2234c6789
parent 5189
8fa3e3d379d1
child 5191
b7471cf89b07

Added a resolver for array.array types to the debuggers.

DebugClients/Python2/DebugClientBase.py file | annotate | diff | comparison | revisions
DebugClients/Python2/DebugVariables.py file | annotate | diff | comparison | revisions
DebugClients/Python3/DebugClientBase.py file | annotate | diff | comparison | revisions
DebugClients/Python3/DebugVariables.py file | annotate | diff | comparison | revisions
Debugger/VariablesViewer.py file | annotate | diff | comparison | revisions
--- a/DebugClients/Python2/DebugClientBase.py	Fri Sep 30 19:44:01 2016 +0200
+++ b/DebugClients/Python2/DebugClientBase.py	Fri Sep 30 19:52:16 2016 +0200
@@ -1677,7 +1677,7 @@
                 else:
                     valtype = valtypestr[6:-1]
                     if valtype not in ConfigVarTypeStrings:
-                        if valtype == "numpy.ndarray":
+                        if valtype in ["numpy.ndarray", "array.array"]:
                             if ConfigVarTypeStrings.index('list') in filter:
                                 continue
                         elif valtypename == "MultiValueDict":
@@ -1694,7 +1694,7 @@
                             continue
                         
                         if valtypename not in ["MultiValueDict"] and \
-                                not valtype.startswith("numpy"):
+                                not valtype.startswith(("numpy.", "array.")):
                             valtype = valtypestr
                     else:
                         try:
@@ -1720,7 +1720,7 @@
                     
                 try:
                     if valtype in ['list', 'tuple', 'dict', 'set',
-                                   'frozenset']:
+                                   'frozenset', 'array.array']:
                         if valtype == 'dict':
                             rvalue = "%d" % len(value.keys())
                         else:
--- a/DebugClients/Python2/DebugVariables.py	Fri Sep 30 19:44:01 2016 +0200
+++ b/DebugClients/Python2/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": "long (unsigned int)",
+        "l": "int (signed long)",
+        "L": "long (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:
--- a/DebugClients/Python3/DebugClientBase.py	Fri Sep 30 19:44:01 2016 +0200
+++ b/DebugClients/Python3/DebugClientBase.py	Fri Sep 30 19:52:16 2016 +0200
@@ -1678,7 +1678,7 @@
                 valtype = valtypestr[7:-1]
                 valtypename = type(value).__name__
                 if valtype not in ConfigVarTypeStrings:
-                    if valtype == "numpy.ndarray":
+                    if valtype in ["numpy.ndarray", "array.array"]:
                         if ConfigVarTypeStrings.index('list') in filter:
                             continue
                     elif valtypename == "MultiValueDict":
@@ -1694,7 +1694,8 @@
                     elif ConfigVarTypeStrings.index('instance') in filter:
                         continue
                     
-                    if valtypename not in ["ndarray", "MultiValueDict"]:
+                    if valtypename not in ["ndarray", "MultiValueDict",
+                                           "array"]:
                         valtype = valtypestr
                 else:
                     try:
@@ -1718,7 +1719,7 @@
                 
                 try:
                     if valtype in ['list', 'tuple', 'dict', 'set',
-                                   'frozenset']:
+                                   'frozenset', 'array.array']:
                         if valtype == 'dict':
                             rvalue = "{0:d}".format(len(value.keys()))
                         else:
--- 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:
--- a/Debugger/VariablesViewer.py	Fri Sep 30 19:44:01 2016 +0200
+++ b/Debugger/VariablesViewer.py	Fri Sep 30 19:52:16 2016 +0200
@@ -588,7 +588,8 @@
             else:
                 return SpecialVarItem(parent, dvar, dvalue, dtype,
                                       self.framenr, self.__globalScope)
-        elif dtype in ["numpy.ndarray", "django.MultiValueDict"]:
+        elif dtype in ["numpy.ndarray", "django.MultiValueDict",
+                       "array.array"]:
             return SpecialVarItem(
                 parent, dvar, self.tr("{0} items").format(dvalue), dtype,
                 self.framenr, self.__globalScope)
@@ -703,7 +704,7 @@
         name = VariableItem.extractIndicators(itm.text(0).strip())[0]
         
         par = itm.parent()
-        if name.startswith("["):    # numpy.ndarray
+        if name.startswith("["):    # numpy.ndarray, array.array
             nlist = []
         else:
             nlist = [name]
@@ -717,7 +718,7 @@
                     nlist[0] = '[{0}].'.format(nlist[0][:-1])
                 else:
                     nlist[0] = '[{0}]'.format(nlist[0])
-                if not pname.startswith("["):   # numpy.ndarray
+                if not pname.startswith("["):   # numpy.ndarray, array.array
                     nlist.insert(0, pname)
             else:
                 if par.text(2) == "django.MultiValueDict":

eric ide

mercurial