Compare commits

...

4 Commits

Author SHA1 Message Date
Ken Jin 4140f10a16
bpo-42740: Fix get_args for PEP 585 collections.abc.Callable (GH-23963)
PR 1/2. Needs backport to 3.9.
2020-12-28 12:06:19 -08:00
Raymond Hettinger a9621bb301
bpo-42222: Modernize integer test/conversion in randrange() (#23064) 2020-12-28 11:10:34 -08:00
Ammar Askar 1031f23fc3
[workflow] Use MSVC problem matcher for Windows action build (GH-18532)
This makes warnings and errors from the compiler very prominent so this should help prevent warnings from sneaking into the code base and catch them in review. See https://discuss.python.org/t/using-github-problem-matchers-to-catch-warnings-early/4254 for more details

You can see a demo of this in action here: https://github.com/ammaraskar/cpython/pull/15/files#diff-9ba2eeca0f254ece0a9df4d7cb68e870

GCC and Sphinx matchers have previously been added in GH-18567 and GH-20325, respectively.
2020-12-28 12:28:40 -06:00
Erlend Egeberg Aasland bf108bb21e
bpo-40077: Fix typo in simplequeue_get_state_by_type() (GH-23975)
The typo did no damage, but it looks suspicious and confusing.
Introduced by GH-23136.

Skip news.

Automerge-Triggered-By: GH:pitrou
2020-12-28 09:47:16 -08:00
9 changed files with 109 additions and 15 deletions

19
.github/problem-matchers/msvc.json vendored Normal file
View File

@ -0,0 +1,19 @@
{
"__comment": "Taken from vscode's vs/workbench/contrib/tasks/common/problemMatcher.ts msCompile rule",
"problemMatcher": [
{
"owner": "msvc-problem-matcher",
"pattern": [
{
"regexp": "^(?:\\s+\\d+\\>)?([^\\s].*)\\((\\d+),?(\\d+)?(?:,\\d+,\\d+)?\\)\\s*:\\s+(error|warning|info)\\s+(\\w{1,2}\\d+)\\s*:\\s*(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"code": 5,
"message": 6
}
]
}
]
}

View File

@ -99,6 +99,8 @@ jobs:
if: needs.check_source.outputs.run_tests == 'true'
steps:
- uses: actions/checkout@v2
- name: Register MSVC problem matcher
run: echo "::add-matcher::.github/problem-matchers/msvc.json"
- name: Build CPython
run: .\PCbuild\build.bat -e -p x64
- name: Display build info

View File

@ -135,6 +135,15 @@ Functions for integers
values. Formerly it used a style like ``int(random()*n)`` which could produce
slightly uneven distributions.
.. deprecated:: 3.10
The automatic conversion of non-integer types to equivalent integers is
deprecated. Currently ``randrange(10.0)`` is losslessly converted to
``randrange(10)``. In the future, this will raise a :exc:`TypeError`.
.. deprecated:: 3.10
The exception raised for non-integral values such as ``range(10.5)``
will be changed from :exc:`ValueError` to :exc:`TypeError`.
.. function:: randint(a, b)
Return a random integer *N* such that ``a <= N <= b``. Alias for

View File

@ -51,6 +51,7 @@ from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
from math import tau as TWOPI, floor as _floor, isfinite as _isfinite
from os import urandom as _urandom
from _collections_abc import Set as _Set, Sequence as _Sequence
from operator import index as _index
from itertools import accumulate as _accumulate, repeat as _repeat
from bisect import bisect as _bisect
import os as _os
@ -297,8 +298,18 @@ class Random(_random.Random):
# This code is a bit messy to make it fast for the
# common case while still doing adequate error checking.
try:
istart = _index(start)
except TypeError:
if int(start) == start:
istart = int(start)
if istart != start:
_warn('Float arguments to randrange() have been deprecated\n'
'since Python 3.10 and will be removed in a subsequent '
'version.',
DeprecationWarning, 2)
else:
_warn('randrange() will raise TypeError in the future',
DeprecationWarning, 2)
raise ValueError("non-integer arg 1 for randrange()")
if stop is None:
if istart > 0:
@ -306,19 +317,40 @@ class Random(_random.Random):
raise ValueError("empty range for randrange()")
# stop argument supplied.
try:
istop = _index(stop)
except TypeError:
if int(stop) == stop:
istop = int(stop)
if istop != stop:
_warn('Float arguments to randrange() have been deprecated\n'
'since Python 3.10 and will be removed in a subsequent '
'version.',
DeprecationWarning, 2)
else:
_warn('randrange() will raise TypeError in the future',
DeprecationWarning, 2)
raise ValueError("non-integer stop for randrange()")
try:
istep = _index(step)
except TypeError:
if int(step) == step:
istep = int(step)
_warn('Float arguments to randrange() have been deprecated\n'
'since Python 3.10 and will be removed in a subsequent '
'version.',
DeprecationWarning, 2)
else:
_warn('randrange() will raise TypeError in the future',
DeprecationWarning, 2)
raise ValueError("non-integer step for randrange()")
width = istop - istart
if step == 1 and width > 0:
if istep == 1 and width > 0:
return istart + self._randbelow(width)
if step == 1:
if istep == 1:
raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))
# Non-unit step argument supplied.
istep = int(step)
if istep != step:
raise ValueError("non-integer step for randrange()")
if istep > 0:
n = (width + istep - 1) // istep
elif istep < 0:

