diff --git a/_pydemo/callpy/callpy.go b/_pydemo/callpy/callpy.go new file mode 100644 index 00000000..8900a9bb --- /dev/null +++ b/_pydemo/callpy/callpy.go @@ -0,0 +1,12 @@ +package main + +import ( + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/py" + "github.com/goplus/llgo/py/math" +) + +func main() { + x := math.Sqrt(py.Float(2)) + c.Printf(c.Str("sqrt(2) = %f\n"), x.FloatAsDouble()) +} diff --git a/internal/pyimport/llgo_autogen.lla b/internal/pyimport/llgo_autogen.lla new file mode 100644 index 00000000..d839012f Binary files /dev/null and b/internal/pyimport/llgo_autogen.lla differ diff --git a/internal/pyimport/pyimport.go b/internal/pyimport/pyimport.go new file mode 100644 index 00000000..69f96fad --- /dev/null +++ b/internal/pyimport/pyimport.go @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pyimport + +import ( + _ "unsafe" + + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/py" +) + +func init() { + py.Initialize() + py.SetProgramName(*c.Argv) +} + +//go:linkname Module C.PyImport_ImportModule +func Module(name *c.Char) *py.Module diff --git a/py/_demo/callpy/call.go b/py/_demo/callpy/call.go index abc21275..58904c9c 100644 --- a/py/_demo/callpy/call.go +++ b/py/_demo/callpy/call.go @@ -10,7 +10,10 @@ func main() { py.SetProgramName(*c.Argv) math := py.ImportModule(c.Str("math")) sqrt := math.GetAttrString(c.Str("sqrt")) - sqrt2 := sqrt.CallOneArg(py.Float(2)).FloatAsDouble() - c.Printf(c.Str("sqrt(2) = %f\n"), sqrt2) + sqrt2 := sqrt.CallOneArg(py.Float(2)) + c.Printf(c.Str("sqrt(2) = %f\n"), sqrt2.FloatAsDouble()) + sqrt2.DecRef() + sqrt.DecRef() + math.DecRef() py.Finalize() } diff --git a/py/math/math.go b/py/math/math.go new file mode 100644 index 00000000..0af8edd8 --- /dev/null +++ b/py/math/math.go @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// llgo:import py.math +package math + +import ( + _ "unsafe" + + "github.com/goplus/llgo/py" +) + +//go:linkname Sqrt py.sqrt +func Sqrt(x *py.Object) *py.Object diff --git a/py/module.go b/py/module.go index a65e2987..64c1a8a8 100644 --- a/py/module.go +++ b/py/module.go @@ -55,13 +55,4 @@ func Import(name *Object) *Module // llgo:link (*Module).GetDict C.PyModule_GetDict func (m *Module) GetDict() *Object { panic("unreachable") } -// Retrieve an attribute named attrName from object o. Returns the attribute value on success, -// or nil on failure. This is the equivalent of the Python expression o.attrName. -// -// llgo:link (*Object).GetAttr C.PyObject_GetAttr -func (o *Object) GetAttr(attrName *Object) *Object { panic("unreachable") } - -// llgo:link (*Object).GetAttrString C.PyObject_GetAttrString -func (o *Object) GetAttrString(attrName *c.Char) *Object { panic("unreachable") } - // ----------------------------------------------------------------------------- diff --git a/py/object.go b/py/object.go index 387b02da..35c798e8 100644 --- a/py/object.go +++ b/py/object.go @@ -40,6 +40,17 @@ func (o *Object) DecRef() { panic("unreachable") } // ----------------------------------------------------------------------------- +// Retrieve an attribute named attrName from object o. Returns the attribute value on success, +// or nil on failure. This is the equivalent of the Python expression o.attrName. +// +// llgo:link (*Object).GetAttr C.PyObject_GetAttr +func (o *Object) GetAttr(attrName *Object) *Object { panic("unreachable") } + +// llgo:link (*Object).GetAttrString C.PyObject_GetAttrString +func (o *Object) GetAttrString(attrName *c.Char) *Object { panic("unreachable") } + +// ----------------------------------------------------------------------------- + // Determine if the object o is callable. Return 1 if the object is callable and // 0 otherwise. This function always succeeds. //