🌐 AI搜索 & 代理 主页
Skip to content
Merged
Changes from all commits
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
29 changes: 28 additions & 1 deletion src/runtime/Types/MethodObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal class MethodObject : ExtensionType
internal PyString? doc;
internal MaybeType type;

public MethodObject(MaybeType type, string name, MethodBase[] info, bool allow_threads = MethodBinder.DefaultAllowThreads)
public MethodObject(MaybeType type, string name, MethodBase[] info, bool allow_threads)
{
this.type = type;
this.name = name;
Expand All @@ -45,6 +45,11 @@ public MethodObject(MaybeType type, string name, MethodBase[] info, bool allow_t
binder.allow_threads = allow_threads;
}

public MethodObject(MaybeType type, string name, MethodBase[] info)
: this(type, name, info, allow_threads: AllowThreads(info))
{
}

public bool IsInstanceConstructor => name == "__init__";

public MethodObject WithOverloads(MethodBase[] overloads)
Expand Down Expand Up @@ -206,5 +211,27 @@ public static NewReference tp_repr(BorrowedReference ob)
var self = (MethodObject)GetManagedObject(ob)!;
return Runtime.PyString_FromString($"<method '{self.name}'>");
}

static bool AllowThreads(MethodBase[] methods)
{
bool hasAllowOverload = false, hasForbidOverload = false;
foreach (var method in methods)
{
bool forbidsThreads = method.GetCustomAttribute<ForbidPythonThreadsAttribute>(inherit: false) != null;
if (forbidsThreads)
{
hasForbidOverload = true;
}
else
{
hasAllowOverload = true;
}
}

if (hasAllowOverload && hasForbidOverload)
throw new NotImplementedException("All method overloads currently must either allow or forbid Python threads together");

return !hasForbidOverload;
}
}
}