tomerfiliba / plumbum

Plumbum: Shell Combinators
https://plumbum.readthedocs.io
MIT License
2.82k stars 184 forks source link

Cannot use cwd with unicode path #439

Open ghost opened 5 years ago

ghost commented 5 years ago

The change directory function fails with a Unicode path in Python 2.7

from plumbum import local

folder = u'th\xe9'
if not os.path.exists(folder):
    os.makedirs(folder)
#os.chdir(folder)    # This works

with local.cwd(folder):  # Not this
    pass
Traceback (most recent call last):
  File "C:\Python27\lib\contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "C:\Python27\lib\site-packages\plumbum\path\local.py", line 369, in __call__
    newdir = self.chdir(newdir)
  File "C:\Python27\lib\site-packages\plumbum\path\local.py", line 354, in chdir
    os.chdir(str(newdir))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 2: ordinal not in range(128)
henryiii commented 5 years ago

It's becoming a really bad idea to use Python 2.7 on Windows. The fact it's stuck with MSVC 2008 means that lots of things are beginning to fail - you can't use the current version of SSL required and Qt, for example. Microsoft extended the life of the MSVC 2008 SDK just so that Python 2.7 could live a little longer, but it's very, very dated now. Also, Python 3's Unicode by default is perfect for Windows, because it has unicode paths and shell, as you see. See here, even Anaconda is considering dropping 2.7 updates on Windows due to issues.

Anyway, can you check and see if os.chdir(u'th\xe9') works? If it doesn't, there isn't much hope for fixing it in Plumbum. If it does, it should be a pretty easy fix. Edit: Sorry, I can't read comments. It should be fixable.

henryiii commented 5 years ago

Please edit line 354 in C:\Python27\lib\site-packages\plumbum\path\local.py to remove the str() wrapping the newdir and let me know if it works. You might also try unicode() instead of str(). Let me know the results please!

ghost commented 5 years ago

Thanks for the quick reply! Yes I know we should get away from it, that's the plan, but lots of legacy code... I tried with not str() and with unicode() and they both work 👍 I have used a work around with os.cwd() for now.