[PyKDE] RE: Python 2.2 Weak Reference TypeError

Greg Fortune lists at gregfortune.com
Wed Oct 16 09:48:00 BST 2002


Did you ever get a resolution to this problem?  I've got two different 
programs that are exhibiting this problem now, both PyQt apps.  In the first 
case, I'm totally at a loss and haven't been able to isolate anything useful. 
 Using print statements seems to work in the first app so far though...

In the second app, I can't even use print statements to make it go away.  
Basically, I'm creating a new object inside a widget that's getting passed 
into a dialog.  Inside the dialog, the object is None, but even more odd is 
the fact that it's None directly after creating the object.  Here's a snippet 
of code with output so you have an idea of what's going on.  I took the tabs 
off the front of the code so it would display without wrapping, but the code 
is called through the following signal:
self.connect(self.tblResults, SIGNAL("doubleClicked(int,int,int,const 
QPoint&)"), self.item_selected)
where a user has double clicked on a table row.


Code:

print 'Making a new obj'
print self.backend_class
print self.backend_class()
temp = self.backend_class()
print 'temp type: ' + str(type(self.backend_class()))
print 'temp class: ' + str(temp.__class__)
print 'temp: ' + str(self.backend_class())
print locals()

from fsGradeBook.src.backend.Student import Student
print 'New student: ' + str(Student())

from fsGradeBook.src.backend.temp import new_class2
print 'New student2: ' + str(new_class2())

from temp import new_class
print 'New temp class: ' + str(new_class())

from Queue import Queue
print 'New Queue: ' + str(Queue())


Output:

Making a new obj
fsGradeBook.src.backend.Student.Student
None
temp type: <type 'instance'>
temp class: fsGradeBook.src.backend.Student.Student
temp: None
{'col': 1, 'temp': <fsGradeBook.src.backend.Student.Student instance at 
0x8306ed4>, 'point': <qt.QPoint instance at 0x82d8bc4>, 'self': 
<fsGradeBook.src.gui.GenericTab.GenericTab instance at 0x8272114>, 'button': 
1, 'row': -1}
New student: None
Initing the temp class
New student2: <fsGradeBook.src.backend.temp.new_class2 instance at 0x82dfdf4>
Initing the temp class
New temp class: <fsGradeBook.src.gui.temp.new_class instance at 0x8303ca4>
New Queue: <Queue.Queue instance at 0x82e5fd4>


The really scary thing is that although the result of creating a new object 
is None, the None is not really a None object, has the correct parent class, 
etc.  Also, take a look at the two temp classes.  The one that is imported 
through the package fails in the same manner as the main object, but the temp 
class that is imported in the current directory works fine, as does the Queue 
class.  Is it possible that PyQt is somehow mangling the package stuff in 
Python 2.2.1 or do we have a nasty Python bug on our hands?

Both Python versions are using PyQt 3.3.2.  The two versions are 2.1.1 and 
2.2.1.  If someone has a recent PyQt version and would like to test out the 
code on Python 2.2, I would love to know if this has been fixed.  Send me an 
e-mail and I'll ship back a tarball.


Greg Fortune


> My program has an error with Python 2.2 that it doesn't have with 2.1.
> The following exception is generated:
> 
> TypeError: cannot create weak reference to 'builtin_function_or_method'
> object
> 
> from the sort command in the following function:
> 
> def setCurrentSel(self):
>     names = myDict.keys()
>     names.sort()
>     ...
> 
> If "print names" is placed before the sort, the list on keys (unicode
> strings) prints fine, but the exception is still thrown by the sort
> command.  If the sort command is replaced by "for item in names: print
> item", the for loop throws the same exception.
> 
> Strangely, an earlier function that has the same lines (referencing the same
> dictionary keys) works fine.  And subsequent calls to setCurrentSel also
> work fine (the first one always fails).  And there are no changes to
> myDict between these function calls.
> 
> I can save the list of keys from the earlier call as a work-around, but I
> would like to understand what is causing this problem.  This is a PyQt
> program running on a Linux Mandrake 8.1 system.
> 
> Any ideas?
> 
> Doug.
> 




More information about the PyQt mailing list