FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

debug commit - I know, I'm breaking the rules, but its only me for no… · pythonnet/pythonnet@8363502 · GitHub

Commit 8363502

Browse files
committed
debug commit - I know, I'm breaking the rules, but its only me for now ;)
1 parent e073a2e commit 8363502

11 files changed

Lines changed: 942 additions & 18 deletions

File tree

‎pythonnet/src/runtime/arrayobject.cs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,18 @@ internal override bool CanSubclass() {
2727
return false;
2828
}
2929

30-
public static IntPtr tp_new(IntPtr ob, IntPtr args, IntPtr kw) {
30+
public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw) {
31+
ArrayObject self = GetManagedObject(tp) as ArrayObject;
32+
if (Runtime.PyTuple_Size(args) != 1) {
33+
return Exceptions.RaiseTypeError("array expects 1 argument");
34+
}
35+
IntPtr op = Runtime.PyTuple_GetItem(args, 0);
36+
Object result;
37+
38+
if (!Converter.ToManaged(op, self.type, out result, true)) {
39+
return IntPtr.Zero;
40+
}
41+
return CLRObject.GetInstHandle(result, tp);
3142
string message = "cannot instantiate array wrapper";
3243
return Exceptions.RaiseTypeError(message);
3344
}

‎pythonnet/src/runtime/classbase.cs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ public static int tp_init(IntPtr ob, IntPtr args, IntPtr kw) {
5050
return 0;
5151
}
5252

53+
54+
//====================================================================
55+
// Default implementation of [] semantics for reflected types.
56+
//====================================================================
57+
58+
public virtual IntPtr type_subscript(IntPtr ob, IntPtr idx) {
59+
return Exceptions.RaiseTypeError("unsubscriptable object");
60+
}
61+
62+
5363
//====================================================================
5464
// Standard comparison implementation for instances of reflected types.
5565
//====================================================================

‎pythonnet/src/runtime/classobject.cs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,32 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw) {
124124
}
125125

126126

127+
//====================================================================
128+
// Implementation of [] semantics for reflected types. This mainly
129+
// exists to implement the Array[int] syntax for creating arrays.
130+
//====================================================================
131+
132+
public override IntPtr type_subscript(IntPtr ob, IntPtr idx) {
133+
if ((this.type) != typeof(Array)) {
134+
return Exceptions.RaiseTypeError("unsubscriptable object");
135+
}
136+
137+
if (Runtime.PyTuple_Check(idx)) {
138+
return Exceptions.RaiseTypeError("expected single type");
139+
}
140+
141+
ClassBase c = GetManagedObject(idx) as ClassBase;
142+
Type t = (c != null) ? c.type : Converter.GetTypeByAlias(idx);
143+
if (t == null) {
144+
return Exceptions.RaiseTypeError("type expected");
145+
}
146+
Array a = Array.CreateInstance(t, 0);
147+
c = ClassManager.GetClass(a.GetType());
148+
Runtime.Incref(c.pyHandle);
149+
return c.pyHandle;
150+
}
151+
152+
127153
//====================================================================
128154
// Implements __getitem__ for reflected classes and value types.
129155
//====================================================================

‎pythonnet/src/runtime/clrmodule.il‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
// ==========================================================================
99

1010
//============================================================================
11-
// This file is a hand-maintained stub - it implements CLR.dll, which can be
11+
// This file is a hand-maintained stub - it implements clr.dll, which can be
1212
// loaded by a standard CPython interpreter as an extension module. When it
1313
// is loaded, it bootstraps the managed runtime integration layer and defers
14-
// to it do initialization and put the CLR module into sys.modules, etc.
14+
// to it do initialization and put the clr module into sys.modules, etc.
1515
//============================================================================
1616

1717
.assembly extern mscorlib
@@ -23,7 +23,7 @@
2323
{
2424
.ver 1:0:0:0
2525
}
26-
.assembly CLR
26+
.assembly clr
2727
{
2828
// --- The following custom attribute is added automatically, do not uncomment -------
2929
// .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(bool,
@@ -32,7 +32,7 @@
3232
.ver 0:0:0:0
3333
}
3434

35-
.module CLR.dll
35+
.module clr.dll
3636
// MVID: {01BEB897-1638-4D9D-B01C-3638714A76B4}
3737
.imagebase 0x00400000
3838
.subsystem 0x00000003
@@ -43,30 +43,30 @@
4343
.data VT_01 = int32(0)
4444

4545

46-
.class public auto ansi beforefieldinit CLRModule
46+
.class public auto ansi beforefieldinit clrModule
4747
extends [mscorlib]System.Object
4848
{
49-
} // end of class CLRModule
49+
} // end of class clrModule
5050

5151

5252

53-
.class public auto ansi beforefieldinit CLRModule
53+
.class public auto ansi beforefieldinit clrModule
5454
extends [mscorlib]System.Object
5555
{
5656
.method public hidebysig static void
5757
modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl)
58-
initCLR() cil managed
58+
initclr() cil managed
5959
{
6060
.vtentry 1:1
61-
.export [1] as initCLR
61+
.export [1] as initclr
6262

6363
// Code size 8 (0x8)
6464
.maxstack 0
6565
IL_0000: call void [Python.Runtime]Python.Runtime.PythonEngine::InitExt()
6666
IL_0005: br.s IL_0007
6767

6868
IL_0007: ret
69-
} // end of method CLRModule::initCLR
69+
} // end of method clrModule::initclr
7070

