[Patches] [ python-Patches-744321 ] Migrate to new-style classes

SourceForge.net noreply@sourceforge.net
Tue, 27 May 2003 08:13:22 -0700


Patches item #744321, was opened at 2003-05-27 15:13
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=744321&group_id=5470

Category: Library (Lib)
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Gerrit Holl (gerrit)
Assigned to: Nobody/Anonymous (nobody)
Summary: Migrate to new-style classes

Initial Comment:
This patch changes all old-style classes to new-style
classes in the standard library. This should be safe,
because all exceptions derive from Exception, so they
are not new-style classes as long as Exception isn't.,
and the exceptions module is built-in.

This can be wished to prevent class Foo(HTTPLib,
object) or similar. It is almost completely
backwards-compatible, although in some rare cases a
MRO-conflict may occur.

The script I used to do this is as follows:

#!/usr/bin/python

import re
import sys

pattern = "(^ *class [A-Za-z_][A-Za-z0-9_]*[^()]+)(:)"

for f in sys.argv[1:]:
    print "handling", f
    lines = open(f, 'r').readlines()
    fp = open(f, 'w')
    for line in lines:
        fp.write(re.sub(pattern, r"\1(object)\2", line))

...and...

$ python /tmp/repl.py $(find . -name "*.py" | grep -v
lib-old)
$ grep "^RCS " ~/newclass.diff | wc -l
    279
$ find . -name "*.py" | wc -l
    942


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=744321&group_id=5470