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

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


https://hg.python.org/cpython/rev/b871ace5c58f
changeset:   95794:b871ace5c58f
parent:      95792:5c0247a6f98a
parent:      95793:abf1f3ae4fa8
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Sat Apr 25 00:21:52 2015 +0200
summary:
  (Merge 3.4) Issue #9246: On POSIX, os.getcwd() now supports paths longer than
1025 bytes. Patch written by William Orr.

files:
  Misc/NEWS             |   5 +++-
  Modules/posixmodule.c |  36 ++++++++++++++++++++++++------
  2 files changed, 32 insertions(+), 9 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -15,6 +15,9 @@
 Library
 -------
 
+- Issue #9246: On POSIX, os.getcwd() now supports paths longer than 1025 bytes.
+  Patch written by William Orr.
+
 - Issue #17445: add difflib.diff_bytes() to support comparison of
   byte strings (fixes a regression from Python 2).
 
@@ -33,7 +36,7 @@
 - Issue #23728: binascii.crc_hqx() could return an integer outside of the range
   0-0xffff for empty data.
 
-- Issue #23887: urllib.error.HTTPError now has a proper repr() representation. 
+- Issue #23887: urllib.error.HTTPError now has a proper repr() representation.
   Patch by Berker Peksag.
 
 Documentation
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -3267,12 +3267,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;
@@ -3306,14 +3309,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)
+
+    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 @@
         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);

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


More information about the Python-checkins mailing list