[issue16877] Odd behavior of ~ in os.path.abspath and os.path.realpath

2013-01-06 Thread Ian Shields

Ian Shields added the comment:

Regarding last comment. I had missed the comment in documentation fo 
os.path.join Join one or more path components intelligently. If any component 
is an absolute path, all previous components (on Windows, including the 
previous drive letter, if there was one) are thrown away, and joining 
continues. So the issue is really the behavior of os.path.join where the 
intelligence in the joining does not recognize that ~ is usually expanded to 
an absolute path. Consider the following Bash commands:
[ian@attic4 testpath]$ pwd
/home/ian/testpath
[ian@attic4 testpath]$ echo $(cd ~/testpath/..;pwd)
/home/ian
[ian@attic4 testpath]$ cd /home/ian/~
bash: cd: /home/ian/~: No such file or directory

Now consider some Python
 os.getcwd()
'/home/ian/testpath'
 os.path.join(os.getcwd(), /home/ian)
'/home/ian'
 os.path.expanduser(~)
'/home/ian'
 os.path.join(os.getcwd(), ~)
'/home/ian/testpath/~'
 os.path.expanduser(os.path.abspath(~))
'/home/ian/testpath/~'
 os.path.abspath(os.path.expanduser(~))
'/home/ian'

I find the Python behavior rather odd. I cna live with it now I know about it, 
but if it is really intentional it would help to document this rather odd 
behavior somewhat better.

--
status: pending - open

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue16877
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue16877] Odd behavior of ~ in os.path.abspath and os.path.realpath

2013-01-06 Thread Ian Shields

Ian Shields added the comment:

Oddity may be in the eye of the beholder. I've been programming and scripting 
for about 40 years, including several *IX shells and many other systems. I'm 
relatively new to Python. Mostly the results of doing things in Python are what 
I expect. Not doing expansion of a leading tilde when I ask for an absolute 
path is not what I expect. So to me it's odd. Or different. Or just not what I 
expect. Substitute unexpected for odd if you like. Sure, tilde expansion 
wasn't part of the Bourne shell, but it's been in POSIX shells for about the 
same amount of time that Python has been around, so it's odd to me that Python 
differs in this way. It's not hard to work around now that I know about it.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue16877
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue16877] Odd behavior of ~ in os.path.abspath and os.path.realpath

2013-01-06 Thread Ian Shields

Ian Shields added the comment:

David, Tilde expansion is different to globbing. Globbing in Python doesn't 
automatically do tilde expansion either.
 glob.glob(~)
[]

Looking at the documentation, I don't think it would be practical to add 
documentation to each affected function - there are too many. Apart from 
abspath and realpath (which started this conversation), there are join, isdir, 
isfile, isabs, and many others in os.path. posixpath and the other path 
handling modules.

Maybe a note at the top of the module documentation (e.g. 
http://docs.python.org/3/library/os.path.html) which has two notes already. 
Something along the line of:

Unlike many shells, Python does not automatically do tilde expansion or 
variable substitution in paths. Use expanduser for tilde expansion or 
expandvars for variable substitution.

Or just simply
Use expanduser for tilde expansion or expandvars for variable substitution in 
paths as the other path functions do not do this automatically.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue16877
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue16877] Odd behavior of ~ in os.path.abspath and os.path.realpath

2013-01-06 Thread Ian Shields

Ian Shields added the comment:

I think that's an excellent resolution to the problem. Thank you.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue16877
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue16877] Odd behavior of ~ in os.path.abspath and os.path.realpath

2013-01-05 Thread Ian Shields

New submission from Ian Shields:

Filespecs that start with ~ are not properly handled by os.path.realpath or 
os.path.abspath (and maybe other functions). Following console output from 
Fedora 17 using Puthon 3.2 illustrates the issue. Similar issue in 2.7
[ian@attic4 developerworks]$ cd ..
[ian@attic4 ~]$ mkdir testpath
[ian@attic4 ~]$ cd testpath
[ian@attic4 testpath]$ pwd
/home/ian/testpath
[ian@attic4 testpath]$ python3
Python 3.2.3 (default, Jun  8 2012, 05:36:09) 
[GCC 4.7.0 20120507 (Red Hat 4.7.0-5)] on linux2
Type help, copyright, credits or license for more information.
 import os
 os.path.abspath(~)
'/home/ian/testpath/~'
 os.path.realpath(~/xxx/zzz)
'/home/ian/testpath/~/xxx/zzz'
 os.path.abspath(~/..)
'/home/ian/testpath'

Function should probably use expanduser to determine if path is already 
absolute. Documentation at http://docs.python.org/3/library/os.path.html is 
also misleading as this is not how these functions work if given an absolute 
path to start with.

--
components: None
messages: 179170
nosy: ibshields
priority: normal
severity: normal
status: open
title: Odd behavior of ~ in os.path.abspath and os.path.realpath
type: behavior
versions: Python 3.2

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue16877
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com