@@ -34,11 +34,15 @@ def parse():
3434pass
3535elif f == '-' :
3636f = sys .stdin
37+ if sys .version_info [0 ]>= 3 :
38+ encoding = None
3739else :
3840try :
3941# 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 )
4246except IndexError :
4347sys .stderr .write ("No filename provided. Use -h for help\n " )
4448sys .exit (1 )
@@ -76,12 +80,16 @@ def parse():
7680t0 = time .time ()
7781document = run (parseMethod ,f ,encoding )
7882t1 = 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 ))
8289else :
8390document = run (parseMethod ,f ,encoding )
84- printOutput (p ,document ,opts )
91+ if document :
92+ printOutput (p ,document ,opts )
8593
8694def run (parseMethod ,f ,encoding ):
8795try :
@@ -119,7 +127,11 @@ def printOutput(parser, document, opts):
119127del kwargs ['quote_char' ]
120128
121129tokens = 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 ):
123135sys .stdout .write (text )
124136if not text .endswith ('\n ' ):sys .stdout .write ('\n ' )
125137if opts .error :