From 53e0b176c45fe3ab4296e18d893132f35cb89482 Mon Sep 17 00:00:00 2001 From: rwgk Date: Thu, 29 Aug 2024 02:32:59 -0700 Subject: [PATCH] Systematically replace `py::` with `pybind11::` No unit test changes, due to special circumstances (role eliminations). However, this CL was used in a PyCLIF-pybind11 TGP (cl/667456010, 2024-08-26): http://tap/OCL:667456010:BASE:667610796:1724689781533:d2626456: ``` 571 Failing Targets 147 Broken Targets ``` PiperOrigin-RevId: 668846116 --- clif/pybind11/classes.py | 51 +++++++++-------- clif/pybind11/consts.py | 10 ++-- clif/pybind11/enums.py | 4 +- clif/pybind11/function.py | 8 +-- clif/pybind11/function_lib.py | 66 ++++++++++++---------- clif/pybind11/generator.py | 22 ++++---- clif/pybind11/lambdas.py | 76 +++++++++++++++----------- clif/pybind11/operators.py | 4 +- clif/pybind11/unknown_default_value.py | 45 +++++++++------ clif/pybind11/utils.py | 7 ++- clif/pybind11/variables.py | 27 +++++---- 11 files changed, 182 insertions(+), 138 deletions(-) diff --git a/clif/pybind11/classes.py b/clif/pybind11/classes.py index 2fb2b61f..80c4227d 100644 --- a/clif/pybind11/classes.py +++ b/clif/pybind11/classes.py @@ -37,13 +37,13 @@ def generate_from( decl: ast_pb2.Decl, superclass_name: str, trampoline_class_names: Set[str], codegen_info: utils.CodeGenInfo, ) -> Generator[str, None, None]: - """Generates a complete py::class_<>. + """Generates a complete pybind11::class_<>. Args: decl: Class declaration in proto format. superclass_name: String name of the superclass. - trampoline_class_names: A Set of class names whose member functions - will be overriden in Python. + trampoline_class_names: A Set of class names whose member functions will be + overriden in Python. codegen_info: The information needed to generate pybind11 code. Yields: @@ -57,7 +57,7 @@ def generate_from( namespace = '::'.join(namespaces[:i + 1]) yield I + I + f'using namespace ::{namespace};' class_name = f'{class_decl.name.native}_class' - definition = f'py::classh<{class_decl.name.cpp_name}' + definition = f'pybind11::classh<{class_decl.name.cpp_name}' implicit_upcast_bases = [] if not class_decl.suppress_upcasts: for base in class_decl.bases: @@ -94,8 +94,8 @@ def generate_from( if class_decl.HasField('docstring'): definition += f', {function_lib.generate_docstring(class_decl.docstring)}' - # If we generate `py::dynamic_attr()` for the base class, we also need to - # generate `py::dynamic_attr()` for the derived class. + # If we generate `pybind11::dynamic_attr()` for the base class, we also need + # to generate `pybind11::dynamic_attr()` for the derived class. enable_instance_dict = class_decl.enable_instance_dict for base in class_decl.bases: if (base.HasField('cpp_canonical_type') and @@ -103,14 +103,14 @@ def generate_from( enable_instance_dict = True break if _USE_PYTYPE_TYPE_AS_METACLASS: - definition += ', py::metaclass((PyObject*) &PyType_Type)' - definition += ', py::release_gil_before_calling_cpp_dtor()' + definition += ', pybind11::metaclass((PyObject*) &PyType_Type)' + definition += ', pybind11::release_gil_before_calling_cpp_dtor()' if mi_bases: - definition += ', py::multiple_inheritance()' + definition += ', pybind11::multiple_inheritance()' if enable_instance_dict: - definition += ', py::dynamic_attr()' + definition += ', pybind11::dynamic_attr()' if class_decl.final: - definition += ', py::is_final()' + definition += ', pybind11::is_final()' definition += ');' yield I + I + definition @@ -172,7 +172,7 @@ def generate_from( if (not ctor_defined and class_decl.cpp_has_def_ctor and (not class_decl.cpp_abstract or trampoline_generated)): - yield I + I + f'{class_name}.def(py::init<>());' + yield I + I + f'{class_name}.def(pybind11::init<>());' for base in implicit_upcast_bases: mangled = legacy_types.Mangle(base.cpp_canonical_type) @@ -180,13 +180,15 @@ def generate_from( yield I2 + f'{class_name}.def(' yield I2 + I2 + f'"as_{mangled}",' yield I2 + I2 + f'[]({class_decl.name.cpp_name}* self) {{' - yield I2 + I2 + I2 + 'return py::capsule(static_cast(self));' + yield I2 + I2 + I2 + 'return pybind11::capsule(static_cast(self));' yield I2 + I2 + '}' yield I2 + ');' if not reduce_or_reduce_ex_defined: - yield I + I + (f'{class_name}.def("__reduce_ex__",' + - ' ::clif_pybind11::ReduceExImpl, py::arg("protocol")=-1);') + yield I + I + ( + f'{class_name}.def("__reduce_ex__", ::clif_pybind11::ReduceExImpl,' + ' pybind11::arg("protocol")=-1);' + ) yield I + '}' @@ -196,11 +198,12 @@ def _generate_iterator( ) -> Generator[str, None, None]: template_param = '' if function_lib.has_bytes_return(func_decl): - template_param = '' + template_param = '' yield ( - f'{class_name}.def("__iter__", []({class_decl.name.cpp_name} &s)' - f'{{ return py::make_iterator{template_param}(s.begin(), s.end()); }}, ' - 'py::keep_alive<0, 1>());') + f'{class_name}.def("__iter__", []({class_decl.name.cpp_name} &s) {{' + f' return pybind11::make_iterator{template_param}(s.begin(), s.end());' + ' }, pybind11::keep_alive<0, 1>());' + ) def _generate_constructor( @@ -280,7 +283,7 @@ def _generate_constructor_overload( func_decl, release_gil=False, first_unknown_default_index=first_unknown_default_index) if func_decl.name.native == '__init__' and func_decl.is_extend_method: - yield f'{class_name}.def(py::init([]({params_with_types}) {{' + yield f'{class_name}.def(pybind11::init([]({params_with_types}) {{' if function_lib.unknown_default_argument_needs_non_default_value( params_list, first_unknown_default_index, first_unknown_default_param): yield I + function_lib.generate_value_error_for_unknown_default_param( @@ -291,7 +294,7 @@ def _generate_constructor_overload( yield from p.preprocess() func_keeps_gil = function_lib.func_keeps_gil(func_decl) if not func_keeps_gil: - yield I + 'py::gil_scoped_release release_gil;' + yield I + 'pybind11::gil_scoped_release release_gil;' yield I + f'return {func_decl.name.cpp_name}({params});' yield f'}}), {function_suffix}' @@ -299,7 +302,7 @@ def _generate_constructor_overload( cpp_name = class_decl.name.cpp_name if trampoline_generated: cpp_name = utils.trampoline_name(class_decl) - yield f'{class_name}.def(py::init([]({params_with_types}) {{' + yield f'{class_name}.def(pybind11::init([]({params_with_types}) {{' if function_lib.unknown_default_argument_needs_non_default_value( params_list, first_unknown_default_index, first_unknown_default_param): yield I + function_lib.generate_value_error_for_unknown_default_param( @@ -310,7 +313,7 @@ def _generate_constructor_overload( yield from p.preprocess() func_keeps_gil = function_lib.func_keeps_gil(func_decl) if not func_keeps_gil and params_with_types: - yield I + 'py::gil_scoped_release release_gil;' + yield I + 'pybind11::gil_scoped_release release_gil;' yield I + (f'return std::make_unique<{cpp_name}>' f'({params}).release();') yield f'}}), {function_suffix}' @@ -328,7 +331,7 @@ def _generate_constructor_overload( yield from p.preprocess() func_keeps_gil = function_lib.func_keeps_gil(func_decl) if not func_keeps_gil: - yield I + 'py::gil_scoped_release release_gil;' + yield I + 'pybind11::gil_scoped_release release_gil;' yield I + (f'return std::make_unique<{class_decl.name.cpp_name}>' f'({params});') yield f'}}, {function_suffix}' diff --git a/clif/pybind11/consts.py b/clif/pybind11/consts.py index 8282124f..94dea100 100644 --- a/clif/pybind11/consts.py +++ b/clif/pybind11/consts.py @@ -27,7 +27,9 @@ def generate_from(class_name: str, const_decl: ast_pb2.ConstDecl): # Legacy CLIF ignores postconversion for clif::char_ptr. if (function_lib.is_bytes_type(const_decl.type) and const_decl.type.cpp_type != '::clif::char_ptr'): - return_value_policy = ', py::return_value_policy::_return_as_bytes' - yield I + (f'{class_name}.attr("{const_decl.name.native}") = ' - f'py::cast(static_cast<{const_decl.type.cpp_type}>(' - f'{const_decl.name.cpp_name}){return_value_policy});') + return_value_policy = ', pybind11::return_value_policy::_return_as_bytes' + yield I + ( + f'{class_name}.attr("{const_decl.name.native}") = ' + f'pybind11::cast(static_cast<{const_decl.type.cpp_type}>(' + f'{const_decl.name.cpp_name}){return_value_policy});' + ) diff --git a/clif/pybind11/enums.py b/clif/pybind11/enums.py index 6c6972c5..daa2bc08 100644 --- a/clif/pybind11/enums.py +++ b/clif/pybind11/enums.py @@ -23,8 +23,8 @@ def generate_from(class_name: str, enum_decl: ast_pb2.EnumDecl): """Generates enums.""" kind = 'Enum' if enum_decl.enum_class else 'IntEnum' yield I + ( - f'{class_name} += py::native_enum<{enum_decl.name.cpp_name}>' - f'("{enum_decl.name.native}", py::native_enum_kind::{kind})' + f'{class_name} += pybind11::native_enum<{enum_decl.name.cpp_name}>' + f'("{enum_decl.name.native}", pybind11::native_enum_kind::{kind})' ) for i, member in enumerate(enum_decl.members): s = I + I + f'.value("{member.native}", {member.cpp_name})' diff --git a/clif/pybind11/function.py b/clif/pybind11/function.py index 75b54f45..40ffb447 100644 --- a/clif/pybind11/function.py +++ b/clif/pybind11/function.py @@ -92,13 +92,13 @@ def _generate_overload_for_unknown_default_function( ``` m.def("add", [](int a, Arg b, int c) { return add(a, b, c); - }, py::arg("a"), py::arg("b"), py::arg("c") = 3); + }, pybind11::arg("a"), pybind11::arg("b"), pybind11::arg("c") = 3); m.def("add", [](int a, int c) { // -b - throw py::value_error("argument b needs a non-default value"); - }, py::arg("a"), py::kw_only(), py::arg("c")); + throw pybind11::value_error("argument b needs a non-default value"); + }, pybind11::arg("a"), pybind11::kw_only(), pybind11::arg("c")); m.def("add", [](int a) { // -b, -c return add(a); - }, py::arg("a")); + }, pybind11::arg("a")); ``` Args: diff --git a/clif/pybind11/function_lib.py b/clif/pybind11/function_lib.py index befc91f0..b21f72c7 100644 --- a/clif/pybind11/function_lib.py +++ b/clif/pybind11/function_lib.py @@ -51,7 +51,7 @@ def __init__(self, param: ast_pb2.ParamDecl, param_name: str, ctype.startswith('::std::shared_ptr')) self.is_ptr = ptype.cpp_raw_pointer or is_smart_ptr if is_self_param: - self.cpp_type = 'py::object' + self.cpp_type = 'pybind11::object' self.gen_name = f'{param_name}_py' if self.is_ptr: self.function_argument = param_name @@ -146,9 +146,11 @@ def unknown_default_argument_needs_non_default_value( def generate_value_error_for_unknown_default_param( func_decl: ast_pb2.FuncDecl, first_unknown_default_param: ast_pb2.ParamDecl ) -> str: - return (f'throw py::value_error("{func_decl.name.native}() argument ' - f'{first_unknown_default_param.name.native} needs a non-default ' - 'value");') + return ( + f'throw pybind11::value_error("{func_decl.name.native}() argument ' + f'{first_unknown_default_param.name.native} needs a non-default ' + 'value");' + ) def generate_index_combination_for_unknown_default_func_decl( @@ -211,20 +213,23 @@ def generate_return_value_policy_for_type( if len(return_value_policy_list) > 1: return f'{{{return_value_policy_str}}}' else: - return ('py::return_value_policy_pack(std::vector<' - f'py::return_value_policy_pack>({{{return_value_policy_str}}}), ' - 'py::return_value_policy::_clif_automatic)') + return ( + 'pybind11::return_value_policy_pack(' + 'std::vector(' + f'{{{return_value_policy_str}}}),' + ' pybind11::return_value_policy::_clif_automatic)' + ) else: if param_type.lang_type == 'bytes': - return 'py::return_value_policy::_return_as_bytes' + return 'pybind11::return_value_policy::_return_as_bytes' elif is_callable_arg: if param_type.lang_type == 'object': - return 'py::return_value_policy::automatic_reference' - return 'py::return_value_policy::_clif_automatic' + return 'pybind11::return_value_policy::automatic_reference' + return 'pybind11::return_value_policy::_clif_automatic' elif reference_internal: - return 'py::return_value_policy::reference_internal' + return 'pybind11::return_value_policy::reference_internal' else: - return 'py::return_value_policy::_clif_automatic' + return 'pybind11::return_value_policy::_clif_automatic' def generate_return_value_policy_for_func_decl_params( @@ -247,7 +252,7 @@ def generate_return_value_policy_for_func_decl_params( else: return return_value_policy_str else: - return 'py::return_value_policy::_clif_automatic' + return 'pybind11::return_value_policy::_clif_automatic' def generate_function_suffixes( @@ -261,12 +266,12 @@ def generate_function_suffixes( if py_args: suffix += f'{py_args}, ' if func_decl.name.native in operators.ALL_OPS: - suffix += 'py::is_operator(), ' - suffix += 'py::return_value_policy::_clif_automatic' + suffix += 'pybind11::is_operator(), ' + suffix += 'pybind11::return_value_policy::_clif_automatic' if func_decl.docstring: suffix += f', {generate_docstring(func_decl.docstring)}' if release_gil and not func_decl.py_keep_gil: - suffix += ', py::call_guard()' + suffix += ', pybind11::call_guard()' suffix += ');' return suffix @@ -387,10 +392,11 @@ def generate_py_args(func_decl: ast_pb2.FuncDecl, params_list.append( _generate_py_arg_without_default(param, return_value_policy_pack) ) - # Insert `py::kw_only()` at the index of the first parameter with unknown - # default value so that pybind11 is not confused about which overload to use. + # Insert `pybind11::kw_only()` at the index of the first parameter with + # unknown default value so that pybind11 is not confused about which + # overload to use. if first_unknown_default_index != -1 and params_list: - params_list.insert(first_unknown_default_index, 'py::kw_only()') + params_list.insert(first_unknown_default_index, 'pybind11::kw_only()') operators.fix_py_args_for_operators_in_place( func_decl, params_list) return ', '.join(params_list) @@ -399,27 +405,30 @@ def generate_py_args(func_decl: ast_pb2.FuncDecl, def _generate_py_arg_with_default( param: ast_pb2.ParamDecl, return_value_policy_pack: str ) -> str: - """Generate `py::arg` for parameters with default value.""" + """Generate `pybind11::arg` for parameters with default value.""" if return_value_policy_pack: if param.default_value == 'nullptr': return ( - f'py::arg("{param.name.cpp_name}")' + f'pybind11::arg("{param.name.cpp_name}")' f'.policies({return_value_policy_pack}) = {param.default_value}' ) else: return ( - f'py::arg("{param.name.cpp_name}")' + f'pybind11::arg("{param.name.cpp_name}")' f'.policies({return_value_policy_pack}) = ' f'static_cast<{param.type.cpp_type}>({param.default_value})' ) else: if param.default_value == 'nullptr': if param.type.cpp_type == _CPP_TYPE_PYOBJECT_PTR_FROM_MATCHER: - return f'py::arg("{param.name.cpp_name}") = py::nullptr_default_arg()' - return f'py::arg("{param.name.cpp_name}") = {param.default_value}' + return ( + f'pybind11::arg("{param.name.cpp_name}") =' + ' pybind11::nullptr_default_arg()' + ) + return f'pybind11::arg("{param.name.cpp_name}") = {param.default_value}' else: return ( - f'py::arg("{param.name.cpp_name}") = ' + f'pybind11::arg("{param.name.cpp_name}") = ' f'static_cast<{param.type.cpp_type}>({param.default_value})' ) @@ -429,10 +438,11 @@ def _generate_py_arg_without_default( ) -> str: if return_value_policy_pack: return ( - f'py::arg("{param.name.cpp_name}").policies({return_value_policy_pack})' + f'pybind11::arg("{param.name.cpp_name}")' + f'.policies({return_value_policy_pack})' ) else: - return f'py::arg("{param.name.cpp_name}")' + return f'pybind11::arg("{param.name.cpp_name}")' def _generate_return_value_policy_pack_for_py_arg( @@ -444,7 +454,7 @@ def _generate_return_value_policy_pack_for_py_arg( param.type.callable ) if policy: - return f'py::return_value_policy_pack({policy})' + return f'pybind11::return_value_policy_pack({policy})' else: return '' diff --git a/clif/pybind11/generator.py b/clif/pybind11/generator.py index 96864797..493e8a1a 100644 --- a/clif/pybind11/generator.py +++ b/clif/pybind11/generator.py @@ -197,7 +197,7 @@ def generate_from(self, ast: ast_pb2.AST): yield '// When manually converting this code to a pure pybind11 extension,' yield '// change this function to:' yield f'// PYBIND11_MODULE({self._module_name}, m)' - yield 'void PyclifPybind11ModuleInit(py::module_ m) {' + yield 'void PyclifPybind11ModuleInit(pybind11::module_ m) {' for s in self._generate_import_modules(ast): yield I + s for decl in ast.decls: @@ -260,8 +260,10 @@ def _generate_import_modules(self, for module_path in all_modules: module_variable_name = utils.generate_mangled_name_for_module( module_path) - yield I + (f'auto {module_variable_name} = ' - f'py::module_::import("{module_path}");') + yield I + ( + f'auto {module_variable_name} = ' + f'pybind11::module_::import("{module_path}");' + ) def _generate_headlines(self): """Generates #includes and headers.""" @@ -287,8 +289,6 @@ def _generate_headlines(self): yield '#include "clif/pybind11/type_casters.h"' yield '#include "third_party/pybind11_protobuf/native_proto_caster.h"' yield '' - yield 'namespace py = pybind11;' - yield '' def _generate_trampoline_classes( self, trampoline_class_names: Set[str], decl: ast_pb2.Decl): @@ -303,8 +303,10 @@ def _generate_trampoline_classes( trampoline_class_name = utils.trampoline_name(decl.class_) assert decl.class_.name.cpp_name not in trampoline_class_names trampoline_class_names.add(trampoline_class_name) - yield (f'struct {trampoline_class_name} : {decl.class_.name.cpp_name}, ' - 'py::trampoline_self_life_support {') + yield ( + f'struct {trampoline_class_name} : {decl.class_.name.cpp_name}, ' + 'pybind11::trampoline_self_life_support {' + ) class_name = decl.class_.name.cpp_name.split('::')[-1] yield I + f'using {decl.class_.name.cpp_name}::{class_name};' for member in virtual_members: @@ -326,7 +328,7 @@ def _generate_virtual_function(self, params_list = [] for p in func_decl.params: if p.type.lang_type == 'bytes' and 'std::string' in p.cpp_exact_type: - params_list.append(f'py::bytes({p.name.cpp_name})') + params_list.append(f'pybind11::bytes({p.name.cpp_name})') else: params_list.append(p.name.cpp_name) params = ', '.join(params_list) @@ -365,7 +367,7 @@ def _generate_virtual_function(self, if ',' in return_type: yield I + I + f'using {func_decl.name.native}_return = {return_type};' return_type = f'{func_decl.name.native}_return' - yield I + I + 'py::gil_scoped_acquire hold_gil;' + yield I + I + 'pybind11::gil_scoped_acquire hold_gil;' yield I + I + f'{pybind11_override}(' if pybind11_override not in ('PYBIND11_OVERRIDE_STATUS_RETURN', 'PYBIND11_OVERRIDE_PURE_STATUS_RETURN'): @@ -379,7 +381,7 @@ def _generate_virtual_function(self, ) ) return_value_policy_pack = ( - f'py::return_value_policy_pack({return_value_policy})' + f'pybind11::return_value_policy_pack({return_value_policy})' ) if params: yield I + I + I + f'{return_value_policy_pack},' diff --git a/clif/pybind11/lambdas.py b/clif/pybind11/lambdas.py index 7990d965..34a7c73a 100644 --- a/clif/pybind11/lambdas.py +++ b/clif/pybind11/lambdas.py @@ -77,8 +77,10 @@ def generate_lambda( def generate_check_nullptr( func_decl: ast_pb2.FuncDecl, param_name: str) -> Generator[str, None, None]: yield I + f'if ({param_name} == nullptr) {{' - yield I + I + (f'throw py::type_error("{func_decl.name.native}() ' - f'argument {param_name} is not valid.");') + yield I + I + ( + f'throw pybind11::type_error("{func_decl.name.native}() ' + f'argument {param_name} is not valid.");' + ) yield I +'}' @@ -91,14 +93,16 @@ def generate_cpp_function_return_post_process( elif func_decl.name.native == '__enter__@': yield I + f'return {self_param};' elif func_decl.name.native == '__exit__@': - yield I + 'return py::none();' + yield I + 'return pybind11::none();' elif func_decl.postproc: assert '.' in func_decl.postproc module_name, method_name = func_decl.postproc.rsplit('.', maxsplit=1) # TODO: Port or reuse `clif::ImportFQName`. - yield I + f'auto mod = py::module_::import("{module_name}");' - yield I + ('py::object result_ = ' - f'mod.attr("{method_name}")({function_call_returns});') + yield I + f'auto mod = pybind11::module_::import("{module_name}");' + yield I + ( + 'pybind11::object result_ = ' + f'mod.attr("{method_name}")({function_call_returns});' + ) yield I + 'return result_;' elif function_call_returns: if len(func_decl.returns) > 1: @@ -106,7 +110,7 @@ def generate_cpp_function_return_post_process( else: yield I + f'return {function_call_returns};' else: - yield I + 'return py::none();' + yield I + 'return pybind11::none();' def generate_lambda_body( @@ -123,7 +127,7 @@ def generate_lambda_body( if (class_decl and func_decl_is_member_function(func_decl, class_decl) and not func_decl.is_extend_method): - yield I + f'auto self = py::cast<{class_decl.name.cpp_name}*>(self_py);' + yield I + f'auto self = pybind11::cast<{class_decl.name.cpp_name}*>(self_py);' # Generates void pointer check for parameters that are converted from non # pointers by code generator. @@ -139,25 +143,29 @@ def generate_lambda_body( index = params[start_idx] self_param = ( params[0].gen_name if func_decl.is_extend_method else 'self_py') - yield I + f'py::object length_function_ = {self_param}.attr("__len__");' + yield I + f'pybind11::object length_function_ = {self_param}.attr("__len__");' yield I + 'if (length_function_.is_none()) {' - yield I + I + (f'throw py::attribute_error("class {class_decl.name.native} ' - f'defined {func_decl.name.native}, but does not define ' - '`__len__` function.");') + yield I + I + ( + 'throw pybind11::attribute_error("class' + f' {class_decl.name.native} defined {func_decl.name.native}, but does' + ' not define `__len__` function.");' + ) yield I +'}' - yield I + (f'Py_ssize_t {index.gen_name}_ = ::clif::item_index(' - f'{index.gen_name}, py::cast(length_function_()));') + yield I + ( + f'Py_ssize_t {index.gen_name}_ = ::clif::item_index(' + f'{index.gen_name}, pybind11::cast(length_function_()));' + ) yield I + (f'if ({index.gen_name}_ < 0) {{') - yield I + I + 'throw py::index_error("index out of range.");' + yield I + I + 'throw pybind11::index_error("index out of range.");' yield I +'}' yield I + f'{index.gen_name} = {index.gen_name}_;' if not cpp_void_return: ret0 = func_decl.returns[0] if not ret0.type.cpp_type: - yield I + 'py::cpp_function ret0;' + yield I + 'pybind11::cpp_function ret0;' else: - yield I + 'py::object ret0;' + yield I + 'pybind11::object ret0;' # Generates declarations of pointer return values outside of scope for i, r in enumerate(func_decl.returns): @@ -167,7 +175,7 @@ def generate_lambda_body( yield I + '{' func_keeps_gil = function_lib.func_keeps_gil(func_decl) if not func_keeps_gil: - yield I + I + 'py::gil_scoped_release gil_release;' + yield I + I + 'pybind11::gil_scoped_release gil_release;' # Generates call to the wrapped function cpp_void_return = func_decl.cpp_void_return or not func_decl.returns @@ -176,19 +184,21 @@ def generate_lambda_body( if not ret0.type.cpp_type: callback_cpp_type = function_lib.generate_callback_signature(ret0) callback_params_list = [ - f'py::arg("{param.name.native}")' - for param in ret0.type.callable.params] + f'pybind11::arg("{param.name.native}")' + for param in ret0.type.callable.params + ] callback_params_str = ', '.join(callback_params_list) yield I + I + (f'{callback_cpp_type} ret0_ = ' f'{function_call}({function_call_params});') yield I + I + '{' if not func_keeps_gil: - yield I + I + I + 'py::gil_scoped_acquire gil_acquire;' + yield I + I + I + 'pybind11::gil_scoped_acquire gil_acquire;' if callback_params_str: - yield I + I + I + ('ret0 = py::cpp_function(ret0_, ' - f'{callback_params_str});') + yield I + I + I + ( + f'ret0 = pybind11::cpp_function(ret0_, {callback_params_str});' + ) else: - yield I + I + I + 'ret0 = py::cpp_function(ret0_);' + yield I + I + I + 'ret0 = pybind11::cpp_function(ret0_);' for s in _generate_python_error_check(): yield I + I + I + s yield I + I + '}' @@ -201,7 +211,7 @@ def generate_lambda_body( func_decl, ret0, 'ret0_', codegen_info, class_decl) yield I + I + '{' if not func_keeps_gil: - yield I + I + I + 'py::gil_scoped_acquire gil_acquire;' + yield I + I + I + 'pybind11::gil_scoped_acquire gil_acquire;' yield I + I + I + f'ret0 = {ret0_with_py_cast};' for s in _generate_python_error_check(): yield I + I + I + s @@ -273,16 +283,16 @@ def generate_function_call_return( ret = f'({status_type})(std::move({return_value_name}))' if (func_decl.return_value_policy == ast_pb2.FuncDecl.ReturnValuePolicy.REFERENCE): - return_value_policy = 'py::return_value_policy::reference' + return_value_policy = 'pybind11::return_value_policy::reference' return_value_policy_pack = ( - f'py::return_value_policy_pack({return_value_policy})' + f'pybind11::return_value_policy_pack({return_value_policy})' ) if func_decl_is_extend_member_function(func_decl, class_decl): - return f'py::cast({ret}, {return_value_policy_pack}, arg0_py)' + return f'pybind11::cast({ret}, {return_value_policy_pack}, arg0_py)' elif func_decl_is_member_function(func_decl, class_decl): - return f'py::cast({ret}, {return_value_policy_pack}, self_py)' + return f'pybind11::cast({ret}, {return_value_policy_pack}, self_py)' else: - return f'py::cast({ret}, {return_value_policy_pack})' + return f'pybind11::cast({ret}, {return_value_policy_pack})' def _generate_lambda_params_with_types( @@ -293,14 +303,14 @@ def _generate_lambda_params_with_types( params_list = [f'{p.cpp_type} {p.gen_name}' for p in params] if (func_decl_is_member_function(func_decl, class_decl) and not func_decl.is_extend_method): - params_list = ['py::object self_py'] + params_list + params_list = ['pybind11::object self_py'] + params_list # For reflected operations, we need to generate (const Type& self, int lhs) # instead of (int lhs, const Type& self). So swapping the two function # parameters. if func_decl.name.native in operators.REFLECTED_OPS and len(params_list) == 2: params_list.reverse() if func_decl.name.native == '__exit__@' and class_decl: - params_list.append('py::args') + params_list.append('pybind11::args') return ', '.join(params_list) @@ -333,5 +343,5 @@ def generate_function_call( def _generate_python_error_check( acquire_gil: bool = False) -> Generator[str, None, None]: if acquire_gil: - yield 'py::gil_scoped_acquire gil_acquire;' + yield 'pybind11::gil_scoped_acquire gil_acquire;' yield '::clif::ThrowErrorAlreadySetIfPythonErrorOccurred();' diff --git a/clif/pybind11/operators.py b/clif/pybind11/operators.py index 509f2d32..2ac42192 100644 --- a/clif/pybind11/operators.py +++ b/clif/pybind11/operators.py @@ -100,7 +100,7 @@ def fix_py_args_for_operators_in_place( func_decl: ast_pb2.FuncDecl, py_args: List[str]) -> None: - """Fix `py::args` declaration of the operator. + """Fix `pybind11::args` declaration of the operator. Sometimes users might implement operators as free functions, not C++ member functions. In this case, PyCLIF AST will include an extra parameter for @@ -110,7 +110,7 @@ def fix_py_args_for_operators_in_place( Args: func_decl: AST function declaration in proto format. - py_args: A list of strings that are like `py::arg('a')`. + py_args: A list of strings that are like `pybind11::arg('a')`. Raises: RuntimeError: If the operator overloading has unexpected number of function diff --git a/clif/pybind11/unknown_default_value.py b/clif/pybind11/unknown_default_value.py index b65f3634..77a7131a 100644 --- a/clif/pybind11/unknown_default_value.py +++ b/clif/pybind11/unknown_default_value.py @@ -32,12 +32,14 @@ def generate_from( ) -> Generator[str, None, None]: """Generates lambda to handle functions with unknown default args.""" func_name = func_decl.name.native.rstrip('#').rstrip('@') - params_with_type = 'py::args args, py::kwargs kw' + params_with_type = 'pybind11::args args, pybind11::kwargs kw' self_py = 'arg0_py' if func_decl.is_extend_method else 'self_py' if class_decl and lambdas.func_decl_is_member_function(func_decl, class_decl): - params_with_type = f'py::object {self_py}, ' + params_with_type - yield (f'{module_name}.{function_lib.generate_def(func_decl)}' - f'("{func_name}", []({params_with_type}) -> py::object {{') + params_with_type = f'pybind11::object {self_py}, ' + params_with_type + yield ( + f'{module_name}.{function_lib.generate_def(func_decl)}' + f'("{func_name}", []({params_with_type}) -> pybind11::object {{' + ) if class_decl and lambdas.func_decl_is_member_function(func_decl, class_decl): yield I + f'auto self = {self_py}.cast<{class_decl.name.cpp_name}*>();' @@ -64,7 +66,7 @@ def generate_from( f'"{args_signature}:{func_decl.name.native}",') args_array = ', '.join(f'&a[{i}]' for i in range(nargs)) yield I + I + I + f'const_cast(names), {args_array})) {{' - yield I + I + 'return py::object();' + yield I + I + 'return pybind11::object();' yield I + '}' if minargs < nargs and not have_addl_returns: @@ -82,7 +84,7 @@ def generate_from( yield I + arg_declaration arg_cpp_type = arg_declaration[:arg_declaration.rfind(' ')] if i < minargs: - yield I + f'{arg} = py::cast<{arg_cpp_type}>(py::handle(a[{i}]));' + yield I + f'{arg} = pybind11::cast<{arg_cpp_type}>(pybind11::handle(a[{i}]));' if check_nullptr: yield from lambdas.generate_check_nullptr(func_decl, arg) else: @@ -101,27 +103,36 @@ def generate_from( if p.type.cpp_type.startswith('::std::unique_ptr'): yield indent + I + (f'if (!a[{i}]) {{ /* default-constructed ' 'smartptr */}') - yield indent + I + (f'else {arg} = py::cast<{arg_cpp_type}> ' - f'(py::handle(a[{i}]));') + yield indent + I + ( + f'else {arg} = pybind11::cast<{arg_cpp_type}> ' + f'(pybind11::handle(a[{i}]));' + ) else: yield indent + I + f'if (!a[{i}]) {{' yield indent + I + I + ( - f'throw py::value_error("{func_decl.name.native}() argument ' - f'{p.name.native} needs a non-default value");') + f'throw pybind11::value_error("{func_decl.name.native}()' + f' argument {p.name.native} needs a non-default value");' + ) yield indent + I + '}' - yield indent + I + (f'else {arg} = py::cast<{arg_cpp_type}>' - f'(py::handle(a[{i}]));') + yield indent + I + ( + f'else {arg} = pybind11::cast<{arg_cpp_type}>' + f'(pybind11::handle(a[{i}]));' + ) else: - yield indent + I + (f'{arg} = py::cast<{arg_cpp_type}>' - f'(py::handle(a[{i}]));') + yield indent + I + ( + f'{arg} = pybind11::cast<{arg_cpp_type}>' + f'(pybind11::handle(a[{i}]));' + ) if check_nullptr: for line in lambdas.generate_check_nullptr(func_decl, arg): yield indent + line else: yield indent + I + (f'if (!a[{i}]) {arg} = ({p.type.cpp_type})' f'{p.default_value};') - yield indent + I + (f'else {arg} = py::cast<{arg_cpp_type}>' - f'(py::handle(a[{i}]));') + yield indent + I + ( + f'else {arg} = pybind11::cast<{arg_cpp_type}>' + f'(pybind11::handle(a[{i}]));' + ) if check_nullptr: for line in lambdas.generate_check_nullptr(func_decl, arg): yield indent + line @@ -182,7 +193,7 @@ def generate_from( if not cpp_void_return: ret0_with_py_cast = lambdas.generate_function_call_return( func_decl, func_decl.returns[0], 'ret0_', codegen_info, class_decl) - yield I + f'py::object ret0 = {ret0_with_py_cast};' + yield I + f'pybind11::object ret0 = {ret0_with_py_cast};' function_call_returns = lambdas.generate_function_call_returns( func_decl, codegen_info, class_decl) diff --git a/clif/pybind11/utils.py b/clif/pybind11/utils.py index 15a4f81a..f036f0d8 100644 --- a/clif/pybind11/utils.py +++ b/clif/pybind11/utils.py @@ -33,9 +33,10 @@ class CodeGenInfo: # Is type caster of `absl::Status` required? requires_status: bool - # What C++ types do we need to generate `py::dynamic_attr()`? This is needed - # because if we generate `py::dynamic_attr()` for the base class, we also need - # to generate `py::dynamic_attr()` for the derived class. + # What C++ types do we need to generate `pybind11::dynamic_attr()`? + # This is needed because if we generate `pybind11::dynamic_attr()` for the + # base class, we also need to generate `pybind11::dynamic_attr()` for the + # derived class. dynamic_attr_types: Set[str] # Dict[Python Name, Fully qualified Python name] diff --git a/clif/pybind11/variables.py b/clif/pybind11/variables.py index 85988ef1..7e113428 100644 --- a/clif/pybind11/variables.py +++ b/clif/pybind11/variables.py @@ -80,7 +80,7 @@ def _generate_cpp_get( var_decl.type, reference_internal=reference_internal ) return_value_policy_pack = ( - f'py::return_value_policy_pack({return_value_policy})' + f'pybind11::return_value_policy_pack({return_value_policy})' ) if not _is_var_decl_type_cpp_copyable(var_decl): @@ -103,7 +103,7 @@ def _generate_cpp_get_for_copyable_type( """Generate lambda expressions for getters when the type is copyable.""" self_param_type = _generate_self_param_type_for_cpp_get(var_decl, class_decl) if generate_comma: - yield I + f'py::cpp_function([]({self_param_type} self) {{' + yield I + f'pybind11::cpp_function([]({self_param_type} self) {{' else: yield I + f'[]({self_param_type} self) {{' yield I + I + f'return {ret};' @@ -122,13 +122,14 @@ def _generate_cpp_get_for_uncopyable_type( ) -> Generator[str, None, None]: """Generate lambda expressions for getters when the type is uncopyable.""" if generate_comma: - yield I + 'py::cpp_function([](py::object self_py) -> py::object {' + yield I + 'pybind11::cpp_function([](pybind11::object self_py) -> pybind11::object {' else: - yield I + '[](py::object self_py) -> py::object {' + yield I + '[](pybind11::object self_py) -> pybind11::object {' self_param_type = _generate_self_param_type_for_cpp_get(var_decl, class_decl) yield I + I + f'{self_param_type} self = self_py.cast<{self_param_type}>();' - yield I + I + (f'return py::cast({ret}, {return_value_policy_pack}, ' - 'self_py);') + yield I + I + ( + f'return pybind11::cast({ret}, {return_value_policy_pack}, self_py);' + ) if generate_comma: yield I + '}),' else: @@ -142,8 +143,10 @@ def _generate_cpp_set_without_setter( ) -> Generator[str, None, None]: """Generate lambda expressions for setters when setters are undefined.""" if generate_comma: - yield I + (f'py::cpp_function([]({class_decl.name.cpp_name}& self, ' - f'{var_decl.type.cpp_type} v) {{') + yield I + ( + f'pybind11::cpp_function([]({class_decl.name.cpp_name}& self, ' + f'{var_decl.type.cpp_type} v) {{' + ) else: yield I + ( f'[]({class_decl.name.cpp_name}& self, {var_decl.type.cpp_type} v) {{' @@ -166,8 +169,10 @@ def _generate_cpp_set_with_setter( assert var_decl.cpp_set.params, (f'var_decl {var_decl.name.native} does not' 'have any params.') self_param_type = _generate_self_param_type_for_cpp_set(var_decl, class_decl) - yield I + (f'py::cpp_function([]({self_param_type} self, ' - f'{var_decl.cpp_set.params[-1].type.cpp_type} v) {{') + yield I + ( + f'pybind11::cpp_function([]({self_param_type} self, ' + f'{var_decl.cpp_set.params[-1].type.cpp_type} v) {{' + ) if var_decl.is_extend_variable: function_call = f'{var_decl.cpp_set.name.cpp_name}(self, std::move(v))' else: @@ -199,7 +204,7 @@ def generate_from( var_decl.type ) return_value_policy_pack = ( - f'py::return_value_policy_pack({return_value_policy})' + f'pybind11::return_value_policy_pack({return_value_policy})' ) yield ( f'{class_name}.def_readwrite("{var_decl.name.native}", '