Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
23f9fc34
Commit
23f9fc34
authored
Jan 08, 2011
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #10042: Fixed the total_ordering decorator to handle cross-type
comparisons that could lead to infinite recursion.
parent
06ec45e2
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
36 additions
and
8 deletions
+36
-8
Lib/functools.py
Lib/functools.py
+8
-8
Lib/test/test_functools.py
Lib/test/test_functools.py
+24
-0
Misc/ACKS
Misc/ACKS
+1
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/functools.py
View file @
23f9fc34
...
...
@@ -68,17 +68,17 @@ def wraps(wrapped,
def
total_ordering
(
cls
):
"""Class decorator that fills in missing ordering methods"""
convert
=
{
'__lt__'
:
[(
'__gt__'
,
lambda
self
,
other
:
other
<
self
),
(
'__le__'
,
lambda
self
,
other
:
not
other
<
self
),
'__lt__'
:
[(
'__gt__'
,
lambda
self
,
other
:
not
(
self
<
other
or
self
==
other
)
),
(
'__le__'
,
lambda
self
,
other
:
self
<
other
or
self
==
other
),
(
'__ge__'
,
lambda
self
,
other
:
not
self
<
other
)],
'__le__'
:
[(
'__ge__'
,
lambda
self
,
other
:
other
<=
self
),
(
'__lt__'
,
lambda
self
,
other
:
not
other
<=
self
),
'__le__'
:
[(
'__ge__'
,
lambda
self
,
other
:
not
self
<=
other
or
self
==
other
),
(
'__lt__'
,
lambda
self
,
other
:
self
<=
other
and
not
self
==
other
),
(
'__gt__'
,
lambda
self
,
other
:
not
self
<=
other
)],
'__gt__'
:
[(
'__lt__'
,
lambda
self
,
other
:
other
>
self
),
(
'__ge__'
,
lambda
self
,
other
:
not
other
>
self
),
'__gt__'
:
[(
'__lt__'
,
lambda
self
,
other
:
not
(
self
>
other
or
self
==
other
)
),
(
'__ge__'
,
lambda
self
,
other
:
self
>
other
or
self
==
other
),
(
'__le__'
,
lambda
self
,
other
:
not
self
>
other
)],
'__ge__'
:
[(
'__le__'
,
lambda
self
,
other
:
other
>=
self
),
(
'__gt__'
,
lambda
self
,
other
:
not
other
>=
self
),
'__ge__'
:
[(
'__le__'
,
lambda
self
,
other
:
(
not
self
>=
other
)
or
self
==
other
),
(
'__gt__'
,
lambda
self
,
other
:
self
>=
other
and
not
self
==
other
),
(
'__lt__'
,
lambda
self
,
other
:
not
self
>=
other
)]
}
# Find user-defined comparisons (not those inherited from object).
...
...
Lib/test/test_functools.py
View file @
23f9fc34
...
...
@@ -457,6 +457,8 @@ class TestTotalOrdering(unittest.TestCase):
self
.
value
=
value
def
__lt__
(
self
,
other
):
return
self
.
value
<
other
.
value
def
__eq__
(
self
,
other
):
return
self
.
value
==
other
.
value
self
.
assertTrue
(
A
(
1
)
<
A
(
2
))
self
.
assertTrue
(
A
(
2
)
>
A
(
1
))
self
.
assertTrue
(
A
(
1
)
<=
A
(
2
))
...
...
@@ -471,6 +473,8 @@ class TestTotalOrdering(unittest.TestCase):
self
.
value
=
value
def
__le__
(
self
,
other
):
return
self
.
value
<=
other
.
value
def
__eq__
(
self
,
other
):
return
self
.
value
==
other
.
value
self
.
assertTrue
(
A
(
1
)
<
A
(
2
))
self
.
assertTrue
(
A
(
2
)
>
A
(
1
))
self
.
assertTrue
(
A
(
1
)
<=
A
(
2
))
...
...
@@ -485,6 +489,8 @@ class TestTotalOrdering(unittest.TestCase):
self
.
value
=
value
def
__gt__
(
self
,
other
):
return
self
.
value
>
other
.
value
def
__eq__
(
self
,
other
):
return
self
.
value
==
other
.
value
self
.
assertTrue
(
A
(
1
)
<
A
(
2
))
self
.
assertTrue
(
A
(
2
)
>
A
(
1
))
self
.
assertTrue
(
A
(
1
)
<=
A
(
2
))
...
...
@@ -499,6 +505,8 @@ class TestTotalOrdering(unittest.TestCase):
self
.
value
=
value
def
__ge__
(
self
,
other
):
return
self
.
value
>=
other
.
value
def
__eq__
(
self
,
other
):
return
self
.
value
==
other
.
value
self
.
assertTrue
(
A
(
1
)
<
A
(
2
))
self
.
assertTrue
(
A
(
2
)
>
A
(
1
))
self
.
assertTrue
(
A
(
1
)
<=
A
(
2
))
...
...
@@ -524,6 +532,22 @@ class TestTotalOrdering(unittest.TestCase):
class
A
:
pass
def
test_bug_10042
(
self
):
@
functools
.
total_ordering
class
TestTO
:
def
__init__
(
self
,
value
):
self
.
value
=
value
def
__eq__
(
self
,
other
):
if
isinstance
(
other
,
TestTO
):
return
self
.
value
==
other
.
value
return
False
def
__lt__
(
self
,
other
):
if
isinstance
(
other
,
TestTO
):
return
self
.
value
<
other
.
value
raise
TypeError
with
self
.
assertRaises
(
TypeError
):
TestTO
(
8
)
<=
()
class
TestLRU
(
unittest
.
TestCase
):
def
test_lru
(
self
):
...
...
Misc/ACKS
View file @
23f9fc34
...
...
@@ -700,6 +700,7 @@ Bernhard Reiter
Steven Reiz
Roeland Rengelink
Tim Rice
Francesco Ricciardi
Jan Pieter Riegel
Armin Rigo
Nicholas Riley
...
...
Misc/NEWS
View file @
23f9fc34
...
...
@@ -40,6 +40,9 @@ Core and Builtins
Library
-------
- Issue #10042: Fixed the total_ordering decorator to handle cross-type
comparisons that could lead to infinite recursion.
- Issue #10686: the email package now :rfc:`2047`\ -encodes headers with
non-ASCII bytes (parsed by a Bytes Parser) when doing conversion to
7bit-clean presentation, instead of replacing them with ?s.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment