From 39c6a80f9db6ec04cac59f116ee9620c3d540be0 Mon Sep 17 00:00:00 2001 From: Julia Medina Date: Fri, 1 Aug 2014 00:42:25 -0300 Subject: [PATCH] Both getdict and getlist return copies of the requested values --- docs/topics/api.rst | 16 ++++++++++++++-- scrapy/settings/__init__.py | 19 ++++++------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/docs/topics/api.rst b/docs/topics/api.rst index 16bfe5f8f..6e636e826 100644 --- a/docs/topics/api.rst +++ b/docs/topics/api.rst @@ -252,8 +252,8 @@ Settings API .. method:: getlist(name, default=None) - Get a setting value as a list. If the setting original type is a list it - will be returned verbatim. If it's a string it will be split by ",". + Get a setting value as a list. If the setting original type is a list, a + copy of it will be returned. If it's a string it will be split by ",". For example, settings populated through environment variables set to ``'one,two'`` will return a list ['one', 'two'] when using this method. @@ -264,6 +264,18 @@ Settings API :param default: the value to return if no setting is found :type default: any + .. method:: getdict(name, default=None) + + Get a setting value as a dictionary. If the setting original type is a + dictionary, a copy of it will be returned. If it's a string it will + evaluated as a json dictionary. + + :param name: the setting name + :type name: string + + :param default: the value to return if no setting is found + :type default: any + .. method:: copy() Make a deep copy of current settings. diff --git a/scrapy/settings/__init__.py b/scrapy/settings/__init__.py index 978174694..bbe8ef481 100644 --- a/scrapy/settings/__init__.py +++ b/scrapy/settings/__init__.py @@ -76,23 +76,16 @@ class Settings(object): return float(self.get(name, default)) def getlist(self, name, default=None): - value = self.get(name) - if value is None: - return default or [] - elif hasattr(value, '__iter__'): - return value - else: - return str(value).split(',') + value = self.get(name, default or []) + if isinstance(value, six.string_types): + value = value.split(',') + return list(value) def getdict(self, name, default=None): - value = self.get(name) - if value is None: - return default or {} + value = self.get(name, default or {}) if isinstance(value, six.string_types): value = json.loads(value) - if isinstance(value, dict): - return value - raise ValueError("Cannot convert value for setting '%s' to dict: '%s'" % (name, value)) + return dict(value) def set(self, name, value, priority='project'): assert not self.frozen, "Trying to modify an immutable Settings object"