dabo Commit
Revision 6122
Date: 2010-10-21 15:12:34 -0700 (Thu, 21 Oct 2010)
Author: Paul
Trac: http://trac.dabodev.com/changeset/6122

Changed:
U   trunk/dabo/__init__.py
U   trunk/dabo/dApp.py
U   trunk/dabo/db/dCursorMixin.py
U   trunk/dabo/db/dbMySQL.py
U   trunk/dabo/db/dbSQLite.py
U   trunk/dabo/lib/utils.py
U   trunk/dabo/lib/xmltodict.py
U   trunk/dabo/ui/uiwx/dDataControlMixin.py
U   trunk/dabo/ui/uiwx/dTextBoxMixin.py

Log:
Changed some unicode() calls to ustr() calls where it seemed appropriate.
This includes calls with no explicit encoding that could potentially try
to decode non-ASCII chars, as well as other places where it probably 
doesn't matter but resulted in simpler code.

Added the exception type to the "Error fetching records" error log entry.
I'm wondering if we shouldn't be eating these exceptions. In my case,
my customers will never see the error log entry, only that no records
were selected. If the exception weren't eaten, I'd be emailed a report
of the exception.

Annotated dabo.lib.ustr() with comments, mostly because I had to spend time
figuring out the rationale for why it is the way it is, and didn't want to
have to do that again.

Made the exception catching in ustr() more narrow, so that non-expected
exceptions will still be raised normally.

Replaced the code in dApp.str2Unicode() to simply call dabo.lib.ustr()
instead.
 


Diff:
Modified: trunk/dabo/__init__.py
===================================================================
--- trunk/dabo/__init__.py      2010-10-21 17:09:46 UTC (rev 6121)
+++ trunk/dabo/__init__.py      2010-10-21 22:12:34 UTC (rev 6122)
@@ -285,6 +285,7 @@
 from __version__ import version
 import dColors
 import dEvents
+from lib.utils import ustr
 
 from dBug import logPoint
 try:
@@ -297,7 +298,7 @@
 from dPref import dPref
 
 def debugout(*args):
-       txtargs = [unicode(arg) for arg in args]
+       txtargs = [ustr(arg) for arg in args]
        txt = " ".join(txtargs)
        log = logging.getLogger("Debug")
        log.debug(txt)

Modified: trunk/dabo/dApp.py
===================================================================
--- trunk/dabo/dApp.py  2010-10-21 17:09:46 UTC (rev 6121)
+++ trunk/dabo/dApp.py  2010-10-21 22:12:34 UTC (rev 6122)
@@ -27,6 +27,7 @@
 from dabo.dObject import dObject
 from dabo import dUserSettingProvider
 from dabo.lib.RemoteConnector import RemoteConnector
+from dabo.lib.utils import ustr
 
 try:
        import simplejson
@@ -831,24 +832,7 @@
                unicode value. It will first try the default Encoding, and then 
try the
                more common encoding types.
                """
-               if not isinstance(strVal, basestring):
-                       strVal = strVal.__str__()
-               if isinstance(strVal, unicode):
-                       return strVal
-               try:
-                       ret = unicode(strVal, self.Encoding)
-               except UnicodeDecodeError:
-                       # Try some common encodings:
-                       for enc in ("utf-8", "iso8859-1", "cp1252"):
-                               if enc != self.Encoding:
-                                       try:
-                                               ret = unicode(strVal, enc)
-                                       except UnicodeDecodeError:
-                                               continue
-                                       break
-                       else: # All attempts failed
-                               raise
-               return ret
+               return ustr(strVal)
 
 
        # These two methods pass encryption/decryption requests

Modified: trunk/dabo/db/dCursorMixin.py
===================================================================
--- trunk/dabo/db/dCursorMixin.py       2010-10-21 17:09:46 UTC (rev 6121)
+++ trunk/dabo/db/dCursorMixin.py       2010-10-21 22:12:34 UTC (rev 6122)
@@ -375,7 +375,7 @@
                                errMsg = ustr(e).decode(self.Encoding)
                        except UnicodeError:
                                errMsg = ustr(e)
-                       dabo.log.error("Error fetching records: %s" % errMsg)
+                       dabo.log.error("Error fetching records: (%s, %s)" % 
(type(e), errMsg))
 
                if _records and not 
self.BackendObject._alreadyCorrectedFieldTypes:
                        if isinstance(_records[0], (tuple, list)):
@@ -1320,7 +1320,7 @@
                                try:
                                        errMsg = ustr(e).decode(self.Encoding)
                                except UnicodeError:
-                                       errMsg = unicode(e)
+                                       errMsg = ustr(e)
                                dabo.dbActivityLog.info(
                                                _("DBQueryException encountered 
in save(): %s") % errMsg)
                                raise e

Modified: trunk/dabo/db/dbMySQL.py
===================================================================
--- trunk/dabo/db/dbMySQL.py    2010-10-21 17:09:46 UTC (rev 6121)
+++ trunk/dabo/db/dbMySQL.py    2010-10-21 22:12:34 UTC (rev 6122)
@@ -70,7 +70,7 @@
                        try:
                                errMsg = ustr(e).decode(self.Encoding)
                        except UnicodeError:
-                               errMsg = unicode(e)
+                               errMsg = ustr(e)
                        if "access denied" in errMsg.lower():
                                raise dException.DBNoAccessException(errMsg)
                        else:

Modified: trunk/dabo/db/dbSQLite.py
===================================================================
--- trunk/dabo/db/dbSQLite.py   2010-10-21 17:09:46 UTC (rev 6121)
+++ trunk/dabo/db/dbSQLite.py   2010-10-21 22:12:34 UTC (rev 6122)
@@ -114,7 +114,7 @@
                        try:
                                errMsg = ustr(e).decode(self.Encoding)
                        except UnicodeError:
-                               errMsg = unicode(e)
+                               errMsg = ustr(e)
                        dabo.dbActivityLog.info("SQL: commit failed: %s" % 
errMsg)
                        raise dException.DBQueryException(errMsg)
 

Modified: trunk/dabo/lib/utils.py
===================================================================
--- trunk/dabo/lib/utils.py     2010-10-21 17:09:46 UTC (rev 6121)
+++ trunk/dabo/lib/utils.py     2010-10-21 22:12:34 UTC (rev 6122)
@@ -149,21 +149,31 @@
 
 
 def ustr(value):
-       """When converting to a string, do not use the str() function, which
+       """Convert the passed value to a python unicode object.
+
+       When converting to a string, do not use the str() function, which
        can create encoding errors with non-ASCII text.
        """
        if isinstance(value, unicode):
