]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - src/allmydata/util/fileutil.py
Ensure the 'base' argument to abspath_expanduser_unicode takes effect on Windows...
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / util / fileutil.py
index 45b74063831ba445d3604c887178bf96b659164b..4dd501ce82a505eaf8cb16b4d14aa218dae655a9 100644 (file)
@@ -318,10 +318,14 @@ def abspath_expanduser_unicode(path, base=None):
     path = expanduser(path)
 
     if _getfullpathname:
-        # On Windows, os.path.isabs will return True for paths without a drive letter,
+        # On Windows, os.path.isabs will incorrectly return True
+        # for paths without a drive letter (that are not UNC paths),
         # e.g. "\\". See <http://bugs.python.org/issue1669539>.
         try:
-            path = _getfullpathname(path or u".")
+            if base is None:
+                path = _getfullpathname(path or u".")
+            else:
+                path = _getfullpathname(os.path.join(base, path))
         except OSError:
             pass
 
@@ -408,17 +412,25 @@ def windows_getenv(name):
         raise AssertionError("name must be Unicode")
 
     n = GetEnvironmentVariableW(name, None, 0)
-    if n <= 0:
+    # GetEnvironmentVariableW returns DWORD, so n cannot be negative.
+    if n == 0:
         err = GetLastError()
-        raise OSError("Windows error %d attempting to read environment variable %r"
+        raise OSError("Windows error %d attempting to read size of environment variable %r"
                       % (err, name))
+    if n == 1:
+        # Avoid an ambiguity between a zero-length string and an error in the return value of the
+        # call to GetEnvironmentVariableW below.
+        return u""
 
     buf = create_unicode_buffer(u'\0'*n)
     retval = GetEnvironmentVariableW(name, buf, n)
-    if retval <= 0:
+    if retval == 0:
         err = GetLastError()
         raise OSError("Windows error %d attempting to read environment variable %r"
                       % (err, name))
+    if retval >= n:
+        raise OSError("Unexpected result %d (expected less than %d) from GetEnvironmentVariableW attempting to read environment variable %r"
+                      % (retval, n, name))
 
     return buf.value