Skip to content

Commit ee7247e

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 2f79b9f + fff175c commit ee7247e

File tree

13 files changed

+343
-45
lines changed

13 files changed

+343
-45
lines changed

builtin/builtin.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func init() {
2424
py.MustNewMethod("abs", builtin_abs, 0, abs_doc),
2525
py.MustNewMethod("all", builtin_all, 0, all_doc),
2626
py.MustNewMethod("any", builtin_any, 0, any_doc),
27-
// py.MustNewMethod("ascii", builtin_ascii, 0, ascii_doc),
27+
py.MustNewMethod("ascii", builtin_ascii, 0, ascii_doc),
2828
// py.MustNewMethod("bin", builtin_bin, 0, bin_doc),
2929
// py.MustNewMethod("callable", builtin_callable, 0, callable_doc),
3030
py.MustNewMethod("chr", builtin_chr, 0, chr_doc),
@@ -309,6 +309,19 @@ func builtin_any(self, seq py.Object) (py.Object, error) {
309309
return py.False, nil
310310
}
311311

312+
const ascii_doc = `
313+
`
314+
315+
func builtin_ascii(self, o py.Object) (py.Object, error) {
316+
reprObj, err := py.Repr(o)
317+
if err != nil {
318+
return nil, err
319+
}
320+
repr := reprObj.(py.String)
321+
out := py.StringEscape(repr, true)
322+
return py.String(out), err
323+
}
324+
312325
const round_doc = `round(number[, ndigits]) -> number
313326
314327
Round a number to a given precision in decimal digits (default 0 digits).

builtin/tests/builtin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
assert any(["hello", "world"]) == True
2020
assert any([]) == False
2121

22+
doc="ascii"
23+
assert ascii('hello world') == "'hello world'"
24+
assert ascii('안녕 세상') == "'\\uc548\\ub155 \\uc138\\uc0c1'"
25+
assert ascii(chr(0x10001)) == "'\\U00010001'"
26+
assert ascii('안녕 gpython') == "'\\uc548\\ub155 gpython'"
27+
2228
doc="chr"
2329
assert chr(65) == "A"
2430
assert chr(163) == "£"

main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package main
99
import (
1010
"flag"
1111
"fmt"
12+
"runtime"
1213
"runtime/pprof"
1314

1415
_ "github.com/go-python/gpython/builtin"
@@ -63,6 +64,12 @@ func main() {
6364
args := flag.Args()
6465
py.MustGetModule("sys").Globals["argv"] = pysys.MakeArgv(args)
6566
if len(args) == 0 {
67+
68+
fmt.Printf("Python 3.4.0 (%s, %s)\n", commit, date)
69+
fmt.Printf("[Gpython %s]\n", version)
70+
fmt.Printf("- os/arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
71+
fmt.Printf("- go version: %s\n", runtime.Version())
72+
6673
cli.RunREPL()
6774
return
6875
}

py/dict.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ var (
2727
expectingDict = ExceptionNewf(TypeError, "a dict is required")
2828
)
2929

30+
func init() {
31+
StringDictType.Dict["items"] = MustNewMethod("items", func(self Object, args Tuple) (Object, error) {
32+
sMap := self.(StringDict)
33+
o := make([]Object, 0, len(sMap))
34+
for k, v := range sMap {
35+
o = append(o, Tuple{String(k), v})
36+
}
37+
return NewIterator(o), nil
38+
}, 0, "items() -> list of D's (key, value) pairs, as 2-tuples")
39+
}
40+
3041
// String to object dictionary
3142
//
3243
// Used for variables etc where the keys can only be strings
@@ -100,6 +111,15 @@ func (a StringDict) M__repr__() (Object, error) {
100111
return String(out.String()), nil
101112
}
102113

114+
// Returns a list of keys from the dict
115+
func (d StringDict) M__iter__() (Object, error) {
116+
o := make([]Object, 0, len(d))
117+
for k := range d {
118+
o = append(o, String(k))
119+
}
120+
return NewIterator(o), nil
121+
}
122+
103123
func (d StringDict) M__getitem__(key Object) (Object, error) {
104124
str, ok := key.(String)
105125
if ok {
@@ -154,3 +174,15 @@ func (a StringDict) M__ne__(other Object) (Object, error) {
154174
}
155175
return True, nil
156176
}
177+
178+
func (a StringDict) M__contains__(other Object) (Object, error) {
179+
key, ok := other.(String)
180+
if !ok {
181+
return nil, ExceptionNewf(KeyError, "FIXME can only have string keys!: %v", key)
182+
}
183+
184+
if _, ok := a[string(key)]; ok {
185+
return True, nil
186+
}
187+
return False, nil
188+
}

py/list.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,29 @@ type List struct {
1313
Items []Object
1414
}
1515

16+
func init() {
17+
ListType.Dict["append"] = MustNewMethod("append", func(self Object, args Tuple) (Object, error) {
18+
listSelf := self.(*List)
19+
if len(args) != 1 {
20+
return nil, ExceptionNewf(TypeError, "append() takes exactly one argument (%d given)", len(args))
21+
}
22+
listSelf.Items = append(listSelf.Items, args[0])
23+
return NoneType{}, nil
24+
}, 0, "append(item)")
25+
26+
ListType.Dict["extend"] = MustNewMethod("extend", func(self Object, args Tuple) (Object, error) {
27+
listSelf := self.(*List)
28+
if len(args) != 1 {
29+
return nil, ExceptionNewf(TypeError, "append() takes exactly one argument (%d given)", len(args))
30+
}
31+
if oList, ok := args[0].(*List); ok {
32+
listSelf.Items = append(listSelf.Items, oList.Items...)
33+
}
34+
return NoneType{}, nil
35+
}, 0, "extend([item])")
36+
37+
}
38+
1639
// Type of this List object
1740
func (o *List) Type() *Type {
1841
return ListType
@@ -236,6 +259,9 @@ func (l *List) M__mul__(other Object) (Object, error) {
236259
if b, ok := convertToInt(other); ok {
237260
m := len(l.Items)
238261
n := int(b) * m
262+
if n < 0 {
263+
n = 0
264+
}
239265
newList := NewListSized(n)
240266
for i := 0; i < n; i += m {
241267
copy(newList.Items[i:i+m], l.Items)

0 commit comments

Comments
 (0)