aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Möhrmann <moehrmann@google.com>2016-12-11 12:55:17 +0100
committerRob Pike <r@golang.org>2016-12-11 21:59:59 +0000
commitcbcc1db41c505cd8e19e27f9844276dc35d527be (patch)
treecb5b19ec58158a9f3ce3e407e1cb67d26a598411
parentab5a2173f91c1e2779cdf49a2fc8a7abafecd5f1 (diff)
downloadgo-cbcc1db41c505cd8e19e27f9844276dc35d527be.tar.xz
fmt: undo clearflags in catchPanic after error message has been printed
Fixes #18282 Change-Id: I024ca4a03bbbcccd48a0a6245bc3ec22c6a90288 Reviewed-on: https://go-review.googlesource.com/34254 TryBot-Result: Gobot Gobot <gobot@golang.org> Run-TryBot: Martin Möhrmann <moehrmann@google.com> Reviewed-by: Rob Pike <r@golang.org>
-rw-r--r--src/fmt/fmt_test.go27
-rw-r--r--src/fmt/print.go8
2 files changed, 24 insertions, 11 deletions
diff --git a/src/fmt/fmt_test.go b/src/fmt/fmt_test.go
index 6f8c1550a0..b7089be1a1 100644
--- a/src/fmt/fmt_test.go
+++ b/src/fmt/fmt_test.go
@@ -1561,18 +1561,23 @@ func TestWidthAndPrecision(t *testing.T) {
}
}
-// Panic is a type that panics in String.
-type Panic struct {
+// PanicS is a type that panics in String.
+type PanicS struct {
message interface{}
}
// Value receiver.
-func (p Panic) GoString() string {
+func (p PanicS) String() string {
panic(p.message)
}
+// PanicGo is a type that panics in GoString.
+type PanicGo struct {
+ message interface{}
+}
+
// Value receiver.
-func (p Panic) String() string {
+func (p PanicGo) GoString() string {
panic(p.message)
}
@@ -1592,13 +1597,15 @@ var panictests = []struct {
out string
}{
// String
- {"%s", (*Panic)(nil), "<nil>"}, // nil pointer special case
- {"%s", Panic{io.ErrUnexpectedEOF}, "%!s(PANIC=unexpected EOF)"},
- {"%s", Panic{3}, "%!s(PANIC=3)"},
+ {"%s", (*PanicS)(nil), "<nil>"}, // nil pointer special case
+ {"%s", PanicS{io.ErrUnexpectedEOF}, "%!s(PANIC=unexpected EOF)"},
+ {"%s", PanicS{3}, "%!s(PANIC=3)"},
// GoString
- {"%#v", (*Panic)(nil), "<nil>"}, // nil pointer special case
- {"%#v", Panic{io.ErrUnexpectedEOF}, "%!v(PANIC=unexpected EOF)"},
- {"%#v", Panic{3}, "%!v(PANIC=3)"},
+ {"%#v", (*PanicGo)(nil), "<nil>"}, // nil pointer special case
+ {"%#v", PanicGo{io.ErrUnexpectedEOF}, "%!v(PANIC=unexpected EOF)"},
+ {"%#v", PanicGo{3}, "%!v(PANIC=3)"},
+ // Issue 18282. catchPanic should not clear fmtFlags permanently.
+ {"%#v", []interface{}{PanicGo{3}, PanicGo{3}}, "[]interface {}{%!v(PANIC=3), %!v(PANIC=3)}"},
// Format
{"%s", (*PanicF)(nil), "<nil>"}, // nil pointer special case
{"%s", PanicF{io.ErrUnexpectedEOF}, "%!s(PANIC=unexpected EOF)"},
diff --git a/src/fmt/print.go b/src/fmt/print.go
index a95edacc99..a7ef2e5ac2 100644
--- a/src/fmt/print.go
+++ b/src/fmt/print.go
@@ -535,7 +535,11 @@ func (p *pp) catchPanic(arg interface{}, verb rune) {
// Nested panics; the recursion in printArg cannot succeed.
panic(err)
}
- p.fmt.clearflags() // We are done, and for this output we want default behavior.
+
+ oldFlags := p.fmt.fmtFlags
+ // For this output we want default behavior.
+ p.fmt.clearflags()
+
p.buf.WriteString(percentBangString)
p.buf.WriteRune(verb)
p.buf.WriteString(panicString)
@@ -543,6 +547,8 @@ func (p *pp) catchPanic(arg interface{}, verb rune) {
p.printArg(err, 'v')
p.panicking = false
p.buf.WriteByte(')')
+
+ p.fmt.fmtFlags = oldFlags
}
}