Skip to content

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

Merged
merged 1 commit into from
Aug 4, 2017

Conversation

corona10
Copy link
Collaborator

  • Pass hi.go
  • Support unnamed types.

This PR should be reviewd after #120 is merged.

Copy link
Member

@sbinet sbinet left a 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.

*/

Copy link
Member

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.

/*
Copy link
Member

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

"go/types"
"strconv"
"strings"
)

// Generate C definitions of Go struct.
Copy link
Member

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...])

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.
Copy link
Member

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.

func (g *cffiGen) genCdefType(sym *symbol) {
g.wrapper.Printf("// C definitions of the %[1]s.%[2]s.\n", g.pkg.pkg.Name(), sym.id)
Copy link
Member

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"

g.wrapper.Printf("\n")
}

// Generate Type __repr__ method.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc.

g.wrapper.Printf("\n")
}

// Generate Type __str__ method.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc.

g.wrapper.Printf("\n")
}

// Generate Type __setitem__
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc.

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",
Copy link
Member

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 {
Copy link
Member

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...

Copy link
Collaborator Author

@corona10 corona10 Aug 3, 2017

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.

Copy link
Collaborator Author

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?

Copy link
Member

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.

@corona10 corona10 force-pushed the gsoc-unnamed-hi branch 3 times, most recently from b36e491 to 015efae Compare August 3, 2017 11:13
@corona10 corona10 changed the title [WIP] cffi: Support unnamed types and pass hi.go cffi: Support unnamed types and pass hi.go Aug 3, 2017
@corona10
Copy link
Collaborator Author

corona10 commented Aug 3, 2017

@sbinet Updated!

@corona10 corona10 force-pushed the gsoc-unnamed-hi branch 2 times, most recently from 1bfcf1b to 4fcfc48 Compare August 3, 2017 16:29
@corona10
Copy link
Collaborator Author

corona10 commented Aug 3, 2017

@sbinet Done PTAL!

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")
Copy link
Member

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 ?

Copy link
Collaborator Author

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.

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")
Copy link
Member

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'>
Copy link
Member

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...

@@ -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())
Copy link
Member

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.

@corona10 corona10 force-pushed the gsoc-unnamed-hi branch 3 times, most recently from 597d4e8 to fc53bee Compare August 4, 2017 10:08
* Pass hi.go
* Support unnamed types.
@corona10
Copy link
Collaborator Author

corona10 commented Aug 4, 2017

@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
Copy link
Member

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
Copy link
Member

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.

@sbinet sbinet merged commit 4eac7b0 into go-python:master Aug 4, 2017
@corona10 corona10 deleted the gsoc-unnamed-hi branch August 4, 2017 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants