Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(690)

Unified Diff: mojo/public/python/src/python_system_helper.cc

Issue 552783004: mojo: Add a runloop utility in python (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/public/python/src/python_system_helper.h ('k') | mojo/python/tests/runloop_unittest.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « mojo/public/python/src/python_system_helper.h ('k') | mojo/python/tests/runloop_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698