[Zope-Checkins] CVS: Releases/Zope/lib/python/Products/PythonScripts - standard.py:1.11.2.1

Evan Simpson evan@zope.com
Wed, 4 Dec 2002 13:28:38 -0500


Update of /cvs-repository/Releases/Zope/lib/python/Products/PythonScripts
In directory cvs.zope.org:/tmp/cvs-serv32144/lib/python/Products/PythonScripts

Modified Files:
      Tag: Zope-2_6-branch
	standard.py 
Log Message:
Prevent setting of attributes with leading underscores on Object instances,
and make it easier to initialize/update Objects.


=== Releases/Zope/lib/python/Products/PythonScripts/standard.py 1.11 => 1.11.2.1 ===
--- Releases/Zope/lib/python/Products/PythonScripts/standard.py:1.11	Wed Sep  4 18:19:09 2002
+++ Releases/Zope/lib/python/Products/PythonScripts/standard.py	Wed Dec  4 13:28:37 2002
@@ -61,10 +61,30 @@
 class _Object(record):
     _guarded_writes = 1
 
+    def __init__(self, **kw):
+        self.update(kw)
+
     def __setitem__(self, key, value):
-        self.__dict__[str(key)] = value
+        key = str(key)
+        if key.startswith('_'):
+            raise ValueError, ('Object key %s is invalid. '
+                               'Keys may not begin with an underscore.' % `key`)
+        self.__dict__[key] = value
+
+    def update(self, d):
+        for key in d.keys():
+            # Ignore invalid keys, rather than raising an exception.
+            try:
+                skey = str(key)
+            except:
+                continue
+            if skey==key and not skey.startswith('_'):
+                self.__dict__[skey] = d[key]
+
+    def __hash__(self):
+        return id(self)
 
-def Object():
-    return _Object()
+def Object(**kw):
+    return _Object(**kw)
 
 security.apply(globals())