forked from dmitriyse/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonWrapperExtensions.cs
More file actions
57 lines (50 loc) · 1.74 KB
/
PythonWrapperExtensions.cs
File metadata and controls
57 lines (50 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
namespace Python.Net
{
using System;
using ClrCoder.Logging;
using ClrCoder.Logging.Std;
using JetBrains.Annotations;
using Runtime;
/// <summary>
/// Extension methods related to <see cref="PythonWrapper"/> .
/// </summary>
[PublicAPI]
public static class PythonWrapperExtensions
{
/// <summary>
/// Writes log debug log entry with python output content.
/// </summary>
/// <param name="logger">Logger to write logs to.</param>
/// <param name="outputFrame">Output frame where logs was collected.</param>
public static void DebugPythonOutput(
[NotNull] this IJsonLogger logger,
[NotNull] IPythonOutputCapturingFrame outputFrame)
{
string stdOut = outputFrame.ReadStdOut();
if (!string.IsNullOrWhiteSpace(stdOut))
{
logger.Debug(stdOut, (_, str) => _($"PyOut:\n{str}"));
}
string stdErr = outputFrame.ReadStdOut();
if (!string.IsNullOrWhiteSpace(stdErr))
{
logger.Debug(stdErr, (_, str) => _($"PyErr:\n{str}"));
}
}
/// <summary>
/// Wraps <see cref="PythonNetException"/> exception.
/// </summary>
/// <param name="exception">Python exception to wrap.</param>
/// <returns>Wrapped exception.</returns>
public static PythonNetException WrapAndDispose(this PythonException exception)
{
if (exception == null)
{
throw new ArgumentNullException(nameof(exception));
}
var result = new PythonNetException(exception);
exception.Dispose();
return result;
}
}
}