pyimport & deomo: callpy (todo)

This commit is contained in:
xushiwei
2024-05-11 06:23:05 +08:00
parent 29830865d9
commit cca46573ea
7 changed files with 87 additions and 11 deletions

12
_pydemo/callpy/callpy.go Normal file
View File

@@ -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())
}

Binary file not shown.

View File

@@ -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

View File

@@ -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()
}

27
py/math/math.go Normal file
View File

@@ -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

View File

@@ -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") }
// -----------------------------------------------------------------------------

View File

@@ -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.
//