From d3f75a92ad80be9bbc2a993b7cd7a4a43e829ffd Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 13 May 2024 21:01:39 +0800 Subject: [PATCH 1/3] llpyg: print doc --- _pydemo/pi/pi.go | 10 ++++++++++ _xtool/llpyg/llpyg.go | 3 +++ py/exceptions.go | 32 ++++++++++++++++++++++++++++++++ py/math/math.go | 3 +++ py/module.go | 9 +++++++++ 5 files changed, 57 insertions(+) create mode 100644 _pydemo/pi/pi.go create mode 100644 py/exceptions.go diff --git a/_pydemo/pi/pi.go b/_pydemo/pi/pi.go new file mode 100644 index 00000000..934fe31a --- /dev/null +++ b/_pydemo/pi/pi.go @@ -0,0 +1,10 @@ +package main + +import ( + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/py/math" +) + +func main() { + c.Printf(c.Str("pi = %f\n"), math.Pi) +} diff --git a/_xtool/llpyg/llpyg.go b/_xtool/llpyg/llpyg.go index 9dac48a5..eea35c6b 100644 --- a/_xtool/llpyg/llpyg.go +++ b/_xtool/llpyg/llpyg.go @@ -44,8 +44,11 @@ func main() { key := item.TupleItem(0) val := item.TupleItem(1) if val.Callable() != 0 { + doc := val.GetAttrString(c.Str("__doc__")) sig := inspect.Signature(val) + c.Fprintf(c.Stderr, c.Str("-----------------------------------\n")) c.Fprintf(c.Stderr, c.Str("%s: %s\n"), key.CStr(), sig.Str().CStr()) + c.Fprintf(c.Stderr, c.Str("%s\n"), doc.CStr()) } } } diff --git a/py/exceptions.go b/py/exceptions.go new file mode 100644 index 00000000..cd9da49d --- /dev/null +++ b/py/exceptions.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 py + +import ( + _ "unsafe" +) + +// https://docs.python.org/3/c-api/exceptions.html + +// Clear the error indicator. If the error indicator is not set, there is +// no effect. +// +//go:linkname ErrClear C.PyErr_Clear +func ErrClear() + +//go:linkname ErrPrint C.PyErr_Print +func ErrPrint() diff --git a/py/math/math.go b/py/math/math.go index 7bfd8ebe..d65531ff 100644 --- a/py/math/math.go +++ b/py/math/math.go @@ -28,3 +28,6 @@ const ( //go:linkname Sqrt py.sqrt func Sqrt(x *py.Object) *py.Object + +//go:linkname Pi py.pi +var Pi *py.Object diff --git a/py/module.go b/py/module.go index 6f33cfaf..163d0555 100644 --- a/py/module.go +++ b/py/module.go @@ -22,8 +22,17 @@ import ( "github.com/goplus/llgo/c" ) +// https://docs.python.org/3/c-api/import.html // https://docs.python.org/3/c-api/module.html +// Return the module object corresponding to a module name. The name argument +// may be of the form package.module. First check the modules dictionary if +// there’s one there, and if not, create a new one and insert it in the modules +// dictionary. Return nil with an exception set on failure. +// +//go:linkname AddModule C.PyImport_AddModule +func AddModule(name *c.Char) *Object + // This is a wrapper around py.Import which takes a const char* as an argument // instead of an Object. // From bf4f2b6fa03d92ef56e49a67af7058e84a4fe1db Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 13 May 2024 22:02:47 +0800 Subject: [PATCH 2/3] py/builtins: todo --- _xtool/llpyg/llpyg.go | 3 ++ py/builtins/builtins.go | 61 +++++++++++++++++++++++++++++++++++ py/builtins/llgo_autogen.lla | Bin 0 -> 574 bytes py/call.go | 2 +- 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 py/builtins/builtins.go create mode 100644 py/builtins/llgo_autogen.lla diff --git a/_xtool/llpyg/llpyg.go b/_xtool/llpyg/llpyg.go index eea35c6b..8a098c60 100644 --- a/_xtool/llpyg/llpyg.go +++ b/_xtool/llpyg/llpyg.go @@ -20,6 +20,7 @@ import ( "github.com/goplus/llgo/c" "github.com/goplus/llgo/py" "github.com/goplus/llgo/py/inspect" + // "github.com/goplus/llgo/py/builtins" ) func main() { @@ -49,6 +50,8 @@ func main() { c.Fprintf(c.Stderr, c.Str("-----------------------------------\n")) c.Fprintf(c.Stderr, c.Str("%s: %s\n"), key.CStr(), sig.Str().CStr()) c.Fprintf(c.Stderr, c.Str("%s\n"), doc.CStr()) + // c.Fprintf(c.Stderr, c.Str("-----------------------------------\n")) + // builtins.Help(val) } } } diff --git a/py/builtins/builtins.go b/py/builtins/builtins.go new file mode 100644 index 00000000..d425e862 --- /dev/null +++ b/py/builtins/builtins.go @@ -0,0 +1,61 @@ +/* + * 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 builtins + +import ( + _ "unsafe" + + "github.com/goplus/llgo/py" +) + +const ( + LLGoPackage = "py.inspect" +) + +// https://docs.python.org/3/library/functions.html +// https://docs.python.org/3/library/constants.html + +// print(*objects, sep=' ', end='\n', file=None, flush=False) +// +// Print objects to the text stream file, separated by sep and followed by +// end. sep, end, file, and flush, if present, must be given as keyword +// arguments. +// +// All non-keyword arguments are converted to strings like str() does and +// written to the stream, separated by sep and followed by end. Both sep +// and end must be strings; they can also be None, which means to use the +// default values. If no objects are given, print() will just write end. +// +//go:linkname Print py.print +func Print(objects ...*py.Object) + +//go:linkname PrintEx py.print +func PrintEx(__llgo_kwargs *py.Object, objects ...*py.Object) + +// Invoke the built-in help system. (This function is intended for interactive +// use.) If no argument is given, the interactive help system starts on the +// interpreter console. If the argument is a string, then the string is looked +// up as the name of a module, function, class, method, keyword, or documentation +// topic, and a help page is printed on the console. If the argument is any other +// kind of object, a help page on the object is generated. +// +// Note that if a slash(/) appears in the parameter list of a function when invoking +// help(), it means that the parameters prior to the slash are positional-only. For +// more info, see the FAQ entry on positional-only parameters. +// +//go:linkname Help py.help +func Help(object *py.Object) diff --git a/py/builtins/llgo_autogen.lla b/py/builtins/llgo_autogen.lla new file mode 100644 index 0000000000000000000000000000000000000000..3169e1521c37ab109a5e820db1ab3bc0420e02d4 GIT binary patch literal 574 zcmWIWW@Zs#U|`^2n7(Fh#NnJ3-Cc|f4E-z&4Ezi-3^_UJ`SFRRCHd*8d3rfHp&^_M z%z5HYY5720TEWf0$nq7a8f?JQDgNHZh62al{}eg=NlaJg?Y3&oQ`~a*y4erzJ~her znCYI&Dk;JnJMQ1TX}>l~RQs;C*~v4;{&m7deBplThfddj5ciW3mJhNG&a=&*v;4}r zPAT*6vn%KFxF+8gzPL+7BokQy42f2YMmL5jEt*ru5?|WSZ3CmW|a7y@Z?RK_?@ku`|(*J2ib@=(_)24<%20Jw%nP$`S-`?JbeGoNlLt$_O?%p zRXk@g_l>JPiVr{jQ}5R04U>9mXIeIU!z-a_a}uuHV&1-pd%nTZeMhY&ez)JBKRsIF zbaKL!pL}`$ Date: Mon, 13 May 2024 22:05:49 +0800 Subject: [PATCH 3/3] math --- py/math/llgo_autogen.lla | Bin 563 -> 567 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/py/math/llgo_autogen.lla b/py/math/llgo_autogen.lla index 44b7ad79f23add336a1f0ba99a3590860ede996f..5e9568a36ce19676794a477931da84b8523332d7 100644 GIT binary patch delta 512 zcmdnYvYn+qz?+#xgn@y9gJIozyG|Ao`7u~)kdF1t>7 z9ds>JC3W56B5Bbp>#CMq+Iq5Lm&hdTsDhYBfA=0yP6^999JF9czK(g9*NLTZ4_0q! zJ9FvE$>1v%l@YZP)qYiqS|VOIcJEo%R+&*4!MSSMClgN5pcUn7js8VTJr>8;+Gb9Y<|M#`8TQKaSS0{F$X6I9GmV`7?FMO!Kzzv^$hcu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