-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
os.path.abspath with unicode argument should call os.getcwdu #47676
Comments
If current working directory contains non-ascii characters, calling >>> import os
>>> os.path.abspath(u".")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File
"/home/packages/python-2.5.1/x86-linux/lib/python2.5/posixpath.py", line
403, in abspath
path = join(os.getcwd(), path)
File
"/home/packages/python-2.5.1/x86-linux/lib/python2.5/posixpath.py", line
65, in join
path += '/' + b
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 29:
ordinal not in range(128) It works if I do it manually, using os.getcwdu(): >>> os.path.join(os.getcwdu(), u".")
u'/disk1/chn_local/work/test/sk\xe4rg\xe5rds\xf6-latin1/.' |
Well, os.path.supports_unicode_filenames is False on posix platforms. I was about to close the issue, but python 3.0 also defines |
This seems to work fine with Py 3.0 and 3.1 on Linux, it still fails |
This also caused the failure in bpo-7669. |
Here is a patch that uses os.getcwdu() instead of os.getcwd() when the arg of abspath is unicode (with tests). It also include an helper context manager in test_support used to create temp dirs and set them as cwd (this will be committed separately) and two helper methods (assertUnicode and assertStr) that will probably be useful when I (or someone else) will add more tests with unicode strings for the other functions. |
You could use assertIsInstance(s, unicode, '%r is not unicode' % s) in the tests instead of your assertTrue. I think the rest of it looks good. Works for me. |
I added the fix on ntpath and more tests. I also refactored the tests I added for posixpath. |
assertStr and assertUnicode don't exist in test_ntpath so the tests fail on Windows. I copied/pasted the functions over from test_posixpath just to see and test_ntpath passes. Other than that, it looks good to me. |
I'll fix the patch. I added Ronald and Andrew to see if they have any opinion about macpath and os2emxpath. The main idea is that abspath should always return unicode if its arg is unicode or str otherwise. |
abspath is basically dead code in macpath, the module is used to manipulate classic MacOS9-style paths and is no longer used as os.path on any supported platform (it used to be os.path on MacOS9). BTW. the module itself is not dead, OSX still uses OS9-style paths in a number of older APIs. |
For consistency I updated all 4 the modules. If the tests pass on both Windows and Mac the patch should be ready to go in. |
Passes on Windows, Mac, and Linux. |
Small note:
|
The patch intentionally doesn't use assertIsInstance because that method doesn't exist in 2.6. |
The attached patch proposes a decorator which can be used to strengthen any test which is affected by the CWD. (and fixes for test_(mac|nt|posix)path too) |
Fixed in r78247 (trunk) and r78248 (release26-maint) (plus a fix in r78272 and r78279 to avoid test failures when the filesystem encoding is ascii). |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: