Skip to content

Commit ce36839

Browse files
committed
Refactor
1 parent 393e431 commit ce36839

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

Lib/test/test_pickletools.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -526,22 +526,22 @@ def setUp(self):
526526

527527
@staticmethod
528528
def text_normalize(string):
529-
"""Dedent *string* and strip it from its surrounding whitespaces.
530-
531-
This method is used by the other utility functions so that any
532-
string to write or to match against can be freely indented.
533-
"""
534529
return textwrap.dedent(string).strip()
535530

536531
def set_pickle_data(self, data):
537532
with open(self.filename, 'wb') as f:
538533
pickle.dump(data, f)
539534

540535
def invoke_pickletools(self, *flags):
541-
output = io.StringIO()
542-
with contextlib.redirect_stdout(output):
536+
stderr = io.StringIO()
537+
stdout = io.StringIO()
538+
with (
539+
contextlib.redirect_stdout(stdout),
540+
contextlib.redirect_stderr(stderr),
541+
):
543542
pickletools._main(args=[*flags, self.filename])
544-
return self.text_normalize(output.getvalue())
543+
self.assertEqual(stderr.getvalue(), '')
544+
return self.text_normalize(stdout.getvalue())
545545

546546
def check_output(self, data, expect, *flags):
547547
with self.subTest(data=data, flags=flags):
@@ -561,26 +561,28 @@ def test_invocation(self):
561561
('-a', '--annotate'),
562562
('-p="Another:"', '--preamble="Another:"'),
563563
]
564-
data = { "a", "b", "c" }
564+
data = { 'a', 'b', 'c' }
565565

566566
self.set_pickle_data(data)
567567

568568
for r in range(1, len(base_flags) + 1):
569569
for choices in itertools.combinations(base_flags, r=r):
570570
for args in itertools.product(*choices):
571571
with self.subTest(args=args[1:]):
572-
_ = self.invoke_pickletools(*args)
572+
self.invoke_pickletools(*args)
573573

574+
def test_unknown_flag(self):
574575
with self.assertRaises(SystemExit):
575-
# suppress argparse error message
576-
with contextlib.redirect_stderr(io.StringIO()):
577-
_ = self.invoke_pickletools('--unknown')
576+
output = io.StringIO()
577+
with contextlib.redirect_stderr(output):
578+
pickletools._main(args=['--unknown'])
579+
self.assertStartsWith(output.getvalue(), 'usage: ')
578580

579581
def test_output_flag(self):
580582
# test 'python -m pickletools -o/--output'
581583
output_file = tempfile.mktemp()
582584
self.addCleanup(os_helper.unlink, output_file)
583-
data = ("fake_data",)
585+
data = ('fake_data',)
584586
expect = '''
585587
0: \\x80 PROTO 5
586588
2: \\x95 FRAME 15
@@ -605,7 +607,7 @@ def test_output_flag(self):
605607

606608
def test_memo_flag(self):
607609
# test 'python -m pickletools -m/--memo'
608-
data = ("fake_data",)
610+
data = ('fake_data',)
609611
expect = '''
610612
0: \\x80 PROTO 5
611613
2: \\x95 FRAME 15
@@ -621,7 +623,7 @@ def test_memo_flag(self):
621623

622624
def test_indentlevel_flag(self):
623625
# test 'python -m pickletools -l/--indentlevel'
624-
data = ("fake_data",)
626+
data = ('fake_data',)
625627
expect = '''
626628
0: \\x80 PROTO 5
627629
2: \\x95 FRAME 15
@@ -637,7 +639,7 @@ def test_indentlevel_flag(self):
637639

638640
def test_annotate_flag(self):
639641
# test 'python -m pickletools -a/--annotate'
640-
data = ("fake_data",)
642+
data = ('fake_data',)
641643
expect = '''
642644
0: \\x80 PROTO 5 Protocol version indicator.
643645
2: \\x95 FRAME 15 Indicate the beginning of a new frame.
@@ -653,7 +655,7 @@ def test_annotate_flag(self):
653655

654656
def test_preamble_flag(self):
655657
# test 'python -m pickletools -p/--preamble'
656-
data = ("fake_data",)
658+
data = ('fake_data',)
657659
expect = '''
658660
Another:
659661
0: \\x80 PROTO 5

0 commit comments

Comments
 (0)