|
|
@@ -15,14 +15,20 @@
|
|
|
#include "asyncTaskSequence.h"
|
|
|
#include "eventParameter.h"
|
|
|
#include "paramValue.h"
|
|
|
+#include "paramPyObject.h"
|
|
|
#include "pythonTask.h"
|
|
|
+#include "asyncTaskManager.h"
|
|
|
+#include "config_event.h"
|
|
|
|
|
|
#ifdef HAVE_PYTHON
|
|
|
|
|
|
#ifndef CPPPARSER
|
|
|
extern struct Dtool_PyTypedObject Dtool_AsyncFuture;
|
|
|
+extern struct Dtool_PyTypedObject Dtool_EventParameter;
|
|
|
extern struct Dtool_PyTypedObject Dtool_ParamValueBase;
|
|
|
extern struct Dtool_PyTypedObject Dtool_TypedObject;
|
|
|
+extern struct Dtool_PyTypedObject Dtool_TypedReferenceCount;
|
|
|
+extern struct Dtool_PyTypedObject Dtool_TypedWritableReferenceCount;
|
|
|
#endif
|
|
|
|
|
|
/**
|
|
|
@@ -90,20 +96,24 @@ static PyObject *get_done_result(const AsyncFuture *future) {
|
|
|
// EventStoreInt and Double are not exposed to Python for some reason.
|
|
|
if (type == EventStoreInt::get_class_type()) {
|
|
|
return Dtool_WrapValue(((EventStoreInt *)ptr)->get_value());
|
|
|
- } else if (type == EventStoreDouble::get_class_type()) {
|
|
|
+ }
|
|
|
+ else if (type == EventStoreDouble::get_class_type()) {
|
|
|
return Dtool_WrapValue(((EventStoreDouble *)ptr)->get_value());
|
|
|
}
|
|
|
+ else if (type == ParamPyObject::get_class_type()) {
|
|
|
+ return ((ParamPyObject *)ptr)->get_value();
|
|
|
+ }
|
|
|
|
|
|
ParamValueBase *value = (ParamValueBase *)ptr;
|
|
|
PyObject *wrap = DTool_CreatePyInstanceTyped
|
|
|
((void *)value, Dtool_ParamValueBase, false, false, type.get_index());
|
|
|
if (wrap != nullptr) {
|
|
|
PyObject *value = PyObject_GetAttrString(wrap, "value");
|
|
|
+ Py_DECREF(wrap);
|
|
|
if (value != nullptr) {
|
|
|
return value;
|
|
|
}
|
|
|
PyErr_Clear();
|
|
|
- Py_DECREF(wrap);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -154,6 +164,58 @@ __await__(PyObject *self) {
|
|
|
return Dtool_NewGenerator(self, &gen_next);
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Sets this future's result. Can only be called if done() returns false.
|
|
|
+ */
|
|
|
+void Extension<AsyncFuture>::
|
|
|
+set_result(PyObject *result) {
|
|
|
+ if (result == Py_None) {
|
|
|
+ _this->set_result(nullptr);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ else if (DtoolInstance_Check(result)) {
|
|
|
+ void *ptr;
|
|
|
+ if ((ptr = DtoolInstance_UPCAST(result, Dtool_EventParameter))) {
|
|
|
+ _this->set_result(*(const EventParameter *)ptr);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if ((ptr = DtoolInstance_UPCAST(result, Dtool_TypedWritableReferenceCount))) {
|
|
|
+ _this->set_result((TypedWritableReferenceCount *)ptr);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if ((ptr = DtoolInstance_UPCAST(result, Dtool_TypedReferenceCount))) {
|
|
|
+ _this->set_result((TypedReferenceCount *)ptr);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if ((ptr = DtoolInstance_UPCAST(result, Dtool_TypedObject))) {
|
|
|
+ _this->set_result((TypedObject *)ptr);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (PyUnicode_Check(result)) {
|
|
|
+ Py_ssize_t result_len;
|
|
|
+ wchar_t *result_str = PyUnicode_AsWideCharString(result, &result_len);
|
|
|
+ _this->set_result(new EventStoreWstring(std::wstring(result_str, result_len)));
|
|
|
+ PyMem_Free(result_str);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ else if (PyLongOrInt_Check(result)) {
|
|
|
+ long result_val = PyLongOrInt_AS_LONG(result);
|
|
|
+ if (result_val >= INT_MIN && result_val <= INT_MAX) {
|
|
|
+ _this->set_result(new EventStoreInt((int)result_val));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (PyNumber_Check(result)) {
|
|
|
+ _this->set_result(new EventStoreDouble(PyFloat_AsDouble(result)));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // If we don't recognize the type, store it as a generic PyObject pointer.
|
|
|
+ ParamPyObject::init_type();
|
|
|
+ _this->set_result(new ParamPyObject(result));
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* Returns the result of this future, unless it was cancelled, in which case
|
|
|
* it returns CancelledError.
|
|
|
@@ -285,8 +347,19 @@ gather(PyObject *args) {
|
|
|
}
|
|
|
} else if (PyCoro_CheckExact(item)) {
|
|
|
// We allow passing in a coroutine instead of a future. This causes it
|
|
|
- // to be scheduled as a task.
|
|
|
- futures.push_back(new PythonTask(item));
|
|
|
+ // to be scheduled as a task on the current task manager.
|
|
|
+ PT(AsyncTask) task = new PythonTask(item);
|
|
|
+ Thread *current_thread = Thread::get_current_thread();
|
|
|
+ AsyncTask *current_task = (AsyncTask *)current_thread->get_current_task();
|
|
|
+ if (current_task != nullptr) {
|
|
|
+ task->set_task_chain(current_task->get_task_chain());
|
|
|
+ current_task->get_manager()->add(task);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ event_cat.warning()
|
|
|
+ << "gather() with coroutine not called from within a task; not scheduling with task manager.\n";
|
|
|
+ }
|
|
|
+ futures.push_back(task);
|
|
|
continue;
|
|
|
}
|
|
|
return Dtool_Raise_ArgTypeError(item, i, "gather", "coroutine, task or future");
|