1- import contextlib
21import io
32import itertools
43import pickle
@@ -533,11 +532,9 @@ def set_pickle_data(self, data):
533532 pickle .dump (data , f )
534533
535534 def invoke_pickletools (self , * flags ):
536- stderr = io .StringIO ()
537- stdout = io .StringIO ()
538535 with (
539- contextlib . redirect_stdout ( stdout ) ,
540- contextlib . redirect_stderr ( stderr ) ,
536+ support . captured_stdout () as stdout ,
537+ support . captured_stderr () as stderr ,
541538 ):
542539 pickletools ._main (args = [* flags , self .filename ])
543540 self .assertEqual (stderr .getvalue (), '' )
@@ -573,23 +570,22 @@ def test_invocation(self):
573570
574571 def test_unknown_flag (self ):
575572 with self .assertRaises (SystemExit ):
576- output = io .StringIO ()
577- with contextlib .redirect_stderr (output ):
573+ with support .captured_stderr () as stderr :
578574 pickletools ._main (args = ['--unknown' ])
579- self .assertStartsWith (output .getvalue (), 'usage: ' )
575+ self .assertStartsWith (stderr .getvalue (), 'usage: ' )
580576
581577 def test_output_flag (self ):
582578 # test 'python -m pickletools -o/--output'
583579 output_file = tempfile .mktemp ()
584580 self .addCleanup (os_helper .unlink , output_file )
585581 data = ('fake_data' ,)
586- expect = '''
587- 0: \\ x80 PROTO 5
588- 2: \\ x95 FRAME 15
589- 11: \\ x8c SHORT_BINUNICODE 'fake_data'
590- 22: \\ x94 MEMOIZE (as 0)
591- 23: \\ x85 TUPLE1
592- 24: \\ x94 MEMOIZE (as 1)
582+ expect = r '''
583+ 0: \x80 PROTO 5
584+ 2: \x95 FRAME 15
585+ 11: \x8c SHORT_BINUNICODE 'fake_data'
586+ 22: \x94 MEMOIZE (as 0)
587+ 23: \x85 TUPLE1
588+ 24: \x94 MEMOIZE (as 1)
593589 25: . STOP
594590 highest protocol among opcodes = 4
595591 '''
@@ -608,13 +604,13 @@ def test_output_flag(self):
608604 def test_memo_flag (self ):
609605 # test 'python -m pickletools -m/--memo'
610606 data = ('fake_data' ,)
611- expect = '''
612- 0: \\ x80 PROTO 5
613- 2: \\ x95 FRAME 15
614- 11: \\ x8c SHORT_BINUNICODE 'fake_data'
615- 22: \\ x94 MEMOIZE (as 0)
616- 23: \\ x85 TUPLE1
617- 24: \\ x94 MEMOIZE (as 1)
607+ expect = r '''
608+ 0: \x80 PROTO 5
609+ 2: \x95 FRAME 15
610+ 11: \x8c SHORT_BINUNICODE 'fake_data'
611+ 22: \x94 MEMOIZE (as 0)
612+ 23: \x85 TUPLE1
613+ 24: \x94 MEMOIZE (as 1)
618614 25: . STOP
619615 highest protocol among opcodes = 4
620616 '''
@@ -624,13 +620,13 @@ def test_memo_flag(self):
624620 def test_indentlevel_flag (self ):
625621 # test 'python -m pickletools -l/--indentlevel'
626622 data = ('fake_data' ,)
627- expect = '''
628- 0: \\ x80 PROTO 5
629- 2: \\ x95 FRAME 15
630- 11: \\ x8c SHORT_BINUNICODE 'fake_data'
631- 22: \\ x94 MEMOIZE (as 0)
632- 23: \\ x85 TUPLE1
633- 24: \\ x94 MEMOIZE (as 1)
623+ expect = r '''
624+ 0: \x80 PROTO 5
625+ 2: \x95 FRAME 15
626+ 11: \x8c SHORT_BINUNICODE 'fake_data'
627+ 22: \x94 MEMOIZE (as 0)
628+ 23: \x85 TUPLE1
629+ 24: \x94 MEMOIZE (as 1)
634630 25: . STOP
635631 highest protocol among opcodes = 4
636632 '''
@@ -640,13 +636,13 @@ def test_indentlevel_flag(self):
640636 def test_annotate_flag (self ):
641637 # test 'python -m pickletools -a/--annotate'
642638 data = ('fake_data' ,)
643- expect = '''
644- 0: \\ x80 PROTO 5 Protocol version indicator.
645- 2: \\ x95 FRAME 15 Indicate the beginning of a new frame.
646- 11: \\ x8c SHORT_BINUNICODE 'fake_data' Push a Python Unicode string object.
647- 22: \\ x94 MEMOIZE (as 0) Store the stack top into the memo. The stack is not popped.
648- 23: \\ x85 TUPLE1 Build a one-tuple out of the topmost item on the stack.
649- 24: \\ x94 MEMOIZE (as 1) Store the stack top into the memo. The stack is not popped.
639+ expect = r '''
640+ 0: \x80 PROTO 5 Protocol version indicator.
641+ 2: \x95 FRAME 15 Indicate the beginning of a new frame.
642+ 11: \x8c SHORT_BINUNICODE 'fake_data' Push a Python Unicode string object.
643+ 22: \x94 MEMOIZE (as 0) Store the stack top into the memo. The stack is not popped.
644+ 23: \x85 TUPLE1 Build a one-tuple out of the topmost item on the stack.
645+ 24: \x94 MEMOIZE (as 1) Store the stack top into the memo. The stack is not popped.
650646 25: . STOP Stop the unpickling machine.
651647 highest protocol among opcodes = 4
652648 '''
@@ -656,33 +652,32 @@ def test_annotate_flag(self):
656652 def test_preamble_flag (self ):
657653 # test 'python -m pickletools -p/--preamble'
658654 data = ('fake_data' ,)
659- expect = '''
655+ expect = r '''
660656 Another:
661- 0: \\ x80 PROTO 5
662- 2: \\ x95 FRAME 15
663- 11: \\ x8c SHORT_BINUNICODE 'fake_data'
664- 22: \\ x94 MEMOIZE (as 0)
665- 23: \\ x85 TUPLE1
666- 24: \\ x94 MEMOIZE (as 1)
657+ 0: \x80 PROTO 5
658+ 2: \x95 FRAME 15
659+ 11: \x8c SHORT_BINUNICODE 'fake_data'
660+ 22: \x94 MEMOIZE (as 0)
661+ 23: \x85 TUPLE1
662+ 24: \x94 MEMOIZE (as 1)
667663 25: . STOP
668664 highest protocol among opcodes = 4
669665 Another:
670- 0: \\ x80 PROTO 5
671- 2: \\ x95 FRAME 15
672- 11: \\ x8c SHORT_BINUNICODE 'fake_data'
673- 22: \\ x94 MEMOIZE (as 0)
674- 23: \\ x85 TUPLE1
675- 24: \\ x94 MEMOIZE (as 1)
666+ 0: \x80 PROTO 5
667+ 2: \x95 FRAME 15
668+ 11: \x8c SHORT_BINUNICODE 'fake_data'
669+ 22: \x94 MEMOIZE (as 0)
670+ 23: \x85 TUPLE1
671+ 24: \x94 MEMOIZE (as 1)
676672 25: . STOP
677673 highest protocol among opcodes = 4
678674 '''
679675 for flag in ['-p=Another:' , '--preamble=Another:' ]:
680676 with self .subTest (data = data , flags = flag ):
681677 self .set_pickle_data (data )
682- output = io .StringIO ()
683- with contextlib .redirect_stdout (output ):
678+ with support .captured_stdout () as stdout :
684679 pickletools ._main (args = [flag , self .filename , self .filename ])
685- res = self .text_normalize (output .getvalue ())
680+ res = self .text_normalize (stdout .getvalue ())
686681 expect = self .text_normalize (expect )
687682 self .assertListEqual (res .splitlines (), expect .splitlines ())
688683
0 commit comments