Fix Socket#new on X18 and X19. #2033

Merged
merged 4 commits into from Nov 19, 2012

Projects

None yet

2 participants

@frodsan

1.8 mode:

Socket#new socket_type argument accepts a string that
references to a Socket::SOCK_* constant. If it's not an string
or integer, it raises Errno::EPROTONOSUPPORT error.

1.9 mode:

Socket#new socket_type argument accepts a symbol
or string that references to a Socket::SOCK_*
constant.

Francesco Rodriguez Fix Socket#new on X19.
Socket#new `socket_type` argument can accept a symbol
or string that references to a `Socket::SOCK_*`
constant.

    require 'socket'

    # Before:

    >> Socket.new(Socket::PF_UNIX, :DGRAM, 0)
    TypeError: Tried to use non-reference value 0x10256 as type Bignum (10)

    >> Socket.new(Socket::PF_UNIX, 'SOCK_DGRAM', 0)
    TypeError: converting to int

    # After:

    >> Socket.new(Socket::PF_UNIX, :DGRAM, 0)
    => #<Socket:fd 5>

    >> Socket.new(Socket::PF_UNIX, 'SOCK_DGRAM', 0)
    => #<Socket:fd 6>
896b1cf
@jc00ke jc00ke commented on an outdated diff Nov 19, 2012
spec/ruby/library/socket/socket/new_spec.rb
@@ -1,2 +1,22 @@
require File.expand_path('../../../../spec_helper', __FILE__)
require File.expand_path('../../fixtures/classes', __FILE__)
+
+describe "Socket#new" do
+ ruby_version_is "1.9" do
+ it "raises SocketError if given symbol is not a Socket constants reference" do
+ lambda { Socket.new(Socket::PF_UNIX, :NO_EXIST, 0) }.should raise_error(SocketError)
+ end
+
+ it "not raises SocketError if given symbol references a Socket constant" do
@jc00ke
jc00ke Nov 19, 2012

I think this would be better described by: only allows Socket constants as symbols

The not raises doesn't read right.

@jc00ke jc00ke commented on an outdated diff Nov 19, 2012
spec/ruby/library/socket/socket/new_spec.rb
@@ -1,2 +1,22 @@
require File.expand_path('../../../../spec_helper', __FILE__)
require File.expand_path('../../fixtures/classes', __FILE__)
+
+describe "Socket#new" do
+ ruby_version_is "1.9" do
+ it "raises SocketError if given symbol is not a Socket constants reference" do
+ lambda { Socket.new(Socket::PF_UNIX, :NO_EXIST, 0) }.should raise_error(SocketError)
+ end
+
+ it "not raises SocketError if given symbol references a Socket constant" do
+ [ :DGRAM, :RAW, :RDM, :SEQPACKET, :STREAM ].each do |socket_type|
+ lambda { Socket.new(Socket::PF_UNIX, socket_type, 0) }.should_not raise_error(SocketError)
+ end
+ end
+
+ it "not raises SocketError if given string references a Socket constant" do
@jc00ke
jc00ke Nov 19, 2012

I think this would be better described by: only allows Socket constants as strings

@frodsan

@jc00ke updated. Thanks :)

Francesco Ro... added some commits Nov 19, 2012
Francesco Rodriguez Fix Socket#new on X18
This fixes the following behaviour:

    require 'socket'

    # rbx -X18
    >> Socket.new(Socket::PF_UNIX, 'SOCK_DGRAM', 0)
    TypeError: converting to int

    >> Socket.new(Socket::PF_UNIX, :DGRAM, 0)
    TypeError: Tried to use non-reference value 0xf266 as type Bignum (10)

    # ruby1.8

    >> Socket.new(Socket::PF_UNIX, 'SOCK_DGRAM', 0)
    => #<Socket:0x106117798>

    >> Socket.new(Socket::PF_UNIX, :DGRAM, 0)
    Errno::EPROTONOSUPPORT: Protocol not supported - socket(2)
052955b
Francesco Rodriguez Add specs for Socket#new on X18 58ec9c2
@jc00ke jc00ke merged commit b045c63 into rubinius:master Nov 19, 2012

1 check was pending

Details default The Travis build is in progress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment