MCPcopy Create free account
hub / github.com/rabbitstack/fibratus / NewPyObjectFromValue

Function NewPyObjectFromValue

pkg/filament/cpython/object.go:164–204  ·  view source on GitHub ↗

NewPyObjectFromValue builds a new Python object based on the underlying interface type.

(value interface{})

Source from the content-addressed store, hash-verified

162
163// NewPyObjectFromValue builds a new Python object based on the underlying interface type.
164func NewPyObjectFromValue(value interface{}) *PyObject {
165 var ob *C.PyObject
166 switch v := value.(type) {
167 case int8:
168 ob = C.PyChar_FromChar(C.i8(v))
169 case uint8:
170 ob = C.PyChar_FromUnsignedChar(C.u8(v))
171 case int16:
172 ob = C.PyShort_FromShort(C.i16(v))
173 case uint16:
174 ob = C.PyShort_FromUnsignedShort(C.u16(v))
175 case int32:
176 ob = C.PyLong_FromLong(C.i32(v))
177 case uint32:
178 ob = C.PyLong_FromUnsignedLong(C.u32(v))
179 case int64:
180 ob = C.PyLong_FromLongLong(C.i64(v))
181 case uint64:
182 ob = C.PyLong_FromUnsignedLongLong(C.u64(v))
183 case string:
184 ob = PyUnicodeFromString(v).asRaw()
185 case time.Time:
186 ob = C.PyTime_FromDateTime(C.int(v.Year()), C.int(v.Month()), C.int(v.Day()), C.int(v.Hour()), C.int(v.Minute()), C.int(v.Second()), C.int(v.Nanosecond()/1000))
187 case net.IP:
188 if !ipaddressFn.IsNull() {
189 ob = ipaddressFn.Call(PyUnicodeFromString(v.String())).asRaw()
190 } else {
191 ob = PyUnicodeFromString(v.String()).asRaw()
192 }
193 case func(arg1, arg2 PyArgs) PyRawObject:
194 n := C.CString("func")
195 defer C.free(unsafe.Pointer(n))
196 mdef := &C.PyMethodDef{
197 ml_name: n,
198 ml_meth: (C.PyCFunction)(unsafe.Pointer(syscall.NewCallback(v))),
199 ml_flags: C.int(DefaultMethFlags),
200 }
201 ob = C.PyCFunction_NewEx((*C.PyMethodDef)(unsafe.Pointer(mdef)), nil, C.PyUnicode_FromString(n))
202 }
203 return &PyObject{rawptr: ob}
204}
205
206// fromRawOb builds a new Python object from the raw pointer.
207func fromRawOb(ob *C.PyObject) *PyObject { return &PyObject{rawptr: ob} }

Callers 1

newEventDictFunction · 0.92

Calls 5

PyUnicodeFromStringFunction · 0.85
asRawMethod · 0.80
IsNullMethod · 0.80
CallMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected