summaryrefslogtreecommitdiff
path: root/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2019-08-20 20:51:52 +0000
committerDimitry Andric <dim@FreeBSD.org>2019-08-20 20:51:52 +0000
commit5f29bb8a675e8f96452b632e7129113f7dec850e (patch)
tree3d3f2a0d3ad10872a4dcaba8ec8d1d20c87ab147 /source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
parent88c643b6fec27eec436c8d138fee6346e92337d6 (diff)
Notes
Diffstat (limited to 'source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp')
-rw-r--r--source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp99
1 files changed, 57 insertions, 42 deletions
diff --git a/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp b/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
index 7e96dd9893c8..29dd037efd86 100644
--- a/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
+++ b/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
@@ -1,9 +1,8 @@
//===-- PythonDataObjects.cpp -----------------------------------*- C++ -*-===//
//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
@@ -21,12 +20,12 @@
#include "lldb/Interpreter/ScriptInterpreter.h"
#include "lldb/Utility/Stream.h"
+#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/ConvertUTF.h"
+#include "llvm/Support/Errno.h"
#include <stdio.h>
-#include "llvm/ADT/StringSwitch.h"
-
using namespace lldb_private;
using namespace lldb;
@@ -34,13 +33,11 @@ void StructuredPythonObject::Dump(Stream &s, bool pretty_print) const {
s << "Python Obj: 0x" << GetValue();
}
-//----------------------------------------------------------------------
// PythonObject
-//----------------------------------------------------------------------
void PythonObject::Dump(Stream &strm) const {
if (m_py_obj) {
- FILE *file = ::tmpfile();
+ FILE *file = llvm::sys::RetryAfterSignal(nullptr, ::tmpfile);
if (file) {
::PyObject_Print(m_py_obj, file, 0);
const long length = ftell(file);
@@ -78,6 +75,8 @@ PyObjectType PythonObject::GetObjectType() const {
#endif
if (PythonByteArray::Check(m_py_obj))
return PyObjectType::ByteArray;
+ if (PythonBoolean::Check(m_py_obj))
+ return PyObjectType::Boolean;
if (PythonInteger::Check(m_py_obj))
return PyObjectType::Integer;
if (PythonFile::Check(m_py_obj))
@@ -108,7 +107,7 @@ PythonString PythonObject::Str() const {
PythonObject
PythonObject::ResolveNameWithDictionary(llvm::StringRef name,
const PythonDictionary &dict) {
- size_t dot_pos = name.find_first_of('.');
+ size_t dot_pos = name.find('.');
llvm::StringRef piece = name.substr(0, dot_pos);
PythonObject result = dict.GetItemForKey(PythonString(piece));
if (dot_pos == llvm::StringRef::npos) {
@@ -132,7 +131,7 @@ PythonObject PythonObject::ResolveName(llvm::StringRef name) const {
// refers to the `sys` module, and `name` == "path.append", then it will find
// the function `sys.path.append`.
- size_t dot_pos = name.find_first_of('.');
+ size_t dot_pos = name.find('.');
if (dot_pos == llvm::StringRef::npos) {
// No dots in the name, we should be able to find the value immediately as
// an attribute of `m_py_obj`.
@@ -179,6 +178,9 @@ StructuredData::ObjectSP PythonObject::CreateStructuredObject() const {
case PyObjectType::Dictionary:
return PythonDictionary(PyRefType::Borrowed, m_py_obj)
.CreateStructuredDictionary();
+ case PyObjectType::Boolean:
+ return PythonBoolean(PyRefType::Borrowed, m_py_obj)
+ .CreateStructuredBoolean();
case PyObjectType::Integer:
return PythonInteger(PyRefType::Borrowed, m_py_obj)
.CreateStructuredInteger();
@@ -198,9 +200,7 @@ StructuredData::ObjectSP PythonObject::CreateStructuredObject() const {
}
}
-//----------------------------------------------------------------------
// PythonString
-//----------------------------------------------------------------------
PythonBytes::PythonBytes() : PythonObject() {}
PythonBytes::PythonBytes(llvm::ArrayRef<uint8_t> bytes) : PythonObject() {
@@ -215,8 +215,6 @@ PythonBytes::PythonBytes(PyRefType type, PyObject *py_obj) : PythonObject() {
Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a string
}
-PythonBytes::PythonBytes(const PythonBytes &object) : PythonObject(object) {}
-
PythonBytes::~PythonBytes() {}
bool PythonBytes::Check(PyObject *py_obj) {
@@ -334,16 +332,12 @@ StructuredData::StringSP PythonByteArray::CreateStructuredString() const {
return result;
}
-//----------------------------------------------------------------------
// PythonString
-//----------------------------------------------------------------------
PythonString::PythonString(PyRefType type, PyObject *py_obj) : PythonObject() {
Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a string
}
-PythonString::PythonString(const PythonString &object) : PythonObject(object) {}
-
PythonString::PythonString(llvm::StringRef string) : PythonObject() {
SetString(string);
}
@@ -434,9 +428,7 @@ StructuredData::StringSP PythonString::CreateStructuredString() const {
return result;
}
-//----------------------------------------------------------------------
// PythonInteger
-//----------------------------------------------------------------------
PythonInteger::PythonInteger() : PythonObject() {}
@@ -445,9 +437,6 @@ PythonInteger::PythonInteger(PyRefType type, PyObject *py_obj)
Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a integer type
}
-PythonInteger::PythonInteger(const PythonInteger &object)
- : PythonObject(object) {}
-
PythonInteger::PythonInteger(int64_t value) : PythonObject() {
SetInteger(value);
}
@@ -526,9 +515,51 @@ StructuredData::IntegerSP PythonInteger::CreateStructuredInteger() const {
return result;
}
-//----------------------------------------------------------------------
+// PythonBoolean
+
+PythonBoolean::PythonBoolean(PyRefType type, PyObject *py_obj)
+ : PythonObject() {
+ Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a boolean type
+}
+
+PythonBoolean::PythonBoolean(bool value) {
+ SetValue(value);
+}
+
+bool PythonBoolean::Check(PyObject *py_obj) {
+ return py_obj ? PyBool_Check(py_obj) : false;
+}
+
+void PythonBoolean::Reset(PyRefType type, PyObject *py_obj) {
+ // Grab the desired reference type so that if we end up rejecting `py_obj` it
+ // still gets decremented if necessary.
+ PythonObject result(type, py_obj);
+
+ if (!PythonBoolean::Check(py_obj)) {
+ PythonObject::Reset();
+ return;
+ }
+
+ // Calling PythonObject::Reset(const PythonObject&) will lead to stack
+ // overflow since it calls back into the virtual implementation.
+ PythonObject::Reset(PyRefType::Borrowed, result.get());
+}
+
+bool PythonBoolean::GetValue() const {
+ return m_py_obj ? PyObject_IsTrue(m_py_obj) : false;
+}
+
+void PythonBoolean::SetValue(bool value) {
+ PythonObject::Reset(PyRefType::Owned, PyBool_FromLong(value));
+}
+
+StructuredData::BooleanSP PythonBoolean::CreateStructuredBoolean() const {
+ StructuredData::BooleanSP result(new StructuredData::Boolean);
+ result->SetValue(GetValue());
+ return result;
+}
+
// PythonList
-//----------------------------------------------------------------------
PythonList::PythonList(PyInitialValue value) : PythonObject() {
if (value == PyInitialValue::Empty)
@@ -543,8 +574,6 @@ PythonList::PythonList(PyRefType type, PyObject *py_obj) : PythonObject() {
Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a list
}
-PythonList::PythonList(const PythonList &list) : PythonObject(list) {}
-
PythonList::~PythonList() {}
bool PythonList::Check(PyObject *py_obj) {
@@ -607,9 +636,7 @@ StructuredData::ArraySP PythonList::CreateStructuredArray() const {
return result;
}
-//----------------------------------------------------------------------
// PythonTuple
-//----------------------------------------------------------------------
PythonTuple::PythonTuple(PyInitialValue value) : PythonObject() {
if (value == PyInitialValue::Empty)
@@ -624,8 +651,6 @@ PythonTuple::PythonTuple(PyRefType type, PyObject *py_obj) : PythonObject() {
Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a tuple
}
-PythonTuple::PythonTuple(const PythonTuple &tuple) : PythonObject(tuple) {}
-
PythonTuple::PythonTuple(std::initializer_list<PythonObject> objects) {
m_py_obj = PyTuple_New(objects.size());
@@ -703,9 +728,7 @@ StructuredData::ArraySP PythonTuple::CreateStructuredArray() const {
return result;
}
-//----------------------------------------------------------------------
// PythonDictionary
-//----------------------------------------------------------------------
PythonDictionary::PythonDictionary(PyInitialValue value) : PythonObject() {
if (value == PyInitialValue::Empty)
@@ -717,9 +740,6 @@ PythonDictionary::PythonDictionary(PyRefType type, PyObject *py_obj)
Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a dictionary
}
-PythonDictionary::PythonDictionary(const PythonDictionary &object)
- : PythonObject(object) {}
-
PythonDictionary::~PythonDictionary() {}
bool PythonDictionary::Check(PyObject *py_obj) {
@@ -789,8 +809,6 @@ PythonModule::PythonModule(PyRefType type, PyObject *py_obj) {
Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a module
}
-PythonModule::PythonModule(const PythonModule &dict) : PythonObject(dict) {}
-
PythonModule::~PythonModule() {}
PythonModule PythonModule::BuiltinsModule() {
@@ -845,9 +863,6 @@ PythonCallable::PythonCallable(PyRefType type, PyObject *py_obj) {
Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a callable
}
-PythonCallable::PythonCallable(const PythonCallable &callable)
- : PythonObject(callable) {}
-
PythonCallable::~PythonCallable() {}
bool PythonCallable::Check(PyObject *py_obj) {