Merge pull request '[agent/codex:gpt-5.3-codex-spark] Read docs/RFC.md fully. Find ONE feature described in the sp...' (#5) from main into dev
This commit is contained in:
commit
ae24368686
3 changed files with 74 additions and 0 deletions
38
call_test.go
38
call_test.go
|
|
@ -1,6 +1,8 @@
|
|||
package cgo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"syscall"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
|
@ -49,3 +51,39 @@ func TestCallRejectsUnsupportedInputs(t *testing.T) {
|
|||
_ = Call(callFailureFunction(), 1, 2, 3, 4)
|
||||
})
|
||||
}
|
||||
|
||||
func TestErrnoMapping(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if err := Errno(0); err != nil {
|
||||
t.Fatalf("expected nil error for zero, got %v", err)
|
||||
}
|
||||
|
||||
if err := Errno(2); err == nil {
|
||||
t.Fatal("expected non-nil error for non-zero errno")
|
||||
} else if !errors.Is(err, syscall.Errno(2)) {
|
||||
t.Fatalf("expected error type %v, got %T %v", syscall.Errno(2), err, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithErrnoReturnsBothResultAndError(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
result, err := callWithErrnoZero()
|
||||
if result != 0 {
|
||||
t.Fatalf("expected result 0, got %d", result)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("expected nil error, got %v", err)
|
||||
}
|
||||
|
||||
result, err = callWithErrnoFailure()
|
||||
if result != 2 {
|
||||
t.Fatalf("expected result 2, got %d", result)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
} else if !errors.Is(err, syscall.Errno(2)) {
|
||||
t.Fatalf("expected errno error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,3 +36,15 @@ func callFailureFunction() unsafe.Pointer {
|
|||
function := C.call_failure_ptr()
|
||||
return *(*unsafe.Pointer)(unsafe.Pointer(&function))
|
||||
}
|
||||
|
||||
func callWithErrnoZero() (int, error) {
|
||||
return WithErrno(func() C.int {
|
||||
return 0
|
||||
})
|
||||
}
|
||||
|
||||
func callWithErrnoFailure() (int, error) {
|
||||
return WithErrno(func() C.int {
|
||||
return 2
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ import "C"
|
|||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
|
|
@ -196,3 +197,26 @@ func Free(ptr unsafe.Pointer) {
|
|||
}
|
||||
C.free(ptr)
|
||||
}
|
||||
|
||||
// Errno converts a C error number to a Go error.
|
||||
//
|
||||
// rc := cgo.Errno(-2)
|
||||
func Errno(rc C.int) error {
|
||||
if rc == 0 {
|
||||
return nil
|
||||
}
|
||||
return syscall.Errno(rc)
|
||||
}
|
||||
|
||||
// WithErrno runs a function that returns C.int and maps the result to Go error.
|
||||
//
|
||||
// result, err := cgo.WithErrno(func() C.int {
|
||||
// return C.my_function()
|
||||
// })
|
||||
func WithErrno(fn func() C.int) (int, error) {
|
||||
result := fn()
|
||||
if err := Errno(result); err != nil {
|
||||
return int(result), err
|
||||
}
|
||||
return int(result), nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue