[ZODB-Dev] Pre-announce: Oscar 0.1

Greg Ward gward@mems-exchange.org
Mon, 27 Aug 2001 16:48:31 -0400


On 27 August 2001, Christian Robottom Reis said:
> What I meant was that we could use Grouch to raise exceptions which we
> handled in our user interface code. Something like this:
> 
> def __setattr__(self, name, value):
> 	_some_grouch_checktype(name, value)
> 	# do the attribution
> 
> on Domain classes. And

I think that's perfectly feasible.  Grouch is massive flaming overkill
in this case, since users don't generally enter data in the form of
"list of Foo instances, where a Foo instance has attributes a, b, and c,
where a is a list of strings, and b is a ...".

You would probably want to do something like this:
  * use gen_schema to generate a schema.pkl before releasing your code,
    and include this schema.pkl with your code
  * when your app starts up, it loads schema.pkl and stores the schema
    in a global somewhere

The __setattr__ for a domain class might look like this:

  def __setattr__ (self, attr, value):
      setattr(self, attr, value)
      schema.check_value(self)

If someone does "object.adsljkfdf = 666", then the check_value() call
will quickly catch the bogus attribute name and blowup.  Likewise, if
someone does "object.foo = '37'" when foo is declared as an int,
it will blowup.

The principal danger here is that check_value() will recurse your object
graph as deep as it goes; there are currently no limits on what it will
visit as long as it finds no errors.  (If it finds an instance of a
class unknown to the schema, it emits an error and skips that object.)

If you want to catch such errors before making the assignment, life gets
trickier.  But since you're just going to blowup, I don't see that it's
really worth it.
  
        Greg
-- 
Greg Ward - software developer                gward@mems-exchange.org
MEMS Exchange                            http://www.mems-exchange.org