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
a SystemError and an assertion failure in warnings.warn_explicit() #75466
Comments
the following causes an assertion failure in Python/_warnings.c in import warnings
class BadLoader:
def get_source(self, fullname):
class BadSource:
def splitlines(self):
return [42]
return BadSource() del warnings._showwarnmsg in short, the assertion failure would happen in warnings.warn_explicit() in case the following raises a SystemError: import warnings
class BadLoader:
def get_source(self, fullname):
class BadSource:
def splitlines(self):
return 42
return BadSource() warnings.warn_explicit(message='foo', category=UserWarning, filename='bar', in short, warnings.warn_explicit() raises the SystemError in case ISTM that adding a check in warnings_warn_explicit() (in Python/_warnings.c), What do you think? Is it OK to permit get_source() to return only None or a str? |
on a second thought, BadSource could be a subclass of str, so maybe we |
An alternative solution is using str.splitlines(source) instead of source.splitlines(). It raises a TypeError if the argument is not a text string and always returns a list of strings. According to the documentation get_source() must return a text string or None. |
ISTM that your solution is better than mine, Serhiy, so I updated the PR. |
PyUnicode_Splitlines() cannot be used here in 2.7, because the source is likely a 8-bit string. Actually I'm not sure this issue is worth to be fixed in 2.7. The fix would be different from 3.x and more complex. |
In 2.7, PyUnicode_Splitlines() first does: So i thought that PyUnicode_Splitlines() would be fine with receiving a string. But now i realize that even in case i was right there, PyUnicode_Splitlines() |
And it raises an exception if the string contains non-ASCII characters. It is better to avoid str<->unicode convertion as long as possible. And when |
Another thought - the existing code assumes that splitlines() returned a string. |
oh, of course, checking that get_source() returned a string before passing it to |
What if get_source() returned a unicode string? Usually it returns 8-bit string, but in many cases unicode is accepted if str is expected, so you need to check this option too. |
But in case get_source() returned a unicode, is it likely that the splitlines() method |
I didn't check. I supposed that the other code can support unicode by |
New changeset 8b4ff53 by Serhiy Storchaka (Oren Milman) in branch 'master': |
I just missed PR 3803. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: