This repository was archived by the owner on Jul 22, 2023. It is now read-only.
forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestRuntime.cs
More file actions
114 lines (95 loc) · 4.35 KB
/
TestRuntime.cs
File metadata and controls
114 lines (95 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Python.Runtime;
using Python.Runtime.Platform;
namespace Python.EmbeddingTest
{
public class TestRuntime
{
[OneTimeSetUp]
public void SetUp()
{
// We needs to ensure that no any engines are running.
if (PythonEngine.IsInitialized)
{
PythonEngine.Shutdown();
}
}
[Test]
public static void Py_IsInitializedValue()
{
if (Runtime.Runtime.Py_IsInitialized() == 1)
{
Runtime.Runtime.PyGILState_Ensure();
}
Runtime.Runtime.Py_Finalize();
Assert.AreEqual(0, Runtime.Runtime.Py_IsInitialized());
Runtime.Runtime.Py_Initialize();
Assert.AreEqual(1, Runtime.Runtime.Py_IsInitialized());
Runtime.Runtime.Py_Finalize();
Assert.AreEqual(0, Runtime.Runtime.Py_IsInitialized());
}
[Test]
public static void RefCountTest()
{
Runtime.Runtime.Py_Initialize();
IntPtr op = Runtime.Runtime.PyUnicode_FromString("FooBar");
// New object RefCount should be one
Assert.AreEqual(1, Runtime.Runtime.Refcount(op));
// Checking refcount didn't change refcount
Assert.AreEqual(1, Runtime.Runtime.Refcount(op));
// New reference doesn't increase refcount
IntPtr p = op;
Assert.AreEqual(1, Runtime.Runtime.Refcount(p));
// Py_IncRef/Py_DecRef increase and decrease RefCount
Runtime.Runtime.Py_IncRef(op);
Assert.AreEqual(2, Runtime.Runtime.Refcount(op));
Runtime.Runtime.Py_DecRef(op);
Assert.AreEqual(1, Runtime.Runtime.Refcount(op));
// XIncref/XDecref increase and decrease RefCount
Runtime.Runtime.XIncref(op);
Assert.AreEqual(2, Runtime.Runtime.Refcount(op));
Runtime.Runtime.XDecref(op);
Assert.AreEqual(1, Runtime.Runtime.Refcount(op));
Runtime.Runtime.Py_Finalize();
}
[Test]
public static void PyCheck_Iter_PyObject_IsIterable_Test()
{
Runtime.Runtime.Py_Initialize();
// Tests that a python list is an iterable, but not an iterator
var pyList = Runtime.Runtime.PyList_New(0);
Assert.IsFalse(Runtime.Runtime.PyIter_Check(pyList));
Assert.IsTrue(Runtime.Runtime.PyObject_IsIterable(pyList));
// Tests that a python list iterator is both an iterable and an iterator
var pyListIter = Runtime.Runtime.PyObject_GetIter(pyList);
Assert.IsTrue(Runtime.Runtime.PyObject_IsIterable(pyListIter));
Assert.IsTrue(Runtime.Runtime.PyIter_Check(pyListIter));
// Tests that a python float is neither an iterable nor an iterator
var pyFloat = Runtime.Runtime.PyFloat_FromDouble(2.73);
Assert.IsFalse(Runtime.Runtime.PyObject_IsIterable(pyFloat));
Assert.IsFalse(Runtime.Runtime.PyIter_Check(pyFloat));
Runtime.Runtime.Py_Finalize();
}
[Test]
public static void PyCheck_Iter_PyObject_IsIterable_ThreadingLock_Test()
{
Runtime.Runtime.Py_Initialize();
// Create an instance of threading.Lock, which is one of the very few types that does not have the
// TypeFlags.HaveIter set in Python 2. This tests a different code path in PyObject_IsIterable and PyIter_Check.
var threading = Runtime.Runtime.PyImport_ImportModule("threading");
Exceptions.ErrorCheck(threading);
var threadingDict = Runtime.Runtime.PyModule_GetDict(threading);
Exceptions.ErrorCheck(threadingDict);
var lockType = Runtime.Runtime.PyDict_GetItemString(threadingDict, "Lock");
if (lockType == IntPtr.Zero)
throw new KeyNotFoundException("class 'Lock' was not found in 'threading'");
var lockInstance = Runtime.Runtime.PyObject_CallObject(lockType, Runtime.Runtime.PyTuple_New(0));
Exceptions.ErrorCheck(lockInstance);
Assert.IsFalse(Runtime.Runtime.PyObject_IsIterable(lockInstance));
Assert.IsFalse(Runtime.Runtime.PyIter_Check(lockInstance));
Runtime.Runtime.Py_Finalize();
}
}
}