pyimport & deomo: callpy (todo)
This commit is contained in:
12
_pydemo/callpy/callpy.go
Normal file
12
_pydemo/callpy/callpy.go
Normal 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())
|
||||||
|
}
|
||||||
BIN
internal/pyimport/llgo_autogen.lla
Normal file
BIN
internal/pyimport/llgo_autogen.lla
Normal file
Binary file not shown.
32
internal/pyimport/pyimport.go
Normal file
32
internal/pyimport/pyimport.go
Normal 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
|
||||||
@@ -10,7 +10,10 @@ func main() {
|
|||||||
py.SetProgramName(*c.Argv)
|
py.SetProgramName(*c.Argv)
|
||||||
math := py.ImportModule(c.Str("math"))
|
math := py.ImportModule(c.Str("math"))
|
||||||
sqrt := math.GetAttrString(c.Str("sqrt"))
|
sqrt := math.GetAttrString(c.Str("sqrt"))
|
||||||
sqrt2 := sqrt.CallOneArg(py.Float(2)).FloatAsDouble()
|
sqrt2 := sqrt.CallOneArg(py.Float(2))
|
||||||
c.Printf(c.Str("sqrt(2) = %f\n"), sqrt2)
|
c.Printf(c.Str("sqrt(2) = %f\n"), sqrt2.FloatAsDouble())
|
||||||
|
sqrt2.DecRef()
|
||||||
|
sqrt.DecRef()
|
||||||
|
math.DecRef()
|
||||||
py.Finalize()
|
py.Finalize()
|
||||||
}
|
}
|
||||||
|
|||||||
27
py/math/math.go
Normal file
27
py/math/math.go
Normal 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
|
||||||
@@ -55,13 +55,4 @@ func Import(name *Object) *Module
|
|||||||
// llgo:link (*Module).GetDict C.PyModule_GetDict
|
// llgo:link (*Module).GetDict C.PyModule_GetDict
|
||||||
func (m *Module) GetDict() *Object { panic("unreachable") }
|
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") }
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|||||||
11
py/object.go
11
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
|
// Determine if the object o is callable. Return 1 if the object is callable and
|
||||||
// 0 otherwise. This function always succeeds.
|
// 0 otherwise. This function always succeeds.
|
||||||
//
|
//
|
||||||
|
|||||||
Reference in New Issue
Block a user