99- itertools.count (for Python 2.6, with step parameter)
1010- subprocess.check_output (for Python 2.6)
1111- reprlib.recursive_repr (for Python 2.6+)
12+ - functools.cmp_to_key (for Python 2.6)
1213"""
14+
1315from __future__ import absolute_import
1416
1517import subprocess
@@ -881,6 +883,28 @@ def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
881883 else :
882884 raise error ("getaddrinfo returns an empty list" )
883885
886+ # Backport from Py2.7 for Py2.6:
887+ def cmp_to_key (mycmp ):
888+ """Convert a cmp= function into a key= function"""
889+ class K (object ):
890+ __slots__ = ['obj' ]
891+ def __init__ (self , obj , * args ):
892+ self .obj = obj
893+ def __lt__ (self , other ):
894+ return mycmp (self .obj , other .obj ) < 0
895+ def __gt__ (self , other ):
896+ return mycmp (self .obj , other .obj ) > 0
897+ def __eq__ (self , other ):
898+ return mycmp (self .obj , other .obj ) == 0
899+ def __le__ (self , other ):
900+ return mycmp (self .obj , other .obj ) <= 0
901+ def __ge__ (self , other ):
902+ return mycmp (self .obj , other .obj ) >= 0
903+ def __ne__ (self , other ):
904+ return mycmp (self .obj , other .obj ) != 0
905+ def __hash__ (self ):
906+ raise TypeError ('hash not implemented' )
907+ return K
884908
885909# Back up our definitions above in case they're useful
886910_OrderedDict = OrderedDict
@@ -892,6 +916,7 @@ def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
892916_recursive_repr = recursive_repr
893917_ChainMap = ChainMap
894918_create_connection = create_connection
919+ _cmp_to_key = cmp_to_key
895920
896921# Overwrite the definitions above with the usual ones
897922# from the standard library:
@@ -900,6 +925,7 @@ def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
900925 from subprocess import check_output
901926 from itertools import count
902927 from socket import create_connection
928+ from functools import cmp_to_key
903929
904930if sys .version_info >= (3 , 0 ):
905931 from math import ceil
0 commit comments