diff --git a/_xtool/llpyg/llpyg.go b/_xtool/llpyg/llpyg.go new file mode 100644 index 00000000..56053d27 --- /dev/null +++ b/_xtool/llpyg/llpyg.go @@ -0,0 +1,48 @@ +/* + * 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 main + +import ( + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/py" +) + +func main() { + if c.Argc < 2 { + c.Fprintf(c.Stderr, c.Str("Usage: llpyg []\n")) + return + } + + pyLib := c.Index(c.Argv, 1) + destDir := c.Str(".") + if c.Argc > 2 { + destDir = c.Index(c.Argv, 2) + } + c.Fprintf(c.Stderr, c.Str("pyLib: %s, destDir: %s\n"), pyLib, destDir) + + py.Initialize() + mod := py.ImportModule(pyLib) + dict := mod.ModuleGetDict() + items := dict.DictItems() + for i, n := uintptr(0), items.ListLen(); i < n; i++ { + item := items.ListItem(i) + key := item.TupleItem(0) + val := item.TupleItem(1) + _ = val + c.Fprintf(c.Stderr, c.Str("%s\n"), key.CStr()) + } +} diff --git a/py/bytes.go b/py/bytes.go index 1b5277db..52f0275f 100644 --- a/py/bytes.go +++ b/py/bytes.go @@ -16,6 +16,8 @@ package py +/* + import ( _ "unsafe" @@ -44,3 +46,5 @@ func (o *Object) CStr() *c.Char { return nil } // llgo:link (*Object).Strlen C.PyBytes_Size func (o *Object) Strlen() uintptr { return 0 } + +*/ diff --git a/py/dict.go b/py/dict.go new file mode 100644 index 00000000..8f8923b4 --- /dev/null +++ b/py/dict.go @@ -0,0 +1,45 @@ +/* + * 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/dict.html + +// Return a new empty dictionary, or nil on failure. +// +//go:linkname NewDict C.PyDict_New +func NewDict() *Object + +// Return a ListObject containing all the keys from the dictionary. +// +// llgo:link (*Object).DictKeys C.PyDict_Keys +func (d *Object) DictKeys() *Object { return nil } + +// Return a ListObject containing all the values from the dictionary. +// +// llgo:link (*Object).DictValues C.PyDict_Values +func (d *Object) DictValues() *Object { return nil } + +// Return a ListObject containing all the items from the dictionary. +// +// llgo:link (*Object).DictItems C.PyDict_Items +func (d *Object) DictItems() *Object { return nil } + +// ----------------------------------------------------------------------------- diff --git a/py/list.go b/py/list.go new file mode 100644 index 00000000..4a5297c9 --- /dev/null +++ b/py/list.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/list.html + +// Return a new list of length len on success, or nil on failure. +// +//go:linkname NewList C.PyList_New +func NewList(len uintptr) *Object + +// Return the length of the list object in list; this is equivalent to len(list) +// on a list object. +// +// llgo:link (*Object).ListLen C.PyList_Size +func (l *Object) ListLen() uintptr { return 0 } + +// Return the object at position index in the list pointed to by list. The position +// must be non-negative; indexing from the end of the list is not supported. If index +// is out of bounds (<0 or >=len(list)), return nil and set an IndexError exception. +// +// llgo:link (*Object).ListItem C.PyList_GetItem +func (l *Object) ListItem(index uintptr) *Object { return nil } diff --git a/py/module.go b/py/module.go index 17f19b14..6f33cfaf 100644 --- a/py/module.go +++ b/py/module.go @@ -22,7 +22,7 @@ import ( "github.com/goplus/llgo/c" ) -// ----------------------------------------------------------------------------- +// https://docs.python.org/3/c-api/module.html // This is a wrapper around py.Import which takes a const char* as an argument // instead of an Object. diff --git a/py/tuple.go b/py/tuple.go new file mode 100644 index 00000000..a7d7aa30 --- /dev/null +++ b/py/tuple.go @@ -0,0 +1,39 @@ +/* + * 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/tuple.html + +// Return a new tuple object of size len, or nil on failure. +// +//go:linkname NewTuple C.PyTuple_New +func NewTuple(len uintptr) *Object + +// Take a pointer to a tuple object, and return the size of that tuple. +// +// llgo:link (*Object).TupleLen C.PyTuple_Size +func (t *Object) TupleLen() uintptr { return 0 } + +// Return the object at position pos in the tuple pointed to by p. If pos is +// negative or out of bounds, return nil and set an IndexError exception. +// +// llgo:link (*Object).TupleItem C.PyTuple_GetItem +func (t *Object) TupleItem(index uintptr) *Object { return nil } diff --git a/py/unicode.go b/py/unicode.go new file mode 100644 index 00000000..6c9fb85c --- /dev/null +++ b/py/unicode.go @@ -0,0 +1,47 @@ +/* + * 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/unicode.html + +// Return a pointer to the UTF-8 encoding of the Unicode object, and store the +// size of the encoded representation (in bytes) in size. The size argument can +// be nil; in this case no size will be stored. The returned buffer always has +// an extra null byte appended (not included in size), regardless of whether +// there are any other null code points. +// +// In the case of an error, nil is returned with an exception set and no size is +// stored. +// +// This caches the UTF-8 representation of the string in the Unicode object, and +// subsequent calls will return a pointer to the same buffer. The caller is not +// responsible for deallocating the buffer. The buffer is deallocated and pointers +// 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 } + +// As CStrAndLen, but does not store the len. +// +// llgo:link (*Object).CStr C.PyUnicode_AsUTF8 +func (o *Object) CStr() *c.Char { return nil }