[Zope3-dev] Re: Removing hard dependency from zope.i18nmessageid C extension

Christian Heimes heimes at faho.rwth-aachen.de
Thu Feb 10 01:57:18 EST 2005


Jim Fulton wrote:
> 1. I'm 90% sure that the Python and C versions have
>    different APIs.  If this is so, then you need to
>    create a new Python implementation with the same
>    api as the C version.
> 
> 2. If message ids are picklable, then you need to arrange
>    for the Python and C versions to have the same module
>    and class names.  In fact, you need to make sure that
>    they have compatible pickles.

What do you think of this solution? After removing the C extension all 
unit tests are still running (except of the bforest doc tests which 
does't depend on my changes). There is one XXX in the code. I think it 
is a bug in the original C implementation but I'm not sure.

from messageid import MessageID

class Message(MessageID):
     """Message
     This is a string used as a message.  It has a domain attribute that is
     its source domain, and a default attribute that is its default text to
     display when there is no translation.  domain may be None meaning 
there is
     no translation domain.  default may also be None, in which case the
     message id itself implicitly serves as the default text.
     """

     __slots__ = ('domain', 'default', 'mapping', '_readonly')

     def __new__(cls, ustr, domain=None, default=None, mapping=None):
         self = unicode.__new__(cls, ustr)
         if isinstance(ustr, self.__class__):
             domain = ustr.domain and ustr.domain[:] or domain
             default = ustr.default and ustr.default[:] or default
             mapping = ustr.mapping and ustr.mapping.copy() or mapping
             ustr = unicode(ustr)
         self.domain = domain
         if default is None:
             # XXX MessageID does: self.default = ustr
             self.default = default
         else:
             self.default = unicode(default)
         self.mapping = mapping
         self._readonly = True
         return self

     def __setattr__(self, key, value):
         """Message is immutable

         It cannot be changed once the message id is created.
         """
         if getattr(self, '_readonly', False):
             raise TypeError, 'readonly attribute'
         else:
             return MessageID.__setattr__(self, key, value)

     def __reduce__(self):
         """The reduce hook is slightly different to the one in MessageID
         """
         return self.__class__, self.__getstate__()

pyMessage = Message

try:
     from _zope_i18nmessageid_message import Message
except ImportError:
     pass



More information about the Zope3-dev mailing list