Skip to content

Commit 57c2b81

Browse files
committed
Snapshot of cl/621045278
1 parent f2a7c07 commit 57c2b81

12 files changed

+142
-8
lines changed

clif/pybind11/gen_type_info.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def generate_type_caster(self) -> Generator[str, None, None]:
4747
class ClassType(BaseType):
4848
"""Wraps a C++ Class."""
4949

50+
module_path: str
5051
cpp_has_public_dtor: bool
5152
cpp_copyable: bool
5253
cpp_movable: bool
@@ -72,7 +73,11 @@ def generate_type_caster(self) -> Generator[str, None, None]:
7273
# `PYBIND11_SMART_HOLDER_TYPE_CASTERS()` invocation, e.g. a collision
7374
# between a `std::tuple` alias in the global namespace (to work around
7475
# b/118736768) and `pybind11::tuple`.
75-
using_name = f'PyCLIF_py_name_{self.py_name}'.replace('.', '_')
76+
if self.module_path:
77+
py_name_fq = '.'.join([self.module_path, self.py_name])
78+
else:
79+
py_name_fq = self.py_name
80+
using_name = f'PyCLIF_py_name_{py_name_fq}'.replace('.', '_')
7681
yield f'using {using_name} = {self.cpp_name};'
7782
yield f'PYBIND11_SMART_HOLDER_TYPE_CASTERS({using_name})'
7883
if not self.cpp_copyable:

clif/pybind11/generator.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -404,15 +404,21 @@ def _register_types(self, decl: ast_pb2.Decl, parent_py_name: str = '',
404404
[b.native for b in decl.class_.bases if b.native and
405405
not b.cpp_canonical_type])
406406
class_type = gen_type_info.ClassType(
407-
cpp_name=decl.class_.name.cpp_canonical_type, py_name=py_name,
408-
cpp_namespace=cpp_namespace, py_bases=py_bases,
407+
module_path=self._module_path,
408+
cpp_name=decl.class_.name.cpp_canonical_type,
409+
py_name=py_name,
410+
cpp_namespace=cpp_namespace,
411+
py_bases=py_bases,
409412
cpp_has_public_dtor=decl.class_.cpp_has_public_dtor,
410-
cpp_copyable=(decl.class_.cpp_copyable and
411-
not decl.class_.cpp_abstract),
412-
cpp_movable=(decl.class_.cpp_movable and
413-
not decl.class_.cpp_abstract),
413+
cpp_copyable=(
414+
decl.class_.cpp_copyable and not decl.class_.cpp_abstract
415+
),
416+
cpp_movable=(
417+
decl.class_.cpp_movable and not decl.class_.cpp_abstract
418+
),
414419
override_in_python=override_in_python,
415-
enable_instance_dict=decl.class_.enable_instance_dict)
420+
enable_instance_dict=decl.class_.enable_instance_dict,
421+
)
416422
self._types.append(class_type)
417423
for member in decl.class_.members:
418424
self._register_types(member, py_name, cpp_namespace)

