@@ -2848,10 +2848,10 @@ def _test():
2848
2848
parser = argparse .ArgumentParser (
2849
2849
description = 'disassemble one or more pickle files' )
2850
2850
parser .add_argument (
2851
- 'pickle_file' , type = argparse . FileType ( 'br' ),
2851
+ 'pickle_file' ,
2852
2852
nargs = '*' , help = 'the pickle file' )
2853
2853
parser .add_argument (
2854
- '-o' , '--output' , default = sys . stdout , type = argparse . FileType ( 'w' ),
2854
+ '-o' , '--output' ,
2855
2855
help = 'the file where the output should be written' )
2856
2856
parser .add_argument (
2857
2857
'-m' , '--memo' , action = 'store_true' ,
@@ -2876,15 +2876,26 @@ def _test():
2876
2876
if args .test :
2877
2877
_test ()
2878
2878
else :
2879
- annotate = 30 if args .annotate else 0
2880
2879
if not args .pickle_file :
2881
2880
parser .print_help ()
2882
- elif len (args .pickle_file ) == 1 :
2883
- dis (args .pickle_file [0 ], args .output , None ,
2884
- args .indentlevel , annotate )
2885
2881
else :
2882
+ annotate = 30 if args .annotate else 0
2886
2883
memo = {} if args .memo else None
2887
- for f in args .pickle_file :
2888
- preamble = args .preamble .format (name = f .name )
2889
- args .output .write (preamble + '\n ' )
2890
- dis (f , args .output , memo , args .indentlevel , annotate )
2884
+ if args .output is None :
2885
+ output = sys .stdout
2886
+ else :
2887
+ output = open (args .output , 'w' )
2888
+ try :
2889
+ for arg in args .pickle_file :
2890
+ if len (args .pickle_file ) > 1 :
2891
+ name = '<stdin>' if arg == '-' else arg
2892
+ preamble = args .preamble .format (name = name )
2893
+ output .write (preamble + '\n ' )
2894
+ if arg == '-' :
2895
+ dis (sys .stdin .buffer , output , memo , args .indentlevel , annotate )
2896
+ else :
2897
+ with open (arg , 'rb' ) as f :
2898
+ dis (f , output , memo , args .indentlevel , annotate )
2899
+ finally :
2900
+ if output is not sys .stdout :
2901
+ output .close ()
0 commit comments