Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_signal.py

    r2 r391  
    11import unittest
    22from test import test_support
    3 from contextlib import closing, nested
     3from contextlib import closing
    44import gc
    55import pickle
     
    1010import sys, os, time, errno
    1111
    12 if sys.platform[:3] in ('win', 'os2') or sys.platform == 'riscos':
    13     raise test_support.TestSkipped("Can't test signal on %s" % \
    14                                    sys.platform)
     12if sys.platform in ('os2', 'riscos'):
     13    raise unittest.SkipTest("Can't test signal on %s" % sys.platform)
    1514
    1615
     
    3837
    3938
     39@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
    4040class InterProcessSignalTests(unittest.TestCase):
    4141    MAX_DURATION = 20   # Entire test should last at most 20 sec.
     
    110110            self.wait(child)
    111111            time.sleep(1)  # Give the signal time to be delivered.
    112             self.fail('HandlerBCalled exception not thrown')
     112            self.fail('HandlerBCalled exception not raised')
    113113        except HandlerBCalled:
    114114            self.assertTrue(self.b_called)
     
    140140                      " didn't arrive after another second.")
    141141
     142    # Issue 3864. Unknown if this affects earlier versions of freebsd also.
     143    @unittest.skipIf(sys.platform=='freebsd6',
     144        'inter process signals not reliable (do not mix well with threading) '
     145        'on freebsd6')
    142146    def test_main(self):
    143147        # This function spawns a child process to insulate the main
     
    145149        # communicates with that child process over a pipe and
    146150        # re-raises information about any exceptions the child
    147         # throws. The real work happens in self.run_test().
     151        # raises. The real work happens in self.run_test().
    148152        os_done_r, os_done_w = os.pipe()
    149         with nested(closing(os.fdopen(os_done_r)),
    150                     closing(os.fdopen(os_done_w, 'w'))) as (done_r, done_w):
     153        with closing(os.fdopen(os_done_r)) as done_r, \
     154             closing(os.fdopen(os_done_w, 'w')) as done_w:
    151155            child = os.fork()
    152156            if child == 0:
     
    183187
    184188
     189@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
    185190class BasicSignalTests(unittest.TestCase):
    186191    def trivial_signal_handler(self, *args):
     
    199204    def test_getsignal(self):
    200205        hup = signal.signal(signal.SIGHUP, self.trivial_signal_handler)
    201         self.assertEquals(signal.getsignal(signal.SIGHUP),
    202                           self.trivial_signal_handler)
     206        self.assertEqual(signal.getsignal(signal.SIGHUP),
     207                         self.trivial_signal_handler)
    203208        signal.signal(signal.SIGHUP, hup)
    204         self.assertEquals(signal.getsignal(signal.SIGHUP), hup)
    205 
    206 
     209        self.assertEqual(signal.getsignal(signal.SIGHUP), hup)
     210
     211
     212@unittest.skipUnless(sys.platform == "win32", "Windows specific")
     213class WindowsSignalTests(unittest.TestCase):
     214    def test_issue9324(self):
     215        # Updated for issue #10003, adding SIGBREAK
     216        handler = lambda x, y: None
     217        for sig in (signal.SIGABRT, signal.SIGBREAK, signal.SIGFPE,
     218                    signal.SIGILL, signal.SIGINT, signal.SIGSEGV,
     219                    signal.SIGTERM):
     220            # Set and then reset a handler for signals that work on windows
     221            signal.signal(sig, signal.signal(sig, handler))
     222
     223        with self.assertRaises(ValueError):
     224            signal.signal(-1, handler)
     225
     226        with self.assertRaises(ValueError):
     227            signal.signal(7, handler)
     228
     229
     230class WakeupFDTests(unittest.TestCase):
     231
     232    def test_invalid_fd(self):
     233        fd = test_support.make_bad_fd()
     234        self.assertRaises(ValueError, signal.set_wakeup_fd, fd)
     235
     236
     237@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
    207238class WakeupSignalTests(unittest.TestCase):
    208239    TIMEOUT_FULL = 10
     
    218249        time.sleep(self.TIMEOUT_FULL)
    219250        mid_time = time.time()
    220         self.assert_(mid_time - before_time < self.TIMEOUT_HALF)
     251        self.assertTrue(mid_time - before_time < self.TIMEOUT_HALF)
    221252        select.select([self.read], [], [], self.TIMEOUT_FULL)
    222253        after_time = time.time()
    223         self.assert_(after_time - mid_time < self.TIMEOUT_HALF)
     254        self.assertTrue(after_time - mid_time < self.TIMEOUT_HALF)
    224255
    225256    def test_wakeup_fd_during(self):
     
    232263            [self.read], [], [], self.TIMEOUT_FULL)
    233264        after_time = time.time()
    234         self.assert_(after_time - before_time < self.TIMEOUT_HALF)
     265        self.assertTrue(after_time - before_time < self.TIMEOUT_HALF)
    235266
    236267    def setUp(self):
     
    250281        signal.signal(signal.SIGALRM, self.alrm)
    251282
     283@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
    252284class SiginterruptTest(unittest.TestCase):
    253     signum = signal.SIGUSR1
    254     def readpipe_interrupted(self, cb):
     285
     286    def setUp(self):
     287        """Install a no-op signal handler that can be set to allow
     288        interrupts or not, and arrange for the original signal handler to be
     289        re-installed when the test is finished.
     290        """
     291        self.signum = signal.SIGUSR1
     292        oldhandler = signal.signal(self.signum, lambda x,y: None)
     293        self.addCleanup(signal.signal, self.signum, oldhandler)
     294
     295    def readpipe_interrupted(self):
     296        """Perform a read during which a signal will arrive.  Return True if the
     297        read is interrupted by the signal and raises an exception.  Return False
     298        if it returns normally.
     299        """
     300        # Create a pipe that can be used for the read.  Also clean it up
     301        # when the test is over, since nothing else will (but see below for
     302        # the write end).
    255303        r, w = os.pipe()
     304        self.addCleanup(os.close, r)
     305
     306        # Create another process which can send a signal to this one to try
     307        # to interrupt the read.
    256308        ppid = os.getpid()
    257309        pid = os.fork()
    258310
    259         oldhandler = signal.signal(self.signum, lambda x,y: None)
    260         cb()
    261         if pid==0:
    262             # child code: sleep, kill, sleep. and then exit,
    263             # which closes the pipe from which the parent process reads
     311        if pid == 0:
     312            # Child code: sleep to give the parent enough time to enter the
     313            # read() call (there's a race here, but it's really tricky to
     314            # eliminate it); then signal the parent process.  Also, sleep
     315            # again to make it likely that the signal is delivered to the
     316            # parent process before the child exits.  If the child exits
     317            # first, the write end of the pipe will be closed and the test
     318            # is invalid.
    264319            try:
    265320                time.sleep(0.2)
     
    267322                time.sleep(0.2)
    268323            finally:
     324                # No matter what, just exit as fast as possible now.
    269325                exit_subprocess()
    270 
    271         try:
     326        else:
     327            # Parent code.
     328            # Make sure the child is eventually reaped, else it'll be a
     329            # zombie for the rest of the test suite run.
     330            self.addCleanup(os.waitpid, pid, 0)
     331
     332            # Close the write end of the pipe.  The child has a copy, so
     333            # it's not really closed until the child exits.  We need it to
     334            # close when the child exits so that in the non-interrupt case
     335            # the read eventually completes, otherwise we could just close
     336            # it *after* the test.
    272337            os.close(w)
    273338
     339            # Try the read and report whether it is interrupted or not to
     340            # the caller.
    274341            try:
    275                 d=os.read(r, 1)
     342                d = os.read(r, 1)
    276343                return False
    277344            except OSError, err:
     
    279346                    raise
    280347                return True
    281         finally:
    282             signal.signal(self.signum, oldhandler)
    283             os.waitpid(pid, 0)
    284348
    285349    def test_without_siginterrupt(self):
    286         i=self.readpipe_interrupted(lambda: None)
    287         self.assertEquals(i, True)
     350        """If a signal handler is installed and siginterrupt is not called
     351        at all, when that signal arrives, it interrupts a syscall that's in
     352        progress.
     353        """
     354        i = self.readpipe_interrupted()
     355        self.assertTrue(i)
     356        # Arrival of the signal shouldn't have changed anything.
     357        i = self.readpipe_interrupted()
     358        self.assertTrue(i)
    288359
    289360    def test_siginterrupt_on(self):
    290         i=self.readpipe_interrupted(lambda: signal.siginterrupt(self.signum, 1))
    291         self.assertEquals(i, True)
     361        """If a signal handler is installed and siginterrupt is called with
     362        a true value for the second argument, when that signal arrives, it
     363        interrupts a syscall that's in progress.
     364        """
     365        signal.siginterrupt(self.signum, 1)
     366        i = self.readpipe_interrupted()
     367        self.assertTrue(i)
     368        # Arrival of the signal shouldn't have changed anything.
     369        i = self.readpipe_interrupted()
     370        self.assertTrue(i)
    292371
    293372    def test_siginterrupt_off(self):
    294         i=self.readpipe_interrupted(lambda: signal.siginterrupt(self.signum, 0))
    295         self.assertEquals(i, False)
    296 
     373        """If a signal handler is installed and siginterrupt is called with
     374        a false value for the second argument, when that signal arrives, it
     375        does not interrupt a syscall that's in progress.
     376        """
     377        signal.siginterrupt(self.signum, 0)
     378        i = self.readpipe_interrupted()
     379        self.assertFalse(i)
     380        # Arrival of the signal shouldn't have changed anything.
     381        i = self.readpipe_interrupted()
     382        self.assertFalse(i)
     383
     384
     385@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
    297386class ItimerTest(unittest.TestCase):
    298387    def setUp(self):
     
    356445        self.assertEqual(self.hndl_called, True)
    357446
     447    # Issue 3864. Unknown if this affects earlier versions of freebsd also.
     448    @unittest.skipIf(sys.platform in ('freebsd6', 'netbsd5'),
     449        'itimer not reliable (does not mix well with threading) on some BSDs.')
    358450    def test_itimer_virtual(self):
    359451        self.itimer = signal.ITIMER_VIRTUAL
     
    362454
    363455        start_time = time.time()
    364         while time.time() - start_time < 5.0:
     456        while time.time() - start_time < 60.0:
    365457            # use up some virtual time by doing real work
    366458            _ = pow(12345, 67890, 10000019)
    367459            if signal.getitimer(self.itimer) == (0.0, 0.0):
    368460                break # sig_vtalrm handler stopped this itimer
    369         else:
    370             self.fail('timeout waiting for sig_vtalrm signal; '
    371                       'signal.getitimer(self.itimer) gives: %s' %
    372                        (signal.getitimer(self.itimer),))
     461        else: # Issue 8424
     462            self.skipTest("timeout: likely cause: machine too slow or load too "
     463                          "high")
    373464
    374465        # virtual itimer should be (0.0, 0.0) now
    375         self.assertEquals(signal.getitimer(self.itimer), (0.0, 0.0))
     466        self.assertEqual(signal.getitimer(self.itimer), (0.0, 0.0))
    376467        # and the handler should have been called
    377         self.assertEquals(self.hndl_called, True)
    378 
     468        self.assertEqual(self.hndl_called, True)
     469
     470    # Issue 3864. Unknown if this affects earlier versions of freebsd also.
     471    @unittest.skipIf(sys.platform=='freebsd6',
     472        'itimer not reliable (does not mix well with threading) on freebsd6')
    379473    def test_itimer_prof(self):
    380474        self.itimer = signal.ITIMER_PROF
     
    383477
    384478        start_time = time.time()
    385         while time.time() - start_time < 5.0:
     479        while time.time() - start_time < 60.0:
    386480            # do some work
    387481            _ = pow(12345, 67890, 10000019)
    388482            if signal.getitimer(self.itimer) == (0.0, 0.0):
    389483                break # sig_prof handler stopped this itimer
    390         else:
    391             self.fail('timeout waiting for sig_prof signal')
     484        else: # Issue 8424
     485            self.skipTest("timeout: likely cause: machine too slow or load too "
     486                          "high")
    392487
    393488        # profiling itimer should be (0.0, 0.0) now
    394         self.assertEquals(signal.getitimer(self.itimer), (0.0, 0.0))
     489        self.assertEqual(signal.getitimer(self.itimer), (0.0, 0.0))
    395490        # and the handler should have been called
    396491        self.assertEqual(self.hndl_called, True)
     
    398493def test_main():
    399494    test_support.run_unittest(BasicSignalTests, InterProcessSignalTests,
    400         WakeupSignalTests, SiginterruptTest, ItimerTest)
     495                              WakeupFDTests, WakeupSignalTests,
     496                              SiginterruptTest, ItimerTest,
     497                              WindowsSignalTests)
    401498
    402499
Note: See TracChangeset for help on using the changeset viewer.