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

Skip to content

Commit 054b872

Browse files
committed
Fix a few bugs
- Swapped UCS2 in one place - Explicitly acquire GILState in InternalInitialize - Fix a few problems with short-ints by using larger numbers (`PyLongType`) or dropping the respective return (`AtExit`)
1 parent baf0535 commit 054b872

File tree

4 files changed

+43
-17
lines changed

4 files changed

+43
-17
lines changed

Python.Runtime/Python.Runtime.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
66
</PropertyGroup>
77

8+
<ItemGroup>
9+
<EmbeddedResource Include="resources\clr.py">
10+
<LogicalName>clr.py</LogicalName>
11+
</EmbeddedResource>
12+
</ItemGroup>
13+
814
<ItemGroup>
915
<PackageReference Include="System.Reflection.Emit" Version="4.6.0" />
1016
</ItemGroup>

Python.Runtime/moduleobject.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public static IntPtr tp_getattro(IntPtr ob, IntPtr key)
284284
Exceptions.SetError(e);
285285
return IntPtr.Zero;
286286
}
287-
287+
288288

289289
if (attr == null)
290290
{
@@ -435,7 +435,7 @@ public static Assembly AddReference(string name)
435435
/// clr.GetClrType(IComparable) gives you the Type for IComparable,
436436
/// that you can e.g. perform reflection on. Similar to typeof(IComparable) in C#
437437
/// or clr.GetClrType(IComparable) in IronPython.
438-
///
438+
///
439439
/// </summary>
440440
/// <param name="type"></param>
441441
/// <returns>The Type object</returns>
@@ -475,9 +475,9 @@ public static string[] ListAssemblies(bool verbose)
475475
}
476476

477477
[ModuleFunction]
478-
public static int _AtExit()
478+
public static void _AtExit()
479479
{
480-
return Runtime.AtExit();
480+
Runtime.AtExit();
481481
}
482482
}
483483
}

Python.Runtime/pythonengine.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,10 @@ static void OnDomainUnload(object _, EventArgs __)
256256
/// </summary>
257257
public static int InternalInitialize(IntPtr data, int size)
258258
{
259+
IntPtr gilState = IntPtr.Zero;
259260
try
260261
{
262+
gilState = Runtime.PyGILState_Ensure();
261263
Initialize(setSysArgv: false);
262264

263265
// Trickery - when the import hook is installed into an already
@@ -292,16 +294,22 @@ public static int InternalInitialize(IntPtr data, int size)
292294
PythonEngine.Exec(code);
293295
return 0;
294296
}
295-
catch (PythonException e)
297+
catch (PythonException exc)
296298
{
297-
e.Restore();
299+
exc.Restore();
300+
Console.WriteLine($"{exc.Message}\n{exc.Format()}");
298301
return -1;
299302
}
300303
catch (Exception exc)
301304
{
302305
Console.WriteLine($"{exc}\n{exc.StackTrace}");
303306
return -1;
304307
}
308+
finally
309+
{
310+
if (gilState != IntPtr.Zero)
311+
Runtime.PyGILState_Release(gilState);
312+
}
305313
}
306314

307315
/// <summary>

Python.Runtime/runtime.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Runtime.CompilerServices;
12
using System;
23
using System.Runtime.InteropServices;
34
using System.Security;
@@ -22,7 +23,7 @@ public class Runtime
2223

2324
public static int UCS => _UCS;
2425

25-
#if UCS2
26+
#if !UCS2
2627
internal const int _UCS = 4;
2728

2829
/// <summary>
@@ -215,16 +216,22 @@ internal static void Initialize(bool initSigs = false)
215216
() => PyDictType = IntPtr.Zero);
216217
XDecref(op);
217218

218-
op = PyInt_FromInt32(0);
219-
SetPyMember(ref PyIntType, PyObject_Type(op),
220-
() => PyIntType = IntPtr.Zero);
221-
XDecref(op);
219+
// Choose a number >1000 st. we get a real PyObject
220+
op = PyLong_FromLong(1000);
222221

223-
op = PyLong_FromLong(0);
224222
SetPyMember(ref PyLongType, PyObject_Type(op),
225223
() => PyLongType = IntPtr.Zero);
226224
XDecref(op);
227225

226+
#if !PYTHON2
227+
PyIntType = PyLongType;
228+
#else
229+
op = PyInt_FromInt32(1000);
230+
SetPyMember(ref PyIntType, PyObject_Type(op),
231+
() => PyIntType = IntPtr.Zero);
232+
XDecref(op);
233+
#endif
234+
228235
op = PyFloat_FromDouble(0);
229236
SetPyMember(ref PyFloatType, PyObject_Type(op),
230237
() => PyFloatType = IntPtr.Zero);
@@ -351,16 +358,15 @@ internal static void Shutdown()
351358
}
352359

353360
// called *without* the GIL acquired by clr._AtExit
354-
internal static int AtExit()
361+
internal static void AtExit()
355362
{
356363
lock (IsFinalizingLock)
357364
{
358365
IsFinalizing = true;
359366
}
360-
return 0;
361367
}
362368

363-
private static void SetPyMember(ref IntPtr obj, IntPtr value, Action onRelease)
369+
private static void SetPyMember(ref IntPtr obj, IntPtr value, Action onRelease, [CallerLineNumber]int lineNumber = 0)
364370
{
365371
// XXX: For current usages, value should not be null.
366372
PythonException.ThrowIfIsNull(value);
@@ -1444,6 +1450,7 @@ internal static string GetManagedString(IntPtr op)
14441450
int size = length * _UCS;
14451451
var buffer = new byte[size];
14461452
Marshal.Copy(p, buffer, 0, size);
1453+
14471454
return PyEncoding.GetString(buffer, 0, size);
14481455
}
14491456

@@ -1877,8 +1884,13 @@ internal static void SetNoSiteFlag()
18771884
/// </summary>
18781885
internal static IntPtr GetBuiltins()
18791886
{
1880-
return IsPython3 ? PyImport_ImportModule("builtins")
1881-
: PyImport_ImportModule("__builtin__");
1887+
#if !PYTHON2
1888+
const string modName = "builtins";
1889+
#else
1890+
const string modName = "__builtin__";
1891+
#endif
1892+
Console.WriteLine($"Loading {modName}");
1893+
return PyImport_ImportModule(modName);
18821894
}
18831895
}
18841896

0 commit comments

Comments
 (0)