Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
d8089e0d
Commit
d8089e0d
authored
Jul 11, 2014
by
Berker Peksag
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #16382: Improve exception message of warnings.warn() for bad category.
Initial patch by Phil Elson.
parent
6e1ccfe8
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
48 additions
and
7 deletions
+48
-7
Lib/test/test_warnings.py
Lib/test/test_warnings.py
+35
-0
Lib/warnings.py
Lib/warnings.py
+3
-1
Misc/NEWS
Misc/NEWS
+3
-0
Python/_warnings.c
Python/_warnings.c
+7
-6
No files found.
Lib/test/test_warnings.py
View file @
d8089e0d
...
...
@@ -370,6 +370,41 @@ class WarnTests(BaseTest):
with
self
.
assertRaises
(
ValueError
):
self
.
module
.
warn
(
BadStrWarning
())
def
test_warning_classes
(
self
):
class
MyWarningClass
(
Warning
):
pass
class
NonWarningSubclass
:
pass
# passing a non-subclass of Warning should raise a TypeError
with
self
.
assertRaises
(
TypeError
)
as
cm
:
self
.
module
.
warn
(
'bad warning category'
,
''
)
self
.
assertIn
(
'category must be a Warning subclass, not '
,
str
(
cm
.
exception
))
with
self
.
assertRaises
(
TypeError
)
as
cm
:
self
.
module
.
warn
(
'bad warning category'
,
NonWarningSubclass
)
self
.
assertIn
(
'category must be a Warning subclass, not '
,
str
(
cm
.
exception
))
# check that warning instances also raise a TypeError
with
self
.
assertRaises
(
TypeError
)
as
cm
:
self
.
module
.
warn
(
'bad warning category'
,
MyWarningClass
())
self
.
assertIn
(
'category must be a Warning subclass, not '
,
str
(
cm
.
exception
))
with
self
.
assertWarns
(
MyWarningClass
)
as
cm
:
self
.
module
.
warn
(
'good warning category'
,
MyWarningClass
)
self
.
assertEqual
(
'good warning category'
,
str
(
cm
.
warning
))
with
self
.
assertWarns
(
UserWarning
)
as
cm
:
self
.
module
.
warn
(
'good warning category'
,
None
)
self
.
assertEqual
(
'good warning category'
,
str
(
cm
.
warning
))
with
self
.
assertWarns
(
MyWarningClass
)
as
cm
:
self
.
module
.
warn
(
'good warning category'
,
MyWarningClass
)
self
.
assertIsInstance
(
cm
.
warning
,
Warning
)
class
CWarnTests
(
WarnTests
,
unittest
.
TestCase
):
module
=
c_warnings
...
...
Lib/warnings.py
View file @
d8089e0d
...
...
@@ -162,7 +162,9 @@ def warn(message, category=None, stacklevel=1):
# Check category argument
if
category
is
None
:
category
=
UserWarning
assert
issubclass
(
category
,
Warning
)
if
not
(
isinstance
(
category
,
type
)
and
issubclass
(
category
,
Warning
)):
raise
TypeError
(
"category must be a Warning subclass, "
"not '{:s}'"
.
format
(
type
(
category
).
__name__
))
# Get context information
try
:
caller
=
sys
.
_getframe
(
stacklevel
)
...
...
Misc/NEWS
View file @
d8089e0d
...
...
@@ -108,6 +108,9 @@ Core and Builtins
Library
-------
- Issue #16382: Improve exception message of warnings.warn() for bad
category. Initial patch by Phil Elson.
- Issue #21932: os.read() now uses a :c:func:`Py_ssize_t` type instead of
:c:type:`int` for the size to support reading more than 2 GB at once. On
Windows, the size is truncted to INT_MAX. As any call to os.read(), the OS
...
...
Python/_warnings.c
View file @
d8089e0d
...
...
@@ -619,16 +619,17 @@ get_category(PyObject *message, PyObject *category)
if
(
rc
==
1
)
category
=
(
PyObject
*
)
message
->
ob_type
;
else
if
(
category
==
NULL
)
else
if
(
category
==
NULL
||
category
==
Py_None
)
category
=
PyExc_UserWarning
;
/* Validate category. */
rc
=
PyObject_IsSubclass
(
category
,
PyExc_Warning
);
if
(
rc
==
-
1
)
return
NULL
;
if
(
rc
==
0
)
{
PyErr_SetString
(
PyExc_ValueError
,
"category is not a subclass of Warning"
);
/* category is not a subclass of PyExc_Warning or
PyObject_IsSubclass raised an error */
if
(
rc
==
-
1
||
rc
==
0
)
{
PyErr_Format
(
PyExc_TypeError
,
"category must be a Warning subclass, not '%s'"
,
Py_TYPE
(
category
)
->
tp_name
);
return
NULL
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment