Index: Doc/library/2to3.rst =================================================================== --- Doc/library/2to3.rst (revision 77840) +++ Doc/library/2to3.rst (working copy) @@ -169,6 +169,22 @@ Changes ``dict.has_key(key)`` to ``key in dict``. +.. 2to3fixer:: hash + + The :mod:`md5` and :mod:`sha` modules where deprecated in 2.5. The + functionality of these modules was put in :mod:`hashlib`. This fixer + performs the followings mappings between the deprecated modules and + :mod:`hashlib`: + + ================================== ========================================= + From To + ================================== ========================================= + ``md5.new(...)`` ``hashlib.md5(...)`` + ``md5.md5(...)`` ``hashlib.md5(...)`` + ``sha.new(...)`` ``hashlib.sha1(...)`` + ``sha.sha1(...)`` ``hashlib.sha1(...)`` + ================================== ========================================= + .. 2to3fixer:: idioms This optional fixer performs several transformations that make Python code Index: Lib/lib2to3/tests/test_fixers.py =================================================================== --- Lib/lib2to3/tests/test_fixers.py (revision 77840) +++ Lib/lib2to3/tests/test_fixers.py (working copy) @@ -4227,3 +4227,47 @@ def test_bare_sequenceIncludes(self): s = "sequenceIncludes(x, y)" self.warns_unchanged(s, "You should use operator.contains here.") + + +class Test_hash(FixerTestCase): + + fixer = "hash" + + def test_hash_md5(self): + b = """md5.new()""" + a = """hashlib.md5()""" + self.check(b, a) + + b = """md5.new("Nobody inspects the spammish repetition")""" + a = """hashlib.md5("Nobody inspects the spammish repetition")""" + self.check(b, a) + + b = """md5.new("Nobody inspects the spammish").digest()""" + a = """hashlib.md5("Nobody inspects the spammish").digest()""" + self.check(b, a) + + b = """md5.new("Nobody inspects the spammish") .digest()""" + a = """hashlib.md5("Nobody inspects the spammish") .digest()""" + self.check(b, a) + + a = """md5.sha()""" + self.unchanged(a) + + def test_hash_sha(self): + b = """sha.new()""" + a = """hashlib.sha1()""" + self.check(b, a) + + b = """sha.new("Nobody inspects the spammish repetition")""" + a = """hashlib.sha1("Nobody inspects the spammish repetition")""" + self.check(b, a) + + b = """sha.new("Nobody inspects the spammish").digest()""" + a = """hashlib.sha1("Nobody inspects the spammish").digest()""" + + b = """ sha.new("Nobody inspects the spammish"). digest()""" + a = """ hashlib.sha1("Nobody inspects the spammish"). digest()""" + self.check(b, a) + + a = """sha.md5()""" + self.unchanged(a) Index: Lib/lib2to3/fixes/fix_hash.py =================================================================== --- Lib/lib2to3/fixes/fix_hash.py (revision 0) +++ Lib/lib2to3/fixes/fix_hash.py (revision 0) @@ -0,0 +1,46 @@ +"""Fixer for 'md5' and 'sha' modules. + +md5.new(...) -> hashlib.md5(...) +md5.md5(...) -> hashlib.md5(...) + +sha.new(...) -> hashlib.sha1(...) +sha.sha1(...) -> hashlib.sha1(...) +""" +# Author: Meador Inge + +# Local imports +from .. import fixer_base +from ..fixer_util import Node, Name, ArgList, Attr, syms + +class FixHash(fixer_base.BaseFix): + + sigs = """ + (module='md5' trailer< dot='.' method='md5'>) + | (module='md5' trailer< dot='.' method='new'>) + | (module='sha' trailer< dot='.' method='sha'>) + | (module='sha' trailer< dot='.' method='new'>) + """ + args = "( '(' ')' | '(' args=any ')' )" + PATTERN = """power< ({sigs}) trailer< {args} > after=any*> + """.format(sigs=sigs, args=args) + hashlib_name = { + "md5": u"md5", + "sha": u"sha1" + } + + def transform(self, node, results): + module = results["module"] + method = results["method"] + if "args" in results: + args = [results["args"].clone()] + else: + args = [] + after = results["after"] + if after: + after = [n.clone() for n in after] + new_method_name = self.hashlib_name[module.value] + arglst = ArgList(args) + attr = Attr(Name(u"hashlib", prefix=method.prefix), + Name(new_method_name)) + attr[1].children[0].prefix = results["dot"].prefix + return Node(syms.power, attr + [arglst] + after, prefix=node.prefix) Index: Lib/lib2to3/fixes/fix_imports.py =================================================================== --- Lib/lib2to3/fixes/fix_imports.py (revision 77840) +++ Lib/lib2to3/fixes/fix_imports.py (working copy) @@ -55,6 +55,8 @@ 'UserList' : 'collections', 'urlparse' : 'urllib.parse', 'robotparser' : 'urllib.robotparser', + 'md5' : 'hashlib', + 'sha' : 'hashlib', }