From 0ff9d4fb7c23a505d11666dfd4410fb5d7a8d473 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Mon, 29 Aug 2011 10:22:15 -0400 Subject: - The ``settings`` object emitted a deprecation warning any time ``__getattr__`` was called upon it. However, there are legitimate situations in which ``__getattr__`` is called on arbitrary objects (e.g. ``hasattr``). Now, the ``settings`` object only emits the warning upon successful lookup. --- CHANGES.txt | 8 +++++++- pyramid/config/settings.py | 21 ++++++++++++--------- pyramid/tests/test_config/test_settings.py | 23 ++++++++++++++++++----- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 48ef24417..ce42e2136 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -6,7 +6,13 @@ Bug Fixes - Pyramid did not properly generate static URLs using ``pyramid.url.static_url`` when passed a caller-package relative path due - to a refactoring. + to a refactoring done in 1.2a1. + +- The ``settings`` object emitted a deprecation warning any time + ``__getattr__`` was called upon it. However, there are legitimate + situations in which ``__getattr__`` is called on arbitrary objects + (e.g. ``hasattr``). Now, the ``settings`` object only emits the warning + upon successful lookup. Internal -------- diff --git a/pyramid/config/settings.py b/pyramid/config/settings.py index f3ff541fa..6e636bf58 100644 --- a/pyramid/config/settings.py +++ b/pyramid/config/settings.py @@ -1,6 +1,6 @@ import os +import warnings -from zope.deprecation import deprecate from zope.interface import implements from pyramid.interfaces import ISettings @@ -142,16 +142,19 @@ class Settings(dict): self.update(update) - dictlike = ('Use of the request as a dict-like object is deprecated as ' - 'of Pyramid 1.1. Use dict-like methods of "request.environ" ' - 'instead.') - - @deprecate('Obtaining settings via attributes of the settings dictionary ' - 'is deprecated as of Pyramid 1.2; use settings["foo"] instead ' - 'of settings.foo') def __getattr__(self, name): try: - return self[name] + val = self[name] + # only deprecate on success; a probing getattr/hasattr should not + # print this warning + warnings.warn( + 'Obtaining settings via attributes of the settings dictionary ' + 'is deprecated as of Pyramid 1.2; use settings["foo"] instead ' + 'of settings.foo', + DeprecationWarning, + 2 + ) + return val except KeyError: raise AttributeError(name) diff --git a/pyramid/tests/test_config/test_settings.py b/pyramid/tests/test_config/test_settings.py index 3ecdb4853..7fbffda91 100644 --- a/pyramid/tests/test_config/test_settings.py +++ b/pyramid/tests/test_config/test_settings.py @@ -2,12 +2,19 @@ import unittest class TestSettings(unittest.TestCase): def setUp(self): - from zope.deprecation import __show__ - __show__.off() + self.warnings = [] + import warnings + warnings.old_showwarning = warnings.showwarning + warnings.showwarning = self._showwarning def tearDown(self): - from zope.deprecation import __show__ - __show__.on() + del self.warnings + import warnings + warnings.showwarning = warnings.old_showwarning + + def _showwarning(self, message, category, filename, lineno, file=None, + line=None): + self.warnings.append(message) def _getTargetClass(self): from pyramid.config.settings import Settings @@ -19,9 +26,15 @@ class TestSettings(unittest.TestCase): klass = self._getTargetClass() return klass(d, _environ_=environ) - def test_getattr(self): + def test_getattr_success(self): settings = self._makeOne({'reload_templates':False}) self.assertEqual(settings.reload_templates, False) + self.assertEqual(len(self.warnings), 1) + + def test_getattr_fail(self): + settings = self._makeOne({}) + self.assertRaises(AttributeError, settings.__getattr__, 'wontexist') + self.assertEqual(len(self.warnings), 0) def test_getattr_raises_attribute_error(self): settings = self._makeOne() -- cgit v1.2.3