Edgewall Software

Ticket #8290: bug8290.patch

File bug8290.patch, 2.9 KB (added by Joachim Hoessler <hoessler@…>, 3 years ago)

Fix for wrong reading from of options registry

  • trac/tests/config.py

     
    244244        finally: 
    245245            os.remove(sitename) 
    246246 
     247    def test_read_option_from_descriptor(self): 
     248        config = self._read() 
     249        Option('a', 'option', 'x') 
     250        self.assertEqual('x', config.get('a', 'option')) 
    247251 
     252    def test_iterate_option_from_descriptor(self): 
     253        config = self._read() 
     254        Option('a', 'option', 'x') 
     255        self.assertTrue('option' in config['a']) 
     256        options = config['a'].options() 
     257        self.assertEqual(('option', 'x'), options.next()) 
     258         
    248259def suite(): 
    249260    return unittest.makeSuite(ConfigurationTestCase, 'test') 
    250261 
  • trac/config.py

     
    253253        if self.config.parser.has_option(self.name, name): 
    254254            return True 
    255255        if self.config.parent: 
    256             return name in self.config.parent[self.name] 
    257         return False 
     256            if name in self.config.parent[self.name]: 
     257                return True 
     258        return name in [op for sec, op in Option.registry 
     259                        if sec == self.name] 
    258260 
    259261    def __iter__(self): 
    260262        options = set() 
     
    265267        if self.config.parent: 
    266268            for option in self.config.parent[self.name]: 
    267269                if option.lower() not in options: 
     270                    options.add(option.lower()) 
    268271                    yield option 
     272        for option in [op for sec, op in Option.registry if sec == self.name]: 
     273            if option.lower() not in options: 
     274                options.add(option.lower()) 
     275                yield option            
    269276 
    270277    def __repr__(self): 
    271278        return '<Section [%s]>' % (self.name) 
    272279 
     280    def _get(self, name): 
     281        """Iterates through .ini inheritance hierarchy and  
     282        returns value of specified option if found. Otherwise 
     283        returns None.  
     284        """ 
     285        if self.config.parser.has_option(self.name, name): 
     286            return self.config.parser.get(self.name, name) 
     287        elif self.config.parent: 
     288            return self.config.parent[self.name]._get(name) 
     289        return None       
     290 
    273291    def get(self, name, default=''): 
    274292        """Return the value of the specified option. 
    275293         
    276294        Valid default input is a string. Returns a string. 
    277295        """ 
    278         if self.config.parser.has_option(self.name, name): 
    279             value = self.config.parser.get(self.name, name) 
    280         elif self.config.parent: 
    281             value = self.config.parent[self.name].get(name, default) 
    282         else: 
     296        value = self._get(name) 
     297 
     298        if not value: 
    283299            option = Option.registry.get((self.name, name)) 
    284300            if option: 
    285301                value = option.default or default