Skip to content

Commit 651f262

Browse files
Drop math.primes
1 parent 81e40f0 commit 651f262

10 files changed

Lines changed: 11 additions & 427 deletions

‎Doc/library/math.integer.rst‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,3 @@ all return values are computed exactly and are integers.
9898
and the function returns ``n!``.
9999

100100
Raises :exc:`ValueError` if either of the arguments are negative.
101-
102-
103-
.. function:: primes(start=2, stop=None)
104-
105-
Return an iterator of the prime numbers *p* with ``start <= p < stop``,
106-
in increasing order.
107-
If *stop* is ``None`` (the default), the iteration does not stop.
108-
109-
Roughly equivalent to
110-
``(p for p in itertools.count(start) if isprime(p))``
111-
for the unbounded form.
112-
113-
The bounds must be less than 2\ :sup:`64`;
114-
raises :exc:`OverflowError` otherwise.
115-
Unbounded iteration raises :exc:`OverflowError`
116-
if the candidates reach that limit.
117-
118-
.. versionadded:: next

‎Doc/whatsnew/3.16.rst‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,7 @@ math.integer
283283
------------
284284

285285
* Added :func:`math.integer.isprime` for primality testing of integers
286-
less than 2**64 and
287-
:func:`math.integer.primes` for iterating over prime numbers.
286+
less than 2**64.
288287
(Contributed by Serhiy Storchaka in :gh:`153222`.)
289288

290289

