Skip to content

Commit d58e96a

Browse files
committed
Add compile time option for debug messages
1 parent 49bcc8f commit d58e96a

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

vm/eval.go

+31-26
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ objects so they can be GCed
3232
*/
3333

3434
import (
35-
"fmt"
35+
// "fmt"
3636
"github.com/ncw/gpython/py"
3737
"runtime/debug"
3838
)
@@ -45,6 +45,11 @@ const (
4545
cannotCatchMsg = "catching '%s' that does not inherit from BaseException is not allowed"
4646
)
4747

48+
// Debug print
49+
func debugf(format string, a ...interface{}) {
50+
// fmt.Printf(format, a...)
51+
}
52+
4853
// Stack operations
4954
func (vm *Vm) STACK_LEVEL() int { return len(vm.frame.Stack) }
5055
func (vm *Vm) EMPTY() bool { return len(vm.frame.Stack) == 0 }
@@ -124,11 +129,11 @@ func (vm *Vm) CheckExceptionRecover(r interface{}) {
124129
vm.exc = exc
125130
vm.AddTraceback(&vm.exc)
126131
vm.exit = exitException
127-
fmt.Printf("*** Propagating exception: %s\n", exc.Error())
132+
debugf("*** Propagating exception: %s\n", exc.Error())
128133
} else {
129134
// Coerce whatever was raised into a *Exception
130135
vm.SetException(py.MakeException(r))
131-
fmt.Printf("*** Exception raised %v\n", r)
136+
debugf("*** Exception raised %v\n", r)
132137
// Dump the goroutine stack
133138
debug.PrintStack()
134139
}
@@ -556,7 +561,7 @@ func do_RETURN_VALUE(vm *Vm, arg int32) {
556561
defer vm.CheckException()
557562
vm.result = vm.POP()
558563
if len(vm.frame.Stack) != 0 {
559-
fmt.Printf("vmstack = %#v\n", vm.frame.Stack)
564+
debugf("vmstack = %#v\n", vm.frame.Stack)
560565
panic("vm stack should be empty at this point")
561566
}
562567
vm.frame.Yielded = false
@@ -639,7 +644,7 @@ func do_POP_EXCEPT(vm *Vm, arg int32) {
639644
func do_END_FINALLY(vm *Vm, arg int32) {
640645
defer vm.CheckException()
641646
v := vm.POP()
642-
fmt.Printf("END_FINALLY v=%v\n", v)
647+
debugf("END_FINALLY v=%v\n", v)
643648
if vInt, ok := v.(py.Int); ok {
644649
vm.exit = vmExit(vInt)
645650
switch vm.exit {
@@ -727,7 +732,7 @@ func do_WITH_CLEANUP(vm *Vm, arg int32) {
727732
// or STORE_GLOBAL if possible.
728733
func do_STORE_NAME(vm *Vm, namei int32) {
729734
defer vm.CheckException()
730-
fmt.Printf("STORE_NAME %v\n", vm.frame.Code.Names[namei])
735+
debugf("STORE_NAME %v\n", vm.frame.Code.Names[namei])
731736
vm.frame.Locals[vm.frame.Code.Names[namei]] = vm.POP()
732737
}
733738

@@ -791,13 +796,13 @@ func do_DELETE_GLOBAL(vm *Vm, namei int32) {
791796
func do_LOAD_CONST(vm *Vm, consti int32) {
792797
defer vm.CheckException()
793798
vm.PUSH(vm.frame.Code.Consts[consti])
794-
// fmt.Printf("LOAD_CONST %v\n", vm.TOP())
799+
// debugf("LOAD_CONST %v\n", vm.TOP())
795800
}
796801

797802
// Pushes the value associated with co_names[namei] onto the stack.
798803
func do_LOAD_NAME(vm *Vm, namei int32) {
799804
defer vm.CheckException()
800-
fmt.Printf("LOAD_NAME %v\n", vm.frame.Code.Names[namei])
805+
debugf("LOAD_NAME %v\n", vm.frame.Code.Names[namei])
801806
vm.PUSH(vm.frame.Lookup(vm.frame.Code.Names[namei]))
802807
}
803808

@@ -1014,7 +1019,7 @@ func do_FOR_ITER(vm *Vm, delta int32) {
10141019
func do_LOAD_GLOBAL(vm *Vm, namei int32) {
10151020
defer vm.CheckException()
10161021
// FIXME this is looking in local scope too - is that correct?
1017-
fmt.Printf("LOAD_GLOBAL %v\n", vm.frame.Code.Names[namei])
1022+
debugf("LOAD_GLOBAL %v\n", vm.frame.Code.Names[namei])
10181023
vm.PUSH(vm.frame.Lookup(vm.frame.Code.Names[namei]))
10191024
}
10201025

@@ -1055,7 +1060,7 @@ func do_STORE_MAP(vm *Vm, arg int32) {
10551060
func do_LOAD_FAST(vm *Vm, var_num int32) {
10561061
defer vm.CheckException()
10571062
varname := vm.frame.Code.Varnames[var_num]
1058-
fmt.Printf("LOAD_FAST %q\n", varname)
1063+
debugf("LOAD_FAST %q\n", varname)
10591064
if value, ok := vm.frame.Locals[varname]; ok {
10601065
vm.PUSH(value)
10611066
} else {
@@ -1191,9 +1196,9 @@ func do_RAISE_VARARGS(vm *Vm, argc int32) {
11911196
// pushes the return value.
11921197
func do_CALL_FUNCTION(vm *Vm, argc int32) {
11931198
defer vm.CheckException()
1194-
// fmt.Printf("Stack: %v\n", vm.frame.Stack)
1195-
// fmt.Printf("Locals: %v\n", vm.frame.Locals)
1196-
// fmt.Printf("Globals: %v\n", vm.frame.Globals)
1199+
// debugf("Stack: %v\n", vm.frame.Stack)
1200+
// debugf("Locals: %v\n", vm.frame.Locals)
1201+
// debugf("Globals: %v\n", vm.frame.Globals)
11971202
nargs := int(argc & 0xFF)
11981203
nkwargs := int((argc >> 8) & 0xFF)
11991204
p, q := len(vm.frame.Stack)-2*nkwargs, len(vm.frame.Stack)
@@ -1334,8 +1339,8 @@ func do_CALL_FUNCTION_VAR_KW(vm *Vm, argc int32) {
13341339

13351340
// NotImplemented
13361341
func (vm *Vm) NotImplemented(name string, arg int32) {
1337-
fmt.Printf("%s %d NOT IMPLEMENTED\n", name, arg)
1338-
fmt.Printf("vmstack = %#v\n", vm.frame.Stack)
1342+
debugf("%s %d NOT IMPLEMENTED\n", name, arg)
1343+
debugf("vmstack = %#v\n", vm.frame.Stack)
13391344
panic(py.ExceptionNewf(py.SystemError, "Opcode %s %d NOT IMPLEMENTED", name, arg))
13401345
}
13411346

@@ -1348,7 +1353,7 @@ func (vm *Vm) NotImplemented(name string, arg int32) {
13481353
//
13491354
// The result is put on the stack
13501355
func (vm *Vm) Call(fnObj py.Object, args []py.Object, kwargsTuple []py.Object) {
1351-
// fmt.Printf("Call %T %v with args = %v, kwargsTuple = %v\n", fnObj, fnObj, args, kwargsTuple)
1356+
// debugf("Call %T %v with args = %v, kwargsTuple = %v\n", fnObj, fnObj, args, kwargsTuple)
13521357
var kwargs py.StringDict
13531358
if len(kwargsTuple) > 0 {
13541359
// Convert kwargsTuple into dictionary
@@ -1413,7 +1418,7 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
14131418
// default:
14141419
// err = errors.New(fmt.Sprintf("Unknown error '%s'", x))
14151420
// }
1416-
// fmt.Printf("*** Exception raised %v\n", r)
1421+
// debugf("*** Exception raised %v\n", r)
14171422
// // Dump the goroutine stack
14181423
// debug.PrintStack()
14191424
// }
@@ -1423,7 +1428,7 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
14231428
var arg int32
14241429
for vm.exit == exitNot {
14251430
frame := vm.frame
1426-
fmt.Printf("* %4d:", frame.Lasti)
1431+
debugf("* %4d:", frame.Lasti)
14271432
opcodes := frame.Code.Code
14281433
opcode = opcodes[frame.Lasti]
14291434
frame.Lasti++
@@ -1435,17 +1440,17 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
14351440
if vm.extended {
14361441
arg += vm.ext << 16
14371442
}
1438-
fmt.Printf(" %s(%d)\n", OpCodeToName[opcode], arg)
1443+
debugf(" %s(%d)\n", OpCodeToName[opcode], arg)
14391444
} else {
1440-
fmt.Printf(" %s\n", OpCodeToName[opcode])
1445+
debugf(" %s\n", OpCodeToName[opcode])
14411446
}
14421447
vm.extended = false
14431448
jumpTable[opcode](vm, arg)
14441449
if vm.frame != nil {
1445-
fmt.Printf("* Stack = %#v\n", vm.frame.Stack)
1450+
debugf("* Stack = %#v\n", vm.frame.Stack)
14461451
// if len(vm.frame.Stack) > 0 {
14471452
// if t, ok := vm.TOP().(*py.Type); ok {
1448-
// fmt.Printf(" * TOP = %#v\n", t)
1453+
// debugf(" * TOP = %#v\n", t)
14491454
// }
14501455
// }
14511456
}
@@ -1456,7 +1461,7 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
14561461
// Peek at the current block.
14571462
frame := vm.frame
14581463
b := frame.Block
1459-
fmt.Printf("*** Unwinding %#v vm %#v\n", b, vm)
1464+
debugf("*** Unwinding %#v vm %#v\n", b, vm)
14601465

14611466
if vm.exit == exitYield {
14621467
return vm.result, nil
@@ -1466,19 +1471,19 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
14661471
frame.PopBlock()
14671472

14681473
if b.Type == EXCEPT_HANDLER {
1469-
fmt.Printf("*** EXCEPT_HANDLER\n")
1474+
debugf("*** EXCEPT_HANDLER\n")
14701475
vm.UnwindExceptHandler(frame, b)
14711476
continue
14721477
}
14731478
vm.UnwindBlock(frame, b)
14741479
if b.Type == SETUP_LOOP && vm.exit == exitBreak {
1475-
fmt.Printf("*** Loop\n")
1480+
debugf("*** Loop\n")
14761481
vm.exit = exitNot
14771482
frame.Lasti = b.Handler
14781483
break
14791484
}
14801485
if vm.exit&(exitException|exitReraise) != 0 && (b.Type == SETUP_EXCEPT || b.Type == SETUP_FINALLY) {
1481-
fmt.Printf("*** Exception\n")
1486+
debugf("*** Exception\n")
14821487
handler := b.Handler
14831488
// This invalidates b
14841489
frame.PushBlock(EXCEPT_HANDLER, -1, vm.STACK_LEVEL())

0 commit comments

Comments
 (0)