-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfunction.go
101 lines (86 loc) · 2.96 KB
/
function.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2011 Julian Phillips. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package py
// #include "utils.h"
// static inline int functionCheck(PyObject *o) { return PyFunction_Check(o); }
import "C"
import "unsafe"
// *Function represents a Python function. In Python this is a function created
// using the "def" statement.
type Function struct {
AbstractObject
o C.PyFunctionObject
}
// FunctionType is the Type object that represents the Function type.
var FunctionType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyFunction_Type)))
func newFunction(obj *C.PyObject) *Function {
return (*Function)(unsafe.Pointer(obj))
}
func functionCheck(obj Object) bool {
if obj == nil {
return false
}
return C.functionCheck(c(obj)) != 0
}
// NewFunction returns a new Function object that is associated with the given
// "code" and "globals". "globals" must be a dictionary, and it should hold the
// global variables for the function.
//
// The function inherits its docstring, name and __module__ from the "code"
// object, the argument defaults and closure are initialised to nil.
//
// Return value: New Reference.
func NewFunction(code Object, globals Object) (*Function, error) {
ret := C.PyFunction_New(c(code), c(globals))
return newFunction(ret), nil
}
// Code returns the code object associated with the function "fn".
//
// Return value: Borrowed Reference.
func (fn *Function) Code() (Object, error) {
ret := C.PyFunction_GetCode(c(fn))
return obj2ObjErr(ret)
}
// Globals returns the globals dictionary associated with the function "fn".
//
// Return value: Borrowed Reference.
func (fn *Function) Globals() (Object, error) {
ret := C.PyFunction_GetGlobals(c(fn))
return obj2ObjErr(ret)
}
// Module returns the __module__ attribute of the function "fn".
//
// Return value: Borrowed Reference.
func (fn *Function) Module() (Object, error) {
ret := C.PyFunction_GetModule(c(fn))
return obj2ObjErr(ret)
}
// Defaults returns the argument default values for the function "fn". This may
// be nil or a Tuple of values.
//
// Return value: Borrowed Reference.
func (fn *Function) Defaults() (Object, error) {
ret := C.PyFunction_GetDefaults(c(fn))
return obj2ObjErr(ret)
}
// SetDefaults sets teh arguement default values for the function "fn". "o"
// must be either a Tuple, or None.
func (fn *Function) SetDefaults(o Object) error {
ret := C.PyFunction_SetDefaults(c(fn), c(o))
return int2Err(ret)
}
// Closure returns the closure associated with function "fn". This may be nil
// or a Tuple of Cell objects.
//
// Return value: Borrowed Reference.
func (fn *Function) Closure() (Object, error) {
ret := C.PyFunction_GetClosure(c(fn))
return obj2ObjErr(ret)
}
// SetClosure sets the closure associated with function "fn". "o" must be
// either a Tuple of Cell objects, or None.
func (fn *Function) SetClosure(o Object) error {
ret := C.PyFunction_SetClosure(c(fn), c(o))
return int2Err(ret)
}