[ftputil] Problem with session_factory: __init__() takes at most 5 arguments (6 given)

Stefan Schwarzer sschwarzer at sschwarzer.net
Mon Mar 17 18:54:28 CET 2008


On 2008-03-17 13:54, Marco Buccini wrote:
> Hi all!!

Hello Marco :-)

> This is my first message here, and I would compliment with you ;)

You're welcome.

> But now I've a little problem:
> 
> I've this class:
> 
> class ActiveFTPSession(ftplib.FTP):
>     def __init__(self, host, userid, password):
>         ftplib.FTP.__init__(self)
>         self.connect(host, port)
>         self.login(userid, password)
>         # see http://docs.python.org/lib/ftp-objects.html
>         self.set_pasv(False)
> 
> and this:
> 
> class FTPConnection(object):
>     def __init__(self, host, userid, password, port, session_factory):
>        
>         self.host = host
>         self.userid = userid
>         self.password = password
>         self.port = port
>         self.session_factory = session_factory
> 
>         try:
>             self.connection = ftputil.FTPHost(self.host, self.userid,
>                      self.password, self.port, self.session_factory)

session_factory has to be a keyword argument, not a positional argument,
i. e. change

self.connection = ftputil.FTPHost(self.host, self.userid, self.password,
                  self.port, self.session_factory)

to

self.connection = ftputil.FTPHost(self.host, self.userid, self.password,
                  self.port, session_factory=self.session_factory)

The ftputil documentation says:

"""
In fact, all positional and keyword arguments other than session_factory
are passed to the factory to generate a new background session ...
"""

Since all positional arguments are passed directly to the session
factory callable, the session_factory argument itself has to be a
keyword argument.

>         except ftputil.FTPError, e:
>             print "Errore"
>             print e
> 
> When I try to run the main:
> 
> try:
> 
>         fc =  FTPConnection(HOST, USERID, PASSWORD, PORT, ActiveFTPSession)
> 
> except Exception, e:
>         raise FTPConnectionError("Non e` stato possibile effettuare "\
>                                     "la connessione:\n%s" % e)
>         quit()
> 
> I receive this error:
> 
> Traceback (most recent call last):
>   File "main.py", line 37, in <module>
>     "la connessione:\n%s" % e)
> src.FTPConnection.FTPConnectionError: Non e` stato possibile effettuare 
> la connessione:
> __init__() takes at most 5 arguments (6 given)
> 
> It's saying me that I pass 6 arguments instead of 5.
> What's the matter?

Since the ftputil.FTPHost constructor can't recognize and "filter"
the session_factory argument, it tries to stuff the extra argument
in *args and passes this to the _default_ factory, ftplib.FTP.
However, ftplib.FTP's constructor takes at most five arguments
(including self), so you have passed in one argument too much.

Have fun! :)

Best regards,
Stefan


More information about the ftputil mailing list