Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d1e09a

Browse files
filmorlostmsu
andauthored
fixed ForbidPythonThreadsAttribute being ignored in certain scenarios (pythonnet#1815)
Co-authored-by: Victor Nova <[email protected]>
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

+28-1
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)