Skip to content
Open
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
147 changes: 146 additions & 1 deletion Lib/test/test_py3kwarn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
import sys
import contextlib
from test.test_support import check_py3k_warnings, CleanImport, run_unittest
import warnings
import base64
Expand Down Expand Up @@ -35,6 +36,24 @@ def reset_module_registry(module):

class TestPy3KWarnings(unittest.TestCase):

@contextlib.contextmanager
def check_py3k_warnings_with_fix(self):
frame = sys._getframe(2)
registry = frame.f_globals.get('__warningregistry__')
if registry:
registry.clear()
with warnings.catch_warnings(record=True) as w:
# PyErr_WarnExplicit_WithFix uses this runtime hook directly.
showwarningwithfix = warnings.showwarningwithfix
def record_warning_with_fix(*args, **kwargs):
w.append(warnings.WarningMessageWithFix(*args, **kwargs))
warnings.showwarningwithfix = record_warning_with_fix
warnings.simplefilter("always")
try:
yield test_support.WarningsRecorder(w)
finally:
warnings.showwarningwithfix = showwarningwithfix

def assertWarning(self, _, warning, expected_message):
self.assertEqual(str(warning.message), expected_message)

Expand Down Expand Up @@ -424,12 +443,138 @@ def test_b32encode_warns(self):
expected = "base64.b32encode returns str in Python 2 (bytes in 3.x)"
base64.b32encode(b'test')
check_py3k_warnings(expected, UserWarning)

def test_b16encode_warns(self):
expected = "base64.b16encode returns str in Python 2 (bytes in 3.x)"
base64.b16encode(b'test')
check_py3k_warnings(expected, UserWarning)

def assertMROWarning(self, recorder, expected_message):
self.assertEqual(len(recorder.warnings), 1)
msg = str(recorder.warnings[0].message)
self.assertEqual(msg, expected_message)
recorder.reset()

def test_classic_mro_resolution_change_warning(self):
with self.check_py3k_warnings_with_fix() as w:
class A:
def do_this(self):
return "A"

class B(A):
pass

class C(A):
def do_this(self):
return "C"

class D(B, C):
pass

self.assertEqual(D().do_this(), "A")
self.assertMROWarning(
w,
"classic multiple inheritance for class 'D' may resolve "
"attribute 'do_this' from 'A' in 2.x but from 'C' in 3.x "
"due to C3 MRO")

def test_classic_mro_single_inheritance_no_warning(self):
with self.check_py3k_warnings_with_fix() as w:
class A:
def only_here(self):
return "A"

class B(A):
pass

self.assertEqual(B().only_here(), "A")
self.assertEqual(len(w.warnings), 0)

def test_classic_mro_no_conflicting_name_no_warning(self):
with self.check_py3k_warnings_with_fix() as w:
class A:
pass

class B(A):
def left(self):
return "left"

class C(A):
def right(self):
return "right"

class D(B, C):
pass

self.assertEqual(D().left(), "left")
self.assertEqual(D().right(), "right")
self.assertEqual(len(w.warnings), 0)

def test_classic_mro_provider_unchanged_no_warning(self):
with self.check_py3k_warnings_with_fix() as w:
class A:
def only_here(self):
return "A"

class B(A):
pass

class C(A):
pass

class D(B, C):
pass

self.assertEqual(D().only_here(), "A")
self.assertEqual(len(w.warnings), 0)

def test_classic_mro_c3_conflict_warning(self):
with self.check_py3k_warnings_with_fix() as w:
class A:
pass

class B:
pass

class X(A, B):
pass

class Y(B, A):
pass

class Z(X, Y):
pass

self.assertMROWarning(
w,
"classic multiple inheritance hierarchy for class 'Z' has no "
"consistent C3 MRO and will fail in 3.x")

def test_classic_mro_bases_update_warning(self):
with self.check_py3k_warnings_with_fix() as w:
class A:
def do_this(self):
return "A"

class B(A):
pass

class C(A):
def do_this(self):
return "C"

class D(B):
pass

self.assertEqual(len(w.warnings), 0)
D.__bases__ = (B, C)
self.assertEqual(D().do_this(), "A")
self.assertMROWarning(
w,
"classic multiple inheritance for class 'D' may resolve "
"attribute 'do_this' from 'A' in 2.x but from 'C' in 3.x "
"due to C3 MRO")


class TestStdlibRemovals(unittest.TestCase):

Expand Down
Loading