Skip to content
This repository has been archived by the owner on Apr 2, 2020. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'llvm/master' into upstream-with-swift
Browse files Browse the repository at this point in the history
  • Loading branch information
swift-ci committed Oct 17, 2019
2 parents 0fbd3c3 + f464db9 commit 56eccc8
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 145 deletions.
15 changes: 0 additions & 15 deletions include/lldb/Host/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,6 @@ class File : public IOObject {
/// ENOTSUP, success, or another error.
virtual Status GetFileSpec(FileSpec &file_spec) const;

/// DEPRECATED! Extract the underlying FILE* and reset this File without closing it.
///
/// This is only here to support legacy SB interfaces that need to convert scripting
/// language objects into FILE* streams. That conversion is inherently sketchy and
/// doing so may cause the stream to be leaked.
///
/// After calling this the File will be reset to its original state. It will be
/// invalid and it will not hold on to any resources.
///
/// \return
/// The underlying FILE* stream from this File, if one exists and can be extracted,
/// nullptr otherwise.
virtual FILE *TakeStreamAndClear();

/// Get underlying OS file descriptor for this file, or kInvalidDescriptor.
/// If the descriptor is valid, then it may be used directly for I/O
/// However, the File may also perform it's own buffering, so avoid using
Expand Down Expand Up @@ -413,7 +399,6 @@ class NativeFile : public File {
Status Close() override;
WaitableHandle GetWaitableHandle() override;
Status GetFileSpec(FileSpec &file_spec) const override;
FILE *TakeStreamAndClear() override;
int GetDescriptor() const override;
FILE *GetStream() override;
off_t SeekFromStart(off_t offset, Status *error_ptr = nullptr) override;
Expand Down
68 changes: 0 additions & 68 deletions scripts/Python/python-typemaps.swig
Original file line number Diff line number Diff line change
Expand Up @@ -451,74 +451,6 @@ bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
}
}

// FIXME both of these paths wind up calling fdopen() with no provision for ever calling
// fclose() on the result. SB interfaces that use FILE* should be deprecated for scripting
// use and this typemap should eventually be removed.
%typemap(in) FILE * {
using namespace lldb_private;
if ($input == Py_None)
$1 = nullptr;
else if (!lldb_private::PythonFile::Check($input)) {
int fd = PyObject_AsFileDescriptor($input);
if (fd < 0 || PyErr_Occurred())
return nullptr;
PythonObject py_input(PyRefType::Borrowed, $input);
PythonString py_mode = py_input.GetAttributeValue("mode").AsType<PythonString>();
if (!py_mode.IsValid() || PyErr_Occurred())
return nullptr;
FILE *f;
if ((f = fdopen(fd, py_mode.GetString().str().c_str())))
$1 = f;
else {
PyErr_SetString(PyExc_TypeError, strerror(errno));
return nullptr;
}
}
else
{
PythonFile py_file(PyRefType::Borrowed, $input);
lldb::FileUP file = py_file.GetUnderlyingFile();
if (!file)
return nullptr;
$1 = file->TakeStreamAndClear();
}
}

%typemap(out) FILE * {
// TODO: This is gross. We should find a way to fetch the mode flags from the
// lldb_private::File object.
char mode[4] = {0};
%#ifdef __APPLE__
int i = 0;
if ($1)
{
short flags = $1->_flags;

if (flags & __SRD)
mode[i++] = 'r';
else if (flags & __SWR)
mode[i++] = 'w';
else // if (flags & __SRW)
mode[i++] = 'a';
}
%#else
// There's no portable way to get the mode here. We just return a mode which
// permits both reads and writes and count on the operating system to return
// an error when an invalid operation is attempted.
mode[0] = 'r';
mode[1] = '+';
%#endif
using namespace lldb_private;
NativeFile file($1, false);
PythonFile py_file(file, mode);
$result = py_file.release();
if (!$result)
{
$result = Py_None;
Py_INCREF(Py_None);
}
}

