Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _test():
import traceback
import types
import unittest
from io import StringIO, IncrementalNewlineDecoder
from io import StringIO, TextIOWrapper, BytesIO
from collections import namedtuple
import _colorize # Used in doctests
from _colorize import ANSIColors, can_colorize
Expand Down Expand Up @@ -237,10 +237,6 @@ def _normalize_module(module, depth=2):
else:
raise TypeError("Expected a module, string, or None")

def _newline_convert(data):
# The IO module provides a handy decoder for universal newline conversion
return IncrementalNewlineDecoder(None, True).decode(data, True)

def _load_testfile(filename, package, module_relative, encoding):
if module_relative:
package = _normalize_module(package, 3)
Expand All @@ -252,10 +248,9 @@ def _load_testfile(filename, package, module_relative, encoding):
pass
if hasattr(loader, 'get_data'):
file_contents = loader.get_data(filename)
file_contents = file_contents.decode(encoding)
# get_data() opens files as 'rb', so one must do the equivalent
# conversion as universal newlines would do.
return _newline_convert(file_contents), filename
return TextIOWrapper(BytesIO(file_contents), encoding=encoding, newline=None).read(), filename
with open(filename, encoding=encoding) as f:
return f.read(), filename

Expand Down
3 changes: 1 addition & 2 deletions Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,7 @@ def decode_source(source_bytes):
import tokenize # To avoid bootstrap issues.
source_bytes_readline = _io.BytesIO(source_bytes).readline
encoding = tokenize.detect_encoding(source_bytes_readline)
newline_decoder = _io.IncrementalNewlineDecoder(None, True)
return newline_decoder.decode(source_bytes.decode(encoding[0]))
return _io.TextIOWrapper(_io.BytesIO(source_bytes), encoding=encoding[0], newline=None).read()


# Module specifications #######################################################
Expand Down
18 changes: 14 additions & 4 deletions Lib/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,14 @@ def stdev(data, xbar=None):
if n < 2:
raise StatisticsError('stdev requires at least two data points')
mss = ss / (n - 1)
try:
mss_numerator = mss.numerator
mss_denominator = mss.denominator
except AttributeError:
raise ValueError('inf or nan encountered in data')
if issubclass(T, Decimal):
return _decimal_sqrt_of_frac(mss.numerator, mss.denominator)
return _float_sqrt_of_frac(mss.numerator, mss.denominator)
return _decimal_sqrt_of_frac(mss_numerator, mss_denominator)
return _float_sqrt_of_frac(mss_numerator, mss_denominator)


def pstdev(data, mu=None):
Expand All @@ -637,9 +642,14 @@ def pstdev(data, mu=None):
if n < 1:
raise StatisticsError('pstdev requires at least one data point')
mss = ss / n
try:
mss_numerator = mss.numerator
mss_denominator = mss.denominator
except AttributeError:
raise ValueError('inf or nan encountered in data')
if issubclass(T, Decimal):
return _decimal_sqrt_of_frac(mss.numerator, mss.denominator)
return _float_sqrt_of_frac(mss.numerator, mss.denominator)
return _decimal_sqrt_of_frac(mss_numerator, mss_denominator)
return _float_sqrt_of_frac(mss_numerator, mss_denominator)


