Skip to content

Commit 954aa05

Browse files
gouzilco63oc
authored andcommitted
[pybind11] fix deprecated T(handle, bool) (PaddlePaddle#65270)
1 parent 5726379 commit 954aa05

4 files changed

Lines changed: 43 additions & 23 deletions

File tree

paddle/fluid/pybind/eager.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,13 @@ py::object ParsePyArray(
376376
py::object numpy_value = py::object();
377377

378378
if (kw_order_map["value"] <= args_num) {
379-
numpy_value = py::object(
380-
py::handle(PyTuple_GET_ITEM(args, kw_order_map["value"] - 1)), true);
379+
numpy_value = py::reinterpret_borrow<py::object>(
380+
py::handle(PyTuple_GET_ITEM(args, kw_order_map["value"] - 1)));
381381
} else {
382382
if (flag_kwargs && kws_map["value"] != nullptr) {
383-
numpy_value = py::object(py::handle(kws_map["value"]), true);
383+
numpy_value =
384+
py::reinterpret_borrow<py::object>(py::handle(kws_map["value"]));
385+
384386
} else {
385387
PADDLE_THROW(platform::errors::InvalidArgument(
386388
"The first expected arguments is {value: PyArray}, "

paddle/fluid/pybind/eager_math_op_patch.cc

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ static PyObject* tensor__add__method(TensorObject* self,
293293
other_tensor = other_tensor_ref;
294294
} else {
295295
if (IsNumpyArray(other_obj)) {
296-
py::object numpy_value = py::object(py::handle(other_obj), true);
296+
py::object numpy_value =
297+
py::reinterpret_borrow<py::object>(py::handle(other_obj));
297298
other_tensor = paddle::Tensor(place);
298299
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
299300
} else {
@@ -397,7 +398,8 @@ static PyObject* tensor__sub__method(TensorObject* self,
397398
other_tensor = other_tensor_ref;
398399
} else {
399400
if (IsNumpyArray(other_obj)) {
400-
py::object numpy_value = py::object(py::handle(other_obj), true);
401+
py::object numpy_value =
402+
py::reinterpret_borrow<py::object>(py::handle(other_obj));
401403
other_tensor = paddle::Tensor(place);
402404
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
403405
} else {
@@ -499,7 +501,8 @@ static PyObject* tensor__rsub__method(TensorObject* self,
499501
other_tensor = other_tensor_ref;
500502
} else {
501503
if (IsNumpyArray(other_obj)) {
502-
py::object numpy_value = py::object(py::handle(other_obj), true);
504+
py::object numpy_value =
505+
py::reinterpret_borrow<py::object>(py::handle(other_obj));
503506
other_tensor = paddle::Tensor(place);
504507
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
505508
} else {
@@ -605,7 +608,8 @@ static PyObject* tensor__mul__method(TensorObject* self,
605608
other_tensor = other_tensor_ref;
606609
} else {
607610
if (IsNumpyArray(other_obj)) {
608-
py::object numpy_value = py::object(py::handle(other_obj), true);
611+
py::object numpy_value =
612+
py::reinterpret_borrow<py::object>(py::handle(other_obj));
609613
other_tensor = paddle::Tensor(place);
610614
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
611615
} else {
@@ -708,7 +712,8 @@ static PyObject* tensor__div__method(TensorObject* self,
708712
other_tensor = other_tensor_ref;
709713
} else {
710714
if (IsNumpyArray(other_obj)) {
711-
py::object numpy_value = py::object(py::handle(other_obj), true);
715+
py::object numpy_value =
716+
py::reinterpret_borrow<py::object>(py::handle(other_obj));
712717
other_tensor = paddle::Tensor(place);
713718
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
714719
} else {
@@ -814,7 +819,8 @@ static PyObject* tensor__rdiv__method(TensorObject* self,
814819
other_tensor = other_tensor_ref;
815820
} else {
816821
if (IsNumpyArray(other_obj)) {
817-
py::object numpy_value = py::object(py::handle(other_obj), true);
822+
py::object numpy_value =
823+
py::reinterpret_borrow<py::object>(py::handle(other_obj));
818824
other_tensor = paddle::Tensor(place);
819825
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
820826
} else {
@@ -924,7 +930,8 @@ static PyObject* tensor__gt__method(TensorObject* self,
924930
other_tensor = other_tensor_ref;
925931
} else {
926932
if (IsNumpyArray(other_obj)) {
927-
py::object numpy_value = py::object(py::handle(other_obj), true);
933+
py::object numpy_value =
934+
py::reinterpret_borrow<py::object>(py::handle(other_obj));
928935
other_tensor = paddle::Tensor(place);
929936
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
930937
} else {
@@ -1024,7 +1031,8 @@ static PyObject* tensor__ge__method(TensorObject* self,
10241031
other_tensor = other_tensor_ref;
10251032
} else {
10261033
if (IsNumpyArray(other_obj)) {
1027-
py::object numpy_value = py::object(py::handle(other_obj), true);
1034+
py::object numpy_value =
1035+
py::reinterpret_borrow<py::object>(py::handle(other_obj));
10281036
other_tensor = paddle::Tensor(place);
10291037
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
10301038
} else {
@@ -1125,7 +1133,8 @@ static PyObject* tensor__mod__method(TensorObject* self,
11251133
other_tensor = other_tensor_ref;
11261134
} else {
11271135
if (IsNumpyArray(other_obj)) {
1128-
py::object numpy_value = py::object(py::handle(other_obj), true);
1136+
py::object numpy_value =
1137+
py::reinterpret_borrow<py::object>(py::handle(other_obj));
11291138
other_tensor = paddle::Tensor(place);
11301139
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
11311140
} else {
@@ -1222,7 +1231,8 @@ static PyObject* tensor__matmul__method(TensorObject* self,
12221231
other_tensor = other_tensor_ref;
12231232
} else {
12241233
if (IsNumpyArray(other_obj)) {
1225-
py::object numpy_value = py::object(py::handle(other_obj), true);
1234+
py::object numpy_value =
1235+
py::reinterpret_borrow<py::object>(py::handle(other_obj));
12261236
other_tensor = paddle::Tensor(place);
12271237
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
12281238
} else {
@@ -1350,7 +1360,8 @@ static PyObject* tensor__lt__method(TensorObject* self,
13501360
other_tensor = other_tensor_ref;
13511361
} else {
13521362
if (IsNumpyArray(other_obj)) {
1353-
py::object numpy_value = py::object(py::handle(other_obj), true);
1363+
py::object numpy_value =
1364+
py::reinterpret_borrow<py::object>(py::handle(other_obj));
13541365
other_tensor = paddle::Tensor(place);
13551366
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
13561367
} else {
@@ -1450,7 +1461,8 @@ static PyObject* tensor__le__method(TensorObject* self,
14501461
other_tensor = other_tensor_ref;
14511462
} else {
14521463
if (IsNumpyArray(other_obj)) {
1453-
py::object numpy_value = py::object(py::handle(other_obj), true);
1464+
py::object numpy_value =
1465+
py::reinterpret_borrow<py::object>(py::handle(other_obj));
14541466
other_tensor = paddle::Tensor(place);
14551467
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
14561468
} else {
@@ -1551,7 +1563,8 @@ static PyObject* tensor__floordiv__method(TensorObject* self,
15511563
other_tensor = other_tensor_ref;
15521564
} else {
15531565
if (IsNumpyArray(other_obj)) {
1554-
py::object numpy_value = py::object(py::handle(other_obj), true);
1566+
py::object numpy_value =
1567+
py::reinterpret_borrow<py::object>(py::handle(other_obj));
15551568
other_tensor = paddle::Tensor(place);
15561569
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
15571570
} else {
@@ -1655,7 +1668,8 @@ static PyObject* tensor__pow__method(TensorObject* self,
16551668
other_tensor = other_tensor_ref;
16561669
} else {
16571670
if (IsNumpyArray(other_obj)) {
1658-
py::object numpy_value = py::object(py::handle(other_obj), true);
1671+
py::object numpy_value =
1672+
py::reinterpret_borrow<py::object>(py::handle(other_obj));
16591673
other_tensor = paddle::Tensor(place);
16601674
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
16611675
} else {
@@ -1755,7 +1769,8 @@ static PyObject* tensor__rpow__method(TensorObject* self,
17551769
other_tensor = other_tensor_ref;
17561770
} else {
17571771
if (IsNumpyArray(other_obj)) {
1758-
py::object numpy_value = py::object(py::handle(other_obj), true);
1772+
py::object numpy_value =
1773+
py::reinterpret_borrow<py::object>(py::handle(other_obj));
17591774
other_tensor = paddle::Tensor(place);
17601775
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
17611776
} else {
@@ -1855,7 +1870,8 @@ static PyObject* tensor__ne__method(TensorObject* self,
18551870
other_tensor = other_tensor_ref;
18561871
} else {
18571872
if (IsNumpyArray(other_obj)) {
1858-
py::object numpy_value = py::object(py::handle(other_obj), true);
1873+
py::object numpy_value =
1874+
py::reinterpret_borrow<py::object>(py::handle(other_obj));
18591875
other_tensor = paddle::Tensor(place);
18601876
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
18611877
} else {
@@ -1954,7 +1970,8 @@ static PyObject* tensor__eq__method(TensorObject* self,
19541970
other_tensor = other_tensor_ref;
19551971
} else {
19561972
if (IsNumpyArray(other_obj)) {
1957-
py::object numpy_value = py::object(py::handle(other_obj), true);
1973+
py::object numpy_value =
1974+
py::reinterpret_borrow<py::object>(py::handle(other_obj));
19581975
other_tensor = paddle::Tensor(place);
19591976
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
19601977
} else {

paddle/fluid/pybind/pir.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,8 @@ pir::Value apply(Value self, py::object func) {
11131113
return py::cast<py::none>(Py_None); \
11141114
} \
11151115
auto py_data = reinterpret_cast<PyObject *>(prop_ptr); \
1116-
py::object obj = py::object(py::handle(py_data), true); \
1116+
py::object obj = \
1117+
py::reinterpret_borrow<py::object>(py::handle(py_data)); \
11171118
return obj; \
11181119
}, \
11191120
[](Value self, py::object obj) { \

paddle/fluid/pybind/slice_utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ static paddle::Tensor dealWithValues(const paddle::Tensor& tensor,
549549
paddle::Tensor value_tensor_tmp(
550550
std::make_shared<phi::DenseTensor>(),
551551
egr::Controller::Instance().GenerateUniqueName());
552-
py::object value_obj_tmp(py::handle(value_obj), true);
552+
py::object value_obj_tmp = py::reinterpret_borrow<py::object>(value_obj);
553553
py::object value = value_obj_tmp;
554554
if (tensor.dtype() == phi::DataType::FLOAT32) {
555555
if (!py::isinstance<py::array_t<float>>(value_obj_tmp)) {
@@ -595,7 +595,7 @@ static paddle::Tensor dealWithValues(const paddle::Tensor& tensor,
595595
false);
596596
value_tensor = value_tensor_tmp;
597597
} else {
598-
py::object value_obj_tmp(py::handle(value_obj), true);
598+
py::object value_obj_tmp = py::reinterpret_borrow<py::object>(value_obj);
599599
// convert the value to self data type
600600
if (py::isinstance<py::float_>(value_obj_tmp) ||
601601
py::isinstance<py::int_>(value_obj_tmp) ||

0 commit comments

Comments
 (0)