Permalink
Browse files

mirror some deprecation changes from sergey-1.2-py2-only branch

* warn_deprecation raises exceptions for 1.2 deprecations (first_match, weak_match, resp.body_file = 'x')
* Request.str_* and Response.environ/request raise deprecation exceptions when touched
* change deprecated_property implementation
  • Loading branch information...
1 parent 21591d6 commit 627bdbe8d049f22ae2769d375de2928b35b85865 @maluke maluke committed Sep 24, 2011
Showing with 65 additions and 132 deletions.
  1. +0 −18 tests/test_acceptparse.py
  2. +6 −8 tests/test_descriptors.py
  3. +1 −29 tests/test_etag.py
  4. +1 −1 tests/test_exc.py
  5. +4 −22 tests/test_request.py
  6. +5 −21 tests/test_util.py
  7. +30 −29 webob/descriptors.py
  8. +9 −2 webob/request.py
  9. +6 −0 webob/response.py
  10. +3 −2 webob/util.py
@@ -59,7 +59,6 @@ def test_accept_str():
def test_zero_quality():
assert Accept('bar, *;q=0').best_match(['foo']) is None
assert 'foo' not in Accept('*;q=0')
- assert Accept('foo, *;q=0').first_match(['bar', 'foo']) == 'foo'
def test_accept_str_with_q_not_1():
@@ -123,17 +122,6 @@ def test_quality_not_found():
accept = Accept('text/html')
assert accept.quality('foo/bar') is None
-def test_first_match():
- accept = Accept('text/html, foo/bar')
- assert accept.first_match(['text/html', 'foo/bar']) == 'text/html'
- assert accept.first_match(['foo/bar', 'text/html']) == 'foo/bar'
- assert accept.first_match(['xxx/xxx', 'text/html']) == 'text/html'
- assert accept.first_match(['xxx/xxx']) == 'xxx/xxx'
- assert accept.first_match([None, 'text/html']) is None
- assert accept.first_match(['text/html', None]) == 'text/html'
- assert accept.first_match(['foo/bar', None]) == 'foo/bar'
- assert_raises(ValueError, accept.first_match, [])
-
def test_best_match():
accept = Accept('text/html, foo/bar')
assert accept.best_match(['text/html', 'foo/bar']) == 'text/html'
@@ -236,12 +224,6 @@ def test_nil_contains():
nilaccept = NilAccept()
assert 'anything' in nilaccept
-def test_nil_first_match():
- nilaccept = NilAccept()
- # NilAccept.first_match always returns element 0 of the list
- assert nilaccept.first_match(['dummy', '']) == 'dummy'
- assert nilaccept.first_match(['', 'dummy']) == ''
-
def test_nil_best_match():
nilaccept = NilAccept()
assert nilaccept.best_match(['foo', 'bar']) == 'foo'
@@ -380,14 +380,12 @@ def test_date_header_fdel():
eq_(desc.fget(resp), None)
def test_deprecated_property():
- from webob.descriptors import deprecated_property
- class Foo(object):
- foo = deprecated_property('foo', 'deprecated')
- foo = Foo()
- assert_raises(DeprecationWarning, getattr, foo, 'foo')
- assert_raises(DeprecationWarning, setattr, foo, 'foo', {})
- assert_raises(DeprecationWarning, delattr, foo, 'foo')
- eq_(Foo.foo.__repr__(), "<Deprecated attribute foo>")
+ from webob import Response
+ assert_raises(DeprecationWarning, Response, environ={})
+ resp = Response()
+ assert_raises(DeprecationWarning, getattr, resp, 'environ')
+ assert_raises(DeprecationWarning, setattr, resp, 'environ', {})
+ assert_raises(DeprecationWarning, delattr, resp, 'environ')
def test_parse_etag_response():
from webob.descriptors import parse_etag_response
View
@@ -84,7 +84,7 @@ def test___contains__something(self):
def test_weak_match_something(self):
etag = self._makeOne()
- self.assertEqual(etag.weak_match('anything'), True)
+ self.assertRaises(DeprecationWarning, etag.weak_match, 'anything')
def test___str__(self):
etag = self._makeOne()
@@ -110,14 +110,6 @@ def test___contains__something(self):
etag = self._makeOne()
assert 'anything' not in etag
- def test_weak_match_None(self):
- etag = self._makeOne()
- self.assertEqual(etag.weak_match(None), False)
-
- def test_weak_match_something(self):
- etag = self._makeOne()
- assert not etag.weak_match('anything')
-
def test___str__(self):
etag = self._makeOne()
self.assertEqual(str(etag), '')
@@ -155,26 +147,6 @@ def test___contains__None(self):
matcher = self._makeOne(("ETAGS",), ("WEAK",))
self.assertFalse(None in matcher)
- def test_weak_match_etags(self):
- matcher = self._makeOne(("ETAGS",), ("WEAK",))
- self.assertTrue(matcher.weak_match("W/ETAGS"))
-
- def test_weak_match_weak_etags(self):
- matcher = self._makeOne(("ETAGS",), ("WEAK",))
- self.assertTrue(matcher.weak_match("W/WEAK"))
-
- def test_weak_match_weak_not(self):
- matcher = self._makeOne(("ETAGS",), ("WEAK",))
- self.assertFalse(matcher.weak_match("W/BEER"))
-
- def test_weak_match_weak_wo_wslash(self):
- matcher = self._makeOne(("ETAGS",), ("WEAK",))
- self.assertTrue(matcher.weak_match("ETAGS"))
-
- def test_weak_match_weak_wo_wslash_not(self):
- matcher = self._makeOne(("ETAGS",), ("WEAK",))
- self.assertFalse(matcher.weak_match("BEER"))
-
def test___repr__one(self):
matcher = self._makeOne(("ETAGS",), ("WEAK",))
self.assertEqual(matcher.__repr__(), '<ETag ETAGS>')
View
@@ -57,7 +57,7 @@ def _response(environ, start_response):
start_response = object()
exc = HTTPException('testing', _response)
ok_(exc.wsgi_response is _response)
- ok_(exc.exception is exc)
+ assert_raises(DeprecationWarning, getattr, exc, 'exception')
result = exc(environ, start_response)
ok_(result is result)
assert_equal(_called, [(environ, start_response)])
View
@@ -65,25 +65,8 @@ def test_body_file_getter_unreadable(self):
assert req.body_file.read() == b''
def test_body_file_setter_w_string(self):
- data = b'input'
- BEFORE = BytesIO(data)
- AFTER = b'AFTER'
- environ = {
- 'wsgi.input': BEFORE,
- 'CONTENT_LENGTH': str(len(data)),
- 'REQUEST_METHOD': 'POST',
- }
- req = BaseRequest(environ)
- warnings.simplefilter('ignore', PendingDeprecationWarning)
- req.body_file = AFTER
- warnings.resetwarnings()
- self.assertEqual(req.content_length, len(AFTER))
- self.assertEqual(req.body_file.read(), AFTER)
- del req.body_file
- self.assertEqual(req.content_length, 0)
- assert req.is_body_seekable
- req.body_file.seek(0)
- self.assertEqual(req.body_file.read(), b'')
+ req = BaseRequest.blank('/')
+ self.assertRaises(DeprecationWarning, setattr, req, 'body_file', b'foo')
def test_body_file_setter_non_string(self):
BEFORE = BytesIO(b'before')
@@ -2324,8 +2307,7 @@ def test_request_put(self):
self.assert_(isinstance(req.accept, MIMEAccept))
self.assert_('text/html' in req.accept)
- self.assertEqual(req.accept.first_match(['text/html',
- 'application/xhtml+xml']), 'text/html')
+ self.assertRaises(DeprecationWarning, req.accept.first_match, ['text/html'])
self.assertEqual(req.accept.best_match(['text/html',
'application/xhtml+xml']),
'application/xhtml+xml')
@@ -2643,4 +2625,4 @@ def fileno(self):
dummyfile = DummyFile()
inst = self._makeOne(dummyfile, 0)
self.assertEqual(inst.fileno(), 1)
-
+
View
@@ -20,27 +20,6 @@ def _callFUT(self, text, version, stacklevel):
def _warn(self, text, type, stacklevel=1):
self.warnings.append(locals())
- def test_not_1_2(self):
- self._callFUT('text', 'version', 1)
- self.assertEqual(len(self.warnings), 2)
- unknown_version_warning = self.warnings[0]
- self.assertEqual(unknown_version_warning['text'],
- "Unknown warn_deprecation version arg: 'version'")
- self.assertEqual(unknown_version_warning['type'], RuntimeWarning)
- self.assertEqual(unknown_version_warning['stacklevel'], 1)
- deprecation_warning = self.warnings[1]
- self.assertEqual(deprecation_warning['text'], 'text')
- self.assertEqual(deprecation_warning['type'], DeprecationWarning)
- self.assertEqual(deprecation_warning['stacklevel'], 2)
-
- def test_is_1_2(self):
- self._callFUT('text', '1.2', 1)
- self.assertEqual(len(self.warnings), 1)
- deprecation_warning = self.warnings[0]
- self.assertEqual(deprecation_warning['text'], 'text')
- self.assertEqual(deprecation_warning['type'], DeprecationWarning)
- self.assertEqual(deprecation_warning['stacklevel'], 2)
-
def test_multidict_update_warning(self):
# test warning when duplicate keys are passed
r = Response()
@@ -58,3 +37,8 @@ def test_multidict_update_warning_unnecessary(self):
r = Response()
r.headers.update([('Set-Cookie', 'a=b')])
self.assertEqual(len(self.warnings), 0)
+
+ def test_warn_deprecation(self):
+ from webob import __version__ as v
+ from webob.util import warn_deprecation
+ self.assertRaises(DeprecationWarning, warn_deprecation, 'foo', v[:3], 1)
View
@@ -28,7 +28,11 @@
NoIfRange,
)
-from webob.util import header_docstring
+from webob.util import (
+ header_docstring,
+ warn_deprecation,
+ )
+
CHARSET_RE = re.compile(r';\s*charset=([^;]*)', re.I)
QUOTES_RE = re.compile('"(.*)"')
@@ -69,6 +73,30 @@ def fset(req, val):
req.environ[key] = native_(val, 'utf-8', req.unicode_errors)
return property(fget, fset, doc='upath_property(%r)' % key)
+def deprecated_property(attr, name, text, version):
+ """
+ Wraps a descriptor, with a deprecation warning or error
+ """
+ def warn():
+ warn_deprecation('The attribute %s is deprecated: %s'
+ % (attr, text),
+ version,
+ 3
+ )
+ def fget(self):
+ warn()
+ return attr.__get__(self, type(self))
+ def fset(self, val):
+ warn()
+ attr.__set__(self, val)
+ def fdel(self):
+ warn()
+ attr.__delete__(self)
+ return property(fget, fset, fdel,
+ '<Deprecated attribute %s>' % attr
+ )
+
+
def header_getter(header, rfc_section):
doc = header_docstring(header, rfc_section)
key = header.lower()
@@ -143,34 +171,6 @@ def date_header(header, rfc_section):
-class deprecated_property(object):
- """
- Wraps a descriptor, with a deprecation warning or error
- """
- def __init__(self, attr, message):
- self.attr = attr
- self.message = message
-
- def __get__(self, obj, type=None):
- if obj is None:
- return self
- self.warn()
-
- def __set__(self, obj, value):
- self.warn()
-
- def __delete__(self, obj):
- self.warn()
-
- def __repr__(self):
- return '<Deprecated attribute %s>' % self.attr
-
- def warn(self):
- raise DeprecationWarning('The attribute %s is deprecated: %s'
- % (self.attr, self.message)
- )
-
-
########################
## Converter functions
@@ -298,3 +298,4 @@ def serialize_auth(val):
assert isinstance(params, str)
return '%s %s' % (authtype, params)
return val
+
View
@@ -54,6 +54,7 @@
serialize_int,
serialize_range,
upath_property,
+ deprecated_property,
)
from webob.etag import (
@@ -971,7 +972,7 @@ def as_bytes(self, skip_body=False):
def as_string(self, skip_body=False):
warn_deprecation(
"Please use req.as_bytes",
- '1.2',
+ '1.3',
self._setattr_stacklevel
)
return self.as_bytes(skip_body=skip_body)
@@ -998,7 +999,7 @@ def from_bytes(cls, b):
def from_string(cls, b):
warn_deprecation(
"Please use req.from_bytes",
- '1.2',
+ '1.3',
cls._setattr_stacklevel
)
return cls.from_bytes(b)
@@ -1447,3 +1448,9 @@ def _get_charset(content_type):
else:
return 'UTF-8'
+
+# TODO: remove in 1.4
+for _name in 'GET POST params cookies'.split():
+ _str_name = 'str_'+_name
+ _prop = deprecated_property(None, _str_name, "disabled starting WebOb 1.2, use %s instead" % _name, '1.2')
+ setattr(BaseRequest, _str_name, _prop)
View
@@ -51,6 +51,7 @@
serialize_content_range,
serialize_etag_response,
serialize_int,
+ deprecated_property,
)
from webob.headers import ResponseHeaders
@@ -1177,3 +1178,8 @@ def _error_unicode_in_app_iter(app_iter, body):
raise TypeError(
'An item of the app_iter (%s) was text, causing a '
'text body: %r' % (app_iter_repr, body))
+
+
+# TODO: remove in 1.4
+Response.request = deprecated_property(None, 'request', 'Response.request will be removed completely in 1.4', '1.2')
+Response.environ = deprecated_property(None, 'environ', 'Response.environ will be removed completely in 1.4', '1.2')
View
@@ -44,12 +44,13 @@ def header_docstring(header, rfc_section):
return "Gets and sets the ``%s`` header (`HTTP spec section %s <%s>`_)." % (
header, rfc_section, link)
+
def warn_deprecation(text, version, stacklevel): # pragma: no cover
# version specifies when to start raising exceptions instead of warnings
if version == '1.2':
- cls = DeprecationWarning
+ raise DeprecationWarning(text)
elif version == '1.3':
- cls = PendingDeprecationWarning
+ cls = DeprecationWarning
else:
cls = DeprecationWarning
warnings.warn("Unknown warn_deprecation version arg: %r" % version,

0 comments on commit 627bdbe

Please sign in to comment.