@@ -34,11 +34,15 @@ def parse():
34
34
pass
35
35
elif f == '-' :
36
36
f = sys .stdin
37
+ if sys .version_info [0 ] >= 3 :
38
+ encoding = None
37
39
else :
38
40
try :
39
41
# Try opening from file system
40
- f = open (f )
41
- except IOError : pass
42
+ f = open (f , "rb" )
43
+ except IOError as e :
44
+ sys .stderr .write ("Unable to open file: %s\n " % e )
45
+ sys .exit (1 )
42
46
except IndexError :
43
47
sys .stderr .write ("No filename provided. Use -h for help\n " )
44
48
sys .exit (1 )
@@ -76,12 +80,16 @@ def parse():
76
80
t0 = time .time ()
77
81
document = run (parseMethod , f , encoding )
78
82
t1 = time .time ()
79
- printOutput (p , document , opts )
80
- t2 = time .time ()
81
- sys .stderr .write ("\n \n Run took: %fs (plus %fs to print the output)" % (t1 - t0 , t2 - t1 ))
83
+ if document :
84
+ printOutput (p , document , opts )
85
+ t2 = time .time ()
86
+ sys .stderr .write ("\n \n Run took: %fs (plus %fs to print the output)" % (t1 - t0 , t2 - t1 ))
87
+ else :
88
+ sys .stderr .write ("\n \n Run took: %fs" % (t1 - t0 ))
82
89
else :
83
90
document = run (parseMethod , f , encoding )
84
- printOutput (p , document , opts )
91
+ if document :
92
+ printOutput (p , document , opts )
85
93
86
94
def run (parseMethod , f , encoding ):
87
95
try :
@@ -119,7 +127,11 @@ def printOutput(parser, document, opts):
119
127
del kwargs ['quote_char' ]
120
128
121
129
tokens = treewalkers .getTreeWalker (opts .treebuilder )(document )
122
- for text in serializer .HTMLSerializer (** kwargs ).serialize (tokens , encoding = 'utf-8' ):
130
+ if sys .version_info [0 ] >= 3 :
131
+ encoding = None
132
+ else :
133
+ encoding = "utf-8"
134
+ for text in serializer .HTMLSerializer (** kwargs ).serialize (tokens , encoding = encoding ):
123
135
sys .stdout .write (text )
124
136
if not text .endswith ('\n ' ): sys .stdout .write ('\n ' )
125
137
if opts .error :
0 commit comments