Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

py2and3: Minor compatibility updates #10175

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bindings/pydrake/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ drake_cc_library(
visibility = ["//visibility:public"],
)

drake_cc_library(
name = "test_util_pybind",
testonly = 1,
hdrs = ["test/test_util_pybind.h"],
declare_installed_headers = 0,
visibility = ["//visibility:public"],
)

drake_cc_library(
name = "autodiff_types_pybind",
hdrs = ["autodiff_types_pybind.h"],
Expand Down
8 changes: 7 additions & 1 deletion bindings/pydrake/common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,10 @@ drake_py_unittest(

drake_pybind_cc_googletest(
name = "cpp_param_pybind_test",
cc_deps = [":cpp_param_pybind"],
cc_deps = [
":cpp_param_pybind",
"//bindings/pydrake:test_util_pybind",
],
py_deps = [":cpp_param_py"],
)

Expand All @@ -355,6 +358,7 @@ drake_pybind_cc_googletest(
name = "cpp_template_pybind_test",
cc_deps = [
":cpp_template_pybind",
"//bindings/pydrake:test_util_pybind",
"//common:nice_type_name",
"//common/test_utilities:expect_throws_message",
],
Expand All @@ -365,6 +369,7 @@ drake_pybind_cc_googletest(
name = "drake_variant_pybind_test",
cc_deps = [
":drake_variant_pybind",
"//bindings/pydrake:test_util_pybind",
],
)

Expand All @@ -391,6 +396,7 @@ drake_pybind_cc_googletest(
name = "type_safe_index_pybind_test",
cc_deps = [
":type_safe_index_pybind",
"//bindings/pydrake:test_util_pybind",
"//common:nice_type_name",
],
)
Expand Down
5 changes: 3 additions & 2 deletions bindings/pydrake/common/test/cpp_param_pybind_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "pybind11/eval.h"
#include "pybind11/pybind11.h"

#include "drake/bindings/pydrake/test/test_util_pybind.h"

using std::string;

namespace drake {
Expand Down Expand Up @@ -86,8 +88,7 @@ int main(int argc, char** argv) {
// Define custom class only once here.
py::class_<CustomCppType>(m, "CustomCppType");

// For Python3
py::globals().attr("update")(m.attr("__dict__"));
test::SynchronizeGlobalsForPython3(m);
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Expand Down
17 changes: 7 additions & 10 deletions bindings/pydrake/common/test/cpp_template_pybind_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "pybind11/eval.h"
#include "pybind11/pybind11.h"

#include "drake/bindings/pydrake/test/test_util_pybind.h"
#include "drake/common/nice_type_name.h"
#include "drake/common/test_utilities/expect_throws_message.h"

Expand All @@ -22,6 +23,8 @@ namespace drake {
namespace pydrake {
namespace {

using test::SynchronizeGlobalsForPython3;

template <typename... Ts>
struct SimpleTemplate {
vector<string> GetNames() {
Expand All @@ -45,12 +48,6 @@ void CheckValue(const string& expr, const T& expected) {
EXPECT_EQ(py::eval(expr).cast<T>(), expected);
}

// TODO(eric.cousineau): Figure out why this is necessary.
// Necessary for Python3.
void sync(py::module m) {
py::globals().attr("update")(m.attr("__dict__"));
}

GTEST_TEST(CppTemplateTest, TemplateClass) {
py::module m("__main__");

Expand All @@ -59,14 +56,14 @@ GTEST_TEST(CppTemplateTest, TemplateClass) {

const vector<string> expected_1 = {"int"};
const vector<string> expected_2 = {"int", "double"};
sync(m);
SynchronizeGlobalsForPython3(m);

CheckValue("DefaultInst().GetNames()", expected_1);
CheckValue("SimpleTemplate[int]().GetNames()", expected_1);
CheckValue("SimpleTemplate[int, float]().GetNames()", expected_2);

m.def("simple_func", [](const SimpleTemplate<int>&) {});
sync(m);
SynchronizeGlobalsForPython3(m);

// Check error message if a function is called with the incorrect arguments.
// N.B. We use `[^\0]` because C++ regex does not have an equivalent of
Expand All @@ -93,7 +90,7 @@ GTEST_TEST(CppTemplateTest, TemplateFunction) {

const vector<string> expected_1 = {"int"};
const vector<string> expected_2 = {"int", "double"};
sync(m);
SynchronizeGlobalsForPython3(m);
CheckValue("SimpleFunction[int]()", expected_1);
CheckValue("SimpleFunction[int, float]()", expected_2);
}
Expand All @@ -120,7 +117,7 @@ GTEST_TEST(CppTemplateTest, TemplateMethod) {

const vector<string> expected_1 = {"int"};
const vector<string> expected_2 = {"int", "double"};
sync(m);
SynchronizeGlobalsForPython3(m);
CheckValue("SimpleType().SimpleMethod[int]()", expected_1);
CheckValue("SimpleType().SimpleMethod[int, float]()", expected_2);
}
Expand Down
4 changes: 4 additions & 0 deletions bindings/pydrake/common/test/drake_variant_pybind_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
#include "pybind11/pybind11.h"

#include "drake/bindings/pydrake/pydrake_pybind.h"
#include "drake/bindings/pydrake/test/test_util_pybind.h"

using std::string;

namespace drake {
namespace pydrake {
namespace {

using test::SynchronizeGlobalsForPython3;

string VariantToString(const variant<int, double, string>& value) {
const bool is_int = holds_alternative<int>(value);
const bool is_double = holds_alternative<double>(value);
Expand All @@ -36,6 +39,7 @@ GTEST_TEST(VariantTest, CheckCasting) {
py::module m("__main__");

m.def("VariantToString", &VariantToString, py::arg("value"));
SynchronizeGlobalsForPython3(m);
ExpectString("VariantToString(1)", "int(1)");
ExpectString("VariantToString(0.5)", "double(0.5)");
ExpectString("VariantToString('foo')", "string(foo)");
Expand Down
8 changes: 6 additions & 2 deletions bindings/pydrake/common/test/type_safe_index_pybind_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
#include "pybind11/eval.h"
#include "pybind11/pybind11.h"

#include "drake/bindings/pydrake/test/test_util_pybind.h"

using std::string;
using std::vector;

namespace drake {
namespace pydrake {
namespace {

using test::SynchronizeGlobalsForPython3;

template <typename T>
void CheckValue(const string& expr, const T& expected) {
EXPECT_EQ(py::eval(expr).cast<T>(), expected);
Expand All @@ -34,7 +38,7 @@ GTEST_TEST(TypeSafeIndexTest, CheckCasting) {
EXPECT_EQ(x, 10);
return x;
});
py::globals().attr("update")(m.attr("__dict__")); // For Python3
SynchronizeGlobalsForPython3(m);
CheckValue("pass_thru_int(10)", 10);
CheckValue("pass_thru_int(Index(10))", 10);
// TypeSafeIndex<> is not implicitly constructible from an int.
Expand All @@ -46,7 +50,7 @@ GTEST_TEST(TypeSafeIndexTest, CheckCasting) {
return x;
});

py::globals().attr("update")(m.attr("__dict__")); // For Python3
SynchronizeGlobalsForPython3(m);

// TypeSafeIndex<> is not implicitly constructible from an int.
// TODO(eric.cousineau): Consider relaxing this to *only* accept `int`s, and
Expand Down
5 changes: 4 additions & 1 deletion bindings/pydrake/manipulation/simple_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
and potentially other robotics) applications.
"""

import Tkinter as tk
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
import numpy as np

from pydrake.multibody.multibody_tree.multibody_plant import MultibodyPlant
Expand Down
5 changes: 4 additions & 1 deletion bindings/pydrake/manipulation/test/simple_ui_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import unittest

import numpy as np
import Tkinter as tk
try:
import tkinter as tk
except ImportError:
import Tkinter as tk

from pydrake.common import FindResourceOrThrow
from pydrake.multibody.multibody_tree.multibody_plant import MultibodyPlant
Expand Down
5 changes: 2 additions & 3 deletions bindings/pydrake/systems/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
installed.
"""

from sys import maxint
from tempfile import NamedTemporaryFile

import matplotlib.pyplot as plt
Expand Down Expand Up @@ -38,6 +37,6 @@ def plot_graphviz(dot_text):
return plt.imshow(plt.imread(f.name), aspect="equal")


def plot_system_graphviz(system, max_depth=maxint):
def plot_system_graphviz(system, **kwargs):
"""Renders a System's Graphviz representation in `matplotlib`."""
return plot_graphviz(system.GetGraphvizString(max_depth))
return plot_graphviz(system.GetGraphvizString(**kwargs))
5 changes: 3 additions & 2 deletions bindings/pydrake/systems/meshcat_visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package, Meshcat:
https://github.com/rdeits/meshcat
"""
from __future__ import print_function
import argparse
import math
import webbrowser
Expand Down Expand Up @@ -179,8 +180,8 @@ def load(self):
meshcat.geometry.ObjMeshGeometry.from_file(
geom.string_data[0:-3] + "obj")
else:
print "UNSUPPORTED GEOMETRY TYPE ", \
geom.type, " IGNORED"
print("UNSUPPORTED GEOMETRY TYPE {} IGNORED".format(
geom.type))
continue

# Turn a list of R,G,B elements (any indexable list of >= 3
Expand Down
22 changes: 22 additions & 0 deletions bindings/pydrake/test/test_util_pybind.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "pybind11/pybind11.h"

#include "drake/bindings/pydrake/pydrake_pybind.h"

namespace drake {
namespace pydrake {
namespace test {

// TODO(eric.cousineau): Figure out if there is a better solution than this
// hack.
/// pybind11's Python3 implementation seems to disconnect the `globals()` from
/// an embedded interpreter's `__main__` module. To remedy this, we must
/// manually synchronize these variables.
inline void SynchronizeGlobalsForPython3(py::module m) {
py::globals().attr("update")(m.attr("__dict__"));
}

} // namespace test
} // namespace pydrake
} // namespace drake
3 changes: 2 additions & 1 deletion common/test_utilities/drake_py_unittest_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import argparse
import imp
import io
import os
import re
import sys
Expand Down Expand Up @@ -33,7 +34,7 @@
if not found_filename:
raise RuntimeError("No such file found {}!".format(
test_filename))
with open(found_filename, "r") as infile:
with io.open(found_filename, "r", encoding="utf8") as infile:
for line in infile.readlines():
if any([line.startswith("if __name__ =="),
line.strip().startswith("unittest.main")]):
Expand Down
5 changes: 4 additions & 1 deletion examples/manipulation_station/end_effector_teleop.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

import argparse

import Tkinter as tk
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
import numpy as np

from pydrake.common import FindResourceOrThrow
Expand Down