Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os.path: refactor: use sep instead of literal / #730

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions python-stdlib/os-path/os/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ def normpath(s):


def abspath(s):
if s[0] != "/":
return os.getcwd() + "/" + s
if s[0] != sep:
return os.getcwd() + sep + s
return s


def join(*args):
# TODO: this is non-compliant
if type(args[0]) is bytes:
return b"/".join(args)
return bytes(sep).join(args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line will raise an exception:

MicroPython v1.20.0-330-g076bfc7ed8-dirty on 2023-09-26; linux [GCC 13.2.1] version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> bytes("/")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string argument without an encoding
>>> "/".encode()
b'/'

else:
return "/".join(args)
return sep.join(args)


def split(path):
if path == "":
return ("", "")
r = path.rsplit("/", 1)
r = path.rsplit(sep, 1)
if len(r) == 1:
return ("", path)
head = r[0] # .rstrip("/")
if not head:
head = "/"
head = sep
return (head, r[1])


Expand Down Expand Up @@ -67,7 +67,7 @@ def isdir(path):


def expanduser(s):
if s == "~" or s.startswith("~/"):
if s == "~" or s.startswith("~" + sep):
h = os.getenv("HOME")
return h + s[1:]
if s[0] == "~":
Expand Down
Loading