Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a779ea

Browse files
committed
Merged revisions 74524,74556 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r74524 | gregory.p.smith | 2009-08-20 04:39:38 -0500 (Thu, 20 Aug 2009) | 2 lines Add weakref support to the thread.lock type. ........ r74556 | kristjan.jonsson | 2009-08-27 17:20:21 -0500 (Thu, 27 Aug 2009) | 2 lines issue 6275 Add an "exc_value" attribute to the _AssertRaisesContext context manager in the unittest package. This allows further tests on the exception that was raised after the context manager exits. ........
1 parent 2e30440 commit 9a779ea

5 files changed

Lines changed: 30 additions & 2 deletions

File tree

Doc/library/unittest.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,10 @@ Test cases
891891
with self.failUnlessRaises(some_error_class):
892892
do_something()
893893

894+
The context manager will store the caught exception object in its
895+
:attr:`exc_value` attribute. This can be useful if the intention
896+
is to perform additional checks on the exception raised.
897+
894898
.. versionchanged:: 3.1
895899
Added the ability to use :meth:`assertRaises` as a context manager.
896900

Doc/library/weakref.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ instances, functions written in Python (but not in C), instance methods, sets,
6161
frozensets, file objects, :term:`generator`\s, type objects, sockets, arrays,
6262
deques, and regular expression pattern objects.
6363

64+
.. versionchanged:: 2.7
65+
Added support for thread.lock and threading.Lock.
66+
6467
Several built-in types such as :class:`list` and :class:`dict` do not directly
6568
support weak references but can add support through subclassing::
6669

Lib/test/test_unittest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,6 +2834,21 @@ def Stub():
28342834
self.assertRaisesRegexp, Exception,
28352835
re.compile('^Expected$'), Stub)
28362836

2837+
def testAssertRaisesExcValue(self):
2838+
class ExceptionMock(Exception):
2839+
pass
2840+
2841+
def Stub(foo):
2842+
raise ExceptionMock(foo)
2843+
v = "particular value"
2844+
2845+
ctx = self.assertRaises(ExceptionMock)
2846+
with ctx:
2847+
Stub(v)
2848+
e = ctx.exc_value
2849+
self.assertTrue(isinstance(e, ExceptionMock))
2850+
self.assertEqual(e.args[0], v)
2851+
28372852
def testSynonymAssertMethodNames(self):
28382853
"""Test undocumented method name synonyms.
28392854

Lib/unittest/case.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def __exit__(self, exc_type, exc_value, tb):
116116
if not issubclass(exc_type, self.expected):
117117
# let unexpected exceptions pass through
118118
return False
119+
self.exc_value = exc_value #store for later retrieval
119120
if self.expected_regex is None:
120121
return True
121122

Modules/_threadmodule.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* Interface to Sjoerd's portable C thread library */
44

55
#include "Python.h"
6+
#include "structmember.h" /* offsetof */
67

78
#ifndef WITH_THREAD
89
#error "Error! The rest of Python is not compiled with thread support."
@@ -20,12 +21,15 @@ static PyObject *ThreadError;
2021
typedef struct {
2122
PyObject_HEAD
2223
PyThread_type_lock lock_lock;
24+
PyObject *in_weakreflist;
2325
} lockobject;
2426

2527
static void
2628
lock_dealloc(lockobject *self)
2729
{
2830
assert(self->lock_lock);
31+
if (self->in_weakreflist != NULL)
32+
PyObject_ClearWeakRefs((PyObject *) self);
2933
/* Unlock the lock so it's safe to free it */
3034
PyThread_acquire_lock(self->lock_lock, 0);
3135
PyThread_release_lock(self->lock_lock);
@@ -140,12 +144,12 @@ static PyTypeObject Locktype = {
140144
0, /*tp_getattro*/
141145
0, /*tp_setattro*/
142146
0, /*tp_as_buffer*/
143-
Py_TPFLAGS_DEFAULT, /*tp_flags*/
147+
Py_TPFLAGS_DEFAULT, /*tp_flags*/
144148
0, /*tp_doc*/
145149
0, /*tp_traverse*/
146150
0, /*tp_clear*/
147151
0, /*tp_richcompare*/
148-
0, /*tp_weaklistoffset*/
152+
offsetof(lockobject, in_weakreflist), /*tp_weaklistoffset*/
149153
0, /*tp_iter*/
150154
0, /*tp_iternext*/
151155
lock_methods, /*tp_methods*/
@@ -159,6 +163,7 @@ newlockobject(void)
159163
if (self == NULL)
160164
return NULL;
161165
self->lock_lock = PyThread_allocate_lock();
166+
self->in_weakreflist = NULL;
162167
if (self->lock_lock == NULL) {
163168
PyObject_Del(self);
164169
self = NULL;

0 commit comments

Comments
 (0)