[Patches] UserDict/UserList: raise TypeError when trying to set attrs
Gerrit Holl
Gerrit <gerrit@nl.linux.org>
Mon, 14 Feb 2000 17:40:04 +0100
--BXVAT5kNtrzKuDFl
Content-Type: text/plain; charset=us-ascii
Hello Python gurus,
I added a __setattr__ to UserDict and UserList. When trying to set
attributes to dictionairies and lists, an error is raised. In
UserDict and UserList, that's not true:
>>> l=[]
>>> l.a=1
Traceback (innermost last):
File "<stdin>", line 1, in ?
TypeError: object has read-only attributes
>>> from UserList import UserList
>>> l=UserList()
>>> l.a=1
This patch fixes it:
>>> from UserList import UserList
>>> l=UserList()
>>> l.a=1
Traceback (innermost last):
File "<stdin>", line 1, in ?
File "UserList.py", line 57, in __setattr__
raise TypeError("object has read-only attributes")
TypeError: object has read-only attributes
The 'data' attribute can be set though; it's not possible to prohibit this.
I hope you've found some time to read this mail and I hope you'll find
some more time either to include my patch or notify me why not doing so.
(was this sentence correct English?)
I confirm that, to the best of my knowledge and belief, this
contribution is free of any claims of third parties under
copyright, patent or other rights or interests ("claims"). To
the extent that I have any such claims, I hereby grant to CNRI a
nonexclusive, irrevocable, royalty-free, worldwide license to
reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part
of the Python software and its related documentation, or any
derivative versions thereof, at no cost to CNRI or its licensed
users, and to authorize others to do so.
I acknowledge that CNRI may, at its sole discretion, decide
whether or not to incorporate this contribution in the Python
software and its related documentation. I further grant CNRI
permission to use my name and other identifying information
provided to CNRI by me for use in connection with the Python
software and its related documentation.
regards,
Gerrit.
--
Homepage: http://www.nl.linux.org/~gerrit
-----BEGIN GEEK CODE BLOCK----- http://www.geekcode.com
Version: 3.12
GCS dpu s-:-- a14 C++++>$ UL++ P--- L+++ E--- W++ N o? K? w--- !O
!M !V PS+ PE? Y? PGP-- t- 5? X? R- tv- b+(++) DI D+ G++ !e !r !y
-----END GEEK CODE BLOCK----- moc.edockeeg.www//:ptth
--BXVAT5kNtrzKuDFl
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="UserList.py.diff"
--- UserList.py Mon Feb 14 17:31:21 2000
+++ /tmp/UserList.py Mon Feb 14 17:28:21 2000
@@ -51,10 +51,6 @@
def __mul__(self, n):
return self.__class__(self.data*n)
__rmul__ = __mul__
- def __setattr__(self, key, val):
- import types
- if key != 'data' or type(val) is not types.ListType:
- raise TypeError("object has read-only attributes")
def append(self, item): self.data.append(item)
def insert(self, i, item): self.data.insert(i, item)
def pop(self, i=-1): return self.data.pop(i)
--BXVAT5kNtrzKuDFl
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="UserDict.py.diff"
--- UserDict.py Mon Feb 14 17:30:46 2000
+++ /tmp/UserDict.py.diff Mon Feb 14 17:31:41 2000
@@ -1,40 +0,0 @@
-"""A more or less complete user-defined wrapper around dictionary objects."""
-
-class UserDict:
- def __init__(self, dict=None):
- self.data = {}
- if dict is not None: self.update(dict)
- def __repr__(self): return repr(self.data)
- def __cmp__(self, dict):
- if isinstance(dict, UserDict):
- return cmp(self.data, dict.data)
- else:
- return cmp(self.data, dict)
- def __len__(self): return len(self.data)
- def __getitem__(self, key): return self.data[key]
- def __setitem__(self, key, item): self.data[key] = item
- def __delitem__(self, key): del self.data[key]
- def __setattr__(self, key, val):
- import types
- if key != 'data' or type(val) is not types.DictType:
- raise TypeError("object has read-only attributes")
- def clear(self): self.data.clear()
- def copy(self):
- if self.__class__ is UserDict:
- return UserDict(self.data)
- import copy
- return copy.copy(self)
- def keys(self): return self.data.keys()
- def items(self): return self.data.items()
- def values(self): return self.data.values()
- def has_key(self, key): return self.data.has_key(key)
- def update(self, dict):
- if isinstance(dict, UserDict):
- self.data.update(dict.data)
- elif isinstance(dict, type(self.data)):
- self.data.update(dict)
- else:
- for k, v in dict.items():
- self.data[k] = v
- def get(self, key, failobj=None):
- return self.data.get(key, failobj)
--BXVAT5kNtrzKuDFl--