Merge pull request #172 from xushiwei/q

py/numpy
This commit is contained in:
xushiwei
2024-05-15 00:33:10 +08:00
committed by GitHub
7 changed files with 10295 additions and 13 deletions

View File

@@ -43,9 +43,11 @@ func main() {
val := mod.GetAttr(key)
doc := val.GetAttrString(c.Str("__doc__"))
sym := cjson.Object()
sym.SetItem(c.Str("type"), cjson.String(val.Type().Name().CStr()))
sym.SetItem(c.Str("type"), cjson.String(val.Type().TypeName().CStr()))
sym.SetItem(c.Str("name"), cjson.String(key.CStr()))
sym.SetItem(c.Str("doc"), cjson.String(doc.CStr()))
if doc != nil {
sym.SetItem(c.Str("doc"), cjson.String(doc.CStr()))
}
if val.Callable() != 0 {
sig := inspect.Signature(val)
sym.SetItem(c.Str("sig"), cjson.String(sig.Str().CStr()))

View File

@@ -81,10 +81,10 @@ func main() {
ctx := &context{pkg, obj, objPtr, ret, py}
for _, sym := range mod.Items {
switch sym.Type {
case "builtin_function_or_method", "function":
case "builtin_function_or_method", "function", "ufunc":
ctx.genFunc(pkg, sym)
case "str", "float", "bool", "type", "dict", "tuple", "list",
"module", "int", "set", "frozenset", "flags": // skip
"module", "int", "set", "frozenset", "flags", "bool_": // skip
default:
t := sym.Type
if len(t) > 0 && (t[0] >= 'a' && t[0] <= 'z') && !strings.HasSuffix(t, "_info") {
@@ -104,14 +104,14 @@ type context struct {
}
func (ctx *context) genFunc(pkg *gogen.Package, sym *symbol) {
name := sym.Name
name, symSig := sym.Name, sym.Sig
if len(name) == 0 || name[0] == '_' {
return
}
params, variadic, skip := ctx.genParams(pkg, sym.Sig)
params, variadic, skip := ctx.genParams(pkg, symSig)
if skip {
// TODO(xsw): don't skip any func
log.Println("skip func:", name, sym.Sig)
log.Println("skip func:", name, symSig)
return
}
name = genName(name, -1)
@@ -145,14 +145,17 @@ func (ctx *context) genParams(pkg *gogen.Package, sig string) (*types.Tuple, boo
break
}
if strings.HasPrefix(part, "*") {
if len(part) > 1 && part[1] == '*' || i != n-1 {
return nil, false, true
if part[1] != '*' {
list = append(list, pkg.NewParam(0, genName(part[1:], 0), types.NewSlice(objPtr)))
return types.NewTuple(list...), true, false
}
list = append(list, pkg.NewParam(0, genName(part[1:], 0), types.NewSlice(objPtr)))
return types.NewTuple(list...), true, false
return types.NewTuple(list...), false, false
}
pos := strings.IndexByte(part, '=')
if pos >= 0 {
if strings.HasPrefix(part[pos+1:], "<") { // skip complex default value
return nil, false, true
}
part = part[:pos]
}
list = append(list, pkg.NewParam(0, genName(part, 0), objPtr))

View File

@@ -25,7 +25,7 @@ int main() {
Py_Initialize();
PyObject* inspect = PyImport_ImportModule("inspect");
PyObject* signature = PyObject_GetAttrString(inspect, "signature");
PyObject* mod = PyImport_ImportModule("math");
PyObject* mod = PyImport_ImportModule("numpy");
PyObject* dict = PyModule_GetDict(mod);
PyObject* keys = PyDict_Keys(dict);
size_t i, n;
@@ -39,7 +39,10 @@ int main() {
printf("-----------------------------------\n");
printf("sig: %p\n", sig);
printf("%s: %s\n", PyUnicode_AsUTF8(key), PyUnicode_AsUTF8(PyObject_Str(sig)));
printf("%s\n", PyUnicode_AsUTF8(doc));
printf("%s\n", PyUnicode_AsUTF8(key));
if (doc != NULL) {
printf("%s\n", PyUnicode_AsUTF8(doc));
}
}
}
return 0;

10195
py/numpy/gen.go Normal file

File diff suppressed because it is too large Load Diff

BIN
py/numpy/llgo_autogen.lla Normal file

Binary file not shown.

79
py/numpy/numpy.go Normal file
View File

@@ -0,0 +1,79 @@
/*
* 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 numpy
import (
_ "unsafe"
"github.com/goplus/llgo/py"
)
// https://numpy.org/doc/stable/reference/index.html#reference
// Return evenly spaced values within a given interval.
//
// numpy.arange([start, ]stop, [step, ]dtype=None, *, like=None)
//
// See https://numpy.org/doc/stable/reference/generated/numpy.arange.html#numpy-arange
//
//go:linkname Arange py.arange
func Arange(start, stop, step, dtype *py.Object) *py.Object
// Return a new array of given shape and type, without initializing entries.
//
// numpy.empty(shape, dtype=float, order='C', *, like=None)
//
// See https://numpy.org/doc/stable/reference/generated/numpy.empty.html#numpy-empty
//
//go:linkname Empty py.empty
func Empty(shape, dtype, order *py.Object) *py.Object
// Return a 2-D array with ones on the diagonal and zeros elsewhere.
//
// numpy.eye(N, M=None, k=0, dtype=<class 'float'>, order='C', *, like=None)
//
// See https://numpy.org/doc/stable/reference/generated/numpy.eye.html#numpy-eye
//
//go:linkname Eye py.eye
func Eye(N, M, k, dtype, order *py.Object) *py.Object
// Return a new array of given shape and type, filled with zeros.
//
// numpy.zeros(shape, dtype=float, order='C', *, like=None)
//
// See https://numpy.org/doc/stable/reference/generated/numpy.zeros.html#numpy-zeros
//
//go:linkname Zeros py.zeros
func Zeros(shape, dtype, order *py.Object) *py.Object
// Create an array.
//
// numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)
//
// See https://numpy.org/doc/stable/reference/generated/numpy.array.html#numpy-array
//
//go:linkname Array py.array
func Array(object, dtype *py.Object) *py.Object
// Convert the input to an array.
//
// numpy.asarray(a, dtype=None, order=None, *, like=None)
//
// See https://numpy.org/doc/stable/reference/generated/numpy.asarray.html#numpy-asarray
//
//go:linkname AsArray py.asarray
func AsArray(a, dtype, order *py.Object) *py.Object

BIN
py/sys/llgo_autogen.lla Normal file

Binary file not shown.