diff --git a/IPython/ipapi.py b/IPython/ipapi.py index d25d679..91df0ee 100644 --- a/IPython/ipapi.py +++ b/IPython/ipapi.py @@ -169,6 +169,12 @@ class IPApi: def get_options(self): """All configurable variables.""" + + # catch typos by disabling new attribute creation. If new attr creation + # is in fact wanted (e.g. when exposing new options), do allow_new_attr(True) + # for the received rc struct. + + self.IP.rc.allow_new_attr(False) return self.IP.rc options = property(get_options,None,None,get_options.__doc__) diff --git a/IPython/ipstruct.py b/IPython/ipstruct.py index eb3356f..d077ac4 100644 --- a/IPython/ipstruct.py +++ b/IPython/ipstruct.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Mimic C structs with lots of extra functionality. -$Id: ipstruct.py 1005 2006-01-12 08:39:26Z fperez $""" +$Id: ipstruct.py 1949 2006-11-28 19:12:15Z vivainio $""" #***************************************************************************** # Copyright (C) 2001-2004 Fernando Perez @@ -105,6 +105,7 @@ class Struct: Both can be used, but the dictionary must come first: Struct(dict), Struct(k1=v1,k2=v2) or Struct(dict,k1=v1,k2=v2). """ + self.__dict__['__allownew'] = True if dict is None: dict = {} if isinstance(dict,Struct): @@ -117,11 +118,17 @@ class Struct: # safety-checked __setitem__ for k,v in dict.items(): self[k] = v + def __setitem__(self,key,value): """Used when struct[key] = val calls are made.""" if key in Struct.__protected: raise KeyError,'Key '+`key`+' is a protected key of class Struct.' + if not self['__allownew'] and key not in self.__dict__: + raise KeyError( + "Can't create unknown attribute %s - Check for typos, or use allow_new_attr to create new attributes!" % + key) + self.__dict__[key] = value def __setattr__(self, key, value): @@ -372,5 +379,15 @@ class Struct: if not self.has_key(attr): self[attr] = val return self.get(attr,val) + + def allow_new_attr(self, allow = True): + """ Set whether new attributes can be created inside struct + + This can be used to catch typos by verifying that the attribute user tries to + change already exists in this Struct. + """ + self['__allownew'] = allow + + # end class Struct