Python Migration Error: TypeError: exceptions must be old-style classes or derived from BaseException, not str

rusi rustompmody at gmail.com
Mon Oct 3 23:00:55 EDT 2011


On Oct 3, 12:45 pm, Chris Rebert <c... at rebertia.com> wrote:
> On Sun, Oct 2, 2011 at 11:45 PM, Wong Wah Meng-R32813
>
> <r32... at freescale.com> wrote:
> > Hello guys,
>
> > I am migrating my application from python 1.5.2 to 2.7.1. I encountered an error when I run some commands (I put in debug statement however, not able to trace down to which line of code that cause it to generate a lot of messages in one second until my hard disk space is full. The error log I got in my log file is as below:-
>
> > Oct  3 14:12:41  ('Encountered exception while processing from', (0, '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'), <type 'exceptions.TypeError'>, TypeError('exceptions must be old-style classes or derived from BaseException, not str',))
>
> > Does it mean in newer python I need to migrate all my Exception to non-string based exception type?
>
> Correct. You can no longer do merely `raise "Some error message"`. You
> should instead raise an exception instance of the appropriate type;
> e.g. `raise ValueError("foo must be positive")`.
> It's advisable to read the NEWS / "What's New" documents for the
> intervening versions so you can learn what else has changed.
>
> > That's should be a lot of changes. :p
>
> To be fair, v1.5.2 is practically ancient at this point. It's over a
> decade old! And 2 *major* versions behind what's current.
> In a pinch, you could always write a script to mechanically change
> `raise "..."` to `raise StandardError("...")` [or some other fairly
> generic exception type].
>
> Cheers,
> Chris

I wonder whether 2to3 could help directly or else have a hook to do
this semi-automatically?

NOTE: I am not advocating going straight to python 3, just using
(parts of) the 2to3 'fixes' to aid migration.



More information about the Python-list mailing list