Chromium Code Reviews| Index: mojo/public/python/src/python_system_helper.cc |
| diff --git a/mojo/public/python/src/python_system_helper.cc b/mojo/public/python/src/python_system_helper.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a33512e62f6e696b441f670d97603d76d1f325c7 |
| --- /dev/null |
| +++ b/mojo/public/python/src/python_system_helper.cc |
| @@ -0,0 +1,59 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "mojo/public/python/src/python_system_helper.h" |
| + |
| +#include "Python.h" |
| + |
| +#include "mojo/public/cpp/system/macros.h" |
| +#include "mojo/public/cpp/utility/run_loop.h" |
| + |
| +namespace { |
| + |
| +class PythonClosure : public mojo::Closure::Runnable { |
| + public: |
| + PythonClosure(PyObject* callable) { |
|
sdefresne
2014/09/16 16:35:59
If callable is NULL, then callable_ will be uninit
qsr
2014/09/17 11:15:48
Done.
|
| + if (callable) { |
| + callable_ = callable; |
| + Py_XINCREF(callable); |
| + } |
| + } |
| + |
| + virtual ~PythonClosure() { |
| + if (callable_) |
| + Py_DECREF(callable_); |
| + } |
| + |
| + virtual void Run() const MOJO_OVERRIDE { |
| + PyObject* empty_tuple = PyTuple_New(0); |
|
sdefresne
2014/09/16 16:35:59
IIRC, Python function with a prototype returning a
qsr
2014/09/17 11:15:48
Changed to deal with the exception in the followin
|
| + if (!empty_tuple) |
| + return; |
| + |
| + PyObject* result = PyObject_CallObject(callable_, empty_tuple); |
| + Py_DECREF(empty_tuple); |
| + if (result) |
| + Py_DECREF(result); |
| + } |
| + |
| + private: |
| + PyObject* callable_; |
| +}; |
| + |
| +} // namespace |
| + |
| +namespace mojo { |
| + |
| +Closure BuildClosure(PyObject* callable) { |
| + if (!PyCallable_Check(callable)) |
| + return Closure(); |
| + |
| + return Closure( |
| + static_cast<mojo::Closure::Runnable*>(new PythonClosure(callable))); |
| +} |
| + |
| +void InitRunLoop() { |
| + mojo::RunLoop::SetUp(); |
| +} |
| + |
| +} // namespace mojo |