Make message ids into "rocks", was Re: [Zope3-dev] Re: SVN: Zope3/trunk/src/zope/ Internationalized apidoc modules description.

Jim Fulton jim at zope.com
Fri Sep 3 07:19:42 EDT 2004


Dmitry Vasiliev wrote:
> Philipp von Weitershausen wrote:
> 
>> Jim Fulton wrote:
>>
>>>  > Since this problem occurred only as you internationalized API doc,
>>>
>>>> we should be able to quickly determine the place where we should 
>>>> call the removeSecurityProxy() call.
>>>
>>>
>>> A better aproach is to add necessary security declarations for 
>>> message ids.
>>>
>>> It would be nice if we could make message ids into "rocks" so they 
>>> didn't
>>> have to be proxied.  We would have to make them immutable though, and
>>> be careful about the data they contained.
>>
>>
>>
>> I would like them to be rocks, too. Their semantics right now are a 
>> bit problematic for this, though. Consider a message id:
>>
>>   >>> from zope.i18nmessageid import MessageIDFactory
>>   >>> _ = MessageIDFactory('zope')
>>   >>> msg = _(u"foo-label", u"Foo")
>>   >>> msg
>>   u'foo-label'
>>   >>> msg.default
>>   u'Foo'
>>   >>> msg.mapping
>>   {}
>>
>> Since the attributes are writable, message ids are mutable:
>>
>>   >>> msg.default = u"Foo bar"
>>   >>> msg.mapping = {'foo':'bar'}
>>
>> or
>>
>>   >>> msg.mapping.update({'foo':'bar'})
>>
>> I would suggest to
>>
>> 1.) make the 'default' attribute a read-only attribute. 'default' and 
>> message-id are to be seen as one set of information, always tied 
>> together.
> 
> 
> The 'domain' attribute should be a read-only attribute too.

Yup. All of the attributes must be read only.

>> 2.) use a different syntax for updating the mapping:
>>
>>   >>> msg = msg % {'foo':'bar'}
>>
>> Note how the __mod__ (method responsible for the % operator) returns a 
>> new message id object.
>>
>> This is an analogy to
>>
>>   >>> "Insert %(text)s" % {'text': 'here'}
>>   Insert here
> 
> 
> Maybe the 'mapping' attribute should be a read-only attribute which 
> returns the mapping copy?

But that doesn't prevent the mapping from being changed in the
message id. We need to prevent the messaage id's data from being
mutated.

I suggest we use Python dict proxies, which are proxies
used by new-style classes to prevent modification of their
dictionaries.  Unfortunately, creation of these is not
exposed to Python code. Someone will have to whip up a small
C extension to make these creatable from Python.  I've proposed
on python-dev to make these creatable from Python, but that wouldn't
happen until Python 2.4 at the soonest.


 > And the MessageID constructor should accept
> optional 'mapping' argument and save copy of the passed mapping in 
> private attribute, like this:
> 
> class MessageID(unicode):
> 
>     def __new__(cls, ustr, domain=None, default=None, mapping={}):
>         self = super(MessageID, cls).__new__(cls, ustr)
>         self.__mapping = mapping.copy()
>         ...

I propose to use dictproxies instead, however, we should make a copy
anyway and convert all of the keys and values to unicode as we make
the copy. Otherwise, some of the data might be mutable.

> 
>> If this idea is generally welcomed, I could write up a proposal and 
>> provide an implementation, with or without backward compatability.
> 
> 
> +1

Yes Phillipp, this is a great idea.  Please write a proposal.

+1

Jim

-- 
Jim Fulton           mailto:jim at zope.com       Python Powered!
CTO                  (540) 361-1714            http://www.python.org
Zope Corporation     http://www.zope.com       http://www.zope.org


More information about the Zope3-dev mailing list