Skip to content

Conversation

@shihai1991
Copy link
Member

@shihai1991shihai1991 commented Feb 12, 2020

@shihai1991
Copy link
MemberAuthor

cc @brandtbucher
Due to brandt have doing some refactoring work of PyModule_AddObject() ;)

Copy link
Member

@brandtbucherbrandtbucher left a comment

Choose a reason for hiding this comment

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

Thanks for your work on this! Looks good, just one question below:

@brandtbucherbrandtbucher added the type-bug An unexpected behavior, bug, or error label Feb 12, 2020

#ifdef CLOCK_REALTIME
PyModule_AddIntMacro(m, CLOCK_REALTIME);
if (PyModule_AddIntMacro(m, CLOCK_REALTIME) < 0){
Copy link
Member

Choose a reason for hiding this comment

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

Is the reason that you need goto in every one of these blocks that PyErr_Occcurred() will get reset if you make other PyModule_AddIntMacro calls that don't fail along the way?

Either way, I'm mildly tempted to suggest adding a wrapper macro like:

#define_PyModule_AddIntMacroOrGotoError(m, macro) \ if (PyModule_AddIntMacro(m, macro) < 0){goto error}

But I dunno. I'm torn between the principles of "don't repeat yourself" and "don't write a preprocessor macro with a goto in it (you monster)".

Copy link
Member

@brandtbucherbrandtbucherFeb 12, 2020

Choose a reason for hiding this comment

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

Between the two options, I'm personally +0 on repetition. Incorrect error handling for the whole PyModule_Add family of functions is rampant in the stdlib (see my work in bpo-38823), and I think reinforcing the correct usage is good to have to keep people from repeating the same old patterns (especially in outside projects).

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Is the reason that you need goto in every one of these blocks that PyErr_Occcurred() will get reset if you make other PyModule_AddIntMacro calls that don't fail along the way?

No, I just try to make the Py_DECREF(m);return NULL; looks like a common processing item.

I think reinforcing the correct usage is good to have to keep people from repeating the same old patterns

+1

Copy link
Member

Choose a reason for hiding this comment

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

I dislike macros hiding a goto. A goto statement is dangerous, I prefer to make it explicit. The "if (...){goto error}" is a very common pattern in Python, especially in initialization code.

@vstinner
Copy link
Member

Thanks @shihai1991, I merged your PR.

farazs-github pushed a commit to MediaTek-Labs/cpython that referenced this pull request Nov 12, 2021
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip newstype-bugAn unexpected behavior, bug, or error

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@shihai1991@vstinner@pganssle@brandtbucher@the-knights-who-say-ni@bedevere-bot