-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-27535: Optimize warnings.warn() #4508
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
Conversation
* Optimize warnings.filterwarnings(). Replace re.compile('') with None to avoid the cost of calling a regex.match() method, whereas it always matchs. * Optimize get_warnings_attr(): replace PyObject_GetAttrString() with _PyObject_GetAttrId(). Cleanup also create_filter(): * Use _Py_IDENTIFIER() to allow to cleanup strings at Python finalization * Replace Py_FatalError() with a regular exceptions
} | ||
else { | ||
Py_FatalError("unknown action"); | ||
PyErr_SetString(PyExc_ValueError, "unknown action"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this change. This is an internal function called with predefined set of arguments. The code in the else
branch is a dead code. Here should be Py_FatalError()
or Py_UNREACHABLE()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like your idea of using _Py_Identifier: I proposed #4516 to implement your idea. It avoids the need to have to unhandle "unknown action".
FYI I'm trying to remove all Py_FatalError() calls from the Python source code, one by one, when it's possible and when it makes sense. I added a new _PyInitError API to report errors at Python initialization, so the caller can decide when Py_FatalError() will be called, and maybe release memory before calling Py_FatalError() (or flush files, or something else).
static PyObject *default_str = NULL; | ||
static PyObject *always_str = NULL; | ||
PyObject *action_obj = NULL; | ||
_Py_IDENTIFIER(ignore); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This optimization doesn't make sense since create_filter()
is used only few times at start. It would be better to pass _Py_Identifier *
instead of const char *
, this could save few lines of code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I chose to put two changes into the same commit. This change was made to allow to free strings at Python finallization, it's not an optimization.
None to avoid the cost of calling a regex.match() method, whereas
it always matchs.
_PyObject_GetAttrId().
Cleanup also create_filter():
finalization
https://bugs.python.org/issue27535