Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image: broken example #4300

Closed
gopherbot opened this issue Oct 29, 2012 · 9 comments
Closed

image: broken example #4300

gopherbot opened this issue Oct 29, 2012 · 9 comments

Comments

@gopherbot
Copy link

by darkgray:

The top example at http://golang.org/pkg/image/#example_ gives the error:
2012/10/29 05:30:19 image: unknown format
exit status 1

It can be fixed by adding the import of:
_ "image/jpeg"
@robpike
Copy link
Contributor

robpike commented Oct 30, 2012

Comment 1:

Labels changed: added priority-soon, documentation, removed priority-triage.

Owner changed to @robpike.

Status changed to Accepted.

@robpike
Copy link
Contributor

robpike commented Oct 30, 2012

Comment 2:

Fixed at tip.

Status changed to Fixed.

@minux
Copy link
Member

minux commented Oct 30, 2012

Comment 3:

is this really fixed at tip?
i just tried, running the example still didn't work.

@robpike
Copy link
Contributor

robpike commented Oct 30, 2012

Comment 4:

The test runs in isolation and the source code has the import. I can't
get the executable format to work in tip godoc, so I can't test the
other case. How did you reproduce the failure?
-rob

@minux
Copy link
Member

minux commented Oct 31, 2012

Comment 5:

godoc -play -http :6060
then goto http://localhost:6060/pkg/image
click Example, the example shown is lacking the blank id. import of "image/jpeg".
https://golang.org/cl/6813061/ is created to fix that.
After that CL, we can change that example to use absolute path for data files,
and then that example should be able to at least run locally.
btw,
can we change cmd/godoc/play-local.go to run the requested file locally instead
of bouncing to play.golang.org? i think it can enable a new way of learning Go
standard packages.

@griesemer
Copy link
Contributor

Comment 6:

I don't know yet why the formatting is off - need to investigate.
That said, the patch https://golang.org/cl/6813061/ has other issues as well.
When running
godoc -play -http :6060
as suggested, _and_ with image/decode_example_test.go modified such that the import
section looks like this:
import (
    "fmt"
    "image"
    "log"
    "os"
    // Package image/jpeg is not used explicitly in the code below,
    // but is imported for its initialization side-effect, which allows
    // image.Decode to understand JPEG formatted images. Uncomment these
    // two lines to also understand GIF and PNG images:
    // _ "image/gif"
    _ "image/png"
    _ "image/jpeg"
)
(two last imports enabled but one), I get a crash:
2012/11/02 16:45:05 http: panic serving [::1]:49365: runtime error: invalid memory
address or nil pointer dereference
$GOROOT/src/pkg/net/http/server.go:601 (0x873c6)
    func.004: buf.Write(debug.Stack())
$GOROOT/src/pkg/runtime/panic.c:134 (0x3afb6)
    panic: reflect·call(d->fn, (byte*)d->args, d->siz);
$GOROOT/src/pkg/runtime/panic.c:384 (0x3b6a3)
    panicstring: runtime·panic(err);
$GOROOT/src/pkg/runtime/thread_darwin.c:427 (0x42408)
    sigpanic: runtime·panicstring("invalid memory address or nil pointer dereference");
$GOROOT/src/pkg/go/ast/ast.go:77 (0x10e2ac)
    (*CommentGroup).Pos: func (g *CommentGroup) Pos() token.Pos { return g.List[0].Pos() }
$GOROOT/src/pkg/go/doc/example.go:184 (0xc6c5f)
    playExample: if c.Pos() >= p.Doc.Pos() && c.Pos() < p.Doc.End() {
$GOROOT/src/pkg/go/doc/example.go:62 (0xc612d)
    Examples: Play:     playExample(file, f.Body),
$GOROOT/src/cmd/godoc/godoc.go:977 (0x12551)
    (*docServer).getPageInfo: examples = append(examples, doc.Examples(files...)...)
$GOROOT/src/cmd/godoc/godoc.go:1050 (0x12bf8)
    (*docServer).ServeHTTP: info := h.getPageInfo(abspath, relpath, mode)
$GOROOT/src/pkg/net/http/server.go:988 (0x7d831)
    (*ServeMux).ServeHTTP: h.ServeHTTP(w, r)
$GOROOT/src/cmd/godoc/main.go:96 (0x26983)
    func.016: h.ServeHTTP(w, req)
$GOROOT/src/pkg/net/http/server.go:729 (0x7c6ab)
    HandlerFunc.ServeHTTP: f(w, r)
$GOROOT/src/pkg/net/http/server.go:681 (0x7c35d)
    (*conn).serve: handler.ServeHTTP(w, w.req)
$GOROOT/src/pkg/runtime/proc.c:277 (0x3cb60)
    goexit: runtime·goexit(void)

Owner changed to @griesemer.

Status changed to Accepted.

@griesemer
Copy link
Contributor

Comment 8:

Status changed to Started.

@griesemer
Copy link
Contributor

Comment 9:

The reason for the misalignment is due to the change of filename in the position
information of the created example:
Because the created example is made up from nodes from different files (and nodes that
have no position information), the printer sees various transitions of file names in the
position information. For instance, when printing the imports, it sees the following
file name transitions (in the [] boxes):
import [->/src/pkg/image/format.go](>>>
    "fmt"
    "image"
    "log"
    "os"
    [/src/pkg/image/format.go->/src/pkg/image/decode_example_test.go]// Package image/jpeg is not used explicitly in the code below,
// but is imported for its initialization side-effect, which allows
// image.Decode to understand JPEG formatted images. Uncomment these
// two lines to also understand GIF and PNG images:
// _ "image/gif"
// _ "image/png"
_ "image/jpeg"<<<
[/src/pkg/image/decode_example_test.go->/src/pkg/image/format.go])
(The >>> and <<< markers show increases/decreases of indentation).
At the moment, each time the file name changes in the position information, the printer
assumes that the formatting state should be reset - basically it assumes that we are not
building ASTs that contain pieces of different files within a single declaration.
However this is the case here.
When the formatting state is reset, the indentation goes to zero; hence the
misalignment. In fact, before the end of the import declaration, the indentation goes to
-1, and in debug mode (go/printer/printer.go:22 debug = true) we get a respective panic.
The offensive line is go/printer/printer.go:228 p.indent = 0. Commenting that line out
will resolve the issue.
See: http://golang.org/cl/6849066 .

@griesemer
Copy link
Contributor

Comment 10:

This issue was closed by revision 80dcc43.

Status changed to Fixed.

@rsc rsc added this to the Go1.1 milestone Apr 14, 2015
@rsc rsc removed the go1.1 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants