Skip to content

Interop methods with Py_ssize_t works differently in NetCoreApp 2.0 #531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- Fixed Visual Studio 2017 compat ([#434][i434]) for setup.py
- Fixed crashes when integrating pythonnet in Unity3d ([#714][i714]),
related to unloading the Application Domain
- Fixed interop methods with Py_ssize_t. NetCoreApp 2.0 is more sensitive than net40 and requires this fix. ([#531][p531])
- Fixed crash on exit of the Python interpreter if a python class
derived from a .NET class has a `__namespace__` or `__assembly__`
attribute ([#481][i481])
Expand Down Expand Up @@ -689,3 +690,4 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
[p163]: https://github.com/pythonnet/pythonnet/pull/163
[p625]: https://github.com/pythonnet/pythonnet/pull/625
[i131]: https://github.com/pythonnet/pythonnet/issues/131
[p531]: https://github.com/pythonnet/pythonnet/pull/531
6 changes: 2 additions & 4 deletions src/embed_tests/TestPySequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ public void TestRepeat()
PyObject actual = t1.Repeat(3);
Assert.AreEqual("FooFooFoo", actual.ToString());

// On 32 bit system this argument should be int, but on the 64 bit system this should be long value.
// This works on the Framework 4.0 accidentally, it should produce out of memory!
// actual = t1.Repeat(-3);
// Assert.AreEqual("", actual.ToString());
actual = t1.Repeat(-3);
Assert.AreEqual("", actual.ToString());
}

[Test]
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/arrayobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
return IntPtr.Zero;
}

int count = Runtime.PyTuple_Size(idx);
var count = Runtime.PyTuple_Size(idx);

var args = new int[count];

Expand Down Expand Up @@ -186,7 +186,7 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
return -1;
}

int count = Runtime.PyTuple_Size(idx);
var count = Runtime.PyTuple_Size(idx);
var args = new int[count];

for (var i = 0; i < count; i++)
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/assemblymanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private static Assembly ResolveHandler(object ob, ResolveEventArgs args)
internal static void UpdatePath()
{
IntPtr list = Runtime.PySys_GetObject("path");
int count = Runtime.PyList_Size(list);
var count = Runtime.PyList_Size(list);
if (count != pypath.Count)
{
pypath.Clear();
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/classobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
}

// Get the args passed in.
int i = Runtime.PyTuple_Size(args);
var i = Runtime.PyTuple_Size(args);
IntPtr defaultArgs = cls.indexer.GetDefaultArgs(args);
int numOfDefaultArgs = Runtime.PyTuple_Size(defaultArgs);
int temp = i + numOfDefaultArgs;
var numOfDefaultArgs = Runtime.PyTuple_Size(defaultArgs);
var temp = i + numOfDefaultArgs;
IntPtr real = Runtime.PyTuple_New(temp + 1);
for (var n = 0; n < i; n++)
{
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ private static void SetConversionError(IntPtr value, Type target)
private static bool ToArray(IntPtr value, Type obType, out object result, bool setError)
{
Type elementType = obType.GetElementType();
int size = Runtime.PySequence_Size(value);
var size = Runtime.PySequence_Size(value);
result = null;

if (size < 0)
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public static void SetError(Exception e)
/// </remarks>
public static bool ErrorOccurred()
{
return Runtime.PyErr_Occurred() != 0;
return Runtime.PyErr_Occurred() != IntPtr.Zero;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/importhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
// hook is saved as this.py_import. This version handles CLR
// import and defers to the normal builtin for everything else.

int num_args = Runtime.PyTuple_Size(args);
var num_args = Runtime.PyTuple_Size(args);
if (num_args < 1)
{
return Exceptions.RaiseTypeError("__import__() takes at least 1 argument (0 given)");
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/indexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal void SetItem(IntPtr inst, IntPtr args)

internal bool NeedsDefaultArgs(IntPtr args)
{
int pynargs = Runtime.PyTuple_Size(args);
var pynargs = Runtime.PyTuple_Size(args);
MethodBase[] methods = SetterBinder.GetMethods();
if (methods.Length == 0)
{
Expand All @@ -72,7 +72,7 @@ internal bool NeedsDefaultArgs(IntPtr args)
return false;
}

for (int v = pynargs; v < clrnargs; v++)
for (var v = pynargs; v < clrnargs; v++)
{
if (pi[v].DefaultValue == DBNull.Value)
{
Expand All @@ -95,7 +95,7 @@ internal IntPtr GetDefaultArgs(IntPtr args)
{
return Runtime.PyTuple_New(0);
}
int pynargs = Runtime.PyTuple_Size(args);
var pynargs = Runtime.PyTuple_Size(args);

// Get the default arg tuple
MethodBase[] methods = SetterBinder.GetMethods();
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/interfaceobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static InterfaceObject()
public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
{
var self = (InterfaceObject)GetManagedObject(tp);
int nargs = Runtime.PyTuple_Size(args);
var nargs = Runtime.PyTuple_Size(args);
Type type = self.type;
object obj;

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/metatype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static IntPtr Initialize()
/// </summary>
public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
{
int len = Runtime.PyTuple_Size(args);
var len = Runtime.PyTuple_Size(args);
if (len < 3)
{
return Exceptions.RaiseTypeError("invalid argument list");
Expand Down
10 changes: 5 additions & 5 deletions src/runtime/methodbinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
{
// loop to find match, return invoker w/ or /wo error
MethodBase[] _methods = null;
int pynargs = Runtime.PyTuple_Size(args);
var pynargs = (int)Runtime.PyTuple_Size(args);
object arg;
var isGeneric = false;
ArrayList defaultArgList = null;
Expand All @@ -301,9 +301,9 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
isGeneric = true;
}
ParameterInfo[] pi = mi.GetParameters();
int clrnargs = pi.Length;
var clrnargs = pi.Length;
var match = false;
int arrayStart = -1;
var arrayStart = -1;
var outs = 0;

if (pynargs == clrnargs)
Expand All @@ -314,7 +314,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
{
match = true;
defaultArgList = new ArrayList();
for (int v = pynargs; v < clrnargs; v++)
for (var v = pynargs; v < clrnargs; v++)
{
if (pi[v].DefaultValue == DBNull.Value)
{
Expand All @@ -338,7 +338,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
{
var margs = new object[clrnargs];

for (var n = 0; n < clrnargs; n++)
for (int n = 0; n < clrnargs; n++)
{
IntPtr op;
if (n < pynargs)
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/methodbinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
{
if (self.info.IsGenericMethod)
{
int len = Runtime.PyTuple_Size(args); //FIXME: Never used
var len = Runtime.PyTuple_Size(args); //FIXME: Never used
Type[] sigTp = Runtime.PythonArgsToTypeArray(args, true);
if (sigTp != null)
{
Expand All @@ -129,7 +129,7 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)

if (target == IntPtr.Zero && !self.m.IsStatic())
{
int len = Runtime.PyTuple_Size(args);
var len = Runtime.PyTuple_Size(args);
if (len < 1)
{
Exceptions.SetError(Exceptions.TypeError, "not enough arguments");
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/pyobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,9 @@ public virtual void DelItem(int index)
/// Returns the length for objects that support the Python sequence
/// protocol, or 0 if the object does not support the protocol.
/// </remarks>
public virtual int Length()
public virtual long Length()
{
int s = Runtime.PyObject_Size(obj);
var s = Runtime.PyObject_Size(obj);
if (s < 0)
{
Runtime.PyErr_Clear();
Expand Down
Loading