-
Notifications
You must be signed in to change notification settings - Fork 122
cffi: Support unnamed types and pass hi.go #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
ad9bfe1
to
8a8402b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(some of my comments are the same than for #120. I hope I have kept them consistent across the two PRs)
bind/doc.go
Outdated
to call Go code and pass objects from Python. | ||
|
||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove that empty line (otherwise the documentation isn't attached to the package)
bind/doc.go
Outdated
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
/* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Standard documentation for packages look like:
// Package bind provides tools to generate bindings to use Go from Python.
package bind
bind/gencffi_cdef.go
Outdated
"go/types" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// Generate C definitions of Go struct. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the canonical way to document functions/methods is:
// genCdefStruct generates C definitions for a Go struct.
func (g *cffiGen) genCdefStruct(s Struct) {
(ie: // [method or function name] [verb] [stuff...]
)
bind/gencffi_cdef.go
Outdated
func (g *cffiGen) genCdefStruct(s Struct) { | ||
g.wrapper.Printf("// A type definition of the %[1]s.%[2]s for wrapping.\n", s.Package().Name(), s.sym.cgoname) | ||
g.wrapper.Printf("typedef void* %s;\n", s.sym.cgoname) | ||
g.wrapper.Printf("extern void* cgo_func_%s_new();\n", s.sym.id) | ||
} | ||
|
||
// Generate C definitions of Go type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use the canonical way to document genCdefType
.
bind/gencffi_cdef.go
Outdated
func (g *cffiGen) genCdefType(sym *symbol) { | ||
g.wrapper.Printf("// C definitions of the %[1]s.%[2]s.\n", g.pkg.pkg.Name(), sym.id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rephrase as:
"// C definitions for Go type %[1]s.%[2]s.\n"
bind/gencffi_type.go
Outdated
g.wrapper.Printf("\n") | ||
} | ||
|
||
// Generate Type __repr__ method. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doc.
bind/gencffi_type.go
Outdated
g.wrapper.Printf("\n") | ||
} | ||
|
||
// Generate Type __str__ method. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doc.
bind/gencffi_type.go
Outdated
g.wrapper.Printf("\n") | ||
} | ||
|
||
// Generate Type __setitem__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doc.
bind/gencffi_type.go
Outdated
g.wrapper.Printf("_cffi_helper.lib.cgo_func_%[1]s_ass_item(self.cgopy, idx, pyitem)\n", sym.id) | ||
default: | ||
panic(fmt.Errorf( | ||
"gopy: init for %s not handled", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like a copy paste from __init__
.
bind/symtab.go
Outdated
@@ -130,6 +130,10 @@ func (s symbol) isMap() bool { | |||
return (s.kind & skMap) != 0 | |||
} | |||
|
|||
func (s symbol) isSequence() bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this a function that tests for the Go-concept of a sequence or for the python concept of a sequence?
in other words, do we want to also test for maps ?
a quick grep in the code seems to indicate isSequence
is used to determine whether we need to generate methods that implement the python's sequence protocol...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a quick grep in the code seems to indicate isSequence is used to determine whether we need to generate methods that implement the python's sequence protocol...
Yes, that is. :)
in other words, do we want to also test for maps?
Yes, we need to generate protocols for map also. I am investigating about that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sbinet isIterable
might be better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think isPySequence
better conveys what piece of information we want to extract and its later use.
b36e491
to
015efae
Compare
@sbinet Updated! |
1bfcf1b
to
4fcfc48
Compare
@sbinet Done PTAL! |
bind/gencffi_type.go
Outdated
g.wrapper.Indent() | ||
g.wrapper.Printf("if idx >= len(self):\n") | ||
g.wrapper.Indent() | ||
g.wrapper.Printf("raise IndexError('array index out of range')\n") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it be possible to raise "array index ..."
when it's a Go array and "slice index ..."
when it's a Go slice ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sbinet That looks great I will update it.
bind/gencffi_type.go
Outdated
g.wrapper.Indent() | ||
g.wrapper.Printf("if idx >= len(self):\n") | ||
g.wrapper.Indent() | ||
g.wrapper.Printf("raise IndexError('array index out of range')\n") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto: it would be much nicer to raise an error with the correct type name (array/slice).
main_test.go
Outdated
--- p.Salary(2): 20 | ||
--- p.Salary(24): caught: can't work for 24 hours! | ||
--- Person.__init__ | ||
caught: invalid type for '%!s(BADINDEX)' attribute | err-type: <type 'exceptions.TypeError'> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that seems like an error somewhere in our generated code...
bind/gencffi_struct.go
Outdated
@@ -103,6 +104,10 @@ func (g *cffiGen) genStructInit(s Struct) { | |||
g.wrapper.Outdent() | |||
g.wrapper.Printf("if %[1]s != None:\n", kwd_name) | |||
g.wrapper.Indent() | |||
g.wrapper.Printf("if not isinstance(%[1]s, %[2]s):\n", kwd_name, ifield.sym.pysig) | |||
g.wrapper.Indent() | |||
g.wrapper.Printf("raise TypeError(\"invalid type for '%[2]s' attribute\")\n", field.Name()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the index should be %[1]s
instead.
597d4e8
to
fc53bee
Compare
* Pass hi.go * Support unnamed types.
fc53bee
to
2ec1fd9
Compare
@sbinet Updated and sorry for some mistakes. |
slice[2]: caught: slice index out of range | ||
slice: []int{1, 42} | ||
len(slice): 2 | ||
mem(slice): caught: cannot make memory view because object does not have the buffer interface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok.
please file an issue so we don't forget to implement the buffer interface for slices.
arr[2]: caught: array index out of range | ||
arr: [2]int{1, 42} | ||
len(arr): 2 | ||
mem(arr): caught: cannot make memory view because object does not have the buffer interface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok.
please file an issue so we don't forget to implement the buffer interface for arrays.
This PR should be reviewd after #120 is merged.