@@ -36,9 +36,7 @@ const debugging = false
36
36
37
37
// Debug print
38
38
func debugf (format string , a ... interface {}) {
39
- if debugging {
40
- fmt .Printf (format , a ... )
41
- }
39
+ fmt .Printf (format , a ... )
42
40
}
43
41
44
42
// Stack operations
@@ -114,12 +112,14 @@ func (vm *Vm) CheckExceptionRecover(r interface{}) {
114
112
vm .curexc = exc
115
113
vm .AddTraceback (& vm .curexc )
116
114
vm .why = whyException
117
- debugf ("*** Propagating exception: %s\n " , exc .Error ())
115
+ if debugging {
116
+ debugf ("*** Propagating exception: %s\n " , exc .Error ())
117
+ }
118
118
} else {
119
119
// Coerce whatever was raised into a *Exception
120
120
vm .SetException (py .MakeException (r ))
121
- debugf ("*** Exception raised %v\n " , r )
122
121
if debugging {
122
+ debugf ("*** Exception raised %v\n " , r )
123
123
debug .PrintStack ()
124
124
}
125
125
}
@@ -130,7 +130,9 @@ func (vm *Vm) CheckExceptionRecover(r interface{}) {
130
130
// Must be called as a defer function
131
131
func (vm * Vm ) CheckException () {
132
132
if r := recover (); r != nil {
133
- debugf ("*** Panic recovered %v\n " , r )
133
+ if debugging {
134
+ debugf ("*** Panic recovered %v\n " , r )
135
+ }
134
136
vm .CheckExceptionRecover (r )
135
137
}
136
138
}
@@ -694,13 +696,19 @@ func do_POP_EXCEPT(vm *Vm, arg int32) error {
694
696
// continues with the outer-next block.
695
697
func do_END_FINALLY (vm * Vm , arg int32 ) error {
696
698
v := vm .POP ()
697
- debugf ("END_FINALLY v=%#v\n " , v )
699
+ if debugging {
700
+ debugf ("END_FINALLY v=%#v\n " , v )
701
+ }
698
702
if v == py .None {
699
703
// None exception
700
- debugf (" END_FINALLY: None\n " )
704
+ if debugging {
705
+ debugf (" END_FINALLY: None\n " )
706
+ }
701
707
} else if vInt , ok := v .(py.Int ); ok {
702
708
vm .why = vmStatus (vInt )
703
- debugf (" END_FINALLY: Int %v\n " , vm .why )
709
+ if debugging {
710
+ debugf (" END_FINALLY: Int %v\n " , vm .why )
711
+ }
704
712
switch vm .why {
705
713
case whyYield :
706
714
panic ("vm: Unexpected whyYield in END_FINALLY" )
@@ -725,7 +733,9 @@ func do_END_FINALLY(vm *Vm, arg int32) error {
725
733
} else if py .ExceptionClassCheck (v ) {
726
734
w := vm .POP ()
727
735
u := vm .POP ()
728
- debugf (" END_FINALLY: Exc %v, Type %v, Traceback %v\n " , v , w , u )
736
+ if debugging {
737
+ debugf (" END_FINALLY: Exc %v, Type %v, Traceback %v\n " , v , w , u )
738
+ }
729
739
// FIXME PyErr_Restore(v, w, u)
730
740
vm .curexc .Type , _ = v .(* py.Type )
731
741
vm .curexc .Value = w
@@ -734,7 +744,9 @@ func do_END_FINALLY(vm *Vm, arg int32) error {
734
744
} else {
735
745
return py .ExceptionNewf (py .SystemError , "'finally' pops bad exception %#v" , v )
736
746
}
737
- debugf ("END_FINALLY: vm.why = %v\n " , vm .why )
747
+ if debugging {
748
+ debugf ("END_FINALLY: vm.why = %v\n " , vm .why )
749
+ }
738
750
return nil
739
751
}
740
752
@@ -861,7 +873,9 @@ func do_WITH_CLEANUP(vm *Vm, arg int32) error {
861
873
// co_names of the code object. The compiler tries to use STORE_FAST
862
874
// or STORE_GLOBAL if possible.
863
875
func do_STORE_NAME (vm * Vm , namei int32 ) error {
864
- debugf ("STORE_NAME %v\n " , vm .frame .Code .Names [namei ])
876
+ if debugging {
877
+ debugf ("STORE_NAME %v\n " , vm .frame .Code .Names [namei ])
878
+ }
865
879
vm .frame .Locals [vm .frame .Code .Names [namei ]] = vm .POP ()
866
880
return nil
867
881
}
@@ -934,14 +948,16 @@ func do_DELETE_GLOBAL(vm *Vm, namei int32) error {
934
948
// Pushes co_consts[consti] onto the stack.
935
949
func do_LOAD_CONST (vm * Vm , consti int32 ) error {
936
950
vm .PUSH (vm .frame .Code .Consts [consti ])
937
- // debugf("LOAD_CONST %v\n", vm.TOP())
951
+ // if debugging { debugf("LOAD_CONST %v\n", vm.TOP()) }
938
952
return nil
939
953
}
940
954
941
955
// Pushes the value associated with co_names[namei] onto the stack.
942
956
func do_LOAD_NAME (vm * Vm , namei int32 ) error {
943
957
name := vm .frame .Code .Names [namei ]
944
- debugf ("LOAD_NAME %v\n " , name )
958
+ if debugging {
959
+ debugf ("LOAD_NAME %v\n " , name )
960
+ }
945
961
obj , ok := vm .frame .Lookup (name )
946
962
if ! ok {
947
963
return py .ExceptionNewf (py .NameError , nameErrorMsg , name )
@@ -1177,7 +1193,9 @@ func do_FOR_ITER(vm *Vm, delta int32) error {
1177
1193
// Loads the global named co_names[namei] onto the stack.
1178
1194
func do_LOAD_GLOBAL (vm * Vm , namei int32 ) error {
1179
1195
name := vm .frame .Code .Names [namei ]
1180
- debugf ("LOAD_GLOBAL %v\n " , name )
1196
+ if debugging {
1197
+ debugf ("LOAD_GLOBAL %v\n " , name )
1198
+ }
1181
1199
obj , ok := vm .frame .LookupGlobal (name )
1182
1200
if ! ok {
1183
1201
return py .ExceptionNewf (py .NameError , nameErrorMsg , name )
@@ -1351,7 +1369,9 @@ func (vm *Vm) raise(exc, cause py.Object) error {
1351
1369
// raise <instance>
1352
1370
// raise <type>
1353
1371
excException := py .MakeException (exc )
1354
- debugf ("raise: excException = %v\n " , excException )
1372
+ if debugging {
1373
+ debugf ("raise: excException = %v\n " , excException )
1374
+ }
1355
1375
if cause != nil {
1356
1376
excException .Cause = py .MakeException (cause )
1357
1377
}
@@ -1570,9 +1590,9 @@ func callInternal(fn py.Object, args py.Tuple, kwargs py.StringDict, f *py.Frame
1570
1590
//
1571
1591
// The result is put on the stack
1572
1592
func (vm * Vm ) Call (argc int32 , starArgs py.Object , starKwargs py.Object ) error {
1573
- // debugf("Stack: %v\n", vm.frame.Stack)
1574
- // debugf("Locals: %v\n", vm.frame.Locals)
1575
- // debugf("Globals: %v\n", vm.frame.Globals)
1593
+ // if debugging { debugf("Stack: %v\n", vm.frame.Stack) }
1594
+ // if debugging { debugf("Locals: %v\n", vm.frame.Locals) }
1595
+ // if debugging { debugf("Globals: %v\n", vm.frame.Globals) }
1576
1596
1577
1597
// Get the arguments off the stack
1578
1598
nargs := int (argc & 0xFF )
@@ -1588,7 +1608,7 @@ func (vm *Vm) Call(argc int32, starArgs py.Object, starKwargs py.Object) error {
1588
1608
1589
1609
const multipleValues = "%s%s got multiple values for keyword argument '%s'"
1590
1610
1591
- // debugf("Call %T %v with args = %v, kwargsTuple = %v\n", fnObj, fnObj, args, kwargsTuple)
1611
+ // if debugging { debugf("Call %T %v with args = %v, kwargsTuple = %v\n", fnObj, fnObj, args, kwargsTuple) }
1592
1612
var kwargs py.StringDict
1593
1613
if len (kwargsTuple ) > 0 {
1594
1614
// Convert kwargsTuple into dictionary
@@ -1660,17 +1680,23 @@ func (vm *Vm) UnwindBlock(frame *py.Frame, block *py.TryBlock) {
1660
1680
1661
1681
// Unwinds the stack in the presence of an exception
1662
1682
func (vm * Vm ) UnwindExceptHandler (frame * py.Frame , block * py.TryBlock ) {
1663
- debugf ("** UnwindExceptHandler stack depth %v\n " , vm .STACK_LEVEL ())
1683
+ if debugging {
1684
+ debugf ("** UnwindExceptHandler stack depth %v\n " , vm .STACK_LEVEL ())
1685
+ }
1664
1686
if vm .STACK_LEVEL () < block .Level + 3 {
1665
1687
panic ("vm: Couldn't find traceback on stack" )
1666
1688
} else {
1667
1689
frame .Stack = frame .Stack [:block .Level + 3 ]
1668
1690
}
1669
- debugf ("** UnwindExceptHandler stack depth now %v\n " , vm .STACK_LEVEL ())
1691
+ if debugging {
1692
+ debugf ("** UnwindExceptHandler stack depth now %v\n " , vm .STACK_LEVEL ())
1693
+ }
1670
1694
vm .exc .Type , _ = vm .POP ().(* py.Type )
1671
1695
vm .exc .Value = vm .POP ()
1672
1696
vm .exc .Traceback , _ = vm .POP ().(* py.Traceback )
1673
- debugf ("** UnwindExceptHandler exc = (type: %v, value: %v, traceback: %v)\n " , vm .exc .Type , vm .exc .Value , vm .exc .Traceback )
1697
+ if debugging {
1698
+ debugf ("** UnwindExceptHandler exc = (type: %v, value: %v, traceback: %v)\n " , vm .exc .Type , vm .exc .Value , vm .exc .Traceback )
1699
+ }
1674
1700
}
1675
1701
1676
1702
// Run the virtual machine on a Frame object
@@ -1692,7 +1718,7 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
1692
1718
// default:
1693
1719
// err = errors.New(fmt.Sprintf("Unknown error '%s'", x))
1694
1720
// }
1695
- // debugf("*** Exception raised %v\n", r)
1721
+ // if debugging { debugf("*** Exception raised %v\n", r) }
1696
1722
// // Dump the goroutine stack
1697
1723
// debug.PrintStack()
1698
1724
// }
@@ -1718,7 +1744,9 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
1718
1744
var arg int32
1719
1745
for vm .why == whyNot {
1720
1746
frame := vm .frame
1721
- debugf ("* %4d:" , frame .Lasti )
1747
+ if debugging {
1748
+ debugf ("* %4d:" , frame .Lasti )
1749
+ }
1722
1750
opcodes := frame .Code .Code
1723
1751
opcode = OpCode (opcodes [frame .Lasti ])
1724
1752
frame .Lasti ++
@@ -1730,9 +1758,13 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
1730
1758
if vm .extended {
1731
1759
arg += vm .ext << 16
1732
1760
}
1733
- debugf (" %v(%d)\n " , opcode , arg )
1761
+ if debugging {
1762
+ debugf (" %v(%d)\n " , opcode , arg )
1763
+ }
1734
1764
} else {
1735
- debugf (" %v\n " , opcode )
1765
+ if debugging {
1766
+ debugf (" %v\n " , opcode )
1767
+ }
1736
1768
}
1737
1769
vm .extended = false
1738
1770
err = jumpTable [opcode ](vm , arg )
@@ -1747,10 +1779,12 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
1747
1779
}
1748
1780
}
1749
1781
if vm .frame != nil {
1750
- debugf ("* Stack = %#v\n " , vm .frame .Stack )
1782
+ if debugging {
1783
+ debugf ("* Stack = %#v\n " , vm .frame .Stack )
1784
+ }
1751
1785
// if len(vm.frame.Stack) > 0 {
1752
1786
// if t, ok := vm.TOP().(*py.Type); ok {
1753
- // debugf(" * TOP = %#v\n", t)
1787
+ // if debugging { debugf(" * TOP = %#v\n", t) }
1754
1788
// }
1755
1789
// }
1756
1790
}
@@ -1764,7 +1798,9 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
1764
1798
// Peek at the current block.
1765
1799
frame := vm .frame
1766
1800
b := frame .Block
1767
- debugf ("*** Unwinding %#v vm %#v\n " , b , vm )
1801
+ if debugging {
1802
+ debugf ("*** Unwinding %#v vm %#v\n " , b , vm )
1803
+ }
1768
1804
1769
1805
if b .Type == py .TryBlockSetupLoop && vm .why == whyContinue {
1770
1806
vm .why = whyNot
@@ -1777,19 +1813,25 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
1777
1813
frame .PopBlock ()
1778
1814
1779
1815
if b .Type == py .TryBlockExceptHandler {
1780
- debugf ("*** EXCEPT_HANDLER\n " )
1816
+ if debugging {
1817
+ debugf ("*** EXCEPT_HANDLER\n " )
1818
+ }
1781
1819
vm .UnwindExceptHandler (frame , b )
1782
1820
continue
1783
1821
}
1784
1822
vm .UnwindBlock (frame , b )
1785
1823
if b .Type == py .TryBlockSetupLoop && vm .why == whyBreak {
1786
- debugf ("*** Loop\n " )
1824
+ if debugging {
1825
+ debugf ("*** Loop\n " )
1826
+ }
1787
1827
vm .why = whyNot
1788
1828
frame .Lasti = b .Handler
1789
1829
break
1790
1830
}
1791
1831
if vm .why == whyException && (b .Type == py .TryBlockSetupExcept || b .Type == py .TryBlockSetupFinally ) {
1792
- debugf ("*** Exception\n " )
1832
+ if debugging {
1833
+ debugf ("*** Exception\n " )
1834
+ }
1793
1835
handler := b .Handler
1794
1836
// This invalidates b
1795
1837
frame .PushBlock (py .TryBlockExceptHandler , - 1 , vm .STACK_LEVEL ())
@@ -1838,7 +1880,9 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
1838
1880
}
1839
1881
}
1840
1882
}
1841
- debugf ("EXIT with %v\n " , vm .why )
1883
+ if debugging {
1884
+ debugf ("EXIT with %v\n " , vm .why )
1885
+ }
1842
1886
if vm .why != whyReturn {
1843
1887
vm .retval = nil
1844
1888
}
0 commit comments