Skip to content

x:/path/to/dir is not treated as an absolute path #12

@komh

Description

@komh

Hi/2.

Currently, only /path/to/dir is treated as an absolute path. x:/path/to/dir should be, too.

This is because OS/2 uses PosixPath.

Here is the patch to use WindowsPath.

Review, please...

pathlib.py.diff.txt

diff -uNr  pathlib.py.org pathlib.py
--- pathlib.py.org	2023-07-05 02:44:14.000000000 +0000
+++ pathlib.py	2023-10-17 19:57:34.000000000 +0000
@@ -22,6 +22,8 @@
         supports_symlinks = False
         _getfinalpathname = None
 else:
+    if os.name == 'os2':
+        _getfinalpathname = None
     nt = None
 
 
@@ -126,7 +128,7 @@
     has_drv = True
     pathmod = ntpath
 
-    is_supported = (os.name == 'nt')
+    is_supported = (os.name in ['nt', 'os2'])
 
     drive_letters = set('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
     ext_namespace_prefix = '\\\\?\\'
@@ -264,6 +266,21 @@
             return 'file:' + urlquote_from_bytes(path.as_posix().encode('utf-8'))
 
     def gethomedir(self, username):
+        if os.name == 'os2':
+            if not username:
+                try:
+                    return os.environ['HOME']
+                except KeyError:
+                    import pwd
+                    return pwd.getpwuid(os.getuid()).pw_dir
+            else:
+                import pwd
+                try:
+                    return pwd.getpwnam(username).pw_dir
+                except KeyError:
+                    raise RuntimeError("Can't determine home directory "
+                                       "for %r" % username)
+
         if 'USERPROFILE' in os.environ:
             userhome = os.environ['USERPROFILE']
         elif 'HOMEPATH' in os.environ:
@@ -298,7 +315,7 @@
     has_drv = False
     pathmod = posixpath
 
-    is_supported = (os.name != 'nt')
+    is_supported = (os.name not in ['nt', 'os2'])
 
     def splitroot(self, part, sep=sep):
         if part and part[0] == sep:
@@ -1078,7 +1095,7 @@
 
     def __new__(cls, *args, **kwargs):
         if cls is Path:
-            cls = WindowsPath if os.name == 'nt' else PosixPath
+            cls = WindowsPath if os.name in ['nt', 'os2'] else PosixPath
         self = cls._from_parts(args, init=False)
         if not self._flavour.is_supported:
             raise NotImplementedError("cannot instantiate %r on your system"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions