Skip to content

Commit

Permalink
Merge branch 'main' into bpo-15987
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyhylton authored May 21, 2024
2 parents ec8af39 + f6da790 commit c7ea190
Show file tree
Hide file tree
Showing 22 changed files with 1,589 additions and 1,541 deletions.
6 changes: 6 additions & 0 deletions Doc/c-api/hash.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ See also the :c:member:`PyTypeObject.tp_hash` member and :ref:`numeric-hash`.

.. versionadded:: 3.13

.. c:macro:: PyHASH_MULTIPLIER
Prime multiplier used in string and various other hashes.

.. versionadded:: 3.13

.. c:macro:: PyHASH_INF
The hash value returned for a positive infinity.
Expand Down
5 changes: 3 additions & 2 deletions Include/cpython/pyhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#endif

/* Prime multiplier used in string and various other hashes. */
#define _PyHASH_MULTIPLIER 1000003UL /* 0xf4243 */
#define PyHASH_MULTIPLIER 1000003UL /* 0xf4243 */

/* Parameters used for the numeric hash implementation. See notes for
_Py_HashDouble in Python/pyhash.c. Numeric hashes are based on
Expand All @@ -17,9 +17,10 @@

#define PyHASH_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
#define PyHASH_INF 314159
#define PyHASH_IMAG _PyHASH_MULTIPLIER
#define PyHASH_IMAG PyHASH_MULTIPLIER

/* Aliases kept for backward compatibility with Python 3.12 */
#define _PyHASH_MULTIPLIER PyHASH_MULTIPLIER
#define _PyHASH_BITS PyHASH_BITS
#define _PyHASH_MODULUS PyHASH_MODULUS
#define _PyHASH_INF PyHASH_INF
Expand Down
24 changes: 23 additions & 1 deletion Lib/test/test_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def test_count(self):
#check proper internal error handling for large "step' sizes
count(1, maxsize+5); sys.exc_info()

def test_count_with_stride(self):
def test_count_with_step(self):
self.assertEqual(lzip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)])
self.assertEqual(lzip('abc',count(start=2,step=3)),
[('a', 2), ('b', 5), ('c', 8)])
Expand Down Expand Up @@ -590,6 +590,28 @@ def test_count_with_stride(self):
self.assertEqual(type(next(c)), int)
self.assertEqual(type(next(c)), float)

@threading_helper.requires_working_threading()
def test_count_threading(self, step=1):
# this test verifies multithreading consistency, which is
# mostly for testing builds without GIL, but nice to test anyway
count_to = 10_000
num_threads = 10
c = count(step=step)
def counting_thread():
for i in range(count_to):
next(c)
threads = []
for i in range(num_threads):
thread = threading.Thread(target=counting_thread)
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
self.assertEqual(next(c), count_to * num_threads * step)

def test_count_with_step_threading(self):
self.test_count_threading(step=5)

def test_cycle(self):
self.assertEqual(take(10, cycle('abc')), list('abcabcabca'))
self.assertEqual(list(cycle('')), [])
Expand Down
Loading

0 comments on commit c7ea190

Please sign in to comment.