def modifies_known_mutable(obj, attr):
"""This function checks if an attribute on a builtin mutable object
(list, dict, set or deque) would modify it if called. It also supports
the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.) and
with Python 2.6 onwards the abstract base classes `MutableSet`,
`MutableMapping`, and `MutableSequence`.
>>> modifies_known_mutable({}, "clear")
True
>>> modifies_known_mutable({}, "keys")
False
>>> modifies_known_mutable([], "append")
True
>>> modifies_known_mutable([], "index")
False
If called with an unsupported object (such as unicode) `False` is
returned.
>>> modifies_known_mutable("foo", "upper")
False
"""
for typespec, unsafe in _mutable_spec:
if isinstance(obj, typespec):
return attr in unsafe
return False
python类UserDict()的实例源码
def modifies_known_mutable(obj, attr):
"""This function checks if an attribute on a builtin mutable object
(list, dict, set or deque) would modify it if called. It also supports
the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.) and
with Python 2.6 onwards the abstract base classes `MutableSet`,
`MutableMapping`, and `MutableSequence`.
>>> modifies_known_mutable({}, "clear")
True
>>> modifies_known_mutable({}, "keys")
False
>>> modifies_known_mutable([], "append")
True
>>> modifies_known_mutable([], "index")
False
If called with an unsupported object (such as unicode) `False` is
returned.
>>> modifies_known_mutable("foo", "upper")
False
"""
for typespec, unsafe in _mutable_spec:
if isinstance(obj, typespec):
return attr in unsafe
return False
def modifies_known_mutable(obj, attr):
"""This function checks if an attribute on a builtin mutable object
(list, dict, set or deque) would modify it if called. It also supports
the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.) and
with Python 2.6 onwards the abstract base classes `MutableSet`,
`MutableMapping`, and `MutableSequence`.
>>> modifies_known_mutable({}, "clear")
True
>>> modifies_known_mutable({}, "keys")
False
>>> modifies_known_mutable([], "append")
True
>>> modifies_known_mutable([], "index")
False
If called with an unsupported object (such as unicode) `False` is
returned.
>>> modifies_known_mutable("foo", "upper")
False
"""
for typespec, unsafe in _mutable_spec:
if isinstance(obj, typespec):
return attr in unsafe
return False
def modifies_known_mutable(obj, attr):
"""This function checks if an attribute on a builtin mutable object
(list, dict, set or deque) would modify it if called. It also supports
the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.) and
with Python 2.6 onwards the abstract base classes `MutableSet`,
`MutableMapping`, and `MutableSequence`.
>>> modifies_known_mutable({}, "clear")
True
>>> modifies_known_mutable({}, "keys")
False
>>> modifies_known_mutable([], "append")
True
>>> modifies_known_mutable([], "index")
False
If called with an unsupported object (such as unicode) `False` is
returned.
>>> modifies_known_mutable("foo", "upper")
False
"""
for typespec, unsafe in _mutable_spec:
if isinstance(obj, typespec):
return attr in unsafe
return False
def modifies_known_mutable(obj, attr):
"""This function checks if an attribute on a builtin mutable object
(list, dict, set or deque) would modify it if called. It also supports
the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.) and
with Python 2.6 onwards the abstract base classes `MutableSet`,
`MutableMapping`, and `MutableSequence`.
>>> modifies_known_mutable({}, "clear")
True
>>> modifies_known_mutable({}, "keys")
False
>>> modifies_known_mutable([], "append")
True
>>> modifies_known_mutable([], "index")
False
If called with an unsupported object (such as unicode) `False` is
returned.
>>> modifies_known_mutable("foo", "upper")
False
"""
for typespec, unsafe in _mutable_spec:
if isinstance(obj, typespec):
return attr in unsafe
return False
def modifies_known_mutable(obj, attr):
"""This function checks if an attribute on a builtin mutable object
(list, dict, set or deque) would modify it if called. It also supports
the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.) and
with Python 2.6 onwards the abstract base classes `MutableSet`,
`MutableMapping`, and `MutableSequence`.
>>> modifies_known_mutable({}, "clear")
True
>>> modifies_known_mutable({}, "keys")
False
>>> modifies_known_mutable([], "append")
True
>>> modifies_known_mutable([], "index")
False
If called with an unsupported object (such as unicode) `False` is
returned.
>>> modifies_known_mutable("foo", "upper")
False
"""
for typespec, unsafe in _mutable_spec:
if isinstance(obj, typespec):
return attr in unsafe
return False
def modifies_known_mutable(obj, attr):
"""This function checks if an attribute on a builtin mutable object
(list, dict, set or deque) would modify it if called. It also supports
the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.) and
with Python 2.6 onwards the abstract base classes `MutableSet`,
`MutableMapping`, and `MutableSequence`.
>>> modifies_known_mutable({}, "clear")
True
>>> modifies_known_mutable({}, "keys")
False
>>> modifies_known_mutable([], "append")
True
>>> modifies_known_mutable([], "index")
False
If called with an unsupported object (such as unicode) `False` is
returned.
>>> modifies_known_mutable("foo", "upper")
False
"""
for typespec, unsafe in _mutable_spec:
if isinstance(obj, typespec):
return attr in unsafe
return False
def modifies_known_mutable(obj, attr):
"""This function checks if an attribute on a builtin mutable object
(list, dict, set or deque) would modify it if called. It also supports
the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.) and
with Python 2.6 onwards the abstract base classes `MutableSet`,
`MutableMapping`, and `MutableSequence`.
>>> modifies_known_mutable({}, "clear")
True
>>> modifies_known_mutable({}, "keys")
False
>>> modifies_known_mutable([], "append")
True
>>> modifies_known_mutable([], "index")
False
If called with an unsupported object (such as unicode) `False` is
returned.
>>> modifies_known_mutable("foo", "upper")
False
"""
for typespec, unsafe in _mutable_spec:
if isinstance(obj, typespec):
return attr in unsafe
return False
def test_init(self):
for kw in 'self', 'other', 'iterable':
self.assertEqual(list(UserDict.UserDict(**{kw: 42}).items()),
[(kw, 42)])
self.assertEqual(list(UserDict.UserDict({}, dict=42).items()),
[('dict', 42)])
self.assertEqual(list(UserDict.UserDict({}, dict=None).items()),
[('dict', None)])
with test_support.check_warnings((".*'dict'.*",
PendingDeprecationWarning)):
self.assertEqual(list(UserDict.UserDict(dict={'a': 42}).items()),
[('a', 42)])
self.assertRaises(TypeError, UserDict.UserDict, 42)
self.assertRaises(TypeError, UserDict.UserDict, (), ())
self.assertRaises(TypeError, UserDict.UserDict.__init__)
def test_update(self):
for kw in 'self', 'other', 'iterable':
d = UserDict.UserDict()
d.update(**{kw: 42})
self.assertEqual(list(d.items()), [(kw, 42)])
d = UserDict.UserDict()
with test_support.check_warnings((".*'dict'.*",
PendingDeprecationWarning)):
d.update(dict={'a': 42})
self.assertEqual(list(d.items()), [('a', 42)])
self.assertRaises(TypeError, UserDict.UserDict().update, 42)
self.assertRaises(TypeError, UserDict.UserDict().update, {}, {})
self.assertRaises(TypeError, UserDict.UserDict.update)
def test_fromkeys(self):
TestMappingProtocol.test_fromkeys(self)
class mydict(self.type2test):
def __new__(cls):
return UserDict.UserDict()
ud = mydict.fromkeys('ab')
self.assertEqual(ud, {'a':None, 'b':None})
self.assertIsInstance(ud, UserDict.UserDict)
def test_setting_dict_to_invalid(self):
self.cannot_set_attr(self.b, '__dict__', None, TypeError)
self.cannot_set_attr(self.b, 'func_dict', None, TypeError)
from UserDict import UserDict
d = UserDict({'known_attr': 7})
self.cannot_set_attr(self.f.a.im_func, '__dict__', d, TypeError)
self.cannot_set_attr(self.fi.a.im_func, '__dict__', d, TypeError)
def test_plain(self):
f = self.makeCallable('a, b=1')
self.assertEqualCallArgs(f, '2')
self.assertEqualCallArgs(f, '2, 3')
self.assertEqualCallArgs(f, 'a=2')
self.assertEqualCallArgs(f, 'b=3, a=2')
self.assertEqualCallArgs(f, '2, b=3')
# expand *iterable / **mapping
self.assertEqualCallArgs(f, '*(2,)')
self.assertEqualCallArgs(f, '*[2]')
self.assertEqualCallArgs(f, '*(2, 3)')
self.assertEqualCallArgs(f, '*[2, 3]')
self.assertEqualCallArgs(f, '**{"a":2}')
self.assertEqualCallArgs(f, 'b=3, **{"a":2}')
self.assertEqualCallArgs(f, '2, **{"b":3}')
self.assertEqualCallArgs(f, '**{"b":3, "a":2}')
# expand UserList / UserDict
self.assertEqualCallArgs(f, '*UserList([2])')
self.assertEqualCallArgs(f, '*UserList([2, 3])')
self.assertEqualCallArgs(f, '**UserDict(a=2)')
self.assertEqualCallArgs(f, '2, **UserDict(b=3)')
self.assertEqualCallArgs(f, 'b=2, **UserDict(a=3)')
# unicode keyword args
self.assertEqualCallArgs(f, '**{u"a":2}')
self.assertEqualCallArgs(f, 'b=3, **{u"a":2}')
self.assertEqualCallArgs(f, '2, **{u"b":3}')
self.assertEqualCallArgs(f, '**{u"b":3, u"a":2}')
def test_varkw(self):
f = self.makeCallable('a, b=1, **c')
self.assertEqualCallArgs(f, 'a=2')
self.assertEqualCallArgs(f, '2, b=3, c=4')
self.assertEqualCallArgs(f, 'b=3, a=2, c=4')
self.assertEqualCallArgs(f, 'c=4, **{"a":2, "b":3}')
self.assertEqualCallArgs(f, '2, c=4, **{"b":3}')
self.assertEqualCallArgs(f, 'b=2, **{"a":3, "c":4}')
self.assertEqualCallArgs(f, '**UserDict(a=2, b=3, c=4)')
self.assertEqualCallArgs(f, '2, c=4, **UserDict(b=3)')
self.assertEqualCallArgs(f, 'b=2, **UserDict(a=3, c=4)')
# unicode keyword args
self.assertEqualCallArgs(f, 'c=4, **{u"a":2, u"b":3}')
self.assertEqualCallArgs(f, '2, c=4, **{u"b":3}')
self.assertEqualCallArgs(f, 'b=2, **{u"a":3, u"c":4}')
def test_multiple_features(self):
f = self.makeCallable('a, b=2, (c,(d,e))=(3,[4,5]), *f, **g')
self.assertEqualCallArgs(f, '2, 3, (4,[5,6]), 7')
self.assertEqualCallArgs(f, '2, 3, *[(4,[5,6]), 7], x=8')
self.assertEqualCallArgs(f, '2, 3, x=8, *[(4,[5,6]), 7]')
self.assertEqualCallArgs(f, '2, x=8, *[3, (4,[5,6]), 7], y=9')
self.assertEqualCallArgs(f, 'x=8, *[2, 3, (4,[5,6])], y=9')
self.assertEqualCallArgs(f, 'x=8, *UserList([2, 3, (4,[5,6])]), '
'**{"y":9, "z":10}')
self.assertEqualCallArgs(f, '2, x=8, *UserList([3, (4,[5,6])]), '
'**UserDict(y=9, z=10)')
def test_init(self):
for kw in 'self', 'other', 'iterable':
self.assertEqual(list(UserDict.UserDict(**{kw: 42}).items()),
[(kw, 42)])
self.assertEqual(list(UserDict.UserDict({}, dict=42).items()),
[('dict', 42)])
self.assertEqual(list(UserDict.UserDict({}, dict=None).items()),
[('dict', None)])
with test_support.check_warnings((".*'dict'.*",
PendingDeprecationWarning)):
self.assertEqual(list(UserDict.UserDict(dict={'a': 42}).items()),
[('a', 42)])
self.assertRaises(TypeError, UserDict.UserDict, 42)
self.assertRaises(TypeError, UserDict.UserDict, (), ())
self.assertRaises(TypeError, UserDict.UserDict.__init__)
def test_update(self):
for kw in 'self', 'other', 'iterable':
d = UserDict.UserDict()
d.update(**{kw: 42})
self.assertEqual(list(d.items()), [(kw, 42)])
d = UserDict.UserDict()
with test_support.check_warnings((".*'dict'.*",
PendingDeprecationWarning)):
d.update(dict={'a': 42})
self.assertEqual(list(d.items()), [('a', 42)])
self.assertRaises(TypeError, UserDict.UserDict().update, 42)
self.assertRaises(TypeError, UserDict.UserDict().update, {}, {})
self.assertRaises(TypeError, UserDict.UserDict.update)
def test_fromkeys(self):
TestMappingProtocol.test_fromkeys(self)
class mydict(self.type2test):
def __new__(cls):
return UserDict.UserDict()
ud = mydict.fromkeys('ab')
self.assertEqual(ud, {'a':None, 'b':None})
self.assertIsInstance(ud, UserDict.UserDict)
def test_setting_dict_to_invalid(self):
self.cannot_set_attr(self.b, '__dict__', None, TypeError)
self.cannot_set_attr(self.b, 'func_dict', None, TypeError)
from UserDict import UserDict
d = UserDict({'known_attr': 7})
self.cannot_set_attr(self.f.a.im_func, '__dict__', d, TypeError)
self.cannot_set_attr(self.fi.a.im_func, '__dict__', d, TypeError)
def test_plain(self):
f = self.makeCallable('a, b=1')
self.assertEqualCallArgs(f, '2')
self.assertEqualCallArgs(f, '2, 3')
self.assertEqualCallArgs(f, 'a=2')
self.assertEqualCallArgs(f, 'b=3, a=2')
self.assertEqualCallArgs(f, '2, b=3')
# expand *iterable / **mapping
self.assertEqualCallArgs(f, '*(2,)')
self.assertEqualCallArgs(f, '*[2]')
self.assertEqualCallArgs(f, '*(2, 3)')
self.assertEqualCallArgs(f, '*[2, 3]')
self.assertEqualCallArgs(f, '**{"a":2}')
self.assertEqualCallArgs(f, 'b=3, **{"a":2}')
self.assertEqualCallArgs(f, '2, **{"b":3}')
self.assertEqualCallArgs(f, '**{"b":3, "a":2}')
# expand UserList / UserDict
self.assertEqualCallArgs(f, '*UserList([2])')
self.assertEqualCallArgs(f, '*UserList([2, 3])')
self.assertEqualCallArgs(f, '**UserDict(a=2)')
self.assertEqualCallArgs(f, '2, **UserDict(b=3)')
self.assertEqualCallArgs(f, 'b=2, **UserDict(a=3)')
# unicode keyword args
self.assertEqualCallArgs(f, '**{u"a":2}')
self.assertEqualCallArgs(f, 'b=3, **{u"a":2}')
self.assertEqualCallArgs(f, '2, **{u"b":3}')
self.assertEqualCallArgs(f, '**{u"b":3, u"a":2}')