@@ -68,7 +68,7 @@ inline bool
6868sharesWith (const GridType& grid, py::object other)
6969{
7070 if (py::isinstance<GridType>(other)) {
71- typename GridType::ConstPtr otherGrid = other. cast <typename GridType::Ptr>();
71+ typename GridType::ConstPtr otherGrid = py:: cast<typename GridType::Ptr>(other );
7272 return (&otherGrid->tree () == &grid.tree ());
7373 }
7474 return false ;
@@ -893,7 +893,7 @@ applyMap(const char* methodName, GridType& grid, py::object funcObj)
893893
894894 // Verify that the result is of type GridType::ValueType.
895895 try {
896- result. cast <ValueT>();
896+ py:: cast<ValueT>(result );
897897 } catch (py::cast_error&) {
898898 std::ostringstream os;
899899 os << " expected callable argument to " ;
@@ -904,7 +904,7 @@ applyMap(const char* methodName, GridType& grid, py::object funcObj)
904904 throw py::type_error (os.str ());
905905 }
906906
907- it.setValue (result. cast <ValueT>());
907+ it.setValue (py:: cast<ValueT>(result ));
908908 }
909909}
910910
@@ -955,7 +955,7 @@ struct TreeCombineOp
955955 throw py::type_error (os.str ());
956956 }
957957
958- result = resultObj. cast <ValueT>();
958+ result = py:: cast<ValueT>(resultObj );
959959 }
960960 py::function op;
961961};
@@ -1177,15 +1177,15 @@ class IterValueProxy
11771177 py::object getItem (py::object keyObj) const
11781178 {
11791179 if (py::isinstance<std::string>(keyObj)) {
1180- const std::string key = keyObj. cast <std::string>();
1180+ const std::string key = py:: cast<std::string>(keyObj );
11811181 if (key == " value" ) return py::cast (this ->getValue ());
11821182 else if (key == " active" ) return py::cast (this ->getActive ());
11831183 else if (key == " depth" ) return py::cast (this ->getDepth ());
11841184 else if (key == " min" ) return py::cast (this ->getBBoxMin ());
11851185 else if (key == " max" ) return py::cast (this ->getBBoxMax ());
11861186 else if (key == " count" ) return py::cast (this ->getVoxelCount ());
11871187 }
1188- throw py::key_error (keyObj.attr (" __repr__" )(). cast <std::string>( ));
1188+ throw py::key_error (py::cast<std::string>( keyObj.attr (" __repr__" )()));
11891189 return py::object ();
11901190 }
11911191
@@ -1195,20 +1195,20 @@ class IterValueProxy
11951195 void setItem (py::object keyObj, py::object valObj)
11961196 {
11971197 if (py::isinstance<std::string>(keyObj)) {
1198- const std::string key = keyObj. cast <std::string>();
1198+ const std::string key = py:: cast<std::string>(keyObj );
11991199 if (key == " value" ) {
1200- this ->setValue (valObj. cast <ValueT>()); return ;
1200+ this ->setValue (py:: cast<ValueT>(valObj )); return ;
12011201 } else if (key == " active" ) {
1202- this ->setActive (valObj. cast <bool >()); return ;
1202+ this ->setActive (py:: cast<bool >(valObj )); return ;
12031203 } else if (this ->hasKey (key)) {
12041204 std::ostringstream os;
12051205 os << " can't set attribute '" ;
1206- os << keyObj.attr (" __repr__" )(). cast <std::string>( );
1206+ os << py::cast<std::string>( keyObj.attr (" __repr__" )());
12071207 os << " '" ;
12081208 throw py::attribute_error (os.str ());
12091209 }
12101210 }
1211- throw py::key_error (keyObj.attr (" __repr__" )(). cast <std::string>( ));
1211+ throw py::key_error (py::cast<std::string>( keyObj.attr (" __repr__" )()));
12121212 }
12131213
12141214 bool operator ==(const IterValueProxy& other) const
@@ -1235,7 +1235,7 @@ class IterValueProxy
12351235 }
12361236 // print ", ".join(valuesAsStrings)
12371237 py::object joined = py::str (" , " ).attr (" join" )(valuesAsStrings);
1238- std::string s = joined. cast <std::string>();
1238+ std::string s = py:: cast<std::string>(joined );
12391239 os << " {" << s << " }" ;
12401240 return os;
12411241 }
@@ -1379,13 +1379,9 @@ struct PickleSuite
13791379 }
13801380
13811381 // Construct a state tuple for the serialized Grid.
1382- #if PY_MAJOR_VERSION >= 3
13831382 // Convert the byte string to a "bytes" sequence.
13841383 const std::string s = ostr.str ();
13851384 py::bytes bytesObj (s);
1386- #else
1387- py::str bytesObj (ostr.str ());
1388- #endif
13891385 return py::make_tuple (bytesObj);
13901386 }
13911387
@@ -1397,25 +1393,16 @@ struct PickleSuite
13971393 std::string serialized;
13981394 if (!badState) {
13991395 // Extract the sequence containing the serialized Grid.
1400- #if PY_MAJOR_VERSION >= 3
14011396 if (py::isinstance<py::bytes>(state[0 ]))
1402- serialized = state[0 ].cast <py::bytes>();
1403- #else
1404- if (py::isinstance<std::string>(state[0 ]))
1405- serialized = state[0 ].cast <std::string>();
1406- #endif
1397+ serialized = py::cast<py::bytes>(state[0 ]);
14071398 else
14081399 badState = true ;
14091400 }
14101401
14111402 if (badState) {
14121403 std::ostringstream os;
1413- #if PY_MAJOR_VERSION >= 3
14141404 os << " expected (dict, bytes) tuple in call to __setstate__; found " ;
1415- #else
1416- os << " expected (dict, str) tuple in call to __setstate__; found " ;
1417- #endif
1418- os << state.attr (" __repr__" )().cast <std::string>();
1405+ os << py::cast<std::string>(state.attr (" __repr__" )());
14191406 throw py::value_error (os.str ());
14201407 }
14211408
@@ -1457,14 +1444,15 @@ exportGrid(py::module_ m)
14571444 using ValueAllIterT = typename GridType::ValueAllIter;
14581445
14591446 const std::string pyGridTypeName = Traits::name ();
1460- const std::string defaultCtorDescr = " Initialize with a background value of "
1461- + pyutil::str (pyGrid::getZeroValue<GridType>()) + " ." ;
1447+ std::stringstream docstream;
1448+ docstream << " Initialize with a background value of " << pyGrid::getZeroValue<GridType>() << " ." ;
1449+ std::string docstring = docstream.str ();
14621450
14631451 // Define the Grid wrapper class and make it the current scope.
14641452 py::class_<GridType, GridPtr, GridBase>(m,
14651453 /* classname=*/ pyGridTypeName.c_str (),
14661454 /* docstring=*/ (Traits::descr ()).c_str ())
1467- .def (py::init<>(), defaultCtorDescr .c_str ())
1455+ .def (py::init<>(), docstring .c_str ())
14681456 .def (py::init<const ValueT&>(), py::arg (" background" ),
14691457 " Initialize with the given background value." )
14701458
@@ -1711,7 +1699,7 @@ exportGrid(py::module_ m)
17111699 IterWrap<GridType, ValueAllIterT>::wrap (m);
17121700
17131701 // Add the Python type object for this grid type to the module-level list.
1714- m. attr ( " GridTypes " ). cast <py::list>().append (m.attr (pyGridTypeName.c_str ()));
1702+ py:: cast<py::list>(m. attr ( " GridTypes " ) ).append (m.attr (pyGridTypeName.c_str ()));
17151703}
17161704
17171705} // namespace pyGrid
0 commit comments