@@ -59,7 +59,7 @@ class ExamplePythonTypes {
5959 /* Create, manipulate, and return a Python list */
6060 py::list get_list () {
6161 py::list list;
62- list.append (py::str ( " value" ) );
62+ list.append (" value" );
6363 py::print (" Entry at position 0:" , list[0 ]);
6464 list[0 ] = py::str (" overwritten" );
6565 return list;
@@ -269,7 +269,7 @@ test_initializer python_types([](py::module &m) {
269269 d[" missing_attr_chain" ] = " raised" _s;
270270 }
271271
272- d[" is_none" ] = py::cast ( o.attr (" basic_attr" ).is_none () );
272+ d[" is_none" ] = o.attr (" basic_attr" ).is_none ();
273273
274274 d[" operator()" ] = o.attr (" func" )(1 );
275275 d[" operator*" ] = o.attr (" func" )(*o.attr (" begin_end" ));
@@ -279,13 +279,13 @@ test_initializer python_types([](py::module &m) {
279279
280280 m.def (" test_tuple_accessor" , [](py::tuple existing_t ) {
281281 try {
282- existing_t [0 ] = py::cast ( 1 ) ;
282+ existing_t [0 ] = 1 ;
283283 } catch (const py::error_already_set &) {
284284 // --> Python system error
285285 // Only new tuples (refcount == 1) are mutable
286286 auto new_t = py::tuple (3 );
287287 for (size_t i = 0 ; i < new_t .size (); ++i) {
288- new_t [i] = py::cast (i) ;
288+ new_t [i] = i ;
289289 }
290290 return new_t ;
291291 }
@@ -294,15 +294,15 @@ test_initializer python_types([](py::module &m) {
294294
295295 m.def (" test_accessor_assignment" , []() {
296296 auto l = py::list (1 );
297- l[0 ] = py::cast ( 0 ) ;
297+ l[0 ] = 0 ;
298298
299299 auto d = py::dict ();
300300 d[" get" ] = l[0 ];
301301 auto var = l[0 ];
302302 d[" deferred_get" ] = var;
303- l[0 ] = py::cast ( 1 ) ;
303+ l[0 ] = 1 ;
304304 d[" set" ] = l[0 ];
305- var = py::cast ( 99 ) ; // this assignment should not overwrite l[0]
305+ var = 99 ; // this assignment should not overwrite l[0]
306306 d[" deferred_set" ] = l[0 ];
307307 d[" var" ] = var;
308308
@@ -338,8 +338,8 @@ test_initializer python_types([](py::module &m) {
338338 }, py::arg_v (" x" , std::experimental::nullopt , " None" ));
339339#endif
340340
341- m.attr (" has_optional" ) = py::cast ( has_optional) ;
342- m.attr (" has_exp_optional" ) = py::cast ( has_exp_optional) ;
341+ m.attr (" has_optional" ) = has_optional;
342+ m.attr (" has_exp_optional" ) = has_exp_optional;
343343
344344 m.def (" test_default_constructors" , []() {
345345 return py::dict (
@@ -389,4 +389,41 @@ test_initializer python_types([](py::module &m) {
389389 py::class_<MoveOutContainer>(m, " MoveOutContainer" )
390390 .def (py::init<>())
391391 .def_property_readonly (" move_list" , &MoveOutContainer::move_list);
392+
393+ m.def (" get_implicit_casting" , []() {
394+ py::dict d;
395+ d[" char*_i1" ] = " abc" ;
396+ const char *c2 = " abc" ;
397+ d[" char*_i2" ] = c2;
398+ d[" char*_e" ] = py::cast (c2);
399+ d[" char*_p" ] = py::str (c2);
400+
401+ d[" int_i1" ] = 42 ;
402+ int i = 42 ;
403+ d[" int_i2" ] = i;
404+ i++;
405+ d[" int_e" ] = py::cast (i);
406+ i++;
407+ d[" int_p" ] = py::int_ (i);
408+
409+ d[" str_i1" ] = std::string (" str" );
410+ std::string s2 (" str1" );
411+ d[" str_i2" ] = s2;
412+ s2[3 ] = ' 2' ;
413+ d[" str_e" ] = py::cast (s2);
414+ s2[3 ] = ' 3' ;
415+ d[" str_p" ] = py::str (s2);
416+
417+ py::list l (2 );
418+ l[0 ] = 3 ;
419+ l[1 ] = py::cast (6 );
420+ l.append (9 );
421+ l.append (py::cast (12 ));
422+ l.append (py::int_ (15 ));
423+
424+ return py::dict (
425+ " d" _a=d,
426+ " l" _a=l
427+ );
428+ });
392429});
0 commit comments