[Python-checkins] CVS: python/dist/src/Lib UserString.py,1.9,1.10

M.-A. Lemburg lemburg@users.sourceforge.net
Tue, 15 May 2001 04:58:08 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv24065/Lib

Modified Files:
	UserString.py 
Log Message:
This patch changes the way the string .encode() method works slightly
and introduces a new method .decode(). 

The major change is that strg.encode() will no longer try to convert
Unicode returns from the codec into a string, but instead pass along
the Unicode object as-is. The same is now true for all other codec 
return types. The underlying C APIs were changed accordingly.

Note that even though this does have the potential of breaking
existing code, the chances are low since conversion from Unicode 
previously took place using the default encoding which is normally
set to ASCII rendering this auto-conversion mechanism useless for 
most Unicode encodings.

The good news is that you can now use .encode() and .decode() with
much greater ease and that the door was opened for better accessibility
of the builtin codecs.

As demonstration of the new feature, the patch includes a few new
codecs which allow string to string encoding and decoding (rot13,
hex, zip, uu, base64).

Written by Marc-Andre Lemburg. Copyright assigned to the PSF.



Index: UserString.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/UserString.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -r1.9 -r1.10
*** UserString.py	2001/01/20 19:54:20	1.9
--- UserString.py	2001/05/15 11:58:05	1.10
***************
*** 73,76 ****
--- 73,84 ----
      def count(self, sub, start=0, end=sys.maxint):
          return self.data.count(sub, start, end)
+     def decode(self, encoding=None, errors=None): # XXX improve this?
+         if encoding:
+             if errors:
+                 return self.__class__(self.data.decode(encoding, errors))
+             else:
+                 return self.__class__(self.data.decode(encoding))
+         else:
+             return self.__class__(self.data.decode())
      def encode(self, encoding=None, errors=None): # XXX improve this?
          if encoding: