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

Skip to content

Commit 6f3f357

Browse files
committed
Fix PythonEngine.Version
Add TestPythonEngineProperties. Ensure PythonEngine properties are working correctly. Currently only work on 32bit machines. Closes #413
1 parent 5036586 commit 6f3f357

5 files changed

Lines changed: 87 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
5151
- Fixed `AppDomain` unload during GC (#397)(#400)
5252
- Fixed `Py_Main` & `PySys_SetArgvEx` `no mem error` on `UCS4/PY3` (#399)
5353
- Fixed `Python.Runtime.dll.config` on macOS (#120)
54+
- Fixed crash on `PythonEngine.Version` (#413)
5455

5556
### Removed
5657

src/embed_tests/Python.EmbeddingTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<Compile Include="pyrunstring.cs" />
9090
<Compile Include="pythonexception.cs" />
9191
<Compile Include="pytuple.cs" />
92+
<Compile Include="TestPythonEngineProperties.cs" />
9293
</ItemGroup>
9394
<ItemGroup>
9495
<ProjectReference Include="..\runtime\Python.Runtime.csproj">
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using NUnit.Framework;
3+
using Python.Runtime;
4+
5+
namespace Python.EmbeddingTest
6+
{
7+
public class TestPythonEngineProperties
8+
{
9+
[Test]
10+
public static void GetBuildinfoDoesntCrash()
11+
{
12+
using (Py.GIL())
13+
{
14+
string s = PythonEngine.BuildInfo;
15+
16+
Assert.IsTrue(s.Length > 5);
17+
Assert.IsTrue(s.Contains(","));
18+
}
19+
}
20+
21+
[Test]
22+
public static void GetCompilerDoesntCrash()
23+
{
24+
using (Py.GIL())
25+
{
26+
string s = PythonEngine.Compiler;
27+
28+
Assert.IsTrue(s.Length > 0);
29+
Assert.IsTrue(s.Contains("["));
30+
Assert.IsTrue(s.Contains("]"));
31+
}
32+
}
33+
34+
[Test]
35+
public static void GetCopyrightDoesntCrash()
36+
{
37+
using (Py.GIL())
38+
{
39+
string s = PythonEngine.Copyright;
40+
41+
Assert.IsTrue(s.Length > 0);
42+
Assert.IsTrue(s.Contains("Python Software Foundation"));
43+
}
44+
}
45+
46+
[Test]
47+
public static void GetPlatformDoesntCrash()
48+
{
49+
using (Py.GIL())
50+
{
51+
string s = PythonEngine.Platform;
52+
53+
Assert.IsTrue(s.Length > 0);
54+
Assert.IsTrue(s.Contains("x") || s.Contains("win"));
55+
}
56+
}
57+
58+
[Test]
59+
public static void GetVersionDoesntCrash()
60+
{
61+
using (Py.GIL())
62+
{
63+
string s = PythonEngine.Version;
64+
65+
Assert.IsTrue(s.Length > 0);
66+
Assert.IsTrue(s.Contains(","));
67+
}
68+
}
69+
}
70+
}

src/runtime/pythonengine.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Reflection;
6+
using System.Runtime.InteropServices;
67

78
namespace Python.Runtime
89
{
@@ -96,22 +97,27 @@ public static string PythonPath
9697

9798
public static string Version
9899
{
99-
get { return Runtime.Py_GetVersion(); }
100+
get { return Marshal.PtrToStringAnsi(Runtime.Py_GetVersion()); }
100101
}
101102

102103
public static string BuildInfo
103104
{
104-
get { return Runtime.Py_GetBuildInfo(); }
105+
get { return Marshal.PtrToStringAnsi(Runtime.Py_GetBuildInfo()); }
105106
}
106107

107108
public static string Platform
108109
{
109-
get { return Runtime.Py_GetPlatform(); }
110+
get { return Marshal.PtrToStringAnsi(Runtime.Py_GetPlatform()); }
110111
}
111112

112113
public static string Copyright
113114
{
114-
get { return Runtime.Py_GetCopyright(); }
115+
get { return Marshal.PtrToStringAnsi(Runtime.Py_GetCopyright()); }
116+
}
117+
118+
public static string Compiler
119+
{
120+
get { return Marshal.PtrToStringAnsi(Runtime.Py_GetCompiler()); }
115121
}
116122

117123
public static int RunSimpleString(string code)

src/runtime/runtime.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -729,19 +729,19 @@ internal static extern void Py_SetPath(
729729
#endif
730730

731731
[DllImport(PythonDll)]
732-
internal static extern string Py_GetVersion();
732+
internal static extern IntPtr Py_GetVersion();
733733

734734
[DllImport(PythonDll)]
735-
internal static extern string Py_GetPlatform();
735+
internal static extern IntPtr Py_GetPlatform();
736736

737737
[DllImport(PythonDll)]
738-
internal static extern string Py_GetCopyright();
738+
internal static extern IntPtr Py_GetCopyright();
739739

740740
[DllImport(PythonDll)]
741-
internal static extern string Py_GetCompiler();
741+
internal static extern IntPtr Py_GetCompiler();
742742

743743
[DllImport(PythonDll)]
744-
internal static extern string Py_GetBuildInfo();
744+
internal static extern IntPtr Py_GetBuildInfo();
745745

746746
[DllImport(PythonDll)]
747747
internal static extern int PyRun_SimpleString(string code);

0 commit comments

Comments
 (0)