merged method selections and fixes from the trunk
This commit is contained in:
Родитель
804cd5160a
Коммит
4e2a3bd454
|
@ -29,7 +29,18 @@ namespace Python.Runtime {
|
|||
}
|
||||
|
||||
[CallConvCdecl()]
|
||||
public static IntPtr tp_new(IntPtr ob, IntPtr args, IntPtr kw) {
|
||||
public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw) {
|
||||
ArrayObject self = GetManagedObject(tp) as ArrayObject;
|
||||
if (Runtime.PyTuple_Size(args) != 1) {
|
||||
return Exceptions.RaiseTypeError("array expects 1 argument");
|
||||
}
|
||||
IntPtr op = Runtime.PyTuple_GetItem(args, 0);
|
||||
Object result;
|
||||
|
||||
if (!Converter.ToManaged(op, self.type, out result, true)) {
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
return CLRObject.GetInstHandle(result, tp);
|
||||
string message = "cannot instantiate array wrapper";
|
||||
return Exceptions.RaiseTypeError(message);
|
||||
}
|
||||
|
|
|
@ -50,6 +50,16 @@ namespace Python.Runtime {
|
|||
return 0;
|
||||
}
|
||||
|
||||
|
||||
//====================================================================
|
||||
// Default implementation of [] semantics for reflected types.
|
||||
//====================================================================
|
||||
|
||||
public virtual IntPtr type_subscript(IntPtr ob, IntPtr idx) {
|
||||
return Exceptions.RaiseTypeError("unsubscriptable object");
|
||||
}
|
||||
|
||||
|
||||
//====================================================================
|
||||
// Standard comparison implementation for instances of reflected types.
|
||||
//====================================================================
|
||||
|
|
|
@ -126,6 +126,32 @@ namespace Python.Runtime {
|
|||
}
|
||||
|
||||
|
||||
//====================================================================
|
||||
// Implementation of [] semantics for reflected types. This mainly
|
||||
// exists to implement the Array[int] syntax for creating arrays.
|
||||
//====================================================================
|
||||
|
||||
public override IntPtr type_subscript(IntPtr ob, IntPtr idx) {
|
||||
if ((this.type) != typeof(Array)) {
|
||||
return Exceptions.RaiseTypeError("unsubscriptable object");
|
||||
}
|
||||
|
||||
if (Runtime.PyTuple_Check(idx)) {
|
||||
return Exceptions.RaiseTypeError("expected single type");
|
||||
}
|
||||
|
||||
ClassBase c = GetManagedObject(idx) as ClassBase;
|
||||
Type t = (c != null) ? c.type : Converter.GetTypeByAlias(idx);
|
||||
if (t == null) {
|
||||
return Exceptions.RaiseTypeError("type expected");
|
||||
}
|
||||
Array a = Array.CreateInstance(t, 0);
|
||||
c = ClassManager.GetClass(a.GetType());
|
||||
Runtime.Incref(c.pyHandle);
|
||||
return c.pyHandle;
|
||||
}
|
||||
|
||||
|
||||
//====================================================================
|
||||
// Implements __getitem__ for reflected classes and value types.
|
||||
//====================================================================
|
||||
|
|
|
@ -33,7 +33,12 @@ namespace Python.Runtime {
|
|||
// Python type to use when wrapping the result (may be a subclass).
|
||||
//====================================================================
|
||||
|
||||
internal object InvokeRaw(IntPtr inst, IntPtr args, IntPtr kw) {
|
||||
internal object InvokeRaw(IntPtr inst, IntPtr args, IntPtr kw) {
|
||||
return this.InvokeRaw(inst, args, kw, null);
|
||||
}
|
||||
|
||||
internal object InvokeRaw(IntPtr inst, IntPtr args, IntPtr kw,
|
||||
MethodBase info) {
|
||||
Binding binding = this.Bind(inst, args, kw);
|
||||
Object result;
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@ namespace Python.Runtime {
|
|||
static NumberFormatInfo nfi;
|
||||
static Type objectType;
|
||||
static Type stringType;
|
||||
static Type doubleType;
|
||||
static Type int32Type;
|
||||
static Type int64Type;
|
||||
static Type flagsType;
|
||||
|
@ -40,12 +41,38 @@ namespace Python.Runtime {
|
|||
stringType = typeof(String);
|
||||
int32Type = typeof(Int32);
|
||||
int64Type = typeof(Int64);
|
||||
doubleType = typeof(Double);
|
||||
flagsType = typeof(FlagsAttribute);
|
||||
boolType = typeof(Boolean);
|
||||
typeType = typeof(Type);
|
||||
}
|
||||
|
||||
|
||||
//====================================================================
|
||||
// Given a builtin Python type, return the corresponding CLR type.
|
||||
//====================================================================
|
||||
|
||||
internal static Type GetTypeByAlias(IntPtr op) {
|
||||
if ((op == Runtime.PyStringType) ||
|
||||
(op == Runtime.PyUnicodeType)) {
|
||||
return stringType;
|
||||
}
|
||||
else if (op == Runtime.PyIntType) {
|
||||
return int32Type;
|
||||
}
|
||||
else if (op == Runtime.PyLongType) {
|
||||
return int64Type;
|
||||
}
|
||||
else if (op == Runtime.PyFloatType) {
|
||||
return doubleType;
|
||||
}
|
||||
else if (op == Runtime.PyBoolType) {
|
||||
return boolType;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
//====================================================================
|
||||
// Return a Python object for the given native object, converting
|
||||
// basic types (string, int, etc.) into equivalent Python objects.
|
||||
|
@ -177,7 +204,6 @@ namespace Python.Runtime {
|
|||
|
||||
internal static bool ToManagedValue(IntPtr value, Type obType,
|
||||
out Object result, bool setError) {
|
||||
|
||||
// Common case: if the Python value is a wrapped managed object
|
||||
// instance, just return the wrapped object.
|
||||
ManagedType mt = ManagedType.GetManagedObject(value);
|
||||
|
@ -199,6 +225,12 @@ namespace Python.Runtime {
|
|||
mt = ManagedType.GetManagedObject(value);
|
||||
}
|
||||
}
|
||||
IntPtr c = Exceptions.UnwrapExceptionClass(value);
|
||||
if ((c != IntPtr.Zero) && (c != value)) {
|
||||
value = c;
|
||||
Runtime.Decref(c);
|
||||
mt = ManagedType.GetManagedObject(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -601,6 +633,8 @@ namespace Python.Runtime {
|
|||
|
||||
Array items = Array.CreateInstance(elementType, size);
|
||||
|
||||
// XXX - is there a better way to unwrap this if it is a real
|
||||
// array?
|
||||
for (int i = 0; i < size; i++) {
|
||||
Object obj = null;
|
||||
IntPtr item = Runtime.PySequence_GetItem(value, i);
|
||||
|
|
|
@ -192,6 +192,23 @@ namespace Python.Runtime {
|
|||
return op;
|
||||
}
|
||||
|
||||
internal static IntPtr UnwrapExceptionClass(IntPtr op) {
|
||||
// In some cases its necessary to recognize an exception *class*,
|
||||
// and obtain the inner (wrapped) exception class. This method
|
||||
// returns the inner class if found, or a null pointer.
|
||||
|
||||
IntPtr d = Runtime.PyObject_GetAttrString(op, "__dict__");
|
||||
if (d == IntPtr.Zero) {
|
||||
Exceptions.Clear();
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
IntPtr c = Runtime.PyDict_GetItemString(d, "_class");
|
||||
Runtime.Decref(d);
|
||||
if (c == IntPtr.Zero) {
|
||||
Exceptions.Clear();
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -46,6 +46,18 @@ namespace Python.Runtime {
|
|||
GCHandle gc = (GCHandle)op;
|
||||
return (ManagedType)gc.Target;
|
||||
}
|
||||
|
||||
// In certain situations, we need to recognize a wrapped
|
||||
// exception class and be willing to unwrap the class :(
|
||||
|
||||
if (Runtime.wrap_exceptions) {
|
||||
IntPtr e = Exceptions.UnwrapExceptionClass(ob);
|
||||
if ((e != IntPtr.Zero) && (e != ob)) {
|
||||
ManagedType m = GetManagedObject(e);
|
||||
Runtime.Decref(e);
|
||||
return m;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -220,6 +220,20 @@ namespace Python.Runtime {
|
|||
return 0;
|
||||
}
|
||||
|
||||
//====================================================================
|
||||
// The metatype has to implement [] semantics for generic types, so
|
||||
// here we just delegate to the generic type def implementation. Its
|
||||
// own mp_subscript
|
||||
//====================================================================
|
||||
|
||||
[CallConvCdecl()]
|
||||
public static IntPtr mp_subscript(IntPtr tp, IntPtr idx) {
|
||||
ClassBase cb = GetManagedObject(tp) as ClassBase;
|
||||
if (cb != null) {
|
||||
return cb.type_subscript(tp, idx);
|
||||
}
|
||||
return Exceptions.RaiseTypeError("unsubscriptable object");
|
||||
}
|
||||
|
||||
//====================================================================
|
||||
// Dealloc implementation. This is called when a Python type generated
|
||||
|
|
|
@ -114,6 +114,60 @@ namespace Python.Runtime {
|
|||
}
|
||||
|
||||
|
||||
internal static MethodInfo MatchByTypeSig(MethodInfo[] msig,
|
||||
IntPtr psig) {
|
||||
IntPtr args = psig;
|
||||
bool free = false;
|
||||
|
||||
if (!Runtime.PyTuple_Check(psig)) {
|
||||
args = Runtime.PyTuple_New(1);
|
||||
Runtime.Incref(psig);
|
||||
Runtime.PyTuple_SetItem(args, 0, psig);
|
||||
free = true;
|
||||
}
|
||||
|
||||
int plen = Runtime.PyTuple_Size(args);
|
||||
MethodInfo match = null;
|
||||
|
||||
// XXX: what about out args, etc.?
|
||||
|
||||
for (int i = 0; i < msig.Length; i++) {
|
||||
ParameterInfo[] pi = msig[i].GetParameters();
|
||||
if (pi.Length != plen) {
|
||||
continue;
|
||||
}
|
||||
bool matched = true;
|
||||
for (int n = 0; n < pi.Length; n++) {
|
||||
IntPtr p = Runtime.PyTuple_GetItem(args, n);
|
||||
if (p == IntPtr.Zero) {
|
||||
Exceptions.Clear();
|
||||
break;
|
||||
}
|
||||
ClassBase c = ManagedType.GetManagedObject(p) as ClassBase;
|
||||
Type t = (c != null) ? c.type :
|
||||
Converter.GetTypeByAlias(p);
|
||||
|
||||
if (t == null) {
|
||||
break;
|
||||
}
|
||||
if (t != pi[n].ParameterType) {
|
||||
matched = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (matched) {
|
||||
match = msig[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (free) {
|
||||
Runtime.Decref(args);
|
||||
}
|
||||
|
||||
return match;
|
||||
}
|
||||
|
||||
|
||||
//====================================================================
|
||||
// Bind the given Python instance and arguments to a particular method
|
||||
|
@ -122,12 +176,26 @@ namespace Python.Runtime {
|
|||
//====================================================================
|
||||
|
||||
internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw) {
|
||||
return this.Bind(inst, args, kw, null);
|
||||
}
|
||||
|
||||
internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw,
|
||||
MethodBase info) {
|
||||
// loop to find match, return invoker w/ or /wo error
|
||||
MethodBase[] _methods = null;
|
||||
int nargs = Runtime.PyTuple_Size(args);
|
||||
object arg;
|
||||
|
||||
MethodBase[] _methods = GetMethods();
|
||||
|
||||
if (info != null) {
|
||||
_methods = (MethodBase[])Array.CreateInstance(
|
||||
typeof(MethodBase), 1
|
||||
);
|
||||
_methods.SetValue(info, 0);
|
||||
}
|
||||
else {
|
||||
_methods = GetMethods();
|
||||
}
|
||||
|
||||
for (int i = 0; i < _methods.Length; i++) {
|
||||
MethodBase mi = _methods[i];
|
||||
ParameterInfo[] pi = mi.GetParameters();
|
||||
|
@ -171,9 +239,14 @@ namespace Python.Runtime {
|
|||
return null;
|
||||
}
|
||||
|
||||
|
||||
internal virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw) {
|
||||
Binding binding = this.Bind(inst, args, kw);
|
||||
return this.Invoke(inst, args, kw, null);
|
||||
|
||||
}
|
||||
|
||||
internal virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw,
|
||||
MethodBase info) {
|
||||
Binding binding = this.Bind(inst, args, kw, info);
|
||||
Object result;
|
||||
|
||||
if (binding == null) {
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
// ==========================================================================
|
||||
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Python.Runtime {
|
||||
|
||||
|
@ -19,16 +20,38 @@ namespace Python.Runtime {
|
|||
|
||||
internal class MethodBinding : ExtensionType {
|
||||
|
||||
MethodInfo info;
|
||||
MethodObject m;
|
||||
IntPtr target;
|
||||
|
||||
public MethodBinding(MethodObject m, IntPtr target) : base() {
|
||||
Runtime.Incref(target);
|
||||
this.target = target;
|
||||
this.info = null;
|
||||
this.m = m;
|
||||
}
|
||||
|
||||
|
||||
//====================================================================
|
||||
// Implement explicit overload selection using subscript syntax ([]).
|
||||
//====================================================================
|
||||
|
||||
[CallConvCdecl()]
|
||||
public static IntPtr mp_subscript(IntPtr tp, IntPtr idx) {
|
||||
MethodBinding self = (MethodBinding)GetManagedObject(tp);
|
||||
MethodInfo sig = MethodBinder.MatchByTypeSig(self.m.info, idx);
|
||||
if (sig == null) {
|
||||
return Exceptions.RaiseTypeError(
|
||||
"No match found for signature"
|
||||
);
|
||||
}
|
||||
MethodBinding mb = new MethodBinding(self.m, self.target);
|
||||
mb.info = sig;
|
||||
Runtime.Incref(mb.pyHandle);
|
||||
return mb.pyHandle;
|
||||
}
|
||||
|
||||
|
||||
//====================================================================
|
||||
// MethodBinding __getattribute__ implementation.
|
||||
//====================================================================
|
||||
|
@ -77,12 +100,13 @@ namespace Python.Runtime {
|
|||
IntPtr uargs = Runtime.PyTuple_GetSlice(args, 1, len);
|
||||
IntPtr inst = Runtime.PyTuple_GetItem(args, 0);
|
||||
Runtime.Incref(inst);
|
||||
IntPtr r = self.m.Invoke(inst, uargs, kw);
|
||||
IntPtr r = self.m.Invoke(inst, uargs, kw, self.info);
|
||||
Runtime.Decref(inst);
|
||||
Runtime.Decref(uargs);
|
||||
return r;
|
||||
}
|
||||
|
||||
return self.m.Invoke(self.target, args, kw);
|
||||
return self.m.Invoke(self.target, args, kw, self.info);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -35,8 +35,13 @@ namespace Python.Runtime {
|
|||
}
|
||||
|
||||
|
||||
public virtual IntPtr Invoke(IntPtr target, IntPtr args, IntPtr kw) {
|
||||
return binder.Invoke(target, args, kw);
|
||||
public virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw) {
|
||||
return this.Invoke(inst, args, kw, null);
|
||||
}
|
||||
|
||||
public virtual IntPtr Invoke(IntPtr target, IntPtr args, IntPtr kw,
|
||||
MethodBase info) {
|
||||
return binder.Invoke(target, args, kw, info);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -127,6 +127,174 @@ namespace Python.Test {
|
|||
i = 42;
|
||||
}
|
||||
|
||||
// overload selection test support
|
||||
|
||||
public static bool TestOverloadSelection(bool v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static byte TestOverloadSelection(byte v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static sbyte TestOverloadSelection(sbyte v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static char TestOverloadSelection(char v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static short TestOverloadSelection(short v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static int TestOverloadSelection(int v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static long TestOverloadSelection(long v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static ushort TestOverloadSelection(ushort v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static uint TestOverloadSelection(uint v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static ulong TestOverloadSelection(ulong v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static float TestOverloadSelection(float v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static double TestOverloadSelection(double v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static decimal TestOverloadSelection(decimal v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static string TestOverloadSelection(string v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static ShortEnum TestOverloadSelection(ShortEnum v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static object TestOverloadSelection(object v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static InterfaceTest TestOverloadSelection(InterfaceTest v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static ISayHello1 TestOverloadSelection(ISayHello1 v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static bool[] TestOverloadSelection(bool[] v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static byte[] TestOverloadSelection(byte[] v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static sbyte[] TestOverloadSelection(sbyte[] v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static char[] TestOverloadSelection(char[] v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static short[] TestOverloadSelection(short[] v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static int[] TestOverloadSelection(int[] v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static long[] TestOverloadSelection(long[] v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static ushort[] TestOverloadSelection(ushort[] v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static uint[] TestOverloadSelection(uint[] v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static ulong[] TestOverloadSelection(ulong[] v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static float[] TestOverloadSelection(float[] v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static double[] TestOverloadSelection(double[] v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static decimal[] TestOverloadSelection(decimal[] v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static string[] TestOverloadSelection(string[] v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static ShortEnum[] TestOverloadSelection(ShortEnum[] v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static object[] TestOverloadSelection(object[] v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
public static InterfaceTest[] TestOverloadSelection(InterfaceTest[] v){
|
||||
return v;
|
||||
}
|
||||
|
||||
public static ISayHello1[] TestOverloadSelection(ISayHello1[] v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
// public static GenericWrapper<T> TestOverloadSelection(
|
||||
// GenericWrapper<T> v) {
|
||||
// return v;
|
||||
// }
|
||||
|
||||
// public static GenericWrapper<T>[] TestOverloadSelection(
|
||||
// GenericWrapper<T>[] v) {
|
||||
// return v;
|
||||
// }
|
||||
|
||||
public static int TestOverloadSelection(string s, int i, object[] o) {
|
||||
return o.Length;
|
||||
}
|
||||
|
||||
public static int TestOverloadSelection(string s, int i) {
|
||||
return i;
|
||||
}
|
||||
|
||||
public static int TestOverloadSelection(int i, string s) {
|
||||
return i;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ test_modules = (
|
|||
'test_exceptions',
|
||||
'test_module',
|
||||
'test_compat',
|
||||
#'test_generic',
|
||||
'test_conversion',
|
||||
'test_class',
|
||||
'test_interface',
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
import sys, os, string, unittest, types
|
||||
import Python.Test as Test
|
||||
import System
|
||||
|
||||
|
||||
class ArrayTests(unittest.TestCase):
|
||||
|
@ -1325,6 +1326,139 @@ class ArrayTests(unittest.TestCase):
|
|||
self.failUnless(items[i].Y == i + 1)
|
||||
|
||||
|
||||
def testSpecialArrayCreation(self):
|
||||
"""Test using the Array[<type>] syntax for creating arrays."""
|
||||
from Python.Test import ISayHello1, InterfaceTest, ShortEnum
|
||||
from System import Array
|
||||
inst = InterfaceTest()
|
||||
|
||||
value = Array[System.Boolean]([True, True])
|
||||
self.failUnless(value[0] == True)
|
||||
self.failUnless(value[1] == True)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[bool]([True, True])
|
||||
self.failUnless(value[0] == True)
|
||||
self.failUnless(value[1] == True)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[System.Byte]([0, 255])
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 255)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[System.SByte]([0, 127])
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 127)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[System.Char]([u'A', u'Z'])
|
||||
self.failUnless(value[0] == u'A')
|
||||
self.failUnless(value[1] == u'Z')
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[System.Char]([0, 65535])
|
||||
self.failUnless(value[0] == unichr(0))
|
||||
self.failUnless(value[1] == unichr(65535))
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[System.Int16]([0, 32767])
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 32767)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[System.Int32]([0, 2147483647])
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 2147483647)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[int]([0, 2147483647])
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 2147483647)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[System.Int64]([0, 9223372036854775807L])
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 9223372036854775807L)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[long]([0, 9223372036854775807L])
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 9223372036854775807L)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[System.UInt16]([0, 65000])
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 65000)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[System.UInt32]([0, 4294967295L])
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 4294967295L)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[System.UInt64]([0, 18446744073709551615L])
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 18446744073709551615L)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[System.Single]([0.0, 3.402823e38])
|
||||
self.failUnless(value[0] == 0.0)
|
||||
self.failUnless(value[1] == 3.402823e38)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[System.Double]([0.0, 1.7976931348623157e308])
|
||||
self.failUnless(value[0] == 0.0)
|
||||
self.failUnless(value[1] == 1.7976931348623157e308)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[float]([0.0, 1.7976931348623157e308])
|
||||
self.failUnless(value[0] == 0.0)
|
||||
self.failUnless(value[1] == 1.7976931348623157e308)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[System.Decimal]([System.Decimal.Zero,System.Decimal.One])
|
||||
self.failUnless(value[0] == System.Decimal.Zero)
|
||||
self.failUnless(value[1] == System.Decimal.One)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[System.String](["one", "two"])
|
||||
self.failUnless(value[0] == "one")
|
||||
self.failUnless(value[1] == "two")
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[str](["one", "two"])
|
||||
self.failUnless(value[0] == "one")
|
||||
self.failUnless(value[1] == "two")
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[ShortEnum]([ShortEnum.Zero, ShortEnum.One])
|
||||
self.failUnless(value[0] == ShortEnum.Zero)
|
||||
self.failUnless(value[1] == ShortEnum.One)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[System.Object]([inst, inst])
|
||||
self.failUnless(value[0].__class__ == inst.__class__)
|
||||
self.failUnless(value[1].__class__ == inst.__class__)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[InterfaceTest]([inst, inst])
|
||||
self.failUnless(value[0].__class__ == inst.__class__)
|
||||
self.failUnless(value[1].__class__ == inst.__class__)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
value = Array[ISayHello1]([inst, inst])
|
||||
self.failUnless(value[0].__class__ == inst.__class__)
|
||||
self.failUnless(value[1].__class__ == inst.__class__)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
inst = System.Exception("badness")
|
||||
value = Array[System.Exception]([inst, inst])
|
||||
self.failUnless(value[0].__class__ == inst.__class__)
|
||||
self.failUnless(value[1].__class__ == inst.__class__)
|
||||
self.failUnless(value.Length == 2)
|
||||
|
||||
|
||||
def testArrayAbuse(self):
|
||||
"""Test array abuse."""
|
||||
_class = Test.PublicArrayTest
|
||||
|
|
|
@ -388,6 +388,284 @@ class MethodTests(unittest.TestCase):
|
|||
self.failUnlessRaises(TypeError, test)
|
||||
|
||||
|
||||
def testExplicitOverloadSelection(self):
|
||||
"""Check explicit overload selection using [] syntax."""
|
||||
from Python.Test import ISayHello1, InterfaceTest, ShortEnum
|
||||
from System import Array
|
||||
inst = InterfaceTest()
|
||||
|
||||
value = MethodTest.TestOverloadSelection[System.Boolean](True)
|
||||
self.failUnless(value == True)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[bool](True)
|
||||
self.failUnless(value == True)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[System.Byte](255)
|
||||
self.failUnless(value == 255)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[System.SByte](127)
|
||||
self.failUnless(value == 127)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[System.Char](u'A')
|
||||
self.failUnless(value == u'A')
|
||||
|
||||
value = MethodTest.TestOverloadSelection[System.Char](65535)
|
||||
self.failUnless(value == unichr(65535))
|
||||
|
||||
value = MethodTest.TestOverloadSelection[System.Int16](32767)
|
||||
self.failUnless(value == 32767)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[System.Int32](2147483647)
|
||||
self.failUnless(value == 2147483647)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[int](2147483647)
|
||||
self.failUnless(value == 2147483647)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[System.Int64](
|
||||
9223372036854775807L
|
||||
)
|
||||
self.failUnless(value == 9223372036854775807L)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[long](
|
||||
9223372036854775807L
|
||||
)
|
||||
self.failUnless(value == 9223372036854775807L)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[System.UInt16](65000)
|
||||
self.failUnless(value == 65000)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[System.UInt32](4294967295L)
|
||||
self.failUnless(value == 4294967295L)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[System.UInt64](
|
||||
18446744073709551615L
|
||||
)
|
||||
self.failUnless(value == 18446744073709551615L)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[System.Single](3.402823e38)
|
||||
self.failUnless(value == 3.402823e38)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[System.Double](
|
||||
1.7976931348623157e308
|
||||
)
|
||||
self.failUnless(value == 1.7976931348623157e308)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[float](
|
||||
1.7976931348623157e308
|
||||
)
|
||||
self.failUnless(value == 1.7976931348623157e308)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[System.Decimal](
|
||||
System.Decimal.One
|
||||
)
|
||||
self.failUnless(value == System.Decimal.One)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[System.String]("spam")
|
||||
self.failUnless(value == "spam")
|
||||
|
||||
value = MethodTest.TestOverloadSelection[str]("spam")
|
||||
self.failUnless(value == "spam")
|
||||
|
||||
value = MethodTest.TestOverloadSelection[ShortEnum](ShortEnum.Zero)
|
||||
self.failUnless(value == ShortEnum.Zero)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[System.Object](inst)
|
||||
self.failUnless(value.__class__ == inst.__class__)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[InterfaceTest](inst)
|
||||
self.failUnless(value.__class__ == inst.__class__)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[ISayHello1](inst)
|
||||
self.failUnless(value.__class__ == inst.__class__)
|
||||
|
||||
atype = Array[System.Object]
|
||||
value = MethodTest.TestOverloadSelection[str, int, atype](
|
||||
"one", 1, atype([1, 2, 3])
|
||||
)
|
||||
self.failUnless(value == 3)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[str, int]("one", 1)
|
||||
self.failUnless(value == 1)
|
||||
|
||||
value = MethodTest.TestOverloadSelection[int, str](1, "one")
|
||||
self.failUnless(value == 1)
|
||||
|
||||
|
||||
def testOverloadSelectionWithArrayTypes(self):
|
||||
"""Check overload selection using array types."""
|
||||
from Python.Test import ISayHello1, InterfaceTest, ShortEnum
|
||||
from System import Array
|
||||
inst = InterfaceTest()
|
||||
|
||||
vtype = Array[System.Boolean]
|
||||
input = vtype([True, True])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == True)
|
||||
self.failUnless(value[1] == True)
|
||||
|
||||
vtype = Array[bool]
|
||||
input = vtype([True, True])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == True)
|
||||
self.failUnless(value[1] == True)
|
||||
|
||||
vtype = Array[System.Byte]
|
||||
input = vtype([0, 255])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 255)
|
||||
|
||||
vtype = Array[System.SByte]
|
||||
input = vtype([0, 127])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 127)
|
||||
|
||||
vtype = Array[System.Char]
|
||||
input = vtype([u'A', u'Z'])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == u'A')
|
||||
self.failUnless(value[1] == u'Z')
|
||||
|
||||
vtype = Array[System.Char]
|
||||
input = vtype([0, 65535])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == unichr(0))
|
||||
self.failUnless(value[1] == unichr(65535))
|
||||
|
||||
vtype = Array[System.Int16]
|
||||
input = vtype([0, 32767])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 32767)
|
||||
|
||||
vtype = Array[System.Int32]
|
||||
input = vtype([0, 2147483647])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 2147483647)
|
||||
|
||||
vtype = Array[int]
|
||||
input = vtype([0, 2147483647])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 2147483647)
|
||||
|
||||
vtype = Array[System.Int64]
|
||||
input = vtype([0, 9223372036854775807L])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 9223372036854775807L)
|
||||
|
||||
vtype = Array[long]
|
||||
input = vtype([0, 9223372036854775807L])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 9223372036854775807L)
|
||||
|
||||
vtype = Array[System.UInt16]
|
||||
input = vtype([0, 65000])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 65000)
|
||||
|
||||
vtype = Array[System.UInt32]
|
||||
input = vtype([0, 4294967295L])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 4294967295L)
|
||||
|
||||
vtype = Array[System.UInt64]
|
||||
input = vtype([0, 18446744073709551615L])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == 0)
|
||||
self.failUnless(value[1] == 18446744073709551615L)
|
||||
|
||||
vtype = Array[System.Single]
|
||||
input = vtype([0.0, 3.402823e38])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == 0.0)
|
||||
self.failUnless(value[1] == 3.402823e38)
|
||||
|
||||
vtype = Array[System.Double]
|
||||
input = vtype([0.0, 1.7976931348623157e308])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == 0.0)
|
||||
self.failUnless(value[1] == 1.7976931348623157e308)
|
||||
|
||||
vtype = Array[float]
|
||||
input = vtype([0.0, 1.7976931348623157e308])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == 0.0)
|
||||
self.failUnless(value[1] == 1.7976931348623157e308)
|
||||
|
||||
vtype = Array[System.Decimal]
|
||||
input = vtype([System.Decimal.Zero, System.Decimal.One])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == System.Decimal.Zero)
|
||||
self.failUnless(value[1] == System.Decimal.One)
|
||||
|
||||
vtype = Array[System.String]
|
||||
input = vtype(["one", "two"])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == "one")
|
||||
self.failUnless(value[1] == "two")
|
||||
|
||||
vtype = Array[str]
|
||||
input = vtype(["one", "two"])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == "one")
|
||||
self.failUnless(value[1] == "two")
|
||||
|
||||
vtype = Array[ShortEnum]
|
||||
input = vtype([ShortEnum.Zero, ShortEnum.One])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0] == ShortEnum.Zero)
|
||||
self.failUnless(value[1] == ShortEnum.One)
|
||||
|
||||
vtype = Array[System.Object]
|
||||
input = vtype([inst, inst])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0].__class__ == inst.__class__)
|
||||
self.failUnless(value[1].__class__ == inst.__class__)
|
||||
|
||||
vtype = Array[InterfaceTest]
|
||||
input = vtype([inst, inst])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0].__class__ == inst.__class__)
|
||||
self.failUnless(value[1].__class__ == inst.__class__)
|
||||
|
||||
vtype = Array[ISayHello1]
|
||||
input = vtype([inst, inst])
|
||||
value = MethodTest.TestOverloadSelection[vtype](input)
|
||||
self.failUnless(value[0].__class__ == inst.__class__)
|
||||
self.failUnless(value[1].__class__ == inst.__class__)
|
||||
|
||||
|
||||
def testExplicitOverloadSelectionFailure(self):
|
||||
"""Check that overload selection fails correctly."""
|
||||
|
||||
def test():
|
||||
value = MethodTest.TestOverloadSelection[System.Type](True)
|
||||
|
||||
self.failUnlessRaises(TypeError, test)
|
||||
|
||||
def test():
|
||||
value = MethodTest.TestOverloadSelection[int, int](1, 1)
|
||||
|
||||
self.failUnlessRaises(TypeError, test)
|
||||
|
||||
def test():
|
||||
value = MethodTest.TestOverloadSelection[str, int, int]("", 1, 1)
|
||||
|
||||
self.failUnlessRaises(TypeError, test)
|
||||
|
||||
def test():
|
||||
value = MethodTest.TestOverloadSelection[int, long](1)
|
||||
|
||||
self.failUnlessRaises(TypeError, test)
|
||||
|
||||
|
||||
|
||||
|
||||
def test_suite():
|
||||
|
|
Загрузка…
Ссылка в новой задаче