[Python-checkins] cpython (3.4): Issue #9246: On POSIX, os.getcwd() now supports paths longer than 1025 bytes

victor.stinner python-checkins at python.org
Sat Apr 25 00:23:54 CEST 2015


https://hg.python.org/cpython/rev/abf1f3ae4fa8
changeset:   95793:abf1f3ae4fa8
branch:      3.4
parent:      95791:135d5a3e415b
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Sat Apr 25 00:16:10 2015 +0200
summary:
  Issue #9246: On POSIX, os.getcwd() now supports paths longer than 1025 bytes

Patch written by William Orr.

files:
  Misc/NEWS             |   3 ++
  Modules/posixmodule.c |  36 ++++++++++++++++++++++++------
  2 files changed, 31 insertions(+), 8 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -34,6 +34,9 @@
 Library
 -------
 
+- Issue #9246: On POSIX, os.getcwd() now supports paths longer than 1025 bytes.
+  Patch written by William Orr.
+
 - Issue #23008: Fixed resolving attributes with boolean value is False in pydoc.
 
 - Fix asyncio issue 235: LifoQueue and PriorityQueue's put didn't
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -3418,12 +3418,15 @@
 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;
@@ -3457,14 +3460,31 @@
         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)
-        return posix_error();
+
+    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;
 }
 
 PyDoc_STRVAR(posix_getcwd__doc__,

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list