From cb4af3cc4c01baeea5ff7e41ca7de1f877222535 Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Sat, 30 Jul 2016 16:26:03 +1000 Subject: Issue #27366: Implement PEP 487 - __init_subclass__ called when new subclasses defined - __set_name__ called when descriptors are part of a class definition --- Lib/test/test_builtin.py | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) (limited to 'Lib/test/test_builtin.py') diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 8cc1b0074b..acc4f9ce81 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -1699,21 +1699,11 @@ class TestType(unittest.TestCase): self.assertEqual(x.spam(), 'spam42') self.assertEqual(x.to_bytes(2, 'little'), b'\x2a\x00') - def test_type_new_keywords(self): - class B: - def ham(self): - return 'ham%d' % self - C = type.__new__(type, - name='C', - bases=(B, int), - dict={'spam': lambda self: 'spam%s' % self}) - self.assertEqual(C.__name__, 'C') - self.assertEqual(C.__qualname__, 'C') - self.assertEqual(C.__module__, __name__) - self.assertEqual(C.__bases__, (B, int)) - self.assertIs(C.__base__, int) - self.assertIn('spam', C.__dict__) - self.assertNotIn('ham', C.__dict__) + def test_type_nokwargs(self): + with self.assertRaises(TypeError): + type('a', (), {}, x=5) + with self.assertRaises(TypeError): + type('a', (), dict={}) def test_type_name(self): for name in 'A', '\xc4', '\U0001f40d', 'B.A', '42', '': -- cgit v1.2.1 From 823616bdc22856b5df918a443051ec6ec2e9073c Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 5 Sep 2016 14:50:11 -0700 Subject: Issue #24254: Preserve class attribute definition order. --- Lib/test/test_builtin.py | 192 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 191 insertions(+), 1 deletion(-) (limited to 'Lib/test/test_builtin.py') diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 7741a7911d..486f445069 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -16,8 +16,10 @@ import traceback import types import unittest import warnings +from collections import OrderedDict from operator import neg -from test.support import TESTFN, unlink, run_unittest, check_warnings +from test.support import (TESTFN, unlink, run_unittest, check_warnings, + cpython_only) from test.support.script_helper import assert_python_ok try: import pty, signal @@ -1778,6 +1780,194 @@ class TestType(unittest.TestCase): A.__doc__ = doc self.assertEqual(A.__doc__, doc) + def test_type_definition_order_nonempty(self): + class Spam: + b = 1 + c = 3 + a = 2 + d = 4 + eggs = 2 + e = 5 + b = 42 + + self.assertEqual(Spam.__definition_order__, + ('__module__', '__qualname__', + 'b', 'c', 'a', 'd', 'eggs', 'e')) + + def test_type_definition_order_empty(self): + class Empty: + pass + + self.assertEqual(Empty.__definition_order__, + ('__module__', '__qualname__')) + + def test_type_definition_order_on_instance(self): + class Spam: + a = 2 + b = 1 + c = 3 + with self.assertRaises(AttributeError): + Spam().__definition_order__ + + def test_type_definition_order_set_to_None(self): + class Spam: + a = 2 + b = 1 + c = 3 + Spam.__definition_order__ = None + self.assertEqual(Spam.__definition_order__, None) + + def test_type_definition_order_set_to_tuple(self): + class Spam: + a = 2 + b = 1 + c = 3 + Spam.__definition_order__ = ('x', 'y', 'z') + self.assertEqual(Spam.__definition_order__, ('x', 'y', 'z')) + + def test_type_definition_order_deleted(self): + class Spam: + a = 2 + b = 1 + c = 3 + del Spam.__definition_order__ + self.assertEqual(Spam.__definition_order__, None) + + def test_type_definition_order_set_to_bad_type(self): + class Spam: + a = 2 + b = 1 + c = 3 + Spam.__definition_order__ = 42 + self.assertEqual(Spam.__definition_order__, 42) + + def test_type_definition_order_builtins(self): + self.assertEqual(object.__definition_order__, None) + self.assertEqual(type.__definition_order__, None) + self.assertEqual(dict.__definition_order__, None) + self.assertEqual(type(None).__definition_order__, None) + + def test_type_definition_order_dunder_names_included(self): + class Dunder: + VAR = 3 + def __init__(self): + pass + + self.assertEqual(Dunder.__definition_order__, + ('__module__', '__qualname__', + 'VAR', '__init__')) + + def test_type_definition_order_only_dunder_names(self): + class DunderOnly: + __xyz__ = None + def __init__(self): + pass + + self.assertEqual(DunderOnly.__definition_order__, + ('__module__', '__qualname__', + '__xyz__', '__init__')) + + def test_type_definition_order_underscore_names(self): + class HalfDunder: + __whether_to_be = True + or_not_to_be__ = False + + self.assertEqual(HalfDunder.__definition_order__, + ('__module__', '__qualname__', + '_HalfDunder__whether_to_be', 'or_not_to_be__')) + + def test_type_definition_order_with_slots(self): + class Slots: + __slots__ = ('x', 'y') + a = 1 + b = 2 + + self.assertEqual(Slots.__definition_order__, + ('__module__', '__qualname__', + '__slots__', 'a', 'b')) + + def test_type_definition_order_overwritten_None(self): + class OverwrittenNone: + __definition_order__ = None + a = 1 + b = 2 + c = 3 + + self.assertEqual(OverwrittenNone.__definition_order__, None) + + def test_type_definition_order_overwritten_tuple(self): + class OverwrittenTuple: + __definition_order__ = ('x', 'y', 'z') + a = 1 + b = 2 + c = 3 + + self.assertEqual(OverwrittenTuple.__definition_order__, + ('x', 'y', 'z')) + + def test_type_definition_order_overwritten_bad_item(self): + with self.assertRaises(TypeError): + class PoorlyOverwritten: + __definition_order__ = ('a', 2, 'c') + a = 1 + b = 2 + c = 3 + + def test_type_definition_order_overwritten_bad_type(self): + with self.assertRaises(TypeError): + class PoorlyOverwritten: + __definition_order__ = ['a', 2, 'c'] + a = 1 + b = 2 + c = 3 + + def test_type_definition_order_metaclass(self): + class Meta(type): + SPAM = 42 + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + self.assertEqual(Meta.__definition_order__, + ('__module__', '__qualname__', + 'SPAM', '__init__')) + + def test_type_definition_order_OrderedDict(self): + class Meta(type): + def __prepare__(self, *args, **kwargs): + return OrderedDict() + + class WithODict(metaclass=Meta): + x='y' + + self.assertEqual(WithODict.__definition_order__, + ('__module__', '__qualname__', 'x')) + + class Meta(type): + def __prepare__(self, *args, **kwargs): + class ODictSub(OrderedDict): + pass + return ODictSub() + + class WithODictSub(metaclass=Meta): + x='y' + + self.assertEqual(WithODictSub.__definition_order__, + ('__module__', '__qualname__', 'x')) + + @cpython_only + def test_type_definition_order_cpython(self): + # some implementations will have an ordered-by-default dict. + + class Meta(type): + def __prepare__(self, *args, **kwargs): + return {} + + class NotOrdered(metaclass=Meta): + x='y' + + self.assertEqual(NotOrdered.__definition_order__, None) + def test_bad_args(self): with self.assertRaises(TypeError): type() -- cgit v1.2.1 From 03c8a473999b08710ae3661724cf1bf1d76bc555 Mon Sep 17 00:00:00 2001 From: R David Murray Date: Thu, 8 Sep 2016 13:59:53 -0400 Subject: #27364: fix "incorrect" uses of escape character in the stdlib. And most of the tools. Patch by Emanual Barry, reviewed by me, Serhiy Storchaka, and Martin Panter. --- Lib/test/test_builtin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Lib/test/test_builtin.py') diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 486f445069..c0343eff68 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -83,7 +83,7 @@ test_conv_no_sign = [ ('', ValueError), (' ', ValueError), (' \t\t ', ValueError), - (str(b'\u0663\u0661\u0664 ','raw-unicode-escape'), 314), + (str(br'\u0663\u0661\u0664 ','raw-unicode-escape'), 314), (chr(0x200), ValueError), ] @@ -105,7 +105,7 @@ test_conv_sign = [ ('', ValueError), (' ', ValueError), (' \t\t ', ValueError), - (str(b'\u0663\u0661\u0664 ','raw-unicode-escape'), 314), + (str(br'\u0663\u0661\u0664 ','raw-unicode-escape'), 314), (chr(0x200), ValueError), ] -- cgit v1.2.1 From 761928cf31626534ebb81a6326c61b42050ef388 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 8 Sep 2016 15:11:11 -0700 Subject: Issue #24254: Drop cls.__definition_order__. --- Lib/test/test_builtin.py | 192 +---------------------------------------------- 1 file changed, 1 insertion(+), 191 deletions(-) (limited to 'Lib/test/test_builtin.py') diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index c0343eff68..e0cffe1d9c 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -16,10 +16,8 @@ import traceback import types import unittest import warnings -from collections import OrderedDict from operator import neg -from test.support import (TESTFN, unlink, run_unittest, check_warnings, - cpython_only) +from test.support import TESTFN, unlink, run_unittest, check_warnings from test.support.script_helper import assert_python_ok try: import pty, signal @@ -1780,194 +1778,6 @@ class TestType(unittest.TestCase): A.__doc__ = doc self.assertEqual(A.__doc__, doc) - def test_type_definition_order_nonempty(self): - class Spam: - b = 1 - c = 3 - a = 2 - d = 4 - eggs = 2 - e = 5 - b = 42 - - self.assertEqual(Spam.__definition_order__, - ('__module__', '__qualname__', - 'b', 'c', 'a', 'd', 'eggs', 'e')) - - def test_type_definition_order_empty(self): - class Empty: - pass - - self.assertEqual(Empty.__definition_order__, - ('__module__', '__qualname__')) - - def test_type_definition_order_on_instance(self): - class Spam: - a = 2 - b = 1 - c = 3 - with self.assertRaises(AttributeError): - Spam().__definition_order__ - - def test_type_definition_order_set_to_None(self): - class Spam: - a = 2 - b = 1 - c = 3 - Spam.__definition_order__ = None - self.assertEqual(Spam.__definition_order__, None) - - def test_type_definition_order_set_to_tuple(self): - class Spam: - a = 2 - b = 1 - c = 3 - Spam.__definition_order__ = ('x', 'y', 'z') - self.assertEqual(Spam.__definition_order__, ('x', 'y', 'z')) - - def test_type_definition_order_deleted(self): - class Spam: - a = 2 - b = 1 - c = 3 - del Spam.__definition_order__ - self.assertEqual(Spam.__definition_order__, None) - - def test_type_definition_order_set_to_bad_type(self): - class Spam: - a = 2 - b = 1 - c = 3 - Spam.__definition_order__ = 42 - self.assertEqual(Spam.__definition_order__, 42) - - def test_type_definition_order_builtins(self): - self.assertEqual(object.__definition_order__, None) - self.assertEqual(type.__definition_order__, None) - self.assertEqual(dict.__definition_order__, None) - self.assertEqual(type(None).__definition_order__, None) - - def test_type_definition_order_dunder_names_included(self): - class Dunder: - VAR = 3 - def __init__(self): - pass - - self.assertEqual(Dunder.__definition_order__, - ('__module__', '__qualname__', - 'VAR', '__init__')) - - def test_type_definition_order_only_dunder_names(self): - class DunderOnly: - __xyz__ = None - def __init__(self): - pass - - self.assertEqual(DunderOnly.__definition_order__, - ('__module__', '__qualname__', - '__xyz__', '__init__')) - - def test_type_definition_order_underscore_names(self): - class HalfDunder: - __whether_to_be = True - or_not_to_be__ = False - - self.assertEqual(HalfDunder.__definition_order__, - ('__module__', '__qualname__', - '_HalfDunder__whether_to_be', 'or_not_to_be__')) - - def test_type_definition_order_with_slots(self): - class Slots: - __slots__ = ('x', 'y') - a = 1 - b = 2 - - self.assertEqual(Slots.__definition_order__, - ('__module__', '__qualname__', - '__slots__', 'a', 'b')) - - def test_type_definition_order_overwritten_None(self): - class OverwrittenNone: - __definition_order__ = None - a = 1 - b = 2 - c = 3 - - self.assertEqual(OverwrittenNone.__definition_order__, None) - - def test_type_definition_order_overwritten_tuple(self): - class OverwrittenTuple: - __definition_order__ = ('x', 'y', 'z') - a = 1 - b = 2 - c = 3 - - self.assertEqual(OverwrittenTuple.__definition_order__, - ('x', 'y', 'z')) - - def test_type_definition_order_overwritten_bad_item(self): - with self.assertRaises(TypeError): - class PoorlyOverwritten: - __definition_order__ = ('a', 2, 'c') - a = 1 - b = 2 - c = 3 - - def test_type_definition_order_overwritten_bad_type(self): - with self.assertRaises(TypeError): - class PoorlyOverwritten: - __definition_order__ = ['a', 2, 'c'] - a = 1 - b = 2 - c = 3 - - def test_type_definition_order_metaclass(self): - class Meta(type): - SPAM = 42 - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - - self.assertEqual(Meta.__definition_order__, - ('__module__', '__qualname__', - 'SPAM', '__init__')) - - def test_type_definition_order_OrderedDict(self): - class Meta(type): - def __prepare__(self, *args, **kwargs): - return OrderedDict() - - class WithODict(metaclass=Meta): - x='y' - - self.assertEqual(WithODict.__definition_order__, - ('__module__', '__qualname__', 'x')) - - class Meta(type): - def __prepare__(self, *args, **kwargs): - class ODictSub(OrderedDict): - pass - return ODictSub() - - class WithODictSub(metaclass=Meta): - x='y' - - self.assertEqual(WithODictSub.__definition_order__, - ('__module__', '__qualname__', 'x')) - - @cpython_only - def test_type_definition_order_cpython(self): - # some implementations will have an ordered-by-default dict. - - class Meta(type): - def __prepare__(self, *args, **kwargs): - return {} - - class NotOrdered(metaclass=Meta): - x='y' - - self.assertEqual(NotOrdered.__definition_order__, None) - def test_bad_args(self): with self.assertRaises(TypeError): type() -- cgit v1.2.1 From 660076761180bac82ff7467926a4b1d41a50945f Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 20 Jan 2017 08:33:06 +0200 Subject: Issue #29327: Fixed a crash when pass the iterable keyword argument to sorted(). --- Lib/test/test_builtin.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'Lib/test/test_builtin.py') diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index a792099f10..416316c028 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -1627,6 +1627,16 @@ class TestSorted(unittest.TestCase): self.assertEqual(data, sorted(copy, reverse=1)) self.assertNotEqual(data, copy) + def test_bad_arguments(self): + # Issue #29327: The first argument is positional-only. + sorted([]) + with self.assertRaises(TypeError): + sorted(iterable=[]) + # Other arguments are keyword-only + sorted([], key=None) + with self.assertRaises(TypeError): + sorted([], None) + def test_inputtypes(self): s = 'abracadabra' types = [list, tuple, str] -- cgit v1.2.1