treating str as unicode in legacy code?

Steve Holden steve at holdenweb.com
Fri Apr 13 08:48:34 EDT 2007


Ben wrote:
> I'm left with some legacy code using plain old str, and I need to make
> sure it works with unicode input/output. I have a simple plan to do
> this:
> 
> - Run the code with "python -U" so all the string literals become
> unicode litrals.
> - Add this statement
> 
>   str = unicode
> 
>   to all .py files so the type comparison (e.g., type('123') == str)
> would work.
> 
> 
> Did I miss anything? Does this sound like a workable plan?
> 
> Thanks!
> 

Well, don't forget that the assignment to str *shadows* the built-in 
rather than replacing it, so there may be places (imported modules being 
the example that most readily springs to mind) where that replacement 
won't be effective.

Plus which in CPython the C parts of the code may well be creating and 
expecting objects of type str but they won't use the Python naming 
mechanism at all, so you will have no way to effect changes in those 
behaviors.

This will probably account for about 95% of any strangeness you see, but 
it's probably a good first step in the conversion process.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Recent Ramblings       http://holdenweb.blogspot.com




More information about the Python-list mailing list