I’ve been consistently getting errors using the PyDev debugger with both stock Eclipse and Aptana studio, PyDev 1.4.7.x and Python 3.x. Many times when I try to use the debugger to view the internals of an object, I don’t see any attributes except “Error” with the description “Could not resolve variable.” It was as if the PyDev debugger couldn’t see inside the object. Since looking at the internals of an object is the fundamental usefulness of a debugger, it meant that my debugger was basically useless.
I decided to do some hunting in the error logs and PyDev code to see if I could hunt down an easy solution.
The PyDev error log showed me this:
!ENTRY org.python.pydev.debug 4 4 2009-08-17 10:53:23.854 !MESSAGE Error fetching a variable !STACK 1 org.eclipse.core.runtime.CoreException: pydevd error:Error resolving variables Traceback (most recent call last): File "C:Program FilesAptanaAptana Studio 1.5pluginsorg.python.pydev.debug_1.4.7.2843pysrcpydevd_comm.py", line 571, in doIt keys = sorted(keys) #Jython 2.1 does not have it TypeError: unorderable types: str() < int() at org.python.pydev.debug.model.remote.GetVariableCommand.getResponse(GetVariableCommand.java:61) at org.python.pydev.debug.model.PyVariableCollection.getCommandVariables(PyVariableCollection.java:86) at org.python.pydev.debug.model.PyVariableCollection.getCommandVariables(PyVariableCollection.java:77) at org.python.pydev.debug.model.PyVariableCollection.commandComplete(PyVariableCollection.java:68) at org.python.pydev.debug.model.remote.AbstractDebuggerCommand.processResponse(AbstractDebuggerCommand.java:114) at org.python.pydev.debug.model.remote.DebuggerReader.processCommand(DebuggerReader.java:112) at org.python.pydev.debug.model.remote.DebuggerReader.run(DebuggerReader.java:131) at java.lang.Thread.run(Thread.java:619) !SUBENTRY 1 org.python.pydev.debug 4 4 2009-08-17 10:53:23.854 !MESSAGE pydevd error:Error resolving variables Traceback (most recent call last): File "C:Program FilesAptanaAptana Studio 1.5pluginsorg.python.pydev.debug_1.4.7.2843pysrcpydevd_comm.py", line 571, in doIt keys = sorted(keys) #Jython 2.1 does not have it TypeError: unorderable types: str() < int()
Gobbledygook? To some, but this little bit told me what to look for:
File "C:Program FilesAptanaAptana Studio 1.5pluginsorg.python.pydev.debug_1.4.7.2843pysrcpydevd_comm.py", line 571, in doIt keys = sorted(keys) #Jython 2.1 does not have it TypeError: unorderable types: str() < int()
Consulting the Grand All Oracle, I found that Python no longer supports sorting of some arbitrary types. As discussed in this thread, it seems that if you try to sort a list of integers and strings, Python will throw an error. Because there’s really no REAL way for Python to tell whether “Elephant” should come before the number 11. Alphabetical? Well, if we’re sorting alphabetically, why do we have a number and not the word “Eleven?”
Basically, the Python team decided that they didn’t want the language to guess how to order these types, and left it up to the developer to explicitly define the ordering in their code. I think this is a good choice, but it broke my debugger, so let’s get back to that.
So, I look at my log file and I find a traceback showing a specific file in the python debugger. Zooming to that file, I see the following code on lines 568-574:
if hasattr(keys, 'sort'):
keys.sort() #Python 3.0 does not have it
else:
keys = sorted(keys) #Jython 2.1 does not have it
for k in keys:
xml += pydevd_vars.varToXML(valDict[k], str(k))
Python 3.0 has no sort() method on dictionary keys, so the PyDev debugger uses the toplevel sorted() function. However, Python 3.0 cannot sort arbitrary types, so the line “keys = sorted(keys)” is throwing on exception. That exception was actually caught, but it was just spit to the debugger and execution continued. Thus, what I was left with was “Error: Could not resolve variable” in my debugger window.
Taking a chance, I commented out that else statement, resulting in this:
if hasattr(keys, 'sort'):
keys.sort() #Python 3.0 does not have it
#else:
# keys = sorted(keys) #Jython 2.1 does not have it
for k in keys:
xml += pydevd_vars.varToXML(valDict[k], str(k))
So, if there’s a sort() method, then it’s used, otherwise, things are just grabbed in whatever order they’re found (generally random, since this is a dictionary). It seems that it will order the variable keys randomly in the debugger, but that’s better than not having a debugger.
This change has fixed my “could not resolve variable” problem, but I am unsure as the side effects. Your mileage may vary.