From 025cc5cdcf17d33f619b8582970b8fe8b28c113e Mon Sep 17 00:00:00 2001 From: Victor Nova Date: Sun, 12 Jun 2022 10:16:19 -0700 Subject: [PATCH] fixed ForbidPythonThreadsAttribute being ignored in certain scenarios --- src/runtime/Types/MethodObject.cs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/runtime/Types/MethodObject.cs b/src/runtime/Types/MethodObject.cs index 55ad06e2d..05198da76 100644 --- a/src/runtime/Types/MethodObject.cs +++ b/src/runtime/Types/MethodObject.cs @@ -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; @@ -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) @@ -206,5 +211,27 @@ public static NewReference tp_repr(BorrowedReference ob) var self = (MethodObject)GetManagedObject(ob)!; return Runtime.PyString_FromString($""); } + + static bool AllowThreads(MethodBase[] methods) + { + bool hasAllowOverload = false, hasForbidOverload = false; + foreach (var method in methods) + { + bool forbidsThreads = method.GetCustomAttribute(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; + } } }