8000 Add IsNone() and Runtime.None (#1137) · sdpython/pythonnet@960286d · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jul 22, 2023. It is now read-only.

Commit 960286d

Browse files
authored
Add IsNone() and Runtime.None (pythonnet#1137)
* Add `PyObject.IsNone()` * Add `Runtime.None` which returns a reference to `None` as a `PyObject` to be able to pass it into Python from .NET
1 parent 2e0874a commit 960286d

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
- Luke Stratman ([@lstratman](https://github.com/lstratman))
4646
- Konstantin Posudevskiy ([@konstantin-posudevskiy](https://github.com/konstantin-posudevskiy))
4747
- Matthias Dittrich ([@matthid](https://github.com/matthid))
48+
- Meinrad Recheis ([@henon](https://github.com/henon))
4849
- Mohamed Koubaa ([@koubaa](https://github.com/koubaa))
4950
- Patrick Stewart ([@patstew](https://github.com/patstew))
5051
- Raphael Nestler ([@rnestler](https://github.com/rnestler))

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
1414
- Added support for Jetson Nano.
1515
- Added support for __len__ for .NET classes that implement ICollection
1616
- Added PythonException.Format method to format exceptions the same as traceback.format_exception
17+
- Added Runtime.None to be able to pass None as parameter into Python from .NET
18+
- Added PyObject.IsNone() to check if a Python object is None in .NET.
1719

1820
### Changed
1921

src/runtime/pyobject.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,10 @@ public bool IsTrue()
979979
return Runtime.PyObject_IsTrue(obj) != 0;
980980
}
981981

982+
/// <summary>
983+
/// Return true if the object is None
984+
/// </summary>
985+
public bool IsNone() => CheckNone(this) == null;
982986

983987
/// <summary>
984988
/// Dir Method

src/runtime/runtime.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,16 @@ private static void ResetPyMembers()
473473
internal static IntPtr PyNone;
474474
internal static IntPtr Error;
475475

476+
public static PyObject None
477+
{
478+
get
479+
{
480+
var none = Runtime.PyNone;
481+
Runtime.XIncref(none);
482+
return new PyObject(none);
483+
}
484+
}
485+
476486
/// <summary>
477487
/// Check if any Python Exceptions occurred.
478488
/// If any exist throw new PythonException.

0 commit comments

Comments
 (0)
0