| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 87fc365 commit 01d6772
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -546,7 +546,7 @@ private static ClassInfo GetClassInfo(Type type, ClassBase impl) | |||
| 546 | 546 | ci.members[pyName] = new MethodObject(type, name, forwardMethods).AllocObject(); | |
| 547 | 547 | // Only methods where only the right operand is the declaring type. | |
| 548 | 548 | if (reverseMethods.Length > 0) | |
| 549 | - ci.members[pyNameReverse] = new MethodObject(type, name, reverseMethods).AllocObject(); | ||
| 549 | + ci.members[pyNameReverse] = new MethodObject(type, name, reverseMethods, reverse_args: true).AllocObject(); | ||
| 550 | 550 | } | |
| 551 | 551 | } | |
| 552 | 552 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,17 +28,22 @@ internal class MethodBinder | |||
| 28 | 28 | ||
| 29 | 29 | [NonSerialized] | |
| 30 | 30 | public bool init = false; | |
| 31 | + | ||
| 31 | 32 | public const bool DefaultAllowThreads = true; | |
| 32 | 33 | public bool allow_threads = DefaultAllowThreads; | |
| 33 | 34 | ||
| 34 | - internal MethodBinder() | ||
| 35 | + public bool args_reversed = false; | ||
| 36 | + | ||
| 37 | + internal MethodBinder(bool reverse_args = false) | ||
| 35 | 38 | { | |
| 36 | 39 | list = new List<MaybeMethodBase>(); | |
| 40 | + args_reversed = reverse_args; | ||
| 37 | 41 | } | |
| 38 | 42 | ||
| 39 | - internal MethodBinder(MethodInfo mi) | ||
| 43 | + internal MethodBinder(MethodInfo mi, bool reverse_args = false) | ||
| 40 | 44 | { | |
| 41 | 45 | list = new List<MaybeMethodBase> { new MaybeMethodBase(mi) }; | |
| 46 | + args_reversed = reverse_args; | ||
| 42 | 47 | } | |
| 43 | 48 | ||
| 44 | 49 | public int Count | |
@@ -271,10 +276,11 @@ internal static int ArgPrecedence(Type t) | |||
| 271 | 276 | /// <param name="inst">The Python target of the method invocation.</param> | |
| 272 | 277 | /// <param name="args">The Python arguments.</param> | |
| 273 | 278 | /// <param name="kw">The Python keyword arguments.</param> | |
| 279 | + /// <param name="reverse_args">Reverse arguments of methods. Used for methods such as __radd__, __rsub__, __rmod__ etc</param> | ||
| 274 | 280 | /// <returns>A Binding if successful. Otherwise null.</returns> | |
| 275 | - internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw) | ||
| 281 | + internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, bool reverse_args = false) | ||
| 276 | 282 | { | |
| 277 | - return Bind(inst, args, kw, null, null); | ||
| 283 | + return Bind(inst, args, kw, null, null, reverse_args); | ||
| 278 | 284 | } | |
| 279 | 285 | ||
| 280 | 286 | /// <summary> | |
@@ -287,10 +293,11 @@ internal static int ArgPrecedence(Type t) | |||
| 287 | 293 | /// <param name="args">The Python arguments.</param> | |
| 288 | 294 | /// <param name="kw">The Python keyword arguments.</param> | |
| 289 | 295 | /// <param name="info">If not null, only bind to that method.</param> | |
| 296 | + /// <param name="reverse_args">Reverse arguments of methods. Used for methods such as __radd__, __rsub__, __rmod__ etc</param> | ||
| 290 | 297 | /// <returns>A Binding if successful. Otherwise null.</returns> | |
| 291 | - internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info) | ||
| 298 | + internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, bool reverse_args = false) | ||
| 292 | 299 | { | |
| 293 | - return Bind(inst, args, kw, info, null); | ||
| 300 | + return Bind(inst, args, kw, info, null, reverse_args); | ||
| 294 | 301 | } | |
| 295 | 302 | ||
| 296 | 303 | private readonly struct MatchedMethod | |
@@ -334,8 +341,9 @@ public MismatchedMethod(Exception exception, MethodBase mb) | |||
| 334 | 341 | /// <param name="kw">The Python keyword arguments.</param> | |
| 335 | 342 | /// <param name="info">If not null, only bind to that method.</param> | |
| 336 | 343 | /// <param name="methodinfo">If not null, additionally attempt to bind to the generic methods in this array by inferring generic type parameters.</param> | |
| 344 | + /// <param name="reverse_args">Reverse arguments of methods. Used for methods such as __radd__, __rsub__, __rmod__ etc</param> | ||
| 337 | 345 | /// <returns>A Binding if successful. Otherwise null.</returns> | |
| 338 | - internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, MethodBase[]? methodinfo) | ||
| 346 | + internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, MethodBase[]? methodinfo, bool reverse_args = false) | ||
| 339 | 347 | { | |
| 340 | 348 | // loop to find match, return invoker w/ or w/o error | |
| 341 | 349 | var kwargDict = new Dictionary<string, PyObject>(); | |
@@ -363,10 +371,10 @@ public MismatchedMethod(Exception exception, MethodBase mb) | |||
| 363 | 371 | _methods = GetMethods(); | |
| 364 | 372 | } | |
| 365 | 373 | ||
| 366 | - return Bind(inst, args, kwargDict, _methods, matchGenerics: true); | ||
| 374 | + return Bind(inst, args, kwargDict, _methods, matchGenerics: true, reverse_args); | ||
| 367 | 375 | } | |
| 368 | 376 | ||
| 369 | - static Binding? Bind(BorrowedReference inst, BorrowedReference args, Dictionary<string, PyObject> kwargDict, MethodBase[] methods, bool matchGenerics) | ||
| 377 | + private static Binding? Bind(BorrowedReference inst, BorrowedReference args, Dictionary<string, PyObject> kwargDict, MethodBase[] methods, bool matchGenerics, bool reversed = false) | ||
| 370 | 378 | { | |
| 371 | 379 | var pynargs = (int)Runtime.PyTuple_Size(args); | |
| 372 | 380 | var isGeneric = false; | |
@@ -386,7 +394,7 @@ public MismatchedMethod(Exception exception, MethodBase mb) | |||
| 386 | 394 | // Binary operator methods will have 2 CLR args but only one Python arg | |
| 387 | 395 | // (unary operators will have 1 less each), since Python operator methods are bound. | |
| 388 | 396 | isOperator = isOperator && pynargs == pi.Length - 1; | |
| 389 | - bool isReverse = isOperator && OperatorMethod.IsReverse((MethodInfo)mi); // Only cast if isOperator. | ||
| 397 | + bool isReverse = isOperator && reversed; // Only cast if isOperator. | ||
| 390 | 398 | if (isReverse && OperatorMethod.IsComparisonOp((MethodInfo)mi)) | |
| 391 | 399 | continue; // Comparison operators in Python have no reverse mode. | |
| 392 | 400 | if (!MatchesArgumentCount(pynargs, pi, kwargDict, out bool paramsArray, out ArrayList? defaultArgList, out int kwargsMatched, out int defaultsNeeded) && !isOperator) | |
@@ -809,14 +817,14 @@ static bool MatchesArgumentCount(int positionalArgumentCount, ParameterInfo[] pa | |||
| 809 | 817 | return match; | |
| 810 | 818 | } | |
| 811 | 819 | ||
| 812 | - internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw) | ||
| 820 | + internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, bool reverse_args = false) | ||
| 813 | 821 | { | |
| 814 | - return Invoke(inst, args, kw, null, null); | ||
| 822 | + return Invoke(inst, args, kw, null, null, reverse_args); | ||
| 815 | 823 | } | |
| 816 | 824 | ||
| 817 | - internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info) | ||
| 825 | + internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, bool reverse_args = false) | ||
| 818 | 826 | { | |
| 819 | - return Invoke(inst, args, kw, info, null); | ||
| 827 | + return Invoke(inst, args, kw, info, null, reverse_args = false); | ||
| 820 | 828 | } | |
| 821 | 829 | ||
| 822 | 830 | protected static void AppendArgumentTypes(StringBuilder to, BorrowedReference args) | |
@@ -852,7 +860,7 @@ protected static void AppendArgumentTypes(StringBuilder to, BorrowedReference ar | |||
| 852 | 860 | to.Append(')'); | |
| 853 | 861 | } | |
| 854 | 862 | ||
| 855 | - internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, MethodBase[]? methodinfo) | ||
| 863 | + internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, MethodBase[]? methodinfo, bool reverse_args = false) | ||
| 856 | 864 | { | |
| 857 | 865 | // No valid methods, nothing to bind. | |
| 858 | 866 | if (GetMethods().Length == 0) | |
@@ -865,7 +873,7 @@ internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference a | |||
| 865 | 873 | return Exceptions.RaiseTypeError(msg.ToString()); | |
| 866 | 874 | } | |
| 867 | 875 | ||
| 868 | - Binding? binding = Bind(inst, args, kw, info, methodinfo); | ||
| 876 | + Binding? binding = Bind(inst, args, kw, info, methodinfo, reverse_args); | ||
| 869 | 877 | object result; | |
| 870 | 878 | IntPtr ts = IntPtr.Zero; | |
| 871 | 879 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -237,7 +237,8 @@ public static NewReference tp_call(BorrowedReference ob, BorrowedReference args, | |||
| 237 | 237 | } | |
| 238 | 238 | } | |
| 239 | 239 | } | |
| 240 | - return self.m.Invoke(target is null ? BorrowedReference.Null : target, args, kw, self.info.UnsafeValue); | ||
| 240 | + | ||
| 241 | + return self.m.Invoke(target is null ? BorrowedReference.Null : target, args, kw, self.info.UnsafeValue, self.m.binder.args_reversed); | ||
| 241 | 242 | } | |
| 242 | 243 | finally | |
| 243 | 244 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,20 +19,20 @@ internal class MethodObject : ExtensionType | |||
| 19 | 19 | { | |
| 20 | 20 | [NonSerialized] | |
| 21 | 21 | private MethodBase[]? _info = null; | |
| 22 | + | ||
| 22 | 23 | private readonly List<MaybeMethodInfo> infoList; | |
| 23 | 24 | internal string name; | |
| 24 | 25 | internal readonly MethodBinder binder; | |
| 25 | 26 | internal bool is_static = false; | |
| 26 | - | ||
| 27 | 27 | internal PyString? doc; | |
| 28 | 28 | internal MaybeType type; | |
| 29 | 29 | ||
| 30 | - public MethodObject(MaybeType type, string name, MethodBase[] info, bool allow_threads) | ||
| 30 | + public MethodObject(MaybeType type, string name, MethodBase[] info, bool allow_threads, bool reverse_args = false) | ||
| 31 | 31 | { | |
| 32 | 32 | this.type = type; | |
| 33 | 33 | this.name = name; | |
| 34 | 34 | this.infoList = new List<MaybeMethodInfo>(); | |
| 35 | - binder = new MethodBinder(); | ||
| 35 | + binder = new MethodBinder(reverse_args); | ||
| 36 | 36 | foreach (MethodBase item in info) | |
| 37 | 37 | { | |
| 38 | 38 | this.infoList.Add(item); | |
@@ -45,8 +45,8 @@ public MethodObject(MaybeType type, string name, MethodBase[] info, bool allow_t | |||
| 45 | 45 | binder.allow_threads = allow_threads; | |
| 46 | 46 | } | |
| 47 | 47 | ||
| 48 | - public MethodObject(MaybeType type, string name, MethodBase[] info) | ||
| 49 | - : this(type, name, info, allow_threads: AllowThreads(info)) | ||
| 48 | + public MethodObject(MaybeType type, string name, MethodBase[] info, bool reverse_args = false) | ||
| 49 | + : this(type, name, info, allow_threads: AllowThreads(info), reverse_args) | ||
| 50 | 50 | { | |
| 51 | 51 | } | |
| 52 | 52 | ||
@@ -67,14 +67,14 @@ internal MethodBase[] info | |||
| 67 | 67 | } | |
| 68 | 68 | } | |
| 69 | 69 | ||
| 70 | - public virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw) | ||
| 70 | + public virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, bool reverse_args = false) | ||
| 71 | 71 | { | |
| 72 | - return Invoke(inst, args, kw, null); | ||
| 72 | + return Invoke(inst, args, kw, null, reverse_args); | ||
| 73 | 73 | } | |
| 74 | 74 | ||
| 75 | - public virtual NewReference Invoke(BorrowedReference target, BorrowedReference args, BorrowedReference kw, MethodBase? info) | ||
| 75 | + public virtual NewReference Invoke(BorrowedReference target, BorrowedReference args, BorrowedReference kw, MethodBase? info, bool reverse_args = false) | ||
| 76 | 76 | { | |
| 77 | - return binder.Invoke(target, args, kw, info, this.info); | ||
| 77 | + return binder.Invoke(target, args, kw, info, this.info, reverse_args); | ||
| 78 | 78 | } | |
| 79 | 79 | ||
| 80 | 80 | /// <summary> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -177,17 +177,14 @@ public static string ReversePyMethodName(string pyName) | |||
| 177 | 177 | } | |
| 178 | 178 | ||
| 179 | 179 | /// <summary> | |
| 180 | - /// Check if the method is performing a reverse operation. | ||
| 180 | + /// Check if the method should have a reversed operation. | ||
| 181 | 181 | /// </summary> | |
| 182 | 182 | /// <param name="method">The operator method.</param> | |
| 183 | 183 | /// <returns></returns> | |
| 184 | - public static bool IsReverse(MethodBase method) | ||
| 184 | + public static bool HaveReverse(MethodBase method) | ||
| 185 | 185 | { | |
| 186 | - Type primaryType = method.IsOpsHelper() | ||
| 187 | - ? method.DeclaringType.GetGenericArguments()[0] | ||
| 188 | - : method.DeclaringType; | ||
| 189 | - Type leftOperandType = method.GetParameters()[0].ParameterType; | ||
| 190 | - return leftOperandType != primaryType; | ||
| 186 | + var pi = method.GetParameters(); | ||
| 187 | + return OpMethodMap.ContainsKey(method.Name) && pi.Length == 2; | ||
| 191 | 188 | } | |
| 192 | 189 | ||
| 193 | 190 | public static void FilterMethods(MethodBase[] methods, out MethodBase[] forwardMethods, out MethodBase[] reverseMethods) | |
@@ -196,14 +193,11 @@ public static void FilterMethods(MethodBase[] methods, out MethodBase[] forwardM | |||
| 196 | 193 | var reverseMethodsList = new List<MethodBase>(); | |
| 197 | 194 | foreach (var method in methods) | |
| 198 | 195 | { | |
| 199 | - if (IsReverse(method)) | ||
| 196 | + forwardMethodsList.Add(method); | ||
| 197 | + if (HaveReverse(method)) | ||
| 200 | 198 | { | |
| 201 | 199 | reverseMethodsList.Add(method); | |
| 202 | - } else | ||
| 203 | - { | ||
| 204 | - forwardMethodsList.Add(method); | ||
| 205 | 200 | } | |
| 206 | - | ||
| 207 | 201 | } | |
| 208 | 202 | forwardMethods = forwardMethodsList.ToArray(); | |
| 209 | 203 | reverseMethods = reverseMethodsList.ToArray(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments