diff options
| author | John Anderson <sontek@gmail.com> | 2015-01-01 20:06:48 -0800 |
|---|---|---|
| committer | John Anderson <sontek@gmail.com> | 2015-01-01 20:06:48 -0800 |
| commit | 9e7248258800ef2c7072f497901172ab94988708 (patch) | |
| tree | 43f577285eb1d1bc41a7d168e4202d6594fe7b0b | |
| parent | 731a8e8380bbf9b41298c0417795e68899b91953 (diff) | |
| download | pyramid-9e7248258800ef2c7072f497901172ab94988708.tar.gz pyramid-9e7248258800ef2c7072f497901172ab94988708.tar.bz2 pyramid-9e7248258800ef2c7072f497901172ab94988708.zip | |
Catch bad `name` and raise a `ValueError`
| -rw-r--r-- | pyramid/tests/test_config/test_factories.py | 18 | ||||
| -rw-r--r-- | pyramid/util.py | 10 |
2 files changed, 26 insertions, 2 deletions
diff --git a/pyramid/tests/test_config/test_factories.py b/pyramid/tests/test_config/test_factories.py index 6e679397f..5ae486c4b 100644 --- a/pyramid/tests/test_config/test_factories.py +++ b/pyramid/tests/test_config/test_factories.py @@ -111,6 +111,22 @@ class TestFactoriesMixin(unittest.TestCase): config = self._makeOne(autocommit=True) self.assertRaises(AttributeError, config.add_request_method) + def test_add_request_method_with_text_type_name(self): + from pyramid.interfaces import IRequestExtensions + from pyramid.compat import text_ + from pyramid.util import InstancePropertyMixin + + config = self._makeOne(autocommit=True) + def boomshaka(r): pass + name = text_(b'La Pe\xc3\xb1a', 'utf-8') + config.add_request_method(boomshaka, name=name) + exts = config.registry.getUtility(IRequestExtensions) + inst = InstancePropertyMixin() + + def set_extensions(): + inst._set_extensions(exts) + self.assertRaises(ValueError, set_extensions) + self.assertTrue(name in exts.methods) class TestDeprecatedFactoriesMixinMethods(unittest.TestCase): def setUp(self): @@ -120,7 +136,7 @@ class TestDeprecatedFactoriesMixinMethods(unittest.TestCase): def tearDown(self): from zope.deprecation import __show__ __show__.on() - + def _makeOne(self, *arg, **kw): from pyramid.config import Configurator config = Configurator(*arg, **kw) diff --git a/pyramid/util.py b/pyramid/util.py index 6de53d559..e9f5760a6 100644 --- a/pyramid/util.py +++ b/pyramid/util.py @@ -111,7 +111,15 @@ class InstancePropertyMixin(object): def _set_extensions(self, extensions): for name, fn in iteritems_(extensions.methods): method = fn.__get__(self, self.__class__) - setattr(self, name, method) + try: + setattr(self, name, method) + except UnicodeEncodeError: + msg = ( + '`name="%s"` is invalid. `name` must be ascii because it is ' + 'used on __name__ of the method' + ) + raise ValueError(msg % name) + self._set_properties(extensions.descriptors) def set_property(self, callable, name=None, reify=False): |
