Skip to content

Commit c85199b

Browse files
committed
Raise internal errors properly using panic(ExceptionNewf(Exception, "msg"))
1 parent ef89736 commit c85199b

File tree

13 files changed

+85
-183
lines changed

13 files changed

+85
-183
lines changed

builtin/builtin.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,7 @@ func builtin_round(self py.Object, args py.Tuple, kwargs py.StringDict) py.Objec
206206

207207
numberRounder, ok := number.(py.I__round__)
208208
if !ok {
209-
// FIXME TypeError
210-
panic(fmt.Sprintf("TypeError: type %s doesn't define __round__ method", number.Type().Name))
209+
panic(py.ExceptionNewf(py.TypeError, "type %s doesn't define __round__ method", number.Type().Name))
211210
}
212211

213212
return numberRounder.M__round__(ndigits)
@@ -225,21 +224,18 @@ func builtin___build_class__(self py.Object, args py.Tuple, kwargs py.StringDict
225224
var isclass bool
226225

227226
if len(args) < 2 {
228-
// FIXME TypeError
229-
panic(fmt.Sprintf("TypeError: __build_class__: not enough arguments"))
227+
panic(py.ExceptionNewf(py.TypeError, "__build_class__: not enough arguments"))
230228
}
231229

232230
// Better be callable
233231
fn, ok := args[0].(*py.Function)
234232
if !ok {
235-
// FIXME TypeError
236-
panic(fmt.Sprintf("TypeError: __build__class__: func must be a function"))
233+
panic(py.ExceptionNewf(py.TypeError, "__build__class__: func must be a function"))
237234
}
238235

239236
name := args[1].(py.String)
240237
if !ok {
241-
// FIXME TypeError
242-
panic(fmt.Sprintf("TypeError: __build_class__: name is not a string"))
238+
panic(py.ExceptionNewf(py.TypeError, "__build_class__: name is not a string"))
243239
}
244240
bases := args[2:]
245241

notes.txt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
Todo
2+
====
3+
14
Note byte code is from python 3.3!
25

36
FIXME need to be able to tell classes an instances apart!
@@ -13,9 +16,8 @@ Make a gpython-minimal with no built in stdlib
1316

1417
Bytecode compile the standard library and embed into go files - can then make a gpython with no external files.
1518

16-
** Make a NewErrorf() so can make lots of type errors!
17-
1819
Testing
20+
=======
1921

2022
python3 -m dis hello.py
2123
python3 -mcompileall hello.py
@@ -26,6 +28,7 @@ Testing
2628
./gpython hello.pyc
2729

2830
Polymorphism
31+
============
2932

3033
Want to try to retain I__XXX__ and M__XXX__ for speed
3134

@@ -68,12 +71,6 @@ all together
6871
res = NotImplemented
6972
}
7073

71-
Idea
72-
73-
could run the vm in a go routine - then could start new stack frames in existing vm from go
74-
75-
BoundMethods should be run straight away in the vm not make a new vm
76-
7774
ObjectType in *Type is probably redundant
7875
- except that Base can be nil
7976

py/args.go

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,6 @@
403403

404404
package py
405405

406-
import (
407-
"fmt"
408-
)
409-
410406
// ParseTupleAndKeywords
411407
func ParseTupleAndKeywords(args Tuple, kwargs StringDict, format string, kwlist []string, results ...*Object) {
412408
if len(results) != len(kwlist) {
@@ -423,7 +419,7 @@ func ParseTupleAndKeywords(args Tuple, kwargs StringDict, format string, kwlist
423419
goto found
424420
}
425421
}
426-
panic(fmt.Sprintf("TypeError: %s() got an unexpected keyword argument '%s'", name, kwargName))
422+
panic(ExceptionNewf(TypeError, "%s() got an unexpected keyword argument '%s'", name, kwargName))
427423
found:
428424
}
429425

@@ -432,8 +428,7 @@ func ParseTupleAndKeywords(args Tuple, kwargs StringDict, format string, kwlist
432428
for i, kw := range kwlist {
433429
if value, ok := kwargs[kw]; ok {
434430
if len(args) >= i {
435-
// FIXME type error
436-
panic(fmt.Sprintf("TypeError: %s() got multiple values for argument '%s'", name, kw))
431+
panic(ExceptionNewf(TypeError, "%s() got multiple values for argument '%s'", name, kw))
437432
}
438433
args = append(args, value)
439434
}
@@ -448,12 +443,11 @@ func ParseTupleAndKeywords(args Tuple, kwargs StringDict, format string, kwlist
448443
*result = arg
449444
case "U":
450445
if _, ok := arg.(String); !ok {
451-
// FIXME type error
452-
panic(fmt.Sprintf("TypeError: %s() argument %d must be str, not %s", name, i+1, arg.Type().Name))
446+
panic(ExceptionNewf(TypeError, "%s() argument %d must be str, not %s", name, i+1, arg.Type().Name))
453447
}
454448
*result = arg
455449
default:
456-
panic(fmt.Sprintf("Unknown/Unimplemented format character %q in ParseTupleAndKeywords called from %s", op, name))
450+
panic(ExceptionNewf(TypeError, "Unknown/Unimplemented format character %q in ParseTupleAndKeywords called from %s", op, name))
457451
}
458452
}
459453
}
@@ -490,17 +484,14 @@ func parseFormat(format string) (min, max int, name string, ops []string) {
490484
func checkNumberOfArgs(name string, nargs, nresults, min, max int) {
491485
if min == max {
492486
if nargs != max {
493-
// FIXME type error
494-
panic(fmt.Sprintf("TypeError: %s() takes exactly %d arguments (%d given)", name, max, nargs))
487+
panic(ExceptionNewf(TypeError, "%s() takes exactly %d arguments (%d given)", name, max, nargs))
495488
}
496489
} else {
497490
if nargs > max {
498-
// FIXME type error
499-
panic(fmt.Sprintf("TypeError: %s() takes at most %d arguments (%d given)", name, max, nargs))
491+
panic(ExceptionNewf(TypeError, "%s() takes at most %d arguments (%d given)", name, max, nargs))
500492
}
501493
if nargs < min {
502-
// FIXME type error
503-
panic(fmt.Sprintf("TypeError: %s() takes at least %d arguments (%d given)", name, min, nargs))
494+
panic(ExceptionNewf(TypeError, "%s() takes at least %d arguments (%d given)", name, min, nargs))
504495
}
505496
}
506497

@@ -514,8 +505,7 @@ func checkNumberOfArgs(name string, nargs, nresults, min, max int) {
514505
// Up to the caller to set default values
515506
func UnpackTuple(args Tuple, kwargs StringDict, name string, min int, max int, results ...*Object) {
516507
if len(kwargs) != 0 {
517-
// FIXME type error
518-
panic(fmt.Sprintf("TypeError: %s() does not take keyword arguments", name))
508+
panic(ExceptionNewf(TypeError, "%s() does not take keyword arguments", name))
519509
}
520510

521511
// Check number of arguments

0 commit comments

Comments
 (0)