mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-18 21:40:32 +00:00
This commit is contained in:
@@ -44,6 +44,8 @@
|
||||
|
||||
- `os.FileInfo` (returned by `getFileInfo`) now contains `blockSize`,
|
||||
determining preferred I/O block size for this file object.
|
||||
- `repr` now doesn't insert trailing newline; previous behavior was very inconsistent,
|
||||
see #16034. Use `-d:nimLegacyReprWithNewline` for previous behavior.
|
||||
|
||||
## Language changes
|
||||
|
||||
|
||||
@@ -1334,6 +1334,9 @@ proc insert*[T](x: var seq[T], item: sink T, i = 0.Natural) {.noSideEffect.} =
|
||||
when not defined(nimV2):
|
||||
proc repr*[T](x: T): string {.magic: "Repr", noSideEffect.}
|
||||
## Takes any Nim variable and returns its string representation.
|
||||
## No trailing newline is inserted (so `echo` won't add an empty newline).
|
||||
## Use `-d:nimLegacyReprWithNewline` to revert to old behavior where newlines
|
||||
## were added in some cases.
|
||||
##
|
||||
## It works even for complex data graphs with cycles. This is a great
|
||||
## debugging tool.
|
||||
|
||||
@@ -325,5 +325,6 @@ when not defined(useNimRtl):
|
||||
else:
|
||||
var p = p
|
||||
reprAux(result, addr(p), typ, cl)
|
||||
add result, "\n"
|
||||
when defined(nimLegacyReprWithNewline): # see PR #16034
|
||||
add result, "\n"
|
||||
deinitReprClosure(cl)
|
||||
|
||||
@@ -237,4 +237,5 @@ proc reprAny(p: pointer, typ: PNimType): string {.compilerRtl.} =
|
||||
var cl: ReprClosure
|
||||
initReprClosure(cl)
|
||||
reprAux(result, p, typ, cl)
|
||||
add(result, "\n")
|
||||
when defined(nimLegacyReprWithNewline): # see PR #16034
|
||||
add result, "\n"
|
||||
|
||||
@@ -11,6 +11,9 @@ Obj(member: ref @["hello"])
|
||||
ref (member: ref @["hello"])
|
||||
'''
|
||||
"""
|
||||
|
||||
# xxx consider merging with `tests/stdlib/trepr.nim` to increase overall test coverage
|
||||
|
||||
import tables
|
||||
|
||||
type
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
discard """
|
||||
output: '''
|
||||
[4, 5, 6]
|
||||
|
||||
[16, 25, 36]
|
||||
|
||||
[16, 25, 36]
|
||||
|
||||
apple
|
||||
banana
|
||||
Fruit
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
discard """
|
||||
action: run
|
||||
"""
|
||||
# xxx consider merging with `tests/stdlib/trepr.nim` to increase overall test coverage
|
||||
|
||||
block ints:
|
||||
let
|
||||
@@ -137,15 +135,13 @@ block tuples:
|
||||
when defined js:
|
||||
doAssert(repr(ot) == """
|
||||
[Field0 = true,
|
||||
Field1 = 120]
|
||||
""")
|
||||
Field1 = 120]""")
|
||||
doAssert(repr(t) == """
|
||||
[Field0 = 42,
|
||||
Field1 = 12.34,
|
||||
Field2 = "tuple",
|
||||
Field3 = [Field0 = true,
|
||||
Field1 = 120]]
|
||||
""")
|
||||
Field1 = 120]]""")
|
||||
|
||||
block objects:
|
||||
type
|
||||
@@ -162,14 +158,12 @@ block objects:
|
||||
|
||||
doAssert(repr(oo) == """
|
||||
[a = true,
|
||||
b = 120]
|
||||
""")
|
||||
b = 120]""")
|
||||
doAssert(repr(o) == """
|
||||
[a = 42,
|
||||
b = 12.34,
|
||||
c = [a = true,
|
||||
b = 120]]
|
||||
""")
|
||||
b = 120]]""")
|
||||
|
||||
block arrays:
|
||||
type
|
||||
@@ -183,15 +177,14 @@ block arrays:
|
||||
c = [o, o, o]
|
||||
d = ["hi", "array", "!"]
|
||||
|
||||
doAssert(repr(a) == "[0.0, 1.0, 2.0]\n")
|
||||
doAssert(repr(b) == "[[0.0, 1.0, 2.0], [0.0, 1.0, 2.0], [0.0, 1.0, 2.0]]\n")
|
||||
doAssert(repr(a) == "[0.0, 1.0, 2.0]")
|
||||
doAssert(repr(b) == "[[0.0, 1.0, 2.0], [0.0, 1.0, 2.0], [0.0, 1.0, 2.0]]")
|
||||
doAssert(repr(c) == """
|
||||
[[x = 42,
|
||||
y = [0.0, 1.0, 2.0]], [x = 42,
|
||||
y = [0.0, 1.0, 2.0]], [x = 42,
|
||||
y = [0.0, 1.0, 2.0]]]
|
||||
""")
|
||||
doAssert(repr(d) == "[\"hi\", \"array\", \"!\"]\n")
|
||||
y = [0.0, 1.0, 2.0]]]""")
|
||||
doAssert(repr(d) == "[\"hi\", \"array\", \"!\"]")
|
||||
|
||||
block seqs:
|
||||
type
|
||||
@@ -205,15 +198,14 @@ block seqs:
|
||||
c = @[o, o, o]
|
||||
d = @["hi", "array", "!"]
|
||||
|
||||
doAssert(repr(a) == "@[0.0, 1.0, 2.0]\n")
|
||||
doAssert(repr(b) == "@[@[0.0, 1.0, 2.0], @[0.0, 1.0, 2.0], @[0.0, 1.0, 2.0]]\n")
|
||||
doAssert(repr(a) == "@[0.0, 1.0, 2.0]")
|
||||
doAssert(repr(b) == "@[@[0.0, 1.0, 2.0], @[0.0, 1.0, 2.0], @[0.0, 1.0, 2.0]]")
|
||||
doAssert(repr(c) == """
|
||||
@[[x = 42,
|
||||
y = @[0.0, 1.0, 2.0]], [x = 42,
|
||||
y = @[0.0, 1.0, 2.0]], [x = 42,
|
||||
y = @[0.0, 1.0, 2.0]]]
|
||||
""")
|
||||
doAssert(repr(d) == "@[\"hi\", \"array\", \"!\"]\n")
|
||||
y = @[0.0, 1.0, 2.0]]]""")
|
||||
doAssert(repr(d) == "@[\"hi\", \"array\", \"!\"]")
|
||||
|
||||
block ptrs:
|
||||
type
|
||||
@@ -226,13 +218,12 @@ block ptrs:
|
||||
c = addr a[2]
|
||||
d = AObj()
|
||||
|
||||
doAssert(repr(a) == "[12.0, 13.0, 14.0]\n")
|
||||
doAssert(repr(b) == "ref 0 --> 12.0\n")
|
||||
doAssert(repr(c) == "ref 2 --> 14.0\n")
|
||||
doAssert(repr(a) == "[12.0, 13.0, 14.0]")
|
||||
doAssert(repr(b) == "ref 0 --> 12.0")
|
||||
doAssert(repr(c) == "ref 2 --> 14.0")
|
||||
doAssert(repr(d) == """
|
||||
[x = nil,
|
||||
y = 0]
|
||||
""")
|
||||
y = 0]""")
|
||||
|
||||
block ptrs:
|
||||
type
|
||||
@@ -248,8 +239,7 @@ block ptrs:
|
||||
[x = ref 0 --> [[x = nil,
|
||||
y = 0], [x = nil,
|
||||
y = 0]],
|
||||
y = 0]
|
||||
""")
|
||||
y = 0]""")
|
||||
|
||||
block procs:
|
||||
proc test(): int =
|
||||
@@ -258,9 +248,9 @@ block procs:
|
||||
ptest = test
|
||||
nilproc: proc(): int
|
||||
|
||||
doAssert(repr(test) == "0\n")
|
||||
doAssert(repr(ptest) == "0\n")
|
||||
doAssert(repr(nilproc) == "nil\n")
|
||||
doAssert(repr(test) == "0")
|
||||
doAssert(repr(ptest) == "0")
|
||||
doAssert(repr(nilproc) == "nil")
|
||||
|
||||
block bunch:
|
||||
type
|
||||
@@ -322,8 +312,7 @@ o = [Field0 = [a = "",
|
||||
b = @[]],
|
||||
Field1 = ""],
|
||||
p = nil,
|
||||
q = nil]
|
||||
""")
|
||||
q = nil]""")
|
||||
doAssert(repr(cc) == """
|
||||
[a = 12,
|
||||
b = 1,
|
||||
@@ -346,8 +335,7 @@ o = [Field0 = [a = "inner",
|
||||
b = @['o', 'b', 'j']],
|
||||
Field1 = "tuple!"],
|
||||
p = 0,
|
||||
q = "cstringtest"]
|
||||
""")
|
||||
q = "cstringtest"]""")
|
||||
|
||||
block another:
|
||||
type
|
||||
@@ -358,9 +346,9 @@ block another:
|
||||
Size3 = enum
|
||||
s3e=0, s3f=2000000000
|
||||
|
||||
doAssert(repr([s1a, s1b]) == "[s1a, s1b]\n")
|
||||
doAssert(repr([s2c, s2d]) == "[s2c, s2d]\n")
|
||||
doAssert(repr([s3e, s3f]) == "[s3e, s3f]\n")
|
||||
doAssert(repr([s1a, s1b]) == "[s1a, s1b]")
|
||||
doAssert(repr([s2c, s2d]) == "[s2c, s2d]")
|
||||
doAssert(repr([s3e, s3f]) == "[s3e, s3f]")
|
||||
|
||||
block another2:
|
||||
|
||||
@@ -395,15 +383,13 @@ block another2:
|
||||
y = 13,
|
||||
z = 45,
|
||||
s = ["abc", "xyz"],
|
||||
e = en6]
|
||||
""")
|
||||
e = en6]""")
|
||||
doAssert(repr(q) == """
|
||||
ref 0 --> [x = 0,
|
||||
y = 13,
|
||||
z = 45,
|
||||
s = ["abc", "xyz"],
|
||||
e = en6]
|
||||
""")
|
||||
e = en6]""")
|
||||
doAssert(repr(s) == """
|
||||
@[ref 0 --> [x = 0,
|
||||
y = 13,
|
||||
@@ -421,8 +407,7 @@ e = en6], ref 3 --> [x = 0,
|
||||
y = 13,
|
||||
z = 45,
|
||||
s = ["abc", "xyz"],
|
||||
e = en6]]
|
||||
""")
|
||||
e = en6]]""")
|
||||
doAssert(repr(en4) == "en4")
|
||||
|
||||
doAssert(repr({'a'..'p'}) == "{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'}")
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
discard """
|
||||
output: '''[1, 0, 0, 0, 0, 0, 0, 0]
|
||||
CTBool[Ct[system.uint32]]'''
|
||||
output: '''[1, 0, 0, 0, 0, 0, 0, 0] CTBool[Ct[system.uint32]]'''
|
||||
"""
|
||||
|
||||
block tconstraints:
|
||||
|
||||
@@ -67,8 +67,7 @@ let limit = 1'u64
|
||||
let rangeVar = 0'u64 ..< limit
|
||||
|
||||
doAssert repr(rangeVar) == """[a = 0,
|
||||
b = 0]
|
||||
"""
|
||||
b = 0]"""
|
||||
|
||||
# bug #15210
|
||||
|
||||
|
||||
@@ -1,36 +1,69 @@
|
||||
discard """
|
||||
output: '''{a, b}{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}
|
||||
[1, 2, 3, 4, 5, 6]'''
|
||||
targets: "c cpp js"
|
||||
matrix: ";--gc:arc"
|
||||
"""
|
||||
|
||||
type
|
||||
TEnum = enum
|
||||
a, b
|
||||
# if excessive, could remove 'cpp' from targets
|
||||
|
||||
var val = {a, b}
|
||||
stdout.write(repr(val))
|
||||
stdout.writeLine(repr({'a'..'z', 'A'..'Z'}))
|
||||
from strutils import endsWith, contains
|
||||
|
||||
type
|
||||
TObj {.pure, inheritable.} = object
|
||||
data: int
|
||||
TFoo = ref object of TObj
|
||||
d2: float
|
||||
var foo: TFoo
|
||||
new(foo)
|
||||
template main() =
|
||||
doAssert repr({3,5}) == "{3, 5}"
|
||||
|
||||
when false:
|
||||
# cannot capture this output as it contains a memory address :-/
|
||||
echo foo.repr
|
||||
#var testseq: seq[string] = @[
|
||||
# "a", "b", "c", "d", "e"
|
||||
#]
|
||||
#echo(repr(testseq))
|
||||
block:
|
||||
type TEnum = enum a, b
|
||||
var val = {a, b}
|
||||
when nimvm:
|
||||
discard
|
||||
#[
|
||||
# BUG:
|
||||
{0, 1}
|
||||
{97..99, 65..67}
|
||||
]#
|
||||
else:
|
||||
doAssert repr(val) == "{a, b}"
|
||||
doAssert repr({'a'..'c', 'A'..'C'}) == "{'A', 'B', 'C', 'a', 'b', 'c'}"
|
||||
|
||||
# bug #7878
|
||||
proc test(variable: var openarray[int]) =
|
||||
echo repr(variable)
|
||||
type
|
||||
TObj {.pure, inheritable.} = object
|
||||
data: int
|
||||
TFoo = ref object of TObj
|
||||
d2: float
|
||||
var foo: TFoo
|
||||
new(foo)
|
||||
|
||||
var arr = [1, 2, 3, 4, 5, 6]
|
||||
#[
|
||||
BUG:
|
||||
--gc:arc returns `"abc"`
|
||||
regular gc returns with address, e.g. 0x1068aae60"abc", but only
|
||||
for c,cpp backends (not js, vm)
|
||||
]#
|
||||
block:
|
||||
doAssert repr("abc").endsWith "\"abc\""
|
||||
var b: cstring = "def"
|
||||
doAssert repr(b).endsWith "\"def\""
|
||||
|
||||
test(arr)
|
||||
block:
|
||||
var c = @[1,2]
|
||||
when nimvm:
|
||||
discard # BUG: this shows [1, 2] instead of @[1, 2]
|
||||
else:
|
||||
# BUG (already mentioned above): some backends / gc show address, others don't
|
||||
doAssert repr(c).endsWith "@[1, 2]"
|
||||
|
||||
let d = @["foo", "bar"]
|
||||
let s = repr(d)
|
||||
# depending on backend/gc, we get 0x106a1c350@[0x106a1c390"foo", 0x106a1c3c0"bar"]
|
||||
doAssert "\"foo\"," in s
|
||||
|
||||
var arr = [1, 2, 3]
|
||||
doAssert repr(arr) == "[1, 2, 3]"
|
||||
|
||||
block: # bug #7878
|
||||
proc reprOpenarray(variable: var openarray[int]): string = repr(variable)
|
||||
when defined(js): discard # BUG: doesn't work
|
||||
else:
|
||||
doAssert reprOpenarray(arr) == "[1, 2, 3]"
|
||||
|
||||
static: main()
|
||||
main()
|
||||
|
||||
@@ -4,7 +4,6 @@ discard """
|
||||
Hello! What is your name?
|
||||
Nice name: Arne
|
||||
fs is: nil
|
||||
|
||||
threw exception
|
||||
_heh_
|
||||
'''
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
discard """
|
||||
output: '''[127, 127, 0, 255]
|
||||
[127, 127, 0, 255]
|
||||
|
||||
output: '''
|
||||
[127, 127, 0, 255][127, 127, 0, 255]
|
||||
(data: 1)
|
||||
'''
|
||||
|
||||
|
||||
Reference in New Issue
Block a user