Skip to content
Permalink
Browse files

Merge pull request #808 from kostya/expand_path

fix File.expand_path
  • Loading branch information
alex committed Jul 16, 2013
2 parents 12a12c0 + b23c6a2 commit 642c9ebcfdcec1b679b261442f2b3d77fc5d2d01
Showing with 21 additions and 25 deletions.
  1. +2 −17 spec/tags/core/file/expand_path_tags.txt
  2. +19 −8 topaz/objects/fileobject.py
@@ -1,19 +1,4 @@
fails:File.expand_path expand path with
fails:File.expand_path expand_path for commoms unix path give a full path
fails:File.expand_path does not replace multiple '/' at the beginning of the path
fails:File.expand_path replaces multiple '/' with a single '/'
fails:File.expand_path raises an ArgumentError if the path is not valid
fails:File.expand_path expands ~ENV['USER'] to the user's home directory
fails:File.expand_path does not expand ~ENV['USER'] when it's not at the start
fails:File.expand_path expands ../foo with ~/dir as base dir to /path/to/user/home/foo
fails:File.expand_path accepts objects that have a #to_path method
fails:File.expand_path raises a TypeError if not passed a String type
fails:File.expand_path expands /./dir to /dir
fails:File.expand_path produces a String in the default external encoding
fails:File.expand_path does not modify the string argument
fails:File.expand_path does not modify a HOME string argument
fails:File.expand_path returns a String when passed a String subclass
fails:File.expand_path when HOME is not set raises an ArgumentError when passed '~' if HOME is nil
fails:File.expand_path when HOME is not set raises an ArgumentError when passed '~/' if HOME is nil
fails:File.expand_path when HOME is not set raises an ArgumentError when passed '~' if HOME == ''
fails:File.expand_path raises an Encoding::CompatibilityError if the external encoding is not compatible
fails:File.expand_path raises an ArgumentError if the path is not valid
fails:File.expand_path expands ~ENV['USER'] to the user's home directory
@@ -135,12 +135,18 @@ def method_dirname(self, space, path):
@classdef.singleton_method("expand_path", path="path")
def method_expand_path(self, space, path, w_dir=None):
if path and path[0] == "~":
try:
home = os.environ["HOME"]
except KeyError:
raise space.error(space.w_ArgumentError, "couldn't find HOME environment -- expanding")
if not home or (not IS_WINDOWS and home[0] != "/"):
raise space.error(space.w_ArgumentError, "non-absolute home")
if len(path) >= 2 and path[1] == "/":
path = os.environ["HOME"] + path[1:]
path = home + path[1:]
elif len(path) < 2:
return space.newstr_fromstr(os.environ["HOME"])
return space.newstr_fromstr(home)
else:
raise NotImplementedError
raise space.error(space.w_NotImplementedError, "~user for File.expand_path")
elif not path or path[0] != "/":
if w_dir is not None and w_dir is not space.w_nil:
dir = space.str_w(space.send(self, "expand_path", [w_dir]))
@@ -153,18 +159,23 @@ def method_expand_path(self, space, path, w_dir=None):
if IS_WINDOWS:
path = path.replace("\\", "/")
parts = path.split("/")
was_letter = False
first_slash = True
for part in parts:
if part == "..":
items.pop()
if not part and not was_letter:
if not first_slash:
items.append(part)
first_slash = False
elif part == "..":
if len(items) > 0:
items.pop()
elif part and part != ".":
was_letter = True
items.append(part)

if not IS_WINDOWS:
root = "/"
else:
root = ""
if not items:
return space.newstr_fromstr(root)
return space.newstr_fromstr(root + "/".join(items))

@classdef.singleton_method("join")

0 comments on commit 642c9eb

Please sign in to comment.
You can’t perform that action at this time.