summaryrefslogtreecommitdiffstats
path: root/lldb/scripts
diff options
context:
space:
mode:
authorLawrence D'Anna <lawrence_danna@apple.com>2019-10-09 20:56:17 +0000
committerLawrence D'Anna <lawrence_danna@apple.com>2019-10-09 20:56:17 +0000
commit21b8a8ae27f3a374c55efce5a5637f732eb6595c (patch)
tree7c46a80978b5210654dc96a3ee41da1a08cdd5c4 /lldb/scripts
parentadc38dcf5ff066ca5c445af023c6e75e32757bd9 (diff)
downloadbcm5719-llvm-21b8a8ae27f3a374c55efce5a5637f732eb6595c.tar.gz
bcm5719-llvm-21b8a8ae27f3a374c55efce5a5637f732eb6595c.zip
allow arbitrary python streams to be converted to SBFile
Summary: This patch adds SWIG typemaps that can convert arbitrary python file objects into lldb_private::File. A SBFile may be initialized from a python file using the constructor. There are also alternate, tagged constructors that allow python files to be borrowed, and for the caller to control whether or not the python I/O methods will be called even when a file descriptor is available.I Reviewers: JDevlieghere, jasonmolenda, labath Reviewed By: labath Subscribers: zturner, amccarth, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D68188 llvm-svn: 374225
Diffstat (limited to 'lldb/scripts')
-rw-r--r--lldb/scripts/Python/python-typemaps.swig63
-rw-r--r--lldb/scripts/interface/SBFile.i44
2 files changed, 107 insertions, 0 deletions
diff --git a/lldb/scripts/Python/python-typemaps.swig b/lldb/scripts/Python/python-typemaps.swig
index 77ca1156ec5..f963e37da6f 100644
--- a/lldb/scripts/Python/python-typemaps.swig
+++ b/lldb/scripts/Python/python-typemaps.swig
@@ -372,6 +372,69 @@ bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
$1 = $1 || PyCallable_Check(reinterpret_cast<PyObject*>($input));
}
+
+%typemap(in) lldb::FileSP {
+ using namespace lldb_private;
+ PythonFile py_file(PyRefType::Borrowed, $input);
+ if (!py_file) {
+ PyErr_SetString(PyExc_TypeError, "not a file");
+ return nullptr;
+ }
+ auto sp = unwrapOrSetPythonException(py_file.ConvertToFile());
+ if (!sp)
+ return nullptr;
+ $1 = sp;
+}
+
+%typemap(in) lldb::FileSP FORCE_IO_METHODS {
+ using namespace lldb_private;
+ PythonFile py_file(PyRefType::Borrowed, $input);
+ if (!py_file) {
+ PyErr_SetString(PyExc_TypeError, "not a file");
+ return nullptr;
+ }
+ auto sp = unwrapOrSetPythonException(py_file.ConvertToFileForcingUseOfScriptingIOMethods());
+ if (!sp)
+ return nullptr;
+ $1 = sp;
+}
+
+%typemap(in) lldb::FileSP BORROWED {
+ using namespace lldb_private;
+ PythonFile py_file(PyRefType::Borrowed, $input);
+ if (!py_file) {
+ PyErr_SetString(PyExc_TypeError, "not a file");
+ return nullptr;
+ }
+ auto sp = unwrapOrSetPythonException(py_file.ConvertToFile(/*borrowed=*/true));
+ if (!sp)
+ return nullptr;
+ $1 = sp;
+}
+
+%typemap(in) lldb::FileSP BORROWED_FORCE_IO_METHODS {
+ using namespace lldb_private;
+ PythonFile py_file(PyRefType::Borrowed, $input);
+ if (!py_file) {
+ PyErr_SetString(PyExc_TypeError, "not a file");
+ return nullptr;
+ }
+ auto sp = unwrapOrSetPythonException(py_file.ConvertToFileForcingUseOfScriptingIOMethods(/*borrowed=*/true));
+ if (!sp)
+ return nullptr;
+ $1 = sp;
+}
+
+%typecheck(SWIG_TYPECHECK_POINTER) lldb::FileSP {
+ if (lldb_private::PythonFile::Check($input)) {
+ $1 = 1;
+ } else {
+ PyErr_Clear();
+ $1 = 0;
+ }
+}
+
+
// 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.
diff --git a/lldb/scripts/interface/SBFile.i b/lldb/scripts/interface/SBFile.i
index 6cdb192f26e..179446d5a53 100644
--- a/lldb/scripts/interface/SBFile.i
+++ b/lldb/scripts/interface/SBFile.i
@@ -15,9 +15,53 @@ namespace lldb {
class SBFile
{
public:
+
SBFile();
+
+ %feature("docstring", "
+ Initialize a SBFile from a file descriptor. mode is
+ 'r', 'r+', or 'w', like fdopen.");
SBFile(int fd, const char *mode, bool transfer_ownership);
+ %feature("docstring", "initialize a SBFile from a python file object");
+ SBFile(FileSP file);
+
+ %extend {
+ static lldb::SBFile MakeBorrowed(lldb::FileSP BORROWED) {
+ return lldb::SBFile(BORROWED);
+ }
+ static lldb::SBFile MakeForcingIOMethods(lldb::FileSP FORCE_IO_METHODS) {
+ return lldb::SBFile(FORCE_IO_METHODS);
+ }
+ static lldb::SBFile MakeBorrowedForcingIOMethods(lldb::FileSP BORROWED_FORCE_IO_METHODS) {
+ return lldb::SBFile(BORROWED_FORCE_IO_METHODS);
+ }
+ }
+
+ %pythoncode {
+ @classmethod
+ def Create(cls, file, borrow=False, force_io_methods=False):
+ """
+ Create a SBFile from a python file object, with options.
+
+ If borrow is set then the underlying file will
+ not be closed when the SBFile is closed or destroyed.
+
+ If force_scripting_io is set then the python read/write
+ methods will be called even if a file descriptor is available.
+ """
+ if borrow:
+ if force_io_methods:
+ return cls.MakeBorrowedForcingIOMethods(file)
+ else:
+ return cls.MakeBorrowed(file)
+ else:
+ if force_io_methods:
+ return cls.MakeForcingIOMethods(file)
+ else:
+ return cls(file)
+ }
+
~SBFile ();
%feature("autodoc", "Read(buffer) -> SBError, bytes_read") Read;
OpenPOWER on IntegriCloud