summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorSergey <bizywiz@gmail.com>2019-10-29 05:10:24 (GMT)
committerRaymond Hettinger <rhettinger@users.noreply.github.com>2019-10-29 05:10:24 (GMT)
commit0078a0c2a59a358fa032ec9847c108378b66b656 (patch)
treeb5a2f7ffd048d6bb763a19a077b91b266f4b57c2 /Modules
parent3c88199e0be352c0813f145d7c4c83af044268aa (diff)
downloadcpython-0078a0c2a59a358fa032ec9847c108378b66b656.zip
cpython-0078a0c2a59a358fa032ec9847c108378b66b656.tar.gz
cpython-0078a0c2a59a358fa032ec9847c108378b66b656.tar.bz2
Permutations Python code equivalent in comment was invalid for Python 3 (GH-16927)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/itertoolsmodule.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index e60ad5b..3d39fa2 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -3076,12 +3076,15 @@ static PyTypeObject cwr_type = {
/* permutations object ********************************************************
def permutations(iterable, r=None):
- 'permutations(range(3), 2) --> (0,1) (0,2) (1,0) (1,2) (2,0) (2,1)'
+ # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
+ # permutations(range(3)) --> 012 021 102 120 201 210
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
- indices = range(n)
- cycles = range(n-r+1, n+1)[::-1]
+ if r > n:
+ return
+ indices = list(range(n))
+ cycles = list(range(n, n-r, -1))
yield tuple(pool[i] for i in indices[:r])
while n:
for i in reversed(range(r)):