Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PYBIND11_MODULE name usable with define #1082

Merged
merged 1 commit into from
Sep 13, 2017
Merged

Make PYBIND11_MODULE name usable with define #1082

merged 1 commit into from
Sep 13, 2017

Conversation

tzh1043
Copy link
Contributor

@tzh1043 tzh1043 commented Sep 13, 2017

The name in the PYBIND11_MODULE(name, variable) #define can not be used with a preprocessor define.

Given a simple cpp file define_module_name.cpp, where MODULENAME is defined elsewhere, and preprocess it.

// define_module_name.cpp, preprocessor command (add your pybind11/python -I flags!):
// c++ -E -o define_module_name.ii -DMODULENAME=my_module_name define_module_name.cpp
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>

PYBIND11_MODULE(MODULENAME, m) { }
PYBIND11_EMBEDDED_MODULE(MODULENAME, m) { }

The result in define_module_name.ii will contain both my_module_name and MODULENAME:

PyObject *PyInit_my_module_name()

static void pybind11_init_MODULENAME(pybind11::module &);
auto m = pybind11::module("MODULENAME");
pybind11_init_MODULENAME(m);
void pybind11_init_MODULENAME(pybind11::module &m) { }

This commit fixes this by adding one level of macro indirection.

@@ -205,6 +205,7 @@ extern "C" {
#define PYBIND11_TRY_NEXT_OVERLOAD ((PyObject *) 1) // special failure return code
#define PYBIND11_STRINGIFY(x) #x
#define PYBIND11_TOSTRING(x) PYBIND11_STRINGIFY(x)
#define PYBIND11_STRCONCAT(first, second) first##second
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick: this isn't actually string concatenation; perhaps rename to just PYBIND11_CONCAT?

#define PYBIND11_MODULE(name, variable) \
static void pybind11_init_##name(pybind11::module &); \
static void PYBIND11_STRCONCAT(pybind11_init_,name)(pybind11::module &); \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor style consistency issue: need a space after a comma.

@@ -293,7 +295,7 @@ extern "C" {
return nullptr; \
} \
} \
void pybind11_init_##name(pybind11::module &variable)
void PYBIND11_STRCONCAT(pybind11_init_,name)(pybind11::module &variable)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Member

@jagerman jagerman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me; I left a couple comments for minor cosmetic details.

@jagerman jagerman added this to the v2.2.1 milestone Sep 13, 2017
@jagerman
Copy link
Member

This is a pretty small change and seems worth including in 2.2.1.

Copy link
Member

@dean0x7d dean0x7d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I just have two whitespace nitpicks.

@@ -266,8 +267,9 @@ extern "C" {
});
}
\endrst */

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove whitespace between docs and definition.

@@ -43,12 +43,14 @@
});
}
\endrst */


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@dean0x7d dean0x7d merged commit d81d11a into pybind:master Sep 13, 2017
@dean0x7d
Copy link
Member

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants