Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix breakage of IO.parse_mode on Rubinius (issue #10).

A version of IO.parse_mode was being defined that lacked certain
functionality (such as handling integer modes). Move parse_mode to
the IO::Mode class, and move that to Ole::IOMode to reduce namespace
pollution.
  • Loading branch information...
commit 5d47217f3987bd818b0948be4400096cad0a85dd 1 parent 7eb248d
@aquasync authored
View
6 lib/ole/ranges_io.rb
@@ -1,6 +1,6 @@
# encoding: ASCII-8BIT
-# need IO::Mode
+# need Ole::IOMode
require 'ole/support'
#
@@ -57,7 +57,7 @@ def initialize io, mode='r', params={}
mode, params = 'r', mode if Hash === mode
ranges = params[:ranges]
@params = {:close_parent => false}.merge params
- @mode = IO::Mode.new mode
+ @mode = Ole::IOMode.new mode
@io = io
# initial position in the file
@pos = 0
@@ -255,7 +255,7 @@ def inspect
class RangesIONonResizeable < RangesIO # :nodoc:
def initialize io, mode='r', params={}
mode, params = 'r', mode if Hash === mode
- flags = IO::Mode.new(mode).flags & ~IO::TRUNC
+ flags = Ole::IOMode.new(mode).flags & ~IO::TRUNC
super io, flags, params
end
end
View
2  lib/ole/storage/base.rb
@@ -57,7 +57,7 @@ def initialize arg, mode=nil, params={}
# io.writeable? otherwise.
@writeable = begin
if mode
- IO::Mode.new(mode).writeable?
+ IOMode.new(mode).writeable?
else
@io.flush
# this is for the benefit of ruby-1.9
View
2  lib/ole/storage/file_system.rb
@@ -151,7 +151,7 @@ def directory? path
end
def open path, mode='r', &block
- if IO::Mode.new(mode).create?
+ if IOMode.new(mode).create?
begin
dirent = dirent_from_path path
rescue Errno::ENOENT
View
56 lib/ole/support.rb
@@ -165,34 +165,8 @@ def to_tree io='', &inspect
end
end
-# can include File::Constants
-class IO
- # this is for jruby
- include File::Constants unless defined?(RDONLY)
-
- # nabbed from rubinius, and modified
- def self.parse_mode mode
- ret = 0
-
- case mode[0, 1]
- when 'r'; ret |= RDONLY
- when 'w'; ret |= WRONLY | CREAT | TRUNC
- when 'a'; ret |= WRONLY | CREAT | APPEND
- else raise ArgumentError, "illegal access mode #{mode}"
- end
-
- (1...mode.length).each do |i|
- case mode[i, 1]
- when '+'; ret = (ret & ~(RDONLY | WRONLY)) | RDWR
- when 'b'; ret |= Mode::BINARY
- else raise ArgumentError, "illegal access mode #{mode}"
- end
- end
-
- ret
- end
-
- class Mode
+module Ole
+ class IOMode
# ruby 1.9 defines binary as 0, which isn't very helpful.
# its 4 in rubinius. no longer using
#
@@ -207,9 +181,31 @@ module Constants
include Constants
NAMES = %w[rdonly wronly rdwr creat trunc append binary]
+ # nabbed from rubinius, and modified
+ def self.parse_mode mode
+ ret = 0
+
+ case mode[0, 1]
+ when 'r'; ret |= RDONLY
+ when 'w'; ret |= WRONLY | CREAT | TRUNC
+ when 'a'; ret |= WRONLY | CREAT | APPEND
+ else raise ArgumentError, "illegal access mode #{mode}"
+ end
+
+ (1...mode.length).each do |i|
+ case mode[i, 1]
+ when '+'; ret = (ret & ~(RDONLY | WRONLY)) | RDWR
+ when 'b'; ret |= BINARY
+ else raise ArgumentError, "illegal access mode #{mode}"
+ end
+ end
+
+ ret
+ end
+
attr_reader :flags
def initialize flags
- flags = IO.parse_mode flags.to_str if flags.respond_to? :to_str
+ flags = self.class.parse_mode flags.to_str if flags.respond_to? :to_str
raise ArgumentError, "invalid flags - #{flags.inspect}" unless Fixnum === flags
@flags = flags
end
@@ -251,7 +247,7 @@ def apply io
=end
def inspect
- names = NAMES.map { |name| name if (flags & Mode.const_get(name.upcase)) != 0 }
+ names = NAMES.map { |name| name if (flags & IOMode.const_get(name.upcase)) != 0 }
names.unshift 'rdonly' if (flags & 0x3) == 0
"#<#{self.class} #{names.compact * '|'}>"
end
View
2  test/test_ranges_io.rb
@@ -97,7 +97,7 @@ def test_non_resizeable
end
# will be fine
@io = RangesIONonResizeable.new(StringIO.new, 'w', :ranges => [])
- assert_equal '#<IO::Mode wronly|creat>', @io.instance_variable_get(:@mode).inspect
+ assert_equal '#<Ole::IOMode wronly|creat>', @io.instance_variable_get(:@mode).inspect
end
end
View
8 test/test_support.rb
@@ -53,7 +53,7 @@ def test_symbol
class TestIOMode < Test::Unit::TestCase
def mode s
- IO::Mode.new s
+ Ole::IOMode.new s
end
def test_parse
@@ -83,9 +83,9 @@ def test_invalid
end
def test_inspect
- assert_equal '#<IO::Mode rdonly>', IO::Mode.new('r').inspect
- assert_equal '#<IO::Mode rdwr|creat|trunc|binary>', IO::Mode.new('wb+').inspect
- assert_equal '#<IO::Mode wronly|creat|append>', IO::Mode.new('a').inspect
+ assert_equal '#<Ole::IOMode rdonly>', mode('r').inspect
+ assert_equal '#<Ole::IOMode rdwr|creat|trunc|binary>', mode('wb+').inspect
+ assert_equal '#<Ole::IOMode wronly|creat|append>', mode('a').inspect
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.