TG 0.9a9 - TypeError: decoding Unicode is not supported

0 views
Skip to first unread message

Bruce

unread,
Aug 21, 2006, 6:19:26 PM8/21/06
to TurboGears
Couldn`t find any information about this problem so here goes...

This is for TG 0.9a9 on win xp with mysql 5.0 and mysql-python
(mysqldb) 1.2.1_p2.

I`m doing the 20 minute wiki. I get to the part where the edit feature
is introduced. Finally I add

<p><a href="${tg.url('/edit', pagename=page.pagename)}">Edit this
page</a></p>

, to "page.kid". Then, when I reload the browser and follow the "edit
link" I get this:

500 Internal error

The server encountered an unexpected condition which prevented it from
fulfilling the request.

Page handler: <bound method Root.edit of <myproject.controllers.Root
object at 0x0151D650>>
Traceback (most recent call last):
File
"c:\python24\lib\site-packages\CherryPy-2.2.1-py2.4.egg\cherrypy\_cphttptools.py",
line 105, in _run
self.main()
File
"c:\python24\lib\site-packages\CherryPy-2.2.1-py2.4.egg\cherrypy\_cphttptools.py",
line 254, in main
body = page_handler(*virtual_path, **self.params)
File "<string>", line 3, in edit
File
"c:\python24\lib\site-packages\TurboGears-0.9a9-py2.4.egg\turbogears\controllers.py",
line 331, in expose
output = database.run_with_transaction(
File "<string>", line 5, in run_with_transaction
File
"c:\python24\lib\site-packages\TurboGears-0.9a9-py2.4.egg\turbogears\database.py",
line 245, in so_rwt
retval = func(*args, **kw)
File "<string>", line 5, in _expose
File
"c:\python24\lib\site-packages\TurboGears-0.9a9-py2.4.egg\turbogears\controllers.py",
line 348, in <lambda>
mapping, fragment, *args, **kw)))
File
"c:\python24\lib\site-packages\TurboGears-0.9a9-py2.4.egg\turbogears\controllers.py",
line 372, in _execute_func
output = errorhandling.try_call(func, *args, **kw)
File
"c:\python24\lib\site-packages\TurboGears-0.9a9-py2.4.egg\turbogears\errorhandling.py",
line 71, in try_call
return func(self, *args, **kw)
File "C:\Documents and
Settings\bruce\TG\myproject\myproject\controllers.py", line 26, in edit
page = Page.byPagename(pagename)
File "<string>", line 1, in <lambda>
File
"c:\python24\lib\site-packages\SQLObject-0.7.1dev_r1860-py2.4.egg\sqlobject\main.py",
line 1266, in _SO_fetchAlternateID
result, obj = cls._findAlternateID(name, dbName, value, connection)
File
"c:\python24\lib\site-packages\SQLObject-0.7.1dev_r1860-py2.4.egg\sqlobject\main.py",
line 1262, in _findAlternateID
value), None
File
"c:\python24\lib\site-packages\SQLObject-0.7.1dev_r1860-py2.4.egg\sqlobject\dbconnection.py",
line 590, in _SO_selectOneAlt
return self.queryOne("SELECT %s FROM %s WHERE %s = %s" %
File
"c:\python24\lib\site-packages\SQLObject-0.7.1dev_r1860-py2.4.egg\sqlobject\dbconnection.py",
line 349, in queryOne
return self._runWithConnection(self._queryOne, s)
File
"c:\python24\lib\site-packages\SQLObject-0.7.1dev_r1860-py2.4.egg\sqlobject\dbconnection.py",
line 220, in _runWithConnection
val = meth(conn, *args)
File
"c:\python24\lib\site-packages\SQLObject-0.7.1dev_r1860-py2.4.egg\sqlobject\dbconnection.py",
line 342, in _queryOne
self._executeRetry(conn, c, s)
File
"C:\Python24\lib\site-packages\sqlobject-0.7.1dev_r1860-py2.4.egg\sqlobject\mysql\mysqlconnection.py",
line 73, in _executeRetry
myquery = unicode(query, self.encoding)
TypeError: decoding Unicode is not supported

As you see it fails in this method:

def _executeRetry(self, conn, cursor, query):
while 1:
try:
if self.need_unicode:
# For MysqlDB 1.2.1 and later, we go
# encoding->unicode->charset (in the mysql db)
myquery = unicode(query, self.encoding)
return cursor.execute(myquery)
else:
return cursor.execute(query)
except MySQLdb.OperationalError, e:
if e.args[0] == 2013: # SERVER_LOST error
if self.debug:
self.printDebug(conn, str(e), 'ERROR')
else:
raise

self.need_unicode had previously been set to True after this:

if MySQLdb.version_info[0] > 1 or (MySQLdb.version_info[0] == 1 and \
(MySQLdb.version_info[1] > 2 or \
(MySQLdb.version_info[1] == 2 and
MySQLdb.version_info[2] >= 1))):
self.need_unicode = True

If I set self.need_unicode = False after this, and restart the project,
the problem seems to be gone (the "edit" page loads nicely). It seems
that this version of MySQLdb doesn`t "need unicode" but SQLObject fails
to recognize this, what do you think?

Max Ischenko

unread,
Aug 22, 2006, 4:50:56 AM8/22/06
to TurboGears
I think that's a known issue with SQLObject, see for instance:
http://maxischenko.in.ua/blog/entries/89/sqlobject-unicode-and-ascii-error/

I'd suggest use str instead of unicode:

File "C:\Documents and
Settings\bruce\TG\myproject\myproject\controllers.py", line 26, in edit
page = Page.byPagename(pagename)

becomes

page = Page.byPagename(pagename.encode('utf8'))
or
from turbogears.util import to_utf8
page = Page.byPagename(to_utf8(pagename))

--Max.

Reply all
Reply to author
Forward
0 new messages