%typemap(in) (const char* string, int len) {
using namespace lldb_private;
if ($input == Py_None)
Expand Down
21 changes: 10 additions & 11 deletions source/API/SBCommandReturnObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ size_t SBCommandReturnObject::GetErrorSize() {
}

size_t SBCommandReturnObject::PutOutput(FILE *fh) {
LLDB_RECORD_METHOD(size_t, SBCommandReturnObject, PutOutput, (FILE *), fh);
LLDB_RECORD_DUMMY(size_t, SBCommandReturnObject, PutOutput, (FILE *), fh);
if (fh) {
size_t num_bytes = GetOutputSize();
if (num_bytes)
Expand All @@ -141,8 +141,7 @@ size_t SBCommandReturnObject::PutOutput(SBFile file) {
}

size_t SBCommandReturnObject::PutError(FILE *fh) {
LLDB_RECORD_METHOD(size_t, SBCommandReturnObject, PutError, (FILE *), fh);

LLDB_RECORD_DUMMY(size_t, SBCommandReturnObject, PutError, (FILE *), fh);
if (fh) {
size_t num_bytes = GetErrorSize();
if (num_bytes)
Expand Down Expand Up @@ -255,31 +254,31 @@ bool SBCommandReturnObject::GetDescription(SBStream &description) {
}

void SBCommandReturnObject::SetImmediateOutputFile(FILE *fh) {
LLDB_RECORD_METHOD(void, SBCommandReturnObject, SetImmediateOutputFile,
(FILE *), fh);
LLDB_RECORD_DUMMY(void, SBCommandReturnObject, SetImmediateOutputFile,
(FILE *), fh);

SetImmediateOutputFile(fh, false);
}

void SBCommandReturnObject::SetImmediateErrorFile(FILE *fh) {
LLDB_RECORD_METHOD(void, SBCommandReturnObject, SetImmediateErrorFile,
(FILE *), fh);
LLDB_RECORD_DUMMY(void, SBCommandReturnObject, SetImmediateErrorFile,
(FILE *), fh);

SetImmediateErrorFile(fh, false);
}

void SBCommandReturnObject::SetImmediateOutputFile(FILE *fh,
bool transfer_ownership) {
LLDB_RECORD_METHOD(void, SBCommandReturnObject, SetImmediateOutputFile,
(FILE *, bool), fh, transfer_ownership);
LLDB_RECORD_DUMMY(void, SBCommandReturnObject, SetImmediateOutputFile,
(FILE *, bool), fh, transfer_ownership);
FileSP file = std::make_shared<NativeFile>(fh, transfer_ownership);
ref().SetImmediateOutputFile(file);
}

void SBCommandReturnObject::SetImmediateErrorFile(FILE *fh,
bool transfer_ownership) {
LLDB_RECORD_METHOD(void, SBCommandReturnObject, SetImmediateErrorFile,
(FILE *, bool), fh, transfer_ownership);
LLDB_RECORD_DUMMY(void, SBCommandReturnObject, SetImmediateErrorFile,
(FILE *, bool), fh, transfer_ownership);
FileSP file = std::make_shared<NativeFile>(fh, transfer_ownership);
ref().SetImmediateErrorFile(file);
}
Expand Down
14 changes: 0 additions & 14 deletions source/Host/common/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ Status File::GetFileSpec(FileSpec &file_spec) const {
return std::error_code(ENOTSUP, std::system_category());
}

FILE *File::TakeStreamAndClear() { return nullptr; }

int File::GetDescriptor() const { return kInvalidDescriptor; }

FILE *File::GetStream() { return nullptr; }
Expand Down Expand Up @@ -331,18 +329,6 @@ Status NativeFile::Close() {
return error;
}

FILE *NativeFile::TakeStreamAndClear() {
FILE *stream = GetStream();
m_stream = NULL;
m_descriptor = kInvalidDescriptor;
m_options = OpenOptions();
m_own_stream = false;
m_own_descriptor = false;
m_is_interactive = m_supports_colors = m_is_real_terminal =
eLazyBoolCalculate;
return stream;
}

Status NativeFile::GetFileSpec(FileSpec &file_spec) const {
Status error;
#ifdef F_GETPATH
Expand Down
19 changes: 0 additions & 19 deletions source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,25 +837,6 @@ bool PythonFile::Check(PyObject *py_obj) {
#endif
}

FileUP PythonFile::GetUnderlyingFile() const {
if (!IsValid())
return nullptr;

// We don't own the file descriptor returned by this function, make sure the
// File object knows about that.
PythonString py_mode = GetAttributeValue("mode").AsType<PythonString>();
auto options = File::GetOptionsFromMode(py_mode.GetString());
if (!options) {
llvm::consumeError(options.takeError());
return nullptr;
}
auto file = std::unique_ptr<File>(new NativeFile(
PyObject_AsFileDescriptor(m_py_obj), options.get(), false));
if (!file->IsValid())
return nullptr;
return file;
}

namespace {
class GIL {
public:
Expand Down
14 changes: 0 additions & 14 deletions source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -637,20 +637,6 @@ class PythonFile : public TypedPythonObject<PythonFile> {
static llvm::Expected<PythonFile> FromFile(File &file,
const char *mode = nullptr);

// FIXME delete this after FILE* typemaps are deleted
// and ScriptInterpreterPython is fixed
PythonFile(File &file, const char *mode = nullptr) {
auto f = FromFile(file, mode);
if (f)
*this = std::move(f.get());
else {
Reset();
llvm::consumeError(f.takeError());
}
}

lldb::FileUP GetUnderlyingFile() const;

llvm::Expected<lldb::FileSP> ConvertToFile(bool borrowed = false);
llvm::Expected<lldb::FileSP>
ConvertToFileForcingUseOfScriptingIOMethods(bool borrowed = false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,10 +651,15 @@ bool ScriptInterpreterPythonImpl::SetStdHandle(FileSP file_sp,

PythonDictionary &sys_module_dict = GetSysModuleDictionary();

auto new_file = PythonFile::FromFile(file, mode);
if (!new_file) {
llvm::consumeError(new_file.takeError());
return false;
}

save_file = sys_module_dict.GetItemForKey(PythonString(py_name));

PythonFile new_file(file, mode);
sys_module_dict.SetItemForKey(PythonString(py_name), new_file);
sys_module_dict.SetItemForKey(PythonString(py_name), new_file.get());
return true;
}

Expand Down
5 changes: 3 additions & 2 deletions unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,9 @@ TEST_F(PythonDataObjectsTest, TestPythonFile) {
auto file = FileSystem::Instance().Open(FileSpec(FileSystem::DEV_NULL),
File::eOpenOptionRead);
ASSERT_THAT_EXPECTED(file, llvm::Succeeded());
PythonFile py_file(*file.get(), "r");
EXPECT_TRUE(PythonFile::Check(py_file.get()));
auto py_file = PythonFile::FromFile(*file.get(), "r");
ASSERT_THAT_EXPECTED(py_file, llvm::Succeeded());
EXPECT_TRUE(PythonFile::Check(py_file.get().get()));
}

TEST_F(PythonDataObjectsTest, TestObjectAttributes) {
Expand Down

0 comments on commit 56eccc8

Please sign in to comment.