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

add an example to setControlCHook #19416

Merged
merged 2 commits into from
Jan 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2320,6 +2320,15 @@ when notJSnotNims:
proc setControlCHook*(hook: proc () {.noconv.})
## Allows you to override the behaviour of your application when CTRL+C
## is pressed. Only one such hook is supported.
## Example:
##
## .. code-block:: Nim
## proc ctrlc() {.noconv.} =
## echo "Ctrl+C fired!"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this example is broken: ctrl-c hooks are signal handlers, and you can't (reliably) allocate memory inside them, which echo does - this is a common pitfall with using signal handlers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a new to Nim and I don't understand a lot, and when I wanted to catch Control-C I looked in system, and there was no example, but luckily I found one in stack overflow for Nim, otherwise I would have been stuck. It was suggested I make a PR to add the example, and this is the first PR I've make (and I had lots of hand-holding to do it from the Nim channel, to guide me step-by-step though the process), and so with that in mind, I'm confused...
I can do:

proc ctrlc() {.noconv.} =
  echo "Ctrl+C fired!"
  quit()

setControlCHook(ctrlc)

while true:
  discard

... and it does work?

## # do clean up stuff
## quit()
##
## setControlCHook(ctrlc)
Comment on lines +2323 to +2331
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the future: runnableExamples are preferred for examples, since they're tested when the documentation is generated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you - I was advised that in this case it would be tricky to send a ctrl-c for testing?

Copy link
Member

@ringabout ringabout Jan 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use runnableExamples("-r:off"). But runnableExamples seems not to be able to be used for forward declare.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you once again xflywind. I don't know what "But runnableExamples seems not to be used for forward declare" means?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It simply doesn't work for forward declare.

  proc setControlCHook*(hook: proc () {.noconv.})
    ## Allows you to override the behaviour of your application when CTRL+C
    ## is pressed. Only one such hook is supported.
    runnableExamples("-r:off"):
      proc ctrlc() {.noconv.} =
        echo "Ctrl+C fired!"
        # do clean up stuff
        quit()
      setControlCHook(ctrlc)

=> Error: invalid indentation


when not defined(noSignalHandler) and not defined(useNimRtl):
proc unsetControlCHook*()
Expand Down