Pewarta Proxy

📋 Server Request & Proxy Logs

Loading active proxy settings...
Developed by Khairul Adha (adhexa.id)
Mengambil log server...

⚙️ Pengaturan Proxy Target

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Normalized names. Added HashCode and Equals to testing objects
  • Loading branch information
gertdreyer committed Feb 26, 2024
commit 5277ed97f83f8d5907ecb2376d36a7e7205fea85
19 changes: 12 additions & 7 deletions src/embed_tests/TestOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ public OwnInt(int value)
_value = value;
}

public override int GetHashCode()
{
return unchecked(65535 + _value.GetHashCode());
}

public override bool Equals(object obj)
{
return obj is OwnInt @object &&
Num == @object.Num;
}

public static OwnInt operator -(OwnInt p1, OwnInt p2)
{
return new OwnInt(p1._value - p2._value);
Expand Down Expand Up @@ -125,14 +136,8 @@ public bool CanDecode(PyType objectType, Type targetType)
return objectType.Name == "int" && targetType == typeof(OwnInt);
}

public bool TryDecode<T>(PyObject pyObj, out T? value)
public bool TryDecode<T>(PyObject pyObj, out T value)
{
if (pyObj.PyType.Name != "int" || typeof(T) != typeof(OwnInt))
{
value = default(T);
return false;
}

value = (T)(object)new OwnInt(pyObj.As<int>());
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/ClassManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ private static ClassInfo GetClassInfo(Type type, ClassBase impl)
ci.members[pyName] = new MethodObject(type, name, forwardMethods).AllocObject();
// Only methods where only the right operand is the declaring type.
if (reverseMethods.Length > 0)
ci.members[pyNameReverse] = new MethodObject(type, name, reverseMethods, reverse_args: true).AllocObject();
ci.members[pyNameReverse] = new MethodObject(type, name, reverseMethods, argsReversed: true).AllocObject();
}
}

Expand Down
50 changes: 26 additions & 24 deletions src/runtime/MethodBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ internal class MethodBinder
public const bool DefaultAllowThreads = true;
public bool allow_threads = DefaultAllowThreads;

public bool args_reversed = false;
public bool argsReversed = false;

internal MethodBinder(bool reverse_args = false)
internal MethodBinder(bool argsReversed = false)
{
list = new List<MaybeMethodBase>();
args_reversed = reverse_args;
this.argsReversed = argsReversed;
}

internal MethodBinder(MethodInfo mi, bool reverse_args = false)
internal MethodBinder(MethodInfo mi, bool argsReversed = false)
{
list = new List<MaybeMethodBase> { new MaybeMethodBase(mi) };
args_reversed = reverse_args;
this.argsReversed = argsReversed;
}

public int Count
Expand Down Expand Up @@ -276,11 +276,11 @@ internal static int ArgPrecedence(Type t)
/// <param name="inst">The Python target of the method invocation.</param>
/// <param name="args">The Python arguments.</param>
/// <param name="kw">The Python keyword arguments.</param>
/// <param name="reverse_args">Reverse arguments of methods. Used for methods such as __radd__, __rsub__, __rmod__ etc</param>
/// <param name="argsReversed">Reverse arguments of methods. Used for methods such as __radd__, __rsub__, __rmod__ etc</param>
/// <returns>A Binding if successful. Otherwise null.</returns>
internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, bool reverse_args = false)
internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, bool argsReversed = false)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to pass this parameter at all? Don't you have the field?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaned it up a bit. I missed that... The one method is static though so we will need to still pass it there...

{
return Bind(inst, args, kw, null, null, reverse_args);
return Bind(inst, args, kw, null, null, argsReversed);
}

/// <summary>
Expand All @@ -293,11 +293,11 @@ internal static int ArgPrecedence(Type t)
/// <param name="args">The Python arguments.</param>
/// <param name="kw">The Python keyword arguments.</param>
/// <param name="info">If not null, only bind to that method.</param>
/// <param name="reverse_args">Reverse arguments of methods. Used for methods such as __radd__, __rsub__, __rmod__ etc</param>
/// <param name="argsReversed">Reverse arguments of methods. Used for methods such as __radd__, __rsub__, __rmod__ etc</param>
/// <returns>A Binding if successful. Otherwise null.</returns>
internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, bool reverse_args = false)
internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, bool argsReversed = false)
{
return Bind(inst, args, kw, info, null, reverse_args);
return Bind(inst, args, kw, info, null, argsReversed);
}

private readonly struct MatchedMethod
Expand Down Expand Up @@ -341,9 +341,9 @@ public MismatchedMethod(Exception exception, MethodBase mb)
/// <param name="kw">The Python keyword arguments.</param>
/// <param name="info">If not null, only bind to that method.</param>
/// <param name="methodinfo">If not null, additionally attempt to bind to the generic methods in this array by inferring generic type parameters.</param>
/// <param name="reverse_args">Reverse arguments of methods. Used for methods such as __radd__, __rsub__, __rmod__ etc</param>
/// <param name="argsReversed">Reverse arguments of methods. Used for methods such as __radd__, __rsub__, __rmod__ etc</param>
/// <returns>A Binding if successful. Otherwise null.</returns>
internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, MethodBase[]? methodinfo, bool reverse_args = false)
internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, MethodBase[]? methodinfo, bool argsReversed = false)
{
// loop to find match, return invoker w/ or w/o error
var kwargDict = new Dictionary<string, PyObject>();
Expand Down Expand Up @@ -371,10 +371,10 @@ public MismatchedMethod(Exception exception, MethodBase mb)
_methods = GetMethods();
}

return Bind(inst, args, kwargDict, _methods, matchGenerics: true, reverse_args);
return Bind(inst, args, kwargDict, _methods, matchGenerics: true, argsReversed);
}

private static Binding? Bind(BorrowedReference inst, BorrowedReference args, Dictionary<string, PyObject> kwargDict, MethodBase[] methods, bool matchGenerics, bool reversed = false)
private static Binding? Bind(BorrowedReference inst, BorrowedReference args, Dictionary<string, PyObject> kwargDict, MethodBase[] methods, bool matchGenerics, bool argsReversed = false)
{
var pynargs = (int)Runtime.PyTuple_Size(args);
var isGeneric = false;
Expand All @@ -394,20 +394,22 @@ public MismatchedMethod(Exception exception, MethodBase mb)
// Binary operator methods will have 2 CLR args but only one Python arg
// (unary operators will have 1 less each), since Python operator methods are bound.
isOperator = isOperator && pynargs == pi.Length - 1;
bool isReverse = isOperator && reversed; // Only cast if isOperator.
bool isReverse = isOperator && argsReversed; // Only cast if isOperator.
if (isReverse && OperatorMethod.IsComparisonOp((MethodInfo)mi))
continue; // Comparison operators in Python have no reverse mode.
if (!MatchesArgumentCount(pynargs, pi, kwargDict, out bool paramsArray, out ArrayList? defaultArgList, out int kwargsMatched, out int defaultsNeeded) && !isOperator)
{
continue;
}
// Preprocessing pi to remove either the first or second argument.
if (isOperator && !isReverse) {
if (isOperator && !isReverse)
{
// The first Python arg is the right operand, while the bound instance is the left.
// We need to skip the first (left operand) CLR argument.
pi = pi.Skip(1).ToArray();
}
else if (isOperator && isReverse) {
else if (isOperator && isReverse)
{
// The first Python arg is the left operand.
// We need to take the first CLR argument.
pi = pi.Take(1).ToArray();
Expand Down Expand Up @@ -817,14 +819,14 @@ static bool MatchesArgumentCount(int positionalArgumentCount, ParameterInfo[] pa
return match;
}

internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, bool reverse_args = false)
internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, bool argsReversed = false)
{
return Invoke(inst, args, kw, null, null, reverse_args);
return Invoke(inst, args, kw, null, null, argsReversed);
}

internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, bool reverse_args = false)
internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, bool argsReversed = false)
{
return Invoke(inst, args, kw, info, null, reverse_args = false);
return Invoke(inst, args, kw, info, null, argsReversed = false);
}

protected static void AppendArgumentTypes(StringBuilder to, BorrowedReference args)
Expand Down Expand Up @@ -860,7 +862,7 @@ protected static void AppendArgumentTypes(StringBuilder to, BorrowedReference ar
to.Append(')');
}

internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, MethodBase[]? methodinfo, bool reverse_args = false)
internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, MethodBase[]? methodinfo, bool argsReversed = false)
{
// No valid methods, nothing to bind.
if (GetMethods().Length == 0)
Expand All @@ -873,7 +875,7 @@ internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference a
return Exceptions.RaiseTypeError(msg.ToString());
}

Binding? binding = Bind(inst, args, kw, info, methodinfo, reverse_args);
Binding? binding = Bind(inst, args, kw, info, methodinfo, argsReversed);
object result;
IntPtr ts = IntPtr.Zero;

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/Types/MethodBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public static NewReference tp_call(BorrowedReference ob, BorrowedReference args,
}
}

return self.m.Invoke(target is null ? BorrowedReference.Null : target, args, kw, self.info.UnsafeValue, self.m.binder.args_reversed);
return self.m.Invoke(target is null ? BorrowedReference.Null : target, args, kw, self.info.UnsafeValue, self.m.binder.argsReversed);
}
finally
{
Expand Down
16 changes: 8 additions & 8 deletions src/runtime/Types/MethodObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ internal class MethodObject : ExtensionType
internal PyString? doc;
internal MaybeType type;

public MethodObject(MaybeType type, string name, MethodBase[] info, bool allow_threads, bool reverse_args = false)
public MethodObject(MaybeType type, string name, MethodBase[] info, bool allow_threads, bool argsReversed = false)
{
this.type = type;
this.name = name;
this.infoList = new List<MaybeMethodInfo>();
binder = new MethodBinder(reverse_args);
binder = new MethodBinder(argsReversed);
foreach (MethodBase item in info)
{
this.infoList.Add(item);
Expand All @@ -45,8 +45,8 @@ public MethodObject(MaybeType type, string name, MethodBase[] info, bool allow_t
binder.allow_threads = allow_threads;
}

public MethodObject(MaybeType type, string name, MethodBase[] info, bool reverse_args = false)
: this(type, name, info, allow_threads: AllowThreads(info), reverse_args)
public MethodObject(MaybeType type, string name, MethodBase[] info, bool argsReversed = false)
: this(type, name, info, allow_threads: AllowThreads(info), argsReversed)
{
}

Expand All @@ -67,14 +67,14 @@ internal MethodBase[] info
}
}

public virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, bool reverse_args = false)
public virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, bool argsReversed = false)
{
return Invoke(inst, args, kw, null, reverse_args);
return Invoke(inst, args, kw, null, argsReversed);
}

public virtual NewReference Invoke(BorrowedReference target, BorrowedReference args, BorrowedReference kw, MethodBase? info, bool reverse_args = false)
public virtual NewReference Invoke(BorrowedReference target, BorrowedReference args, BorrowedReference kw, MethodBase? info, bool argsReversed = false)
{
return binder.Invoke(target, args, kw, info, this.info, reverse_args);
return binder.Invoke(target, args, kw, info, this.info, argsReversed);
}

/// <summary>
Expand Down