1- #! /usr/bin/env python
1+ #!/usr/bin/env python3
22
33# Change the #! line occurring in Python scripts. The new interpreter
44# pathname must be given with a -i option.
@@ -43,8 +43,9 @@ def main():
4343 sys .exit (2 )
4444 for o , a in opts :
4545 if o == '-i' :
46- new_interpreter = a
47- if not new_interpreter or new_interpreter [0 ] != '/' or not args :
46+ new_interpreter = a .encode ()
47+ if not new_interpreter or not new_interpreter .startswith (b'/' ) or \
48+ not args :
4849 err ('-i option or file-or-directory missing\n ' )
4950 err (usage )
5051 sys .exit (2 )
@@ -61,7 +62,7 @@ def main():
6162
6263ispythonprog = re .compile ('^[a-zA-Z0-9_]+\.py$' )
6364def ispython (name ):
64- return ispythonprog .match (name ) >= 0
65+ return bool ( ispythonprog .match (name ))
6566
6667def recursedown (dirname ):
6768 dbg ('recursedown(%r)\n ' % (dirname ,))
@@ -88,7 +89,7 @@ def recursedown(dirname):
8889def fix (filename ):
8990## dbg('fix(%r)\n' % (filename,))
9091 try :
91- f = open (filename , 'r ' )
92+ f = open (filename , 'rb ' )
9293 except IOError as msg :
9394 err ('%s: cannot open: %r\n ' % (filename , msg ))
9495 return 1
@@ -101,7 +102,7 @@ def fix(filename):
101102 head , tail = os .path .split (filename )
102103 tempname = os .path .join (head , '@' + tail )
103104 try :
104- g = open (tempname , 'w ' )
105+ g = open (tempname , 'wb ' )
105106 except IOError as msg :
106107 f .close ()
107108 err ('%s: cannot create: %r\n ' % (tempname , msg ))
@@ -139,11 +140,11 @@ def fix(filename):
139140 return 0
140141
141142def fixline (line ):
142- if not line .startswith ('#!' ):
143+ if not line .startswith (b '#!' ):
143144 return line
144- if "python" not in line :
145+ if b "python" not in line :
145146 return line
146- return '#! %s \n ' % new_interpreter
147+ return b '#! ' + new_interpreter + b' \n '
147148
148149if __name__ == '__main__' :
149150 main ()
0 commit comments