7171
.method public hidebysig specialname rtspecialname
7272
instance void .ctor() cil managed
@@ -76,8 +76,8 @@
7676
IL_0000: ldarg.0
7777
IL_0001: call instance void [mscorlib]System.Object::.ctor()
7878
IL_0006: ret
79-
} // end of method CLRModule::.ctor
79+
} // end of method clrModule::.ctor
8080

81-
} // end of class CLRModule
81+
} // end of class clrModule
8282

8383

‎pythonnet/src/runtime/metatype.cs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,11 @@ public static int tp_setattro(IntPtr tp, IntPtr name, IntPtr value) {
221221
// own mp_subscript
222222
//====================================================================
223223
public static IntPtr mp_subscript(IntPtr tp, IntPtr idx) {
224-
GenericType gt = GetManagedObject(tp) as GenericType;
225-
if (gt != null) {
226-
return GenericType.bind(tp, idx);
227-
}
228-
return Exceptions.RaiseTypeError("unsubscriptable object");
224+
ClassBase cb = GetManagedObject(tp) as ClassBase;
225+
if (cb != null) {
226+
return cb.type_subscript(tp, idx);
227+
}
228+
return Exceptions.RaiseTypeError("unsubscriptable object");
229229
}
230230

231231
//====================================================================

‎pythonnet/src/runtime/methodbinder.cs‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,55 @@ internal static int ArgPrecedence(Type t) {
114114
}
115115

116116

117+
internal static MethodInfo MatchByTypeSig(MethodInfo[] msig,
118+
IntPtr psig) {
119+
IntPtr args = psig;
120+
bool free = false;
121+
122+
if (!Runtime.PyTuple_Check(psig)) {
123+
args = Runtime.PyTuple_New(1);
124+
Runtime.Incref(psig);
125+
Runtime.PyTuple_SetItem(args, 0, psig);
126+
free = true;
127+
}
128+
129+
int plen = Runtime.PyTuple_Size(args);
130+
MethodInfo match = null;
131+
132+
// XXX: what about out args, etc.?
133+
134+
for (int i = 0; i < msig.Length; i++) {
135+
ParameterInfo[] pi = msig[i].GetParameters();
136+
if (pi.Length != plen) {
137+
continue;
138+
}
139+
for (int n = 0; n < pi.Length; n++) {
140+
IntPtr p = Runtime.PyTuple_GetItem(args, n);
141+
if (p == IntPtr.Zero) {
142+
Exceptions.Clear();
143+
break;
144+
}
145+
ClassBase c = ManagedType.GetManagedObject(p) as ClassBase;
146+
Type t = (c != null) ? c.type :
147+
Converter.GetTypeByAlias(p);
148+
if (t == null) {
149+
break;
150+
}
151+
if (t != pi[n].ParameterType) {
152+
break;
153+
}
154+
}
155+
match = msig[i];
156+
break;
157+
}
158+
159+
if (free) {
160+
Runtime.Decref(args);
161+
}
162+
163+
return match;
164+
}
165+
117166

118167
//====================================================================
119168
// Bind the given Python instance and arguments to a particular method

‎pythonnet/src/runtime/methodbinding.cs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// ==========================================================================
99

1010
using System;
11+
using System.Reflection;
1112

1213
namespace Python.Runtime {
1314

@@ -19,16 +20,37 @@ namespace Python.Runtime {
1920

2021
internal class MethodBinding : ExtensionType {
2122

23+
MethodInfo info;
2224
MethodObject m;
2325
IntPtr target;
2426

2527
public MethodBinding(MethodObject m, IntPtr target) : base() {
2628
Runtime.Incref(target);
2729
this.target = target;
30+
this.info = null;
2831
this.m = m;
2932
}
3033

3134

35+
//====================================================================
36+
// Implement explicit overload selection using subscript syntax ([]).
37+
//====================================================================
38+
39+
public static IntPtr mp_subscript(IntPtr tp, IntPtr idx) {
40+
MethodBinding self = (MethodBinding)GetManagedObject(tp);
41+
MethodInfo sig = MethodBinder.MatchByTypeSig(self.m.info, idx);
42+
if (sig == null) {
43+
return Exceptions.RaiseTypeError(
44+
"No match found for signature"
45+
);
46+
}
47+
MethodBinding mb = new MethodBinding(self.m, self.target);
48+
mb.info = sig;
49+
Runtime.Incref(mb.pyHandle);
50+
return mb.pyHandle;
51+
}
52+
53+
3254
//====================================================================
3355
// MethodBinding __getattribute__ implementation.
3456
//====================================================================

‎pythonnet/src/testing/generictest.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ namespace Python.Test {
1717
// Supports CLR generics unit tests.
1818
//========================================================================
1919

20+
public class GenericWrapper<T> {
21+
public T value;
22+
23+
public GenericWrapper(T value) {
24+
this.value = value;
25+
}
26+
}
27+
2028
public class GenericTypeDefinition<T, U> {
2129
public T value1;
2230
public U value2;

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL