You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In 2.x, os.getcwd() uses a dynamic allocation scheme to accomodate whatever buffer size the current path needs to be represented.
In 3.x, the max path length is hardcoded to 1026 bytes or characters, and an error is raised if the current path length is larger than that. Even on systems where MAX_PATH is 1024 (a common value), it is still valid to create paths larger than that (using e.g. os.mkdir()).
The attached script shows that os.getcwd() works with a 1032-character path in 2.x, but fails in 3.x.
Even on systems where MAX_PATH is 1024 (a common value), it is still
valid to create paths larger than that (using e.g. os.mkdir()).
It seems I am mistaken on that. MAX_PATH is actually 4096 on the Linux system I am testing on. Calling getcwd() in a path longer than that fails with ENAMETOOLONG.
Still, 1026 shouldn't be the hard coded max length.
skrah commented on Jul 13, 2010
skrahmannequin
Mannequin
More actions
Just as a reminder: In 2.x, posix_getcwdu() also uses a buffer of size
1026.
Just as a reminder: In 2.x, posix_getcwdu() also uses a buffer of size
1026.
I suppose the implementation was simply copied into py3k, then.
Still, it's not a very good idea and it will also be a regression when
porting scripts from 2.x to 3.x.
skrah commented on Jul 20, 2010
skrahmannequin
Mannequin
More actions
Closed bpo-6817 as a duplicate of this one. There are some patches in
that issue.
On WinXP, 3.1, I get
...
mkdir: 242
Traceback (most recent call last):
File "C:\Programs\Python31\misc\t1.py", line 14, in <module>
os.mkdir(s)
WindowsError: [Error 206] The filename or extension is too long: 'C:\\Programs\\Python31\\misc\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab'
skrah commented on Jul 23, 2010
skrahmannequin
Mannequin
More actions
Terry J. Reedy <report@bugs.python.org> wrote:
> mkdir: 242
> Traceback (most recent call last):
> File "C:\Programs\Python31\misc\t1.py", line 14, in <module>
> os.mkdir(s)
> WindowsError: [Error 206] The filename or extension is too long: 'C:\\Programs\\Python31\\misc\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab\\aaaaab'
Patch based on Python 2 source code, but raises a MemoryError (instead of an OSError) on memory allocation failure.
With my patch, bigpath.py ends with "cwd: 1028 ...aab/aaaaab" with Python Python 3.2. Same result with Python 2.6. 1028 is bigger than 1026 (previous hardcoded max length in bytes including nul byte).
I'm not sure that PyMem_Realloc(NULL, size) is always equivalent to PyMem_Malloc(size). And I don't really know why I'm using PyMem_* instead of malloc() / free() :-) I suppose that Python has a faster memory allocator, or that it has better checks when compiled with pydebug?
Antoine asked me why not using a buffer of MAX_PATH+1 (instead of a dynamic buffer size). I don't know, I just copied/pasted the code from Python2. Extract of getcwd() manpage:
Note that on some systems, PATH_MAX may not be a compile-time
constant; furthermore, its value may depend on the file system,
see pathconf(3).
It's maybe to support strange OS like Hurd :-) (Hurd has no hardcoded limits).
Most of the time, the first realloc() should be enough.
skrah commented on Jul 28, 2010
skrahmannequin
Mannequin
More actions
For 2.x, unlimited path lengths were apparently introduced in bpo-2722.
This strategy does not work on Solaris and OpenBSD (bpo-9185).
FreeBSD also seems to support arbitrarily long paths. I would be somewhat
surprised though if anyone used them in practice. APUE (second edition)
uses PATH_MAX if it's available in limits.h.
Simpler patch replacing 1026 constant by MAXPATHLEN. On my Linux box, MAXPATHLEN is 4096 and os.pathconf('/', 'PC_PATH_MAX') returns 4096. I am able to get a path of 4095 bytes using the patch.
bigpath2.py: script to check the maximum path length of os.getcwd().
loewis commented on Jun 17, 2011
loewismannequin
Mannequin
More actions
I can adapt os_getcwd_buffer-2.patch to support Solaris/OpenBSD, but
do we need a dynamic buffer? (do we need to support OS without
PATH_MAX)
From a practicality point of view, we need to make no change at all:
nobody sane ever has a current working directory path of more than
1000 characters. Even if people have very long path names, they
don't make them the current working directory.
So if anything is changed, it's for purity only. Then, for purity,
we should get it right and support any path that the operating system
supports.
From a practicality point of view, we need to make no change at all:
nobody sane ever has a current working directory path of more than
1000 characters. Even if people have very long path names, they
don't make them the current working directory.
I don't see why that wouldn't be the case. They probably don't change to these directories *by hand*, but they can have scripts that cd into such a directory before doing stuff inside it. And these scripts can be written in Python.
# Just returning nothing instead of the SkipTest exception,
# because the test results in Error in that case.
# Is that ok?
# raise unittest.SkipTest("cannot create directory for testing")
return
Revisiting this, I've updated python3 to calculate this and use gradual dynamic allocation like the python2 implementation.
worr commented on Apr 21, 2015
worrmannequin
Mannequin
More actions
I've incorporated some of the feedback from the reviews into this new patch. I used the PyMem_Raw* functions to do allocation to avoid having to acquire the GIL and also avoid complciations from the builtin memory allocator, since I'm not using python objects.
I have also fixed a memory leak in my original patch, as well as a case where OSes with a small MAX_PATH fail with ENAMETOOLONG
worr commented on Apr 24, 2015
worrmannequin
Mannequin
More actions
I've updated the patch with the comments from the review
I've updated the patch with the comments from the review
Thanks William for your contribution, I commited your fix.
I just made a minor change on "if (cwd && use_bytes) {": you forgot to remove test now useless test on cwd, and I dropped { and } to make to short more readable (Note: the PEP-7 requires to put "}" and "else {" on two lines).
Ok, I just took 5 years to get Python 2 features in Python 3 :-)
Activity
pitrou commentedon Jul 13, 2010
In 2.x, os.getcwd() uses a dynamic allocation scheme to accomodate whatever buffer size the current path needs to be represented.
In 3.x, the max path length is hardcoded to 1026 bytes or characters, and an error is raised if the current path length is larger than that. Even on systems where MAX_PATH is 1024 (a common value), it is still valid to create paths larger than that (using e.g. os.mkdir()).
The attached script shows that os.getcwd() works with a 1032-character path in 2.x, but fails in 3.x.
pitrou commentedon Jul 13, 2010
It seems I am mistaken on that. MAX_PATH is actually 4096 on the Linux system I am testing on. Calling getcwd() in a path longer than that fails with ENAMETOOLONG.
Still, 1026 shouldn't be the hard coded max length.
skrah commentedon Jul 13, 2010
Just as a reminder: In 2.x, posix_getcwdu() also uses a buffer of size
1026.
pitrou commentedon Jul 13, 2010
I suppose the implementation was simply copied into py3k, then.
Still, it's not a very good idea and it will also be a regression when
porting scripts from 2.x to 3.x.
skrah commentedon Jul 20, 2010
Closed bpo-6817 as a duplicate of this one. There are some patches in
that issue.
terryjreedy commentedon Jul 23, 2010
skrah commentedon Jul 23, 2010
On Windows MAX_PATH seems to be 260 characters:
http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx
vstinner commentedon Jul 24, 2010
Patch based on Python 2 source code, but raises a MemoryError (instead of an OSError) on memory allocation failure.
With my patch, bigpath.py ends with "cwd: 1028 ...aab/aaaaab" with Python Python 3.2. Same result with Python 2.6. 1028 is bigger than 1026 (previous hardcoded max length in bytes including nul byte).
vstinner commentedon Jul 24, 2010
I'm not sure that PyMem_Realloc(NULL, size) is always equivalent to PyMem_Malloc(size). And I don't really know why I'm using PyMem_* instead of malloc() / free() :-) I suppose that Python has a faster memory allocator, or that it has better checks when compiled with pydebug?
pitrou commentedon Jul 27, 2010
It's not ok to call PyMem_* functions when the GIL is released. You should only release the GIL around the call to the system getcwd().
In this case it doesn't really make a difference, since all allocations larger than 256 bytes are delegated to the system allocator.
vstinner commentedon Jul 28, 2010
New version of the patch avoiding PyMem_*() functions to avoid a possible race condition (issue with the GIL).
vstinner commentedon Jul 28, 2010
Antoine asked me why not using a buffer of MAX_PATH+1 (instead of a dynamic buffer size). I don't know, I just copied/pasted the code from Python2. Extract of getcwd() manpage:
Note that on some systems, PATH_MAX may not be a compile-time
constant; furthermore, its value may depend on the file system,
see pathconf(3).
It's maybe to support strange OS like Hurd :-) (Hurd has no hardcoded limits).
Most of the time, the first realloc() should be enough.
skrah commentedon Jul 28, 2010
For 2.x, unlimited path lengths were apparently introduced in bpo-2722.
This strategy does not work on Solaris and OpenBSD (bpo-9185).
FreeBSD also seems to support arbitrarily long paths. I would be somewhat
surprised though if anyone used them in practice. APUE (second edition)
uses PATH_MAX if it's available in limits.h.
vstinner commentedon Jun 17, 2011
Simpler patch replacing 1026 constant by MAXPATHLEN. On my Linux box, MAXPATHLEN is 4096 and os.pathconf('/', 'PC_PATH_MAX') returns 4096. I am able to get a path of 4095 bytes using the patch.
vstinner commentedon Jun 17, 2011
You may use get_current_dir_name() which allocates the memory for us.
I can adapt os_getcwd_buffer-2.patch to support Solaris/OpenBSD, but do we need a dynamic buffer? (do we need to support OS without PATH_MAX)
vstinner commentedon Jun 17, 2011
bigpath2.py: script to check the maximum path length of os.getcwd().
loewis commentedon Jun 17, 2011
From a practicality point of view, we need to make no change at all:
nobody sane ever has a current working directory path of more than
1000 characters. Even if people have very long path names, they
don't make them the current working directory.
So if anything is changed, it's for purity only. Then, for purity,
we should get it right and support any path that the operating system
supports.
pitrou commentedon Jun 17, 2011
I don't see why that wouldn't be the case. They probably don't change to these directories *by hand*, but they can have scripts that cd into such a directory before doing stuff inside it. And these scripts can be written in Python.
vstinner commentedon Mar 25, 2013
For the record, os.getcwd() of Python 2 was improved by 96adf96d861a (issue bpo-2722) to use a dynamic buffer.
vstinner commentedon Mar 25, 2013
test_posix.py of Python 3 contains the test, but the test is "disabled" (dead code):
# Just returning nothing instead of the SkipTest exception,
# because the test results in Error in that case.
# Is that ok?
# raise unittest.SkipTest("cannot create directory for testing")
return
See the issue bpo-17516 for removal of dead code.
worr commentedon Apr 14, 2015
Revisiting this, I've updated python3 to calculate this and use gradual dynamic allocation like the python2 implementation.
worr commentedon Apr 21, 2015
I've incorporated some of the feedback from the reviews into this new patch. I used the PyMem_Raw* functions to do allocation to avoid having to acquire the GIL and also avoid complciations from the builtin memory allocator, since I'm not using python objects.
I have also fixed a memory leak in my original patch, as well as a case where OSes with a small MAX_PATH fail with ENAMETOOLONG
worr commentedon Apr 24, 2015
I've updated the patch with the comments from the review
python-dev commentedon Apr 24, 2015
New changeset abf1f3ae4fa8 by Victor Stinner in branch '3.4':
Issue bpo-9246: On POSIX, os.getcwd() now supports paths longer than 1025 bytes
https://hg.python.org/cpython/rev/abf1f3ae4fa8
New changeset b871ace5c58f by Victor Stinner in branch 'default':
(Merge 3.4) Issue bpo-9246: On POSIX, os.getcwd() now supports paths longer than
https://hg.python.org/cpython/rev/b871ace5c58f
vstinner commentedon Apr 24, 2015
Thanks William for your contribution, I commited your fix.
I just made a minor change on "if (cwd && use_bytes) {": you forgot to remove test now useless test on cwd, and I dropped { and } to make to short more readable (Note: the PEP-7 requires to put "}" and "else {" on two lines).
Ok, I just took 5 years to get Python 2 features in Python 3 :-)