Skip to content

Commit

Permalink
Merge pull request Pylons#2959 from Natim/2958-do-not-update-the-sett…
Browse files Browse the repository at this point in the history
…ings-parameter

The Configurator object should not alter the input settings dict object.
  • Loading branch information
mmerickel authored Feb 20, 2017
2 parents 40d71e8 + 3c04c12 commit d3cb4b5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions pyramid/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def Settings(d=None, _environ_=os.environ, **kw):
keyword args)."""
if d is None:
d = {}
d = dict(d)
d.update(**kw)

eget = _environ_.get
Expand Down
25 changes: 22 additions & 3 deletions pyramid/tests/test_config/test_settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest


class TestSettingsConfiguratorMixin(unittest.TestCase):
def _makeOne(self, *arg, **kw):
from pyramid.config import Configurator
Expand All @@ -11,12 +12,12 @@ def test__set_settings_as_None(self):
settings = config._set_settings(None)
self.assertTrue(settings)

def test__set_settings_uses_original_dict(self):
def test__set_settings_does_not_uses_original_dict(self):
config = self._makeOne()
dummy = {}
result = config._set_settings(dummy)
self.assertTrue(dummy is result)
self.assertEqual(dummy['pyramid.debug_all'], False)
self.assertTrue(dummy is not result)
self.assertNotIn('pyramid.debug_all', dummy)

def test__set_settings_as_dictwithvalues(self):
config = self._makeOne()
Expand Down Expand Up @@ -63,6 +64,24 @@ def test_add_settings_settings_None(self):
settings = reg.getUtility(ISettings)
self.assertEqual(settings['a'], 1)

def test_settings_parameter_dict_is_never_updated(self):
class ReadOnlyDict(dict):
def __readonly__(self, *args, **kwargs): # pragma: no cover
raise RuntimeError("Cannot modify ReadOnlyDict")
__setitem__ = __readonly__
__delitem__ = __readonly__
pop = __readonly__
popitem = __readonly__
clear = __readonly__
update = __readonly__
setdefault = __readonly__
del __readonly__

initial = ReadOnlyDict()
config = self._makeOne(settings=initial)
config._set_settings({'a': '1'})


class TestSettings(unittest.TestCase):

def _getTargetClass(self):
Expand Down

0 comments on commit d3cb4b5

Please sign in to comment.