This commit is contained in:
parent
1efd11e266
commit
b3c3557c33
12 changed files with 105 additions and 84 deletions
|
|
@ -44,6 +44,8 @@
|
||||||
|
|
||||||
- `os.FileInfo` (returned by `getFileInfo`) now contains `blockSize`,
|
- `os.FileInfo` (returned by `getFileInfo`) now contains `blockSize`,
|
||||||
determining preferred I/O block size for this file object.
|
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
|
## Language changes
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1334,6 +1334,9 @@ proc insert*[T](x: var seq[T], item: sink T, i = 0.Natural) {.noSideEffect.} =
|
||||||
when not defined(nimV2):
|
when not defined(nimV2):
|
||||||
proc repr*[T](x: T): string {.magic: "Repr", noSideEffect.}
|
proc repr*[T](x: T): string {.magic: "Repr", noSideEffect.}
|
||||||
## Takes any Nim variable and returns its string representation.
|
## 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
|
## It works even for complex data graphs with cycles. This is a great
|
||||||
## debugging tool.
|
## debugging tool.
|
||||||
|
|
|
||||||
|
|
@ -325,5 +325,6 @@ when not defined(useNimRtl):
|
||||||
else:
|
else:
|
||||||
var p = p
|
var p = p
|
||||||
reprAux(result, addr(p), typ, cl)
|
reprAux(result, addr(p), typ, cl)
|
||||||
|
when defined(nimLegacyReprWithNewline): # see PR #16034
|
||||||
add result, "\n"
|
add result, "\n"
|
||||||
deinitReprClosure(cl)
|
deinitReprClosure(cl)
|
||||||
|
|
|
||||||
|
|
@ -237,4 +237,5 @@ proc reprAny(p: pointer, typ: PNimType): string {.compilerRtl.} =
|
||||||
var cl: ReprClosure
|
var cl: ReprClosure
|
||||||
initReprClosure(cl)
|
initReprClosure(cl)
|
||||||
reprAux(result, p, typ, 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"])
|
ref (member: ref @["hello"])
|
||||||
'''
|
'''
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# xxx consider merging with `tests/stdlib/trepr.nim` to increase overall test coverage
|
||||||
|
|
||||||
import tables
|
import tables
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,8 @@
|
||||||
discard """
|
discard """
|
||||||
output: '''
|
output: '''
|
||||||
[4, 5, 6]
|
[4, 5, 6]
|
||||||
|
|
||||||
[16, 25, 36]
|
[16, 25, 36]
|
||||||
|
|
||||||
[16, 25, 36]
|
[16, 25, 36]
|
||||||
|
|
||||||
apple
|
apple
|
||||||
banana
|
banana
|
||||||
Fruit
|
Fruit
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
discard """
|
# xxx consider merging with `tests/stdlib/trepr.nim` to increase overall test coverage
|
||||||
action: run
|
|
||||||
"""
|
|
||||||
|
|
||||||
block ints:
|
block ints:
|
||||||
let
|
let
|
||||||
|
|
@ -137,15 +135,13 @@ block tuples:
|
||||||
when defined js:
|
when defined js:
|
||||||
doAssert(repr(ot) == """
|
doAssert(repr(ot) == """
|
||||||
[Field0 = true,
|
[Field0 = true,
|
||||||
Field1 = 120]
|
Field1 = 120]""")
|
||||||
""")
|
|
||||||
doAssert(repr(t) == """
|
doAssert(repr(t) == """
|
||||||
[Field0 = 42,
|
[Field0 = 42,
|
||||||
Field1 = 12.34,
|
Field1 = 12.34,
|
||||||
Field2 = "tuple",
|
Field2 = "tuple",
|
||||||
Field3 = [Field0 = true,
|
Field3 = [Field0 = true,
|
||||||
Field1 = 120]]
|
Field1 = 120]]""")
|
||||||
""")
|
|
||||||
|
|
||||||
block objects:
|
block objects:
|
||||||
type
|
type
|
||||||
|
|
@ -162,14 +158,12 @@ block objects:
|
||||||
|
|
||||||
doAssert(repr(oo) == """
|
doAssert(repr(oo) == """
|
||||||
[a = true,
|
[a = true,
|
||||||
b = 120]
|
b = 120]""")
|
||||||
""")
|
|
||||||
doAssert(repr(o) == """
|
doAssert(repr(o) == """
|
||||||
[a = 42,
|
[a = 42,
|
||||||
b = 12.34,
|
b = 12.34,
|
||||||
c = [a = true,
|
c = [a = true,
|
||||||
b = 120]]
|
b = 120]]""")
|
||||||
""")
|
|
||||||
|
|
||||||
block arrays:
|
block arrays:
|
||||||
type
|
type
|
||||||
|
|
@ -183,15 +177,14 @@ block arrays:
|
||||||
c = [o, o, o]
|
c = [o, o, o]
|
||||||
d = ["hi", "array", "!"]
|
d = ["hi", "array", "!"]
|
||||||
|
|
||||||
doAssert(repr(a) == "[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]]\n")
|
doAssert(repr(b) == "[[0.0, 1.0, 2.0], [0.0, 1.0, 2.0], [0.0, 1.0, 2.0]]")
|
||||||
doAssert(repr(c) == """
|
doAssert(repr(c) == """
|
||||||
[[x = 42,
|
[[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]], [x = 42,
|
y = [0.0, 1.0, 2.0]], [x = 42,
|
||||||
y = [0.0, 1.0, 2.0]]]
|
y = [0.0, 1.0, 2.0]]]""")
|
||||||
""")
|
doAssert(repr(d) == "[\"hi\", \"array\", \"!\"]")
|
||||||
doAssert(repr(d) == "[\"hi\", \"array\", \"!\"]\n")
|
|
||||||
|
|
||||||
block seqs:
|
block seqs:
|
||||||
type
|
type
|
||||||
|
|
@ -205,15 +198,14 @@ block seqs:
|
||||||
c = @[o, o, o]
|
c = @[o, o, o]
|
||||||
d = @["hi", "array", "!"]
|
d = @["hi", "array", "!"]
|
||||||
|
|
||||||
doAssert(repr(a) == "@[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]]\n")
|
doAssert(repr(b) == "@[@[0.0, 1.0, 2.0], @[0.0, 1.0, 2.0], @[0.0, 1.0, 2.0]]")
|
||||||
doAssert(repr(c) == """
|
doAssert(repr(c) == """
|
||||||
@[[x = 42,
|
@[[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]], [x = 42,
|
y = @[0.0, 1.0, 2.0]], [x = 42,
|
||||||
y = @[0.0, 1.0, 2.0]]]
|
y = @[0.0, 1.0, 2.0]]]""")
|
||||||
""")
|
doAssert(repr(d) == "@[\"hi\", \"array\", \"!\"]")
|
||||||
doAssert(repr(d) == "@[\"hi\", \"array\", \"!\"]\n")
|
|
||||||
|
|
||||||
block ptrs:
|
block ptrs:
|
||||||
type
|
type
|
||||||
|
|
@ -226,13 +218,12 @@ block ptrs:
|
||||||
c = addr a[2]
|
c = addr a[2]
|
||||||
d = AObj()
|
d = AObj()
|
||||||
|
|
||||||
doAssert(repr(a) == "[12.0, 13.0, 14.0]\n")
|
doAssert(repr(a) == "[12.0, 13.0, 14.0]")
|
||||||
doAssert(repr(b) == "ref 0 --> 12.0\n")
|
doAssert(repr(b) == "ref 0 --> 12.0")
|
||||||
doAssert(repr(c) == "ref 2 --> 14.0\n")
|
doAssert(repr(c) == "ref 2 --> 14.0")
|
||||||
doAssert(repr(d) == """
|
doAssert(repr(d) == """
|
||||||
[x = nil,
|
[x = nil,
|
||||||
y = 0]
|
y = 0]""")
|
||||||
""")
|
|
||||||
|
|
||||||
block ptrs:
|
block ptrs:
|
||||||
type
|
type
|
||||||
|
|
@ -248,8 +239,7 @@ block ptrs:
|
||||||
[x = ref 0 --> [[x = nil,
|
[x = ref 0 --> [[x = nil,
|
||||||
y = 0], [x = nil,
|
y = 0], [x = nil,
|
||||||
y = 0]],
|
y = 0]],
|
||||||
y = 0]
|
y = 0]""")
|
||||||
""")
|
|
||||||
|
|
||||||
block procs:
|
block procs:
|
||||||
proc test(): int =
|
proc test(): int =
|
||||||
|
|
@ -258,9 +248,9 @@ block procs:
|
||||||
ptest = test
|
ptest = test
|
||||||
nilproc: proc(): int
|
nilproc: proc(): int
|
||||||
|
|
||||||
doAssert(repr(test) == "0\n")
|
doAssert(repr(test) == "0")
|
||||||
doAssert(repr(ptest) == "0\n")
|
doAssert(repr(ptest) == "0")
|
||||||
doAssert(repr(nilproc) == "nil\n")
|
doAssert(repr(nilproc) == "nil")
|
||||||
|
|
||||||
block bunch:
|
block bunch:
|
||||||
type
|
type
|
||||||
|
|
@ -322,8 +312,7 @@ o = [Field0 = [a = "",
|
||||||
b = @[]],
|
b = @[]],
|
||||||
Field1 = ""],
|
Field1 = ""],
|
||||||
p = nil,
|
p = nil,
|
||||||
q = nil]
|
q = nil]""")
|
||||||
""")
|
|
||||||
doAssert(repr(cc) == """
|
doAssert(repr(cc) == """
|
||||||
[a = 12,
|
[a = 12,
|
||||||
b = 1,
|
b = 1,
|
||||||
|
|
@ -346,8 +335,7 @@ o = [Field0 = [a = "inner",
|
||||||
b = @['o', 'b', 'j']],
|
b = @['o', 'b', 'j']],
|
||||||
Field1 = "tuple!"],
|
Field1 = "tuple!"],
|
||||||
p = 0,
|
p = 0,
|
||||||
q = "cstringtest"]
|
q = "cstringtest"]""")
|
||||||
""")
|
|
||||||
|
|
||||||
block another:
|
block another:
|
||||||
type
|
type
|
||||||
|
|
@ -358,9 +346,9 @@ block another:
|
||||||
Size3 = enum
|
Size3 = enum
|
||||||
s3e=0, s3f=2000000000
|
s3e=0, s3f=2000000000
|
||||||
|
|
||||||
doAssert(repr([s1a, s1b]) == "[s1a, s1b]\n")
|
doAssert(repr([s1a, s1b]) == "[s1a, s1b]")
|
||||||
doAssert(repr([s2c, s2d]) == "[s2c, s2d]\n")
|
doAssert(repr([s2c, s2d]) == "[s2c, s2d]")
|
||||||
doAssert(repr([s3e, s3f]) == "[s3e, s3f]\n")
|
doAssert(repr([s3e, s3f]) == "[s3e, s3f]")
|
||||||
|
|
||||||
block another2:
|
block another2:
|
||||||
|
|
||||||
|
|
@ -395,15 +383,13 @@ block another2:
|
||||||
y = 13,
|
y = 13,
|
||||||
z = 45,
|
z = 45,
|
||||||
s = ["abc", "xyz"],
|
s = ["abc", "xyz"],
|
||||||
e = en6]
|
e = en6]""")
|
||||||
""")
|
|
||||||
doAssert(repr(q) == """
|
doAssert(repr(q) == """
|
||||||
ref 0 --> [x = 0,
|
ref 0 --> [x = 0,
|
||||||
y = 13,
|
y = 13,
|
||||||
z = 45,
|
z = 45,
|
||||||
s = ["abc", "xyz"],
|
s = ["abc", "xyz"],
|
||||||
e = en6]
|
e = en6]""")
|
||||||
""")
|
|
||||||
doAssert(repr(s) == """
|
doAssert(repr(s) == """
|
||||||
@[ref 0 --> [x = 0,
|
@[ref 0 --> [x = 0,
|
||||||
y = 13,
|
y = 13,
|
||||||
|
|
@ -421,8 +407,7 @@ e = en6], ref 3 --> [x = 0,
|
||||||
y = 13,
|
y = 13,
|
||||||
z = 45,
|
z = 45,
|
||||||
s = ["abc", "xyz"],
|
s = ["abc", "xyz"],
|
||||||
e = en6]]
|
e = en6]]""")
|
||||||
""")
|
|
||||||
doAssert(repr(en4) == "en4")
|
doAssert(repr(en4) == "en4")
|
||||||
|
|
||||||
doAssert(repr({'a'..'p'}) == "{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'}")
|
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 """
|
discard """
|
||||||
output: '''[1, 0, 0, 0, 0, 0, 0, 0]
|
output: '''[1, 0, 0, 0, 0, 0, 0, 0] CTBool[Ct[system.uint32]]'''
|
||||||
CTBool[Ct[system.uint32]]'''
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
block tconstraints:
|
block tconstraints:
|
||||||
|
|
|
||||||
|
|
@ -67,8 +67,7 @@ let limit = 1'u64
|
||||||
let rangeVar = 0'u64 ..< limit
|
let rangeVar = 0'u64 ..< limit
|
||||||
|
|
||||||
doAssert repr(rangeVar) == """[a = 0,
|
doAssert repr(rangeVar) == """[a = 0,
|
||||||
b = 0]
|
b = 0]"""
|
||||||
"""
|
|
||||||
|
|
||||||
# bug #15210
|
# bug #15210
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,36 +1,69 @@
|
||||||
discard """
|
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'}
|
targets: "c cpp js"
|
||||||
[1, 2, 3, 4, 5, 6]'''
|
matrix: ";--gc:arc"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
type
|
# if excessive, could remove 'cpp' from targets
|
||||||
TEnum = enum
|
|
||||||
a, b
|
|
||||||
|
|
||||||
var val = {a, b}
|
from strutils import endsWith, contains
|
||||||
stdout.write(repr(val))
|
|
||||||
stdout.writeLine(repr({'a'..'z', 'A'..'Z'}))
|
|
||||||
|
|
||||||
type
|
template main() =
|
||||||
|
doAssert repr({3,5}) == "{3, 5}"
|
||||||
|
|
||||||
|
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'}"
|
||||||
|
|
||||||
|
type
|
||||||
TObj {.pure, inheritable.} = object
|
TObj {.pure, inheritable.} = object
|
||||||
data: int
|
data: int
|
||||||
TFoo = ref object of TObj
|
TFoo = ref object of TObj
|
||||||
d2: float
|
d2: float
|
||||||
var foo: TFoo
|
var foo: TFoo
|
||||||
new(foo)
|
new(foo)
|
||||||
|
|
||||||
when false:
|
#[
|
||||||
# cannot capture this output as it contains a memory address :-/
|
BUG:
|
||||||
echo foo.repr
|
--gc:arc returns `"abc"`
|
||||||
#var testseq: seq[string] = @[
|
regular gc returns with address, e.g. 0x1068aae60"abc", but only
|
||||||
# "a", "b", "c", "d", "e"
|
for c,cpp backends (not js, vm)
|
||||||
#]
|
]#
|
||||||
#echo(repr(testseq))
|
block:
|
||||||
|
doAssert repr("abc").endsWith "\"abc\""
|
||||||
|
var b: cstring = "def"
|
||||||
|
doAssert repr(b).endsWith "\"def\""
|
||||||
|
|
||||||
# bug #7878
|
block:
|
||||||
proc test(variable: var openarray[int]) =
|
var c = @[1,2]
|
||||||
echo repr(variable)
|
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]"
|
||||||
|
|
||||||
var arr = [1, 2, 3, 4, 5, 6]
|
let d = @["foo", "bar"]
|
||||||
|
let s = repr(d)
|
||||||
|
# depending on backend/gc, we get 0x106a1c350@[0x106a1c390"foo", 0x106a1c3c0"bar"]
|
||||||
|
doAssert "\"foo\"," in s
|
||||||
|
|
||||||
test(arr)
|
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?
|
Hello! What is your name?
|
||||||
Nice name: Arne
|
Nice name: Arne
|
||||||
fs is: nil
|
fs is: nil
|
||||||
|
|
||||||
threw exception
|
threw exception
|
||||||
_heh_
|
_heh_
|
||||||
'''
|
'''
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
discard """
|
discard """
|
||||||
output: '''[127, 127, 0, 255]
|
output: '''
|
||||||
[127, 127, 0, 255]
|
[127, 127, 0, 255][127, 127, 0, 255]
|
||||||
|
|
||||||
(data: 1)
|
(data: 1)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue