Skip to content

Commit 46f071c

Browse files
Remy Varannesblastrock
authored andcommitted
bind call / async in pymodule
also clean up tests Change-Id: I3cc9edb879209a883fc94618906367cc440f4a0b Reviewed-on: http://gerrit.aldebaran.lan/44249 Reviewed-by: pdaouadi <pdaouadi@aldebaran-robotics.com> Tested-by: pdaouadi <pdaouadi@aldebaran-robotics.com>
1 parent 87bff0b commit 46f071c

17 files changed

Lines changed: 283 additions & 185 deletions

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ qi_add_optional_package(BOOST_PYTHON3)
1010
# Generate each version in a separated build folder.
1111
# (include_directories clash otherwise)
1212

13+
add_subdirectory(tests)
14+
1315
if (WITH_PYTHON AND WITH_BOOST_PYTHON)
1416
set(pyver "")
1517
add_subdirectory(py2py3 "python2")

py2py3/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ set(_headers
2323
)
2424

2525
set(_sources
26+
../src/pyobject_p.hpp
2627
../src/pyinit.cpp
2728
../src/pyexport.cpp
2829
../src/pyapplication.cpp
@@ -87,4 +88,4 @@ else()
8788
qi_use_lib(_qi3 qipython3)
8889
endif()
8990

90-
add_subdirectory(../tests ${CMAKE_CURRENT_BINARY_DIR}/tests)
91+
add_subdirectory(../testspy ${CMAKE_CURRENT_BINARY_DIR}/testspy)

qi/test/pythonruntest.sh

Lines changed: 0 additions & 5 deletions
This file was deleted.

qi/test/run_qimessaging_test.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

qi/test/test_call.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def setValue(p, v):
1414
time.sleep(0.2)
1515
p.setValue(v)
1616

17+
@qi.multiThreaded()
1718
class FooService:
1819
def __init__(self):
1920
pass
@@ -74,13 +75,25 @@ def retc(self, name, index):
7475
def fooStat(i):
7576
return i * 3
7677

78+
def slow(self):
79+
time.sleep(.2)
80+
return 18
81+
7782

7883
FooService.fooLambda = lambda self, x: x * 2
7984

85+
@qi.multiThreaded()
86+
class Multi:
87+
def slow(self):
88+
time.sleep(0.1)
89+
return 42
90+
91+
8092
def docalls(sserver, sclient):
81-
m = FooService()
82-
sserver.registerService("FooService", m)
93+
sserver.registerService("FooService", FooService())
8394
s = sclient.service("FooService")
95+
sserver.registerService("Multi", Multi())
96+
m = sclient.service("Multi")
8497

8598
print("simple test")
8699
assert s.simple() == 42
@@ -143,6 +156,21 @@ def docalls(sserver, sclient):
143156
print("test staticmethod")
144157
assert s.fooStat(4) == 4 * 3
145158

159+
print("test async")
160+
start = time.time()
161+
print("call !")
162+
f1 = m.slow(_async=True)
163+
print("call !")
164+
f2 = m.slow(_async=True)
165+
print("call !")
166+
f3 = m.slow(_async=True)
167+
print("done !")
168+
assert f1.value() == 42
169+
assert f2.value() == 42
170+
assert f3.value() == 42
171+
end = time.time()
172+
print (end-start)
173+
assert end - start < 0.15
146174

147175
def test_calldirect():
148176
ses = qi.Session()

qi/test/test_log.py

100755100644
File mode changed.

qi/test/test_module.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import qi
2+
import sys
3+
import pytest
4+
5+
def test_module():
6+
mod = qi.module("moduletest")
7+
8+
cat = mod.createObject("Cat", "truc")
9+
assert cat.meow(3) == 'meow'
10+
11+
mouse = mod.createObject("Mouse")
12+
assert mouse.squeak() == 18
13+
14+
assert mod.call("lol") == 3

qi/test/test_promise.py

100755100644
File mode changed.

qi/test/test_typespassing.py

100755100644
File mode changed.

src/pymodule.cpp

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <qipython/error.hpp>
1414
#include <boost/python/raw_function.hpp>
1515

16+
#include "pyobject_p.hpp"
17+
1618
qiLogCategory("qipy.factory");
1719

1820
namespace qi {
@@ -33,42 +35,10 @@ namespace qi {
3335
PyErr_SetString(PyExc_RuntimeError, e.what());
3436
}
3537

36-
// static qi::AnyObject pyconstruct_object(boost::python::object class_)
37-
// {
38-
// GILScopedLock lock;
39-
// qi::AnyObject obj;
40-
// PY_CATCH_ERROR(obj = makeQiAnyObject(class_()));
41-
// return obj;
42-
// }
43-
44-
// static void pyregister_object_factory(boost::python::str class_name, boost::python::object class_)
45-
// {
46-
// std::string name = boost::python::extract<std::string>(class_name);
47-
// boost::function<qi::AnyObject()> func = boost::bind(&qi::py::pyconstruct_object, class_);
48-
// qi::registerObjectFactory(name, AnyFunction::from(func));
49-
// }
50-
51-
// static boost::python::object pycreate_object_args(boost::python::tuple pyargs,
52-
// boost::python::dict kwargs)
53-
// {
54-
// int len = boost::python::len(pyargs);
55-
// std::string objectName = boost::python::extract<std::string>(pyargs[0]);
56-
// qi::AnyObject object;
57-
58-
// qi::AnyReferenceVector args;
59-
// for (int i = 1; i < len; ++i)
60-
// args.push_back(AnyReference::from(boost::python::object(pyargs[i])));
61-
// object = qi::createObject(objectName, args);
62-
63-
// if(!object)
64-
// throw PyCreateException(objectName);
65-
// return makePyQiObject(object, objectName);
66-
// }
67-
68-
class PyModule {
38+
class PyModule : public PyQiObject {
6939
public:
7040
explicit PyModule(const qi::AnyModule& mod)
71-
: _mod(mod)
41+
: PyQiObject(mod), _mod(mod)
7242
{}
7343

7444
PyModule()
@@ -130,7 +100,6 @@ namespace qi {
130100
}
131101
QI_REGISTER_MODULE_FACTORY("python", &importPyModule);
132102

133-
134103
void export_pyobjectfactory()
135104
{
136105
boost::python::register_exception_translator<PyCreateException>(&translate_pycreateexception);
@@ -139,13 +108,12 @@ namespace qi {
139108
boost::python::def("listModules", &pylistModules);
140109
boost::python::class_<PyModule>("Module", boost::python::init<>())
141110
.def("createObject", boost::python::raw_function(&createObjectAdapter, 2))
111+
.def("call", boost::python::raw_function(&pyParamShrinker<PyModule>, 1))
112+
.def("async", boost::python::raw_function(&pyParamShrinkerAsync<PyModule>, 1))
142113
// .def("constants", &PyModule::constants)
143114
// .def("functions", &PyModule::functions)
144115
// .def("factories", &PyModule::factories)
145116
;
146-
147-
//boost::python::def("createObject", boost::python::raw_function(&pycreate_object_args, 1));
148-
//boost::python::def("registerObjectFactory", &qi::py::pyregister_object_factory);
149117
}
150118
}
151119
}

0 commit comments

Comments
 (0)