Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/public/python/src/python_system_helper.h" | |
| 6 | |
| 7 #include "Python.h" | |
| 8 | |
| 9 #include "mojo/public/cpp/system/macros.h" | |
| 10 #include "mojo/public/cpp/utility/run_loop.h" | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 class PythonClosure : public mojo::Closure::Runnable { | |
| 15 public: | |
| 16 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.
| |
| 17 if (callable) { | |
| 18 callable_ = callable; | |
| 19 Py_XINCREF(callable); | |
| 20 } | |
| 21 } | |
| 22 | |
| 23 virtual ~PythonClosure() { | |
| 24 if (callable_) | |
| 25 Py_DECREF(callable_); | |
| 26 } | |
| 27 | |
| 28 virtual void Run() const MOJO_OVERRIDE { | |
| 29 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
| |
| 30 if (!empty_tuple) | |
| 31 return; | |
| 32 | |
| 33 PyObject* result = PyObject_CallObject(callable_, empty_tuple); | |
| 34 Py_DECREF(empty_tuple); | |
| 35 if (result) | |
| 36 Py_DECREF(result); | |
| 37 } | |
| 38 | |
| 39 private: | |
| 40 PyObject* callable_; | |
| 41 }; | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 namespace mojo { | |
| 46 | |
| 47 Closure BuildClosure(PyObject* callable) { | |
| 48 if (!PyCallable_Check(callable)) | |
| 49 return Closure(); | |
| 50 | |
| 51 return Closure( | |
| 52 static_cast<mojo::Closure::Runnable*>(new PythonClosure(callable))); | |
| 53 } | |
| 54 | |
| 55 void InitRunLoop() { | |
| 56 mojo::RunLoop::SetUp(); | |
| 57 } | |
| 58 | |
| 59 } // namespace mojo | |
| OLD | NEW |