## Statistics for relations between two inputs #############################
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_importlib/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ def test_universal_newlines(self):
mock = self.SourceOnlyLoaderMock('mod.file')
source = "x = 42\r\ny = -13\r\n"
mock.source = source.encode('utf-8')
expect = io.IncrementalNewlineDecoder(None, True).decode(source)
expect = io.StringIO(source, newline=None).getvalue()
self.assertEqual(mock.get_source(name), expect)


Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_perf_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,24 @@ def test_sys_api_get_status(self):
"""
assert_python_ok("-c", code, PYTHON_JIT="0")

def test_sys_api_perf_jit_backend(self):
code = """if 1:
import sys
sys.activate_stack_trampoline("perf_jit")
assert sys.is_stack_trampoline_active() is True
sys.deactivate_stack_trampoline()
assert sys.is_stack_trampoline_active() is False
"""
assert_python_ok("-c", code, PYTHON_JIT="0")

def test_sys_api_with_existing_perf_jit_trampoline(self):
code = """if 1:
import sys
sys.activate_stack_trampoline("perf_jit")
sys.activate_stack_trampoline("perf_jit")
"""
assert_python_ok("-c", code, PYTHON_JIT="0")


def is_unwinding_reliable_with_frame_pointers():
cflags = sysconfig.get_config_var("PY_CORE_CFLAGS")
Expand Down
9 changes: 8 additions & 1 deletion Lib/test/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2005,7 +2005,6 @@ def test_iter_list_same(self):
expected = self.func(data)
self.assertEqual(self.func(iter(data)), expected)


class TestPVariance(VarianceStdevMixin, NumericTestCase, UnivariateTypeMixin):
# Tests for population variance.
def setUp(self):
Expand Down Expand Up @@ -2113,6 +2112,14 @@ def test_center_not_at_mean(self):
self.assertEqual(self.func(data), 2.5)
self.assertEqual(self.func(data, mu=0.5), 6.5)

def test_gh_140938(self):
# Inputs with inf/nan should raise a ValueError
with self.assertRaises(ValueError):
self.func([1.0, math.inf])
with self.assertRaises(ValueError):
self.func([1.0, math.nan])


class TestSqrtHelpers(unittest.TestCase):

def test_integer_sqrt_of_frac_rto(self):
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_unittest/test_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ def testUnexpectedSuccess(self):
class Empty(unittest.TestCase):
pass

class SetUpClassFailure(unittest.TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
raise Exception
def testPass(self):
pass

class TestLoader(unittest.TestLoader):
"""Test loader that returns a suite containing the supplied testcase."""

Expand Down Expand Up @@ -191,6 +199,18 @@ def test_ExitEmptySuite(self):
out = stream.getvalue()
self.assertIn('\nNO TESTS RAN\n', out)

def test_ExitSetUpClassFailureSuite(self):
stream = BufferedWriter()
with self.assertRaises(SystemExit) as cm:
unittest.main(
argv=["setup_class_failure"],
testRunner=unittest.TextTestRunner(stream=stream),
testLoader=self.TestLoader(self.SetUpClassFailure))
self.assertEqual(cm.exception.code, 1)
out = stream.getvalue()
self.assertIn("ERROR: setUpClass", out)
self.assertIn("SetUpClassFailure", out)


class InitialisableProgram(unittest.TestProgram):
exit = False
Expand Down
8 changes: 4 additions & 4 deletions Lib/unittest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,12 @@ def runTests(self):
testRunner = self.testRunner
self.result = testRunner.run(self.test)
if self.exit:
if self.result.testsRun == 0 and len(self.result.skipped) == 0:
if not self.result.wasSuccessful():
sys.exit(1)
elif self.result.testsRun == 0 and len(self.result.skipped) == 0:
sys.exit(_NO_TESTS_EXITCODE)
elif self.result.wasSuccessful():
sys.exit(0)
else:
sys.exit(1)
sys.exit(0)


main = TestProgram
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replace :class:`io.IncrementalNewlineDecoder` with non incremental newline decoders in codebase where :meth:`!io.IncrementalNewlineDecoder.decode` was being called once.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :func:`sys.activate_stack_trampoline` to properly support the
``perf_jit`` backend. Patch by Pablo Galindo.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The :func:`statistics.stdev` and :func:`statistics.pstdev` functions now raise a
:exc:`ValueError` when the input contains an infinity or a NaN.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use exitcode ``1`` instead of ``5`` if :func:`unittest.TestCase.setUpClass` raises an exception
16 changes: 8 additions & 8 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2380,14 +2380,14 @@ sys_activate_stack_trampoline_impl(PyObject *module, const char *backend)
return NULL;
}
}
else if (strcmp(backend, "perf_jit") == 0) {
_PyPerf_Callbacks cur_cb;
_PyPerfTrampoline_GetCallbacks(&cur_cb);
if (cur_cb.write_state != _Py_perfmap_jit_callbacks.write_state) {
if (_PyPerfTrampoline_SetCallbacks(&_Py_perfmap_jit_callbacks) < 0 ) {
PyErr_SetString(PyExc_ValueError, "can't activate perf jit trampoline");
return NULL;
}
}
else if (strcmp(backend, "perf_jit") == 0) {
_PyPerf_Callbacks cur_cb;
_PyPerfTrampoline_GetCallbacks(&cur_cb);
if (cur_cb.write_state != _Py_perfmap_jit_callbacks.write_state) {
if (_PyPerfTrampoline_SetCallbacks(&_Py_perfmap_jit_callbacks) < 0 ) {
PyErr_SetString(PyExc_ValueError, "can't activate perf jit trampoline");
return NULL;
}
}
}
Expand Down
Loading