clif/testing/python/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,11 @@ add_pyclif_library_for_test(std_containers_copy_move std_containers_copy_move.cl
253253

254254
add_pyclif_library_for_test(class_release_gil_before_calling_cpp_dtor class_release_gil_before_calling_cpp_dtor.clif)
255255

256+
add_pyclif_library_for_test(same_class_name_in_two_modules_alpha same_class_name_in_two_modules_alpha.clif)
257+
add_pyclif_library_for_test(same_class_name_in_two_modules_bravo same_class_name_in_two_modules_bravo.clif)
258+
add_pyclif_library_for_test(same_class_name_in_two_modules_client same_class_name_in_two_modules_client.clif
259+
CLIF_DEPS clif_testing_python_same_class_name_in_two_modules_alpha
260+
CLIF_DEPS clif_testing_python_same_class_name_in_two_modules_bravo
261+
)
262+
256263
# OMITTED: instrumentation_for_testing_test (pytype currently not supported on GitHub)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from "clif/testing/same_class_name_in_two_modules_alpha.h":
2+
namespace `clif_testing_same_class_name_in_two_modules_alpha`:
3+
class Smith:
4+
@classmethod
5+
def WhereIsHome(cls) -> str
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from absl.testing import absltest
2+
3+
from clif.testing.python import same_class_name_in_two_modules_alpha as alpha
4+
5+
6+
class AlphaTest(absltest.TestCase):
7+
8+
def testWhereIsHome(self):
9+
self.assertEqual(alpha.Smith.WhereIsHome(), 'I live in Alphaville.')
10+
11+
12+
if __name__ == '__main__':
13+
absltest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from "clif/testing/same_class_name_in_two_modules_bravo.h":
2+
namespace `clif_testing_same_class_name_in_two_modules_bravo`:
3+
class Smith:
4+
@classmethod
5+
def WhereIsHome(cls) -> str
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from absl.testing import absltest
2+
3+
from clif.testing.python import same_class_name_in_two_modules_bravo as bravo
4+
5+
6+
class BravoTest(absltest.TestCase):
7+
8+
def testWhereIsHome(self):
9+
self.assertEqual(bravo.Smith.WhereIsHome(), 'I live in Bravoville.')
10+
11+
12+
if __name__ == '__main__':
13+
absltest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from "clif/testing/python/same_class_name_in_two_modules_alpha_clif.h" import *
2+
from "clif/testing/python/same_class_name_in_two_modules_bravo_clif.h" import *
3+
4+
from "clif/testing/same_class_name_in_two_modules_client.h":
5+
namespace `clif_testing_same_class_name_in_two_modules_client`:
6+
def ReturnAlphaSmith() -> `clif_testing_same_class_name_in_two_modules_alpha::Smith` as Smith
7+
def ReturnBravoSmith() -> `clif_testing_same_class_name_in_two_modules_bravo::Smith` as Smith
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from absl.testing import absltest
2+
3+
from clif.testing.python import same_class_name_in_two_modules_alpha as alpha
4+
from clif.testing.python import same_class_name_in_two_modules_bravo as bravo
5+
from clif.testing.python import same_class_name_in_two_modules_client as client
6+
7+
8+
class AlphaTest(absltest.TestCase):
9+
10+
def testPassAlphaSmith(self):
11+
obj = client.ReturnAlphaSmith()
12+
self.assertEqual(obj.WhereIsHome(), 'I live in Alphaville.')
13+
self.assertIsInstance(obj, alpha.Smith)
14+
15+
def testPassBravoSmith(self):
16+
obj = client.ReturnBravoSmith()
17+
self.assertEqual(obj.WhereIsHome(), 'I live in Bravoville.')
18+
self.assertIsInstance(obj, bravo.Smith)
19+
20+
21+
if __name__ == '__main__':
22+
absltest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef CLIF_TESTING_SAME_CLASS_NAME_IN_TWO_MODULES_ALPHA_H_
2+
#define CLIF_TESTING_SAME_CLASS_NAME_IN_TWO_MODULES_ALPHA_H_
3+
4+
#include <string>
5+
6+
namespace clif_testing_same_class_name_in_two_modules_alpha {
7+
8+
class Smith {
9+
public:
10+
static std::string WhereIsHome() { return "I live in Alphaville."; }
11+
};
12+
13+
} // namespace clif_testing_same_class_name_in_two_modules_alpha
14+
15+
#endif // CLIF_TESTING_SAME_CLASS_NAME_IN_TWO_MODULES_ALPHA_H_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef CLIF_TESTING_SAME_CLASS_NAME_IN_TWO_MODULES_BRAVO_H_
2+
#define CLIF_TESTING_SAME_CLASS_NAME_IN_TWO_MODULES_BRAVO_H_
3+
4+
#include <string>
5+
6+
namespace clif_testing_same_class_name_in_two_modules_bravo {
7+
8+
class Smith {
9+
public:
10+
static std::string WhereIsHome() { return "I live in Bravoville."; }
11+
};
12+
13+
} // namespace clif_testing_same_class_name_in_two_modules_bravo
14+
15+
#endif // CLIF_TESTING_SAME_CLASS_NAME_IN_TWO_MODULES_BRAVO_H_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef CLIF_TESTING_SAME_CLASS_NAME_IN_TWO_MODULES_CLIENT_H_
2+
#define CLIF_TESTING_SAME_CLASS_NAME_IN_TWO_MODULES_CLIENT_H_
3+
4+
#include "clif/testing/same_class_name_in_two_modules_alpha.h"
5+
#include "clif/testing/same_class_name_in_two_modules_bravo.h"
6+
7+
namespace clif_testing_same_class_name_in_two_modules_client {
8+
9+
inline clif_testing_same_class_name_in_two_modules_alpha::Smith
10+
ReturnAlphaSmith() {
11+
return clif_testing_same_class_name_in_two_modules_alpha::Smith();
12+
}
13+
14+
inline clif_testing_same_class_name_in_two_modules_bravo::Smith
15+
ReturnBravoSmith() {
16+
return clif_testing_same_class_name_in_two_modules_bravo::Smith();
17+
}
18+
19+
} // namespace clif_testing_same_class_name_in_two_modules_client
20+
21+
#endif // CLIF_TESTING_SAME_CLASS_NAME_IN_TWO_MODULES_CLIENT_H_

0 commit comments

Comments
 (0)