1 //===-- PythonDataObjectsTests.cpp ----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Plugins/ScriptInterpreter/Python/lldb-python.h"
10 #include "gtest/gtest.h"
11 
12 #include "Plugins/ScriptInterpreter/Python/PythonDataObjects.h"
13 #include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h"
14 #include "lldb/Host/File.h"
15 #include "lldb/Host/FileSystem.h"
16 #include "lldb/Host/HostInfo.h"
17 #include "lldb/lldb-enumerations.h"
18 #include "llvm/Testing/Support/Error.h"
19 
20 #include "PythonTestSuite.h"
21 
22 using namespace lldb_private;
23 using namespace lldb_private::python;
24 using llvm::Error;
25 using llvm::Expected;
26 
27 class PythonDataObjectsTest : public PythonTestSuite {
28 public:
SetUp()29   void SetUp() override {
30     PythonTestSuite::SetUp();
31 
32     m_sys_module = unwrapIgnoringErrors(PythonModule::Import("sys"));
33     m_main_module = PythonModule::MainModule();
34     m_builtins_module = PythonModule::BuiltinsModule();
35   }
36 
TearDown()37   void TearDown() override {
38     m_sys_module.Reset();
39     m_main_module.Reset();
40     m_builtins_module.Reset();
41 
42     PythonTestSuite::TearDown();
43   }
44 
45 protected:
46   PythonModule m_sys_module;
47   PythonModule m_main_module;
48   PythonModule m_builtins_module;
49 };
50 
TEST_F(PythonDataObjectsTest,TestOwnedReferences)51 TEST_F(PythonDataObjectsTest, TestOwnedReferences) {
52   // After creating a new object, the refcount should be >= 1
53   PyObject *obj = PyLong_FromLong(3);
54   Py_ssize_t original_refcnt = obj->ob_refcnt;
55   EXPECT_LE(1, original_refcnt);
56 
57   // If we take an owned reference, the refcount should be the same
58   PythonObject owned_long(PyRefType::Owned, obj);
59   EXPECT_EQ(original_refcnt, owned_long.get()->ob_refcnt);
60 
61   // Take another reference and verify that the refcount increases by 1
62   PythonObject strong_ref(owned_long);
63   EXPECT_EQ(original_refcnt + 1, strong_ref.get()->ob_refcnt);
64 
65   // If we reset the first one, the refcount should be the original value.
66   owned_long.Reset();
67   EXPECT_EQ(original_refcnt, strong_ref.get()->ob_refcnt);
68 }
69 
TEST_F(PythonDataObjectsTest,TestResetting)70 TEST_F(PythonDataObjectsTest, TestResetting) {
71   PythonDictionary dict(PyInitialValue::Empty);
72 
73   PyObject *new_dict = PyDict_New();
74   dict = Take<PythonDictionary>(new_dict);
75   EXPECT_EQ(new_dict, dict.get());
76 
77   dict = Take<PythonDictionary>(PyDict_New());
78   EXPECT_NE(nullptr, dict.get());
79   dict.Reset();
80   EXPECT_EQ(nullptr, dict.get());
81 }
82 
TEST_F(PythonDataObjectsTest,TestBorrowedReferences)83 TEST_F(PythonDataObjectsTest, TestBorrowedReferences) {
84   PythonInteger long_value(PyRefType::Owned, PyLong_FromLong(3));
85   Py_ssize_t original_refcnt = long_value.get()->ob_refcnt;
86   EXPECT_LE(1, original_refcnt);
87 
88   PythonInteger borrowed_long(PyRefType::Borrowed, long_value.get());
89   EXPECT_EQ(original_refcnt + 1, borrowed_long.get()->ob_refcnt);
90 }
91 
TEST_F(PythonDataObjectsTest,TestGlobalNameResolutionNoDot)92 TEST_F(PythonDataObjectsTest, TestGlobalNameResolutionNoDot) {
93   PythonObject sys_module = m_main_module.ResolveName("sys");
94   EXPECT_EQ(m_sys_module.get(), sys_module.get());
95   EXPECT_TRUE(sys_module.IsAllocated());
96   EXPECT_TRUE(PythonModule::Check(sys_module.get()));
97 }
98 
TEST_F(PythonDataObjectsTest,TestModuleNameResolutionNoDot)99 TEST_F(PythonDataObjectsTest, TestModuleNameResolutionNoDot) {
100   PythonObject sys_path = m_sys_module.ResolveName("path");
101   PythonObject sys_version_info = m_sys_module.ResolveName("version_info");
102   EXPECT_TRUE(sys_path.IsAllocated());
103   EXPECT_TRUE(sys_version_info.IsAllocated());
104 
105   EXPECT_TRUE(PythonList::Check(sys_path.get()));
106 }
107 
TEST_F(PythonDataObjectsTest,TestTypeNameResolutionNoDot)108 TEST_F(PythonDataObjectsTest, TestTypeNameResolutionNoDot) {
109   PythonObject sys_version_info = m_sys_module.ResolveName("version_info");
110 
111   PythonObject version_info_type(PyRefType::Owned,
112                                  PyObject_Type(sys_version_info.get()));
113   EXPECT_TRUE(version_info_type.IsAllocated());
114   PythonObject major_version_field = version_info_type.ResolveName("major");
115   EXPECT_TRUE(major_version_field.IsAllocated());
116 }
117 
TEST_F(PythonDataObjectsTest,TestInstanceNameResolutionNoDot)118 TEST_F(PythonDataObjectsTest, TestInstanceNameResolutionNoDot) {
119   PythonObject sys_version_info = m_sys_module.ResolveName("version_info");
120   PythonObject major_version_field = sys_version_info.ResolveName("major");
121   PythonObject minor_version_field = sys_version_info.ResolveName("minor");
122 
123   EXPECT_TRUE(major_version_field.IsAllocated());
124   EXPECT_TRUE(minor_version_field.IsAllocated());
125 
126   auto major_version_value = As<long long>(major_version_field);
127   auto minor_version_value = As<long long>(minor_version_field);
128 
129   EXPECT_THAT_EXPECTED(major_version_value, llvm::HasValue(PY_MAJOR_VERSION));
130   EXPECT_THAT_EXPECTED(minor_version_value, llvm::HasValue(PY_MINOR_VERSION));
131 }
132 
TEST_F(PythonDataObjectsTest,TestGlobalNameResolutionWithDot)133 TEST_F(PythonDataObjectsTest, TestGlobalNameResolutionWithDot) {
134   PythonObject sys_path = m_main_module.ResolveName("sys.path");
135   EXPECT_TRUE(sys_path.IsAllocated());
136   EXPECT_TRUE(PythonList::Check(sys_path.get()));
137 
138   auto version_major =
139       As<long long>(m_main_module.ResolveName("sys.version_info.major"));
140 
141   auto version_minor =
142       As<long long>(m_main_module.ResolveName("sys.version_info.minor"));
143 
144   EXPECT_THAT_EXPECTED(version_major, llvm::HasValue(PY_MAJOR_VERSION));
145   EXPECT_THAT_EXPECTED(version_minor, llvm::HasValue(PY_MINOR_VERSION));
146 }
147 
TEST_F(PythonDataObjectsTest,TestDictionaryResolutionWithDot)148 TEST_F(PythonDataObjectsTest, TestDictionaryResolutionWithDot) {
149   // Make up a custom dictionary with "sys" pointing to the `sys` module.
150   PythonDictionary dict(PyInitialValue::Empty);
151   dict.SetItemForKey(PythonString("sys"), m_sys_module);
152 
153   // Now use that dictionary to resolve `sys.version_info.major`
154   auto version_major = As<long long>(
155       PythonObject::ResolveNameWithDictionary("sys.version_info.major", dict));
156 
157   auto version_minor = As<long long>(
158       PythonObject::ResolveNameWithDictionary("sys.version_info.minor", dict));
159 
160   EXPECT_THAT_EXPECTED(version_major, llvm::HasValue(PY_MAJOR_VERSION));
161   EXPECT_THAT_EXPECTED(version_minor, llvm::HasValue(PY_MINOR_VERSION));
162 }
163 
TEST_F(PythonDataObjectsTest,TestPythonInteger)164 TEST_F(PythonDataObjectsTest, TestPythonInteger) {
165   // Test that integers behave correctly when wrapped by a PythonInteger.
166 
167 #if PY_MAJOR_VERSION < 3
168   // Verify that `PythonInt` works correctly when given a PyInt object.
169   // Note that PyInt doesn't exist in Python 3.x, so this is only for 2.x
170   PyObject *py_int = PyInt_FromLong(12);
171   EXPECT_TRUE(PythonInteger::Check(py_int));
172   PythonInteger python_int(PyRefType::Owned, py_int);
173 
174   EXPECT_EQ(PyObjectType::Integer, python_int.GetObjectType());
175   auto python_int_value = As<long long>(python_int);
176   EXPECT_THAT_EXPECTED(python_int_value, llvm::HasValue(12));
177 #endif
178 
179   // Verify that `PythonInteger` works correctly when given a PyLong object.
180   PyObject *py_long = PyLong_FromLong(12);
181   EXPECT_TRUE(PythonInteger::Check(py_long));
182   PythonInteger python_long(PyRefType::Owned, py_long);
183   EXPECT_EQ(PyObjectType::Integer, python_long.GetObjectType());
184 
185   // Verify that you can reset the value and that it is reflected properly.
186   python_long.SetInteger(40);
187   auto e = As<long long>(python_long);
188   EXPECT_THAT_EXPECTED(e, llvm::HasValue(40));
189 
190   // Test that creating a `PythonInteger` object works correctly with the
191   // int constructor.
192   PythonInteger constructed_int(7);
193   auto value = As<long long>(constructed_int);
194   EXPECT_THAT_EXPECTED(value, llvm::HasValue(7));
195 }
196 
TEST_F(PythonDataObjectsTest,TestPythonBoolean)197 TEST_F(PythonDataObjectsTest, TestPythonBoolean) {
198   // Test PythonBoolean constructed from Py_True
199   EXPECT_TRUE(PythonBoolean::Check(Py_True));
200   PythonBoolean python_true(PyRefType::Owned, Py_True);
201   EXPECT_EQ(PyObjectType::Boolean, python_true.GetObjectType());
202 
203   // Test PythonBoolean constructed from Py_False
204   EXPECT_TRUE(PythonBoolean::Check(Py_False));
205   PythonBoolean python_false(PyRefType::Owned, Py_False);
206   EXPECT_EQ(PyObjectType::Boolean, python_false.GetObjectType());
207 
208   auto test_from_long = [](long value) {
209     PyObject *py_bool = PyBool_FromLong(value);
210     EXPECT_TRUE(PythonBoolean::Check(py_bool));
211     PythonBoolean python_boolean(PyRefType::Owned, py_bool);
212     EXPECT_EQ(PyObjectType::Boolean, python_boolean.GetObjectType());
213     EXPECT_EQ(bool(value), python_boolean.GetValue());
214   };
215 
216   // Test PythonBoolean constructed from long integer values.
217   test_from_long(0); // Test 'false' value.
218   test_from_long(1); // Test 'true' value.
219   test_from_long(~0); // Any value != 0 is 'true'.
220 }
221 
TEST_F(PythonDataObjectsTest,TestPythonBytes)222 TEST_F(PythonDataObjectsTest, TestPythonBytes) {
223   static const char *test_bytes = "PythonDataObjectsTest::TestPythonBytes";
224   PyObject *py_bytes = PyBytes_FromString(test_bytes);
225   EXPECT_TRUE(PythonBytes::Check(py_bytes));
226   PythonBytes python_bytes(PyRefType::Owned, py_bytes);
227 
228 #if PY_MAJOR_VERSION < 3
229   EXPECT_TRUE(PythonString::Check(py_bytes));
230   EXPECT_EQ(PyObjectType::String, python_bytes.GetObjectType());
231 #else
232   EXPECT_FALSE(PythonString::Check(py_bytes));
233   EXPECT_EQ(PyObjectType::Bytes, python_bytes.GetObjectType());
234 #endif
235 
236   llvm::ArrayRef<uint8_t> bytes = python_bytes.GetBytes();
237   EXPECT_EQ(bytes.size(), strlen(test_bytes));
238   EXPECT_EQ(0, ::memcmp(bytes.data(), test_bytes, bytes.size()));
239 }
240 
TEST_F(PythonDataObjectsTest,TestPythonByteArray)241 TEST_F(PythonDataObjectsTest, TestPythonByteArray) {
242   static const char *test_bytes = "PythonDataObjectsTest::TestPythonByteArray";
243   llvm::StringRef orig_bytes(test_bytes);
244   PyObject *py_bytes =
245       PyByteArray_FromStringAndSize(test_bytes, orig_bytes.size());
246   EXPECT_TRUE(PythonByteArray::Check(py_bytes));
247   PythonByteArray python_bytes(PyRefType::Owned, py_bytes);
248   EXPECT_EQ(PyObjectType::ByteArray, python_bytes.GetObjectType());
249 
250   llvm::ArrayRef<uint8_t> after_bytes = python_bytes.GetBytes();
251   EXPECT_EQ(after_bytes.size(), orig_bytes.size());
252   EXPECT_EQ(0, ::memcmp(orig_bytes.data(), test_bytes, orig_bytes.size()));
253 }
254 
TEST_F(PythonDataObjectsTest,TestPythonString)255 TEST_F(PythonDataObjectsTest, TestPythonString) {
256   // Test that strings behave correctly when wrapped by a PythonString.
257 
258   static const char *test_string = "PythonDataObjectsTest::TestPythonString1";
259   static const char *test_string2 = "PythonDataObjectsTest::TestPythonString2";
260 
261 #if PY_MAJOR_VERSION < 3
262   // Verify that `PythonString` works correctly when given a PyString object.
263   // Note that PyString doesn't exist in Python 3.x, so this is only for 2.x
264   PyObject *py_string = PyString_FromString(test_string);
265   EXPECT_TRUE(PythonString::Check(py_string));
266   PythonString python_string(PyRefType::Owned, py_string);
267 
268   EXPECT_EQ(PyObjectType::String, python_string.GetObjectType());
269   EXPECT_STREQ(test_string, python_string.GetString().data());
270 #else
271   // Verify that `PythonString` works correctly when given a PyUnicode object.
272   PyObject *py_unicode = PyUnicode_FromString(test_string);
273   EXPECT_TRUE(PythonString::Check(py_unicode));
274   PythonString python_unicode(PyRefType::Owned, py_unicode);
275   EXPECT_EQ(PyObjectType::String, python_unicode.GetObjectType());
276   EXPECT_STREQ(test_string, python_unicode.GetString().data());
277 #endif
278 
279   // Test that creating a `PythonString` object works correctly with the
280   // string constructor
281   PythonString constructed_string(test_string2);
282   EXPECT_EQ(test_string2, constructed_string.GetString());
283 }
284 
TEST_F(PythonDataObjectsTest,TestPythonStringToStr)285 TEST_F(PythonDataObjectsTest, TestPythonStringToStr) {
286   const char *GetString = "PythonDataObjectsTest::TestPythonStringToStr";
287 
288   PythonString str(GetString);
289   EXPECT_EQ(GetString, str.GetString());
290 
291   PythonString str_str = str.Str();
292   EXPECT_EQ(GetString, str_str.GetString());
293 }
294 
TEST_F(PythonDataObjectsTest,TestPythonIntegerToStr)295 TEST_F(PythonDataObjectsTest, TestPythonIntegerToStr) {}
296 
TEST_F(PythonDataObjectsTest,TestPythonIntegerToStructuredInteger)297 TEST_F(PythonDataObjectsTest, TestPythonIntegerToStructuredInteger) {
298   PythonInteger integer(7);
299   auto int_sp = integer.CreateStructuredInteger();
300   EXPECT_EQ(7U, int_sp->GetValue());
301 }
302 
TEST_F(PythonDataObjectsTest,TestPythonStringToStructuredString)303 TEST_F(PythonDataObjectsTest, TestPythonStringToStructuredString) {
304   static const char *test_string =
305       "PythonDataObjectsTest::TestPythonStringToStructuredString";
306   PythonString constructed_string(test_string);
307   auto string_sp = constructed_string.CreateStructuredString();
308   EXPECT_EQ(test_string, string_sp->GetStringValue());
309 }
310 
TEST_F(PythonDataObjectsTest,TestPythonListValueEquality)311 TEST_F(PythonDataObjectsTest, TestPythonListValueEquality) {
312   // Test that a list which is built through the native
313   // Python API behaves correctly when wrapped by a PythonList.
314   static const unsigned list_size = 2;
315   static const long long_value0 = 5;
316   static const char *const string_value1 = "String Index 1";
317 
318   PyObject *py_list = PyList_New(2);
319   EXPECT_TRUE(PythonList::Check(py_list));
320   PythonList list(PyRefType::Owned, py_list);
321 
322   PythonObject list_items[list_size];
323   list_items[0] = PythonInteger(long_value0);
324   list_items[1] = PythonString(string_value1);
325 
326   for (unsigned i = 0; i < list_size; ++i)
327     list.SetItemAtIndex(i, list_items[i]);
328 
329   EXPECT_EQ(list_size, list.GetSize());
330   EXPECT_EQ(PyObjectType::List, list.GetObjectType());
331 
332   // Verify that the values match
333   PythonObject chk_value1 = list.GetItemAtIndex(0);
334   PythonObject chk_value2 = list.GetItemAtIndex(1);
335   EXPECT_TRUE(PythonInteger::Check(chk_value1.get()));
336   EXPECT_TRUE(PythonString::Check(chk_value2.get()));
337 
338   PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get());
339   PythonString chk_str(PyRefType::Borrowed, chk_value2.get());
340 
341   auto chkint = As<long long>(chk_value1);
342   ASSERT_THAT_EXPECTED(chkint, llvm::HasValue(long_value0));
343   EXPECT_EQ(string_value1, chk_str.GetString());
344 }
345 
TEST_F(PythonDataObjectsTest,TestPythonListManipulation)346 TEST_F(PythonDataObjectsTest, TestPythonListManipulation) {
347   // Test that manipulation of a PythonList behaves correctly when
348   // wrapped by a PythonDictionary.
349 
350   static const long long_value0 = 5;
351   static const char *const string_value1 = "String Index 1";
352 
353   PythonList list(PyInitialValue::Empty);
354   PythonInteger integer(long_value0);
355   PythonString string(string_value1);
356 
357   list.AppendItem(integer);
358   list.AppendItem(string);
359   EXPECT_EQ(2U, list.GetSize());
360 
361   // Verify that the values match
362   PythonObject chk_value1 = list.GetItemAtIndex(0);
363   PythonObject chk_value2 = list.GetItemAtIndex(1);
364   EXPECT_TRUE(PythonInteger::Check(chk_value1.get()));
365   EXPECT_TRUE(PythonString::Check(chk_value2.get()));
366 
367   PythonInteger chk_int(PyRefType::Borrowed, chk_value1.get());
368   PythonString chk_str(PyRefType::Borrowed, chk_value2.get());
369 
370   auto e = As<long long>(chk_int);
371   EXPECT_THAT_EXPECTED(e, llvm::HasValue(long_value0));
372   EXPECT_EQ(string_value1, chk_str.GetString());
373 }
374 
TEST_F(PythonDataObjectsTest,TestPythonListToStructuredList)375 TEST_F(PythonDataObjectsTest, TestPythonListToStructuredList) {
376   static const long long_value0 = 5;
377   static const char *const string_value1 = "String Index 1";
378 
379   PythonList list(PyInitialValue::Empty);
380   list.AppendItem(PythonInteger(long_value0));
381   list.AppendItem(PythonString(string_value1));
382 
383   auto array_sp = list.CreateStructuredArray();
384   EXPECT_EQ(lldb::eStructuredDataTypeInteger,
385             array_sp->GetItemAtIndex(0)->GetType());
386   EXPECT_EQ(lldb::eStructuredDataTypeString,
387             array_sp->GetItemAtIndex(1)->GetType());
388 
389   auto int_sp = array_sp->GetItemAtIndex(0)->GetAsInteger();
390   auto string_sp = array_sp->GetItemAtIndex(1)->GetAsString();
391 
392   EXPECT_EQ(long_value0, long(int_sp->GetValue()));
393   EXPECT_EQ(string_value1, string_sp->GetValue());
394 }
395 
TEST_F(PythonDataObjectsTest,TestPythonTupleSize)396 TEST_F(PythonDataObjectsTest, TestPythonTupleSize) {
397   PythonTuple tuple(PyInitialValue::Empty);
398   EXPECT_EQ(0U, tuple.GetSize());
399 
400   tuple = PythonTuple(3);
401   EXPECT_EQ(3U, tuple.GetSize());
402 }
403 
TEST_F(PythonDataObjectsTest,TestPythonTupleValues)404 TEST_F(PythonDataObjectsTest, TestPythonTupleValues) {
405   PythonTuple tuple(3);
406 
407   PythonInteger int_value(1);
408   PythonString string_value("Test");
409   PythonObject none_value(PyRefType::Borrowed, Py_None);
410 
411   tuple.SetItemAtIndex(0, int_value);
412   tuple.SetItemAtIndex(1, string_value);
413   tuple.SetItemAtIndex(2, none_value);
414 
415   EXPECT_EQ(tuple.GetItemAtIndex(0).get(), int_value.get());
416   EXPECT_EQ(tuple.GetItemAtIndex(1).get(), string_value.get());
417   EXPECT_EQ(tuple.GetItemAtIndex(2).get(), none_value.get());
418 }
419 
TEST_F(PythonDataObjectsTest,TestPythonTupleInitializerList)420 TEST_F(PythonDataObjectsTest, TestPythonTupleInitializerList) {
421   PythonInteger int_value(1);
422   PythonString string_value("Test");
423   PythonObject none_value(PyRefType::Borrowed, Py_None);
424   PythonTuple tuple{int_value, string_value, none_value};
425   EXPECT_EQ(3U, tuple.GetSize());
426 
427   EXPECT_EQ(tuple.GetItemAtIndex(0).get(), int_value.get());
428   EXPECT_EQ(tuple.GetItemAtIndex(1).get(), string_value.get());
429   EXPECT_EQ(tuple.GetItemAtIndex(2).get(), none_value.get());
430 }
431 
TEST_F(PythonDataObjectsTest,TestPythonTupleInitializerList2)432 TEST_F(PythonDataObjectsTest, TestPythonTupleInitializerList2) {
433   PythonInteger int_value(1);
434   PythonString string_value("Test");
435   PythonObject none_value(PyRefType::Borrowed, Py_None);
436 
437   PythonTuple tuple{int_value.get(), string_value.get(), none_value.get()};
438   EXPECT_EQ(3U, tuple.GetSize());
439 
440   EXPECT_EQ(tuple.GetItemAtIndex(0).get(), int_value.get());
441   EXPECT_EQ(tuple.GetItemAtIndex(1).get(), string_value.get());
442   EXPECT_EQ(tuple.GetItemAtIndex(2).get(), none_value.get());
443 }
444 
TEST_F(PythonDataObjectsTest,TestPythonTupleToStructuredList)445 TEST_F(PythonDataObjectsTest, TestPythonTupleToStructuredList) {
446   PythonInteger int_value(1);
447   PythonString string_value("Test");
448 
449   PythonTuple tuple{int_value.get(), string_value.get()};
450 
451   auto array_sp = tuple.CreateStructuredArray();
452   EXPECT_EQ(tuple.GetSize(), array_sp->GetSize());
453   EXPECT_EQ(lldb::eStructuredDataTypeInteger,
454             array_sp->GetItemAtIndex(0)->GetType());
455   EXPECT_EQ(lldb::eStructuredDataTypeString,
456             array_sp->GetItemAtIndex(1)->GetType());
457 }
458 
TEST_F(PythonDataObjectsTest,TestPythonDictionaryValueEquality)459 TEST_F(PythonDataObjectsTest, TestPythonDictionaryValueEquality) {
460   // Test that a dictionary which is built through the native
461   // Python API behaves correctly when wrapped by a PythonDictionary.
462   static const unsigned dict_entries = 2;
463   const char *key_0 = "Key 0";
464   int key_1 = 1;
465   const int value_0 = 0;
466   const char *value_1 = "Value 1";
467 
468   PythonObject py_keys[dict_entries];
469   PythonObject py_values[dict_entries];
470 
471   py_keys[0] = PythonString(key_0);
472   py_keys[1] = PythonInteger(key_1);
473   py_values[0] = PythonInteger(value_0);
474   py_values[1] = PythonString(value_1);
475 
476   PyObject *py_dict = PyDict_New();
477   EXPECT_TRUE(PythonDictionary::Check(py_dict));
478   PythonDictionary dict(PyRefType::Owned, py_dict);
479 
480   for (unsigned i = 0; i < dict_entries; ++i)
481     PyDict_SetItem(py_dict, py_keys[i].get(), py_values[i].get());
482   EXPECT_EQ(dict.GetSize(), dict_entries);
483   EXPECT_EQ(PyObjectType::Dictionary, dict.GetObjectType());
484 
485   // Verify that the values match
486   PythonObject chk_value1 = dict.GetItemForKey(py_keys[0]);
487   PythonObject chk_value2 = dict.GetItemForKey(py_keys[1]);
488   EXPECT_TRUE(PythonInteger::Check(chk_value1.get()));
489   EXPECT_TRUE(PythonString::Check(chk_value2.get()));
490 
491   PythonString chk_str(PyRefType::Borrowed, chk_value2.get());
492   auto chkint = As<long long>(chk_value1);
493 
494   EXPECT_THAT_EXPECTED(chkint, llvm::HasValue(value_0));
495   EXPECT_EQ(value_1, chk_str.GetString());
496 }
497 
TEST_F(PythonDataObjectsTest,TestPythonDictionaryManipulation)498 TEST_F(PythonDataObjectsTest, TestPythonDictionaryManipulation) {
499   // Test that manipulation of a dictionary behaves correctly when wrapped
500   // by a PythonDictionary.
501   static const unsigned dict_entries = 2;
502 
503   const char *const key_0 = "Key 0";
504   const char *const key_1 = "Key 1";
505   const long value_0 = 1;
506   const char *const value_1 = "Value 1";
507 
508   PythonString keys[dict_entries];
509   PythonObject values[dict_entries];
510 
511   keys[0] = PythonString(key_0);
512   keys[1] = PythonString(key_1);
513   values[0] = PythonInteger(value_0);
514   values[1] = PythonString(value_1);
515 
516   PythonDictionary dict(PyInitialValue::Empty);
517   for (int i = 0; i < 2; ++i)
518     dict.SetItemForKey(keys[i], values[i]);
519 
520   EXPECT_EQ(dict_entries, dict.GetSize());
521 
522   // Verify that the keys and values match
523   PythonObject chk_value1 = dict.GetItemForKey(keys[0]);
524   PythonObject chk_value2 = dict.GetItemForKey(keys[1]);
525   EXPECT_TRUE(PythonInteger::Check(chk_value1.get()));
526   EXPECT_TRUE(PythonString::Check(chk_value2.get()));
527 
528   auto chkint = As<long long>(chk_value1);
529   PythonString chk_str(PyRefType::Borrowed, chk_value2.get());
530 
531   EXPECT_THAT_EXPECTED(chkint, llvm::HasValue(value_0));
532   EXPECT_EQ(value_1, chk_str.GetString());
533 }
534 
TEST_F(PythonDataObjectsTest,TestPythonDictionaryToStructuredDictionary)535 TEST_F(PythonDataObjectsTest, TestPythonDictionaryToStructuredDictionary) {
536   static const char *const string_key0 = "String Key 0";
537   static const char *const string_key1 = "String Key 1";
538 
539   static const char *const string_value0 = "String Value 0";
540   static const long int_value1 = 7;
541 
542   PythonDictionary dict(PyInitialValue::Empty);
543   dict.SetItemForKey(PythonString(string_key0), PythonString(string_value0));
544   dict.SetItemForKey(PythonString(string_key1), PythonInteger(int_value1));
545 
546   auto dict_sp = dict.CreateStructuredDictionary();
547   EXPECT_EQ(2U, dict_sp->GetSize());
548 
549   EXPECT_TRUE(dict_sp->HasKey(string_key0));
550   EXPECT_TRUE(dict_sp->HasKey(string_key1));
551 
552   auto string_sp = dict_sp->GetValueForKey(string_key0)->GetAsString();
553   auto int_sp = dict_sp->GetValueForKey(string_key1)->GetAsInteger();
554 
555   EXPECT_EQ(string_value0, string_sp->GetValue());
556   EXPECT_EQ(int_value1, long(int_sp->GetValue()));
557 }
558 
TEST_F(PythonDataObjectsTest,TestPythonCallableCheck)559 TEST_F(PythonDataObjectsTest, TestPythonCallableCheck) {
560   PythonObject sys_exc_info = m_sys_module.ResolveName("exc_info");
561   PythonObject none(PyRefType::Borrowed, Py_None);
562 
563   EXPECT_TRUE(PythonCallable::Check(sys_exc_info.get()));
564   EXPECT_FALSE(PythonCallable::Check(none.get()));
565 }
566 
TEST_F(PythonDataObjectsTest,TestPythonCallableInvoke)567 TEST_F(PythonDataObjectsTest, TestPythonCallableInvoke) {
568   auto list = m_builtins_module.ResolveName("list").AsType<PythonCallable>();
569   PythonInteger one(1);
570   PythonString two("two");
571   PythonTuple three = {one, two};
572 
573   PythonTuple tuple_to_convert = {one, two, three};
574   PythonObject result = list({tuple_to_convert});
575 
576   EXPECT_TRUE(PythonList::Check(result.get()));
577   auto list_result = result.AsType<PythonList>();
578   EXPECT_EQ(3U, list_result.GetSize());
579   EXPECT_EQ(one.get(), list_result.GetItemAtIndex(0).get());
580   EXPECT_EQ(two.get(), list_result.GetItemAtIndex(1).get());
581   EXPECT_EQ(three.get(), list_result.GetItemAtIndex(2).get());
582 }
583 
TEST_F(PythonDataObjectsTest,TestPythonFile)584 TEST_F(PythonDataObjectsTest, TestPythonFile) {
585   auto file = FileSystem::Instance().Open(FileSpec(FileSystem::DEV_NULL),
586                                           File::eOpenOptionRead);
587   ASSERT_THAT_EXPECTED(file, llvm::Succeeded());
588   auto py_file = PythonFile::FromFile(*file.get(), "r");
589   ASSERT_THAT_EXPECTED(py_file, llvm::Succeeded());
590   EXPECT_TRUE(PythonFile::Check(py_file.get().get()));
591 }
592 
TEST_F(PythonDataObjectsTest,TestObjectAttributes)593 TEST_F(PythonDataObjectsTest, TestObjectAttributes) {
594   PythonInteger py_int(42);
595   EXPECT_TRUE(py_int.HasAttribute("numerator"));
596   EXPECT_FALSE(py_int.HasAttribute("this_should_not_exist"));
597 
598   auto numerator_attr = As<long long>(py_int.GetAttributeValue("numerator"));
599 
600   EXPECT_THAT_EXPECTED(numerator_attr, llvm::HasValue(42));
601 }
602 
TEST_F(PythonDataObjectsTest,TestExtractingUInt64ThroughStructuredData)603 TEST_F(PythonDataObjectsTest, TestExtractingUInt64ThroughStructuredData) {
604   // Make up a custom dictionary with "sys" pointing to the `sys` module.
605   const char *key_name = "addr";
606   const uint64_t value = 0xf000000000000000ull;
607   PythonDictionary python_dict(PyInitialValue::Empty);
608   PythonInteger python_ull_value(PyRefType::Owned,
609                                  PyLong_FromUnsignedLongLong(value));
610   python_dict.SetItemForKey(PythonString(key_name), python_ull_value);
611   StructuredData::ObjectSP structured_data_sp =
612       python_dict.CreateStructuredObject();
613   EXPECT_TRUE((bool)structured_data_sp);
614   if (structured_data_sp) {
615     StructuredData::Dictionary *structured_dict_ptr =
616         structured_data_sp->GetAsDictionary();
617     EXPECT_TRUE(structured_dict_ptr != nullptr);
618     if (structured_dict_ptr) {
619       StructuredData::ObjectSP structured_addr_value_sp =
620           structured_dict_ptr->GetValueForKey(key_name);
621       EXPECT_TRUE((bool)structured_addr_value_sp);
622       const uint64_t extracted_value =
623           structured_addr_value_sp->GetIntegerValue(123);
624       EXPECT_TRUE(extracted_value == value);
625     }
626   }
627 }
628 
TEST_F(PythonDataObjectsTest,TestCallable)629 TEST_F(PythonDataObjectsTest, TestCallable) {
630 
631   PythonDictionary globals(PyInitialValue::Empty);
632   auto builtins = PythonModule::BuiltinsModule();
633   llvm::Error error = globals.SetItem("__builtins__", builtins);
634   ASSERT_FALSE(error);
635 
636   {
637     PyObject *o = PyRun_String("lambda x : x", Py_eval_input, globals.get(),
638                                globals.get());
639     ASSERT_FALSE(o == NULL);
640     auto lambda = Take<PythonCallable>(o);
641     auto arginfo = lambda.GetArgInfo();
642     ASSERT_THAT_EXPECTED(arginfo, llvm::Succeeded());
643     EXPECT_EQ(arginfo.get().max_positional_args, 1u);
644   }
645 
646   {
647     PyObject *o = PyRun_String("lambda x,y=0: x", Py_eval_input, globals.get(),
648                                globals.get());
649     ASSERT_FALSE(o == NULL);
650     auto lambda = Take<PythonCallable>(o);
651     auto arginfo = lambda.GetArgInfo();
652     ASSERT_THAT_EXPECTED(arginfo, llvm::Succeeded());
653     EXPECT_EQ(arginfo.get().max_positional_args, 2u);
654   }
655 
656   {
657     PyObject *o = PyRun_String("lambda x,y=0, **kw: x", Py_eval_input,
658                                globals.get(), globals.get());
659     ASSERT_FALSE(o == NULL);
660     auto lambda = Take<PythonCallable>(o);
661     auto arginfo = lambda.GetArgInfo();
662     ASSERT_THAT_EXPECTED(arginfo, llvm::Succeeded());
663     EXPECT_EQ(arginfo.get().max_positional_args, 2u);
664   }
665 
666   {
667     PyObject *o = PyRun_String("lambda x,y,*a: x", Py_eval_input, globals.get(),
668                                globals.get());
669     ASSERT_FALSE(o == NULL);
670     auto lambda = Take<PythonCallable>(o);
671     auto arginfo = lambda.GetArgInfo();
672     ASSERT_THAT_EXPECTED(arginfo, llvm::Succeeded());
673     EXPECT_EQ(arginfo.get().max_positional_args,
674               PythonCallable::ArgInfo::UNBOUNDED);
675   }
676 
677   {
678     PyObject *o = PyRun_String("lambda x,y,*a,**kw: x", Py_eval_input,
679                                globals.get(), globals.get());
680     ASSERT_FALSE(o == NULL);
681     auto lambda = Take<PythonCallable>(o);
682     auto arginfo = lambda.GetArgInfo();
683     ASSERT_THAT_EXPECTED(arginfo, llvm::Succeeded());
684     EXPECT_EQ(arginfo.get().max_positional_args,
685               PythonCallable::ArgInfo::UNBOUNDED);
686   }
687 
688   {
689     const char *script = R"(
690 class Foo:
691   def bar(self, x):
692      return x
693   @classmethod
694   def classbar(cls, x):
695      return x
696   @staticmethod
697   def staticbar(x):
698      return x
699   def __call__(self, x):
700      return x
701 obj = Foo()
702 bar_bound   = Foo().bar
703 bar_class   = Foo().classbar
704 bar_static  = Foo().staticbar
705 bar_unbound = Foo.bar
706 
707 
708 class OldStyle:
709   def __init__(self, one, two, three):
710     pass
711 
712 class NewStyle(object):
713   def __init__(self, one, two, three):
714     pass
715 
716 )";
717     PyObject *o =
718         PyRun_String(script, Py_file_input, globals.get(), globals.get());
719     ASSERT_FALSE(o == NULL);
720     Take<PythonObject>(o);
721 
722     auto bar_bound = As<PythonCallable>(globals.GetItem("bar_bound"));
723     ASSERT_THAT_EXPECTED(bar_bound, llvm::Succeeded());
724     auto arginfo = bar_bound.get().GetArgInfo();
725     ASSERT_THAT_EXPECTED(arginfo, llvm::Succeeded());
726     EXPECT_EQ(arginfo.get().max_positional_args, 1u);
727 
728     auto bar_unbound = As<PythonCallable>(globals.GetItem("bar_unbound"));
729     ASSERT_THAT_EXPECTED(bar_unbound, llvm::Succeeded());
730     arginfo = bar_unbound.get().GetArgInfo();
731     ASSERT_THAT_EXPECTED(arginfo, llvm::Succeeded());
732     EXPECT_EQ(arginfo.get().max_positional_args, 2u);
733 
734     auto bar_class = As<PythonCallable>(globals.GetItem("bar_class"));
735     ASSERT_THAT_EXPECTED(bar_class, llvm::Succeeded());
736     arginfo = bar_class.get().GetArgInfo();
737     ASSERT_THAT_EXPECTED(arginfo, llvm::Succeeded());
738     EXPECT_EQ(arginfo.get().max_positional_args, 1u);
739 
740     auto bar_static = As<PythonCallable>(globals.GetItem("bar_static"));
741     ASSERT_THAT_EXPECTED(bar_static, llvm::Succeeded());
742     arginfo = bar_static.get().GetArgInfo();
743     ASSERT_THAT_EXPECTED(arginfo, llvm::Succeeded());
744     EXPECT_EQ(arginfo.get().max_positional_args, 1u);
745 
746     auto obj = As<PythonCallable>(globals.GetItem("obj"));
747     ASSERT_THAT_EXPECTED(obj, llvm::Succeeded());
748     arginfo = obj.get().GetArgInfo();
749     ASSERT_THAT_EXPECTED(arginfo, llvm::Succeeded());
750     EXPECT_EQ(arginfo.get().max_positional_args, 1u);
751 
752     auto oldstyle = As<PythonCallable>(globals.GetItem("OldStyle"));
753     ASSERT_THAT_EXPECTED(oldstyle, llvm::Succeeded());
754     arginfo = oldstyle.get().GetArgInfo();
755     ASSERT_THAT_EXPECTED(arginfo, llvm::Succeeded());
756     EXPECT_EQ(arginfo.get().max_positional_args, 3u);
757 
758     auto newstyle = As<PythonCallable>(globals.GetItem("NewStyle"));
759     ASSERT_THAT_EXPECTED(newstyle, llvm::Succeeded());
760     arginfo = newstyle.get().GetArgInfo();
761     ASSERT_THAT_EXPECTED(arginfo, llvm::Succeeded());
762     EXPECT_EQ(arginfo.get().max_positional_args, 3u);
763   }
764 
765 #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
766 
767   // the old implementation of GetArgInfo just doesn't work on builtins.
768 
769   {
770     auto builtins = PythonModule::BuiltinsModule();
771     auto hex = As<PythonCallable>(builtins.GetAttribute("hex"));
772     ASSERT_THAT_EXPECTED(hex, llvm::Succeeded());
773     auto arginfo = hex.get().GetArgInfo();
774     ASSERT_THAT_EXPECTED(arginfo, llvm::Succeeded());
775     EXPECT_EQ(arginfo.get().max_positional_args, 1u);
776   }
777 
778 #endif
779 }
780 
TEST_F(PythonDataObjectsTest,TestScript)781 TEST_F(PythonDataObjectsTest, TestScript) {
782 
783   static const char script[] = R"(
784 def factorial(n):
785   if n > 1:
786     return n * factorial(n-1)
787   else:
788     return 1;
789 main = factorial
790 )";
791 
792   PythonScript factorial(script);
793 
794   EXPECT_THAT_EXPECTED(As<long long>(factorial(5ll)), llvm::HasValue(120));
795 }
796 
TEST_F(PythonDataObjectsTest,TestExceptions)797 TEST_F(PythonDataObjectsTest, TestExceptions) {
798 
799   static const char script[] = R"(
800 def foo():
801   return bar()
802 def bar():
803   return baz()
804 def baz():
805   return 1 / 0
806 main = foo
807 )";
808 
809   PythonScript foo(script);
810 
811   EXPECT_THAT_EXPECTED(
812       foo(), llvm::Failed<PythonException>(testing::Property(
813                  &PythonException::ReadBacktrace,
814                  testing::AllOf(testing::ContainsRegex("line 3, in foo"),
815                                 testing::ContainsRegex("line 5, in bar"),
816                                 testing::ContainsRegex("line 7, in baz"),
817                                 testing::ContainsRegex("ZeroDivisionError")))));
818 
819   static const char script2[] = R"(
820 class MyError(Exception):
821   def __str__(self):
822     return self.my_message
823 
824 def main():
825   raise MyError("lol")
826 
827 )";
828 
829   PythonScript lol(script2);
830 
831   EXPECT_THAT_EXPECTED(lol(),
832                        llvm::Failed<PythonException>(testing::Property(
833                            &PythonException::ReadBacktrace,
834                            testing::ContainsRegex("unprintable MyError"))));
835 }
836 
TEST_F(PythonDataObjectsTest,TestRun)837 TEST_F(PythonDataObjectsTest, TestRun) {
838 
839   PythonDictionary globals(PyInitialValue::Empty);
840 
841   auto x = As<long long>(runStringOneLine("40 + 2", globals, globals));
842   ASSERT_THAT_EXPECTED(x, llvm::Succeeded());
843   EXPECT_EQ(x.get(), 42l);
844 
845   Expected<PythonObject> r = runStringOneLine("n = 42", globals, globals);
846   ASSERT_THAT_EXPECTED(r, llvm::Succeeded());
847   auto y = As<long long>(globals.GetItem("n"));
848   ASSERT_THAT_EXPECTED(y, llvm::Succeeded());
849   EXPECT_EQ(y.get(), 42l);
850 
851   const char script[] = R"(
852 def foobar():
853   return "foo" + "bar" + "baz"
854 g = foobar()
855 )";
856 
857   r = runStringMultiLine(script, globals, globals);
858   ASSERT_THAT_EXPECTED(r, llvm::Succeeded());
859   auto g = As<std::string>(globals.GetItem("g"));
860   ASSERT_THAT_EXPECTED(g, llvm::HasValue("foobarbaz"));
861 }