8000 Add missing cmp_to_key for Py2.6 (issue #189) · thecodingchicken/python-future@7d311e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d311e4

Browse files
committed
Add missing cmp_to_key for Py2.6 (issue PythonCharmers#189)
1 parent efe07a0 commit 7d311e4

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

docs/whatsnew.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ This releases fixes also these bugs:
3131
- Fix `standard_library.install_aliases()` on PyPy. (Issue #205)
3232
- Fix assertRaises for `pow` and `compile` on Python 3.5. (Issue #183)
3333
- Fix return argument of `future.utils.ensure_new_type` if conversion to new type does not exist. (Issue #185)
34+
- Add missing `cmp_to_key` for Py2.6. (Issue #189)
3435
- Allow the `old_div` fixer to be disabled. (Issue #190)
3536

3637
What's new in version 0.15.2 (2015-09-11)

src/future/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@
8686
__license__ = 'MIT'
8787
__copyright__ = 'Copyright 2013-2016 Python Charmers Pty Ltd'
8888
__ver_major__ = 0
89-
__ver_minor__ = 15
90-
__ver_patch__ = 3
89+
__ver_minor__ = 16
90+
__ver_patch__ = 0
9191
__ver_sub__ = '-dev'
9292
__version__ = "%d.%d.%d%s" % (__ver_major__, __ver_minor__,
9393
__ver_patch__, __ver_sub__)

src/future/backports/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@
2222
count,
2323
recursive_repr,
2424
_count_elements,
25+
cmp_to_key
2526
)

src/future/backports/misc.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
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+
1315
from __future__ import absolute_import
1416

1517
import 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

904930
if sys.version_info >= (3, 0):
905931
from math import ceil

src/future/standard_library/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197
('collections', 'Counter', 'future.backports.misc', 'Counter'),
198198
('itertools', 'count', 'future.backports.misc', 'count'),
199199
('reprlib', 'recursive_repr', 'future.backports.misc', 'recursive_repr'),
200+
('functools', 'cmp_to_key', 'future.backports.misc', 'cmp_to_key'),
200201

201202
# This is no use, since "import urllib.request" etc. still fails:
202203
# ('urllib', 'error', 'future.moves.urllib', 'error'),

0 commit comments

Comments
 (0)
0