Skip to content

Commit 9bba23c

Browse files
committed
[GR-44710] Fix rb_time_timespec_new function
PullRequest: truffleruby/3679
2 parents 01e0ce5 + ed5362b commit 9bba23c

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Bug fixes:
2323
* Fix `\P{}` matching in regular expressions (#2798, @andrykonchin).
2424
* Fix constants lookup when `BasicObject#instance_eval` method is called with a String (#2810, @andrykonchin).
2525
* Don't trigger the `method_added` event when changing a method's visibility or calling `module_function` (@paracycle, @nirvdrum).
26+
* Fix `rb_time_timespec_new` function to not call `Time.at` method directly (@andrykonchin).
2627

2728
Compatibility:
2829

lib/truffle/truffle/cext.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ module Truffle::CExt
112112
LIBTRUFFLERUBY = libtruffleruby
113113
end
114114

115+
# Rails 6 overrides `Time.at` in a way that doesn't delegate keyword arguments to the original `Time.at` method.
116+
# The issue was fixed in Rails 6.1 but wasn't backported.
117+
# It causes ArgumentError exception when `rb_time_timespec_new` function is called in the `pg` gem.
118+
# See https://github.com/rails/rails/issues/47586.
119+
ORIGINAL_TIME_AT = Time.method(:at)
120+
115121
def self.register_libtruffleruby(libtruffleruby)
116122
SET_LIBTRUFFLERUBY.call(libtruffleruby)
117123
end
@@ -1673,21 +1679,21 @@ def warning?
16731679
end
16741680

16751681
def rb_time_nano_new(sec, nsec)
1676-
Time.at sec, nsec, :nanosecond
1682+
ORIGINAL_TIME_AT.call(sec, nsec, :nanosecond)
16771683
end
16781684

16791685
def rb_time_timespec_new(sec, nsec, offset, is_utc, is_local)
16801686
if is_local
1681-
Time.at(sec, nsec, :nanosecond)
1687+
ORIGINAL_TIME_AT.call(sec, nsec, :nanosecond)
16821688
elsif is_utc
1683-
Time.at(sec, nsec, :nanosecond, in: 0).getgm
1689+
ORIGINAL_TIME_AT.call(sec, nsec, :nanosecond, in: 0).getgm
16841690
else
1685-
Time.at(sec, nsec, :nanosecond, in: offset)
1691+
ORIGINAL_TIME_AT.call(sec, nsec, :nanosecond, in: offset)
16861692
end
16871693
end
16881694

16891695
def rb_time_num_new(timev, off)
1690-
Time.at(timev).getlocal(off)
1696+
ORIGINAL_TIME_AT.call(timev).getlocal(off)
16911697
end
16921698

16931699
def rb_time_interval_acceptable(time_val)

spec/ruby/core/warning/warn_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ def Warning.warn(msg)
7979
Warning[:deprecated] = true
8080
begin
8181
-> {
82-
warn("foo", category: :deprecated)
83-
}.should complain("foo\n")
82+
Warning.warn("foo", category: :deprecated)
83+
}.should complain("foo")
8484
ensure
8585
Warning[:deprecated] = warn_deprecated
8686
end
@@ -91,8 +91,8 @@ def Warning.warn(msg)
9191
Warning[:experimental] = true
9292
begin
9393
-> {
94-
warn("foo", category: :experimental)
95-
}.should complain("foo\n")
94+
Warning.warn("foo", category: :experimental)
95+
}.should complain("foo")
9696
ensure
9797
Warning[:experimental] = warn_experimental
9898
end
@@ -103,7 +103,7 @@ def Warning.warn(msg)
103103
Warning[:deprecated] = false
104104
begin
105105
-> {
106-
warn("foo", category: :deprecated)
106+
Warning.warn("foo", category: :deprecated)
107107
}.should_not complain
108108
ensure
109109
Warning[:deprecated] = warn_deprecated
@@ -115,7 +115,7 @@ def Warning.warn(msg)
115115
Warning[:experimental] = false
116116
begin
117117
-> {
118-
warn("foo", category: :experimental)
118+
Warning.warn("foo", category: :experimental)
119119
}.should_not complain
120120
ensure
121121
Warning[:experimental] = warn_experimental

spec/ruby/optional/capi/time_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@
283283
-> { @s.rb_time_timespec_new(1447087832, 476451125, 86400) }.should raise_error(ArgumentError)
284284
-> { @s.rb_time_timespec_new(1447087832, 476451125, -86400) }.should raise_error(ArgumentError)
285285
end
286+
287+
it "doesn't call Time.at directly" do
288+
Time.should_not_receive(:at)
289+
@s.rb_time_timespec_new(1447087832, 476451125, 32400).should be_kind_of(Time)
290+
end
286291
end
287292

288293
describe "rb_timespec_now" do

0 commit comments

Comments
 (0)