[Python-bugs-list] [ python-Bugs-478534 ] SystemError with WeakKeyDictionary

noreply@sourceforge.net noreply@sourceforge.net
Tue, 06 Nov 2001 10:47:58 -0800


Bugs item #478534, was opened at 2001-11-05 19:15
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=478534&group_id=5470

Category: Python Interpreter Core
Group: Python 2.2
>Status: Open
Resolution: Works For Me
Priority: 5
Submitted By: Sverker Nilsson (svenil)
Assigned to: Fred L. Drake, Jr. (fdrake)
Summary: SystemError with WeakKeyDictionary

Initial Comment:
SystemError with WeakKeyDictionary

A SystemError is generated when trying
to iterate over a function returned from a
function that stored it in a WeakKeyDictionary.
The expected error was TypeError.

Sverker Nilsson

Examples:

This program gives a SystemError:

import weakref
ref = weakref.WeakKeyDictionary()

def encapsulate():
    f = lambda : ()
    ref[f] = None
    return f

for x in encapsulate():
 print x

Python 2.2b1 (#5, Oct 20 2001, 03:03:53) 
[GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
Type "help", "copyright", "credits" or "license" for
more information.
...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/tmp/pythona04442", line 9, in ?
    for x in encapsulate():
SystemError: error return without exception set
>>> 

A variation of it, with a temporary variable,
gives the expected TypeError:

import weakref
ref = weakref.WeakKeyDictionary()

def encapsulate():
    f = lambda : ()
    ref[f] = None
    return f

g = encapsulate()
for x in g:
 print x


Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/tmp/pythona04442", line 10, in ?
    for x in g:
TypeError: iteration over non-sequence
>>>



----------------------------------------------------------------------

>Comment By: Sverker Nilsson (svenil)
Date: 2001-11-06 10:47

Message:
Logged In: YES 
user_id=356603

To answer your question, I am using 2.2b1,
from the tarball not from CVS.

In the second example, I get SystemError
from the first call to add actually. I
thought it was from the second one, but
since I changed from my own exception
class to Exception it became hidden. 

When I remove the uninteded self parameter
of add, I get SystemError in the second
call, as I thought I was getting.

But you get TypeError.. hmmm..

I suppose I should download the CVS then?..
Or maybe wait for 2.2b2..

Sverker

ps. Here's my updated second example, anyway..

import weakref
ref = weakref.WeakKeyDictionary()

class MyError(Exception):
    pass

def add(self, x):
    raise MyError

def encapsulate():
    f = lambda : ()
    ref[f] = None
    return f

try:
    add(encapsulate())
except MyError: pass

print 'first add ok'

add(encapsulate())
# Here we would like to get Exception but gets SystemError
instead.



----------------------------------------------------------------------

Comment By: Fred L. Drake, Jr. (fdrake)
Date: 2001-11-06 08:33

Message:
Logged In: YES 
user_id=3066

I cannot reproduce this using either code snippet.  Note
that the second example should (and does for me) raise
TypeError when calling add() -- you left in a "self" that
appears to be unintended.

Are you using 2.2b1 or CVS python?

----------------------------------------------------------------------

Comment By: Sverker Nilsson (svenil)
Date: 2001-11-05 23:17

Message:
Logged In: YES 
user_id=356603

The same problem now occured in another situation. I am
enclosing the condensed program. /Sverker

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=478534&group_id=5470