I've recently downloaded the .Net extensions for Python, and I've found
some bugs regarding constructors and exceptions.
My test code is below, with two classes. The first takes a single
integer argument, and generates a division by zero exception if this is
zero. The second class will always generate an exception. Both
constructors will write a line to stdout when run.
MyTest.cs:
----------------------------------------------------------------------
using System;
using System.Collections;
using System.Management;
namespace MyTest
{
public class Bad
{
public Bad(int i)
{
Console.WriteLine("Test");
int i2 = 1;
int i1 = i;
i2 = i2 / i1;
}
}
public class Bad2
{
public Bad2()
{
Console.WriteLine("Test");
int i2 = 1;
int i1 = 0;
i2 = i2 / i1;
}
}
}
----------------------------------------------------------------------
test.py:
----------------------------------------------------------------------
import CLR.MyTest
import sys, traceback
try:
print repr(CLR.MyTest.Bad(1))
except:
print '-'*60
traceback.print_exc()
print '-'*60
try:
print repr(CLR.MyTest.Bad(0))
except:
print '-'*60
traceback.print_exc()
print '-'*60
try:
print repr(CLR.MyTest.Bad2())
except:
print '-'*60
traceback.print_exc()
print '-'*60
try:
print repr(CLR.MyTest.Bad2(1))
except:
print '-'*60
traceback.print_exc()
print '-'*60
----------------------------------------------------------------------
Output from test.py:
----------------------------------------------------------------------
Test
<CLR.MyTest.Bad object at 0x00B3F7B0>
Test
------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line 12, in ?
print repr(CLR.MyTest.Bad(0))
TypeError: no constructor matches given arguments
------------------------------------------------------------
Test
Test
------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line 19, in ?
print repr(CLR.MyTest.Bad2())
DivideByZeroException: Attempted to divide by zero.
at MyTest.Bad2..ctor()
------------------------------------------------------------
Test
------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line 26, in ?
print repr(CLR.MyTest.Bad2(1))
DivideByZeroException: Attempted to divide by zero.
at MyTest.Bad2..ctor()
------------------------------------------------------------
----------------------------------------------------------------------
The problems are as follows:
CLR.MyTest.Bad(0) -> Wrong traceback, wrong exception
CLR.MyTest.Bad2() -> Seems to call constructor twice (prints
"Test" twice.)
CLR.MyTest.Bad2(1) -> Calls constructor instead of raising
TypeError for bad number of arguments.
--
Dag Nummedal (Dag.Nummedal(a)ime.ntnu.no)