diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2015-04-24 22:21:52 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2015-04-24 22:21:52 (GMT) |
commit | cc0bbbc781254cc43628649815435ababafa35e2 (patch) | |
tree | 30d66f42f696052e68a45ada7696b93c29361fd7 /Modules | |
parent | 51f58059f6d5eb958201d1d90286e5917b77ed59 (diff) | |
parent | 4403d7def0ec8e6b395223fb2ab8b570ecc7a6e4 (diff) | |
download | cpython-cc0bbbc781254cc43628649815435ababafa35e2.zip cpython-cc0bbbc781254cc43628649815435ababafa35e2.tar.gz cpython-cc0bbbc781254cc43628649815435ababafa35e2.tar.bz2 |
(Merge 3.4) Issue #9246: On POSIX, os.getcwd() now supports paths longer than
1025 bytes. Patch written by William Orr.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/posixmodule.c | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index ce34b3b..3bed958 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -3267,12 +3267,15 @@ os_lchown_impl(PyModuleDef *module, path_t *path, uid_t uid, gid_t gid) static PyObject * posix_getcwd(int use_bytes) { - char buf[1026]; - char *res; + char *buf, *tmpbuf; + char *cwd; + const size_t chunk = 1024; + size_t buflen = 0; + PyObject *obj; #ifdef MS_WINDOWS if (!use_bytes) { - wchar_t wbuf[1026]; + wchar_t wbuf[MAXPATHLEN]; wchar_t *wbuf2 = wbuf; PyObject *resobj; DWORD len; @@ -3306,14 +3309,31 @@ posix_getcwd(int use_bytes) return NULL; #endif + buf = cwd = NULL; Py_BEGIN_ALLOW_THREADS - res = getcwd(buf, sizeof buf); + do { + buflen += chunk; + tmpbuf = PyMem_RawRealloc(buf, buflen); + if (tmpbuf == NULL) + break; + + buf = tmpbuf; + cwd = getcwd(buf, buflen); + } while (cwd == NULL && errno == ERANGE); Py_END_ALLOW_THREADS - if (res == NULL) + + if (cwd == NULL) { + PyMem_RawFree(buf); return posix_error(); + } + if (use_bytes) - return PyBytes_FromStringAndSize(buf, strlen(buf)); - return PyUnicode_DecodeFSDefault(buf); + obj = PyBytes_FromStringAndSize(buf, strlen(buf)); + else + obj = PyUnicode_DecodeFSDefault(buf); + PyMem_RawFree(buf); + + return obj; } @@ -8873,7 +8893,7 @@ os_truncate_impl(PyModuleDef *module, path_t *path, Py_off_t length) fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); else fd = _open(path->narrow, _O_WRONLY | _O_BINARY | _O_NOINHERIT); - if (fd < 0) + if (fd < 0) result = -1; else { result = _chsize_s(fd, length); |