Skip to content

Commit 57ec7de

Browse files
committed
[GR-20446] Fix argument handling in Kernel.printf implementation.
PullRequest: truffleruby/1258
2 parents 7daa863 + 2000969 commit 57ec7de

File tree

3 files changed

+9
-105
lines changed

3 files changed

+9
-105
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Bug fixes:
6868
* Fixed issue with `ENV.each_key`.
6969
* Fixed `ENV.replace` implementation.
7070
* Fixed `ENV.udpate` implementation.
71+
* Fixed argument handling in `Kernel.printf`.
7172

7273
Compatibility:
7374

spec/tags/core/kernel/printf_tags.txt

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,17 @@
1-
fails:Kernel.printf calls write on the first argument when it is not a string
2-
fails:Kernel.printf formatting io is specified integer formats converts argument into Integer with to_int
3-
fails:Kernel.printf formatting io is specified integer formats converts argument into Integer with to_i if to_int isn't available
4-
fails:Kernel.printf formatting io is specified integer formats converts String argument with Kernel#Integer
5-
fails:Kernel.printf formatting io is specified integer formats b converts argument as a binary number
6-
fails:Kernel.printf formatting io is specified integer formats b displays negative number as a two's complement prefixed with '..1'
7-
fails:Kernel.printf formatting io is specified integer formats b collapse negative number representation if it equals 1
8-
fails:Kernel.printf formatting io is specified integer formats B converts argument as a binary number
9-
fails:Kernel.printf formatting io is specified integer formats B displays negative number as a two's complement prefixed with '..1'
10-
fails:Kernel.printf formatting io is specified integer formats B collapse negative number representation if it equals 1
11-
fails:Kernel.printf formatting io is specified integer formats d converts argument as a decimal number
12-
fails:Kernel.printf formatting io is specified integer formats d works well with large numbers
13-
fails:Kernel.printf formatting io is specified integer formats i converts argument as a decimal number
14-
fails:Kernel.printf formatting io is specified integer formats i works well with large numbers
15-
fails:Kernel.printf formatting io is specified integer formats u converts argument as a decimal number
16-
fails:Kernel.printf formatting io is specified integer formats u works well with large numbers
17-
fails:Kernel.printf formatting io is specified integer formats o converts argument as an octal number
18-
fails:Kernel.printf formatting io is specified integer formats o displays negative number as a two's complement prefixed with '..7'
19-
fails:Kernel.printf formatting io is specified integer formats o collapse negative number representation if it equals 7
20-
fails:Kernel.printf formatting io is specified integer formats x converts argument as a hexadecimal number
21-
fails:Kernel.printf formatting io is specified integer formats x displays negative number as a two's complement prefixed with '..f'
22-
fails:Kernel.printf formatting io is specified integer formats x collapse negative number representation if it equals f
23-
fails:Kernel.printf formatting io is specified integer formats X converts argument as a hexadecimal number with uppercase letters
24-
fails:Kernel.printf formatting io is specified integer formats X displays negative number as a two's complement prefixed with '..f'
25-
fails:Kernel.printf formatting io is specified integer formats X collapse negative number representation if it equals F
26-
fails:Kernel.printf formatting io is specified float formats converts argument into Float
27-
fails:Kernel.printf formatting io is specified float formats e converts argument into exponential notation [-]d.dddddde[+-]dd
28-
fails:Kernel.printf formatting io is specified float formats e cuts excessive digits and keeps only 6 ones
29-
fails:Kernel.printf formatting io is specified float formats e rounds the last significant digit to the closest one
30-
fails:Kernel.printf formatting io is specified float formats e displays Float::INFINITY as Inf
31-
fails:Kernel.printf formatting io is specified float formats e displays Float::NAN as NaN
32-
fails:Kernel.printf formatting io is specified float formats E converts argument into exponential notation [-]d.dddddde[+-]dd
33-
fails:Kernel.printf formatting io is specified float formats E cuts excessive digits and keeps only 6 ones
34-
fails:Kernel.printf formatting io is specified float formats E rounds the last significant digit to the closest one
35-
fails:Kernel.printf formatting io is specified float formats E displays Float::INFINITY as Inf
36-
fails:Kernel.printf formatting io is specified float formats E displays Float::NAN as NaN
37-
fails:Kernel.printf formatting io is specified float formats f converts floating point argument as [-]ddd.dddddd
38-
fails:Kernel.printf formatting io is specified float formats f cuts excessive digits and keeps only 6 ones
39-
fails:Kernel.printf formatting io is specified float formats f rounds the last significant digit to the closest one
40-
fails:Kernel.printf formatting io is specified float formats f displays Float::INFINITY as Inf
41-
fails:Kernel.printf formatting io is specified float formats f displays Float::NAN as NaN
42-
fails:Kernel.printf formatting io is specified float formats g displays Float::INFINITY as Inf
43-
fails:Kernel.printf formatting io is specified float formats g displays Float::NAN as NaN
44-
fails:Kernel.printf formatting io is specified float formats g the exponent is less than -4 converts a floating point number using exponential form
45-
fails:Kernel.printf formatting io is specified float formats g the exponent is greater than or equal to the precision (6 by default) converts a floating point number using exponential form
46-
fails:Kernel.printf formatting io is specified float formats g otherwise converts a floating point number in dd.dddd form
47-
fails:Kernel.printf formatting io is specified float formats g otherwise cuts excessive digits in fractional part and keeps only 4 ones
48-
fails:Kernel.printf formatting io is specified float formats g otherwise rounds the last significant digit to the closest one in fractional part
49-
fails:Kernel.printf formatting io is specified float formats g otherwise cuts fraction part to have only 6 digits at all
50-
fails:Kernel.printf formatting io is specified float formats G displays Float::INFINITY as Inf
51-
fails:Kernel.printf formatting io is specified float formats G displays Float::NAN as NaN
52-
fails:Kernel.printf formatting io is specified float formats G the exponent is less than -4 converts a floating point number using exponential form
53-
fails:Kernel.printf formatting io is specified float formats G the exponent is greater than or equal to the precision (6 by default) converts a floating point number using exponential form
54-
fails:Kernel.printf formatting io is specified float formats G otherwise converts a floating point number in dd.dddd form
55-
fails:Kernel.printf formatting io is specified float formats G otherwise cuts excessive digits in fractional part and keeps only 4 ones
56-
fails:Kernel.printf formatting io is specified float formats G otherwise rounds the last significant digit to the closest one in fractional part
57-
fails:Kernel.printf formatting io is specified float formats G otherwise cuts fraction part to have only 6 digits at all
58-
fails:Kernel.printf formatting io is specified float formats a converts floating point argument as [-]0xh.hhhhp[+-]dd
59-
fails:Kernel.printf formatting io is specified float formats a displays Float::INFINITY as Inf
60-
fails:Kernel.printf formatting io is specified float formats a displays Float::NAN as NaN
61-
fails:Kernel.printf formatting io is specified float formats A converts floating point argument as [-]0xh.hhhhp[+-]dd and use uppercase X and P
62-
fails:Kernel.printf formatting io is specified float formats A displays Float::INFINITY as Inf
63-
fails:Kernel.printf formatting io is specified float formats A displays Float::NAN as NaN
64-
fails:Kernel.printf formatting io is specified other formats c displays character if argument is a numeric code of character
65-
fails:Kernel.printf formatting io is specified other formats c displays character if argument is a single character string
66-
fails:Kernel.printf formatting io is specified other formats c raises ArgumentError if argument is a string of several characters
671
fails:Kernel.printf formatting io is specified other formats c raises ArgumentError if argument is an empty string
682
fails:Kernel.printf formatting io is specified other formats c supports Unicode characters
69-
fails:Kernel.printf formatting io is specified other formats p displays argument.inspect value
70-
fails:Kernel.printf formatting io is specified other formats s substitute argument passes as a string
71-
fails:Kernel.printf formatting io is specified other formats s converts argument to string with to_s
723
fails:Kernel.printf formatting io is specified other formats s does not try to convert with to_str
734
fails:Kernel.printf formatting io is specified other formats % alone displays the percent sign
74-
fails:Kernel.printf formatting io is specified other formats % is escaped by %
75-
fails:Kernel.printf formatting io is specified flags space applies to numeric formats bBdiouxXeEfgGaA leaves a space at the start of non-negative numbers
76-
fails:Kernel.printf formatting io is specified flags space applies to numeric formats bBdiouxXeEfgGaA does not leave a space at the start of negative numbers
77-
fails:Kernel.printf formatting io is specified flags space applies to numeric formats bBdiouxXeEfgGaA prevents converting negative argument to two's complement form
78-
fails:Kernel.printf formatting io is specified flags space applies to numeric formats bBdiouxXeEfgGaA treats several white spaces as one
79-
fails:Kernel.printf formatting io is specified flags (digit)$ specifies the absolute argument number for this field
80-
fails:Kernel.printf formatting io is specified flags (digit)$ raises exception if argument number is bigger than actual arguments list
81-
fails:Kernel.printf formatting io is specified flags (digit)$ ignores '-' sign
82-
fails:Kernel.printf formatting io is specified flags (digit)$ raises ArgumentError exception when absolute and relative argument numbers are mixed
83-
fails:Kernel.printf formatting io is specified flags # applies to format o increases the precision until the first digit will be `0' if it is not formatted as complements
845
fails:Kernel.printf formatting io is specified flags # applies to format o does nothing for negative argument
85-
fails:Kernel.printf formatting io is specified flags # applies to formats bBxX prefixes the result with 0x, 0X, 0b and 0B respectively for non-zero argument
866
fails:Kernel.printf formatting io is specified flags # applies to formats bBxX does nothing for zero argument
877
fails:Kernel.printf formatting io is specified flags # applies to formats aAeEfgG forces a decimal point to be added, even if no digits follow
88-
fails:Kernel.printf formatting io is specified flags # applies to formats aAeEfgG changes format from dd.dddd to exponential form for gG
898
fails:Kernel.printf formatting io is specified flags # applies to gG does not remove trailing zeros
90-
fails:Kernel.printf formatting io is specified flags + applies to numeric formats bBdiouxXaAeEfgG adds a leading plus sign to non-negative numbers
91-
fails:Kernel.printf formatting io is specified flags + applies to numeric formats bBdiouxXaAeEfgG does not use two's complement form for negative numbers for formats bBoxX
929
fails:Kernel.printf formatting io is specified flags - left-justifies the result of conversion if width is specified
93-
fails:Kernel.printf formatting io is specified flags 0 (zero) applies to numeric formats bBdiouxXaAeEfgG and width is specified pads with zeros, not spaces
9410
fails:Kernel.printf formatting io is specified flags 0 (zero) applies to numeric formats bBdiouxXaAeEfgG and width is specified uses radix-1 when displays negative argument as a two's complement
95-
fails:Kernel.printf formatting io is specified flags * uses the previous argument as the field width
9611
fails:Kernel.printf formatting io is specified flags * left-justifies the result if width is negative
97-
fails:Kernel.printf formatting io is specified flags * uses the specified argument as the width if * is followed by a number and $
9812
fails:Kernel.printf formatting io is specified flags * left-justifies the result if specified with $ argument is negative
99-
fails:Kernel.printf formatting io is specified flags * raises ArgumentError when is mixed with width
100-
fails:Kernel.printf formatting io is specified width specifies the minimum number of characters that will be written to the result
101-
fails:Kernel.printf formatting io is specified width is ignored if argument's actual length is greater
102-
fails:Kernel.printf formatting io is specified precision integer types controls the number of decimal places displayed
103-
fails:Kernel.printf formatting io is specified precision float types controls the number of decimal places displayed in fraction part
104-
fails:Kernel.printf formatting io is specified precision float types does not affect G format
10513
fails:Kernel.printf formatting io is specified precision string formats determines the maximum number of characters to be copied from the string
106-
fails:Kernel.printf formatting io is specified reference by name %<name>s style uses value passed in a hash argument
107-
fails:Kernel.printf formatting io is specified reference by name %<name>s style supports flags, width, precision and type
108-
fails:Kernel.printf formatting io is specified reference by name %<name>s style allows to place name in any position
109-
fails:Kernel.printf formatting io is specified reference by name %<name>s style cannot be mixed with unnamed style
110-
fails:Kernel.printf formatting io is specified reference by name %{name} style uses value passed in a hash argument
111-
fails:Kernel.printf formatting io is specified reference by name %{name} style does not support type style
11214
fails:Kernel.printf formatting io is specified reference by name %{name} style supports flags, width and precision
113-
fails:Kernel.printf formatting io is specified reference by name %{name} style cannot be mixed with unnamed style
114-
fails:Kernel.printf formatting io is specified reference by name %{name} style raises KeyError when there is no matching key
115-
fails:Kernel.printf formatting io is specified reference by name %{name} style converts value to String with to_s
11615
fails:Kernel.printf formatting io is not specified other formats c raises ArgumentError if argument is an empty string
11716
fails:Kernel.printf formatting io is not specified other formats c supports Unicode characters
11817
fails:Kernel.printf formatting io is not specified other formats s does not try to convert with to_str
@@ -127,7 +26,4 @@ fails:Kernel.printf formatting io is not specified flags * left-justifies the re
12726
fails:Kernel.printf formatting io is not specified precision string formats determines the maximum number of characters to be copied from the string
12827
fails:Kernel.printf formatting io is not specified reference by name %{name} style supports flags, width and precision
12928
fails:Kernel.printf formatting io is specified other formats % alone raises an ArgumentError
130-
fails:Kernel.printf formatting io is specified faulty key sets the Hash as the receiver of KeyError
131-
fails:Kernel.printf formatting io is specified faulty key sets the unmatched key as the key of KeyError
13229
fails:Kernel.printf formatting io is not specified other formats % alone raises an ArgumentError
133-
fails:Kernel.printf formatting io is specified faulty key raises a KeyError

src/main/ruby/truffleruby/core/kernel.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,14 @@ def __dir__
586586
module_function :__dir__
587587

588588
def printf(*args)
589-
print sprintf(*args)
589+
return nil if args.empty?
590+
if Truffle::Type.object_kind_of?(args[0], String)
591+
print sprintf(*args)
592+
else
593+
io = args.shift
594+
io.write(sprintf(*args))
595+
end
596+
nil
590597
end
591598
module_function :printf
592599

0 commit comments

Comments
 (0)