| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -223,7 +223,6 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v) { | |||
| 223 | 223 | // Arg may be a tuple in the case of an indexer with multiple | |
| 224 | 224 | // parameters. If so, use it directly, else make a new tuple | |
| 225 | 225 | // with the index arg (method binders expect arg tuples). | |
| 226 | - | ||
| 227 | 226 | IntPtr args = idx; | |
| 228 | 227 | bool free = false; | |
| 229 | 228 | ||
@@ -234,13 +233,38 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v) { | |||
| 234 | 233 | free = true; | |
| 235 | 234 | } | |
| 236 | 235 | ||
| 236 | + // Add args given from caller | ||
| 237 | 237 | int i = Runtime.PyTuple_Size(args); | |
| 238 | 238 | IntPtr real = Runtime.PyTuple_New(i + 1); | |
| 239 | 239 | for (int n = 0; n < i; n++) { | |
| 240 | 240 | IntPtr item = Runtime.PyTuple_GetItem(args, n); | |
| 241 | 241 | Runtime.Incref(item); | |
| 242 | 242 | Runtime.PyTuple_SetItem(real, n, item); | |
| 243 | 243 | } | |
| 244 | + | ||
| 245 | + // Do we need default arguments added to the list | ||
| 246 | + if (cls.indexer.NeedsDefaultArgs(ob, real)) { | ||
| 247 | + IntPtr defaultArgs = cls.indexer.GetDefaultArgs(ob, real); | ||
| 248 | + int sizeOfDefaultArgs = Runtime.PyTuple_Size(defaultArgs); | ||
| 249 | + int temp = i + sizeOfDefaultArgs; | ||
| 250 | + real = Runtime.PyTuple_New(temp + 1); | ||
| 251 | + for (int n = 0; n < i; n++) { | ||
| 252 | + IntPtr item = Runtime.PyTuple_GetItem(args, n); | ||
| 253 | + Runtime.Incref(item); | ||
| 254 | + Runtime.PyTuple_SetItem(real, n, item); | ||
| 255 | + } | ||
| 256 | + | ||
| 257 | + for (int n = 0; n < sizeOfDefaultArgs; n++) { | ||
| 258 | + | ||
| 259 | + IntPtr item = Runtime.PyTuple_GetItem(defaultArgs, n); | ||
| 260 | + Runtime.Incref(item); | ||
| 261 | + Runtime.PyTuple_SetItem(real, n + i, item); | ||
| 262 | + } | ||
| 263 | + | ||
| 264 | + i = temp; | ||
| 265 | + } | ||
| 266 | + | ||
| 267 | + // Add value to argument list | ||
| 244 | 268 | Runtime.Incref(v); | |
| 245 | 269 | Runtime.PyTuple_SetItem(real, i, v); | |
| 246 | 270 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,7 +62,61 @@ internal void SetItem(IntPtr inst, IntPtr args) { | |||
| 62 | 62 | SetterBinder.Invoke(inst, args, IntPtr.Zero); | |
| 63 | 63 | } | |
| 64 | 64 | ||
| 65 | - } | ||
| 65 | + internal bool NeedsDefaultArgs(IntPtr inst, IntPtr args){ | ||
| 66 | + int pynargs = Runtime.PyTuple_Size(args) - 1; | ||
| 67 | + var methods = SetterBinder.GetMethods(); | ||
| 68 | + if(methods.Length == 0) | ||
| 69 | + return false; | ||
| 70 | + | ||
| 71 | + MethodBase mi = methods[0]; | ||
| 72 | + ParameterInfo[] pi = mi.GetParameters(); | ||
| 73 | + // need to subtract one for the value | ||
| 74 | + int clrnargs = pi.Length - 1; | ||
| 75 | + if(pynargs == clrnargs) | ||
| 76 | + return false; | ||
| 77 | + | ||
| 78 | + for (int v = pynargs; v < clrnargs; v++){ | ||
| 79 | + if (pi[v].DefaultValue == DBNull.Value) | ||
| 80 | + return false; | ||
| 81 | + } | ||
| 82 | + return true; | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + internal IntPtr GetDefaultArgs(IntPtr inst, IntPtr args){ | ||
| 86 | + //IntPtr real = Runtime.PyTuple_New(i + 1); | ||
| 87 | + int pynargs = Runtime.PyTuple_Size(args) - 1; | ||
| 88 | + var methods = SetterBinder.GetMethods(); | ||
| 89 | + IntPtr defaultArgs; | ||
| 90 | + if(methods.Length == 0){ | ||
| 91 | + defaultArgs = Runtime.PyTuple_New(0); | ||
| 92 | + return defaultArgs; | ||
| 93 | + } | ||
| 94 | + MethodBase mi = methods[0]; | ||
| 95 | + ParameterInfo[] pi = mi.GetParameters(); | ||
| 96 | + int clrnargs = pi.Length - 1; | ||
| 97 | + if(pynargs == clrnargs|| pynargs > clrnargs){ | ||
| 98 | + defaultArgs = Runtime.PyTuple_New(0); | ||
| 99 | + return defaultArgs; | ||
| 100 | + } | ||
| 66 | 101 | ||
| 102 | + defaultArgs = Runtime.PyTuple_New(clrnargs - pynargs); | ||
| 103 | + for (int i = 0; i < (clrnargs - pynargs); i++) { | ||
| 104 | + // Here we own the reference to the Python value, and we | ||
| 105 | + // give the ownership to the arg tuple. | ||
| 106 | + if (pi[i + pynargs].DefaultValue == DBNull.Value) | ||
| 107 | + continue; | ||
| 108 | + else{ | ||
| 109 | + IntPtr arg = Converter.ToPython(pi[i + pynargs].DefaultValue, pi[i + pynargs].ParameterType); | ||
| 110 | + Type type = typeof(String); | ||
| 111 | + Object arg2; | ||
| 112 | + Converter.ToManaged(arg, type, out arg2, false); | ||
| 113 | + Runtime.PyTuple_SetItem(defaultArgs, i, arg); | ||
| 114 | + } | ||
| 115 | + } | ||
| 116 | + return defaultArgs; | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + | ||
| 120 | + } | ||
| 67 | 121 | ||
| 68 | 122 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -226,7 +226,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, | |||
| 226 | 226 | int pynargs = Runtime.PyTuple_Size(args); | |
| 227 | 227 | object arg; | |
| 228 | 228 | bool isGeneric = false; | |
| 229 | - | ||
| 229 | + ArrayList defaultArgList = null; | ||
| 230 | 230 | if (info != null) { | |
| 231 | 231 | _methods = (MethodBase[])Array.CreateInstance( | |
| 232 | 232 | typeof(MethodBase), 1 | |
@@ -247,7 +247,17 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, | |||
| 247 | 247 | int outs = 0; | |
| 248 | 248 | ||
| 249 | 249 | if (pynargs == clrnargs) { | |
| 250 | - match = true; | ||
| 250 | + match = true; | ||
| 251 | + } else if(pynargs < clrnargs){ | ||
| 252 | + match = true; | ||
| 253 | + defaultArgList = new ArrayList(); | ||
| 254 | + for (int v = pynargs; v < clrnargs; v++) | ||
| 255 | + { | ||
| 256 | + if (pi[v].DefaultValue == DBNull.Value) | ||
| 257 | + match = false; | ||
| 258 | + else | ||
| 259 | + defaultArgList.Add((object)pi[v].DefaultValue); | ||
| 260 | + } | ||
| 251 | 261 | } else if ((pynargs > clrnargs) && (clrnargs > 0) && | |
| 252 | 262 | (pi[clrnargs-1].ParameterType.IsArray)) { | |
| 253 | 263 | // The last argument of the mananged functions seems to | |
@@ -262,30 +272,43 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, | |||
| 262 | 272 | ||
| 263 | 273 | for (int n = 0; n < clrnargs; n++) { | |
| 264 | 274 | IntPtr op; | |
| 265 | - if (arrayStart == n) { | ||
| 266 | - // map remaining Python arguments to a tuple since | ||
| 267 | - // the managed function accepts it - hopefully :] | ||
| 268 | - op = Runtime.PyTuple_GetSlice(args, arrayStart, pynargs); | ||
| 269 | - } | ||
| 270 | - else { | ||
| 271 | - op = Runtime.PyTuple_GetItem(args, n); | ||
| 272 | - } | ||
| 273 | - Type type = pi[n].ParameterType; | ||
| 274 | - if (pi[n].IsOut || type.IsByRef) { | ||
| 275 | - outs++; | ||
| 276 | - } | ||
| 277 | - | ||
| 278 | - if (!Converter.ToManaged(op, type, out arg, false)) { | ||
| 279 | - Exceptions.Clear(); | ||
| 280 | - margs = null; | ||
| 281 | - break; | ||
| 275 | + if (n < pynargs) | ||
| 276 | + { | ||
| 277 | + if (arrayStart == n) | ||
| 278 | + { | ||
| 279 | + // map remaining Python arguments to a tuple since | ||
| 280 | + // the managed function accepts it - hopefully :] | ||
| 281 | + op = Runtime.PyTuple_GetSlice(args, arrayStart, pynargs); | ||
| 282 | + } | ||
| 283 | + else | ||
| 284 | + { | ||
| 285 | + op = Runtime.PyTuple_GetItem(args, n); | ||
| 286 | + } | ||
| 287 | + Type type = pi[n].ParameterType; | ||
| 288 | + if (pi[n].IsOut || type.IsByRef) | ||
| 289 | + { | ||
| 290 | + outs++; | ||
| 291 | + } | ||
| 292 | + | ||
| 293 | + if (!Converter.ToManaged(op, type, out arg, false)) | ||
| 294 | + { | ||
| 295 | + Exceptions.Clear(); | ||
| 296 | + margs = null; | ||
| 297 | + break; | ||
| 298 | + } | ||
| 299 | + if (arrayStart == n) | ||
| 300 | + { | ||
| 301 | + // GetSlice() creates a new reference but GetItem() | ||
| 302 | + // returns only a borrow reference. | ||
| 303 | + Runtime.Decref(op); | ||
| 304 | + } | ||
| 305 | + margs[n] = arg; | ||
| 282 | 306 | } | |
| 283 | - if (arrayStart == n) { | ||
| 284 | - // GetSlice() creates a new reference but GetItem() | ||
| 285 | - // returns only a borrow reference. | ||
| 286 | - Runtime.Decref(op); | ||
| 307 | + else | ||
| 308 | + { | ||
| 309 | + if (defaultArgList != null) | ||
| 310 | + margs[n] = defaultArgList[n - pynargs]; | ||
| 287 | 311 | } | |
| 288 | - margs[n] = arg; | ||
| 289 | 312 | } | |
| 290 | 313 | ||
| 291 | 314 | if (margs == null) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -347,6 +347,23 @@ public MultiTypeIndexerTest() : base() {} | |||
| 347 | 347 | ||
| 348 | 348 | } | |
| 349 | 349 | ||
| 350 | + public class MultiDefaultKeyIndexerTest : IndexerBase { | ||
| 351 | + | ||
| 352 | + public MultiDefaultKeyIndexerTest() : base() { } | ||
| 353 | + | ||
| 354 | + public string this[int i1, int i2 = 2] { | ||
| 355 | + get { | ||
| 356 | + string key = i1.ToString() + i2.ToString(); | ||
| 357 | + return (string)t[key]; | ||
| 358 | + } | ||
| 359 | + set { | ||
| 360 | + string key = i1.ToString() + i2.ToString(); | ||
| 361 | + t[key] = value; | ||
| 362 | + } | ||
| 363 | + } | ||
| 364 | + | ||
| 365 | + } | ||
| 366 | + | ||
| 350 | 367 | ||
| 351 | 368 | ||
| 352 | 369 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -155,6 +155,16 @@ public static void TestVoidSingleRefParam (ref int i) { | |||
| 155 | 155 | i = 42; | |
| 156 | 156 | } | |
| 157 | 157 | ||
| 158 | + public static int TestSingleDefaultParam(int i = 5) { | ||
| 159 | + return i; | ||
| 160 | + } | ||
| 161 | + public static int TestTwoDefaultParam(int i = 5, int j = 6) { | ||
| 162 | + return i + j; | ||
| 163 | + } | ||
| 164 | + public static int TestOneArgAndTwoDefaultParam(int z, int i = 5, int j = 6) { | ||
| 165 | + return i + j + z; | ||
| 166 | + } | ||
| 167 | + | ||
| 158 | 168 | ||
| 159 | 169 | ||
| 160 | 170 | // overload selection test support | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,8 @@ | |||
| 8 | 8 | # =========================================================================== | |
| 9 | 9 | ||
| 10 | 10 | import sys, os, string, unittest, types | |
| 11 | + import clr | ||
| 12 | + clr.AddReference("Python.Test") | ||
| 11 | 13 | import Python.Test as Test | |
| 12 | 14 | import six | |
| 13 | 15 | ||
@@ -630,6 +632,17 @@ def test(): | |||
| 630 | 632 | object[0, 1, spam] = "wrong" | |
| 631 | 633 | ||
| 632 | 634 | self.assertRaises(TypeError, test) | |
| 635 | + | ||
| 636 | + | ||
| 637 | + def testMultiDefaultKeyIndexer(self): | ||
| 638 | + """Test indexers that take multiple indices with a default key arguments.""" | ||
| 639 | + #default argument is 2 in the MultiDefaultKeyIndexerTest object | ||
| 640 | + object = Test.MultiDefaultKeyIndexerTest() | ||
| 641 | + object[0, 2] = "zero one spam" | ||
| 642 | + self.assertTrue(object[0] == "zero one spam") | ||
| 643 | + | ||
| 644 | + object[1] = "one nine spam" | ||
| 645 | + self.assertTrue(object[1, 2] == "one nine spam") | ||
| 633 | 646 | ||
| 634 | 647 | ||
| 635 | 648 | def testIndexerWrongKeyType(self): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments