1*061da546Spatrick //===-- PythonDataObjects.cpp -----------------------------------*- C++ -*-===// 2*061da546Spatrick // 3*061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*061da546Spatrick // See https://llvm.org/LICENSE.txt for license information. 5*061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*061da546Spatrick // 7*061da546Spatrick //===----------------------------------------------------------------------===// 8*061da546Spatrick 9*061da546Spatrick #include "lldb/Host/Config.h" 10*061da546Spatrick 11*061da546Spatrick #if LLDB_ENABLE_PYTHON 12*061da546Spatrick 13*061da546Spatrick #include "PythonDataObjects.h" 14*061da546Spatrick #include "ScriptInterpreterPython.h" 15*061da546Spatrick 16*061da546Spatrick #include "lldb/Host/File.h" 17*061da546Spatrick #include "lldb/Host/FileSystem.h" 18*061da546Spatrick #include "lldb/Interpreter/ScriptInterpreter.h" 19*061da546Spatrick #include "lldb/Utility/Log.h" 20*061da546Spatrick #include "lldb/Utility/Stream.h" 21*061da546Spatrick 22*061da546Spatrick #include "llvm/ADT/StringSwitch.h" 23*061da546Spatrick #include "llvm/Support/Casting.h" 24*061da546Spatrick #include "llvm/Support/ConvertUTF.h" 25*061da546Spatrick #include "llvm/Support/Errno.h" 26*061da546Spatrick 27*061da546Spatrick #include <stdio.h> 28*061da546Spatrick 29*061da546Spatrick using namespace lldb_private; 30*061da546Spatrick using namespace lldb; 31*061da546Spatrick using namespace lldb_private::python; 32*061da546Spatrick using llvm::cantFail; 33*061da546Spatrick using llvm::Error; 34*061da546Spatrick using llvm::Expected; 35*061da546Spatrick using llvm::Twine; 36*061da546Spatrick 37*061da546Spatrick template <> Expected<bool> python::As<bool>(Expected<PythonObject> &&obj) { 38*061da546Spatrick if (!obj) 39*061da546Spatrick return obj.takeError(); 40*061da546Spatrick return obj.get().IsTrue(); 41*061da546Spatrick } 42*061da546Spatrick 43*061da546Spatrick template <> 44*061da546Spatrick Expected<long long> python::As<long long>(Expected<PythonObject> &&obj) { 45*061da546Spatrick if (!obj) 46*061da546Spatrick return obj.takeError(); 47*061da546Spatrick return obj.get().AsLongLong(); 48*061da546Spatrick } 49*061da546Spatrick 50*061da546Spatrick template <> 51*061da546Spatrick Expected<std::string> python::As<std::string>(Expected<PythonObject> &&obj) { 52*061da546Spatrick if (!obj) 53*061da546Spatrick return obj.takeError(); 54*061da546Spatrick PyObject *str_obj = PyObject_Str(obj.get().get()); 55*061da546Spatrick if (!obj) 56*061da546Spatrick return llvm::make_error<PythonException>(); 57*061da546Spatrick auto str = Take<PythonString>(str_obj); 58*061da546Spatrick auto utf8 = str.AsUTF8(); 59*061da546Spatrick if (!utf8) 60*061da546Spatrick return utf8.takeError(); 61*061da546Spatrick return utf8.get(); 62*061da546Spatrick } 63*061da546Spatrick 64*061da546Spatrick void StructuredPythonObject::Serialize(llvm::json::OStream &s) const { 65*061da546Spatrick s.value(llvm::formatv("Python Obj: {0:X}", GetValue()).str()); 66*061da546Spatrick } 67*061da546Spatrick 68*061da546Spatrick // PythonObject 69*061da546Spatrick 70*061da546Spatrick void PythonObject::Dump(Stream &strm) const { 71*061da546Spatrick if (m_py_obj) { 72*061da546Spatrick FILE *file = llvm::sys::RetryAfterSignal(nullptr, ::tmpfile); 73*061da546Spatrick if (file) { 74*061da546Spatrick ::PyObject_Print(m_py_obj, file, 0); 75*061da546Spatrick const long length = ftell(file); 76*061da546Spatrick if (length) { 77*061da546Spatrick ::rewind(file); 78*061da546Spatrick std::vector<char> file_contents(length, '\0'); 79*061da546Spatrick const size_t length_read = 80*061da546Spatrick ::fread(file_contents.data(), 1, file_contents.size(), file); 81*061da546Spatrick if (length_read > 0) 82*061da546Spatrick strm.Write(file_contents.data(), length_read); 83*061da546Spatrick } 84*061da546Spatrick ::fclose(file); 85*061da546Spatrick } 86*061da546Spatrick } else 87*061da546Spatrick strm.PutCString("NULL"); 88*061da546Spatrick } 89*061da546Spatrick 90*061da546Spatrick PyObjectType PythonObject::GetObjectType() const { 91*061da546Spatrick if (!IsAllocated()) 92*061da546Spatrick return PyObjectType::None; 93*061da546Spatrick 94*061da546Spatrick if (PythonModule::Check(m_py_obj)) 95*061da546Spatrick return PyObjectType::Module; 96*061da546Spatrick if (PythonList::Check(m_py_obj)) 97*061da546Spatrick return PyObjectType::List; 98*061da546Spatrick if (PythonTuple::Check(m_py_obj)) 99*061da546Spatrick return PyObjectType::Tuple; 100*061da546Spatrick if (PythonDictionary::Check(m_py_obj)) 101*061da546Spatrick return PyObjectType::Dictionary; 102*061da546Spatrick if (PythonString::Check(m_py_obj)) 103*061da546Spatrick return PyObjectType::String; 104*061da546Spatrick #if PY_MAJOR_VERSION >= 3 105*061da546Spatrick if (PythonBytes::Check(m_py_obj)) 106*061da546Spatrick return PyObjectType::Bytes; 107*061da546Spatrick #endif 108*061da546Spatrick if (PythonByteArray::Check(m_py_obj)) 109*061da546Spatrick return PyObjectType::ByteArray; 110*061da546Spatrick if (PythonBoolean::Check(m_py_obj)) 111*061da546Spatrick return PyObjectType::Boolean; 112*061da546Spatrick if (PythonInteger::Check(m_py_obj)) 113*061da546Spatrick return PyObjectType::Integer; 114*061da546Spatrick if (PythonFile::Check(m_py_obj)) 115*061da546Spatrick return PyObjectType::File; 116*061da546Spatrick if (PythonCallable::Check(m_py_obj)) 117*061da546Spatrick return PyObjectType::Callable; 118*061da546Spatrick return PyObjectType::Unknown; 119*061da546Spatrick } 120*061da546Spatrick 121*061da546Spatrick PythonString PythonObject::Repr() const { 122*061da546Spatrick if (!m_py_obj) 123*061da546Spatrick return PythonString(); 124*061da546Spatrick PyObject *repr = PyObject_Repr(m_py_obj); 125*061da546Spatrick if (!repr) 126*061da546Spatrick return PythonString(); 127*061da546Spatrick return PythonString(PyRefType::Owned, repr); 128*061da546Spatrick } 129*061da546Spatrick 130*061da546Spatrick PythonString PythonObject::Str() const { 131*061da546Spatrick if (!m_py_obj) 132*061da546Spatrick return PythonString(); 133*061da546Spatrick PyObject *str = PyObject_Str(m_py_obj); 134*061da546Spatrick if (!str) 135*061da546Spatrick return PythonString(); 136*061da546Spatrick return PythonString(PyRefType::Owned, str); 137*061da546Spatrick } 138*061da546Spatrick 139*061da546Spatrick PythonObject 140*061da546Spatrick PythonObject::ResolveNameWithDictionary(llvm::StringRef name, 141*061da546Spatrick const PythonDictionary &dict) { 142*061da546Spatrick size_t dot_pos = name.find('.'); 143*061da546Spatrick llvm::StringRef piece = name.substr(0, dot_pos); 144*061da546Spatrick PythonObject result = dict.GetItemForKey(PythonString(piece)); 145*061da546Spatrick if (dot_pos == llvm::StringRef::npos) { 146*061da546Spatrick // There was no dot, we're done. 147*061da546Spatrick return result; 148*061da546Spatrick } 149*061da546Spatrick 150*061da546Spatrick // There was a dot. The remaining portion of the name should be looked up in 151*061da546Spatrick // the context of the object that was found in the dictionary. 152*061da546Spatrick return result.ResolveName(name.substr(dot_pos + 1)); 153*061da546Spatrick } 154*061da546Spatrick 155*061da546Spatrick PythonObject PythonObject::ResolveName(llvm::StringRef name) const { 156*061da546Spatrick // Resolve the name in the context of the specified object. If, for example, 157*061da546Spatrick // `this` refers to a PyModule, then this will look for `name` in this 158*061da546Spatrick // module. If `this` refers to a PyType, then it will resolve `name` as an 159*061da546Spatrick // attribute of that type. If `this` refers to an instance of an object, 160*061da546Spatrick // then it will resolve `name` as the value of the specified field. 161*061da546Spatrick // 162*061da546Spatrick // This function handles dotted names so that, for example, if `m_py_obj` 163*061da546Spatrick // refers to the `sys` module, and `name` == "path.append", then it will find 164*061da546Spatrick // the function `sys.path.append`. 165*061da546Spatrick 166*061da546Spatrick size_t dot_pos = name.find('.'); 167*061da546Spatrick if (dot_pos == llvm::StringRef::npos) { 168*061da546Spatrick // No dots in the name, we should be able to find the value immediately as 169*061da546Spatrick // an attribute of `m_py_obj`. 170*061da546Spatrick return GetAttributeValue(name); 171*061da546Spatrick } 172*061da546Spatrick 173*061da546Spatrick // Look up the first piece of the name, and resolve the rest as a child of 174*061da546Spatrick // that. 175*061da546Spatrick PythonObject parent = ResolveName(name.substr(0, dot_pos)); 176*061da546Spatrick if (!parent.IsAllocated()) 177*061da546Spatrick return PythonObject(); 178*061da546Spatrick 179*061da546Spatrick // Tail recursion.. should be optimized by the compiler 180*061da546Spatrick return parent.ResolveName(name.substr(dot_pos + 1)); 181*061da546Spatrick } 182*061da546Spatrick 183*061da546Spatrick bool PythonObject::HasAttribute(llvm::StringRef attr) const { 184*061da546Spatrick if (!IsValid()) 185*061da546Spatrick return false; 186*061da546Spatrick PythonString py_attr(attr); 187*061da546Spatrick return !!PyObject_HasAttr(m_py_obj, py_attr.get()); 188*061da546Spatrick } 189*061da546Spatrick 190*061da546Spatrick PythonObject PythonObject::GetAttributeValue(llvm::StringRef attr) const { 191*061da546Spatrick if (!IsValid()) 192*061da546Spatrick return PythonObject(); 193*061da546Spatrick 194*061da546Spatrick PythonString py_attr(attr); 195*061da546Spatrick if (!PyObject_HasAttr(m_py_obj, py_attr.get())) 196*061da546Spatrick return PythonObject(); 197*061da546Spatrick 198*061da546Spatrick return PythonObject(PyRefType::Owned, 199*061da546Spatrick PyObject_GetAttr(m_py_obj, py_attr.get())); 200*061da546Spatrick } 201*061da546Spatrick 202*061da546Spatrick StructuredData::ObjectSP PythonObject::CreateStructuredObject() const { 203*061da546Spatrick switch (GetObjectType()) { 204*061da546Spatrick case PyObjectType::Dictionary: 205*061da546Spatrick return PythonDictionary(PyRefType::Borrowed, m_py_obj) 206*061da546Spatrick .CreateStructuredDictionary(); 207*061da546Spatrick case PyObjectType::Boolean: 208*061da546Spatrick return PythonBoolean(PyRefType::Borrowed, m_py_obj) 209*061da546Spatrick .CreateStructuredBoolean(); 210*061da546Spatrick case PyObjectType::Integer: 211*061da546Spatrick return PythonInteger(PyRefType::Borrowed, m_py_obj) 212*061da546Spatrick .CreateStructuredInteger(); 213*061da546Spatrick case PyObjectType::List: 214*061da546Spatrick return PythonList(PyRefType::Borrowed, m_py_obj).CreateStructuredArray(); 215*061da546Spatrick case PyObjectType::String: 216*061da546Spatrick return PythonString(PyRefType::Borrowed, m_py_obj).CreateStructuredString(); 217*061da546Spatrick case PyObjectType::Bytes: 218*061da546Spatrick return PythonBytes(PyRefType::Borrowed, m_py_obj).CreateStructuredString(); 219*061da546Spatrick case PyObjectType::ByteArray: 220*061da546Spatrick return PythonByteArray(PyRefType::Borrowed, m_py_obj) 221*061da546Spatrick .CreateStructuredString(); 222*061da546Spatrick case PyObjectType::None: 223*061da546Spatrick return StructuredData::ObjectSP(); 224*061da546Spatrick default: 225*061da546Spatrick return StructuredData::ObjectSP(new StructuredPythonObject(m_py_obj)); 226*061da546Spatrick } 227*061da546Spatrick } 228*061da546Spatrick 229*061da546Spatrick // PythonString 230*061da546Spatrick 231*061da546Spatrick PythonBytes::PythonBytes(llvm::ArrayRef<uint8_t> bytes) { SetBytes(bytes); } 232*061da546Spatrick 233*061da546Spatrick PythonBytes::PythonBytes(const uint8_t *bytes, size_t length) { 234*061da546Spatrick SetBytes(llvm::ArrayRef<uint8_t>(bytes, length)); 235*061da546Spatrick } 236*061da546Spatrick 237*061da546Spatrick bool PythonBytes::Check(PyObject *py_obj) { 238*061da546Spatrick if (!py_obj) 239*061da546Spatrick return false; 240*061da546Spatrick return PyBytes_Check(py_obj); 241*061da546Spatrick } 242*061da546Spatrick 243*061da546Spatrick llvm::ArrayRef<uint8_t> PythonBytes::GetBytes() const { 244*061da546Spatrick if (!IsValid()) 245*061da546Spatrick return llvm::ArrayRef<uint8_t>(); 246*061da546Spatrick 247*061da546Spatrick Py_ssize_t size; 248*061da546Spatrick char *c; 249*061da546Spatrick 250*061da546Spatrick PyBytes_AsStringAndSize(m_py_obj, &c, &size); 251*061da546Spatrick return llvm::ArrayRef<uint8_t>(reinterpret_cast<uint8_t *>(c), size); 252*061da546Spatrick } 253*061da546Spatrick 254*061da546Spatrick size_t PythonBytes::GetSize() const { 255*061da546Spatrick if (!IsValid()) 256*061da546Spatrick return 0; 257*061da546Spatrick return PyBytes_Size(m_py_obj); 258*061da546Spatrick } 259*061da546Spatrick 260*061da546Spatrick void PythonBytes::SetBytes(llvm::ArrayRef<uint8_t> bytes) { 261*061da546Spatrick const char *data = reinterpret_cast<const char *>(bytes.data()); 262*061da546Spatrick *this = Take<PythonBytes>(PyBytes_FromStringAndSize(data, bytes.size())); 263*061da546Spatrick } 264*061da546Spatrick 265*061da546Spatrick StructuredData::StringSP PythonBytes::CreateStructuredString() const { 266*061da546Spatrick StructuredData::StringSP result(new StructuredData::String); 267*061da546Spatrick Py_ssize_t size; 268*061da546Spatrick char *c; 269*061da546Spatrick PyBytes_AsStringAndSize(m_py_obj, &c, &size); 270*061da546Spatrick result->SetValue(std::string(c, size)); 271*061da546Spatrick return result; 272*061da546Spatrick } 273*061da546Spatrick 274*061da546Spatrick PythonByteArray::PythonByteArray(llvm::ArrayRef<uint8_t> bytes) 275*061da546Spatrick : PythonByteArray(bytes.data(), bytes.size()) {} 276*061da546Spatrick 277*061da546Spatrick PythonByteArray::PythonByteArray(const uint8_t *bytes, size_t length) { 278*061da546Spatrick const char *str = reinterpret_cast<const char *>(bytes); 279*061da546Spatrick *this = Take<PythonByteArray>(PyByteArray_FromStringAndSize(str, length)); 280*061da546Spatrick } 281*061da546Spatrick 282*061da546Spatrick bool PythonByteArray::Check(PyObject *py_obj) { 283*061da546Spatrick if (!py_obj) 284*061da546Spatrick return false; 285*061da546Spatrick return PyByteArray_Check(py_obj); 286*061da546Spatrick } 287*061da546Spatrick 288*061da546Spatrick llvm::ArrayRef<uint8_t> PythonByteArray::GetBytes() const { 289*061da546Spatrick if (!IsValid()) 290*061da546Spatrick return llvm::ArrayRef<uint8_t>(); 291*061da546Spatrick 292*061da546Spatrick char *c = PyByteArray_AsString(m_py_obj); 293*061da546Spatrick size_t size = GetSize(); 294*061da546Spatrick return llvm::ArrayRef<uint8_t>(reinterpret_cast<uint8_t *>(c), size); 295*061da546Spatrick } 296*061da546Spatrick 297*061da546Spatrick size_t PythonByteArray::GetSize() const { 298*061da546Spatrick if (!IsValid()) 299*061da546Spatrick return 0; 300*061da546Spatrick 301*061da546Spatrick return PyByteArray_Size(m_py_obj); 302*061da546Spatrick } 303*061da546Spatrick 304*061da546Spatrick StructuredData::StringSP PythonByteArray::CreateStructuredString() const { 305*061da546Spatrick StructuredData::StringSP result(new StructuredData::String); 306*061da546Spatrick llvm::ArrayRef<uint8_t> bytes = GetBytes(); 307*061da546Spatrick const char *str = reinterpret_cast<const char *>(bytes.data()); 308*061da546Spatrick result->SetValue(std::string(str, bytes.size())); 309*061da546Spatrick return result; 310*061da546Spatrick } 311*061da546Spatrick 312*061da546Spatrick // PythonString 313*061da546Spatrick 314*061da546Spatrick Expected<PythonString> PythonString::FromUTF8(llvm::StringRef string) { 315*061da546Spatrick #if PY_MAJOR_VERSION >= 3 316*061da546Spatrick PyObject *str = PyUnicode_FromStringAndSize(string.data(), string.size()); 317*061da546Spatrick #else 318*061da546Spatrick PyObject *str = PyString_FromStringAndSize(string.data(), string.size()); 319*061da546Spatrick #endif 320*061da546Spatrick if (!str) 321*061da546Spatrick return llvm::make_error<PythonException>(); 322*061da546Spatrick return Take<PythonString>(str); 323*061da546Spatrick } 324*061da546Spatrick 325*061da546Spatrick PythonString::PythonString(llvm::StringRef string) { SetString(string); } 326*061da546Spatrick 327*061da546Spatrick bool PythonString::Check(PyObject *py_obj) { 328*061da546Spatrick if (!py_obj) 329*061da546Spatrick return false; 330*061da546Spatrick 331*061da546Spatrick if (PyUnicode_Check(py_obj)) 332*061da546Spatrick return true; 333*061da546Spatrick #if PY_MAJOR_VERSION < 3 334*061da546Spatrick if (PyString_Check(py_obj)) 335*061da546Spatrick return true; 336*061da546Spatrick #endif 337*061da546Spatrick return false; 338*061da546Spatrick } 339*061da546Spatrick 340*061da546Spatrick void PythonString::Convert(PyRefType &type, PyObject *&py_obj) { 341*061da546Spatrick #if PY_MAJOR_VERSION < 3 342*061da546Spatrick // In Python 2, Don't store PyUnicode objects directly, because we need 343*061da546Spatrick // access to their underlying character buffers which Python 2 doesn't 344*061da546Spatrick // provide. 345*061da546Spatrick if (PyUnicode_Check(py_obj)) { 346*061da546Spatrick PyObject *s = PyUnicode_AsUTF8String(py_obj); 347*061da546Spatrick if (s == nullptr) { 348*061da546Spatrick PyErr_Clear(); 349*061da546Spatrick if (type == PyRefType::Owned) 350*061da546Spatrick Py_DECREF(py_obj); 351*061da546Spatrick return; 352*061da546Spatrick } 353*061da546Spatrick if (type == PyRefType::Owned) 354*061da546Spatrick Py_DECREF(py_obj); 355*061da546Spatrick else 356*061da546Spatrick type = PyRefType::Owned; 357*061da546Spatrick py_obj = s; 358*061da546Spatrick } 359*061da546Spatrick #endif 360*061da546Spatrick } 361*061da546Spatrick 362*061da546Spatrick llvm::StringRef PythonString::GetString() const { 363*061da546Spatrick auto s = AsUTF8(); 364*061da546Spatrick if (!s) { 365*061da546Spatrick llvm::consumeError(s.takeError()); 366*061da546Spatrick return llvm::StringRef(""); 367*061da546Spatrick } 368*061da546Spatrick return s.get(); 369*061da546Spatrick } 370*061da546Spatrick 371*061da546Spatrick Expected<llvm::StringRef> PythonString::AsUTF8() const { 372*061da546Spatrick if (!IsValid()) 373*061da546Spatrick return nullDeref(); 374*061da546Spatrick 375*061da546Spatrick Py_ssize_t size; 376*061da546Spatrick const char *data; 377*061da546Spatrick 378*061da546Spatrick #if PY_MAJOR_VERSION >= 3 379*061da546Spatrick data = PyUnicode_AsUTF8AndSize(m_py_obj, &size); 380*061da546Spatrick #else 381*061da546Spatrick char *c = NULL; 382*061da546Spatrick int r = PyString_AsStringAndSize(m_py_obj, &c, &size); 383*061da546Spatrick if (r < 0) 384*061da546Spatrick c = NULL; 385*061da546Spatrick data = c; 386*061da546Spatrick #endif 387*061da546Spatrick 388*061da546Spatrick if (!data) 389*061da546Spatrick return exception(); 390*061da546Spatrick 391*061da546Spatrick return llvm::StringRef(data, size); 392*061da546Spatrick } 393*061da546Spatrick 394*061da546Spatrick size_t PythonString::GetSize() const { 395*061da546Spatrick if (IsValid()) { 396*061da546Spatrick #if PY_MAJOR_VERSION >= 3 397*061da546Spatrick return PyUnicode_GetSize(m_py_obj); 398*061da546Spatrick #else 399*061da546Spatrick return PyString_Size(m_py_obj); 400*061da546Spatrick #endif 401*061da546Spatrick } 402*061da546Spatrick return 0; 403*061da546Spatrick } 404*061da546Spatrick 405*061da546Spatrick void PythonString::SetString(llvm::StringRef string) { 406*061da546Spatrick auto s = FromUTF8(string); 407*061da546Spatrick if (!s) { 408*061da546Spatrick llvm::consumeError(s.takeError()); 409*061da546Spatrick Reset(); 410*061da546Spatrick } else { 411*061da546Spatrick *this = std::move(s.get()); 412*061da546Spatrick } 413*061da546Spatrick } 414*061da546Spatrick 415*061da546Spatrick StructuredData::StringSP PythonString::CreateStructuredString() const { 416*061da546Spatrick StructuredData::StringSP result(new StructuredData::String); 417*061da546Spatrick result->SetValue(GetString()); 418*061da546Spatrick return result; 419*061da546Spatrick } 420*061da546Spatrick 421*061da546Spatrick // PythonInteger 422*061da546Spatrick 423*061da546Spatrick PythonInteger::PythonInteger(int64_t value) { SetInteger(value); } 424*061da546Spatrick 425*061da546Spatrick bool PythonInteger::Check(PyObject *py_obj) { 426*061da546Spatrick if (!py_obj) 427*061da546Spatrick return false; 428*061da546Spatrick 429*061da546Spatrick #if PY_MAJOR_VERSION >= 3 430*061da546Spatrick // Python 3 does not have PyInt_Check. There is only one type of integral 431*061da546Spatrick // value, long. 432*061da546Spatrick return PyLong_Check(py_obj); 433*061da546Spatrick #else 434*061da546Spatrick return PyLong_Check(py_obj) || PyInt_Check(py_obj); 435*061da546Spatrick #endif 436*061da546Spatrick } 437*061da546Spatrick 438*061da546Spatrick void PythonInteger::Convert(PyRefType &type, PyObject *&py_obj) { 439*061da546Spatrick #if PY_MAJOR_VERSION < 3 440*061da546Spatrick // Always store this as a PyLong, which makes interoperability between Python 441*061da546Spatrick // 2.x and Python 3.x easier. This is only necessary in 2.x, since 3.x 442*061da546Spatrick // doesn't even have a PyInt. 443*061da546Spatrick if (PyInt_Check(py_obj)) { 444*061da546Spatrick // Since we converted the original object to a different type, the new 445*061da546Spatrick // object is an owned object regardless of the ownership semantics 446*061da546Spatrick // requested by the user. 447*061da546Spatrick long long value = PyInt_AsLong(py_obj); 448*061da546Spatrick PyObject *l = nullptr; 449*061da546Spatrick if (!PyErr_Occurred()) 450*061da546Spatrick l = PyLong_FromLongLong(value); 451*061da546Spatrick if (l == nullptr) { 452*061da546Spatrick PyErr_Clear(); 453*061da546Spatrick if (type == PyRefType::Owned) 454*061da546Spatrick Py_DECREF(py_obj); 455*061da546Spatrick return; 456*061da546Spatrick } 457*061da546Spatrick if (type == PyRefType::Owned) 458*061da546Spatrick Py_DECREF(py_obj); 459*061da546Spatrick else 460*061da546Spatrick type = PyRefType::Owned; 461*061da546Spatrick py_obj = l; 462*061da546Spatrick } 463*061da546Spatrick #endif 464*061da546Spatrick } 465*061da546Spatrick 466*061da546Spatrick int64_t PythonInteger::GetInteger() const { 467*061da546Spatrick if (m_py_obj) { 468*061da546Spatrick assert(PyLong_Check(m_py_obj) && 469*061da546Spatrick "PythonInteger::GetInteger has a PyObject that isn't a PyLong"); 470*061da546Spatrick 471*061da546Spatrick int overflow = 0; 472*061da546Spatrick int64_t result = PyLong_AsLongLongAndOverflow(m_py_obj, &overflow); 473*061da546Spatrick if (overflow != 0) { 474*061da546Spatrick // We got an integer that overflows, like 18446744072853913392L we can't 475*061da546Spatrick // use PyLong_AsLongLong() as it will return 0xffffffffffffffff. If we 476*061da546Spatrick // use the unsigned long long it will work as expected. 477*061da546Spatrick const uint64_t uval = PyLong_AsUnsignedLongLong(m_py_obj); 478*061da546Spatrick result = static_cast<int64_t>(uval); 479*061da546Spatrick } 480*061da546Spatrick return result; 481*061da546Spatrick } 482*061da546Spatrick return UINT64_MAX; 483*061da546Spatrick } 484*061da546Spatrick 485*061da546Spatrick void PythonInteger::SetInteger(int64_t value) { 486*061da546Spatrick *this = Take<PythonInteger>(PyLong_FromLongLong(value)); 487*061da546Spatrick } 488*061da546Spatrick 489*061da546Spatrick StructuredData::IntegerSP PythonInteger::CreateStructuredInteger() const { 490*061da546Spatrick StructuredData::IntegerSP result(new StructuredData::Integer); 491*061da546Spatrick result->SetValue(GetInteger()); 492*061da546Spatrick return result; 493*061da546Spatrick } 494*061da546Spatrick 495*061da546Spatrick // PythonBoolean 496*061da546Spatrick 497*061da546Spatrick PythonBoolean::PythonBoolean(bool value) { 498*061da546Spatrick SetValue(value); 499*061da546Spatrick } 500*061da546Spatrick 501*061da546Spatrick bool PythonBoolean::Check(PyObject *py_obj) { 502*061da546Spatrick return py_obj ? PyBool_Check(py_obj) : false; 503*061da546Spatrick } 504*061da546Spatrick 505*061da546Spatrick bool PythonBoolean::GetValue() const { 506*061da546Spatrick return m_py_obj ? PyObject_IsTrue(m_py_obj) : false; 507*061da546Spatrick } 508*061da546Spatrick 509*061da546Spatrick void PythonBoolean::SetValue(bool value) { 510*061da546Spatrick *this = Take<PythonBoolean>(PyBool_FromLong(value)); 511*061da546Spatrick } 512*061da546Spatrick 513*061da546Spatrick StructuredData::BooleanSP PythonBoolean::CreateStructuredBoolean() const { 514*061da546Spatrick StructuredData::BooleanSP result(new StructuredData::Boolean); 515*061da546Spatrick result->SetValue(GetValue()); 516*061da546Spatrick return result; 517*061da546Spatrick } 518*061da546Spatrick 519*061da546Spatrick // PythonList 520*061da546Spatrick 521*061da546Spatrick PythonList::PythonList(PyInitialValue value) { 522*061da546Spatrick if (value == PyInitialValue::Empty) 523*061da546Spatrick *this = Take<PythonList>(PyList_New(0)); 524*061da546Spatrick } 525*061da546Spatrick 526*061da546Spatrick PythonList::PythonList(int list_size) { 527*061da546Spatrick *this = Take<PythonList>(PyList_New(list_size)); 528*061da546Spatrick } 529*061da546Spatrick 530*061da546Spatrick bool PythonList::Check(PyObject *py_obj) { 531*061da546Spatrick if (!py_obj) 532*061da546Spatrick return false; 533*061da546Spatrick return PyList_Check(py_obj); 534*061da546Spatrick } 535*061da546Spatrick 536*061da546Spatrick uint32_t PythonList::GetSize() const { 537*061da546Spatrick if (IsValid()) 538*061da546Spatrick return PyList_GET_SIZE(m_py_obj); 539*061da546Spatrick return 0; 540*061da546Spatrick } 541*061da546Spatrick 542*061da546Spatrick PythonObject PythonList::GetItemAtIndex(uint32_t index) const { 543*061da546Spatrick if (IsValid()) 544*061da546Spatrick return PythonObject(PyRefType::Borrowed, PyList_GetItem(m_py_obj, index)); 545*061da546Spatrick return PythonObject(); 546*061da546Spatrick } 547*061da546Spatrick 548*061da546Spatrick void PythonList::SetItemAtIndex(uint32_t index, const PythonObject &object) { 549*061da546Spatrick if (IsAllocated() && object.IsValid()) { 550*061da546Spatrick // PyList_SetItem is documented to "steal" a reference, so we need to 551*061da546Spatrick // convert it to an owned reference by incrementing it. 552*061da546Spatrick Py_INCREF(object.get()); 553*061da546Spatrick PyList_SetItem(m_py_obj, index, object.get()); 554*061da546Spatrick } 555*061da546Spatrick } 556*061da546Spatrick 557*061da546Spatrick void PythonList::AppendItem(const PythonObject &object) { 558*061da546Spatrick if (IsAllocated() && object.IsValid()) { 559*061da546Spatrick // `PyList_Append` does *not* steal a reference, so do not call `Py_INCREF` 560*061da546Spatrick // here like we do with `PyList_SetItem`. 561*061da546Spatrick PyList_Append(m_py_obj, object.get()); 562*061da546Spatrick } 563*061da546Spatrick } 564*061da546Spatrick 565*061da546Spatrick StructuredData::ArraySP PythonList::CreateStructuredArray() const { 566*061da546Spatrick StructuredData::ArraySP result(new StructuredData::Array); 567*061da546Spatrick uint32_t count = GetSize(); 568*061da546Spatrick for (uint32_t i = 0; i < count; ++i) { 569*061da546Spatrick PythonObject obj = GetItemAtIndex(i); 570*061da546Spatrick result->AddItem(obj.CreateStructuredObject()); 571*061da546Spatrick } 572*061da546Spatrick return result; 573*061da546Spatrick } 574*061da546Spatrick 575*061da546Spatrick // PythonTuple 576*061da546Spatrick 577*061da546Spatrick PythonTuple::PythonTuple(PyInitialValue value) { 578*061da546Spatrick if (value == PyInitialValue::Empty) 579*061da546Spatrick *this = Take<PythonTuple>(PyTuple_New(0)); 580*061da546Spatrick } 581*061da546Spatrick 582*061da546Spatrick PythonTuple::PythonTuple(int tuple_size) { 583*061da546Spatrick *this = Take<PythonTuple>(PyTuple_New(tuple_size)); 584*061da546Spatrick } 585*061da546Spatrick 586*061da546Spatrick PythonTuple::PythonTuple(std::initializer_list<PythonObject> objects) { 587*061da546Spatrick m_py_obj = PyTuple_New(objects.size()); 588*061da546Spatrick 589*061da546Spatrick uint32_t idx = 0; 590*061da546Spatrick for (auto object : objects) { 591*061da546Spatrick if (object.IsValid()) 592*061da546Spatrick SetItemAtIndex(idx, object); 593*061da546Spatrick idx++; 594*061da546Spatrick } 595*061da546Spatrick } 596*061da546Spatrick 597*061da546Spatrick PythonTuple::PythonTuple(std::initializer_list<PyObject *> objects) { 598*061da546Spatrick m_py_obj = PyTuple_New(objects.size()); 599*061da546Spatrick 600*061da546Spatrick uint32_t idx = 0; 601*061da546Spatrick for (auto py_object : objects) { 602*061da546Spatrick PythonObject object(PyRefType::Borrowed, py_object); 603*061da546Spatrick if (object.IsValid()) 604*061da546Spatrick SetItemAtIndex(idx, object); 605*061da546Spatrick idx++; 606*061da546Spatrick } 607*061da546Spatrick } 608*061da546Spatrick 609*061da546Spatrick bool PythonTuple::Check(PyObject *py_obj) { 610*061da546Spatrick if (!py_obj) 611*061da546Spatrick return false; 612*061da546Spatrick return PyTuple_Check(py_obj); 613*061da546Spatrick } 614*061da546Spatrick 615*061da546Spatrick uint32_t PythonTuple::GetSize() const { 616*061da546Spatrick if (IsValid()) 617*061da546Spatrick return PyTuple_GET_SIZE(m_py_obj); 618*061da546Spatrick return 0; 619*061da546Spatrick } 620*061da546Spatrick 621*061da546Spatrick PythonObject PythonTuple::GetItemAtIndex(uint32_t index) const { 622*061da546Spatrick if (IsValid()) 623*061da546Spatrick return PythonObject(PyRefType::Borrowed, PyTuple_GetItem(m_py_obj, index)); 624*061da546Spatrick return PythonObject(); 625*061da546Spatrick } 626*061da546Spatrick 627*061da546Spatrick void PythonTuple::SetItemAtIndex(uint32_t index, const PythonObject &object) { 628*061da546Spatrick if (IsAllocated() && object.IsValid()) { 629*061da546Spatrick // PyTuple_SetItem is documented to "steal" a reference, so we need to 630*061da546Spatrick // convert it to an owned reference by incrementing it. 631*061da546Spatrick Py_INCREF(object.get()); 632*061da546Spatrick PyTuple_SetItem(m_py_obj, index, object.get()); 633*061da546Spatrick } 634*061da546Spatrick } 635*061da546Spatrick 636*061da546Spatrick StructuredData::ArraySP PythonTuple::CreateStructuredArray() const { 637*061da546Spatrick StructuredData::ArraySP result(new StructuredData::Array); 638*061da546Spatrick uint32_t count = GetSize(); 639*061da546Spatrick for (uint32_t i = 0; i < count; ++i) { 640*061da546Spatrick PythonObject obj = GetItemAtIndex(i); 641*061da546Spatrick result->AddItem(obj.CreateStructuredObject()); 642*061da546Spatrick } 643*061da546Spatrick return result; 644*061da546Spatrick } 645*061da546Spatrick 646*061da546Spatrick // PythonDictionary 647*061da546Spatrick 648*061da546Spatrick PythonDictionary::PythonDictionary(PyInitialValue value) { 649*061da546Spatrick if (value == PyInitialValue::Empty) 650*061da546Spatrick *this = Take<PythonDictionary>(PyDict_New()); 651*061da546Spatrick } 652*061da546Spatrick 653*061da546Spatrick bool PythonDictionary::Check(PyObject *py_obj) { 654*061da546Spatrick if (!py_obj) 655*061da546Spatrick return false; 656*061da546Spatrick 657*061da546Spatrick return PyDict_Check(py_obj); 658*061da546Spatrick } 659*061da546Spatrick 660*061da546Spatrick uint32_t PythonDictionary::GetSize() const { 661*061da546Spatrick if (IsValid()) 662*061da546Spatrick return PyDict_Size(m_py_obj); 663*061da546Spatrick return 0; 664*061da546Spatrick } 665*061da546Spatrick 666*061da546Spatrick PythonList PythonDictionary::GetKeys() const { 667*061da546Spatrick if (IsValid()) 668*061da546Spatrick return PythonList(PyRefType::Owned, PyDict_Keys(m_py_obj)); 669*061da546Spatrick return PythonList(PyInitialValue::Invalid); 670*061da546Spatrick } 671*061da546Spatrick 672*061da546Spatrick PythonObject PythonDictionary::GetItemForKey(const PythonObject &key) const { 673*061da546Spatrick auto item = GetItem(key); 674*061da546Spatrick if (!item) { 675*061da546Spatrick llvm::consumeError(item.takeError()); 676*061da546Spatrick return PythonObject(); 677*061da546Spatrick } 678*061da546Spatrick return std::move(item.get()); 679*061da546Spatrick } 680*061da546Spatrick 681*061da546Spatrick Expected<PythonObject> 682*061da546Spatrick PythonDictionary::GetItem(const PythonObject &key) const { 683*061da546Spatrick if (!IsValid()) 684*061da546Spatrick return nullDeref(); 685*061da546Spatrick #if PY_MAJOR_VERSION >= 3 686*061da546Spatrick PyObject *o = PyDict_GetItemWithError(m_py_obj, key.get()); 687*061da546Spatrick if (PyErr_Occurred()) 688*061da546Spatrick return exception(); 689*061da546Spatrick #else 690*061da546Spatrick PyObject *o = PyDict_GetItem(m_py_obj, key.get()); 691*061da546Spatrick #endif 692*061da546Spatrick if (!o) 693*061da546Spatrick return keyError(); 694*061da546Spatrick return Retain<PythonObject>(o); 695*061da546Spatrick } 696*061da546Spatrick 697*061da546Spatrick Expected<PythonObject> PythonDictionary::GetItem(const Twine &key) const { 698*061da546Spatrick if (!IsValid()) 699*061da546Spatrick return nullDeref(); 700*061da546Spatrick PyObject *o = PyDict_GetItemString(m_py_obj, NullTerminated(key)); 701*061da546Spatrick if (PyErr_Occurred()) 702*061da546Spatrick return exception(); 703*061da546Spatrick if (!o) 704*061da546Spatrick return keyError(); 705*061da546Spatrick return Retain<PythonObject>(o); 706*061da546Spatrick } 707*061da546Spatrick 708*061da546Spatrick Error PythonDictionary::SetItem(const PythonObject &key, 709*061da546Spatrick const PythonObject &value) const { 710*061da546Spatrick if (!IsValid() || !value.IsValid()) 711*061da546Spatrick return nullDeref(); 712*061da546Spatrick int r = PyDict_SetItem(m_py_obj, key.get(), value.get()); 713*061da546Spatrick if (r < 0) 714*061da546Spatrick return exception(); 715*061da546Spatrick return Error::success(); 716*061da546Spatrick } 717*061da546Spatrick 718*061da546Spatrick Error PythonDictionary::SetItem(const Twine &key, 719*061da546Spatrick const PythonObject &value) const { 720*061da546Spatrick if (!IsValid() || !value.IsValid()) 721*061da546Spatrick return nullDeref(); 722*061da546Spatrick int r = PyDict_SetItemString(m_py_obj, NullTerminated(key), value.get()); 723*061da546Spatrick if (r < 0) 724*061da546Spatrick return exception(); 725*061da546Spatrick return Error::success(); 726*061da546Spatrick } 727*061da546Spatrick 728*061da546Spatrick void PythonDictionary::SetItemForKey(const PythonObject &key, 729*061da546Spatrick const PythonObject &value) { 730*061da546Spatrick Error error = SetItem(key, value); 731*061da546Spatrick if (error) 732*061da546Spatrick llvm::consumeError(std::move(error)); 733*061da546Spatrick } 734*061da546Spatrick 735*061da546Spatrick StructuredData::DictionarySP 736*061da546Spatrick PythonDictionary::CreateStructuredDictionary() const { 737*061da546Spatrick StructuredData::DictionarySP result(new StructuredData::Dictionary); 738*061da546Spatrick PythonList keys(GetKeys()); 739*061da546Spatrick uint32_t num_keys = keys.GetSize(); 740*061da546Spatrick for (uint32_t i = 0; i < num_keys; ++i) { 741*061da546Spatrick PythonObject key = keys.GetItemAtIndex(i); 742*061da546Spatrick PythonObject value = GetItemForKey(key); 743*061da546Spatrick StructuredData::ObjectSP structured_value = value.CreateStructuredObject(); 744*061da546Spatrick result->AddItem(key.Str().GetString(), structured_value); 745*061da546Spatrick } 746*061da546Spatrick return result; 747*061da546Spatrick } 748*061da546Spatrick 749*061da546Spatrick PythonModule PythonModule::BuiltinsModule() { 750*061da546Spatrick #if PY_MAJOR_VERSION >= 3 751*061da546Spatrick return AddModule("builtins"); 752*061da546Spatrick #else 753*061da546Spatrick return AddModule("__builtin__"); 754*061da546Spatrick #endif 755*061da546Spatrick } 756*061da546Spatrick 757*061da546Spatrick PythonModule PythonModule::MainModule() { return AddModule("__main__"); } 758*061da546Spatrick 759*061da546Spatrick PythonModule PythonModule::AddModule(llvm::StringRef module) { 760*061da546Spatrick std::string str = module.str(); 761*061da546Spatrick return PythonModule(PyRefType::Borrowed, PyImport_AddModule(str.c_str())); 762*061da546Spatrick } 763*061da546Spatrick 764*061da546Spatrick Expected<PythonModule> PythonModule::Import(const Twine &name) { 765*061da546Spatrick PyObject *mod = PyImport_ImportModule(NullTerminated(name)); 766*061da546Spatrick if (!mod) 767*061da546Spatrick return exception(); 768*061da546Spatrick return Take<PythonModule>(mod); 769*061da546Spatrick } 770*061da546Spatrick 771*061da546Spatrick Expected<PythonObject> PythonModule::Get(const Twine &name) { 772*061da546Spatrick if (!IsValid()) 773*061da546Spatrick return nullDeref(); 774*061da546Spatrick PyObject *dict = PyModule_GetDict(m_py_obj); 775*061da546Spatrick if (!dict) 776*061da546Spatrick return exception(); 777*061da546Spatrick PyObject *item = PyDict_GetItemString(dict, NullTerminated(name)); 778*061da546Spatrick if (!item) 779*061da546Spatrick return exception(); 780*061da546Spatrick return Retain<PythonObject>(item); 781*061da546Spatrick } 782*061da546Spatrick 783*061da546Spatrick bool PythonModule::Check(PyObject *py_obj) { 784*061da546Spatrick if (!py_obj) 785*061da546Spatrick return false; 786*061da546Spatrick 787*061da546Spatrick return PyModule_Check(py_obj); 788*061da546Spatrick } 789*061da546Spatrick 790*061da546Spatrick PythonDictionary PythonModule::GetDictionary() const { 791*061da546Spatrick if (!IsValid()) 792*061da546Spatrick return PythonDictionary(); 793*061da546Spatrick return Retain<PythonDictionary>(PyModule_GetDict(m_py_obj)); 794*061da546Spatrick } 795*061da546Spatrick 796*061da546Spatrick bool PythonCallable::Check(PyObject *py_obj) { 797*061da546Spatrick if (!py_obj) 798*061da546Spatrick return false; 799*061da546Spatrick 800*061da546Spatrick return PyCallable_Check(py_obj); 801*061da546Spatrick } 802*061da546Spatrick 803*061da546Spatrick #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3 804*061da546Spatrick static const char get_arg_info_script[] = R"( 805*061da546Spatrick from inspect import signature, Parameter, ismethod 806*061da546Spatrick from collections import namedtuple 807*061da546Spatrick ArgInfo = namedtuple('ArgInfo', ['count', 'has_varargs']) 808*061da546Spatrick def main(f): 809*061da546Spatrick count = 0 810*061da546Spatrick varargs = False 811*061da546Spatrick for parameter in signature(f).parameters.values(): 812*061da546Spatrick kind = parameter.kind 813*061da546Spatrick if kind in (Parameter.POSITIONAL_ONLY, 814*061da546Spatrick Parameter.POSITIONAL_OR_KEYWORD): 815*061da546Spatrick count += 1 816*061da546Spatrick elif kind == Parameter.VAR_POSITIONAL: 817*061da546Spatrick varargs = True 818*061da546Spatrick elif kind in (Parameter.KEYWORD_ONLY, 819*061da546Spatrick Parameter.VAR_KEYWORD): 820*061da546Spatrick pass 821*061da546Spatrick else: 822*061da546Spatrick raise Exception(f'unknown parameter kind: {kind}') 823*061da546Spatrick return ArgInfo(count, varargs) 824*061da546Spatrick )"; 825*061da546Spatrick #endif 826*061da546Spatrick 827*061da546Spatrick Expected<PythonCallable::ArgInfo> PythonCallable::GetArgInfo() const { 828*061da546Spatrick ArgInfo result = {}; 829*061da546Spatrick if (!IsValid()) 830*061da546Spatrick return nullDeref(); 831*061da546Spatrick 832*061da546Spatrick #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3 833*061da546Spatrick 834*061da546Spatrick // no need to synchronize access to this global, we already have the GIL 835*061da546Spatrick static PythonScript get_arg_info(get_arg_info_script); 836*061da546Spatrick Expected<PythonObject> pyarginfo = get_arg_info(*this); 837*061da546Spatrick if (!pyarginfo) 838*061da546Spatrick return pyarginfo.takeError(); 839*061da546Spatrick long long count = 840*061da546Spatrick cantFail(As<long long>(pyarginfo.get().GetAttribute("count"))); 841*061da546Spatrick bool has_varargs = 842*061da546Spatrick cantFail(As<bool>(pyarginfo.get().GetAttribute("has_varargs"))); 843*061da546Spatrick result.max_positional_args = has_varargs ? ArgInfo::UNBOUNDED : count; 844*061da546Spatrick 845*061da546Spatrick #else 846*061da546Spatrick PyObject *py_func_obj; 847*061da546Spatrick bool is_bound_method = false; 848*061da546Spatrick bool is_class = false; 849*061da546Spatrick 850*061da546Spatrick if (PyType_Check(m_py_obj) || PyClass_Check(m_py_obj)) { 851*061da546Spatrick auto init = GetAttribute("__init__"); 852*061da546Spatrick if (!init) 853*061da546Spatrick return init.takeError(); 854*061da546Spatrick py_func_obj = init.get().get(); 855*061da546Spatrick is_class = true; 856*061da546Spatrick } else { 857*061da546Spatrick py_func_obj = m_py_obj; 858*061da546Spatrick } 859*061da546Spatrick 860*061da546Spatrick if (PyMethod_Check(py_func_obj)) { 861*061da546Spatrick py_func_obj = PyMethod_GET_FUNCTION(py_func_obj); 862*061da546Spatrick PythonObject im_self = GetAttributeValue("im_self"); 863*061da546Spatrick if (im_self.IsValid() && !im_self.IsNone()) 864*061da546Spatrick is_bound_method = true; 865*061da546Spatrick } else { 866*061da546Spatrick // see if this is a callable object with an __call__ method 867*061da546Spatrick if (!PyFunction_Check(py_func_obj)) { 868*061da546Spatrick PythonObject __call__ = GetAttributeValue("__call__"); 869*061da546Spatrick if (__call__.IsValid()) { 870*061da546Spatrick auto __callable__ = __call__.AsType<PythonCallable>(); 871*061da546Spatrick if (__callable__.IsValid()) { 872*061da546Spatrick py_func_obj = PyMethod_GET_FUNCTION(__callable__.get()); 873*061da546Spatrick PythonObject im_self = __callable__.GetAttributeValue("im_self"); 874*061da546Spatrick if (im_self.IsValid() && !im_self.IsNone()) 875*061da546Spatrick is_bound_method = true; 876*061da546Spatrick } 877*061da546Spatrick } 878*061da546Spatrick } 879*061da546Spatrick } 880*061da546Spatrick 881*061da546Spatrick if (!py_func_obj) 882*061da546Spatrick return result; 883*061da546Spatrick 884*061da546Spatrick PyCodeObject *code = (PyCodeObject *)PyFunction_GET_CODE(py_func_obj); 885*061da546Spatrick if (!code) 886*061da546Spatrick return result; 887*061da546Spatrick 888*061da546Spatrick auto count = code->co_argcount; 889*061da546Spatrick bool has_varargs = !!(code->co_flags & CO_VARARGS); 890*061da546Spatrick result.max_positional_args = 891*061da546Spatrick has_varargs ? ArgInfo::UNBOUNDED 892*061da546Spatrick : (count - (int)is_bound_method) - (int)is_class; 893*061da546Spatrick 894*061da546Spatrick #endif 895*061da546Spatrick 896*061da546Spatrick return result; 897*061da546Spatrick } 898*061da546Spatrick 899*061da546Spatrick constexpr unsigned 900*061da546Spatrick PythonCallable::ArgInfo::UNBOUNDED; // FIXME delete after c++17 901*061da546Spatrick 902*061da546Spatrick PythonObject PythonCallable::operator()() { 903*061da546Spatrick return PythonObject(PyRefType::Owned, PyObject_CallObject(m_py_obj, nullptr)); 904*061da546Spatrick } 905*061da546Spatrick 906*061da546Spatrick PythonObject PythonCallable:: 907*061da546Spatrick operator()(std::initializer_list<PyObject *> args) { 908*061da546Spatrick PythonTuple arg_tuple(args); 909*061da546Spatrick return PythonObject(PyRefType::Owned, 910*061da546Spatrick PyObject_CallObject(m_py_obj, arg_tuple.get())); 911*061da546Spatrick } 912*061da546Spatrick 913*061da546Spatrick PythonObject PythonCallable:: 914*061da546Spatrick operator()(std::initializer_list<PythonObject> args) { 915*061da546Spatrick PythonTuple arg_tuple(args); 916*061da546Spatrick return PythonObject(PyRefType::Owned, 917*061da546Spatrick PyObject_CallObject(m_py_obj, arg_tuple.get())); 918*061da546Spatrick } 919*061da546Spatrick 920*061da546Spatrick bool PythonFile::Check(PyObject *py_obj) { 921*061da546Spatrick if (!py_obj) 922*061da546Spatrick return false; 923*061da546Spatrick #if PY_MAJOR_VERSION < 3 924*061da546Spatrick return PyFile_Check(py_obj); 925*061da546Spatrick #else 926*061da546Spatrick // In Python 3, there is no `PyFile_Check`, and in fact PyFile is not even a 927*061da546Spatrick // first-class object type anymore. `PyFile_FromFd` is just a thin wrapper 928*061da546Spatrick // over `io.open()`, which returns some object derived from `io.IOBase`. As a 929*061da546Spatrick // result, the only way to detect a file in Python 3 is to check whether it 930*061da546Spatrick // inherits from `io.IOBase`. 931*061da546Spatrick auto io_module = PythonModule::Import("io"); 932*061da546Spatrick if (!io_module) { 933*061da546Spatrick llvm::consumeError(io_module.takeError()); 934*061da546Spatrick return false; 935*061da546Spatrick } 936*061da546Spatrick auto iobase = io_module.get().Get("IOBase"); 937*061da546Spatrick if (!iobase) { 938*061da546Spatrick llvm::consumeError(iobase.takeError()); 939*061da546Spatrick return false; 940*061da546Spatrick } 941*061da546Spatrick int r = PyObject_IsInstance(py_obj, iobase.get().get()); 942*061da546Spatrick if (r < 0) { 943*061da546Spatrick llvm::consumeError(exception()); // clear the exception and log it. 944*061da546Spatrick return false; 945*061da546Spatrick } 946*061da546Spatrick return !!r; 947*061da546Spatrick #endif 948*061da546Spatrick } 949*061da546Spatrick 950*061da546Spatrick namespace { 951*061da546Spatrick class GIL { 952*061da546Spatrick public: 953*061da546Spatrick GIL() { 954*061da546Spatrick m_state = PyGILState_Ensure(); 955*061da546Spatrick assert(!PyErr_Occurred()); 956*061da546Spatrick } 957*061da546Spatrick ~GIL() { PyGILState_Release(m_state); } 958*061da546Spatrick 959*061da546Spatrick protected: 960*061da546Spatrick PyGILState_STATE m_state; 961*061da546Spatrick }; 962*061da546Spatrick } // namespace 963*061da546Spatrick 964*061da546Spatrick const char *PythonException::toCString() const { 965*061da546Spatrick if (!m_repr_bytes) 966*061da546Spatrick return "unknown exception"; 967*061da546Spatrick return PyBytes_AS_STRING(m_repr_bytes); 968*061da546Spatrick } 969*061da546Spatrick 970*061da546Spatrick PythonException::PythonException(const char *caller) { 971*061da546Spatrick assert(PyErr_Occurred()); 972*061da546Spatrick m_exception_type = m_exception = m_traceback = m_repr_bytes = NULL; 973*061da546Spatrick PyErr_Fetch(&m_exception_type, &m_exception, &m_traceback); 974*061da546Spatrick PyErr_NormalizeException(&m_exception_type, &m_exception, &m_traceback); 975*061da546Spatrick PyErr_Clear(); 976*061da546Spatrick if (m_exception) { 977*061da546Spatrick PyObject *repr = PyObject_Repr(m_exception); 978*061da546Spatrick if (repr) { 979*061da546Spatrick m_repr_bytes = PyUnicode_AsEncodedString(repr, "utf-8", nullptr); 980*061da546Spatrick if (!m_repr_bytes) { 981*061da546Spatrick PyErr_Clear(); 982*061da546Spatrick } 983*061da546Spatrick Py_XDECREF(repr); 984*061da546Spatrick } else { 985*061da546Spatrick PyErr_Clear(); 986*061da546Spatrick } 987*061da546Spatrick } 988*061da546Spatrick Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_SCRIPT); 989*061da546Spatrick if (caller) 990*061da546Spatrick LLDB_LOGF(log, "%s failed with exception: %s", caller, toCString()); 991*061da546Spatrick else 992*061da546Spatrick LLDB_LOGF(log, "python exception: %s", toCString()); 993*061da546Spatrick } 994*061da546Spatrick void PythonException::Restore() { 995*061da546Spatrick if (m_exception_type && m_exception) { 996*061da546Spatrick PyErr_Restore(m_exception_type, m_exception, m_traceback); 997*061da546Spatrick } else { 998*061da546Spatrick PyErr_SetString(PyExc_Exception, toCString()); 999*061da546Spatrick } 1000*061da546Spatrick m_exception_type = m_exception = m_traceback = NULL; 1001*061da546Spatrick } 1002*061da546Spatrick 1003*061da546Spatrick PythonException::~PythonException() { 1004*061da546Spatrick Py_XDECREF(m_exception_type); 1005*061da546Spatrick Py_XDECREF(m_exception); 1006*061da546Spatrick Py_XDECREF(m_traceback); 1007*061da546Spatrick Py_XDECREF(m_repr_bytes); 1008*061da546Spatrick } 1009*061da546Spatrick 1010*061da546Spatrick void PythonException::log(llvm::raw_ostream &OS) const { OS << toCString(); } 1011*061da546Spatrick 1012*061da546Spatrick std::error_code PythonException::convertToErrorCode() const { 1013*061da546Spatrick return llvm::inconvertibleErrorCode(); 1014*061da546Spatrick } 1015*061da546Spatrick 1016*061da546Spatrick bool PythonException::Matches(PyObject *exc) const { 1017*061da546Spatrick return PyErr_GivenExceptionMatches(m_exception_type, exc); 1018*061da546Spatrick } 1019*061da546Spatrick 1020*061da546Spatrick const char read_exception_script[] = R"( 1021*061da546Spatrick import sys 1022*061da546Spatrick from traceback import print_exception 1023*061da546Spatrick if sys.version_info.major < 3: 1024*061da546Spatrick from StringIO import StringIO 1025*061da546Spatrick else: 1026*061da546Spatrick from io import StringIO 1027*061da546Spatrick def main(exc_type, exc_value, tb): 1028*061da546Spatrick f = StringIO() 1029*061da546Spatrick print_exception(exc_type, exc_value, tb, file=f) 1030*061da546Spatrick return f.getvalue() 1031*061da546Spatrick )"; 1032*061da546Spatrick 1033*061da546Spatrick std::string PythonException::ReadBacktrace() const { 1034*061da546Spatrick 1035*061da546Spatrick if (!m_traceback) 1036*061da546Spatrick return toCString(); 1037*061da546Spatrick 1038*061da546Spatrick // no need to synchronize access to this global, we already have the GIL 1039*061da546Spatrick static PythonScript read_exception(read_exception_script); 1040*061da546Spatrick 1041*061da546Spatrick Expected<std::string> backtrace = As<std::string>( 1042*061da546Spatrick read_exception(m_exception_type, m_exception, m_traceback)); 1043*061da546Spatrick 1044*061da546Spatrick if (!backtrace) { 1045*061da546Spatrick std::string message = 1046*061da546Spatrick std::string(toCString()) + "\n" + 1047*061da546Spatrick "Traceback unavailble, an error occurred while reading it:\n"; 1048*061da546Spatrick return (message + llvm::toString(backtrace.takeError())); 1049*061da546Spatrick } 1050*061da546Spatrick 1051*061da546Spatrick return std::move(backtrace.get()); 1052*061da546Spatrick } 1053*061da546Spatrick 1054*061da546Spatrick char PythonException::ID = 0; 1055*061da546Spatrick 1056*061da546Spatrick llvm::Expected<File::OpenOptions> 1057*061da546Spatrick GetOptionsForPyObject(const PythonObject &obj) { 1058*061da546Spatrick #if PY_MAJOR_VERSION >= 3 1059*061da546Spatrick auto options = File::OpenOptions(0); 1060*061da546Spatrick auto readable = As<bool>(obj.CallMethod("readable")); 1061*061da546Spatrick if (!readable) 1062*061da546Spatrick return readable.takeError(); 1063*061da546Spatrick auto writable = As<bool>(obj.CallMethod("writable")); 1064*061da546Spatrick if (!writable) 1065*061da546Spatrick return writable.takeError(); 1066*061da546Spatrick if (readable.get()) 1067*061da546Spatrick options |= File::eOpenOptionRead; 1068*061da546Spatrick if (writable.get()) 1069*061da546Spatrick options |= File::eOpenOptionWrite; 1070*061da546Spatrick return options; 1071*061da546Spatrick #else 1072*061da546Spatrick PythonString py_mode = obj.GetAttributeValue("mode").AsType<PythonString>(); 1073*061da546Spatrick return File::GetOptionsFromMode(py_mode.GetString()); 1074*061da546Spatrick #endif 1075*061da546Spatrick } 1076*061da546Spatrick 1077*061da546Spatrick // Base class template for python files. All it knows how to do 1078*061da546Spatrick // is hold a reference to the python object and close or flush it 1079*061da546Spatrick // when the File is closed. 1080*061da546Spatrick namespace { 1081*061da546Spatrick template <typename Base> class OwnedPythonFile : public Base { 1082*061da546Spatrick public: 1083*061da546Spatrick template <typename... Args> 1084*061da546Spatrick OwnedPythonFile(const PythonFile &file, bool borrowed, Args... args) 1085*061da546Spatrick : Base(args...), m_py_obj(file), m_borrowed(borrowed) { 1086*061da546Spatrick assert(m_py_obj); 1087*061da546Spatrick } 1088*061da546Spatrick 1089*061da546Spatrick ~OwnedPythonFile() override { 1090*061da546Spatrick assert(m_py_obj); 1091*061da546Spatrick GIL takeGIL; 1092*061da546Spatrick Close(); 1093*061da546Spatrick // we need to ensure the python object is released while we still 1094*061da546Spatrick // hold the GIL 1095*061da546Spatrick m_py_obj.Reset(); 1096*061da546Spatrick } 1097*061da546Spatrick 1098*061da546Spatrick bool IsPythonSideValid() const { 1099*061da546Spatrick GIL takeGIL; 1100*061da546Spatrick auto closed = As<bool>(m_py_obj.GetAttribute("closed")); 1101*061da546Spatrick if (!closed) { 1102*061da546Spatrick llvm::consumeError(closed.takeError()); 1103*061da546Spatrick return false; 1104*061da546Spatrick } 1105*061da546Spatrick return !closed.get(); 1106*061da546Spatrick } 1107*061da546Spatrick 1108*061da546Spatrick bool IsValid() const override { 1109*061da546Spatrick return IsPythonSideValid() && Base::IsValid(); 1110*061da546Spatrick } 1111*061da546Spatrick 1112*061da546Spatrick Status Close() override { 1113*061da546Spatrick assert(m_py_obj); 1114*061da546Spatrick Status py_error, base_error; 1115*061da546Spatrick GIL takeGIL; 1116*061da546Spatrick if (!m_borrowed) { 1117*061da546Spatrick auto r = m_py_obj.CallMethod("close"); 1118*061da546Spatrick if (!r) 1119*061da546Spatrick py_error = Status(r.takeError()); 1120*061da546Spatrick } 1121*061da546Spatrick base_error = Base::Close(); 1122*061da546Spatrick if (py_error.Fail()) 1123*061da546Spatrick return py_error; 1124*061da546Spatrick return base_error; 1125*061da546Spatrick }; 1126*061da546Spatrick 1127*061da546Spatrick PyObject *GetPythonObject() const { 1128*061da546Spatrick assert(m_py_obj.IsValid()); 1129*061da546Spatrick return m_py_obj.get(); 1130*061da546Spatrick } 1131*061da546Spatrick 1132*061da546Spatrick static bool classof(const File *file) = delete; 1133*061da546Spatrick 1134*061da546Spatrick protected: 1135*061da546Spatrick PythonFile m_py_obj; 1136*061da546Spatrick bool m_borrowed; 1137*061da546Spatrick }; 1138*061da546Spatrick } // namespace 1139*061da546Spatrick 1140*061da546Spatrick // A SimplePythonFile is a OwnedPythonFile that just does all I/O as 1141*061da546Spatrick // a NativeFile 1142*061da546Spatrick namespace { 1143*061da546Spatrick class SimplePythonFile : public OwnedPythonFile<NativeFile> { 1144*061da546Spatrick public: 1145*061da546Spatrick SimplePythonFile(const PythonFile &file, bool borrowed, int fd, 1146*061da546Spatrick File::OpenOptions options) 1147*061da546Spatrick : OwnedPythonFile(file, borrowed, fd, options, false) {} 1148*061da546Spatrick 1149*061da546Spatrick static char ID; 1150*061da546Spatrick bool isA(const void *classID) const override { 1151*061da546Spatrick return classID == &ID || NativeFile::isA(classID); 1152*061da546Spatrick } 1153*061da546Spatrick static bool classof(const File *file) { return file->isA(&ID); } 1154*061da546Spatrick }; 1155*061da546Spatrick char SimplePythonFile::ID = 0; 1156*061da546Spatrick } // namespace 1157*061da546Spatrick 1158*061da546Spatrick #if PY_MAJOR_VERSION >= 3 1159*061da546Spatrick 1160*061da546Spatrick namespace { 1161*061da546Spatrick class PythonBuffer { 1162*061da546Spatrick public: 1163*061da546Spatrick PythonBuffer &operator=(const PythonBuffer &) = delete; 1164*061da546Spatrick PythonBuffer(const PythonBuffer &) = delete; 1165*061da546Spatrick 1166*061da546Spatrick static Expected<PythonBuffer> Create(PythonObject &obj, 1167*061da546Spatrick int flags = PyBUF_SIMPLE) { 1168*061da546Spatrick Py_buffer py_buffer = {}; 1169*061da546Spatrick PyObject_GetBuffer(obj.get(), &py_buffer, flags); 1170*061da546Spatrick if (!py_buffer.obj) 1171*061da546Spatrick return llvm::make_error<PythonException>(); 1172*061da546Spatrick return PythonBuffer(py_buffer); 1173*061da546Spatrick } 1174*061da546Spatrick 1175*061da546Spatrick PythonBuffer(PythonBuffer &&other) { 1176*061da546Spatrick m_buffer = other.m_buffer; 1177*061da546Spatrick other.m_buffer.obj = nullptr; 1178*061da546Spatrick } 1179*061da546Spatrick 1180*061da546Spatrick ~PythonBuffer() { 1181*061da546Spatrick if (m_buffer.obj) 1182*061da546Spatrick PyBuffer_Release(&m_buffer); 1183*061da546Spatrick } 1184*061da546Spatrick 1185*061da546Spatrick Py_buffer &get() { return m_buffer; } 1186*061da546Spatrick 1187*061da546Spatrick private: 1188*061da546Spatrick // takes ownership of the buffer. 1189*061da546Spatrick PythonBuffer(const Py_buffer &py_buffer) : m_buffer(py_buffer) {} 1190*061da546Spatrick Py_buffer m_buffer; 1191*061da546Spatrick }; 1192*061da546Spatrick } // namespace 1193*061da546Spatrick 1194*061da546Spatrick // Shared methods between TextPythonFile and BinaryPythonFile 1195*061da546Spatrick namespace { 1196*061da546Spatrick class PythonIOFile : public OwnedPythonFile<File> { 1197*061da546Spatrick public: 1198*061da546Spatrick PythonIOFile(const PythonFile &file, bool borrowed) 1199*061da546Spatrick : OwnedPythonFile(file, borrowed) {} 1200*061da546Spatrick 1201*061da546Spatrick ~PythonIOFile() override { Close(); } 1202*061da546Spatrick 1203*061da546Spatrick bool IsValid() const override { return IsPythonSideValid(); } 1204*061da546Spatrick 1205*061da546Spatrick Status Close() override { 1206*061da546Spatrick assert(m_py_obj); 1207*061da546Spatrick GIL takeGIL; 1208*061da546Spatrick if (m_borrowed) 1209*061da546Spatrick return Flush(); 1210*061da546Spatrick auto r = m_py_obj.CallMethod("close"); 1211*061da546Spatrick if (!r) 1212*061da546Spatrick return Status(r.takeError()); 1213*061da546Spatrick return Status(); 1214*061da546Spatrick } 1215*061da546Spatrick 1216*061da546Spatrick Status Flush() override { 1217*061da546Spatrick GIL takeGIL; 1218*061da546Spatrick auto r = m_py_obj.CallMethod("flush"); 1219*061da546Spatrick if (!r) 1220*061da546Spatrick return Status(r.takeError()); 1221*061da546Spatrick return Status(); 1222*061da546Spatrick } 1223*061da546Spatrick 1224*061da546Spatrick Expected<File::OpenOptions> GetOptions() const override { 1225*061da546Spatrick GIL takeGIL; 1226*061da546Spatrick return GetOptionsForPyObject(m_py_obj); 1227*061da546Spatrick } 1228*061da546Spatrick 1229*061da546Spatrick static char ID; 1230*061da546Spatrick bool isA(const void *classID) const override { 1231*061da546Spatrick return classID == &ID || File::isA(classID); 1232*061da546Spatrick } 1233*061da546Spatrick static bool classof(const File *file) { return file->isA(&ID); } 1234*061da546Spatrick }; 1235*061da546Spatrick char PythonIOFile::ID = 0; 1236*061da546Spatrick } // namespace 1237*061da546Spatrick 1238*061da546Spatrick namespace { 1239*061da546Spatrick class BinaryPythonFile : public PythonIOFile { 1240*061da546Spatrick protected: 1241*061da546Spatrick int m_descriptor; 1242*061da546Spatrick 1243*061da546Spatrick public: 1244*061da546Spatrick BinaryPythonFile(int fd, const PythonFile &file, bool borrowed) 1245*061da546Spatrick : PythonIOFile(file, borrowed), 1246*061da546Spatrick m_descriptor(File::DescriptorIsValid(fd) ? fd 1247*061da546Spatrick : File::kInvalidDescriptor) {} 1248*061da546Spatrick 1249*061da546Spatrick int GetDescriptor() const override { return m_descriptor; } 1250*061da546Spatrick 1251*061da546Spatrick Status Write(const void *buf, size_t &num_bytes) override { 1252*061da546Spatrick GIL takeGIL; 1253*061da546Spatrick PyObject *pybuffer_p = PyMemoryView_FromMemory( 1254*061da546Spatrick const_cast<char *>((const char *)buf), num_bytes, PyBUF_READ); 1255*061da546Spatrick if (!pybuffer_p) 1256*061da546Spatrick return Status(llvm::make_error<PythonException>()); 1257*061da546Spatrick auto pybuffer = Take<PythonObject>(pybuffer_p); 1258*061da546Spatrick num_bytes = 0; 1259*061da546Spatrick auto bytes_written = As<long long>(m_py_obj.CallMethod("write", pybuffer)); 1260*061da546Spatrick if (!bytes_written) 1261*061da546Spatrick return Status(bytes_written.takeError()); 1262*061da546Spatrick if (bytes_written.get() < 0) 1263*061da546Spatrick return Status(".write() method returned a negative number!"); 1264*061da546Spatrick static_assert(sizeof(long long) >= sizeof(size_t), "overflow"); 1265*061da546Spatrick num_bytes = bytes_written.get(); 1266*061da546Spatrick return Status(); 1267*061da546Spatrick } 1268*061da546Spatrick 1269*061da546Spatrick Status Read(void *buf, size_t &num_bytes) override { 1270*061da546Spatrick GIL takeGIL; 1271*061da546Spatrick static_assert(sizeof(long long) >= sizeof(size_t), "overflow"); 1272*061da546Spatrick auto pybuffer_obj = 1273*061da546Spatrick m_py_obj.CallMethod("read", (unsigned long long)num_bytes); 1274*061da546Spatrick if (!pybuffer_obj) 1275*061da546Spatrick return Status(pybuffer_obj.takeError()); 1276*061da546Spatrick num_bytes = 0; 1277*061da546Spatrick if (pybuffer_obj.get().IsNone()) { 1278*061da546Spatrick // EOF 1279*061da546Spatrick num_bytes = 0; 1280*061da546Spatrick return Status(); 1281*061da546Spatrick } 1282*061da546Spatrick auto pybuffer = PythonBuffer::Create(pybuffer_obj.get()); 1283*061da546Spatrick if (!pybuffer) 1284*061da546Spatrick return Status(pybuffer.takeError()); 1285*061da546Spatrick memcpy(buf, pybuffer.get().get().buf, pybuffer.get().get().len); 1286*061da546Spatrick num_bytes = pybuffer.get().get().len; 1287*061da546Spatrick return Status(); 1288*061da546Spatrick } 1289*061da546Spatrick }; 1290*061da546Spatrick } // namespace 1291*061da546Spatrick 1292*061da546Spatrick namespace { 1293*061da546Spatrick class TextPythonFile : public PythonIOFile { 1294*061da546Spatrick protected: 1295*061da546Spatrick int m_descriptor; 1296*061da546Spatrick 1297*061da546Spatrick public: 1298*061da546Spatrick TextPythonFile(int fd, const PythonFile &file, bool borrowed) 1299*061da546Spatrick : PythonIOFile(file, borrowed), 1300*061da546Spatrick m_descriptor(File::DescriptorIsValid(fd) ? fd 1301*061da546Spatrick : File::kInvalidDescriptor) {} 1302*061da546Spatrick 1303*061da546Spatrick int GetDescriptor() const override { return m_descriptor; } 1304*061da546Spatrick 1305*061da546Spatrick Status Write(const void *buf, size_t &num_bytes) override { 1306*061da546Spatrick GIL takeGIL; 1307*061da546Spatrick auto pystring = 1308*061da546Spatrick PythonString::FromUTF8(llvm::StringRef((const char *)buf, num_bytes)); 1309*061da546Spatrick if (!pystring) 1310*061da546Spatrick return Status(pystring.takeError()); 1311*061da546Spatrick num_bytes = 0; 1312*061da546Spatrick auto bytes_written = 1313*061da546Spatrick As<long long>(m_py_obj.CallMethod("write", pystring.get())); 1314*061da546Spatrick if (!bytes_written) 1315*061da546Spatrick return Status(bytes_written.takeError()); 1316*061da546Spatrick if (bytes_written.get() < 0) 1317*061da546Spatrick return Status(".write() method returned a negative number!"); 1318*061da546Spatrick static_assert(sizeof(long long) >= sizeof(size_t), "overflow"); 1319*061da546Spatrick num_bytes = bytes_written.get(); 1320*061da546Spatrick return Status(); 1321*061da546Spatrick } 1322*061da546Spatrick 1323*061da546Spatrick Status Read(void *buf, size_t &num_bytes) override { 1324*061da546Spatrick GIL takeGIL; 1325*061da546Spatrick size_t num_chars = num_bytes / 6; 1326*061da546Spatrick size_t orig_num_bytes = num_bytes; 1327*061da546Spatrick num_bytes = 0; 1328*061da546Spatrick if (orig_num_bytes < 6) { 1329*061da546Spatrick return Status("can't read less than 6 bytes from a utf8 text stream"); 1330*061da546Spatrick } 1331*061da546Spatrick auto pystring = As<PythonString>( 1332*061da546Spatrick m_py_obj.CallMethod("read", (unsigned long long)num_chars)); 1333*061da546Spatrick if (!pystring) 1334*061da546Spatrick return Status(pystring.takeError()); 1335*061da546Spatrick if (pystring.get().IsNone()) { 1336*061da546Spatrick // EOF 1337*061da546Spatrick return Status(); 1338*061da546Spatrick } 1339*061da546Spatrick auto stringref = pystring.get().AsUTF8(); 1340*061da546Spatrick if (!stringref) 1341*061da546Spatrick return Status(stringref.takeError()); 1342*061da546Spatrick num_bytes = stringref.get().size(); 1343*061da546Spatrick memcpy(buf, stringref.get().begin(), num_bytes); 1344*061da546Spatrick return Status(); 1345*061da546Spatrick } 1346*061da546Spatrick }; 1347*061da546Spatrick } // namespace 1348*061da546Spatrick 1349*061da546Spatrick #endif 1350*061da546Spatrick 1351*061da546Spatrick llvm::Expected<FileSP> PythonFile::ConvertToFile(bool borrowed) { 1352*061da546Spatrick if (!IsValid()) 1353*061da546Spatrick return llvm::createStringError(llvm::inconvertibleErrorCode(), 1354*061da546Spatrick "invalid PythonFile"); 1355*061da546Spatrick 1356*061da546Spatrick int fd = PyObject_AsFileDescriptor(m_py_obj); 1357*061da546Spatrick if (fd < 0) { 1358*061da546Spatrick PyErr_Clear(); 1359*061da546Spatrick return ConvertToFileForcingUseOfScriptingIOMethods(borrowed); 1360*061da546Spatrick } 1361*061da546Spatrick auto options = GetOptionsForPyObject(*this); 1362*061da546Spatrick if (!options) 1363*061da546Spatrick return options.takeError(); 1364*061da546Spatrick 1365*061da546Spatrick if (options.get() & File::eOpenOptionWrite) { 1366*061da546Spatrick // LLDB and python will not share I/O buffers. We should probably 1367*061da546Spatrick // flush the python buffers now. 1368*061da546Spatrick auto r = CallMethod("flush"); 1369*061da546Spatrick if (!r) 1370*061da546Spatrick return r.takeError(); 1371*061da546Spatrick } 1372*061da546Spatrick 1373*061da546Spatrick FileSP file_sp; 1374*061da546Spatrick if (borrowed) { 1375*061da546Spatrick // In this case we we don't need to retain the python 1376*061da546Spatrick // object at all. 1377*061da546Spatrick file_sp = std::make_shared<NativeFile>(fd, options.get(), false); 1378*061da546Spatrick } else { 1379*061da546Spatrick file_sp = std::static_pointer_cast<File>( 1380*061da546Spatrick std::make_shared<SimplePythonFile>(*this, borrowed, fd, options.get())); 1381*061da546Spatrick } 1382*061da546Spatrick if (!file_sp->IsValid()) 1383*061da546Spatrick return llvm::createStringError(llvm::inconvertibleErrorCode(), 1384*061da546Spatrick "invalid File"); 1385*061da546Spatrick 1386*061da546Spatrick return file_sp; 1387*061da546Spatrick } 1388*061da546Spatrick 1389*061da546Spatrick llvm::Expected<FileSP> 1390*061da546Spatrick PythonFile::ConvertToFileForcingUseOfScriptingIOMethods(bool borrowed) { 1391*061da546Spatrick 1392*061da546Spatrick assert(!PyErr_Occurred()); 1393*061da546Spatrick 1394*061da546Spatrick if (!IsValid()) 1395*061da546Spatrick return llvm::createStringError(llvm::inconvertibleErrorCode(), 1396*061da546Spatrick "invalid PythonFile"); 1397*061da546Spatrick 1398*061da546Spatrick #if PY_MAJOR_VERSION < 3 1399*061da546Spatrick 1400*061da546Spatrick return llvm::createStringError(llvm::inconvertibleErrorCode(), 1401*061da546Spatrick "not supported on python 2"); 1402*061da546Spatrick 1403*061da546Spatrick #else 1404*061da546Spatrick 1405*061da546Spatrick int fd = PyObject_AsFileDescriptor(m_py_obj); 1406*061da546Spatrick if (fd < 0) { 1407*061da546Spatrick PyErr_Clear(); 1408*061da546Spatrick fd = File::kInvalidDescriptor; 1409*061da546Spatrick } 1410*061da546Spatrick 1411*061da546Spatrick auto io_module = PythonModule::Import("io"); 1412*061da546Spatrick if (!io_module) 1413*061da546Spatrick return io_module.takeError(); 1414*061da546Spatrick auto textIOBase = io_module.get().Get("TextIOBase"); 1415*061da546Spatrick if (!textIOBase) 1416*061da546Spatrick return textIOBase.takeError(); 1417*061da546Spatrick auto rawIOBase = io_module.get().Get("RawIOBase"); 1418*061da546Spatrick if (!rawIOBase) 1419*061da546Spatrick return rawIOBase.takeError(); 1420*061da546Spatrick auto bufferedIOBase = io_module.get().Get("BufferedIOBase"); 1421*061da546Spatrick if (!bufferedIOBase) 1422*061da546Spatrick return bufferedIOBase.takeError(); 1423*061da546Spatrick 1424*061da546Spatrick FileSP file_sp; 1425*061da546Spatrick 1426*061da546Spatrick auto isTextIO = IsInstance(textIOBase.get()); 1427*061da546Spatrick if (!isTextIO) 1428*061da546Spatrick return isTextIO.takeError(); 1429*061da546Spatrick if (isTextIO.get()) 1430*061da546Spatrick file_sp = std::static_pointer_cast<File>( 1431*061da546Spatrick std::make_shared<TextPythonFile>(fd, *this, borrowed)); 1432*061da546Spatrick 1433*061da546Spatrick auto isRawIO = IsInstance(rawIOBase.get()); 1434*061da546Spatrick if (!isRawIO) 1435*061da546Spatrick return isRawIO.takeError(); 1436*061da546Spatrick auto isBufferedIO = IsInstance(bufferedIOBase.get()); 1437*061da546Spatrick if (!isBufferedIO) 1438*061da546Spatrick return isBufferedIO.takeError(); 1439*061da546Spatrick 1440*061da546Spatrick if (isRawIO.get() || isBufferedIO.get()) { 1441*061da546Spatrick file_sp = std::static_pointer_cast<File>( 1442*061da546Spatrick std::make_shared<BinaryPythonFile>(fd, *this, borrowed)); 1443*061da546Spatrick } 1444*061da546Spatrick 1445*061da546Spatrick if (!file_sp) 1446*061da546Spatrick return llvm::createStringError(llvm::inconvertibleErrorCode(), 1447*061da546Spatrick "python file is neither text nor binary"); 1448*061da546Spatrick 1449*061da546Spatrick if (!file_sp->IsValid()) 1450*061da546Spatrick return llvm::createStringError(llvm::inconvertibleErrorCode(), 1451*061da546Spatrick "invalid File"); 1452*061da546Spatrick 1453*061da546Spatrick return file_sp; 1454*061da546Spatrick 1455*061da546Spatrick #endif 1456*061da546Spatrick } 1457*061da546Spatrick 1458*061da546Spatrick Expected<PythonFile> PythonFile::FromFile(File &file, const char *mode) { 1459*061da546Spatrick if (!file.IsValid()) 1460*061da546Spatrick return llvm::createStringError(llvm::inconvertibleErrorCode(), 1461*061da546Spatrick "invalid file"); 1462*061da546Spatrick 1463*061da546Spatrick if (auto *simple = llvm::dyn_cast<SimplePythonFile>(&file)) 1464*061da546Spatrick return Retain<PythonFile>(simple->GetPythonObject()); 1465*061da546Spatrick #if PY_MAJOR_VERSION >= 3 1466*061da546Spatrick if (auto *pythonio = llvm::dyn_cast<PythonIOFile>(&file)) 1467*061da546Spatrick return Retain<PythonFile>(pythonio->GetPythonObject()); 1468*061da546Spatrick #endif 1469*061da546Spatrick 1470*061da546Spatrick if (!mode) { 1471*061da546Spatrick auto m = file.GetOpenMode(); 1472*061da546Spatrick if (!m) 1473*061da546Spatrick return m.takeError(); 1474*061da546Spatrick mode = m.get(); 1475*061da546Spatrick } 1476*061da546Spatrick 1477*061da546Spatrick PyObject *file_obj; 1478*061da546Spatrick #if PY_MAJOR_VERSION >= 3 1479*061da546Spatrick file_obj = PyFile_FromFd(file.GetDescriptor(), nullptr, mode, -1, nullptr, 1480*061da546Spatrick "ignore", nullptr, /*closefd=*/0); 1481*061da546Spatrick #else 1482*061da546Spatrick // I'd like to pass ::fflush here if the file is writable, so that 1483*061da546Spatrick // when the python side destructs the file object it will be flushed. 1484*061da546Spatrick // However, this would be dangerous. It can cause fflush to be called 1485*061da546Spatrick // after fclose if the python program keeps a reference to the file after 1486*061da546Spatrick // the original lldb_private::File has been destructed. 1487*061da546Spatrick // 1488*061da546Spatrick // It's all well and good to ask a python program not to use a closed file 1489*061da546Spatrick // but asking a python program to make sure objects get released in a 1490*061da546Spatrick // particular order is not safe. 1491*061da546Spatrick // 1492*061da546Spatrick // The tradeoff here is that if a python 2 program wants to make sure this 1493*061da546Spatrick // file gets flushed, they'll have to do it explicitly or wait untill the 1494*061da546Spatrick // original lldb File itself gets flushed. 1495*061da546Spatrick file_obj = PyFile_FromFile(file.GetStream(), py2_const_cast(""), 1496*061da546Spatrick py2_const_cast(mode), [](FILE *) { return 0; }); 1497*061da546Spatrick #endif 1498*061da546Spatrick 1499*061da546Spatrick if (!file_obj) 1500*061da546Spatrick return exception(); 1501*061da546Spatrick 1502*061da546Spatrick return Take<PythonFile>(file_obj); 1503*061da546Spatrick } 1504*061da546Spatrick 1505*061da546Spatrick Error PythonScript::Init() { 1506*061da546Spatrick if (function.IsValid()) 1507*061da546Spatrick return Error::success(); 1508*061da546Spatrick 1509*061da546Spatrick PythonDictionary globals(PyInitialValue::Empty); 1510*061da546Spatrick auto builtins = PythonModule::BuiltinsModule(); 1511*061da546Spatrick if (Error error = globals.SetItem("__builtins__", builtins)) 1512*061da546Spatrick return error; 1513*061da546Spatrick PyObject *o = 1514*061da546Spatrick PyRun_String(script, Py_file_input, globals.get(), globals.get()); 1515*061da546Spatrick if (!o) 1516*061da546Spatrick return exception(); 1517*061da546Spatrick Take<PythonObject>(o); 1518*061da546Spatrick auto f = As<PythonCallable>(globals.GetItem("main")); 1519*061da546Spatrick if (!f) 1520*061da546Spatrick return f.takeError(); 1521*061da546Spatrick function = std::move(f.get()); 1522*061da546Spatrick 1523*061da546Spatrick return Error::success(); 1524*061da546Spatrick } 1525*061da546Spatrick 1526*061da546Spatrick llvm::Expected<PythonObject> 1527*061da546Spatrick python::runStringOneLine(const llvm::Twine &string, 1528*061da546Spatrick const PythonDictionary &globals, 1529*061da546Spatrick const PythonDictionary &locals) { 1530*061da546Spatrick if (!globals.IsValid() || !locals.IsValid()) 1531*061da546Spatrick return nullDeref(); 1532*061da546Spatrick 1533*061da546Spatrick PyObject *code = 1534*061da546Spatrick Py_CompileString(NullTerminated(string), "<string>", Py_eval_input); 1535*061da546Spatrick if (!code) { 1536*061da546Spatrick PyErr_Clear(); 1537*061da546Spatrick code = 1538*061da546Spatrick Py_CompileString(NullTerminated(string), "<string>", Py_single_input); 1539*061da546Spatrick } 1540*061da546Spatrick if (!code) 1541*061da546Spatrick return exception(); 1542*061da546Spatrick auto code_ref = Take<PythonObject>(code); 1543*061da546Spatrick 1544*061da546Spatrick #if PY_MAJOR_VERSION < 3 1545*061da546Spatrick PyObject *result = 1546*061da546Spatrick PyEval_EvalCode((PyCodeObject *)code, globals.get(), locals.get()); 1547*061da546Spatrick #else 1548*061da546Spatrick PyObject *result = PyEval_EvalCode(code, globals.get(), locals.get()); 1549*061da546Spatrick #endif 1550*061da546Spatrick 1551*061da546Spatrick if (!result) 1552*061da546Spatrick return exception(); 1553*061da546Spatrick 1554*061da546Spatrick return Take<PythonObject>(result); 1555*061da546Spatrick } 1556*061da546Spatrick 1557*061da546Spatrick llvm::Expected<PythonObject> 1558*061da546Spatrick python::runStringMultiLine(const llvm::Twine &string, 1559*061da546Spatrick const PythonDictionary &globals, 1560*061da546Spatrick const PythonDictionary &locals) { 1561*061da546Spatrick if (!globals.IsValid() || !locals.IsValid()) 1562*061da546Spatrick return nullDeref(); 1563*061da546Spatrick PyObject *result = PyRun_String(NullTerminated(string), Py_file_input, 1564*061da546Spatrick globals.get(), locals.get()); 1565*061da546Spatrick if (!result) 1566*061da546Spatrick return exception(); 1567*061da546Spatrick return Take<PythonObject>(result); 1568*061da546Spatrick } 1569*061da546Spatrick 1570*061da546Spatrick #endif 1571