Edgewall Software

Opened 12 years ago

Closed 11 years ago

Last modified 9 years ago

#10541 closed defect (fixed)

ListOption seems to ignore/suppress 0 — at Version 4

Reported by: Chris Nelson <Chris.Nelson@…> Owned by:
Priority: normal Milestone: 1.0.1
Component: general Version: 0.11.6
Severity: major Keywords: config
Cc: Branch:
Release Notes:

ListOption keeps values other than empty string and None in raw list as default

API Changes:
Internal Changes:

Description

I'm trying to add a list of work days (days of the week) to TeamCalendar plugin. Near the top I do:

class TeamCalendar(Component):
    implements(INavigationContributor, IRequestHandler, IPermissionRequestor, 
               ITemplateProvider)

...
    # Default work week.
    work_days = ListOption('team-calendar',
                            'work_days',
                            [0, 1, 2, 3, 4],
                            doc="Lists days of week that are worked. " + \
                                "Defaults to none.  0 is Monday.")

and later

        self.env.log.debug('work_days: %s ' % self.work_days)

        timetable = {}
        current_date = from_date
        while current_date <= to_date:
            if current_date.weekday() in self.work_days:

The log does not show the 0, only 1..4. If I set the default to [99, 0, 1, 2, 3, 4], the log shows 99 but not 0 so it isn't the first element that's getting dropped.

Change History (4)

comment:1 by Jun Omae, 12 years ago

Keywords: config added
Milestone: 1.0.1

Reproduced. ListOption drops 0 (int) and 0.0 (float) from raw list as default value.

Workaround is to specify string as default value.

    work_days = ListOption('team-calendar',
                           'work_days',
                           '0, 1, 2, 3, 4',
                           doc="Lists days of week that are worked. " + \
                                "Defaults to none. 0 is Monday."

If did fix this…

  • trac/config.py

     
    455455        else:
    456456            items = list(value)
    457457        if not keep_empty:
    458             items = filter(None, items)
     458            items = filter(lambda val: val or val is not None and \
     459                                       not isinstance(val, basestring),
     460                           items)
    459461        return items
    460462
    461463    def getpath(self, key, default=''):
  • trac/tests/config.py

     
    172172                    config.getlist('b', 'option2', ['foo', 'bar', 'baz']))
    173173        self.assertEquals(['foo', 'bar', 'baz'],
    174174                    config.getlist('b', 'option2', 'foo, bar, baz'))
     175        self.assertEquals([-1, 0, 1],
     176                          config.getlist('c', 'int', [-1, 0, 1]))
     177        self.assertEquals([-1.0, 0.0, 1.0],
     178                          config.getlist('c', 'float', [-1.0, 0.0, 1.0]))
    175179
    176180    def test_read_and_getlist_sep(self):
    177181        self._write(['[a]', 'option = foo | bar | baz'])

comment:2 by Christian Boos, 12 years ago

If we're not using filter(None, ), we could as well use a list comprehension here?

items = [v for v in items if v or v is not None etc.]

And for the test itself, in trunk we could use isinstance(v, numbers.Number) if we indeed switch to Python 2.6 as a base requirement.

in reply to:  2 comment:3 by Jun Omae, 12 years ago

Replying to cboos:

If we're not using filter(None, ), we could as well use a list comprehension here?

items = [v for v in items if v or v is not None etc.]

Oh, yes. Thanks ;-)

  • trac/config.py

     
    455455        else:
    456456            items = list(value)
    457457        if not keep_empty:
    458             items = filter(None, items)
     458            items = [v for v in items
     459                       if v or isinstance(v, (int, long, float))]
    459460        return items
    460461
    461462    def getpath(self, key, default=''):

I'll apply later.

comment:4 by Jun Omae, 11 years ago

Release Notes: modified (diff)
Resolution: fixed
Status: newclosed

Fixed in [11478-11479].

Note: See TracTickets for help on using tickets.