xref: /llvm-project/lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp (revision 7d6d218e1219f7df284b83dfb3eaa5e5b70e77e8)
1 //===-- PythonDataObjectsTests.cpp ------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "gtest/gtest.h"
11 
12 #include "lldb/Host/File.h"
13 #include "lldb/Host/FileSystem.h"
14 #include "lldb/Host/HostInfo.h"
15 #include "Plugins/ScriptInterpreter/Python/lldb-python.h"
16 #include "Plugins/ScriptInterpreter/Python/PythonDataObjects.h"
17 #include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h"
18 
19 using namespace lldb_private;
20 
21 class PythonDataObjectsTest : public testing::Test
22 {
23   public:
24     void
25     SetUp() override
26     {
27         HostInfoBase::Initialize();
28         // ScriptInterpreterPython::Initialize() depends on HostInfo being
29         // initializedso it can compute the python directory etc.
30         ScriptInterpreterPython::Initialize();
31 
32         // Although we don't care about concurrency for the purposes of running
33         // this test suite, Python requires the GIL to be locked even for
34         // deallocating memory, which can happen when you call Py_DECREF or
35         // Py_INCREF.  So acquire the GIL for the entire duration of this
36         // test suite.
37         m_gil_state = PyGILState_Ensure();
38     }
39 
40     void
41     TearDown() override
42     {
43         PyGILState_Release(m_gil_state);
44 
45         ScriptInterpreterPython::Terminate();
46     }
47 
48   private:
49     PyGILState_STATE m_gil_state;
50 };
51 
52 TEST_F(PythonDataObjectsTest, TestOwnedReferences)
53 {
54     // After creating a new object, the refcount should be >= 1
55     PyObject *obj = PyLong_FromLong(3);
56     Py_ssize_t original_refcnt = obj->ob_refcnt;
57     EXPECT_LE(1, original_refcnt);
58 
59     // If we take an owned reference, the refcount should be the same
60     PythonObject owned_long(PyRefType::Owned, obj);
61     EXPECT_EQ(original_refcnt, owned_long.get()->ob_refcnt);
62 
63     // Take another reference and verify that the refcount increases by 1
64     PythonObject strong_ref(owned_long);
65     EXPECT_EQ(original_refcnt + 1, strong_ref.get()->ob_refcnt);
66 
67     // If we reset the first one, the refcount should be the original value.
68     owned_long.Reset();
69     EXPECT_EQ(original_refcnt, strong_ref.get()->ob_refcnt);
70 }
71 
72 TEST_F(PythonDataObjectsTest, TestResetting)
73 {
74     PythonDictionary dict(PyInitialValue::Empty);
75 
76     PyObject *new_dict = PyDict_New();
77     dict.Reset(PyRefType::Owned, new_dict);
78     EXPECT_EQ(new_dict, dict.get());
79 
80     dict.Reset(PyRefType::Owned, nullptr);
81     EXPECT_EQ(nullptr, dict.get());
82 
83     dict.Reset(PyRefType::Owned, PyDict_New());
84     EXPECT_NE(nullptr, dict.get());
85     dict.Reset();
86     EXPECT_EQ(nullptr, dict.get());
87 }
88 
89 TEST_F(PythonDataObjectsTest, TestBorrowedReferences)
90 {
91     PythonInteger long_value(PyRefType::Owned, PyLong_FromLong(3));
92     Py_ssize_t original_refcnt = long_value.get()->ob_refcnt;
93     EXPECT_LE(1, original_refcnt);
94 
95     PythonInteger borrowed_long(PyRefType::Borrowed, long_value.get());
96     EXPECT_EQ(original_refcnt + 1, borrowed_long.get()->ob_refcnt);
97 }
98 
99 TEST_F(PythonDataObjectsTest, TestPythonInteger)
100 {
101 // Test that integers behave correctly when wrapped by a PythonInteger.
102 
103 #if PY_MAJOR_VERSION < 3
104     // Verify that `PythonInt` works correctly when given a PyInt object.
105     // Note that PyInt doesn't exist in Python 3.x, so this is only for 2.x
106     PyObject *py_int = PyInt_FromLong(12);
107     EXPECT_TRUE(PythonInteger::Check(py_int));
108     PythonInteger python_int(PyRefType::Owned, py_int);
109 
110     EXPECT_EQ(PyObjectType::Integer, python_int.GetObjectType());
111     EXPECT_EQ(12, python_int.GetInteger());
112 #endif
113 
114     // Verify that `PythonInteger` works correctly when given a PyLong object.
115     PyObject *py_long = PyLong_FromLong(12);
116     EXPECT_TRUE(PythonInteger::Check(py_long));
117     PythonInteger python_long(PyRefType::Owned, py_long);
118     EXPECT_EQ(PyObjectType::Integer, python_long.GetObjectType());
119 
120     // Verify that you can reset the value and that it is reflected properly.
121     python_long.SetInteger(40);
122     EXPECT_EQ(40, python_long.GetInteger());
123 
124     // Test that creating a `PythonInteger` object works correctly with the
125     // int constructor.
126     PythonInteger constructed_int(7);
127     EXPECT_EQ(7, constructed_int.GetInteger());
128 }
129 
130 TEST_F(PythonDataObjectsTest, TestPythonString)
131 {
132     // Test that strings behave correctly when wrapped by a PythonString.
133 
134     static const char *test_string = "PythonDataObjectsTest::TestPythonString1";
135     static const char *test_string2 = "PythonDataObjectsTest::TestPythonString2";
136     static const char *test_string3 = "PythonDataObjectsTest::TestPythonString3";
137 
138 #if PY_MAJOR_VERSION < 3
139     // Verify that `PythonString` works correctly when given a PyString object.
140     // Note that PyString doesn't exist in Python 3.x, so this is only for 2.x
141     PyObject *py_string = PyString_FromString(test_string);
142     EXPECT_TRUE(PythonString::Check(py_string));
143     PythonString python_string(PyRefType::Owned, py_string);
144 
145     EXPECT_EQ(PyObjectType::String, python_string.GetObjectType());
146     EXPECT_STREQ(test_string, python_string.GetString().data());
147 #else
148     // Verify that `PythonString` works correctly when given a PyUnicode object.
149     PyObject *py_unicode = PyUnicode_FromString(test_string);
150     EXPECT_TRUE(PythonString::Check(py_unicode));
151     PythonString python_unicode(PyRefType::Owned, py_unicode);
152     EXPECT_EQ(PyObjectType::String, python_unicode.GetObjectType());
153     EXPECT_STREQ(test_string, python_unicode.GetString().data());
154 #endif
155 
156     // Test that creating a `PythonString` object works correctly with the
157     // string constructor
158     PythonString constructed_string(test_string3);
159     EXPECT_STREQ(test_string3, constructed_string.GetString().str().c_str());
160 }
161 
162 TEST_F(PythonDataObjectsTest, TestPythonStringToStr)
163 {
164     const char *c_str = "PythonDataObjectsTest::TestPythonStringToStr";
165 
166     PythonString str(c_str);
167     EXPECT_STREQ(c_str, str.GetString().str().c_str());
168 
169     PythonString str_str = str.Str();
170     EXPECT_STREQ(c_str, str_str.GetString().str().c_str());
171 }
172 
173 TEST_F(PythonDataObjectsTest, TestPythonIntegerToStr)
174 {
175 }
176 
177 TEST_F(PythonDataObjectsTest, TestPythonIntegerToStructuredInteger)
178 {
179     PythonInteger integer(7);
180     auto int_sp = integer.CreateStructuredInteger();
181     EXPECT_EQ(7, int_sp->GetValue());
182 }
183 
184 TEST_F(PythonDataObjectsTest, TestPythonStringToStructuredString)
185 {
186     static const char *test_string = "PythonDataObjectsTest::TestPythonStringToStructuredString";
187     PythonString constructed_string(test_string);
188     auto string_sp = constructed_string.CreateStructuredString();
189     EXPECT_STREQ(test_string, string_sp->GetStringValue().c_str());
190 }
191 
192 TEST_F(PythonDataObjectsTest, TestPythonListValueEquality)
193 {
194     // Test that a list which is built through the native
195     // Python API behaves correctly when wrapped by a PythonList.
196     static const int list_size = 2;
197     static const long long_value0 = 5;
198     static const char *const string_value1 = "String Index 1";
199 
200     PyObject *py_list = PyList_New(2);
201     EXPECT_TRUE(PythonList::Check(py_list));
202     PythonList list(PyRefType::Owned, py_list);
203 
204     PythonObject list_items[list_size];
205     list_items[0].Reset(PythonInteger(long_value0));
206     list_items[1].Reset(PythonString(string_value1));
207 
208     for (int i = 0; i < list_size; ++i)
209         list.SetItemAtIndex(i, list_items[i]);
210 
211     EXPECT_EQ(list_size, list.GetSize());
212     EXPECT_EQ(PyObjectType::List, list.GetObjectType());
213 
214     // Verify that the values match
215     PythonObject chk_value1 = list.GetItemAtIndex(0);
216     PythonObject chk_value2 = list.GetItemAtIndex(1);
217     EXPECT_TRUE(PythonInteger::Check(chk_value1.get()));
218     EXPECT_TRUE(PythonString::Check(chk_value2.get()));
219 
220     PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get());
221     PythonString chk_str(PyRefType::Borrowed, chk_value2.get());
222 
223     EXPECT_EQ(long_value0, chk_int.GetInteger());
224     EXPECT_STREQ(string_value1, chk_str.GetString().str().c_str());
225 }
226 
227 TEST_F(PythonDataObjectsTest, TestPythonListManipulation)
228 {
229     // Test that manipulation of a PythonList behaves correctly when
230     // wrapped by a PythonDictionary.
231 
232     static const long long_value0 = 5;
233     static const char *const string_value1 = "String Index 1";
234 
235     PythonList list(PyInitialValue::Empty);
236     PythonInteger integer(long_value0);
237     PythonString string(string_value1);
238 
239     list.AppendItem(integer);
240     list.AppendItem(string);
241     EXPECT_EQ(2, list.GetSize());
242 
243     // Verify that the values match
244     PythonObject chk_value1 = list.GetItemAtIndex(0);
245     PythonObject chk_value2 = list.GetItemAtIndex(1);
246     EXPECT_TRUE(PythonInteger::Check(chk_value1.get()));
247     EXPECT_TRUE(PythonString::Check(chk_value2.get()));
248 
249     PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get());
250     PythonString chk_str(PyRefType::Borrowed, chk_value2.get());
251 
252     EXPECT_EQ(long_value0, chk_int.GetInteger());
253     EXPECT_STREQ(string_value1, chk_str.GetString().str().c_str());
254 }
255 
256 TEST_F(PythonDataObjectsTest, TestPythonListToStructuredList)
257 {
258     static const long long_value0 = 5;
259     static const char *const string_value1 = "String Index 1";
260 
261     PythonList list(PyInitialValue::Empty);
262     list.AppendItem(PythonInteger(long_value0));
263     list.AppendItem(PythonString(string_value1));
264 
265     auto array_sp = list.CreateStructuredArray();
266     EXPECT_EQ(StructuredData::Type::eTypeInteger, array_sp->GetItemAtIndex(0)->GetType());
267     EXPECT_EQ(StructuredData::Type::eTypeString, array_sp->GetItemAtIndex(1)->GetType());
268 
269     auto int_sp = array_sp->GetItemAtIndex(0)->GetAsInteger();
270     auto string_sp = array_sp->GetItemAtIndex(1)->GetAsString();
271 
272     EXPECT_EQ(long_value0, int_sp->GetValue());
273     EXPECT_STREQ(string_value1, string_sp->GetValue().c_str());
274 }
275 
276 TEST_F(PythonDataObjectsTest, TestPythonDictionaryValueEquality)
277 {
278     // Test that a dictionary which is built through the native
279     // Python API behaves correctly when wrapped by a PythonDictionary.
280     static const int dict_entries = 2;
281     const char *key_0 = "Key 0";
282     int key_1 = 1;
283     const int value_0 = 0;
284     const char *value_1 = "Value 1";
285 
286     PythonObject py_keys[dict_entries];
287     PythonObject py_values[dict_entries];
288 
289     py_keys[0].Reset(PythonString(key_0));
290     py_keys[1].Reset(PythonInteger(key_1));
291     py_values[0].Reset(PythonInteger(value_0));
292     py_values[1].Reset(PythonString(value_1));
293 
294     PyObject *py_dict = PyDict_New();
295     EXPECT_TRUE(PythonDictionary::Check(py_dict));
296     PythonDictionary dict(PyRefType::Owned, py_dict);
297 
298     for (int i = 0; i < dict_entries; ++i)
299         PyDict_SetItem(py_dict, py_keys[i].get(), py_values[i].get());
300     EXPECT_EQ(dict.GetSize(), dict_entries);
301     EXPECT_EQ(PyObjectType::Dictionary, dict.GetObjectType());
302 
303     // Verify that the values match
304     PythonObject chk_value1 = dict.GetItemForKey(py_keys[0]);
305     PythonObject chk_value2 = dict.GetItemForKey(py_keys[1]);
306     EXPECT_TRUE(PythonInteger::Check(chk_value1.get()));
307     EXPECT_TRUE(PythonString::Check(chk_value2.get()));
308 
309     PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get());
310     PythonString chk_str(PyRefType::Borrowed, chk_value2.get());
311 
312     EXPECT_EQ(value_0, chk_int.GetInteger());
313     EXPECT_STREQ(value_1, chk_str.GetString().str().c_str());
314 }
315 
316 TEST_F(PythonDataObjectsTest, TestPythonDictionaryManipulation)
317 {
318     // Test that manipulation of a dictionary behaves correctly when wrapped
319     // by a PythonDictionary.
320     static const int dict_entries = 2;
321 
322     const char *const key_0 = "Key 0";
323     const char *const key_1 = "Key 1";
324     const long value_0 = 1;
325     const char *const value_1 = "Value 1";
326 
327     PythonString keys[dict_entries];
328     PythonObject values[dict_entries];
329 
330     keys[0].Reset(PythonString(key_0));
331     keys[1].Reset(PythonString(key_1));
332     values[0].Reset(PythonInteger(value_0));
333     values[1].Reset(PythonString(value_1));
334 
335     PythonDictionary dict(PyInitialValue::Empty);
336     for (int i = 0; i < 2; ++i)
337         dict.SetItemForKey(keys[i], values[i]);
338 
339     EXPECT_EQ(dict_entries, dict.GetSize());
340 
341     // Verify that the keys and values match
342     PythonObject chk_value1 = dict.GetItemForKey(keys[0]);
343     PythonObject chk_value2 = dict.GetItemForKey(keys[1]);
344     EXPECT_TRUE(PythonInteger::Check(chk_value1.get()));
345     EXPECT_TRUE(PythonString::Check(chk_value2.get()));
346 
347     PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get());
348     PythonString chk_str(PyRefType::Borrowed, chk_value2.get());
349 
350     EXPECT_EQ(value_0, chk_int.GetInteger());
351     EXPECT_STREQ(value_1, chk_str.GetString().str().c_str());
352 }
353 
354 TEST_F(PythonDataObjectsTest, TestPythonDictionaryToStructuredDictionary)
355 {
356     static const char *const string_key0 = "String Key 0";
357     static const char *const string_key1 = "String Key 1";
358 
359     static const char *const string_value0 = "String Value 0";
360     static const long int_value1 = 7;
361 
362     PythonDictionary dict(PyInitialValue::Empty);
363     dict.SetItemForKey(PythonString(string_key0), PythonString(string_value0));
364     dict.SetItemForKey(PythonString(string_key1), PythonInteger(int_value1));
365 
366     auto dict_sp = dict.CreateStructuredDictionary();
367     EXPECT_EQ(2, dict_sp->GetSize());
368 
369     EXPECT_TRUE(dict_sp->HasKey(string_key0));
370     EXPECT_TRUE(dict_sp->HasKey(string_key1));
371 
372     auto string_sp = dict_sp->GetValueForKey(string_key0)->GetAsString();
373     auto int_sp = dict_sp->GetValueForKey(string_key1)->GetAsInteger();
374 
375     EXPECT_STREQ(string_value0, string_sp->GetValue().c_str());
376     EXPECT_EQ(int_value1, int_sp->GetValue());
377 }
378 
379 TEST_F(PythonDataObjectsTest, TestPythonFile)
380 {
381     File file(FileSystem::DEV_NULL, File::eOpenOptionRead);
382     PythonFile py_file(file, "r");
383     EXPECT_TRUE(PythonFile::Check(py_file.get()));
384 }
385 
386 TEST_F(PythonDataObjectsTest, TestObjectAttributes)
387 {
388     PythonInteger py_int(42);
389     EXPECT_TRUE(py_int.HasAttribute("numerator"));
390     EXPECT_FALSE(py_int.HasAttribute("this_should_not_exist"));
391 
392     PythonInteger numerator_attr = py_int.GetAttributeValue("numerator").AsType<PythonInteger>();
393     EXPECT_TRUE(numerator_attr.IsAllocated());
394     EXPECT_EQ(42, numerator_attr.GetInteger());
395 }