diff -r 9e59cb403efa Lib/test/test_compile.py --- a/Lib/test/test_compile.py Tue Sep 27 11:37:10 2016 +0300 +++ b/Lib/test/test_compile.py Tue Sep 27 13:05:03 2016 +0300 @@ -567,19 +567,25 @@ if 1: # objects are accepted, which could be not terminated. with self.assertRaisesRegex(ValueError, "cannot contain null"): compile("123\x00", "", "eval") - with self.assertRaisesRegex(ValueError, "cannot contain null"): + with self.assertRaisesRegex(ValueError, "cannot contain null"), \ + self.assertWarns(DeprecationWarning): compile(memoryview(b"123\x00"), "", "eval") - code = compile(memoryview(b"123\x00")[1:-1], "", "eval") + with self.assertWarns(DeprecationWarning): + code = compile(memoryview(b"123\x00")[1:-1], "", "eval") self.assertEqual(eval(code), 23) - code = compile(memoryview(b"1234")[1:-1], "", "eval") + with self.assertWarns(DeprecationWarning): + code = compile(memoryview(b"1234")[1:-1], "", "eval") self.assertEqual(eval(code), 23) - code = compile(memoryview(b"$23$")[1:-1], "", "eval") + with self.assertWarns(DeprecationWarning): + code = compile(memoryview(b"$23$")[1:-1], "", "eval") self.assertEqual(eval(code), 23) # Also test when eval() and exec() do the compilation step - self.assertEqual(eval(memoryview(b"1234")[1:-1]), 23) + with self.assertWarns(DeprecationWarning): + self.assertEqual(eval(memoryview(b"1234")[1:-1]), 23) namespace = dict() - exec(memoryview(b"ax = 123")[1:-1], namespace) + with self.assertWarns(DeprecationWarning): + exec(memoryview(b"ax = 123")[1:-1], namespace) self.assertEqual(namespace['x'], 12) def check_constant(self, func, expected): diff -r 9e59cb403efa Lib/test/test_float.py --- a/Lib/test/test_float.py Tue Sep 27 11:37:10 2016 +0300 +++ b/Lib/test/test_float.py Tue Sep 27 13:05:03 2016 +0300 @@ -85,38 +85,42 @@ class GeneralFloatCases(unittest.TestCas def test_non_numeric_input_types(self): # Test possible non-numeric types for the argument x, including # subclasses of the explicitly documented accepted types. + def check(f): + x = f(b" 3.14 ") + self.assertEqual(float(x), 3.14) + with self.assertRaisesRegex(ValueError, "could not convert"): + float(f(b'A' * 0x10)) + class CustomStr(str): pass class CustomBytes(bytes): pass class CustomByteArray(bytearray): pass - factories = [ - bytes, - bytearray, - lambda b: CustomStr(b.decode()), - CustomBytes, - CustomByteArray, - memoryview, - ] + check(bytes) + check(bytearray) + check(lambda b: CustomStr(b.decode())) + check(CustomBytes) + check(CustomByteArray) + with self.assertWarns(DeprecationWarning): + check(memoryview) try: from array import array except ImportError: pass else: - factories.append(lambda b: array('B', b)) - - for f in factories: - x = f(b" 3.14 ") - with self.subTest(type(x)): - self.assertEqual(float(x), 3.14) - with self.assertRaisesRegex(ValueError, "could not convert"): - float(f(b'A' * 0x10)) + with self.assertWarns(DeprecationWarning): + check(lambda b: array('B', b)) def test_float_memoryview(self): - self.assertEqual(float(memoryview(b'12.3')[1:4]), 2.3) - self.assertEqual(float(memoryview(b'12.3\x00')[1:4]), 2.3) - self.assertEqual(float(memoryview(b'12.3 ')[1:4]), 2.3) - self.assertEqual(float(memoryview(b'12.3A')[1:4]), 2.3) - self.assertEqual(float(memoryview(b'12.34')[1:4]), 2.3) + with self.assertWarns(DeprecationWarning): + self.assertEqual(float(memoryview(b'12.3')[1:4]), 2.3) + with self.assertWarns(DeprecationWarning): + self.assertEqual(float(memoryview(b'12.3\x00')[1:4]), 2.3) + with self.assertWarns(DeprecationWarning): + self.assertEqual(float(memoryview(b'12.3 ')[1:4]), 2.3) + with self.assertWarns(DeprecationWarning): + self.assertEqual(float(memoryview(b'12.3A')[1:4]), 2.3) + with self.assertWarns(DeprecationWarning): + self.assertEqual(float(memoryview(b'12.34')[1:4]), 2.3) def test_error_message(self): testlist = ('\xbd', '123\xbd', ' 123 456 ') diff -r 9e59cb403efa Lib/test/test_int.py --- a/Lib/test/test_int.py Tue Sep 27 11:37:10 2016 +0300 +++ b/Lib/test/test_int.py Tue Sep 27 13:05:03 2016 +0300 @@ -296,44 +296,48 @@ class IntTestCases(unittest.TestCase): def test_non_numeric_input_types(self): # Test possible non-numeric types for the argument x, including # subclasses of the explicitly documented accepted types. + def check(f): + x = f(b'100') + self.assertEqual(int(x), 100) + if isinstance(x, (str, bytes, bytearray)): + self.assertEqual(int(x, 2), 4) + else: + msg = "can't convert non-string" + with self.assertRaisesRegex(TypeError, msg): + int(x, 2) + with self.assertRaisesRegex(ValueError, 'invalid literal'): + int(f(b'A' * 0x10)) + class CustomStr(str): pass class CustomBytes(bytes): pass class CustomByteArray(bytearray): pass - factories = [ - bytes, - bytearray, - lambda b: CustomStr(b.decode()), - CustomBytes, - CustomByteArray, - memoryview, - ] + check(bytes) + check(bytearray) + check(lambda b: CustomStr(b.decode())) + check(CustomBytes) + check(CustomByteArray) + with self.assertWarns(DeprecationWarning): + check(memoryview) try: from array import array except ImportError: pass else: - factories.append(lambda b: array('B', b)) - - for f in factories: - x = f(b'100') - with self.subTest(type(x)): - self.assertEqual(int(x), 100) - if isinstance(x, (str, bytes, bytearray)): - self.assertEqual(int(x, 2), 4) - else: - msg = "can't convert non-string" - with self.assertRaisesRegex(TypeError, msg): - int(x, 2) - with self.assertRaisesRegex(ValueError, 'invalid literal'): - int(f(b'A' * 0x10)) + with self.assertWarns(DeprecationWarning): + check(lambda b: array('B', b)) def test_int_memoryview(self): - self.assertEqual(int(memoryview(b'123')[1:3]), 23) - self.assertEqual(int(memoryview(b'123\x00')[1:3]), 23) - self.assertEqual(int(memoryview(b'123 ')[1:3]), 23) - self.assertEqual(int(memoryview(b'123A')[1:3]), 23) - self.assertEqual(int(memoryview(b'1234')[1:3]), 23) + with self.assertWarns(DeprecationWarning): + self.assertEqual(int(memoryview(b'123')[1:3]), 23) + with self.assertWarns(DeprecationWarning): + self.assertEqual(int(memoryview(b'123\x00')[1:3]), 23) + with self.assertWarns(DeprecationWarning): + self.assertEqual(int(memoryview(b'123 ')[1:3]), 23) + with self.assertWarns(DeprecationWarning): + self.assertEqual(int(memoryview(b'123A')[1:3]), 23) + with self.assertWarns(DeprecationWarning): + self.assertEqual(int(memoryview(b'1234')[1:3]), 23) def test_string_float(self): self.assertRaises(ValueError, int, '1.2') diff -r 9e59cb403efa Objects/abstract.c --- a/Objects/abstract.c Tue Sep 27 11:37:10 2016 +0300 +++ b/Objects/abstract.c Tue Sep 27 13:05:03 2016 +0300 @@ -1371,6 +1371,12 @@ PyNumber_Long(PyObject *o) if (PyObject_GetBuffer(o, &view, PyBUF_SIMPLE) == 0) { PyObject *bytes; + if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, + "int() argument must be a string, a bytes object, " + "a bytearray or a number, not '%.100s'", + o->ob_type->tp_name)) { + return NULL; + } /* Copy to NUL-terminated buffer. */ bytes = PyBytes_FromStringAndSize((const char *)view.buf, view.len); if (bytes == NULL) { diff -r 9e59cb403efa Objects/floatobject.c --- a/Objects/floatobject.c Tue Sep 27 11:37:10 2016 +0300 +++ b/Objects/floatobject.c Tue Sep 27 13:05:03 2016 +0300 @@ -185,6 +185,12 @@ PyFloat_FromString(PyObject *v) len = PyByteArray_GET_SIZE(v); } else if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) == 0) { + if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, + "float() argument must be a string, a bytes object, " + "a bytearray or a number, not '%.100s'", + v->ob_type->tp_name)) { + return NULL; + } s = (const char *)view.buf; len = view.len; /* Copy to NUL-terminated buffer. */ diff -r 9e59cb403efa Python/bltinmodule.c --- a/Python/bltinmodule.c Tue Sep 27 11:37:10 2016 +0300 +++ b/Python/bltinmodule.c Tue Sep 27 13:05:03 2016 +0300 @@ -608,6 +608,12 @@ source_as_string(PyObject *cmd, const ch size = PyByteArray_GET_SIZE(cmd); } else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) { + if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, + "%s() arg 1 must be a %s object, not '%.100s'", + funcname, what, + cmd->ob_type->tp_name)) { + return NULL; + } /* Copy to NUL-terminated buffer. */ *cmd_copy = PyBytes_FromStringAndSize( (const char *)view.buf, view.len); @@ -736,7 +742,7 @@ builtin_compile_impl(PyObject *module, P goto finally; } - str = source_as_string(source, "compile", "string, bytes or AST", &cf, &source_copy); + str = source_as_string(source, "compile", "string, bytes, bytearray or AST", &cf, &source_copy); if (str == NULL) goto error; @@ -864,7 +870,7 @@ builtin_eval_impl(PyObject *module, PyOb } cf.cf_flags = PyCF_SOURCE_IS_UTF8; - str = source_as_string(source, "eval", "string, bytes or code", &cf, &source_copy); + str = source_as_string(source, "eval", "string, bytes, bytearray or code", &cf, &source_copy); if (str == NULL) return NULL; @@ -949,7 +955,7 @@ builtin_exec_impl(PyObject *module, PyOb PyCompilerFlags cf; cf.cf_flags = PyCF_SOURCE_IS_UTF8; str = source_as_string(source, "exec", - "string, bytes or code", &cf, + "string, bytes, bytearray or code", &cf, &source_copy); if (str == NULL) return NULL;