Re: socket.makefile raises ValueError when mode = 'rt'

Discussion in 'Python' started by Terry Reedy, Jan 9, 2013.

  1. Terry Reedy

    Terry Reedy Guest

    On 1/9/2013 9:14 AM, Antoon Pardon wrote:
    > Op 01/09/13 14:54, Dave Angel schreef:
    >> On 01/09/2013 08:22 AM, Antoon Pardon wrote:
    >>> This is using python 3.2.

    > ...
    >>> But the documentation states:
    >>> socket.makefile(mode='r', buffering=None, *, encoding=None, errors=None,
    >>> newline=None)
    >>> Return a file object associated with the socket. The exact returned
    >>> type depends on the arguments given to makefile(). These arguments are
    >>> interpreted the same way as by the built-in open() function.
    >>>
    >>> And since 't' is allowed in the mode of the built-in open() function I
    >>> would consider this a bug.
    >>> Unless I am missing something?

    >> I believe that 't' was a new addition to mode, for Python 3.x So
    >> perhaps the socket library hasn't kept consistent with it.
    >>
    >> I don't really know the socket library. Does it even support text
    >> mode? Does that make sense? Remember that text mode means a different
    >> thing in 3.x than it did in 2.x

    > As far as I understand the code, it does support text. This is part of
    > the makefile method.
    >
    > def makefile(self, mode="r", buffering=None, *,
    > encoding=None, errors=None, newline=None):
    >
    > for c in mode:
    > if c not in {"r", "w", "b"}:
    > raise ValueError("invalid mode %r (only r, w, b allowed)")
    > writing = "w" in mode
    > reading = "r" in mode or not writing
    > assert reading or writing
    > binary = "b" in mode
    > ...
    > if binary:
    > return buffer
    > text = io.TextIOWrapper(buffer, encoding, errors, newline)
    > text.mode = mode
    > return text
    >
    > So it seems that if the mode is not binary an io.TextIOWrapper is
    > returned. That indicates to me that
    > text mode is supported.


    The doc does not specify any limit on mode, though the exclusions 'a',
    '+', 'x', and 'U' seem proper to me. That contradicts the mode check.
    The exclusion of of 't' (which is the default, in that 'b' must be
    explicitly given to have effect) contradicts the later code. I think you
    should open an issue on the tracker suggesting that 't' be added to the
    mode check and that the doc mention the remaining mode limitation.

    In the meanwhile, your ftpfile.__init__ could remove t from the mode
    before passing it on.


    --
    Terry Jan Reedy
     
    Terry Reedy, Jan 9, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    367
  2. Philip Semanchuk
    Replies:
    0
    Views:
    431
    Philip Semanchuk
    Mar 9, 2006
  3. Antoon Pardon
    Replies:
    0
    Views:
    135
    Antoon Pardon
    Jan 9, 2013
  4. Dave Angel
    Replies:
    0
    Views:
    129
    Dave Angel
    Jan 9, 2013
  5. Antoon Pardon
    Replies:
    0
    Views:
    119
    Antoon Pardon
    Jan 9, 2013
Loading...

Share This Page