diff options
Diffstat (limited to 'mojo/public/python/src')
-rw-r--r-- | mojo/public/python/src/common.cc | 173 | ||||
-rw-r--r-- | mojo/public/python/src/common.h | 103 | ||||
-rw-r--r-- | mojo/public/python/src/python_system_helper.cc | 43 | ||||
-rw-r--r-- | mojo/public/python/src/python_system_helper.h | 30 |
4 files changed, 0 insertions, 349 deletions
diff --git a/mojo/public/python/src/common.cc b/mojo/public/python/src/common.cc deleted file mode 100644 index a80d013..0000000 --- a/mojo/public/python/src/common.cc +++ /dev/null @@ -1,173 +0,0 @@ -// Copyright 2013 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/common.h" - -#include <Python.h> - -#include "mojo/public/c/environment/async_waiter.h" -#include "mojo/public/cpp/bindings/callback.h" -#include "mojo/public/cpp/bindings/lib/shared_ptr.h" -#include "mojo/public/cpp/environment/logging.h" -#include "mojo/public/cpp/system/core.h" -#include "mojo/public/cpp/system/macros.h" -#include "mojo/public/cpp/utility/run_loop.h" - -namespace { - -void AsyncCallbackForwarder(void* closure, MojoResult result) { - mojo::Callback<void(MojoResult)>* callback = - static_cast<mojo::Callback<void(MojoResult)>*>(closure); - // callback will be deleted when it is run. - callback->Run(result); -} - -} // namespace - -namespace mojo { -namespace python { - -ScopedGIL::ScopedGIL() { - state_ = PyGILState_Ensure(); -} - -ScopedGIL::~ScopedGIL() { - PyGILState_Release(state_); -} - -ScopedPyRef::ScopedPyRef(PyObject* object) : object_(object) { -} - -ScopedPyRef::ScopedPyRef(PyObject* object, ScopedPyRefAcquire) - : object_(object) { - Py_XINCREF(object_); -} - -ScopedPyRef::~ScopedPyRef() { - if (object_) { - ScopedGIL acquire_gil; - Py_DECREF(object_); - } -} - -ScopedPyRef::operator PyObject*() const { - return object_; -} - -PythonClosure::PythonClosure(PyObject* callable, const mojo::Closure& quit) - : callable_(callable, kAcquire), quit_(quit) { - MOJO_DCHECK(callable); -} - -PythonClosure::~PythonClosure() {} - -void PythonClosure::Run() const { - ScopedGIL acquire_gil; - ScopedPyRef empty_tuple(PyTuple_New(0)); - if (!empty_tuple) { - quit_.Run(); - return; - } - - ScopedPyRef result(PyObject_CallObject(callable_, empty_tuple)); - if (!result) { - quit_.Run(); - return; - } -} - -Closure::Runnable* NewRunnableFromCallable(PyObject* callable, - const mojo::Closure& quit_closure) { - MOJO_DCHECK(PyCallable_Check(callable)); - - return new PythonClosure(callable, quit_closure); -} - -class PythonAsyncWaiter::AsyncWaiterRunnable - : public mojo::Callback<void(MojoResult)>::Runnable { - public: - AsyncWaiterRunnable(PyObject* callable, - CallbackMap* callbacks, - const mojo::Closure& quit) - : wait_id_(0), - callable_(callable, kAcquire), - callbacks_(callbacks), - quit_(quit) { - MOJO_DCHECK(callable_); - MOJO_DCHECK(callbacks_); - } - - void set_wait_id(MojoAsyncWaitID wait_id) { wait_id_ = wait_id; } - - void Run(MojoResult mojo_result) const override { - MOJO_DCHECK(wait_id_); - - // Remove to reference to this object from PythonAsyncWaiter and ensure this - // object will be destroyed when this method exits. - MOJO_DCHECK(callbacks_->find(wait_id_) != callbacks_->end()); - internal::SharedPtr<mojo::Callback<void(MojoResult)>> self = - (*callbacks_)[wait_id_]; - callbacks_->erase(wait_id_); - - ScopedGIL acquire_gil; - ScopedPyRef args_tuple(Py_BuildValue("(i)", mojo_result)); - if (!args_tuple) { - quit_.Run(); - return; - } - - ScopedPyRef result(PyObject_CallObject(callable_, args_tuple)); - if (!result) { - quit_.Run(); - return; - } - } - - private: - MojoAsyncWaitID wait_id_; - ScopedPyRef callable_; - CallbackMap* callbacks_; - const mojo::Closure quit_; - - MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterRunnable); -}; - -PythonAsyncWaiter::PythonAsyncWaiter(const mojo::Closure& quit_closure) - : quit_(quit_closure) { - async_waiter_ = Environment::GetDefaultAsyncWaiter(); -} - -PythonAsyncWaiter::~PythonAsyncWaiter() { - for (CallbackMap::const_iterator it = callbacks_.begin(); - it != callbacks_.end(); - ++it) { - async_waiter_->CancelWait(it->first); - } -} - -MojoAsyncWaitID PythonAsyncWaiter::AsyncWait(MojoHandle handle, - MojoHandleSignals signals, - MojoDeadline deadline, - PyObject* callable) { - AsyncWaiterRunnable* runner = - new AsyncWaiterRunnable(callable, &callbacks_, quit_); - internal::SharedPtr<mojo::Callback<void(MojoResult)>> callback( - new mojo::Callback<void(MojoResult)>( - static_cast<mojo::Callback<void(MojoResult)>::Runnable*>(runner))); - MojoAsyncWaitID wait_id = async_waiter_->AsyncWait( - handle, signals, deadline, &AsyncCallbackForwarder, callback.get()); - callbacks_[wait_id] = callback; - runner->set_wait_id(wait_id); - return wait_id; -} - -void PythonAsyncWaiter::CancelWait(MojoAsyncWaitID wait_id) { - if (callbacks_.find(wait_id) != callbacks_.end()) { - async_waiter_->CancelWait(wait_id); - callbacks_.erase(wait_id); - } -} - -} // namespace python -} // namespace mojo diff --git a/mojo/public/python/src/common.h b/mojo/public/python/src/common.h deleted file mode 100644 index 03ed73b..0000000 --- a/mojo/public/python/src/common.h +++ /dev/null @@ -1,103 +0,0 @@ -// 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. - -#ifndef MOJO_PUBLIC_PYTHON_SRC_COMMON_H_ -#define MOJO_PUBLIC_PYTHON_SRC_COMMON_H_ - -#include <Python.h> - -#include <map> - -#include "mojo/public/c/environment/async_waiter.h" -#include "mojo/public/cpp/bindings/callback.h" -#include "mojo/public/cpp/bindings/lib/shared_ptr.h" -#include "mojo/public/cpp/system/core.h" - -namespace mojo { -namespace python { - -class ScopedGIL { - public: - ScopedGIL(); - - ~ScopedGIL(); - - private: - PyGILState_STATE state_; - - MOJO_DISALLOW_COPY_AND_ASSIGN(ScopedGIL); -}; - -enum ScopedPyRefAcquire { - kAcquire, -}; - -class ScopedPyRef { - public: - explicit ScopedPyRef(PyObject* object); - ScopedPyRef(PyObject* object, ScopedPyRefAcquire); - - ~ScopedPyRef(); - - operator PyObject*() const; - - private: - PyObject* object_; - - MOJO_DISALLOW_COPY_AND_ASSIGN(ScopedPyRef); -}; - - -class PythonClosure : public mojo::Closure::Runnable { - public: - PythonClosure(PyObject* callable, const mojo::Closure& quit); - ~PythonClosure(); - - void Run() const override; - - private: - ScopedPyRef callable_; - const mojo::Closure quit_; - - MOJO_DISALLOW_COPY_AND_ASSIGN(PythonClosure); -}; - -// Create a mojo::Closure from a callable python object. -Closure::Runnable* NewRunnableFromCallable(PyObject* callable, - const Closure& quit_closure); - -// AsyncWaiter for python, used to execute a python closure after waiting. If an -// error occurs while executing the closure, the current message loop will be -// exited. See |AsyncWaiter| in mojo/public/c/environment/async_waiter.h for -// more details. -class PythonAsyncWaiter { - public: - explicit PythonAsyncWaiter(const mojo::Closure& quit_closure); - ~PythonAsyncWaiter(); - MojoAsyncWaitID AsyncWait(MojoHandle handle, - MojoHandleSignals signals, - MojoDeadline deadline, - PyObject* callable); - - void CancelWait(MojoAsyncWaitID wait_id); - - private: - class AsyncWaiterRunnable; - - typedef std::map<MojoAsyncWaitID, - internal::SharedPtr<mojo::Callback<void(MojoResult)> > > - CallbackMap; - - CallbackMap callbacks_; - const MojoAsyncWaiter* async_waiter_; - const mojo::Closure quit_; - - MOJO_DISALLOW_COPY_AND_ASSIGN(PythonAsyncWaiter); -}; - -} // namespace python -} // namespace mojo - -#endif // MOJO_PUBLIC_PYTHON_SRC_COMMON_H_ - diff --git a/mojo/public/python/src/python_system_helper.cc b/mojo/public/python/src/python_system_helper.cc deleted file mode 100644 index 0c5a358..0000000 --- a/mojo/public/python/src/python_system_helper.cc +++ /dev/null @@ -1,43 +0,0 @@ -// 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/utility/run_loop.h" -#include "mojo/public/python/src/common.h" - -namespace { -class QuitCurrentRunLoop : public mojo::Closure::Runnable { - public: - void Run() const override { - mojo::RunLoop::current()->Quit(); - } - - static mojo::Closure NewQuitClosure() { - return mojo::Closure( - static_cast<mojo::Closure::Runnable*>(new QuitCurrentRunLoop())); - } -}; - -} // namespace - -namespace mojo { -namespace python { - -Closure BuildClosure(PyObject* callable) { - if (!PyCallable_Check(callable)) - return Closure(); - - return mojo::Closure( - NewRunnableFromCallable(callable, QuitCurrentRunLoop::NewQuitClosure())); -} - -PythonAsyncWaiter* NewAsyncWaiter() { - return new PythonAsyncWaiter(QuitCurrentRunLoop::NewQuitClosure()); -} - -} // namespace python -} // namespace mojo diff --git a/mojo/public/python/src/python_system_helper.h b/mojo/public/python/src/python_system_helper.h deleted file mode 100644 index ff9bb0b..0000000 --- a/mojo/public/python/src/python_system_helper.h +++ /dev/null @@ -1,30 +0,0 @@ -// 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. - -#ifndef MOJO_PUBLIC_PYTHON_SRC_PYTHON_SYSTEM_HELPER_H_ -#define MOJO_PUBLIC_PYTHON_SRC_PYTHON_SYSTEM_HELPER_H_ - -// Python must be the first include, as it defines preprocessor variable without -// checking if they already exist. -#include <Python.h> - -#include <map> - -#include "mojo/public/cpp/bindings/callback.h" -#include "mojo/public/python/src/common.h" - - -namespace mojo { -namespace python { -// Create a mojo::Closure from a callable python object. If an error occurs -// while executing callable, the closure will quit the current run loop. -Closure BuildClosure(PyObject* callable); - -// Create a new PythonAsyncWaiter object. Ownership is passed to the caller. -PythonAsyncWaiter* NewAsyncWaiter(); - -} // namespace python -} // namespace mojo - -#endif // MOJO_PUBLIC_PYTHON_SRC_PYTHON_SYSTEM_HELPER_H_ |