diff --git a/_xtool/llpyg/llpyg.go b/_xtool/llpyg/llpyg.go index 56053d27..9dac48a5 100644 --- a/_xtool/llpyg/llpyg.go +++ b/_xtool/llpyg/llpyg.go @@ -19,6 +19,7 @@ package main import ( "github.com/goplus/llgo/c" "github.com/goplus/llgo/py" + "github.com/goplus/llgo/py/inspect" ) func main() { @@ -42,7 +43,9 @@ func main() { item := items.ListItem(i) key := item.TupleItem(0) val := item.TupleItem(1) - _ = val - c.Fprintf(c.Stderr, c.Str("%s\n"), key.CStr()) + if val.Callable() != 0 { + sig := inspect.Signature(val) + c.Fprintf(c.Stderr, c.Str("%s: %s\n"), key.CStr(), sig.Str().CStr()) + } } } diff --git a/py/arg.go b/py/arg.go new file mode 100644 index 00000000..aca2fd89 --- /dev/null +++ b/py/arg.go @@ -0,0 +1,33 @@ +/* + * 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 py + +import ( + _ "unsafe" + + "github.com/goplus/llgo/c" +) + +// https://docs.python.org/3/c-api/arg.html + +// Create a new value based on a format string similar to those accepted by the +// PyArg_Parse* family of functions and a sequence of values. Returns the value or +// nil in the case of an error; an exception will be raised if nil is returned. +// See https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue +// +//go:linkname BuildValue C.Py_BuildValue +func BuildValue(format *c.Char, __llgo_va_list ...any) *Object diff --git a/py/call.go b/py/call.go new file mode 100644 index 00000000..a54cf623 --- /dev/null +++ b/py/call.go @@ -0,0 +1,131 @@ +/* + * 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 py + +import ( + _ "unsafe" + + "github.com/goplus/llgo/c" +) + +// https://docs.python.org/3/c-api/call.html + +// Determine if the object o is callable. Return 1 if the object is callable and +// 0 otherwise. This function always succeeds. +// +// llgo:link (*Object).Callable C.PyCallable_Check +func (o *Object) Callable() c.Int { return 0 } + +// Call a callable Python object o, with arguments given by the tuple args, and +// named arguments given by the dictionary kwargs. +// +// args must not be nil; use an empty tuple if no arguments are needed. If no named +// arguments are needed, kwargs can be nil. +// +// Return the result of the call on success, or raise an exception and return nil +// on failure. +// +// This is the equivalent of the Python expression: o(*args, **kwargs). +// +// llgo:link (*Object).Call C.PyObject_Call +func (o *Object) Call(args, kwargs *Object) *Object { return nil } + +// Call a callable Python object callable without any arguments. It is the most +// efficient way to call a callable Python object without any argument. +// +// Return the result of the call on success, or raise an exception and return nil +// on failure. +// +// llgo:link (*Object).CallNoArgs C.PyObject_CallNoArgs +func (o *Object) CallNoArgs() *Object { return nil } + +// Call a callable Python object callable with exactly 1 positional argument arg +// and no keyword arguments. +// +// Return the result of the call on success, or raise an exception and return nil +// on failure. +// +// llgo:link (*Object).CallOneArg C.PyObject_CallOneArg +func (o *Object) CallOneArg(arg *Object) *Object { return nil } + +// Call a callable Python object o, with arguments given by the tuple args. If no +// arguments are needed, then args can be nil. +// +// Return the result of the call on success, or raise an exception and return nil +// on failure. +// +// This is the equivalent of the Python expression: o(*args). +// +// llgo:link (*Object).CallObject C.PyObject_CallObject +func (o *Object) CallObject(callable, args *Object) *Object { return nil } + +// Call a callable Python object o, with a variable number of C arguments. The C +// arguments are described using a py.BuildValue style format string. The format +// can be nil, indicating that no arguments are provided. +// +// Return the result of the call on success, or raise an exception and return nil +// on failure. +// +// This is the equivalent of the Python expression: o(*args). +// +// Note that if you only pass PyObject* args, (*Object).CallFunctionObjArgs is a +// faster alternative. +// +// llgo:link (*Object).CallFunction C.PyObject_CallFunction +func (o *Object) CallFunction(format *c.Char, __llgo_va_list ...any) *Object { return nil } + +// Call a callable Python object o, with a variable number of PyObject* arguments. +// The arguments are provided as a variable number of parameters followed by nil. +// +// Return the result of the call on success, or raise an exception and return nil +// on failure. +// +// This is the equivalent of the Python expression: o(arg1, arg2, ...). +// +// llgo:link (*Object).CallFunctionObjArgs C.PyObject_CallFunctionObjArgs +func (o *Object) CallFunctionObjArgs(__llgo_va_list ...any) *Object { return nil } + +// llgo:link (*Object).CallMethod C.PyObject_CallMethod +func (o *Object) CallMethod(name *c.Char, format *c.Char, __llgo_va_list ...any) *Object { + return nil +} + +// llgo:link (*Object).CallMethodObjArgs C.PyObject_CallMethodObjArgs +func (o *Object) CallMethodObjArgs(name *Object, __llgo_va_list ...any) *Object { return nil } + +// llgo:link (*Object).CallMethodNoArgs C.PyObject_CallMethodNoArgs +func (o *Object) CallMethodNoArgs(name *Object) *Object { return nil } + +// llgo:link (*Object).CallMethodOneArg C.PyObject_CallMethodOneArg +func (o *Object) CallMethodOneArg(name, arg *Object) *Object { return nil } + +// llgo:link (*Object).Vectorcall C.PyObject_Vectorcall +func (o *Object) Vectorcall(args **Object, nargs uintptr, kwnames *Object) *Object { + return nil +} + +// llgo:link (*Object).VectorcallDict C.PyObject_VectorcallDict +func (o *Object) VectorcallDict(args **Object, nargs uintptr, kwdict *Object) *Object { + return nil +} + +// llgo:link (*Object).VectorcallMethod C.PyObject_VectorcallMethod +func (o *Object) VectorcallMethod(name *Object, args **Object, nargs uintptr, kwnames *Object) *Object { + return nil +} + +// ----------------------------------------------------------------------------- diff --git a/py/code.go b/py/code.go new file mode 100644 index 00000000..1e0468c6 --- /dev/null +++ b/py/code.go @@ -0,0 +1,41 @@ +/* + * 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 py + +import ( + _ "unsafe" +) + +// https://docs.python.org/3/c-api/code.html + +// Equivalent to the Python code getattr(co, 'co_code'). Returns a strong +// reference to a BytesObject representing the bytecode in a code object. +// On error, nil is returned and an exception is raised. +// +// This BytesObject may be created on-demand by the interpreter and does +// not necessarily represent the bytecode actually executed by CPython. +// The primary use case for this function is debuggers and profilers. +// +// llgo:link (*Object).CodeBytes C.PyCode_GetCode +func (o *Object) CodeBytes() *Object { return nil } + +// Equivalent to the Python code getattr(co, 'co_varnames'). Returns a new +// reference to a TupleObject containing the names of the local variables. +// On error, nil is returned and an exception is raised. +// +// llgo:link (*Object).CodeVarnames C.PyCode_GetVarnames +func (o *Object) CodeVarnames() *Object { return nil } diff --git a/py/func.go b/py/func.go new file mode 100644 index 00000000..fcb14f6c --- /dev/null +++ b/py/func.go @@ -0,0 +1,56 @@ +/* + * 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 py + +import ( + _ "unsafe" +) + +// https://docs.python.org/3/c-api/function.html + +// Return a new function object associated with the code object code. +// globals must be a dictionary with the global variables accessible +// to the function. +// +// The function’s docstring and name are retrieved from the code object. +// __module__ is retrieved from globals. The argument defaults, annotations +// and closure are set to nil. __qualname__ is set to the same value as +// the code object’s co_qualname field. +// +//go:linkname NewFunc C.PyFunction_New +func NewFunc(code, globals *Object) *Object + +// As NewFunc, but also allows setting the function object’s __qualname__ +// attribute. qualname should be a unicode object or nil; if nil, the +// __qualname__ attribute is set to the same value as the code object’s +// co_qualname field. +// +//go:linkname NewFuncWithQualName C.PyFunction_NewWithQualName +func NewFuncWithQualName(code, globals, qualname *Object) *Object + +/* +// Return true if o is a function object (has type PyFunction_Type). The +// parameter must not be nil. This function always succeeds. +// +// llgo:link (*Object).FuncCheck C.PyFunction_Check +func (o *Object) FuncCheck() c.Int { return 0 } +*/ + +// Return the code object associated with the function object op. +// +// llgo:link (*Object).FuncCode C.PyFunction_GetCode +func (f *Object) FuncCode() *Object { return nil } diff --git a/py/inspect/inspect.go b/py/inspect/inspect.go new file mode 100644 index 00000000..b9cb3324 --- /dev/null +++ b/py/inspect/inspect.go @@ -0,0 +1,42 @@ +/* + * 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 inspect + +import ( + _ "unsafe" + + "github.com/goplus/llgo/py" +) + +const ( + LLGoPackage = "py.inspect" +) + +// https://docs.python.org/3/library/inspect.html + +// Return a signature object for the given callable. +// +//go:linkname Signature py.signature +func Signature(callable *py.Object) *py.Object + +// Get the names and default values of a Python function’s parameters. A named +// tuple is returned: +// +// FullArgSpec(args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations) +// +//go:linkname Getfullargspec py.getfullargspec +func Getfullargspec(f *py.Object) *py.Object diff --git a/py/inspect/llgo_autogen.lla b/py/inspect/llgo_autogen.lla new file mode 100644 index 00000000..d4dfb550 Binary files /dev/null and b/py/inspect/llgo_autogen.lla differ diff --git a/py/object.go b/py/object.go index 6c3d3cc2..bfa1e04b 100644 --- a/py/object.go +++ b/py/object.go @@ -27,17 +27,16 @@ type Object struct { Unused [8]byte } -// Create a new value based on a format string similar to those accepted by the -// PyArg_Parse* family of functions and a sequence of values. Returns the value or -// nil in the case of an error; an exception will be raised if nil is returned. -// See https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue -// -//go:linkname BuildValue C.Py_BuildValue -func BuildValue(format *c.Char, __llgo_va_list ...any) *Object - // llgo:link (*Object).DecRef C.Py_DecRef func (o *Object) DecRef() {} +// Compute a string representation of object o. Returns the string representation on +// success, nil on failure. This is the equivalent of the Python expression str(o). +// Called by the str() built-in function and, therefore, by the print() function. +// +// llgo:link (*Object).Str C.PyObject_Str +func (o *Object) Str() *Object { return nil } + // ----------------------------------------------------------------------------- // Retrieve an attribute named attrName from object o. Returns the attribute value on success, @@ -50,109 +49,3 @@ func (o *Object) GetAttr(attrName *Object) *Object { return nil } func (o *Object) GetAttrString(attrName *c.Char) *Object { return nil } // ----------------------------------------------------------------------------- - -// Determine if the object o is callable. Return 1 if the object is callable and -// 0 otherwise. This function always succeeds. -// -// llgo:link (*Object).Callable C.PyCallable_Check -func (o *Object) Callable() c.Int { return 0 } - -// Call a callable Python object o, with arguments given by the tuple args, and -// named arguments given by the dictionary kwargs. -// -// args must not be nil; use an empty tuple if no arguments are needed. If no named -// arguments are needed, kwargs can be nil. -// -// Return the result of the call on success, or raise an exception and return nil -// on failure. -// -// This is the equivalent of the Python expression: o(*args, **kwargs). -// -// llgo:link (*Object).Call C.PyObject_Call -func (o *Object) Call(args, kwargs *Object) *Object { return nil } - -// Call a callable Python object callable without any arguments. It is the most -// efficient way to call a callable Python object without any argument. -// -// Return the result of the call on success, or raise an exception and return nil -// on failure. -// -// llgo:link (*Object).CallNoArgs C.PyObject_CallNoArgs -func (o *Object) CallNoArgs() *Object { return nil } - -// Call a callable Python object callable with exactly 1 positional argument arg -// and no keyword arguments. -// -// Return the result of the call on success, or raise an exception and return nil -// on failure. -// -// llgo:link (*Object).CallOneArg C.PyObject_CallOneArg -func (o *Object) CallOneArg(arg *Object) *Object { return nil } - -// Call a callable Python object o, with arguments given by the tuple args. If no -// arguments are needed, then args can be nil. -// -// Return the result of the call on success, or raise an exception and return nil -// on failure. -// -// This is the equivalent of the Python expression: o(*args). -// -// llgo:link (*Object).CallObject C.PyObject_CallObject -func (o *Object) CallObject(callable, args *Object) *Object { return nil } - -// Call a callable Python object o, with a variable number of C arguments. The C -// arguments are described using a py.BuildValue style format string. The format -// can be nil, indicating that no arguments are provided. -// -// Return the result of the call on success, or raise an exception and return nil -// on failure. -// -// This is the equivalent of the Python expression: o(*args). -// -// Note that if you only pass PyObject* args, (*Object).CallFunctionObjArgs is a -// faster alternative. -// -// llgo:link (*Object).CallFunction C.PyObject_CallFunction -func (o *Object) CallFunction(format *c.Char, __llgo_va_list ...any) *Object { return nil } - -// Call a callable Python object o, with a variable number of PyObject* arguments. -// The arguments are provided as a variable number of parameters followed by nil. -// -// Return the result of the call on success, or raise an exception and return nil -// on failure. -// -// This is the equivalent of the Python expression: o(arg1, arg2, ...). -// -// llgo:link (*Object).CallFunctionObjArgs C.PyObject_CallFunctionObjArgs -func (o *Object) CallFunctionObjArgs(__llgo_va_list ...any) *Object { return nil } - -// llgo:link (*Object).CallMethod C.PyObject_CallMethod -func (o *Object) CallMethod(name *c.Char, format *c.Char, __llgo_va_list ...any) *Object { - return nil -} - -// llgo:link (*Object).CallMethodObjArgs C.PyObject_CallMethodObjArgs -func (o *Object) CallMethodObjArgs(name *Object, __llgo_va_list ...any) *Object { return nil } - -// llgo:link (*Object).CallMethodNoArgs C.PyObject_CallMethodNoArgs -func (o *Object) CallMethodNoArgs(name *Object) *Object { return nil } - -// llgo:link (*Object).CallMethodOneArg C.PyObject_CallMethodOneArg -func (o *Object) CallMethodOneArg(name, arg *Object) *Object { return nil } - -// llgo:link (*Object).Vectorcall C.PyObject_Vectorcall -func (o *Object) Vectorcall(args **Object, nargs uintptr, kwnames *Object) *Object { - return nil -} - -// llgo:link (*Object).VectorcallDict C.PyObject_VectorcallDict -func (o *Object) VectorcallDict(args **Object, nargs uintptr, kwdict *Object) *Object { - return nil -} - -// llgo:link (*Object).VectorcallMethod C.PyObject_VectorcallMethod -func (o *Object) VectorcallMethod(name *Object, args **Object, nargs uintptr, kwnames *Object) *Object { - return nil -} - -// ----------------------------------------------------------------------------- diff --git a/py/unicode.go b/py/unicode.go index 6c9fb85c..62ac7b4b 100644 --- a/py/unicode.go +++ b/py/unicode.go @@ -39,9 +39,9 @@ import ( // to it become invalid when the Unicode object is garbage collected. // // llgo:link (*Object).CStrAndLen C.PyUnicode_AsUTF8AndSize -func (o *Object) CStrAndLen() (*c.Char, uintptr) { return nil, 0 } +func (u *Object) CStrAndLen() (*c.Char, uintptr) { return nil, 0 } // As CStrAndLen, but does not store the len. // // llgo:link (*Object).CStr C.PyUnicode_AsUTF8 -func (o *Object) CStr() *c.Char { return nil } +func (u *Object) CStr() *c.Char { return nil } diff --git a/x/sqlite/llgo.cfg b/x/sqlite/llgo.cfg index 8c24d7a7..3b457a74 100644 --- a/x/sqlite/llgo.cfg +++ b/x/sqlite/llgo.cfg @@ -5,6 +5,8 @@ "../sqlite/configure", "make", "clang -emit-llvm -S -o ../llgo_autogen.ll -c sqlite3.c", - "cd ..; rm llgo_autogen.lla; zip llgo_autogen.lla llgo_autogen.ll sqlite.ll", + "cd ..", + "llgen .", + "rm llgo_autogen.lla; zip llgo_autogen.lla llgo_autogen.ll sqlite.ll", ] }