What's the tidy/elegant way to protect this against null/empty parameters?

Terry Reedy tjreedy at udel.edu
Mon Oct 15 11:45:43 EDT 2012


On 10/15/2012 7:23 AM, tinnews at isbd.co.uk wrote:
> I want to fix an error in some code I have installed, however I don't
> really want to just bodge it.
>
> The function producing the error is:-
>
>      def get_text(self, idx):               # override !
>          node = self.items[idx]
>
>          a= [
>              ", ".join(node.tags),
>              node.comment,
>              node.folderName,
>              cd2rd(node.date),
>              node.name,
>              '[' + self.rating_stars[node.rating] + ']'
>              ] [self.select]
>
>          return a
>
>
> The error occurs when node[] (or at least its members) turn out to be
> empty,

This is not the problem.

 > you get a Traceback that ends with:-
>
>    File "/usr/lib/jbrout/jbrout/listview.py", line 608, in draw_cell layout.set_text(self.get_text(thumbnail_num))
>    File "/usr/lib/jbrout/jbrout.py", line 325, in get_text ", ".join(node.tags),
>    TypeError: sequence item 0: expected string, NoneType found

The specific problem is that node.tags is supposed to be a sequence of 
strings and somehow one instead has None as the first, and probably last 
item. This was likely intended to indicate an empty list, but the way to 
do that is to have a empty list, which would have worked just fine. In 
other words, the likely problem is that node.tags is *not* an empty 
sequence when it should be.

 >>> ','.join([None])
Traceback (most recent call last):
   File "<pyshell#9>", line 1, in <module>
     ','.join([None])
TypeError: sequence item 0: expected str instance, NoneType found
 >>> ','.join([])
''

-- 
Terry Jan Reedy




More information about the Python-list mailing list