Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Both getdict and getlist return copies of the requested values

Julia Medina39c6a80f9db6
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"