Skip to content

Commit

Permalink
Fix backwards-compatibility for users who explicitly set _BASE settings
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemaeyer committed Oct 27, 2015
1 parent f249b30 commit 03349ff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
15 changes: 11 additions & 4 deletions scrapy/settings/__init__.py
Expand Up @@ -116,9 +116,9 @@ def get(self, name, default=None):
def getbool(self, name, default=False):
"""
Get a setting value as a boolean.
``1``, ``'1'``, and ``True`` return ``True``, while ``0``, ``'0'``,
``False`` and ``None`` return ``False``.
``False`` and ``None`` return ``False``.
For example, settings populated through environment variables set to
``'0'`` will return ``False`` when using this method.
Expand Down Expand Up @@ -203,8 +203,15 @@ def _getcomposite(self, name):
if basename in self:
warnings.warn('_BASE settings are deprecated.',
category=ScrapyDeprecationWarning)
compsett = BaseSettings(self[name + "_BASE"], priority='default')
compsett.update(self[name])
# When users defined a _BASE setting, they explicitly don't want to
# use any of Scrapy's defaults. Therefore, we only use these entries
# from self[name] (where the defaults now live) that have a priority
# higher than 'default'
compsett = BaseSettings(self[basename], priority='default')
for k in self[name]:
prio = self[name].getpriority(k)
if prio > get_settings_priority('default'):
compsett.set(k, self[name][k], prio)
return compsett
else:
return self[name]
Expand Down
14 changes: 10 additions & 4 deletions tests/test_settings/__init__.py
Expand Up @@ -252,12 +252,18 @@ def test_getpriority(self):

def test_getcomposite(self):
s = BaseSettings({'TEST_BASE': {1: 1, 2: 2},
'TEST': BaseSettings({1: 10}),
'HASNOBASE': BaseSettings({1: 1})})
'TEST': BaseSettings({1: 10, 3: 30}, 'default'),
'HASNOBASE': BaseSettings({1: 1}, 'default')})
s['TEST'].set(4, 4, priority='project')
# When users specify a _BASE setting they explicitly don't want to use
# Scrapy's defaults, so we don't want to see anything that has a
# 'default' priority from TEST
cs = s._getcomposite('TEST')
self.assertEqual(len(cs), 2)
self.assertEqual(cs[1], 10)
print cs
self.assertEqual(len(cs), 3)
self.assertEqual(cs[1], 1)
self.assertEqual(cs[2], 2)
self.assertEqual(cs[4], 4)
cs = s._getcomposite('HASNOBASE')
self.assertEqual(len(cs), 1)
self.assertEqual(cs[1], 1)
Expand Down

0 comments on commit 03349ff

Please sign in to comment.