Skip to content
Prev Previous commit
Next Next commit
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`)
  • Loading branch information
filmor committed Apr 11, 2020
commit 054b87263827c47b5e2f7cefad932586392fd351
6 changes: 6 additions & 0 deletions Python.Runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="resources\clr.py">
<LogicalName>clr.py</LogicalName>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Reflection.Emit" Version="4.6.0" />
</ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions Python.Runtime/moduleobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public static IntPtr tp_getattro(IntPtr ob, IntPtr key)
Exceptions.SetError(e);
return IntPtr.Zero;
}


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

[ModuleFunction]
public static int _AtExit()
public static void _AtExit()
{
return Runtime.AtExit();
Runtime.AtExit();
}
}
}
12 changes: 10 additions & 2 deletions Python.Runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,10 @@ static void OnDomainUnload(object _, EventArgs __)
/// </summary>
public static int InternalInitialize(IntPtr data, int size)
{
IntPtr gilState = IntPtr.Zero;
try
{
gilState = Runtime.PyGILState_Ensure();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have to be inside try block?

Initialize(setSysArgv: false);

// Trickery - when the import hook is installed into an already
Expand Down Expand Up @@ -292,16 +294,22 @@ public static int InternalInitialize(IntPtr data, int size)
PythonEngine.Exec(code);
return 0;
}
catch (PythonException e)
catch (PythonException exc)
{
e.Restore();
exc.Restore();
Console.WriteLine($"{exc.Message}\n{exc.Format()}");
return -1;
}
catch (Exception exc)
{
Console.WriteLine($"{exc}\n{exc.StackTrace}");
return -1;
}
finally
{
if (gilState != IntPtr.Zero)
Runtime.PyGILState_Release(gilState);
}
}

/// <summary>
Expand Down
34 changes: 23 additions & 11 deletions Python.Runtime/runtime.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;
using System;
using System.Runtime.InteropServices;
using System.Security;
Expand All @@ -22,7 +23,7 @@ public class Runtime

public static int UCS => _UCS;

#if UCS2
#if !UCS2
internal const int _UCS = 4;

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

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

op = PyLong_FromLong(0);
SetPyMember(ref PyLongType, PyObject_Type(op),
() => PyLongType = IntPtr.Zero);
XDecref(op);

#if !PYTHON2
PyIntType = PyLongType;
#else
op = PyInt_FromInt32(1000);
SetPyMember(ref PyIntType, PyObject_Type(op),
() => PyIntType = IntPtr.Zero);
XDecref(op);
#endif

op = PyFloat_FromDouble(0);
SetPyMember(ref PyFloatType, PyObject_Type(op),
() => PyFloatType = IntPtr.Zero);
Expand Down Expand Up @@ -351,16 +358,15 @@ internal static void Shutdown()
}

// called *without* the GIL acquired by clr._AtExit
internal static int AtExit()
internal static void AtExit()
{
lock (IsFinalizingLock)
{
IsFinalizing = true;
}
return 0;
}

private static void SetPyMember(ref IntPtr obj, IntPtr value, Action onRelease)
private static void SetPyMember(ref IntPtr obj, IntPtr value, Action onRelease, [CallerLineNumber]int lineNumber = 0)
{
// XXX: For current usages, value should not be null.
PythonException.ThrowIfIsNull(value);
Expand Down Expand Up @@ -1444,6 +1450,7 @@ internal static string GetManagedString(IntPtr op)
int size = length * _UCS;
var buffer = new byte[size];
Marshal.Copy(p, buffer, 0, size);

return PyEncoding.GetString(buffer, 0, size);
}

Expand Down Expand Up @@ -1877,8 +1884,13 @@ internal static void SetNoSiteFlag()
/// </summary>
internal static IntPtr GetBuiltins()
{
return IsPython3 ? PyImport_ImportModule("builtins")
: PyImport_ImportModule("__builtin__");
#if !PYTHON2
const string modName = "builtins";
#else
const string modName = "__builtin__";
#endif
Console.WriteLine($"Loading {modName}");
return PyImport_ImportModule(modName);
}
}

Expand Down