[Grok-dev] Re: Understanding unicode

Jan Ulrich Hasecke janulrich.hasecke at web.de
Sat Sep 22 13:48:16 EDT 2007


Am 31.08.2007 um 22:32 schrieb Philipp von Weitershausen:
> Jan Ulrich Hasecke wrote:
>> zope.configuration.xmlconfig.ZopeXMLConfigurationError: File "/ 
>> Users/juh/GrokZoo/parts/app/site.zcml", line 4.0-4.29
>>     ZopeXMLConfigurationError: File "/Users/juh/GrokZoo/src/ 
>> grokzoo/configure.zcml", line 4.2-4.27
>>     UnicodeEncodeError: 'ascii' codec can't encode character  
>> u'\xe4' in position 7: ordinal not in range(128)
>
> I think I understand what the problem is. The error message says it  
> can't *encode* a unicode character. That means it's trying to  
> conver it to a bytes string (not the other way around).
>
> I suspect this is a limiatation of the Choice field and the  
> 'values' parameter. It wants to be able to get a bytes  
> representation of all items in the choice. Trying to use the ascii  
> encoding, it obviously fails. This can be seen as a bug of the  
> Choice field.
>

Hi all,

yesterday on the meeting of the zope user group rhineland we tracked  
down this error. Special thanks to Uli and Charlie Clark. Today I  
tried to find a solution and I hope that I found a fix for this.

To get into the problem again here comes my code and the traceback

----- zoo.py ----------

class GehegeBauen(grok.AddForm):
	"""The view to add a cage for elephants"""
	grok.context(GrokZoo)
	grok.name('gehegebauen')
	form_fields = grok.Fields(
	name=Choice(title=u'Gehege', values= 
[u'Elefantengehege',u'Giraffengehege', u'Paviankäfig']),
	groesse=Int(title=u"Wieviele Tiere sollen Platz haben?"))
	
	label= u'Neues Gehege bauen'
	@grok.action('Gehege bauen')
	def add(self,name, groesse=8):
		if name in self.context.keys():
			return
			self.redirect(self.url('index'))
		self.context.bauegehege(name, groesse, tierart='default', kosten=100)
		self.redirect(self.url('index'))

-----------traceback

./bin/zopectl fg
/Users/juh/GrokZoo/parts/app/runzope -C /Users/juh/GrokZoo/parts/ 
zopectl/zope.conf
Traceback (most recent call last):
   File "/Users/juh/GrokZoo/parts/app/runzope", line 110, in ?
     zope.app.twisted.main.main()
   File "/Users/juh/buildout-eggs/tmp_JdxsP/ 
zope.app.twisted-3.4.0b1_r76119-py2.4.egg/zope/app/twisted/main.py",  
line 74, in main
   File "/Users/juh/buildout-eggs/tmp_JdxsP/ 
zope.app.twisted-3.4.0b1_r76119-py2.4.egg/zope/app/twisted/main.py",  
line 145, in setup
   File "/Users/juh/buildout-eggs/tmpNn-RO6/zope.app.appsetup-3.4.0a1- 
py2.4.egg/zope/app/appsetup/appsetup.py", line 110, in config
   File "/Users/juh/buildout-eggs/tmpSiL1m9/ 
zope.configuration-3.4.0b1-py2.4.egg/zope/configuration/ 
xmlconfig.py", line 610, in file
   File "/Users/juh/buildout-eggs/tmpSiL1m9/ 
zope.configuration-3.4.0b1-py2.4.egg/zope/configuration/ 
xmlconfig.py", line 546, in include
   File "/Users/juh/buildout-eggs/tmpSiL1m9/ 
zope.configuration-3.4.0b1-py2.4.egg/zope/configuration/ 
xmlconfig.py", line 378, in processxmlfile
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/ 
python2.4/xml/sax/expatreader.py", line 107, in parse
     xmlreader.IncrementalParser.parse(self, source)
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/ 
python2.4/xml/sax/xmlreader.py", line 123, in parse
     self.feed(buffer)
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/ 
python2.4/xml/sax/expatreader.py", line 207, in feed
     self._parser.Parse(data, isFinal)
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/ 
python2.4/xml/sax/expatreader.py", line 348, in end_element_ns
     self._cont_handler.endElementNS(pair, None)
   File "/Users/juh/buildout-eggs/tmpSiL1m9/ 
zope.configuration-3.4.0b1-py2.4.egg/zope/configuration/ 
xmlconfig.py", line 357, in endElementNS
   File "/Users/juh/buildout-eggs/tmpSiL1m9/ 
zope.configuration-3.4.0b1-py2.4.egg/zope/configuration/config.py",  
line 542, in end
   File "/Users/juh/buildout-eggs/tmpSiL1m9/ 
zope.configuration-3.4.0b1-py2.4.egg/zope/configuration/config.py",  
line 690, in finish
   File "/Users/juh/buildout-eggs/tmpSiL1m9/ 
zope.configuration-3.4.0b1-py2.4.egg/zope/configuration/ 
xmlconfig.py", line 546, in include
   File "/Users/juh/buildout-eggs/tmpSiL1m9/ 
zope.configuration-3.4.0b1-py2.4.egg/zope/configuration/ 
xmlconfig.py", line 378, in processxmlfile
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/ 
python2.4/xml/sax/expatreader.py", line 107, in parse
     xmlreader.IncrementalParser.parse(self, source)
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/ 
python2.4/xml/sax/xmlreader.py", line 123, in parse
     self.feed(buffer)
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/ 
python2.4/xml/sax/expatreader.py", line 207, in feed
     self._parser.Parse(data, isFinal)
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/ 
python2.4/xml/sax/expatreader.py", line 348, in end_element_ns
     self._cont_handler.endElementNS(pair, None)
   File "/Users/juh/buildout-eggs/tmpSiL1m9/ 
zope.configuration-3.4.0b1-py2.4.egg/zope/configuration/ 
xmlconfig.py", line 357, in endElementNS
   File "/Users/juh/buildout-eggs/tmpSiL1m9/ 
zope.configuration-3.4.0b1-py2.4.egg/zope/configuration/config.py",  
line 542, in end
   File "/Users/juh/buildout-eggs/tmpSiL1m9/ 
zope.configuration-3.4.0b1-py2.4.egg/zope/configuration/config.py",  
line 690, in finish
   File "/Users/juh/buildout-eggs/grok-0.10-py2.4.egg/grok/zcml.py",  
line 33, in grokDirective
     grok.grok(package.__name__)
   File "/Users/juh/buildout-eggs/grok-0.10-py2.4.egg/grok/_grok.py",  
line 81, in do_grok
     martian.grok_dotted_name(dotted_name, the_module_grokker)
   File "/Users/juh/buildout-eggs/martian-0.8.1-py2.4.egg/martian/ 
core.py", line 193, in grok_dotted_name
     grok_package(module_info, grokker, **kw)
   File "/Users/juh/buildout-eggs/martian-0.8.1-py2.4.egg/martian/ 
core.py", line 198, in grok_package
     grok_package(sub_module_info, grokker, **kw)
   File "/Users/juh/buildout-eggs/martian-0.8.1-py2.4.egg/martian/ 
core.py", line 196, in grok_package
     grok_module(module_info, grokker, **kw)
   File "/Users/juh/buildout-eggs/martian-0.8.1-py2.4.egg/martian/ 
core.py", line 201, in grok_module
     grokker.grok(module_info.dotted_name, module_info.getModule(),  
**kw)
   File "/Users/juh/buildout-eggs/martian-0.8.1-py2.4.egg/martian/ 
scan.py", line 116, in getModule
     self._module = resolve(self.dotted_name)
   File "/Users/juh/buildout-eggs/martian-0.8.1-py2.4.egg/martian/ 
scan.py", line 154, in resolve
     __import__(used)
   File "/Users/juh/GrokZoo/src/grokzoo/zoo.py", line 62, in ?
     class GehegeBauen(grok.AddForm):
   File "/Users/juh/GrokZoo/src/grokzoo/zoo.py", line 66, in GehegeBauen
     form_fields = grok.Fields(
   File "/Users/juh/buildout-eggs/tmpa_oDQ6/ 
zope.schema-3.4.0b1dev_r77624-py2.4.egg/zope/schema/_field.py", line  
251, in __init__
   File "/Users/juh/buildout-eggs/tmpa_oDQ6/ 
zope.schema-3.4.0b1dev_r77624-py2.4.egg/zope/schema/vocabulary.py",  
line 98, in fromValues
   File "/Users/juh/buildout-eggs/tmpa_oDQ6/ 
zope.schema-3.4.0b1dev_r77624-py2.4.egg/zope/schema/vocabulary.py",  
line 108, in createTerm
   File "/Users/juh/buildout-eggs/tmpa_oDQ6/ 
zope.schema-3.4.0b1dev_r77624-py2.4.egg/zope/schema/vocabulary.py",  
line 41, in __init__
zope.configuration.xmlconfig.ZopeXMLConfigurationError: File "/Users/ 
juh/GrokZoo/parts/app/site.zcml", line 4.0-4.29
     ZopeXMLConfigurationError: File "/Users/juh/GrokZoo/src/grokzoo/ 
configure.zcml", line 4.2-4.27
     UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4'  
in position 7: ordinal not in range(128)

----------------

We can skip the ConfigurationError, and go to the errors above in  
zope.schema

We looked into all places and finally found the place where the  
values in my choice list is encoded to ASCII

In zope.schema vocabulary.py

there is:

     def __init__(self, value, token=None, title=None):
         """Create a term for value and token. If token is omitted,
         str(value) is used for the token.  If title is provided,
         term implements ITitledTokenizedTerm.
         """
         self.value = value
         if token is None:
             token = value
         self.token = str(token)
         self.title = title
         if title is not None:
             directlyProvides(self, ITitledTokenizedTerm)

self.token = str(token) converts the values from my Choice list to  
ASCII, so that there is an error when there are non unicode strings  
in the values like u'Paviankäfig'

if you change the line to

self.token = unicode(token)

it works.

Please have a look at this solution, maybe there are side effects.  
But I hope that this is a good solution.

juh



-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: Signierter Teil der Nachricht
Url : http://mail.zope.org/pipermail/grok-dev/attachments/20070922/1c12a4cc/PGP.bin


More information about the Grok-dev mailing list