diff options
Diffstat (limited to 'sphinx/testing/path.py')
-rw-r--r-- | sphinx/testing/path.py | 43 |
1 files changed, 15 insertions, 28 deletions
diff --git a/sphinx/testing/path.py b/sphinx/testing/path.py index 1c9781dea..9477391b4 100644 --- a/sphinx/testing/path.py +++ b/sphinx/testing/path.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- """ sphinx.testing.path ~~~~~~~~~~~~~~~~~~~ @@ -9,29 +8,20 @@ import os import shutil import sys -from io import open - -from six import PY2, text_type if False: # For type annotation + import builtins # NOQA from typing import Any, Callable, IO, List # NOQA FILESYSTEMENCODING = sys.getfilesystemencoding() or sys.getdefaultencoding() -class path(text_type): +class path(str): """ Represents a path which behaves like a string. """ - if PY2: - def __new__(cls, s, encoding=FILESYSTEMENCODING, errors='strict'): - # type: (unicode, unicode, unicode) -> path - if isinstance(s, str): - s = s.decode(encoding, errors) - return text_type.__new__(cls, s) # type: ignore - return text_type.__new__(cls, s) # type: ignore @property def parent(self): @@ -42,7 +32,7 @@ class path(text_type): return self.__class__(os.path.dirname(self)) def basename(self): - # type: () -> unicode + # type: () -> str return os.path.basename(self) def abspath(self): @@ -107,7 +97,7 @@ class path(text_type): shutil.rmtree(self, ignore_errors=ignore_errors, onerror=onerror) def copytree(self, destination, symlinks=False): - # type: (unicode, bool) -> None + # type: (str, bool) -> None """ Recursively copy a directory to the given `destination`. If the given `destination` does not exist it will be created. @@ -120,7 +110,7 @@ class path(text_type): shutil.copytree(self, destination, symlinks=symlinks) def movetree(self, destination): - # type: (unicode) -> None + # type: (str) -> None """ Recursively move the file or directory to the given `destination` similar to the Unix "mv" command. @@ -151,30 +141,27 @@ class path(text_type): os.utime(self, arg) def open(self, mode='r', **kwargs): - # type: (unicode, Any) -> IO + # type: (str, Any) -> IO return open(self, mode, **kwargs) def write_text(self, text, encoding='utf-8', **kwargs): - # type: (unicode, unicode, Any) -> None + # type: (str, str, Any) -> None """ Writes the given `text` to the file. """ - if isinstance(text, bytes): - text = text.decode(encoding) with open(self, 'w', encoding=encoding, **kwargs) as f: f.write(text) def text(self, encoding='utf-8', **kwargs): - # type: (unicode, Any) -> unicode + # type: (str, Any) -> str """ Returns the text in the file. """ - mode = 'rU' if PY2 else 'r' - with open(self, mode=mode, encoding=encoding, **kwargs) as f: + with open(self, encoding=encoding, **kwargs) as f: return f.read() def bytes(self): - # type: () -> str + # type: () -> builtins.bytes """ Returns the bytes in the file. """ @@ -211,12 +198,12 @@ class path(text_type): """ return os.path.lexists(self) - def makedirs(self, mode=0o777): - # type: (int) -> None + def makedirs(self, mode=0o777, exist_ok=False): + # type: (int, bool) -> None """ Recursively create directories. """ - os.makedirs(self, mode) + os.makedirs(self, mode, exist_ok=exist_ok) def joinpath(self, *args): # type: (Any) -> path @@ -226,11 +213,11 @@ class path(text_type): return self.__class__(os.path.join(self, *map(self.__class__, args))) def listdir(self): - # type: () -> List[unicode] + # type: () -> List[str] return os.listdir(self) __div__ = __truediv__ = joinpath def __repr__(self): # type: () -> str - return '%s(%s)' % (self.__class__.__name__, text_type.__repr__(self)) + return '%s(%s)' % (self.__class__.__name__, super().__repr__()) |