[ python-Bugs-1581357 ] missing __enter__ + __getattr__ forwarding

SourceForge.net noreply at sourceforge.net
Sun Oct 29 15:47:51 CET 2006


Bugs item #1581357, was opened at 2006-10-20 15:17
Message generated for change (Comment added) made by gbrandl
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1581357&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.5
>Status: Closed
>Resolution: Fixed
Priority: 7
Private: No
Submitted By: Hirokazu Yamamoto (ocean-city)
>Assigned to: Georg Brandl (gbrandl)
Summary: missing __enter__ + __getattr__ forwarding

Initial Comment:
Hello. I encountered some unexpected behavior on "with"
statement.

First, please run attached "a.py" file.

# traditional way
<open file 'a.txt', mode 'wb' at 0x009373C8> shift_jis
<type 'instance'> True
# with statement
<open file 'a.txt', mode 'wb' at 0x009373C8> None <type
'file'> False
Traceback (most recent call last):
  File "R:\a.py", line 15, in <module>
    test(io)
  File "R:\a.py", line 8, in test
    io.write(u"あいうえお")
UnicodeEncodeError: 'ascii' codec can't encode
characters in position 0-4: ordin
al not in range(128)

"traditional way" runs as expected, but "with
statement" crashes. This happens because....

  1. codecs.open returns codecs.StreamReaderWriter
  2. codecs.StreamReaderWriter defines __getattr__
     like this.

    def __getattr__(self, name,
                    getattr=getattr):

        """ Inherit all other methods from the
            underlying stream.
        """
        return getattr(self.stream, name)

  3. But codecs.StreamReaderWriter doesn't have its
     __enter__ definition, so 

    srw = StreamReaderWriter(stream, ...
    srw.__enter__() # actually calls stream.__enter__
                      which returns stream not srw
                      via __getattr__

     And more worse, with statement doesn't complain
     StreamReaderWriter (currently) doesn't support
     context manager.

Is this intended behavior? If not, only this problem
can be solved by attached "a.patch". I greped library
files, I found many __getattr__ without __enter__...



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

>Comment By: Georg Brandl (gbrandl)
Date: 2006-10-29 14:47

Message:
Logged In: YES 
user_id=849994

This is a duplicate of #1586513 which has been fixed today.

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

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


More information about the Python-bugs-list mailing list