|
1 |
| -using System; |
| 1 | +using System; |
2 | 2 | using NUnit.Framework;
|
3 | 3 | using Python.Runtime;
|
4 | 4 |
|
@@ -47,5 +47,46 @@ public static void RefCountTest()
|
47 | 47 |
|
48 | 48 | Runtime.Runtime.Py_Finalize();
|
49 | 49 | }
|
| 50 | + |
| 51 | + [Test] |
| 52 | + public static void PyCheck_Iter_PyObject_IsIterable_Test() |
| 53 | + { |
| 54 | + Runtime.Runtime.Py_Initialize(); |
| 55 | + |
| 56 | + // Tests that a python list is an iterable, but not an iterator |
| 57 | + var pyList = Runtime.Runtime.PyList_New(0); |
| 58 | + Assert.IsFalse(Runtime.Runtime.PyIter_Check(pyList)); |
| 59 | + Assert.IsTrue(Runtime.Runtime.PyObject_IsIterable(pyList)); |
| 60 | + |
| 61 | + // Tests that a python list iterator is both an iterable and an iterator |
| 62 | + var pyListIter = Runtime.Runtime.PyObject_GetIter(pyList); |
| 63 | + Assert.IsTrue(Runtime.Runtime.PyObject_IsIterable(pyListIter)); |
| 64 | + Assert.IsTrue(Runtime.Runtime.PyIter_Check(pyListIter)); |
| 65 | + |
| 66 | + // Tests that a python float is neither an iterable nor an iterator |
| 67 | + var pyFloat = Runtime.Runtime.PyFloat_FromDouble(2.73); |
| 68 | + Assert.IsFalse(Runtime.Runtime.PyObject_IsIterable(pyFloat)); |
| 69 | + Assert.IsFalse(Runtime.Runtime.PyIter_Check(pyFloat)); |
| 70 | + |
| 71 | + Runtime.Runtime.Py_Finalize(); |
| 72 | + } |
| 73 | + |
| 74 | + [Test] |
| 75 | + public static void PyCheck_Iter_PyObject_IsIterable_ThreadingLock_Test() |
| 76 | + { |
| 77 | + Runtime.Runtime.Py_Initialize(); |
| 78 | + |
| 79 | + // Create an instance of threading.Lock, which is one of the very few types that does not have the |
| 80 | + // TypeFlags.HaveIter set in Python 2. This tests a different code path in PyObject_IsIterable and PyIter_Check. |
| 81 | + var threading = Runtime.Runtime.PyImport_ImportModule("threading"); |
| 82 | + var threadingDict = Runtime.Runtime.PyModule_GetDict(threading); |
| 83 | + var lockType = Runtime.Runtime.PyDict_GetItemString(threadingDict, "Lock"); |
| 84 | + var lockInstance = Runtime.Runtime.PyObject_CallObject(lockType, Runtime.Runtime.PyTuple_New(0)); |
| 85 | + |
| 86 | + Assert.IsFalse(Runtime.Runtime.PyObject_IsIterable(lockInstance)); |
| 87 | + Assert.IsFalse(Runtime.Runtime.PyIter_Check(lockInstance)); |
| 88 | + |
| 89 | + Runtime.Runtime.Py_Finalize(); |
| 90 | + } |
50 | 91 | }
|
51 | 92 | }
|
0 commit comments