Skip to content
This repository was archived by the owner on Jan 25, 2023. It is now read-only.

Commit 8a183cc

Browse files
author
dse
committed
OutputCapturingFrame thread binding assertions added.
1 parent 5ee0f4a commit 8a183cc

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

src/Python.Plus/PythonWrapper.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public PythonWrapper([CanBeNull] IJsonLogger logger = null)
7777
string pythonetModuleFileName = Path.Combine(AppContext.BaseDirectory, "pythonnet.py");
7878

7979
// Ensuring that pythonnet.py file exists in the bin directory
80-
if (!File.Exists(pythonetModuleFileName) || new FileInfo(pythonetModuleFileName).Length == 0)
80+
if (!File.Exists(pythonetModuleFileName) || (new FileInfo(pythonetModuleFileName).Length == 0))
8181
{
8282
Assembly assembly = typeof(PythonWrapper).GetTypeInfo().Assembly;
8383
using (Stream stream = assembly.GetManifestResourceStream("Python.Net.pythonnet.py"))
@@ -443,13 +443,17 @@ private class OutputCapturingFrame : IPythonOutputCapturingFrame
443443
[NotNull]
444444
private dynamic _stdErr;
445445

446+
private int _threadId;
447+
446448
public OutputCapturingFrame(
447449
[NotNull] string callerFilePath,
448450
[NotNull] string callerMemberName,
449451
int callerLineNumber = 0)
450452
{
451453
VerifyEngineAlive();
452454

455+
_threadId = Thread.CurrentThread.ManagedThreadId;
456+
453457
CallerFilePath = callerFilePath;
454458
CallerMemberName = callerMemberName;
455459
CallerLineNumber = callerLineNumber;
@@ -504,6 +508,9 @@ public void Dispose()
504508
$"Inner python output frame was not disposed. File={CallerFilePath}, Method={CallerMemberName}, Line={CallerLineNumber}\nInner frame:\n{innerLockStr}");
505509
}
506510

511+
Debug.Assert(
512+
_threadId == Thread.CurrentThread.ManagedThreadId,
513+
"Dispose should be called from the same thread.");
507514
Debug.Assert(_instance != null, "_instance != null");
508515
Debug.Assert(_curGIL != null, "_curGIL != null");
509516

@@ -582,12 +589,12 @@ public PyGIL(
582589

583590
if (_parent == null)
584591
{
585-
var gilTimer = CodeTimer.Start(3);
592+
CodeTimer gilTimer = CodeTimer.Start(3);
586593
_pythonNetGIL = Py.GIL();
587594
if (gilTimer.Time > 1.0)
588595
{
589596
// ReSharper disable once PossibleNullReferenceException
590-
_instance.Log.Warning(_=>_("GIL takes too long"));
597+
_instance.Log.Warning(_ => _("GIL takes too long"));
591598
}
592599
}
593600
else

0 commit comments

Comments
 (0)