Skip to content

fixed ForbidPythonThreadsAttribute being ignored in certain scenarios #1815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2022
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;
}
}
}