xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1dda28197Spatrick //===-- PythonDataObjects.cpp ---------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "lldb/Host/Config.h"
10061da546Spatrick 
11061da546Spatrick #if LLDB_ENABLE_PYTHON
12061da546Spatrick 
13061da546Spatrick #include "PythonDataObjects.h"
14061da546Spatrick #include "ScriptInterpreterPython.h"
15061da546Spatrick 
16061da546Spatrick #include "lldb/Host/File.h"
17061da546Spatrick #include "lldb/Host/FileSystem.h"
18061da546Spatrick #include "lldb/Interpreter/ScriptInterpreter.h"
19*f6aab3d8Srobert #include "lldb/Utility/LLDBLog.h"
20061da546Spatrick #include "lldb/Utility/Log.h"
21061da546Spatrick #include "lldb/Utility/Stream.h"
22061da546Spatrick 
23061da546Spatrick #include "llvm/Support/Casting.h"
24061da546Spatrick #include "llvm/Support/ConvertUTF.h"
25061da546Spatrick #include "llvm/Support/Errno.h"
26061da546Spatrick 
27be691f3bSpatrick #include <cstdio>
28061da546Spatrick 
29061da546Spatrick using namespace lldb_private;
30061da546Spatrick using namespace lldb;
31061da546Spatrick using namespace lldb_private::python;
32061da546Spatrick using llvm::cantFail;
33061da546Spatrick using llvm::Error;
34061da546Spatrick using llvm::Expected;
35061da546Spatrick using llvm::Twine;
36061da546Spatrick 
As(Expected<PythonObject> && obj)37061da546Spatrick template <> Expected<bool> python::As<bool>(Expected<PythonObject> &&obj) {
38061da546Spatrick   if (!obj)
39061da546Spatrick     return obj.takeError();
40061da546Spatrick   return obj.get().IsTrue();
41061da546Spatrick }
42061da546Spatrick 
43061da546Spatrick template <>
As(Expected<PythonObject> && obj)44061da546Spatrick Expected<long long> python::As<long long>(Expected<PythonObject> &&obj) {
45061da546Spatrick   if (!obj)
46061da546Spatrick     return obj.takeError();
47dda28197Spatrick   return obj->AsLongLong();
48dda28197Spatrick }
49dda28197Spatrick 
50dda28197Spatrick template <>
51dda28197Spatrick Expected<unsigned long long>
As(Expected<PythonObject> && obj)52dda28197Spatrick python::As<unsigned long long>(Expected<PythonObject> &&obj) {
53dda28197Spatrick   if (!obj)
54dda28197Spatrick     return obj.takeError();
55dda28197Spatrick   return obj->AsUnsignedLongLong();
56061da546Spatrick }
57061da546Spatrick 
58061da546Spatrick template <>
As(Expected<PythonObject> && obj)59061da546Spatrick Expected<std::string> python::As<std::string>(Expected<PythonObject> &&obj) {
60061da546Spatrick   if (!obj)
61061da546Spatrick     return obj.takeError();
62061da546Spatrick   PyObject *str_obj = PyObject_Str(obj.get().get());
63061da546Spatrick   if (!obj)
64061da546Spatrick     return llvm::make_error<PythonException>();
65061da546Spatrick   auto str = Take<PythonString>(str_obj);
66061da546Spatrick   auto utf8 = str.AsUTF8();
67061da546Spatrick   if (!utf8)
68061da546Spatrick     return utf8.takeError();
69dda28197Spatrick   return std::string(utf8.get());
70dda28197Spatrick }
71dda28197Spatrick 
python_is_finalizing()72*f6aab3d8Srobert static bool python_is_finalizing() {
73*f6aab3d8Srobert #if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 7
74*f6aab3d8Srobert   return _Py_Finalizing != nullptr;
75*f6aab3d8Srobert #else
76*f6aab3d8Srobert   return _Py_IsFinalizing();
77*f6aab3d8Srobert #endif
78*f6aab3d8Srobert }
79*f6aab3d8Srobert 
Reset()80*f6aab3d8Srobert void PythonObject::Reset() {
81*f6aab3d8Srobert   if (m_py_obj && Py_IsInitialized()) {
82*f6aab3d8Srobert     if (python_is_finalizing()) {
83*f6aab3d8Srobert       // Leak m_py_obj rather than crashing the process.
84*f6aab3d8Srobert       // https://docs.python.org/3/c-api/init.html#c.PyGILState_Ensure
85*f6aab3d8Srobert     } else {
86*f6aab3d8Srobert       PyGILState_STATE state = PyGILState_Ensure();
87*f6aab3d8Srobert       Py_DECREF(m_py_obj);
88*f6aab3d8Srobert       PyGILState_Release(state);
89*f6aab3d8Srobert     }
90*f6aab3d8Srobert   }
91*f6aab3d8Srobert   m_py_obj = nullptr;
92*f6aab3d8Srobert }
93*f6aab3d8Srobert 
AsLongLong() const94dda28197Spatrick Expected<long long> PythonObject::AsLongLong() const {
95dda28197Spatrick   if (!m_py_obj)
96dda28197Spatrick     return nullDeref();
97dda28197Spatrick   assert(!PyErr_Occurred());
98dda28197Spatrick   long long r = PyLong_AsLongLong(m_py_obj);
99dda28197Spatrick   if (PyErr_Occurred())
100dda28197Spatrick     return exception();
101dda28197Spatrick   return r;
102dda28197Spatrick }
103dda28197Spatrick 
AsUnsignedLongLong() const104dda28197Spatrick Expected<long long> PythonObject::AsUnsignedLongLong() const {
105dda28197Spatrick   if (!m_py_obj)
106dda28197Spatrick     return nullDeref();
107dda28197Spatrick   assert(!PyErr_Occurred());
108dda28197Spatrick   long long r = PyLong_AsUnsignedLongLong(m_py_obj);
109dda28197Spatrick   if (PyErr_Occurred())
110dda28197Spatrick     return exception();
111dda28197Spatrick   return r;
112dda28197Spatrick }
113dda28197Spatrick 
114dda28197Spatrick // wraps on overflow, instead of raising an error.
AsModuloUnsignedLongLong() const115dda28197Spatrick Expected<unsigned long long> PythonObject::AsModuloUnsignedLongLong() const {
116dda28197Spatrick   if (!m_py_obj)
117dda28197Spatrick     return nullDeref();
118dda28197Spatrick   assert(!PyErr_Occurred());
119dda28197Spatrick   unsigned long long r = PyLong_AsUnsignedLongLongMask(m_py_obj);
120dda28197Spatrick   if (PyErr_Occurred())
121dda28197Spatrick     return exception();
122dda28197Spatrick   return r;
123061da546Spatrick }
124061da546Spatrick 
Serialize(llvm::json::OStream & s) const125061da546Spatrick void StructuredPythonObject::Serialize(llvm::json::OStream &s) const {
126061da546Spatrick   s.value(llvm::formatv("Python Obj: {0:X}", GetValue()).str());
127061da546Spatrick }
128061da546Spatrick 
129061da546Spatrick // PythonObject
130061da546Spatrick 
Dump(Stream & strm) const131061da546Spatrick void PythonObject::Dump(Stream &strm) const {
132061da546Spatrick   if (m_py_obj) {
133061da546Spatrick     FILE *file = llvm::sys::RetryAfterSignal(nullptr, ::tmpfile);
134061da546Spatrick     if (file) {
135061da546Spatrick       ::PyObject_Print(m_py_obj, file, 0);
136061da546Spatrick       const long length = ftell(file);
137061da546Spatrick       if (length) {
138061da546Spatrick         ::rewind(file);
139061da546Spatrick         std::vector<char> file_contents(length, '\0');
140061da546Spatrick         const size_t length_read =
141061da546Spatrick             ::fread(file_contents.data(), 1, file_contents.size(), file);
142061da546Spatrick         if (length_read > 0)
143061da546Spatrick           strm.Write(file_contents.data(), length_read);
144061da546Spatrick       }
145061da546Spatrick       ::fclose(file);
146061da546Spatrick     }
147061da546Spatrick   } else
148061da546Spatrick     strm.PutCString("NULL");
149061da546Spatrick }
150061da546Spatrick 
GetObjectType() const151061da546Spatrick PyObjectType PythonObject::GetObjectType() const {
152061da546Spatrick   if (!IsAllocated())
153061da546Spatrick     return PyObjectType::None;
154061da546Spatrick 
155061da546Spatrick   if (PythonModule::Check(m_py_obj))
156061da546Spatrick     return PyObjectType::Module;
157061da546Spatrick   if (PythonList::Check(m_py_obj))
158061da546Spatrick     return PyObjectType::List;
159061da546Spatrick   if (PythonTuple::Check(m_py_obj))
160061da546Spatrick     return PyObjectType::Tuple;
161061da546Spatrick   if (PythonDictionary::Check(m_py_obj))
162061da546Spatrick     return PyObjectType::Dictionary;
163061da546Spatrick   if (PythonString::Check(m_py_obj))
164061da546Spatrick     return PyObjectType::String;
165061da546Spatrick   if (PythonBytes::Check(m_py_obj))
166061da546Spatrick     return PyObjectType::Bytes;
167061da546Spatrick   if (PythonByteArray::Check(m_py_obj))
168061da546Spatrick     return PyObjectType::ByteArray;
169061da546Spatrick   if (PythonBoolean::Check(m_py_obj))
170061da546Spatrick     return PyObjectType::Boolean;
171061da546Spatrick   if (PythonInteger::Check(m_py_obj))
172061da546Spatrick     return PyObjectType::Integer;
173061da546Spatrick   if (PythonFile::Check(m_py_obj))
174061da546Spatrick     return PyObjectType::File;
175061da546Spatrick   if (PythonCallable::Check(m_py_obj))
176061da546Spatrick     return PyObjectType::Callable;
177061da546Spatrick   return PyObjectType::Unknown;
178061da546Spatrick }
179061da546Spatrick 
Repr() const180061da546Spatrick PythonString PythonObject::Repr() const {
181061da546Spatrick   if (!m_py_obj)
182061da546Spatrick     return PythonString();
183061da546Spatrick   PyObject *repr = PyObject_Repr(m_py_obj);
184061da546Spatrick   if (!repr)
185061da546Spatrick     return PythonString();
186061da546Spatrick   return PythonString(PyRefType::Owned, repr);
187061da546Spatrick }
188061da546Spatrick 
Str() const189061da546Spatrick PythonString PythonObject::Str() const {
190061da546Spatrick   if (!m_py_obj)
191061da546Spatrick     return PythonString();
192061da546Spatrick   PyObject *str = PyObject_Str(m_py_obj);
193061da546Spatrick   if (!str)
194061da546Spatrick     return PythonString();
195061da546Spatrick   return PythonString(PyRefType::Owned, str);
196061da546Spatrick }
197061da546Spatrick 
198061da546Spatrick PythonObject
ResolveNameWithDictionary(llvm::StringRef name,const PythonDictionary & dict)199061da546Spatrick PythonObject::ResolveNameWithDictionary(llvm::StringRef name,
200061da546Spatrick                                         const PythonDictionary &dict) {
201061da546Spatrick   size_t dot_pos = name.find('.');
202061da546Spatrick   llvm::StringRef piece = name.substr(0, dot_pos);
203061da546Spatrick   PythonObject result = dict.GetItemForKey(PythonString(piece));
204061da546Spatrick   if (dot_pos == llvm::StringRef::npos) {
205061da546Spatrick     // There was no dot, we're done.
206061da546Spatrick     return result;
207061da546Spatrick   }
208061da546Spatrick 
209061da546Spatrick   // There was a dot.  The remaining portion of the name should be looked up in
210061da546Spatrick   // the context of the object that was found in the dictionary.
211061da546Spatrick   return result.ResolveName(name.substr(dot_pos + 1));
212061da546Spatrick }
213061da546Spatrick 
ResolveName(llvm::StringRef name) const214061da546Spatrick PythonObject PythonObject::ResolveName(llvm::StringRef name) const {
215061da546Spatrick   // Resolve the name in the context of the specified object.  If, for example,
216061da546Spatrick   // `this` refers to a PyModule, then this will look for `name` in this
217061da546Spatrick   // module.  If `this` refers to a PyType, then it will resolve `name` as an
218061da546Spatrick   // attribute of that type.  If `this` refers to an instance of an object,
219061da546Spatrick   // then it will resolve `name` as the value of the specified field.
220061da546Spatrick   //
221061da546Spatrick   // This function handles dotted names so that, for example, if `m_py_obj`
222061da546Spatrick   // refers to the `sys` module, and `name` == "path.append", then it will find
223061da546Spatrick   // the function `sys.path.append`.
224061da546Spatrick 
225061da546Spatrick   size_t dot_pos = name.find('.');
226061da546Spatrick   if (dot_pos == llvm::StringRef::npos) {
227061da546Spatrick     // No dots in the name, we should be able to find the value immediately as
228061da546Spatrick     // an attribute of `m_py_obj`.
229061da546Spatrick     return GetAttributeValue(name);
230061da546Spatrick   }
231061da546Spatrick 
232061da546Spatrick   // Look up the first piece of the name, and resolve the rest as a child of
233061da546Spatrick   // that.
234061da546Spatrick   PythonObject parent = ResolveName(name.substr(0, dot_pos));
235061da546Spatrick   if (!parent.IsAllocated())
236061da546Spatrick     return PythonObject();
237061da546Spatrick 
238061da546Spatrick   // Tail recursion.. should be optimized by the compiler
239061da546Spatrick   return parent.ResolveName(name.substr(dot_pos + 1));
240061da546Spatrick }
241061da546Spatrick 
HasAttribute(llvm::StringRef attr) const242061da546Spatrick bool PythonObject::HasAttribute(llvm::StringRef attr) const {
243061da546Spatrick   if (!IsValid())
244061da546Spatrick     return false;
245061da546Spatrick   PythonString py_attr(attr);
246061da546Spatrick   return !!PyObject_HasAttr(m_py_obj, py_attr.get());
247061da546Spatrick }
248061da546Spatrick 
GetAttributeValue(llvm::StringRef attr) const249061da546Spatrick PythonObject PythonObject::GetAttributeValue(llvm::StringRef attr) const {
250061da546Spatrick   if (!IsValid())
251061da546Spatrick     return PythonObject();
252061da546Spatrick 
253061da546Spatrick   PythonString py_attr(attr);
254061da546Spatrick   if (!PyObject_HasAttr(m_py_obj, py_attr.get()))
255061da546Spatrick     return PythonObject();
256061da546Spatrick 
257061da546Spatrick   return PythonObject(PyRefType::Owned,
258061da546Spatrick                       PyObject_GetAttr(m_py_obj, py_attr.get()));
259061da546Spatrick }
260061da546Spatrick 
CreateStructuredObject() const261061da546Spatrick StructuredData::ObjectSP PythonObject::CreateStructuredObject() const {
262*f6aab3d8Srobert   assert(PyGILState_Check());
263061da546Spatrick   switch (GetObjectType()) {
264061da546Spatrick   case PyObjectType::Dictionary:
265061da546Spatrick     return PythonDictionary(PyRefType::Borrowed, m_py_obj)
266061da546Spatrick         .CreateStructuredDictionary();
267061da546Spatrick   case PyObjectType::Boolean:
268061da546Spatrick     return PythonBoolean(PyRefType::Borrowed, m_py_obj)
269061da546Spatrick         .CreateStructuredBoolean();
270061da546Spatrick   case PyObjectType::Integer:
271061da546Spatrick     return PythonInteger(PyRefType::Borrowed, m_py_obj)
272061da546Spatrick         .CreateStructuredInteger();
273061da546Spatrick   case PyObjectType::List:
274061da546Spatrick     return PythonList(PyRefType::Borrowed, m_py_obj).CreateStructuredArray();
275061da546Spatrick   case PyObjectType::String:
276061da546Spatrick     return PythonString(PyRefType::Borrowed, m_py_obj).CreateStructuredString();
277061da546Spatrick   case PyObjectType::Bytes:
278061da546Spatrick     return PythonBytes(PyRefType::Borrowed, m_py_obj).CreateStructuredString();
279061da546Spatrick   case PyObjectType::ByteArray:
280061da546Spatrick     return PythonByteArray(PyRefType::Borrowed, m_py_obj)
281061da546Spatrick         .CreateStructuredString();
282061da546Spatrick   case PyObjectType::None:
283061da546Spatrick     return StructuredData::ObjectSP();
284061da546Spatrick   default:
285*f6aab3d8Srobert     return StructuredData::ObjectSP(new StructuredPythonObject(
286*f6aab3d8Srobert         PythonObject(PyRefType::Borrowed, m_py_obj)));
287061da546Spatrick   }
288061da546Spatrick }
289061da546Spatrick 
290061da546Spatrick // PythonString
291061da546Spatrick 
PythonBytes(llvm::ArrayRef<uint8_t> bytes)292061da546Spatrick PythonBytes::PythonBytes(llvm::ArrayRef<uint8_t> bytes) { SetBytes(bytes); }
293061da546Spatrick 
PythonBytes(const uint8_t * bytes,size_t length)294061da546Spatrick PythonBytes::PythonBytes(const uint8_t *bytes, size_t length) {
295061da546Spatrick   SetBytes(llvm::ArrayRef<uint8_t>(bytes, length));
296061da546Spatrick }
297061da546Spatrick 
Check(PyObject * py_obj)298061da546Spatrick bool PythonBytes::Check(PyObject *py_obj) {
299061da546Spatrick   if (!py_obj)
300061da546Spatrick     return false;
301061da546Spatrick   return PyBytes_Check(py_obj);
302061da546Spatrick }
303061da546Spatrick 
GetBytes() const304061da546Spatrick llvm::ArrayRef<uint8_t> PythonBytes::GetBytes() const {
305061da546Spatrick   if (!IsValid())
306061da546Spatrick     return llvm::ArrayRef<uint8_t>();
307061da546Spatrick 
308061da546Spatrick   Py_ssize_t size;
309061da546Spatrick   char *c;
310061da546Spatrick 
311061da546Spatrick   PyBytes_AsStringAndSize(m_py_obj, &c, &size);
312061da546Spatrick   return llvm::ArrayRef<uint8_t>(reinterpret_cast<uint8_t *>(c), size);
313061da546Spatrick }
314061da546Spatrick 
GetSize() const315061da546Spatrick size_t PythonBytes::GetSize() const {
316061da546Spatrick   if (!IsValid())
317061da546Spatrick     return 0;
318061da546Spatrick   return PyBytes_Size(m_py_obj);
319061da546Spatrick }
320061da546Spatrick 
SetBytes(llvm::ArrayRef<uint8_t> bytes)321061da546Spatrick void PythonBytes::SetBytes(llvm::ArrayRef<uint8_t> bytes) {
322061da546Spatrick   const char *data = reinterpret_cast<const char *>(bytes.data());
323061da546Spatrick   *this = Take<PythonBytes>(PyBytes_FromStringAndSize(data, bytes.size()));
324061da546Spatrick }
325061da546Spatrick 
CreateStructuredString() const326061da546Spatrick StructuredData::StringSP PythonBytes::CreateStructuredString() const {
327061da546Spatrick   StructuredData::StringSP result(new StructuredData::String);
328061da546Spatrick   Py_ssize_t size;
329061da546Spatrick   char *c;
330061da546Spatrick   PyBytes_AsStringAndSize(m_py_obj, &c, &size);
331061da546Spatrick   result->SetValue(std::string(c, size));
332061da546Spatrick   return result;
333061da546Spatrick }
334061da546Spatrick 
PythonByteArray(llvm::ArrayRef<uint8_t> bytes)335061da546Spatrick PythonByteArray::PythonByteArray(llvm::ArrayRef<uint8_t> bytes)
336061da546Spatrick     : PythonByteArray(bytes.data(), bytes.size()) {}
337061da546Spatrick 
PythonByteArray(const uint8_t * bytes,size_t length)338061da546Spatrick PythonByteArray::PythonByteArray(const uint8_t *bytes, size_t length) {
339061da546Spatrick   const char *str = reinterpret_cast<const char *>(bytes);
340061da546Spatrick   *this = Take<PythonByteArray>(PyByteArray_FromStringAndSize(str, length));
341061da546Spatrick }
342061da546Spatrick 
Check(PyObject * py_obj)343061da546Spatrick bool PythonByteArray::Check(PyObject *py_obj) {
344061da546Spatrick   if (!py_obj)
345061da546Spatrick     return false;
346061da546Spatrick   return PyByteArray_Check(py_obj);
347061da546Spatrick }
348061da546Spatrick 
GetBytes() const349061da546Spatrick llvm::ArrayRef<uint8_t> PythonByteArray::GetBytes() const {
350061da546Spatrick   if (!IsValid())
351061da546Spatrick     return llvm::ArrayRef<uint8_t>();
352061da546Spatrick 
353061da546Spatrick   char *c = PyByteArray_AsString(m_py_obj);
354061da546Spatrick   size_t size = GetSize();
355061da546Spatrick   return llvm::ArrayRef<uint8_t>(reinterpret_cast<uint8_t *>(c), size);
356061da546Spatrick }
357061da546Spatrick 
GetSize() const358061da546Spatrick size_t PythonByteArray::GetSize() const {
359061da546Spatrick   if (!IsValid())
360061da546Spatrick     return 0;
361061da546Spatrick 
362061da546Spatrick   return PyByteArray_Size(m_py_obj);
363061da546Spatrick }
364061da546Spatrick 
CreateStructuredString() const365061da546Spatrick StructuredData::StringSP PythonByteArray::CreateStructuredString() const {
366061da546Spatrick   StructuredData::StringSP result(new StructuredData::String);
367061da546Spatrick   llvm::ArrayRef<uint8_t> bytes = GetBytes();
368061da546Spatrick   const char *str = reinterpret_cast<const char *>(bytes.data());
369061da546Spatrick   result->SetValue(std::string(str, bytes.size()));
370061da546Spatrick   return result;
371061da546Spatrick }
372061da546Spatrick 
373061da546Spatrick // PythonString
374061da546Spatrick 
FromUTF8(llvm::StringRef string)375061da546Spatrick Expected<PythonString> PythonString::FromUTF8(llvm::StringRef string) {
376061da546Spatrick   PyObject *str = PyUnicode_FromStringAndSize(string.data(), string.size());
377061da546Spatrick   if (!str)
378061da546Spatrick     return llvm::make_error<PythonException>();
379061da546Spatrick   return Take<PythonString>(str);
380061da546Spatrick }
381061da546Spatrick 
PythonString(llvm::StringRef string)382061da546Spatrick PythonString::PythonString(llvm::StringRef string) { SetString(string); }
383061da546Spatrick 
Check(PyObject * py_obj)384061da546Spatrick bool PythonString::Check(PyObject *py_obj) {
385061da546Spatrick   if (!py_obj)
386061da546Spatrick     return false;
387061da546Spatrick 
388061da546Spatrick   if (PyUnicode_Check(py_obj))
389061da546Spatrick     return true;
390061da546Spatrick   return false;
391061da546Spatrick }
392061da546Spatrick 
GetString() const393061da546Spatrick llvm::StringRef PythonString::GetString() const {
394061da546Spatrick   auto s = AsUTF8();
395061da546Spatrick   if (!s) {
396061da546Spatrick     llvm::consumeError(s.takeError());
397061da546Spatrick     return llvm::StringRef("");
398061da546Spatrick   }
399061da546Spatrick   return s.get();
400061da546Spatrick }
401061da546Spatrick 
AsUTF8() const402061da546Spatrick Expected<llvm::StringRef> PythonString::AsUTF8() const {
403061da546Spatrick   if (!IsValid())
404061da546Spatrick     return nullDeref();
405061da546Spatrick 
406061da546Spatrick   Py_ssize_t size;
407061da546Spatrick   const char *data;
408061da546Spatrick 
409061da546Spatrick   data = PyUnicode_AsUTF8AndSize(m_py_obj, &size);
410061da546Spatrick 
411061da546Spatrick   if (!data)
412061da546Spatrick     return exception();
413061da546Spatrick 
414061da546Spatrick   return llvm::StringRef(data, size);
415061da546Spatrick }
416061da546Spatrick 
GetSize() const417061da546Spatrick size_t PythonString::GetSize() const {
418061da546Spatrick   if (IsValid()) {
419be691f3bSpatrick #if PY_MINOR_VERSION >= 3
420be691f3bSpatrick     return PyUnicode_GetLength(m_py_obj);
421be691f3bSpatrick #else
422061da546Spatrick     return PyUnicode_GetSize(m_py_obj);
423be691f3bSpatrick #endif
424061da546Spatrick   }
425061da546Spatrick   return 0;
426061da546Spatrick }
427061da546Spatrick 
SetString(llvm::StringRef string)428061da546Spatrick void PythonString::SetString(llvm::StringRef string) {
429061da546Spatrick   auto s = FromUTF8(string);
430061da546Spatrick   if (!s) {
431061da546Spatrick     llvm::consumeError(s.takeError());
432061da546Spatrick     Reset();
433061da546Spatrick   } else {
434061da546Spatrick     *this = std::move(s.get());
435061da546Spatrick   }
436061da546Spatrick }
437061da546Spatrick 
CreateStructuredString() const438061da546Spatrick StructuredData::StringSP PythonString::CreateStructuredString() const {
439061da546Spatrick   StructuredData::StringSP result(new StructuredData::String);
440061da546Spatrick   result->SetValue(GetString());
441061da546Spatrick   return result;
442061da546Spatrick }
443061da546Spatrick 
444061da546Spatrick // PythonInteger
445061da546Spatrick 
PythonInteger(int64_t value)446061da546Spatrick PythonInteger::PythonInteger(int64_t value) { SetInteger(value); }
447061da546Spatrick 
Check(PyObject * py_obj)448061da546Spatrick bool PythonInteger::Check(PyObject *py_obj) {
449061da546Spatrick   if (!py_obj)
450061da546Spatrick     return false;
451061da546Spatrick 
452061da546Spatrick   // Python 3 does not have PyInt_Check.  There is only one type of integral
453061da546Spatrick   // value, long.
454061da546Spatrick   return PyLong_Check(py_obj);
455061da546Spatrick }
456061da546Spatrick 
SetInteger(int64_t value)457061da546Spatrick void PythonInteger::SetInteger(int64_t value) {
458061da546Spatrick   *this = Take<PythonInteger>(PyLong_FromLongLong(value));
459061da546Spatrick }
460061da546Spatrick 
CreateStructuredInteger() const461061da546Spatrick StructuredData::IntegerSP PythonInteger::CreateStructuredInteger() const {
462061da546Spatrick   StructuredData::IntegerSP result(new StructuredData::Integer);
463dda28197Spatrick   // FIXME this is really not ideal.   Errors are silently converted to 0
464dda28197Spatrick   // and overflows are silently wrapped.   But we'd need larger changes
465dda28197Spatrick   // to StructuredData to fix it, so that's how it is for now.
466dda28197Spatrick   llvm::Expected<unsigned long long> value = AsModuloUnsignedLongLong();
467dda28197Spatrick   if (!value) {
468dda28197Spatrick     llvm::consumeError(value.takeError());
469dda28197Spatrick     result->SetValue(0);
470dda28197Spatrick   } else {
471dda28197Spatrick     result->SetValue(value.get());
472dda28197Spatrick   }
473061da546Spatrick   return result;
474061da546Spatrick }
475061da546Spatrick 
476061da546Spatrick // PythonBoolean
477061da546Spatrick 
PythonBoolean(bool value)478061da546Spatrick PythonBoolean::PythonBoolean(bool value) {
479061da546Spatrick   SetValue(value);
480061da546Spatrick }
481061da546Spatrick 
Check(PyObject * py_obj)482061da546Spatrick bool PythonBoolean::Check(PyObject *py_obj) {
483061da546Spatrick   return py_obj ? PyBool_Check(py_obj) : false;
484061da546Spatrick }
485061da546Spatrick 
GetValue() const486061da546Spatrick bool PythonBoolean::GetValue() const {
487061da546Spatrick   return m_py_obj ? PyObject_IsTrue(m_py_obj) : false;
488061da546Spatrick }
489061da546Spatrick 
SetValue(bool value)490061da546Spatrick void PythonBoolean::SetValue(bool value) {
491061da546Spatrick   *this = Take<PythonBoolean>(PyBool_FromLong(value));
492061da546Spatrick }
493061da546Spatrick 
CreateStructuredBoolean() const494061da546Spatrick StructuredData::BooleanSP PythonBoolean::CreateStructuredBoolean() const {
495061da546Spatrick   StructuredData::BooleanSP result(new StructuredData::Boolean);
496061da546Spatrick   result->SetValue(GetValue());
497061da546Spatrick   return result;
498061da546Spatrick }
499061da546Spatrick 
500061da546Spatrick // PythonList
501061da546Spatrick 
PythonList(PyInitialValue value)502061da546Spatrick PythonList::PythonList(PyInitialValue value) {
503061da546Spatrick   if (value == PyInitialValue::Empty)
504061da546Spatrick     *this = Take<PythonList>(PyList_New(0));
505061da546Spatrick }
506061da546Spatrick 
PythonList(int list_size)507061da546Spatrick PythonList::PythonList(int list_size) {
508061da546Spatrick   *this = Take<PythonList>(PyList_New(list_size));
509061da546Spatrick }
510061da546Spatrick 
Check(PyObject * py_obj)511061da546Spatrick bool PythonList::Check(PyObject *py_obj) {
512061da546Spatrick   if (!py_obj)
513061da546Spatrick     return false;
514061da546Spatrick   return PyList_Check(py_obj);
515061da546Spatrick }
516061da546Spatrick 
GetSize() const517061da546Spatrick uint32_t PythonList::GetSize() const {
518061da546Spatrick   if (IsValid())
519061da546Spatrick     return PyList_GET_SIZE(m_py_obj);
520061da546Spatrick   return 0;
521061da546Spatrick }
522061da546Spatrick 
GetItemAtIndex(uint32_t index) const523061da546Spatrick PythonObject PythonList::GetItemAtIndex(uint32_t index) const {
524061da546Spatrick   if (IsValid())
525061da546Spatrick     return PythonObject(PyRefType::Borrowed, PyList_GetItem(m_py_obj, index));
526061da546Spatrick   return PythonObject();
527061da546Spatrick }
528061da546Spatrick 
SetItemAtIndex(uint32_t index,const PythonObject & object)529061da546Spatrick void PythonList::SetItemAtIndex(uint32_t index, const PythonObject &object) {
530061da546Spatrick   if (IsAllocated() && object.IsValid()) {
531061da546Spatrick     // PyList_SetItem is documented to "steal" a reference, so we need to
532061da546Spatrick     // convert it to an owned reference by incrementing it.
533061da546Spatrick     Py_INCREF(object.get());
534061da546Spatrick     PyList_SetItem(m_py_obj, index, object.get());
535061da546Spatrick   }
536061da546Spatrick }
537061da546Spatrick 
AppendItem(const PythonObject & object)538061da546Spatrick void PythonList::AppendItem(const PythonObject &object) {
539061da546Spatrick   if (IsAllocated() && object.IsValid()) {
540061da546Spatrick     // `PyList_Append` does *not* steal a reference, so do not call `Py_INCREF`
541061da546Spatrick     // here like we do with `PyList_SetItem`.
542061da546Spatrick     PyList_Append(m_py_obj, object.get());
543061da546Spatrick   }
544061da546Spatrick }
545061da546Spatrick 
CreateStructuredArray() const546061da546Spatrick StructuredData::ArraySP PythonList::CreateStructuredArray() const {
547061da546Spatrick   StructuredData::ArraySP result(new StructuredData::Array);
548061da546Spatrick   uint32_t count = GetSize();
549061da546Spatrick   for (uint32_t i = 0; i < count; ++i) {
550061da546Spatrick     PythonObject obj = GetItemAtIndex(i);
551061da546Spatrick     result->AddItem(obj.CreateStructuredObject());
552061da546Spatrick   }
553061da546Spatrick   return result;
554061da546Spatrick }
555061da546Spatrick 
556061da546Spatrick // PythonTuple
557061da546Spatrick 
PythonTuple(PyInitialValue value)558061da546Spatrick PythonTuple::PythonTuple(PyInitialValue value) {
559061da546Spatrick   if (value == PyInitialValue::Empty)
560061da546Spatrick     *this = Take<PythonTuple>(PyTuple_New(0));
561061da546Spatrick }
562061da546Spatrick 
PythonTuple(int tuple_size)563061da546Spatrick PythonTuple::PythonTuple(int tuple_size) {
564061da546Spatrick   *this = Take<PythonTuple>(PyTuple_New(tuple_size));
565061da546Spatrick }
566061da546Spatrick 
PythonTuple(std::initializer_list<PythonObject> objects)567061da546Spatrick PythonTuple::PythonTuple(std::initializer_list<PythonObject> objects) {
568061da546Spatrick   m_py_obj = PyTuple_New(objects.size());
569061da546Spatrick 
570061da546Spatrick   uint32_t idx = 0;
571061da546Spatrick   for (auto object : objects) {
572061da546Spatrick     if (object.IsValid())
573061da546Spatrick       SetItemAtIndex(idx, object);
574061da546Spatrick     idx++;
575061da546Spatrick   }
576061da546Spatrick }
577061da546Spatrick 
PythonTuple(std::initializer_list<PyObject * > objects)578061da546Spatrick PythonTuple::PythonTuple(std::initializer_list<PyObject *> objects) {
579061da546Spatrick   m_py_obj = PyTuple_New(objects.size());
580061da546Spatrick 
581061da546Spatrick   uint32_t idx = 0;
582061da546Spatrick   for (auto py_object : objects) {
583061da546Spatrick     PythonObject object(PyRefType::Borrowed, py_object);
584061da546Spatrick     if (object.IsValid())
585061da546Spatrick       SetItemAtIndex(idx, object);
586061da546Spatrick     idx++;
587061da546Spatrick   }
588061da546Spatrick }
589061da546Spatrick 
Check(PyObject * py_obj)590061da546Spatrick bool PythonTuple::Check(PyObject *py_obj) {
591061da546Spatrick   if (!py_obj)
592061da546Spatrick     return false;
593061da546Spatrick   return PyTuple_Check(py_obj);
594061da546Spatrick }
595061da546Spatrick 
GetSize() const596061da546Spatrick uint32_t PythonTuple::GetSize() const {
597061da546Spatrick   if (IsValid())
598061da546Spatrick     return PyTuple_GET_SIZE(m_py_obj);
599061da546Spatrick   return 0;
600061da546Spatrick }
601061da546Spatrick 
GetItemAtIndex(uint32_t index) const602061da546Spatrick PythonObject PythonTuple::GetItemAtIndex(uint32_t index) const {
603061da546Spatrick   if (IsValid())
604061da546Spatrick     return PythonObject(PyRefType::Borrowed, PyTuple_GetItem(m_py_obj, index));
605061da546Spatrick   return PythonObject();
606061da546Spatrick }
607061da546Spatrick 
SetItemAtIndex(uint32_t index,const PythonObject & object)608061da546Spatrick void PythonTuple::SetItemAtIndex(uint32_t index, const PythonObject &object) {
609061da546Spatrick   if (IsAllocated() && object.IsValid()) {
610061da546Spatrick     // PyTuple_SetItem is documented to "steal" a reference, so we need to
611061da546Spatrick     // convert it to an owned reference by incrementing it.
612061da546Spatrick     Py_INCREF(object.get());
613061da546Spatrick     PyTuple_SetItem(m_py_obj, index, object.get());
614061da546Spatrick   }
615061da546Spatrick }
616061da546Spatrick 
CreateStructuredArray() const617061da546Spatrick StructuredData::ArraySP PythonTuple::CreateStructuredArray() const {
618061da546Spatrick   StructuredData::ArraySP result(new StructuredData::Array);
619061da546Spatrick   uint32_t count = GetSize();
620061da546Spatrick   for (uint32_t i = 0; i < count; ++i) {
621061da546Spatrick     PythonObject obj = GetItemAtIndex(i);
622061da546Spatrick     result->AddItem(obj.CreateStructuredObject());
623061da546Spatrick   }
624061da546Spatrick   return result;
625061da546Spatrick }
626061da546Spatrick 
627061da546Spatrick // PythonDictionary
628061da546Spatrick 
PythonDictionary(PyInitialValue value)629061da546Spatrick PythonDictionary::PythonDictionary(PyInitialValue value) {
630061da546Spatrick   if (value == PyInitialValue::Empty)
631061da546Spatrick     *this = Take<PythonDictionary>(PyDict_New());
632061da546Spatrick }
633061da546Spatrick 
Check(PyObject * py_obj)634061da546Spatrick bool PythonDictionary::Check(PyObject *py_obj) {
635061da546Spatrick   if (!py_obj)
636061da546Spatrick     return false;
637061da546Spatrick 
638061da546Spatrick   return PyDict_Check(py_obj);
639061da546Spatrick }
640061da546Spatrick 
GetSize() const641061da546Spatrick uint32_t PythonDictionary::GetSize() const {
642061da546Spatrick   if (IsValid())
643061da546Spatrick     return PyDict_Size(m_py_obj);
644061da546Spatrick   return 0;
645061da546Spatrick }
646061da546Spatrick 
GetKeys() const647061da546Spatrick PythonList PythonDictionary::GetKeys() const {
648061da546Spatrick   if (IsValid())
649061da546Spatrick     return PythonList(PyRefType::Owned, PyDict_Keys(m_py_obj));
650061da546Spatrick   return PythonList(PyInitialValue::Invalid);
651061da546Spatrick }
652061da546Spatrick 
GetItemForKey(const PythonObject & key) const653061da546Spatrick PythonObject PythonDictionary::GetItemForKey(const PythonObject &key) const {
654061da546Spatrick   auto item = GetItem(key);
655061da546Spatrick   if (!item) {
656061da546Spatrick     llvm::consumeError(item.takeError());
657061da546Spatrick     return PythonObject();
658061da546Spatrick   }
659061da546Spatrick   return std::move(item.get());
660061da546Spatrick }
661061da546Spatrick 
662061da546Spatrick Expected<PythonObject>
GetItem(const PythonObject & key) const663061da546Spatrick PythonDictionary::GetItem(const PythonObject &key) const {
664061da546Spatrick   if (!IsValid())
665061da546Spatrick     return nullDeref();
666061da546Spatrick   PyObject *o = PyDict_GetItemWithError(m_py_obj, key.get());
667061da546Spatrick   if (PyErr_Occurred())
668061da546Spatrick     return exception();
669061da546Spatrick   if (!o)
670061da546Spatrick     return keyError();
671061da546Spatrick   return Retain<PythonObject>(o);
672061da546Spatrick }
673061da546Spatrick 
GetItem(const Twine & key) const674061da546Spatrick Expected<PythonObject> PythonDictionary::GetItem(const Twine &key) const {
675061da546Spatrick   if (!IsValid())
676061da546Spatrick     return nullDeref();
677061da546Spatrick   PyObject *o = PyDict_GetItemString(m_py_obj, NullTerminated(key));
678061da546Spatrick   if (PyErr_Occurred())
679061da546Spatrick     return exception();
680061da546Spatrick   if (!o)
681061da546Spatrick     return keyError();
682061da546Spatrick   return Retain<PythonObject>(o);
683061da546Spatrick }
684061da546Spatrick 
SetItem(const PythonObject & key,const PythonObject & value) const685061da546Spatrick Error PythonDictionary::SetItem(const PythonObject &key,
686061da546Spatrick                                 const PythonObject &value) const {
687061da546Spatrick   if (!IsValid() || !value.IsValid())
688061da546Spatrick     return nullDeref();
689061da546Spatrick   int r = PyDict_SetItem(m_py_obj, key.get(), value.get());
690061da546Spatrick   if (r < 0)
691061da546Spatrick     return exception();
692061da546Spatrick   return Error::success();
693061da546Spatrick }
694061da546Spatrick 
SetItem(const Twine & key,const PythonObject & value) const695061da546Spatrick Error PythonDictionary::SetItem(const Twine &key,
696061da546Spatrick                                 const PythonObject &value) const {
697061da546Spatrick   if (!IsValid() || !value.IsValid())
698061da546Spatrick     return nullDeref();
699061da546Spatrick   int r = PyDict_SetItemString(m_py_obj, NullTerminated(key), value.get());
700061da546Spatrick   if (r < 0)
701061da546Spatrick     return exception();
702061da546Spatrick   return Error::success();
703061da546Spatrick }
704061da546Spatrick 
SetItemForKey(const PythonObject & key,const PythonObject & value)705061da546Spatrick void PythonDictionary::SetItemForKey(const PythonObject &key,
706061da546Spatrick                                      const PythonObject &value) {
707061da546Spatrick   Error error = SetItem(key, value);
708061da546Spatrick   if (error)
709061da546Spatrick     llvm::consumeError(std::move(error));
710061da546Spatrick }
711061da546Spatrick 
712061da546Spatrick StructuredData::DictionarySP
CreateStructuredDictionary() const713061da546Spatrick PythonDictionary::CreateStructuredDictionary() const {
714061da546Spatrick   StructuredData::DictionarySP result(new StructuredData::Dictionary);
715061da546Spatrick   PythonList keys(GetKeys());
716061da546Spatrick   uint32_t num_keys = keys.GetSize();
717061da546Spatrick   for (uint32_t i = 0; i < num_keys; ++i) {
718061da546Spatrick     PythonObject key = keys.GetItemAtIndex(i);
719061da546Spatrick     PythonObject value = GetItemForKey(key);
720061da546Spatrick     StructuredData::ObjectSP structured_value = value.CreateStructuredObject();
721061da546Spatrick     result->AddItem(key.Str().GetString(), structured_value);
722061da546Spatrick   }
723061da546Spatrick   return result;
724061da546Spatrick }
725061da546Spatrick 
BuiltinsModule()726*f6aab3d8Srobert PythonModule PythonModule::BuiltinsModule() { return AddModule("builtins"); }
727061da546Spatrick 
MainModule()728061da546Spatrick PythonModule PythonModule::MainModule() { return AddModule("__main__"); }
729061da546Spatrick 
AddModule(llvm::StringRef module)730061da546Spatrick PythonModule PythonModule::AddModule(llvm::StringRef module) {
731061da546Spatrick   std::string str = module.str();
732061da546Spatrick   return PythonModule(PyRefType::Borrowed, PyImport_AddModule(str.c_str()));
733061da546Spatrick }
734061da546Spatrick 
Import(const Twine & name)735061da546Spatrick Expected<PythonModule> PythonModule::Import(const Twine &name) {
736061da546Spatrick   PyObject *mod = PyImport_ImportModule(NullTerminated(name));
737061da546Spatrick   if (!mod)
738061da546Spatrick     return exception();
739061da546Spatrick   return Take<PythonModule>(mod);
740061da546Spatrick }
741061da546Spatrick 
Get(const Twine & name)742061da546Spatrick Expected<PythonObject> PythonModule::Get(const Twine &name) {
743061da546Spatrick   if (!IsValid())
744061da546Spatrick     return nullDeref();
745061da546Spatrick   PyObject *dict = PyModule_GetDict(m_py_obj);
746061da546Spatrick   if (!dict)
747061da546Spatrick     return exception();
748061da546Spatrick   PyObject *item = PyDict_GetItemString(dict, NullTerminated(name));
749061da546Spatrick   if (!item)
750061da546Spatrick     return exception();
751061da546Spatrick   return Retain<PythonObject>(item);
752061da546Spatrick }
753061da546Spatrick 
Check(PyObject * py_obj)754061da546Spatrick bool PythonModule::Check(PyObject *py_obj) {
755061da546Spatrick   if (!py_obj)
756061da546Spatrick     return false;
757061da546Spatrick 
758061da546Spatrick   return PyModule_Check(py_obj);
759061da546Spatrick }
760061da546Spatrick 
GetDictionary() const761061da546Spatrick PythonDictionary PythonModule::GetDictionary() const {
762061da546Spatrick   if (!IsValid())
763061da546Spatrick     return PythonDictionary();
764061da546Spatrick   return Retain<PythonDictionary>(PyModule_GetDict(m_py_obj));
765061da546Spatrick }
766061da546Spatrick 
Check(PyObject * py_obj)767061da546Spatrick bool PythonCallable::Check(PyObject *py_obj) {
768061da546Spatrick   if (!py_obj)
769061da546Spatrick     return false;
770061da546Spatrick 
771061da546Spatrick   return PyCallable_Check(py_obj);
772061da546Spatrick }
773061da546Spatrick 
774061da546Spatrick #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
775061da546Spatrick static const char get_arg_info_script[] = R"(
776061da546Spatrick from inspect import signature, Parameter, ismethod
777061da546Spatrick from collections import namedtuple
778061da546Spatrick ArgInfo = namedtuple('ArgInfo', ['count', 'has_varargs'])
779061da546Spatrick def main(f):
780061da546Spatrick     count = 0
781061da546Spatrick     varargs = False
782061da546Spatrick     for parameter in signature(f).parameters.values():
783061da546Spatrick         kind = parameter.kind
784061da546Spatrick         if kind in (Parameter.POSITIONAL_ONLY,
785061da546Spatrick                     Parameter.POSITIONAL_OR_KEYWORD):
786061da546Spatrick             count += 1
787061da546Spatrick         elif kind == Parameter.VAR_POSITIONAL:
788061da546Spatrick             varargs = True
789061da546Spatrick         elif kind in (Parameter.KEYWORD_ONLY,
790061da546Spatrick                       Parameter.VAR_KEYWORD):
791061da546Spatrick             pass
792061da546Spatrick         else:
793061da546Spatrick             raise Exception(f'unknown parameter kind: {kind}')
794061da546Spatrick     return ArgInfo(count, varargs)
795061da546Spatrick )";
796061da546Spatrick #endif
797061da546Spatrick 
GetArgInfo() const798061da546Spatrick Expected<PythonCallable::ArgInfo> PythonCallable::GetArgInfo() const {
799061da546Spatrick   ArgInfo result = {};
800061da546Spatrick   if (!IsValid())
801061da546Spatrick     return nullDeref();
802061da546Spatrick 
803061da546Spatrick #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
804061da546Spatrick 
805061da546Spatrick   // no need to synchronize access to this global, we already have the GIL
806061da546Spatrick   static PythonScript get_arg_info(get_arg_info_script);
807061da546Spatrick   Expected<PythonObject> pyarginfo = get_arg_info(*this);
808061da546Spatrick   if (!pyarginfo)
809061da546Spatrick     return pyarginfo.takeError();
810061da546Spatrick   long long count =
811061da546Spatrick       cantFail(As<long long>(pyarginfo.get().GetAttribute("count")));
812061da546Spatrick   bool has_varargs =
813061da546Spatrick       cantFail(As<bool>(pyarginfo.get().GetAttribute("has_varargs")));
814061da546Spatrick   result.max_positional_args = has_varargs ? ArgInfo::UNBOUNDED : count;
815061da546Spatrick 
816061da546Spatrick #else
817061da546Spatrick   PyObject *py_func_obj;
818061da546Spatrick   bool is_bound_method = false;
819061da546Spatrick   bool is_class = false;
820061da546Spatrick 
821061da546Spatrick   if (PyType_Check(m_py_obj) || PyClass_Check(m_py_obj)) {
822061da546Spatrick     auto init = GetAttribute("__init__");
823061da546Spatrick     if (!init)
824061da546Spatrick       return init.takeError();
825061da546Spatrick     py_func_obj = init.get().get();
826061da546Spatrick     is_class = true;
827061da546Spatrick   } else {
828061da546Spatrick     py_func_obj = m_py_obj;
829061da546Spatrick   }
830061da546Spatrick 
831061da546Spatrick   if (PyMethod_Check(py_func_obj)) {
832061da546Spatrick     py_func_obj = PyMethod_GET_FUNCTION(py_func_obj);
833061da546Spatrick     PythonObject im_self = GetAttributeValue("im_self");
834061da546Spatrick     if (im_self.IsValid() && !im_self.IsNone())
835061da546Spatrick       is_bound_method = true;
836061da546Spatrick   } else {
837061da546Spatrick     // see if this is a callable object with an __call__ method
838061da546Spatrick     if (!PyFunction_Check(py_func_obj)) {
839061da546Spatrick       PythonObject __call__ = GetAttributeValue("__call__");
840061da546Spatrick       if (__call__.IsValid()) {
841061da546Spatrick         auto __callable__ = __call__.AsType<PythonCallable>();
842061da546Spatrick         if (__callable__.IsValid()) {
843061da546Spatrick           py_func_obj = PyMethod_GET_FUNCTION(__callable__.get());
844061da546Spatrick           PythonObject im_self = __callable__.GetAttributeValue("im_self");
845061da546Spatrick           if (im_self.IsValid() && !im_self.IsNone())
846061da546Spatrick             is_bound_method = true;
847061da546Spatrick         }
848061da546Spatrick       }
849061da546Spatrick     }
850061da546Spatrick   }
851061da546Spatrick 
852061da546Spatrick   if (!py_func_obj)
853061da546Spatrick     return result;
854061da546Spatrick 
855061da546Spatrick   PyCodeObject *code = (PyCodeObject *)PyFunction_GET_CODE(py_func_obj);
856061da546Spatrick   if (!code)
857061da546Spatrick     return result;
858061da546Spatrick 
859061da546Spatrick   auto count = code->co_argcount;
860061da546Spatrick   bool has_varargs = !!(code->co_flags & CO_VARARGS);
861061da546Spatrick   result.max_positional_args =
862061da546Spatrick       has_varargs ? ArgInfo::UNBOUNDED
863061da546Spatrick                   : (count - (int)is_bound_method) - (int)is_class;
864061da546Spatrick 
865061da546Spatrick #endif
866061da546Spatrick 
867061da546Spatrick   return result;
868061da546Spatrick }
869061da546Spatrick 
870061da546Spatrick constexpr unsigned
871061da546Spatrick     PythonCallable::ArgInfo::UNBOUNDED; // FIXME delete after c++17
872061da546Spatrick 
operator ()()873061da546Spatrick PythonObject PythonCallable::operator()() {
874061da546Spatrick   return PythonObject(PyRefType::Owned, PyObject_CallObject(m_py_obj, nullptr));
875061da546Spatrick }
876061da546Spatrick 
877061da546Spatrick PythonObject PythonCallable::
operator ()(std::initializer_list<PyObject * > args)878061da546Spatrick operator()(std::initializer_list<PyObject *> args) {
879061da546Spatrick   PythonTuple arg_tuple(args);
880061da546Spatrick   return PythonObject(PyRefType::Owned,
881061da546Spatrick                       PyObject_CallObject(m_py_obj, arg_tuple.get()));
882061da546Spatrick }
883061da546Spatrick 
884061da546Spatrick PythonObject PythonCallable::
operator ()(std::initializer_list<PythonObject> args)885061da546Spatrick operator()(std::initializer_list<PythonObject> args) {
886061da546Spatrick   PythonTuple arg_tuple(args);
887061da546Spatrick   return PythonObject(PyRefType::Owned,
888061da546Spatrick                       PyObject_CallObject(m_py_obj, arg_tuple.get()));
889061da546Spatrick }
890061da546Spatrick 
Check(PyObject * py_obj)891061da546Spatrick bool PythonFile::Check(PyObject *py_obj) {
892061da546Spatrick   if (!py_obj)
893061da546Spatrick     return false;
894061da546Spatrick   // In Python 3, there is no `PyFile_Check`, and in fact PyFile is not even a
895061da546Spatrick   // first-class object type anymore.  `PyFile_FromFd` is just a thin wrapper
896061da546Spatrick   // over `io.open()`, which returns some object derived from `io.IOBase`. As a
897061da546Spatrick   // result, the only way to detect a file in Python 3 is to check whether it
898061da546Spatrick   // inherits from `io.IOBase`.
899061da546Spatrick   auto io_module = PythonModule::Import("io");
900061da546Spatrick   if (!io_module) {
901061da546Spatrick     llvm::consumeError(io_module.takeError());
902061da546Spatrick     return false;
903061da546Spatrick   }
904061da546Spatrick   auto iobase = io_module.get().Get("IOBase");
905061da546Spatrick   if (!iobase) {
906061da546Spatrick     llvm::consumeError(iobase.takeError());
907061da546Spatrick     return false;
908061da546Spatrick   }
909061da546Spatrick   int r = PyObject_IsInstance(py_obj, iobase.get().get());
910061da546Spatrick   if (r < 0) {
911061da546Spatrick     llvm::consumeError(exception()); // clear the exception and log it.
912061da546Spatrick     return false;
913061da546Spatrick   }
914061da546Spatrick   return !!r;
915061da546Spatrick }
916061da546Spatrick 
toCString() const917061da546Spatrick const char *PythonException::toCString() const {
918061da546Spatrick   if (!m_repr_bytes)
919061da546Spatrick     return "unknown exception";
920061da546Spatrick   return PyBytes_AS_STRING(m_repr_bytes);
921061da546Spatrick }
922061da546Spatrick 
PythonException(const char * caller)923061da546Spatrick PythonException::PythonException(const char *caller) {
924061da546Spatrick   assert(PyErr_Occurred());
925*f6aab3d8Srobert   m_exception_type = m_exception = m_traceback = m_repr_bytes = nullptr;
926061da546Spatrick   PyErr_Fetch(&m_exception_type, &m_exception, &m_traceback);
927061da546Spatrick   PyErr_NormalizeException(&m_exception_type, &m_exception, &m_traceback);
928061da546Spatrick   PyErr_Clear();
929061da546Spatrick   if (m_exception) {
930061da546Spatrick     PyObject *repr = PyObject_Repr(m_exception);
931061da546Spatrick     if (repr) {
932061da546Spatrick       m_repr_bytes = PyUnicode_AsEncodedString(repr, "utf-8", nullptr);
933061da546Spatrick       if (!m_repr_bytes) {
934061da546Spatrick         PyErr_Clear();
935061da546Spatrick       }
936061da546Spatrick       Py_XDECREF(repr);
937061da546Spatrick     } else {
938061da546Spatrick       PyErr_Clear();
939061da546Spatrick     }
940061da546Spatrick   }
941*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::Script);
942061da546Spatrick   if (caller)
943061da546Spatrick     LLDB_LOGF(log, "%s failed with exception: %s", caller, toCString());
944061da546Spatrick   else
945061da546Spatrick     LLDB_LOGF(log, "python exception: %s", toCString());
946061da546Spatrick }
Restore()947061da546Spatrick void PythonException::Restore() {
948061da546Spatrick   if (m_exception_type && m_exception) {
949061da546Spatrick     PyErr_Restore(m_exception_type, m_exception, m_traceback);
950061da546Spatrick   } else {
951061da546Spatrick     PyErr_SetString(PyExc_Exception, toCString());
952061da546Spatrick   }
953*f6aab3d8Srobert   m_exception_type = m_exception = m_traceback = nullptr;
954061da546Spatrick }
955061da546Spatrick 
~PythonException()956061da546Spatrick PythonException::~PythonException() {
957061da546Spatrick   Py_XDECREF(m_exception_type);
958061da546Spatrick   Py_XDECREF(m_exception);
959061da546Spatrick   Py_XDECREF(m_traceback);
960061da546Spatrick   Py_XDECREF(m_repr_bytes);
961061da546Spatrick }
962061da546Spatrick 
log(llvm::raw_ostream & OS) const963061da546Spatrick void PythonException::log(llvm::raw_ostream &OS) const { OS << toCString(); }
964061da546Spatrick 
convertToErrorCode() const965061da546Spatrick std::error_code PythonException::convertToErrorCode() const {
966061da546Spatrick   return llvm::inconvertibleErrorCode();
967061da546Spatrick }
968061da546Spatrick 
Matches(PyObject * exc) const969061da546Spatrick bool PythonException::Matches(PyObject *exc) const {
970061da546Spatrick   return PyErr_GivenExceptionMatches(m_exception_type, exc);
971061da546Spatrick }
972061da546Spatrick 
973061da546Spatrick const char read_exception_script[] = R"(
974061da546Spatrick import sys
975061da546Spatrick from traceback import print_exception
976061da546Spatrick if sys.version_info.major < 3:
977061da546Spatrick   from StringIO import StringIO
978061da546Spatrick else:
979061da546Spatrick   from io import StringIO
980061da546Spatrick def main(exc_type, exc_value, tb):
981061da546Spatrick   f = StringIO()
982061da546Spatrick   print_exception(exc_type, exc_value, tb, file=f)
983061da546Spatrick   return f.getvalue()
984061da546Spatrick )";
985061da546Spatrick 
ReadBacktrace() const986061da546Spatrick std::string PythonException::ReadBacktrace() const {
987061da546Spatrick 
988061da546Spatrick   if (!m_traceback)
989061da546Spatrick     return toCString();
990061da546Spatrick 
991061da546Spatrick   // no need to synchronize access to this global, we already have the GIL
992061da546Spatrick   static PythonScript read_exception(read_exception_script);
993061da546Spatrick 
994061da546Spatrick   Expected<std::string> backtrace = As<std::string>(
995061da546Spatrick       read_exception(m_exception_type, m_exception, m_traceback));
996061da546Spatrick 
997061da546Spatrick   if (!backtrace) {
998061da546Spatrick     std::string message =
999061da546Spatrick         std::string(toCString()) + "\n" +
1000dda28197Spatrick         "Traceback unavailable, an error occurred while reading it:\n";
1001061da546Spatrick     return (message + llvm::toString(backtrace.takeError()));
1002061da546Spatrick   }
1003061da546Spatrick 
1004061da546Spatrick   return std::move(backtrace.get());
1005061da546Spatrick }
1006061da546Spatrick 
1007061da546Spatrick char PythonException::ID = 0;
1008061da546Spatrick 
1009061da546Spatrick llvm::Expected<File::OpenOptions>
GetOptionsForPyObject(const PythonObject & obj)1010061da546Spatrick GetOptionsForPyObject(const PythonObject &obj) {
1011061da546Spatrick   auto options = File::OpenOptions(0);
1012061da546Spatrick   auto readable = As<bool>(obj.CallMethod("readable"));
1013061da546Spatrick   if (!readable)
1014061da546Spatrick     return readable.takeError();
1015061da546Spatrick   auto writable = As<bool>(obj.CallMethod("writable"));
1016061da546Spatrick   if (!writable)
1017061da546Spatrick     return writable.takeError();
1018*f6aab3d8Srobert   if (readable.get() && writable.get())
1019*f6aab3d8Srobert     options |= File::eOpenOptionReadWrite;
1020*f6aab3d8Srobert   else if (writable.get())
1021*f6aab3d8Srobert     options |= File::eOpenOptionWriteOnly;
1022*f6aab3d8Srobert   else if (readable.get())
1023*f6aab3d8Srobert     options |= File::eOpenOptionReadOnly;
1024061da546Spatrick   return options;
1025061da546Spatrick }
1026061da546Spatrick 
1027061da546Spatrick // Base class template for python files.   All it knows how to do
1028061da546Spatrick // is hold a reference to the python object and close or flush it
1029061da546Spatrick // when the File is closed.
1030061da546Spatrick namespace {
1031061da546Spatrick template <typename Base> class OwnedPythonFile : public Base {
1032061da546Spatrick public:
1033061da546Spatrick   template <typename... Args>
OwnedPythonFile(const PythonFile & file,bool borrowed,Args...args)1034061da546Spatrick   OwnedPythonFile(const PythonFile &file, bool borrowed, Args... args)
1035061da546Spatrick       : Base(args...), m_py_obj(file), m_borrowed(borrowed) {
1036061da546Spatrick     assert(m_py_obj);
1037061da546Spatrick   }
1038061da546Spatrick 
~OwnedPythonFile()1039061da546Spatrick   ~OwnedPythonFile() override {
1040061da546Spatrick     assert(m_py_obj);
1041061da546Spatrick     GIL takeGIL;
1042061da546Spatrick     Close();
1043061da546Spatrick     // we need to ensure the python object is released while we still
1044061da546Spatrick     // hold the GIL
1045061da546Spatrick     m_py_obj.Reset();
1046061da546Spatrick   }
1047061da546Spatrick 
IsPythonSideValid() const1048061da546Spatrick   bool IsPythonSideValid() const {
1049061da546Spatrick     GIL takeGIL;
1050061da546Spatrick     auto closed = As<bool>(m_py_obj.GetAttribute("closed"));
1051061da546Spatrick     if (!closed) {
1052061da546Spatrick       llvm::consumeError(closed.takeError());
1053061da546Spatrick       return false;
1054061da546Spatrick     }
1055061da546Spatrick     return !closed.get();
1056061da546Spatrick   }
1057061da546Spatrick 
IsValid() const1058061da546Spatrick   bool IsValid() const override {
1059061da546Spatrick     return IsPythonSideValid() && Base::IsValid();
1060061da546Spatrick   }
1061061da546Spatrick 
Close()1062061da546Spatrick   Status Close() override {
1063061da546Spatrick     assert(m_py_obj);
1064061da546Spatrick     Status py_error, base_error;
1065061da546Spatrick     GIL takeGIL;
1066061da546Spatrick     if (!m_borrowed) {
1067061da546Spatrick       auto r = m_py_obj.CallMethod("close");
1068061da546Spatrick       if (!r)
1069061da546Spatrick         py_error = Status(r.takeError());
1070061da546Spatrick     }
1071061da546Spatrick     base_error = Base::Close();
1072061da546Spatrick     if (py_error.Fail())
1073061da546Spatrick       return py_error;
1074061da546Spatrick     return base_error;
1075061da546Spatrick   };
1076061da546Spatrick 
GetPythonObject() const1077061da546Spatrick   PyObject *GetPythonObject() const {
1078061da546Spatrick     assert(m_py_obj.IsValid());
1079061da546Spatrick     return m_py_obj.get();
1080061da546Spatrick   }
1081061da546Spatrick 
1082061da546Spatrick   static bool classof(const File *file) = delete;
1083061da546Spatrick 
1084061da546Spatrick protected:
1085061da546Spatrick   PythonFile m_py_obj;
1086061da546Spatrick   bool m_borrowed;
1087061da546Spatrick };
1088061da546Spatrick } // namespace
1089061da546Spatrick 
1090061da546Spatrick // A SimplePythonFile is a OwnedPythonFile that just does all I/O as
1091061da546Spatrick // a NativeFile
1092061da546Spatrick namespace {
1093061da546Spatrick class SimplePythonFile : public OwnedPythonFile<NativeFile> {
1094061da546Spatrick public:
SimplePythonFile(const PythonFile & file,bool borrowed,int fd,File::OpenOptions options)1095061da546Spatrick   SimplePythonFile(const PythonFile &file, bool borrowed, int fd,
1096061da546Spatrick                    File::OpenOptions options)
1097061da546Spatrick       : OwnedPythonFile(file, borrowed, fd, options, false) {}
1098061da546Spatrick 
1099061da546Spatrick   static char ID;
isA(const void * classID) const1100061da546Spatrick   bool isA(const void *classID) const override {
1101061da546Spatrick     return classID == &ID || NativeFile::isA(classID);
1102061da546Spatrick   }
classof(const File * file)1103061da546Spatrick   static bool classof(const File *file) { return file->isA(&ID); }
1104061da546Spatrick };
1105061da546Spatrick char SimplePythonFile::ID = 0;
1106061da546Spatrick } // namespace
1107061da546Spatrick 
1108061da546Spatrick namespace {
1109061da546Spatrick class PythonBuffer {
1110061da546Spatrick public:
1111061da546Spatrick   PythonBuffer &operator=(const PythonBuffer &) = delete;
1112061da546Spatrick   PythonBuffer(const PythonBuffer &) = delete;
1113061da546Spatrick 
Create(PythonObject & obj,int flags=PyBUF_SIMPLE)1114061da546Spatrick   static Expected<PythonBuffer> Create(PythonObject &obj,
1115061da546Spatrick                                        int flags = PyBUF_SIMPLE) {
1116061da546Spatrick     Py_buffer py_buffer = {};
1117061da546Spatrick     PyObject_GetBuffer(obj.get(), &py_buffer, flags);
1118061da546Spatrick     if (!py_buffer.obj)
1119061da546Spatrick       return llvm::make_error<PythonException>();
1120061da546Spatrick     return PythonBuffer(py_buffer);
1121061da546Spatrick   }
1122061da546Spatrick 
PythonBuffer(PythonBuffer && other)1123061da546Spatrick   PythonBuffer(PythonBuffer &&other) {
1124061da546Spatrick     m_buffer = other.m_buffer;
1125061da546Spatrick     other.m_buffer.obj = nullptr;
1126061da546Spatrick   }
1127061da546Spatrick 
~PythonBuffer()1128061da546Spatrick   ~PythonBuffer() {
1129061da546Spatrick     if (m_buffer.obj)
1130061da546Spatrick       PyBuffer_Release(&m_buffer);
1131061da546Spatrick   }
1132061da546Spatrick 
get()1133061da546Spatrick   Py_buffer &get() { return m_buffer; }
1134061da546Spatrick 
1135061da546Spatrick private:
1136061da546Spatrick   // takes ownership of the buffer.
PythonBuffer(const Py_buffer & py_buffer)1137061da546Spatrick   PythonBuffer(const Py_buffer &py_buffer) : m_buffer(py_buffer) {}
1138061da546Spatrick   Py_buffer m_buffer;
1139061da546Spatrick };
1140061da546Spatrick } // namespace
1141061da546Spatrick 
1142061da546Spatrick // Shared methods between TextPythonFile and BinaryPythonFile
1143061da546Spatrick namespace {
1144061da546Spatrick class PythonIOFile : public OwnedPythonFile<File> {
1145061da546Spatrick public:
PythonIOFile(const PythonFile & file,bool borrowed)1146061da546Spatrick   PythonIOFile(const PythonFile &file, bool borrowed)
1147061da546Spatrick       : OwnedPythonFile(file, borrowed) {}
1148061da546Spatrick 
~PythonIOFile()1149061da546Spatrick   ~PythonIOFile() override { Close(); }
1150061da546Spatrick 
IsValid() const1151061da546Spatrick   bool IsValid() const override { return IsPythonSideValid(); }
1152061da546Spatrick 
Close()1153061da546Spatrick   Status Close() override {
1154061da546Spatrick     assert(m_py_obj);
1155061da546Spatrick     GIL takeGIL;
1156061da546Spatrick     if (m_borrowed)
1157061da546Spatrick       return Flush();
1158061da546Spatrick     auto r = m_py_obj.CallMethod("close");
1159061da546Spatrick     if (!r)
1160061da546Spatrick       return Status(r.takeError());
1161061da546Spatrick     return Status();
1162061da546Spatrick   }
1163061da546Spatrick 
Flush()1164061da546Spatrick   Status Flush() override {
1165061da546Spatrick     GIL takeGIL;
1166061da546Spatrick     auto r = m_py_obj.CallMethod("flush");
1167061da546Spatrick     if (!r)
1168061da546Spatrick       return Status(r.takeError());
1169061da546Spatrick     return Status();
1170061da546Spatrick   }
1171061da546Spatrick 
GetOptions() const1172061da546Spatrick   Expected<File::OpenOptions> GetOptions() const override {
1173061da546Spatrick     GIL takeGIL;
1174061da546Spatrick     return GetOptionsForPyObject(m_py_obj);
1175061da546Spatrick   }
1176061da546Spatrick 
1177061da546Spatrick   static char ID;
isA(const void * classID) const1178061da546Spatrick   bool isA(const void *classID) const override {
1179061da546Spatrick     return classID == &ID || File::isA(classID);
1180061da546Spatrick   }
classof(const File * file)1181061da546Spatrick   static bool classof(const File *file) { return file->isA(&ID); }
1182061da546Spatrick };
1183061da546Spatrick char PythonIOFile::ID = 0;
1184061da546Spatrick } // namespace
1185061da546Spatrick 
1186061da546Spatrick namespace {
1187061da546Spatrick class BinaryPythonFile : public PythonIOFile {
1188061da546Spatrick protected:
1189061da546Spatrick   int m_descriptor;
1190061da546Spatrick 
1191061da546Spatrick public:
BinaryPythonFile(int fd,const PythonFile & file,bool borrowed)1192061da546Spatrick   BinaryPythonFile(int fd, const PythonFile &file, bool borrowed)
1193061da546Spatrick       : PythonIOFile(file, borrowed),
1194061da546Spatrick         m_descriptor(File::DescriptorIsValid(fd) ? fd
1195061da546Spatrick                                                  : File::kInvalidDescriptor) {}
1196061da546Spatrick 
GetDescriptor() const1197061da546Spatrick   int GetDescriptor() const override { return m_descriptor; }
1198061da546Spatrick 
Write(const void * buf,size_t & num_bytes)1199061da546Spatrick   Status Write(const void *buf, size_t &num_bytes) override {
1200061da546Spatrick     GIL takeGIL;
1201061da546Spatrick     PyObject *pybuffer_p = PyMemoryView_FromMemory(
1202061da546Spatrick         const_cast<char *>((const char *)buf), num_bytes, PyBUF_READ);
1203061da546Spatrick     if (!pybuffer_p)
1204061da546Spatrick       return Status(llvm::make_error<PythonException>());
1205061da546Spatrick     auto pybuffer = Take<PythonObject>(pybuffer_p);
1206061da546Spatrick     num_bytes = 0;
1207061da546Spatrick     auto bytes_written = As<long long>(m_py_obj.CallMethod("write", pybuffer));
1208061da546Spatrick     if (!bytes_written)
1209061da546Spatrick       return Status(bytes_written.takeError());
1210061da546Spatrick     if (bytes_written.get() < 0)
1211061da546Spatrick       return Status(".write() method returned a negative number!");
1212061da546Spatrick     static_assert(sizeof(long long) >= sizeof(size_t), "overflow");
1213061da546Spatrick     num_bytes = bytes_written.get();
1214061da546Spatrick     return Status();
1215061da546Spatrick   }
1216061da546Spatrick 
Read(void * buf,size_t & num_bytes)1217061da546Spatrick   Status Read(void *buf, size_t &num_bytes) override {
1218061da546Spatrick     GIL takeGIL;
1219061da546Spatrick     static_assert(sizeof(long long) >= sizeof(size_t), "overflow");
1220061da546Spatrick     auto pybuffer_obj =
1221061da546Spatrick         m_py_obj.CallMethod("read", (unsigned long long)num_bytes);
1222061da546Spatrick     if (!pybuffer_obj)
1223061da546Spatrick       return Status(pybuffer_obj.takeError());
1224061da546Spatrick     num_bytes = 0;
1225061da546Spatrick     if (pybuffer_obj.get().IsNone()) {
1226061da546Spatrick       // EOF
1227061da546Spatrick       num_bytes = 0;
1228061da546Spatrick       return Status();
1229061da546Spatrick     }
1230061da546Spatrick     auto pybuffer = PythonBuffer::Create(pybuffer_obj.get());
1231061da546Spatrick     if (!pybuffer)
1232061da546Spatrick       return Status(pybuffer.takeError());
1233061da546Spatrick     memcpy(buf, pybuffer.get().get().buf, pybuffer.get().get().len);
1234061da546Spatrick     num_bytes = pybuffer.get().get().len;
1235061da546Spatrick     return Status();
1236061da546Spatrick   }
1237061da546Spatrick };
1238061da546Spatrick } // namespace
1239061da546Spatrick 
1240061da546Spatrick namespace {
1241061da546Spatrick class TextPythonFile : public PythonIOFile {
1242061da546Spatrick protected:
1243061da546Spatrick   int m_descriptor;
1244061da546Spatrick 
1245061da546Spatrick public:
TextPythonFile(int fd,const PythonFile & file,bool borrowed)1246061da546Spatrick   TextPythonFile(int fd, const PythonFile &file, bool borrowed)
1247061da546Spatrick       : PythonIOFile(file, borrowed),
1248061da546Spatrick         m_descriptor(File::DescriptorIsValid(fd) ? fd
1249061da546Spatrick                                                  : File::kInvalidDescriptor) {}
1250061da546Spatrick 
GetDescriptor() const1251061da546Spatrick   int GetDescriptor() const override { return m_descriptor; }
1252061da546Spatrick 
Write(const void * buf,size_t & num_bytes)1253061da546Spatrick   Status Write(const void *buf, size_t &num_bytes) override {
1254061da546Spatrick     GIL takeGIL;
1255061da546Spatrick     auto pystring =
1256061da546Spatrick         PythonString::FromUTF8(llvm::StringRef((const char *)buf, num_bytes));
1257061da546Spatrick     if (!pystring)
1258061da546Spatrick       return Status(pystring.takeError());
1259061da546Spatrick     num_bytes = 0;
1260061da546Spatrick     auto bytes_written =
1261061da546Spatrick         As<long long>(m_py_obj.CallMethod("write", pystring.get()));
1262061da546Spatrick     if (!bytes_written)
1263061da546Spatrick       return Status(bytes_written.takeError());
1264061da546Spatrick     if (bytes_written.get() < 0)
1265061da546Spatrick       return Status(".write() method returned a negative number!");
1266061da546Spatrick     static_assert(sizeof(long long) >= sizeof(size_t), "overflow");
1267061da546Spatrick     num_bytes = bytes_written.get();
1268061da546Spatrick     return Status();
1269061da546Spatrick   }
1270061da546Spatrick 
Read(void * buf,size_t & num_bytes)1271061da546Spatrick   Status Read(void *buf, size_t &num_bytes) override {
1272061da546Spatrick     GIL takeGIL;
1273061da546Spatrick     size_t num_chars = num_bytes / 6;
1274061da546Spatrick     size_t orig_num_bytes = num_bytes;
1275061da546Spatrick     num_bytes = 0;
1276061da546Spatrick     if (orig_num_bytes < 6) {
1277061da546Spatrick       return Status("can't read less than 6 bytes from a utf8 text stream");
1278061da546Spatrick     }
1279061da546Spatrick     auto pystring = As<PythonString>(
1280061da546Spatrick         m_py_obj.CallMethod("read", (unsigned long long)num_chars));
1281061da546Spatrick     if (!pystring)
1282061da546Spatrick       return Status(pystring.takeError());
1283061da546Spatrick     if (pystring.get().IsNone()) {
1284061da546Spatrick       // EOF
1285061da546Spatrick       return Status();
1286061da546Spatrick     }
1287061da546Spatrick     auto stringref = pystring.get().AsUTF8();
1288061da546Spatrick     if (!stringref)
1289061da546Spatrick       return Status(stringref.takeError());
1290061da546Spatrick     num_bytes = stringref.get().size();
1291061da546Spatrick     memcpy(buf, stringref.get().begin(), num_bytes);
1292061da546Spatrick     return Status();
1293061da546Spatrick   }
1294061da546Spatrick };
1295061da546Spatrick } // namespace
1296061da546Spatrick 
ConvertToFile(bool borrowed)1297061da546Spatrick llvm::Expected<FileSP> PythonFile::ConvertToFile(bool borrowed) {
1298061da546Spatrick   if (!IsValid())
1299061da546Spatrick     return llvm::createStringError(llvm::inconvertibleErrorCode(),
1300061da546Spatrick                                    "invalid PythonFile");
1301061da546Spatrick 
1302061da546Spatrick   int fd = PyObject_AsFileDescriptor(m_py_obj);
1303061da546Spatrick   if (fd < 0) {
1304061da546Spatrick     PyErr_Clear();
1305061da546Spatrick     return ConvertToFileForcingUseOfScriptingIOMethods(borrowed);
1306061da546Spatrick   }
1307061da546Spatrick   auto options = GetOptionsForPyObject(*this);
1308061da546Spatrick   if (!options)
1309061da546Spatrick     return options.takeError();
1310061da546Spatrick 
1311*f6aab3d8Srobert   File::OpenOptions rw =
1312*f6aab3d8Srobert       options.get() & (File::eOpenOptionReadOnly | File::eOpenOptionWriteOnly |
1313*f6aab3d8Srobert                        File::eOpenOptionReadWrite);
1314*f6aab3d8Srobert   if (rw == File::eOpenOptionWriteOnly || rw == File::eOpenOptionReadWrite) {
1315061da546Spatrick     // LLDB and python will not share I/O buffers.  We should probably
1316061da546Spatrick     // flush the python buffers now.
1317061da546Spatrick     auto r = CallMethod("flush");
1318061da546Spatrick     if (!r)
1319061da546Spatrick       return r.takeError();
1320061da546Spatrick   }
1321061da546Spatrick 
1322061da546Spatrick   FileSP file_sp;
1323061da546Spatrick   if (borrowed) {
1324061da546Spatrick     // In this case we we don't need to retain the python
1325061da546Spatrick     // object at all.
1326061da546Spatrick     file_sp = std::make_shared<NativeFile>(fd, options.get(), false);
1327061da546Spatrick   } else {
1328061da546Spatrick     file_sp = std::static_pointer_cast<File>(
1329061da546Spatrick         std::make_shared<SimplePythonFile>(*this, borrowed, fd, options.get()));
1330061da546Spatrick   }
1331061da546Spatrick   if (!file_sp->IsValid())
1332061da546Spatrick     return llvm::createStringError(llvm::inconvertibleErrorCode(),
1333061da546Spatrick                                    "invalid File");
1334061da546Spatrick 
1335061da546Spatrick   return file_sp;
1336061da546Spatrick }
1337061da546Spatrick 
1338061da546Spatrick llvm::Expected<FileSP>
ConvertToFileForcingUseOfScriptingIOMethods(bool borrowed)1339061da546Spatrick PythonFile::ConvertToFileForcingUseOfScriptingIOMethods(bool borrowed) {
1340061da546Spatrick 
1341061da546Spatrick   assert(!PyErr_Occurred());
1342061da546Spatrick 
1343061da546Spatrick   if (!IsValid())
1344061da546Spatrick     return llvm::createStringError(llvm::inconvertibleErrorCode(),
1345061da546Spatrick                                    "invalid PythonFile");
1346061da546Spatrick 
1347061da546Spatrick   int fd = PyObject_AsFileDescriptor(m_py_obj);
1348061da546Spatrick   if (fd < 0) {
1349061da546Spatrick     PyErr_Clear();
1350061da546Spatrick     fd = File::kInvalidDescriptor;
1351061da546Spatrick   }
1352061da546Spatrick 
1353061da546Spatrick   auto io_module = PythonModule::Import("io");
1354061da546Spatrick   if (!io_module)
1355061da546Spatrick     return io_module.takeError();
1356061da546Spatrick   auto textIOBase = io_module.get().Get("TextIOBase");
1357061da546Spatrick   if (!textIOBase)
1358061da546Spatrick     return textIOBase.takeError();
1359061da546Spatrick   auto rawIOBase = io_module.get().Get("RawIOBase");
1360061da546Spatrick   if (!rawIOBase)
1361061da546Spatrick     return rawIOBase.takeError();
1362061da546Spatrick   auto bufferedIOBase = io_module.get().Get("BufferedIOBase");
1363061da546Spatrick   if (!bufferedIOBase)
1364061da546Spatrick     return bufferedIOBase.takeError();
1365061da546Spatrick 
1366061da546Spatrick   FileSP file_sp;
1367061da546Spatrick 
1368061da546Spatrick   auto isTextIO = IsInstance(textIOBase.get());
1369061da546Spatrick   if (!isTextIO)
1370061da546Spatrick     return isTextIO.takeError();
1371061da546Spatrick   if (isTextIO.get())
1372061da546Spatrick     file_sp = std::static_pointer_cast<File>(
1373061da546Spatrick         std::make_shared<TextPythonFile>(fd, *this, borrowed));
1374061da546Spatrick 
1375061da546Spatrick   auto isRawIO = IsInstance(rawIOBase.get());
1376061da546Spatrick   if (!isRawIO)
1377061da546Spatrick     return isRawIO.takeError();
1378061da546Spatrick   auto isBufferedIO = IsInstance(bufferedIOBase.get());
1379061da546Spatrick   if (!isBufferedIO)
1380061da546Spatrick     return isBufferedIO.takeError();
1381061da546Spatrick 
1382061da546Spatrick   if (isRawIO.get() || isBufferedIO.get()) {
1383061da546Spatrick     file_sp = std::static_pointer_cast<File>(
1384061da546Spatrick         std::make_shared<BinaryPythonFile>(fd, *this, borrowed));
1385061da546Spatrick   }
1386061da546Spatrick 
1387061da546Spatrick   if (!file_sp)
1388061da546Spatrick     return llvm::createStringError(llvm::inconvertibleErrorCode(),
1389061da546Spatrick                                    "python file is neither text nor binary");
1390061da546Spatrick 
1391061da546Spatrick   if (!file_sp->IsValid())
1392061da546Spatrick     return llvm::createStringError(llvm::inconvertibleErrorCode(),
1393061da546Spatrick                                    "invalid File");
1394061da546Spatrick 
1395061da546Spatrick   return file_sp;
1396061da546Spatrick }
1397061da546Spatrick 
FromFile(File & file,const char * mode)1398061da546Spatrick Expected<PythonFile> PythonFile::FromFile(File &file, const char *mode) {
1399061da546Spatrick   if (!file.IsValid())
1400061da546Spatrick     return llvm::createStringError(llvm::inconvertibleErrorCode(),
1401061da546Spatrick                                    "invalid file");
1402061da546Spatrick 
1403061da546Spatrick   if (auto *simple = llvm::dyn_cast<SimplePythonFile>(&file))
1404061da546Spatrick     return Retain<PythonFile>(simple->GetPythonObject());
1405061da546Spatrick   if (auto *pythonio = llvm::dyn_cast<PythonIOFile>(&file))
1406061da546Spatrick     return Retain<PythonFile>(pythonio->GetPythonObject());
1407061da546Spatrick 
1408061da546Spatrick   if (!mode) {
1409061da546Spatrick     auto m = file.GetOpenMode();
1410061da546Spatrick     if (!m)
1411061da546Spatrick       return m.takeError();
1412061da546Spatrick     mode = m.get();
1413061da546Spatrick   }
1414061da546Spatrick 
1415061da546Spatrick   PyObject *file_obj;
1416061da546Spatrick   file_obj = PyFile_FromFd(file.GetDescriptor(), nullptr, mode, -1, nullptr,
1417061da546Spatrick                            "ignore", nullptr, /*closefd=*/0);
1418061da546Spatrick 
1419061da546Spatrick   if (!file_obj)
1420061da546Spatrick     return exception();
1421061da546Spatrick 
1422061da546Spatrick   return Take<PythonFile>(file_obj);
1423061da546Spatrick }
1424061da546Spatrick 
Init()1425061da546Spatrick Error PythonScript::Init() {
1426061da546Spatrick   if (function.IsValid())
1427061da546Spatrick     return Error::success();
1428061da546Spatrick 
1429061da546Spatrick   PythonDictionary globals(PyInitialValue::Empty);
1430061da546Spatrick   auto builtins = PythonModule::BuiltinsModule();
1431061da546Spatrick   if (Error error = globals.SetItem("__builtins__", builtins))
1432061da546Spatrick     return error;
1433061da546Spatrick   PyObject *o =
1434061da546Spatrick       PyRun_String(script, Py_file_input, globals.get(), globals.get());
1435061da546Spatrick   if (!o)
1436061da546Spatrick     return exception();
1437061da546Spatrick   Take<PythonObject>(o);
1438061da546Spatrick   auto f = As<PythonCallable>(globals.GetItem("main"));
1439061da546Spatrick   if (!f)
1440061da546Spatrick     return f.takeError();
1441061da546Spatrick   function = std::move(f.get());
1442061da546Spatrick 
1443061da546Spatrick   return Error::success();
1444061da546Spatrick }
1445061da546Spatrick 
1446061da546Spatrick llvm::Expected<PythonObject>
runStringOneLine(const llvm::Twine & string,const PythonDictionary & globals,const PythonDictionary & locals)1447061da546Spatrick python::runStringOneLine(const llvm::Twine &string,
1448061da546Spatrick                          const PythonDictionary &globals,
1449061da546Spatrick                          const PythonDictionary &locals) {
1450061da546Spatrick   if (!globals.IsValid() || !locals.IsValid())
1451061da546Spatrick     return nullDeref();
1452061da546Spatrick 
1453061da546Spatrick   PyObject *code =
1454061da546Spatrick       Py_CompileString(NullTerminated(string), "<string>", Py_eval_input);
1455061da546Spatrick   if (!code) {
1456061da546Spatrick     PyErr_Clear();
1457061da546Spatrick     code =
1458061da546Spatrick         Py_CompileString(NullTerminated(string), "<string>", Py_single_input);
1459061da546Spatrick   }
1460061da546Spatrick   if (!code)
1461061da546Spatrick     return exception();
1462061da546Spatrick   auto code_ref = Take<PythonObject>(code);
1463061da546Spatrick 
1464061da546Spatrick   PyObject *result = PyEval_EvalCode(code, globals.get(), locals.get());
1465061da546Spatrick 
1466061da546Spatrick   if (!result)
1467061da546Spatrick     return exception();
1468061da546Spatrick 
1469061da546Spatrick   return Take<PythonObject>(result);
1470061da546Spatrick }
1471061da546Spatrick 
1472061da546Spatrick llvm::Expected<PythonObject>
runStringMultiLine(const llvm::Twine & string,const PythonDictionary & globals,const PythonDictionary & locals)1473061da546Spatrick python::runStringMultiLine(const llvm::Twine &string,
1474061da546Spatrick                            const PythonDictionary &globals,
1475061da546Spatrick                            const PythonDictionary &locals) {
1476061da546Spatrick   if (!globals.IsValid() || !locals.IsValid())
1477061da546Spatrick     return nullDeref();
1478061da546Spatrick   PyObject *result = PyRun_String(NullTerminated(string), Py_file_input,
1479061da546Spatrick                                   globals.get(), locals.get());
1480061da546Spatrick   if (!result)
1481061da546Spatrick     return exception();
1482061da546Spatrick   return Take<PythonObject>(result);
1483061da546Spatrick }
1484061da546Spatrick 
1485061da546Spatrick #endif
1486