Skip to content

Commit

Permalink
Merge pull request topazproject#538 from michelboaventura/master
Browse files Browse the repository at this point in the history
Implements suffix argument to File::basename
  • Loading branch information
alex committed Mar 21, 2013
2 parents f80a646 + eb45983 commit e6071fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions tests/objects/test_fileobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ def test_basename(self, space):
assert space.str_w(space.execute("return File.basename('ab')")) == "ab"
assert space.str_w(space.execute("return File.basename('/ab')")) == "ab"
assert space.str_w(space.execute("return File.basename('/foo/bar/ab')")) == "ab"
assert space.str_w(space.execute("return File.basename('ab.rb', '.rb')")) == "ab"
assert space.str_w(space.execute("return File.basename('ab.rb', 'b.rb')")) == "a"

def test_truncate(self, space, tmpdir):
f = tmpdir.join("file.txt")
Expand Down
11 changes: 8 additions & 3 deletions topaz/objects/fileobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,16 @@ def method_identicalp(self, space, file, other):
return space.newbool(file_stat.st_dev == other_stat.st_dev and
file_stat.st_ino == other_stat.st_ino)

@classdef.singleton_method("basename", filename="path")
def method_basename(self, space, filename):
@classdef.singleton_method("basename", filename="path", suffix="path")
def method_basename(self, space, filename, suffix=None):
i = filename.rfind("/") + 1
assert i >= 0
return space.newstr_fromstr(filename[i:])
filename = filename[i:]
if suffix is not None and filename.endswith(suffix):
end = len(filename) - len(suffix)
assert end >= 0
filename = filename[:end]
return space.newstr_fromstr(filename)

@classdef.singleton_method("umask", mask="int")
def method_umask(self, space, mask=-1):
Expand Down

0 comments on commit e6071fa

Please sign in to comment.