Skip to content

Commit

Permalink
Merge pull request #3594 from raverat/fix/handle_non_string_object_as…
Browse files Browse the repository at this point in the history
…list_method

Handle non string object passed to aslist method
  • Loading branch information
mmerickel authored Jun 4, 2020
2 parents 0ea2378 + 8862ede commit 48a0485
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ unreleased
Features
--------

- The ``aslist`` method now handles non-string object when flattening.
See https://github.com/Pylons/pyramid/pull/3594

- It is now possible to pass multiple values to the ``header`` predicate
for route and view configuration.
See https://github.com/Pylons/pyramid/pull/3576
Expand Down
2 changes: 2 additions & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,5 @@ Contributors
- Andrea Borghi, 2019/11/11

- Sergey Maranchuk, 2020/04/18

- Thibault Ravera, 2020/06/03
14 changes: 9 additions & 5 deletions src/pyramid/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ def aslist_cronly(value):


def aslist(value, flatten=True):
""" Return a list of strings, separating the input based on newlines
and, if flatten=True (the default), also split on spaces within
each line."""
""" Return a list, separating the input based on newlines.
Also if ``flatten`` is ``True`` (the default), and if the line
is a string, then the line will be split on spaces.
"""
values = aslist_cronly(value)
if not flatten:
return values
result = []
for value in values:
subvalues = value.split()
result.extend(subvalues)
if isinstance(value, str):
value = value.split()
result.extend(value)
else:
result.append(value)
return result
12 changes: 12 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ def test_with_list(self):
result = self._callFUT(['abc', 'def'])
self.assertEqual(list(result), ['abc', 'def'])

def test_with_integer(self):
result = self._callFUT([1])
self.assertEqual(result, [1])

def test_with_integer_no_flatten(self):
result = self._callFUT([1], flatten=False)
self.assertEqual(result, [1])

def test_with_string(self):
result = self._callFUT('abc def')
self.assertEqual(result, ['abc', 'def'])
Expand All @@ -84,3 +92,7 @@ def test_with_string_crsep_spacesep(self):
def test_with_string_crsep_spacesep_no_flatten(self):
result = self._callFUT(' abc\n def ghi ', flatten=False)
self.assertEqual(result, ['abc', 'def ghi'])

def test_with_string_crsep_spacesep_and_integer(self):
result = self._callFUT([' abc def ghi ', 1])
self.assertEqual(result, ['abc', 'def', 'ghi', 1])

0 comments on commit 48a0485

Please sign in to comment.