Skip to content

clrmethod working for python 2 #494

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 8 commits into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add Runtime.PyObject_IsIterable and fix PyIter_Check for Python 2
  • Loading branch information
Rickard Holmberg committed Jun 15, 2017
commit 6bfef31fc176159da5898d7c1b06246d7765b54e
2 changes: 1 addition & 1 deletion src/runtime/pyobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ public bool IsCallable()
/// </remarks>
public bool IsIterable()
{
return Runtime.PyIter_Check(obj);
return Runtime.PyObject_IsIterable(obj);
}


Expand Down
39 changes: 27 additions & 12 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
Expand Down Expand Up @@ -274,19 +274,17 @@ internal static void Initialize()

Error = new IntPtr(-1);

#if PYTHON3
IntPtr dllLocal = IntPtr.Zero;
if (PythonDll != "__Internal")
{
NativeMethods.LoadLibrary(PythonDll);
dllLocal = NativeMethods.LoadLibrary(PythonDll);
}
_PyObject_NextNotImplemented = NativeMethods.GetProcAddress(dllLocal, "_PyObject_NextNotImplemented");
#if !(MONO_LINUX || MONO_OSX)
if (dllLocal != IntPtr.Zero)
{
NativeMethods.FreeLibrary(dllLocal);
}
#endif
#endif

// Initialize modules that depend on the runtime class.
Expand Down Expand Up @@ -349,8 +347,8 @@ internal static int AtExit()

#if PYTHON3
internal static IntPtr PyBytesType;
internal static IntPtr _PyObject_NextNotImplemented;
#endif
internal static IntPtr _PyObject_NextNotImplemented;

internal static IntPtr PyNotImplemented;
internal const int Py_LT = 0;
Expand Down Expand Up @@ -780,6 +778,22 @@ internal static string PyObject_GetTypeName(IntPtr op)
return Marshal.PtrToStringAnsi(ppName);
}

/// <summary>
/// Test whether the Python object is an iterable.
/// </summary>
internal static bool PyObject_IsIterable(IntPtr pointer)
{
var ob_type = Marshal.ReadIntPtr(pointer, ObjectOffset.ob_type);
#if PYTHON2
int Py_TPFLAGS_HAVE_ITER = 1 << 7;
long tp_flags = Marshal.ReadInt64(ob_type, TypeOffset.tp_flags);
if ((tp_flags & Py_TPFLAGS_HAVE_ITER) == 0)
return false;
#endif
IntPtr tp_iter = Marshal.ReadIntPtr(ob_type, TypeOffset.tp_iter);
return tp_iter != null;
}

[DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern int PyObject_HasAttrString(IntPtr pointer, string name);

Expand Down Expand Up @@ -1425,17 +1439,18 @@ internal static bool PyTuple_Check(IntPtr ob)
// Python iterator API
//====================================================================

#if PYTHON2
[DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern bool PyIter_Check(IntPtr pointer);
#elif PYTHON3
internal static bool PyIter_Check(IntPtr pointer)
{
var ob_type = (IntPtr)Marshal.PtrToStructure(pointer + ObjectOffset.ob_type, typeof(IntPtr));
IntPtr tp_iternext = ob_type + TypeOffset.tp_iternext;
var ob_type = Marshal.ReadIntPtr(pointer, ObjectOffset.ob_type);
#if PYTHON2
int Py_TPFLAGS_HAVE_ITER = 1 << 7;
long tp_flags = Marshal.ReadInt64(ob_type, TypeOffset.tp_flags);
if ((tp_flags & Py_TPFLAGS_HAVE_ITER) == 0)
return false;
#endif
IntPtr tp_iternext = Marshal.ReadIntPtr(ob_type, TypeOffset.tp_iternext);
return tp_iternext != null && tp_iternext != _PyObject_NextNotImplemented;
}
#endif

[DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr PyIter_Next(IntPtr pointer);
Expand Down