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

Discussion in 'Python' started by Dave Angel, Jan 9, 2013.

  1. Dave Angel

    Dave Angel Guest

    On 01/09/2013 08:22 AM, Antoon Pardon wrote:
    > This is using python 3.2.
    >
    > I am writing somekind of wrapper around the ftplib. So
    > that you can work with it as if you are working with
    > local files.
    >
    > The idea is that you start with making a connection like
    >
    > rmt = FTP(...)
    >
    > and then do something like the following
    >
    > rmtfl = rmt.open("rmtfilename", "rt")
    > for ln in rmtfl:
    > treat(ln)
    >
    > This is part of the code:
    >
    > class ftpfile:
    > def __init__(self, cn, rfn, mode, bound = False):
    > self.ftp = cn
    > self.bound = bound
    > if 'b' in mode:
    > self.ftp.voidcmd('TYPE I')
    > else:
    > self.ftp.voidcmd('TYPE A')
    > if 'r' in mode:
    > self.cnct = self.ftp.transfercmd("RETR %s" % rfn)
    > self.fl = self.cnct.makefile(mode)
    > elif 'w' in mode:
    > self.cnct = self.ftp.transfercmd("STOR %s" % rfn)
    > self.fl = self.cnct.makefile(mode, newline = '\r\n')
    > else:
    > raise ValueError("%s: invalide mode" % mode)
    >
    > The problem is with makefile. If mode contains a "t" I get
    > the following traceback:
    >
    > Traceback (most recent call last):
    > File "ftputil.tpy", line 14, in test_textftp
    > rmtfl1 = rmt.open('ftp1.py', 'wt')
    > File "/local/home/apardon/src/projecten/py3lib/ftputil.py", line 76,
    > in open
    > return ftpfile(ftp, fn, mode, True)
    > File "/local/home/apardon/src/projecten/py3lib/ftputil.py", line 15,
    > in __init__
    > self.fl = self.cnct.makefile(mode, newline = '\r\n')
    > File "/usr/lib/python3.2/socket.py", line 151, in makefile
    > raise ValueError("invalid mode %r (only r, w, b allowed)")
    > ValueError: invalid mode %r (only r, w, b allowed)
    >
    > 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



    --

    DaveA
     
    Dave Angel, 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:
    381
  2. Philip Semanchuk
    Replies:
    0
    Views:
    448
    Philip Semanchuk
    Mar 9, 2006
  3. Antoon Pardon
    Replies:
    0
    Views:
    157
    Antoon Pardon
    Jan 9, 2013
  4. Antoon Pardon
    Replies:
    0
    Views:
    132
    Antoon Pardon
    Jan 9, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    195
    Terry Reedy
    Jan 9, 2013
Loading...

Share This Page