‎Include/internal/pycore_global_objects_fini_generated.h‎

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Include/internal/pycore_global_strings.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,6 @@ struct _Py_global_strings {
820820
STRUCT_FOR_ID(stdout)
821821
STRUCT_FOR_ID(step)
822822
STRUCT_FOR_ID(steps)
823-
STRUCT_FOR_ID(stop)
824823
STRUCT_FOR_ID(store_name)
825824
STRUCT_FOR_ID(strategy)
826825
STRUCT_FOR_ID(strftime)

‎Include/internal/pycore_runtime_init_generated.h‎

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Include/internal/pycore_unicodeobject_generated.h‎

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Lib/test/test_math_integer.py‎

Lines changed: 3 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from decimal import Decimal
22
from fractions import Fraction
3-
import itertools
43
import random
54
import unittest
65
from test import support
@@ -449,8 +448,8 @@ class MathTests(IntMathTests):
449448
import math as module
450449

451450

452-
# isprime() and primes() exist only in math.integer, not in math, so their
453-
# tests are not in IntMathTests (which is re-run against math above).
451+
# isprime() exists only in math.integer, not in math, so its tests
452+
# are not in IntMathTests (which is re-run against math above).
454453

455454
class IsPrimeTests(unittest.TestCase):
456455
import math.integer as module
@@ -580,115 +579,6 @@ def test_isprime_non_integers(self):
580579
self.assertRaises(TypeError, isprime, 7, 11)
581580

582581

583-
class PrimesIterTests(unittest.TestCase):
584-
import math.integer as module
585-
586-
def test_primes(self):
587-
primes = self.module.primes
588-
expected = primes_below(10**4)
589-
self.assertEqual(list(primes(stop=10**4)), expected)
590-
self.assertEqual(len(expected), 1229)
591-
self.assertEqual(list(itertools.islice(primes(), 25)), expected[:25])
592-
593-
def test_primes_start(self):
594-
primes = self.module.primes
595-
for start in [-10**100, -100, -1, 0, 1, 2]:
596-
with self.subTest(start=start):
597-
self.assertEqual(list(primes(start, 10)), [2, 3, 5, 7])
598-
self.assertEqual(list(primes(3, 10)), [3, 5, 7])
599-
self.assertEqual(list(primes(4, 10)), [5, 7])
600-
self.assertEqual(list(primes(8, 12)), [11])
601-
self.assertEqual(list(primes(9, 12)), [11])
602-
self.assertEqual(list(primes(7, 8)), [7])
603-
604-
def test_primes_stop(self):
605-
primes = self.module.primes
606-
# The range is half-open.
607-
self.assertEqual(list(primes(2, 2)), [])
608-
self.assertEqual(list(primes(2, 3)), [2])
609-
self.assertEqual(list(primes(3, 3)), [])
610-
self.assertEqual(list(primes(7, 7)), [])
611-
self.assertEqual(list(primes(2, -10)), [])
612-
self.assertEqual(list(primes(10, 5)), [])
613-
self.assertEqual(list(primes(stop=0)), [])
614-
615-
def test_primes_unbounded(self):
616-
primes = self.module.primes
617-
it = primes()
618-
self.assertEqual([next(it) for _ in range(5)], [2, 3, 5, 7, 11])
619-
it = primes(10**6)
620-
self.assertEqual(next(it), 1000003)
621-
622-
def test_primes_huge(self):
623-
primes = self.module.primes
624-
boundary = 10**18
625-
expected = [n for n in range(boundary - 200, boundary + 200)
626-
if py_isprime(n)]
627-
self.assertEqual(list(primes(boundary - 200, boundary + 200)),
628-
expected)
629-
# The top of the supported range.
630-
expected = [n for n in range(2**64 - 200, 2**64) if py_isprime(n)]
631-
self.assertEqual(list(primes(2**64 - 200, 2**64 - 1)), expected)
632-
633-
def test_primes_overflow(self):
634-
primes = self.module.primes
635-
# The bounds must be less than 2**64.
636-
self.assertRaises(OverflowError, primes, 2**64)
637-
self.assertRaises(OverflowError, primes, 2**64 + 100, 2**64 + 200)
638-
self.assertRaises(OverflowError, primes, 0, 2**64)
639-
self.assertRaises(OverflowError, primes, 10**100)
640-
# An unbounded iterator raises when it runs out of the
641-
# supported range.
642-
it = primes(2**64 - 60)
643-
self.assertEqual(next(it), 2**64 - 59)
644-
self.assertRaises(OverflowError, next, it)
645-
self.assertRaises(StopIteration, next, it)
646-
647-
def test_primes_iterator_protocol(self):
648-
primes = self.module.primes
649-
it = primes(2, 10)
650-
self.assertIs(iter(it), it)
651-
self.assertEqual(list(it), [2, 3, 5, 7])
652-
# An exhausted iterator stays exhausted.
653-
self.assertEqual(list(it), [])
654-
self.assertRaises(StopIteration, next, it)
655-
# The iterator type cannot be instantiated directly.
656-
self.assertRaises(TypeError, type(primes()))
657-
658-
def test_primes_keywords(self):
659-
primes = self.module.primes
660-
self.assertEqual(list(primes(start=10, stop=30)), [11, 13, 17, 19, 23, 29])
661-
self.assertEqual(list(primes(10, stop=30)), [11, 13, 17, 19, 23, 29])
662-
663-
def test_primes_integer_like(self):
664-
primes = self.module.primes
665-
self.assertEqual(list(primes(True, 10)), [2, 3, 5, 7])
666-
self.assertEqual(list(primes(IntSubclass(3), IntSubclass(10))), [3, 5, 7])
667-
self.assertEqual(list(primes(MyIndexable(3), MyIndexable(10))), [3, 5, 7])
668-
# The yielded values are exact ints.
669-
for p in primes(IntSubclass(3), IntSubclass(10)):
670-
self.assertIs(type(p), int)
671-
672-
def test_primes_int_subclass_operators(self):
673-
# Overridden operators of an int subclass must not affect the
674-
# iteration.
675-
primes = self.module.primes
676-
self.assertEqual(list(primes(BadIntSubclass(3), BadIntSubclass(10))),
677-
[3, 5, 7])
678-
big = 10**18
679-
self.assertEqual(list(primes(BadIntSubclass(big), big + 100)),
680-
[n for n in range(big, big + 100) if py_isprime(n)])
681-
682-
def test_primes_non_integers(self):
683-
primes = self.module.primes
684-
self.assertRaises(TypeError, primes, 2.5)
685-
self.assertRaises(TypeError, primes, 2.5, 10)
686-
self.assertRaises(TypeError, primes, 2, 10.5)
687-
self.assertRaises(TypeError, primes, '2')
688-
self.assertRaises(TypeError, primes, 2, '10')
689-
self.assertRaises(TypeError, primes, 2, 10, 3)
690-
691-
692582
class MiscTests(unittest.TestCase):
693583

694584
def test_module_name(self):
@@ -700,11 +590,10 @@ def test_module_name(self):
700590
self.assertEqual(obj.__module__, 'math.integer')
701591

702592
def test_math_namespace(self):
703-
# New functions are added only to math.integer, not to math
593+
# The new function is added only to math.integer, not to math
704594
# (PEP 791).
705595
import math
706596
self.assertFalse(hasattr(math, 'isprime'))
707-
self.assertFalse(hasattr(math, 'primes'))
708597

709598

710599
if __name__ == '__main__':
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
Add :func:`math.integer.isprime` for primality testing of integers
2-
less than 2**64 and
3-
:func:`math.integer.primes` for iterating over prime numbers.
2+
less than 2**64.

‎Modules/clinic/mathintegermodule.c.h‎

Lines changed: 2 additions & 79 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)