Skip to content

Commit 4d1e09a

Browse files
filmorlostmsu
andauthored
fixed ForbidPythonThreadsAttribute being ignored in certain scenarios (#1815)
Co-authored-by: Victor Nova <lost@losttech.software>
1 parent a6e4353 commit 4d1e09a

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/runtime/Types/MethodObject.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal class MethodObject : ExtensionType
2727
internal PyString? doc;
2828
internal MaybeType type;
2929

30-
public MethodObject(MaybeType type, string name, MethodBase[] info, bool allow_threads = MethodBinder.DefaultAllowThreads)
30+
public MethodObject(MaybeType type, string name, MethodBase[] info, bool allow_threads)
3131
{
3232
this.type = type;
3333
this.name = name;
@@ -45,6 +45,11 @@ public MethodObject(MaybeType type, string name, MethodBase[] info, bool allow_t
4545
binder.allow_threads = allow_threads;
4646
}
4747

48+
public MethodObject(MaybeType type, string name, MethodBase[] info)
49+
: this(type, name, info, allow_threads: AllowThreads(info))
50+
{
51+
}
52+
4853
public bool IsInstanceConstructor => name == "__init__";
4954

5055
public MethodObject WithOverloads(MethodBase[] overloads)
@@ -206,5 +211,27 @@ public static NewReference tp_repr(BorrowedReference ob)
206211
var self = (MethodObject)GetManagedObject(ob)!;
207212
return Runtime.PyString_FromString($"<method '{self.name}'>");
208213
}
214+
215+
static bool AllowThreads(MethodBase[] methods)
216+
{
217+
bool hasAllowOverload = false, hasForbidOverload = false;
218+
foreach (var method in methods)
219+
{
220+
bool forbidsThreads = method.GetCustomAttribute<ForbidPythonThreadsAttribute>(inherit: false) != null;
221+
if (forbidsThreads)
222+
{
223+
hasForbidOverload = true;
224+
}
225+
else
226+
{
227+
hasAllowOverload = true;
228+
}
229+
}
230+
231+
if (hasAllowOverload && hasForbidOverload)
232+
throw new NotImplementedException("All method overloads currently must either allow or forbid Python threads together");
233+
234+
return !hasForbidOverload;
235+
}
209236
}
210237
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy