From 3d611cb9f877cfa350528bd95f6e49c7af9c4e14 Mon Sep 17 00:00:00 2001 From: Meinrad Recheis Date: Sun, 10 May 2020 12:43:12 +0200 Subject: [PATCH 1/4] added GetPyNone() to Runtime --- src/runtime/runtime.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index 3d1fe2d39..9d6be9277 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -473,6 +473,19 @@ private static void ResetPyMembers() internal static IntPtr PyNone; internal static IntPtr Error; + public static PyObject _none; + public static PyObject GetPyNone() + { + if (_none == null) + { + var result = Runtime.PyNone; + Runtime.XIncref(result); + _none= new PyObject(result); + } + + return _none; + } + /// /// Check if any Python Exceptions occurred. /// If any exist throw new PythonException. From a3066e08d8e42e805ed818575f7be0b8042e2121 Mon Sep 17 00:00:00 2001 From: Meinrad Recheis Date: Wed, 22 May 2019 08:48:30 +0200 Subject: [PATCH 2/4] pyobject: added IsNone() --- src/runtime/pyobject.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/runtime/pyobject.cs b/src/runtime/pyobject.cs index 96968e678..491c62e30 100644 --- a/src/runtime/pyobject.cs +++ b/src/runtime/pyobject.cs @@ -979,6 +979,10 @@ public bool IsTrue() return Runtime.PyObject_IsTrue(obj) != 0; } + /// + /// Return true if the object is None + /// + public bool IsNone() => CheckNone(this) == null; /// /// Dir Method From a66610aa5e191a94a227427764444b5e75fff872 Mon Sep 17 00:00:00 2001 From: Meinrad Recheis Date: Sun, 10 May 2020 12:58:35 +0200 Subject: [PATCH 3/4] Added Runtime.GetPyNone() and PyObject.IsNone() --- AUTHORS.md | 1 + CHANGELOG.md | 1 + 2 files changed, 2 insertions(+) diff --git a/AUTHORS.md b/AUTHORS.md index b0d1f0c91..19cd4f5ed 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -45,6 +45,7 @@ - Luke Stratman ([@lstratman](https://github.com/lstratman)) - Konstantin Posudevskiy ([@konstantin-posudevskiy](https://github.com/konstantin-posudevskiy)) - Matthias Dittrich ([@matthid](https://github.com/matthid)) +- Meinrad Recheis ([@henon](https://github.com/henon)) - Mohamed Koubaa ([@koubaa](https://github.com/koubaa)) - Patrick Stewart ([@patstew](https://github.com/patstew)) - Raphael Nestler ([@rnestler](https://github.com/rnestler)) diff --git a/CHANGELOG.md b/CHANGELOG.md index f754cbc4e..90d909e00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. - Added support for Jetson Nano. - Added support for __len__ for .NET classes that implement ICollection - Added PythonException.Format method to format exceptions the same as traceback.format_exception +- Added GetPyNone() to Runtime and IsNone() to PyObject ### Changed From f5b32f4e4ac42e97e9b0b402511cea9878572b3b Mon Sep 17 00:00:00 2001 From: Meinrad Recheis Date: Sun, 10 May 2020 22:25:59 +0200 Subject: [PATCH 4/4] Added Runtime.None which returns a reference to None to be able to pass it into Python from .NET --- CHANGELOG.md | 3 ++- src/runtime/runtime.cs | 13 +++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90d909e00..13baf557e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. - Added support for Jetson Nano. - Added support for __len__ for .NET classes that implement ICollection - Added PythonException.Format method to format exceptions the same as traceback.format_exception -- Added GetPyNone() to Runtime and IsNone() to PyObject +- Added Runtime.None to be able to pass None as parameter into Python from .NET +- Added PyObject.IsNone() to check if a Python object is None in .NET. ### Changed diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index 9d6be9277..2e06c9cba 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -473,17 +473,14 @@ private static void ResetPyMembers() internal static IntPtr PyNone; internal static IntPtr Error; - public static PyObject _none; - public static PyObject GetPyNone() + public static PyObject None { - if (_none == null) + get { - var result = Runtime.PyNone; - Runtime.XIncref(result); - _none= new PyObject(result); + var none = Runtime.PyNone; + Runtime.XIncref(none); + return new PyObject(none); } - - return _none; } ///