Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #1039 (closed defect: wontfix)

Opened 13 years ago

Last modified 10 years ago

TypeError when evaluating FieldStorage object as boolean

Reported by: L33tminion Owned by: anonymous
Priority: normal Milestone:
Component: TG Widgets Version: 0.9a6
Severity: normal Keywords:
Cc:

Description

Problem: The cgi.FieldStorage object (call it file) generated by FileField never has its list data member set to something meaningful. As a result, trying to evaluate it as a boolean causes a TypeError (it calls len(file) which in turn calls len(file.keys()), and file.keys() throws a TypeError because file.list is None).

So code of this sort doesn't work:

class MyController(RootController):
    @expose(...)
    def upload(self,file=None,...):
        if file:
            <do stuff with the file...>

Solution: Make sure file.list is set so that the FieldStorage object evaluates to true unless it's empty.

Version: I'm working with Turbogears from SVN trunk, r1629.

Change History

comment:1 Changed 13 years ago by jorge.vargas

  • Milestone set to __future__

comment:2 Changed 11 years ago by Chris Arndt

  • Status changed from new to closed
  • Resolution set to wontfix

That's what  validatators are for.

Also, you can just check for file.filename:

    @expose()
    def upload(self,file=None, **kw):
        if file.filename:
            return "File detected: %s" % file.filename
        else:
            return "No File uploaded!"

comment:3 Changed 10 years ago by anonymous

  • Milestone __future__ deleted

Milestone future deleted

Note: See TracTickets for help on using tickets.