View File

@ -542,6 +542,26 @@ class SystemRandom_TestBasicOps(TestBasicOps, unittest.TestCase):
raises(0, 42, 0)
raises(0, 42, 3.14159)
def test_randrange_argument_handling(self):
randrange = self.gen.randrange
with self.assertWarns(DeprecationWarning):
randrange(10.0, 20, 2)
with self.assertWarns(DeprecationWarning):
randrange(10, 20.0, 2)
with self.assertWarns(DeprecationWarning):
randrange(10, 20, 1.0)
with self.assertWarns(DeprecationWarning):
randrange(10, 20, 2.0)
with self.assertWarns(DeprecationWarning):
with self.assertRaises(ValueError):
randrange(10.5)
with self.assertWarns(DeprecationWarning):
with self.assertRaises(ValueError):
randrange(10, 20.5)
with self.assertWarns(DeprecationWarning):
with self.assertRaises(ValueError):
randrange(10, 20, 1.5)
def test_randbelow_logic(self, _log=log, int=int):
# check bitcount transition points: 2**i and 2**(i+1)-1
# show that: k = int(1.001 + _log(n, 2))

View File

@ -3048,6 +3048,11 @@ class GetUtilitiesTestCase(TestCase):
self.assertEqual(get_args(Callable), ())
self.assertEqual(get_args(list[int]), (int,))
self.assertEqual(get_args(list), ())
self.assertEqual(get_args(collections.abc.Callable[[int], str]), ([int], str))
self.assertEqual(get_args(collections.abc.Callable[..., str]), (..., str))
self.assertEqual(get_args(collections.abc.Callable[[], str]), ([], str))
self.assertEqual(get_args(collections.abc.Callable[[int], str]),
get_args(Callable[[int], str]))
class CollectionsAbcTests(BaseTestCase):

View File

@ -1684,13 +1684,11 @@ def get_args(tp):
"""
if isinstance(tp, _AnnotatedAlias):
return (tp.__origin__,) + tp.__metadata__
if isinstance(tp, _GenericAlias):
if isinstance(tp, (_GenericAlias, GenericAlias)):
res = tp.__args__
if tp.__origin__ is collections.abc.Callable and res[0] is not Ellipsis:
res = (list(res[:-1]), res[-1])
return res
if isinstance(tp, GenericAlias):
return tp.__args__
return ()

View File

@ -0,0 +1,9 @@
Harmonized random.randrange() argument handling to match range().
* The integer test and conversion in randrange() now uses
operator.index().
* Non-integer arguments to randrange() are deprecated.
* The *ValueError* is deprecated in favor of a *TypeError*.
* It now runs a little faster than before.
(Contributed by Raymond Hettinger and Serhiy Storchaka.)

View File

@ -15,7 +15,7 @@ simplequeue_get_state(PyObject *module)
return state;
}
static struct PyModuleDef queuemodule;
#define simplequeue_get_state_by_type(tp) \
#define simplequeue_get_state_by_type(type) \
(simplequeue_get_state(_PyType_GetModuleByDef(type, &queuemodule)))
typedef struct {