1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
package stalecucumber
import "fmt"
import "strings"
type UnparseablePythonGlobalError struct {
Args interface{}
Message string
}
func(this UnparseablePythonGlobalError) Error() string {
return fmt.Sprintf("%s; arguments (%T): %v", this.Message, this.Args, this.Args)
}
type PythonBuiltinResolver struct {}
func (this PythonBuiltinResolver) Resolve(module string, name string, args []interface{}) (interface{}, error) {
// Up to version 2 this is always "__builtin__"
// In version 3+ it becomes "builtin" but that is not supported yet
if module != "__builtin__" {
return nil, ErrUnresolvablePythonGlobal
}
if name == "set" {
return this.handlePythonSet(args)
}
if name == "bytearray" {
return this.handlePythonByteArray(args)
}
return nil, ErrUnresolvablePythonGlobal
}
func (this PythonBuiltinResolver) handlePythonSet(args []interface{}) (interface{}, error){
if len(args) != 1 {
return nil, UnparseablePythonGlobalError{
Args: args,
Message: "Expected args to be of length 1",
}
}
tuple, ok := args[0].([]interface{})
if !ok {
return nil, UnparseablePythonGlobalError{
Args: args,
Message: fmt.Sprintf("Expected first argument of args to be of type %T", tuple),
}
}
// A map is the equivalent golang type for a python set
set := make(map[interface{}]bool, len(tuple))
for _, item := range tuple {
set[item] = true
}
return set, nil
}
func (this PythonBuiltinResolver) handlePythonByteArray(args []interface{}) (interface{}, error){
// Up to version 2 the implementation of bytearray always pickles as a tuple like
// (theStringValue, 'latin-1', )
// version 3+ of pickle is different but we're not supporting that presently
if len(args) != 2{
return nil, UnparseablePythonGlobalError{
Args: args,
Message: "Expected args to be of length 2",
}
}
const magic = `latin-1`
magicValue, ok := args[1].(string)
if !ok || magicValue != magic{
return nil, UnparseablePythonGlobalError{
Args: args,
Message: fmt.Sprintf("Expected second arg to be string %q", magic),
}
}
value, ok := args[0].(string)
if !ok {
return nil, UnparseablePythonGlobalError{
Args: args,
Message: "Expected first arg to be a string",
}
}
return strings.NewReader(value), nil
}
|