pypa/virtualenv — pull request #219 by dhellmann (2012-01-29 15:37:25+00:00)
Title: Make fixup_scripts() unicode-aware so it works under Python 3.

I ran into a problem with virtualenv under Python 3.2.2 while testing virtualenvwrapper support for Python 3. The --relocatable flag was causing a traceback because the script file contents were not being decoded before string operations were applied:

Traceback (most recent call last):
File "/Users/dhellmann/Devel/virtualenvwrapper/dakra/.tox/py32/bin/virtualenv", line 9, in
load_entry_point('virtualenv==1.7', 'console_scripts', 'virtualenv')()
File "/Users/dhellmann/Devel/virtualenvwrapper/dakra/.tox/py32/lib/python3.2/site-packages/virtualenv.py", line 914, in main
make_environment_relocatable(home_dir)
File "/Users/dhellmann/Devel/virtualenvwrapper/dakra/.tox/py32/lib/python3.2/site-packages/virtualenv.py", line 1484, in make_environment_relocatable
fixup_scripts(home_dir)
File "/Users/dhellmann/Devel/virtualenvwrapper/dakra/.tox/py32/lib/python3.2/site-packages/virtualenv.py", line 1514, in fixup_scripts
if not lines[0].strip().startswith(shebang):
TypeError: startswith first arg must be bytes or a tuple of bytes, not str

This changeset fixes the problem by decoding the bytes being read from script files before checking the contents of the first line and reencoding them before writing any changes. Files that cannot be decoded are skipped entirely under the assumption that they are binary files. An alternate solution would be to pass bytes to startswith(), but that wasn't backwards-compatible.