+               # Don't change the encoding of an object that is already 
unicode.
                return value
        if isinstance(value, Exception):
                return exceptionToUnicode(value)
        try:
+               ## Faster for all-ascii strings and converting from 
non-basestring types::
                return unicode(value)
-       except:
+       except UnicodeDecodeError:
+               # Most likely there were bytes whose integer ordinal were > 127 
and so the 
+               # default ASCII codec used by unicode() couldn't decode them.
                pass
+       except UnicodeEncodeError:
+               # Most likely there were bytes whose integer ordinal were > 127 
and so the 
+               # default ASCII codec used by unicode() couldn't encode them.
+               pass
        for ln in getEncodings():
                try:
                        return unicode(value, ln)
-               except:
+               except UnicodeError:
                        pass
        raise UnicodeError("Unable to convert '%r'." % value)
 
@@ -172,14 +182,15 @@
        # Handle DBQueryException first.
        if hasattr(e, "err_desc"):
                return ustr(e.err_desc)
+       if hasattr(e, "args"):
+               return "\n".join((ustr(a) for a in e.args))
        if hasattr(e, "message"):
+               # message is deprecated in python 2.6
                return ustr(e.message)
-       if hasattr(e, "args"):
-               return "\n".join((ustr(a) for a in e.args))
        try:
                return ustr(e)
        except:
-               return u"Unknow message."
+               return u"Unknown message."
 
 
 def relativePathList(toLoc, fromLoc=None):

Modified: trunk/dabo/lib/xmltodict.py
===================================================================
--- trunk/dabo/lib/xmltodict.py 2010-10-21 17:09:46 UTC (rev 6121)
+++ trunk/dabo/lib/xmltodict.py 2010-10-21 22:12:34 UTC (rev 6122)
@@ -218,10 +218,7 @@
        """Add surrounding quotes to the string, and escape
        any illegal XML characters.
        """
-       if not isinstance(val, basestring):
-               val = ustr(val)
-       if not isinstance(val, unicode):
-               val = unicode(val, default_encoding)
+       val = ustr(val)
        if noQuote:
                qt = ''
        else:

Modified: trunk/dabo/ui/uiwx/dDataControlMixin.py
===================================================================
--- trunk/dabo/ui/uiwx/dDataControlMixin.py     2010-10-21 17:09:46 UTC (rev 
6121)
+++ trunk/dabo/ui/uiwx/dDataControlMixin.py     2010-10-21 22:12:34 UTC (rev 
6122)
@@ -46,16 +46,9 @@
                convTypes = (str, unicode, int, float, long, complex)
                oldType = type(oldval)
                if isinstance(val, convTypes) and isinstance(oldval, 
basestring):
-                       if isinstance(oldType, str):
-                               val = ustr(val)
-                       else:
-                               if not isinstance(val, unicode):
-                                       val = unicode(val, dabo.getEncoding())
+                       val = ustr(val)
                elif isinstance(oldval, int) and isinstance(val, basestring):
-                       if val:
-                               val = int(val)
-                       else:
-                               val = 0
+                       val = int(val if val else "0")
                elif isinstance(oldval, int) and isinstance(val, bool):
                        # convert bool to int (original field val was bool, but 
UI
                        # changed to int.

Modified: trunk/dabo/ui/uiwx/dTextBoxMixin.py
===================================================================
--- trunk/dabo/ui/uiwx/dTextBoxMixin.py 2010-10-21 17:09:46 UTC (rev 6121)
+++ trunk/dabo/ui/uiwx/dTextBoxMixin.py 2010-10-21 22:12:34 UTC (rev 6122)
@@ -344,7 +344,7 @@
                try:
                        _value = self._value
                except AttributeError:
-                       _value = self._value = unicode("")
+                       _value = self._value = ustr("")
 
                # Get the string value as reported by wx, which is the 
up-to-date
                # string value of the control:
@@ -667,7 +667,7 @@
                try:
                        _value = self._value
                except AttributeError:
-                       _value = self._value = unicode("")
+                       _value = self._value = ustr("")
                dataType = type(_value)
 
                # Get the string value as reported by wx, which is the 
up-to-date



_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev
Searchable Archives: http://leafe.com/archives/search/dabo-dev
This message: 
http://leafe.com/archives/byMID/[email protected]

Reply via email to