Skip to content
This repository was archived by the owner on Nov 11, 2025. It is now read-only.

Commit 5718e4d

Browse files
committed
Add python_multiple_inheritance_test
To clearly show the behavior differences between PyCLIF-C-API and PyCLIF-pybind11. GitHub testing: #66 PiperOrigin-RevId: 551017585
1 parent f83117c commit 5718e4d

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

clif/testing/python/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,5 @@ add_pyclif_library_for_test(pyobject_ptr pyobject_ptr.clif
230230
)
231231

232232
add_pyclif_library_for_test(property_get_set_cpp_derived property_get_set_cpp_derived.clif)
233+
234+
add_pyclif_library_for_test(python_multiple_inheritance python_multiple_inheritance.clif)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from "clif/testing/python_multiple_inheritance.h":
16+
namespace `clif_testing_python_multiple_inheritance`:
17+
class CppBase:
18+
def __init__(self, value: int)
19+
def get_base_value(self) -> int
20+
def reset_base_value(self, new_value: int)
21+
22+
class CppDrvd(CppBase):
23+
def __init__(self, value: int)
24+
def get_drvd_value(self) -> int
25+
def reset_drvd_value(self, new_value: int)
26+
def get_base_value_from_drvd(self) -> int
27+
def reset_base_value_from_drvd(self, new_value: int)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from absl.testing import absltest
16+
17+
from clif.testing.python import python_multiple_inheritance as tm
18+
19+
20+
class PC(tm.CppBase):
21+
pass
22+
23+
24+
class PPCC(PC, tm.CppDrvd):
25+
pass
26+
27+
28+
class PPCCInit(PC, tm.CppDrvd):
29+
30+
def __init__(self, value):
31+
PC.__init__(self, value)
32+
tm.CppDrvd.__init__(self, value + 1)
33+
34+
35+
class PythonMultipleInheritanceTest(absltest.TestCase):
36+
37+
def testPC(self):
38+
d = PC(11)
39+
self.assertEqual(d.get_base_value(), 11)
40+
d.reset_base_value(13)
41+
self.assertEqual(d.get_base_value(), 13)
42+
43+
@absltest.skipIf(
44+
"pybind11" in tm.__doc__,
45+
"Preempt `__init__() must be called when overriding __init__` exception.",
46+
)
47+
def testPPCC(self):
48+
d = PPCC(11)
49+
self.assertEqual(d.get_drvd_value(), 33)
50+
d.reset_drvd_value(55)
51+
self.assertEqual(d.get_drvd_value(), 55)
52+
53+
# PyCLIF-C-API: CppBase is not initialized and never used.
54+
self.assertEqual(d.get_base_value(), 11)
55+
self.assertEqual(d.get_base_value_from_drvd(), 11)
56+
d.reset_base_value(20)
57+
self.assertEqual(d.get_base_value(), 20)
58+
self.assertEqual(d.get_base_value_from_drvd(), 20)
59+
d.reset_base_value_from_drvd(30)
60+
self.assertEqual(d.get_base_value(), 30)
61+
self.assertEqual(d.get_base_value_from_drvd(), 30)
62+
63+
def testPPCCInit(self):
64+
d = PPCCInit(11)
65+
self.assertEqual(d.get_drvd_value(), 36)
66+
d.reset_drvd_value(55)
67+
self.assertEqual(d.get_drvd_value(), 55)
68+
69+
# PyCLIF-C-API: CppBase is initialized but never used.
70+
# PyCLIF-pybind11: CppBase is initialized and used when CppBase methods
71+
# are called, but CppDrvd is used when CppDrvd methods
72+
# are called.
73+
i = 1 if "pybind11" in tm.__doc__ else 0
74+
self.assertEqual(d.get_base_value(), (12, 11)[i])
75+
self.assertEqual(d.get_base_value_from_drvd(), 12)
76+
d.reset_base_value(20)
77+
self.assertEqual(d.get_base_value(), 20)
78+
self.assertEqual(d.get_base_value_from_drvd(), (20, 12)[i])
79+
d.reset_base_value_from_drvd(30)
80+
self.assertEqual(d.get_base_value(), (30, 20)[i])
81+
self.assertEqual(d.get_base_value_from_drvd(), 30)
82+
83+
84+
if __name__ == "__main__":
85+
absltest.main()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef CLIF_TESTING_PROPERTY_PYTHON_MULTIPLE_INHERITANCE_H_
17+
#define CLIF_TESTING_PROPERTY_PYTHON_MULTIPLE_INHERITANCE_H_
18+
19+
namespace clif_testing_python_multiple_inheritance {
20+
21+
struct CppBase {
22+
CppBase(int value) : base_value(value) {}
23+
int get_base_value() const { return base_value; }
24+
void reset_base_value(int new_value) { base_value = new_value; }
25+
26+
private:
27+
int base_value;
28+
};
29+
30+
struct CppDrvd : CppBase {
31+
CppDrvd(int value) : CppBase(value), drvd_value(value * 3) {}
32+
int get_drvd_value() const { return drvd_value; }
33+
void reset_drvd_value(int new_value) { drvd_value = new_value; }
34+
35+
int get_base_value_from_drvd() const { return get_base_value(); }
36+
void reset_base_value_from_drvd(int new_value) {
37+
reset_base_value(new_value);
38+
}
39+
40+
private:
41+
int drvd_value;
42+
};
43+
44+
} // namespace clif_testing_python_multiple_inheritance
45+
46+
#endif // CLIF_TESTING_PYTHON_MULTIPLE_INHERITANCE_H_

0 commit comments

Comments
 (0)