Skip to content

Commit ff8fc2a

Browse files
committed
Add more tests
1 parent 6289b78 commit ff8fc2a

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

Lib/test/test_exceptions.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,29 @@ def test_attributes(self):
13141314
self.assertEqual(exc.name, 'carry')
13151315
self.assertIs(exc.obj, sentinel)
13161316

1317+
def test_getattr_has_name_and_obj(self):
1318+
class A:
1319+
blech = None
1320+
1321+
obj = A()
1322+
try:
1323+
obj.bluch
1324+
except AttributeError as exc:
1325+
self.assertEqual("bluch", exc.name)
1326+
self.assertEqual(obj, exc.obj)
1327+
1328+
def test_getattr_has_name_and_obj_for_method(self):
1329+
class A:
1330+
def blech(self):
1331+
return
1332+
1333+
obj = A()
1334+
try:
1335+
obj.bluch()
1336+
except AttributeError as exc:
1337+
self.assertEqual("bluch", exc.name)
1338+
self.assertEqual(obj, exc.obj)
1339+
13171340
def test_getattr_suggestions(self):
13181341
class A:
13191342
blech = None
@@ -1324,7 +1347,7 @@ class A:
13241347
with support.captured_stderr() as err:
13251348
sys.__excepthook__(*sys.exc_info())
13261349

1327-
# self.assertIn("blech", err.getvalue())
1350+
self.assertIn("blech", err.getvalue())
13281351

13291352
def test_getattr_suggestions_do_not_trigger_for_long_attributes(self):
13301353
class A:
@@ -1391,13 +1414,24 @@ class A:
13911414
def __getattr__(self, attr):
13921415
raise AttributeError(NonStringifyClass())
13931416

1394-
try:
1395-
A().bluch
1396-
except AttributeError as exc:
1397-
with support.captured_stderr() as err:
1398-
sys.__excepthook__(*sys.exc_info())
1417+
class B:
1418+
blech = None
1419+
def __getattr__(self, attr):
1420+
raise AttributeError("Error", 23)
13991421

1400-
self.assertIn("blech", err.getvalue())
1422+
class C:
1423+
blech = None
1424+
def __getattr__(self, attr):
1425+
raise AttributeError(23)
1426+
1427+
for cls in [A, B, C]:
1428+
try:
1429+
cls().bluch
1430+
except AttributeError as exc:
1431+
with support.captured_stderr() as err:
1432+
sys.__excepthook__(*sys.exc_info())
1433+
1434+
self.assertNotIn("blech", err.getvalue())
14011435

14021436

14031437
class ImportErrorTests(unittest.TestCase):

0 commit comments

Comments
 (0)