pkg/eval: Don't use the new context.Cause API.

Elvish should still be buildable with Go 1.19.
This commit is contained in:
Qi Xiao 2023-05-07 22:37:08 +01:00
parent f54fe608e8
commit ae087dc1e4
5 changed files with 13 additions and 19 deletions

View File

@ -1,7 +1,6 @@
package eval
import (
"context"
"fmt"
"math"
"math/big"
@ -54,7 +53,7 @@ func sleep(fm *Frame, duration any) error {
select {
case <-fm.Context().Done():
return context.Cause(fm.Context())
return ErrInterrupted
case <-timeAfter(fm, d):
return nil
}

View File

@ -38,8 +38,8 @@ func (op chunkOp) exec(fm *Frame) Exception {
// Check for interrupts after the chunk.
// We also check for interrupts before each pipeline, so there is no
// need to check it before the chunk or after each pipeline.
if err := fm.CancelCause(); err != nil {
return fm.errorp(op, err)
if fm.Canceled() {
return fm.errorp(op, ErrInterrupted)
}
return nil
}
@ -68,8 +68,8 @@ type pipelineOp struct {
const pipelineChanBufferSize = 32
func (op *pipelineOp) exec(fm *Frame) Exception {
if err := fm.CancelCause(); err != nil {
return fm.errorp(op, err)
if fm.Canceled() {
return fm.errorp(op, ErrInterrupted)
}
if op.bg {

View File

@ -180,14 +180,13 @@ func (fm *Frame) Context() context.Context {
return fm.ctx
}
// CancelCause checks whether the Context of the Frame has been canceled, and if
// so, returns a non-nil error.
func (fm *Frame) CancelCause() error {
// Canceled reports whether the Context of the Frame has been canceled.
func (fm *Frame) Canceled() bool {
select {
case <-fm.ctx.Done():
return context.Cause(fm.ctx)
return true
default:
return nil
return false
}
}

View File

@ -253,7 +253,7 @@ func doGlob(ctx context.Context, gp globPattern) ([]any, error) {
}
return true
}) {
return nil, context.Cause(ctx)
return nil, ErrInterrupted
}
if len(vs) == 0 && !gp.Flags.Has(noMatchOK) {
return nil, ErrWildcardNoMatch

View File

@ -11,15 +11,11 @@ import (
// ErrInterrupted is thrown when the execution is interrupted by a signal.
var ErrInterrupted = errors.New("interrupted")
// Used to "cancel" a finished evaluation. It is a bug if this actually cancels
// anything.
var errEvalFinished = errors.New("internal bug, eval should have finished")
// ListenInterrupts returns a Context that is canceled when SIGINT or SIGQUIT
// has been received by the process. It also returns a function to cancel the
// Context, which should be called when it is no longer needed.
func ListenInterrupts() (context.Context, func()) {
ctx, cancel := context.WithCancelCause(context.Background())
ctx, cancel := context.WithCancel(context.Background())
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGQUIT)
@ -27,13 +23,13 @@ func ListenInterrupts() (context.Context, func()) {
go func() {
select {
case <-sigCh:
cancel(ErrInterrupted)
cancel()
case <-ctx.Done():
}
signal.Stop(sigCh)
}()
return ctx, func() {
cancel(errEvalFinished)
cancel()
}
}