source: rtems-tools/rtemstoolkit/reraise.py @ b0b9366

5
Last change on this file since b0b9366 was fa81491, checked in by Chris Johns <chrisj@…>, on 09/21/17 at 07:45:39

Move the reraise logic into the tool kit.

  • Property mode set to 100644
File size: 4.1 KB
Line 
1#
2# RTEMS Tools Project (http://www.rtems.org/)
3# Copyright 2013-2017 Chris Johns (chrisj@rtems.org)
4# All rights reserved.
5#
6# This file is part of the RTEMS Tools package in 'rtems-tools'.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions are met:
10#
11# 1. Redistributions of source code must retain the above copyright notice,
12# this list of conditions and the following disclaimer.
13#
14# 2. Redistributions in binary form must reproduce the above copyright notice,
15# this list of conditions and the following disclaimer in the documentation
16# and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30
31from __future__ import print_function
32
33import sys
34
35#
36# The following fragment is taken from https://bitbucket.org/gutworth/six
37# to raise an exception. The python2 code cause a syntax error with python3.
38#
39# Copyright (c) 2010-2016 Benjamin Peterson
40#
41# Permission is hereby granted, free of charge, to any person obtaining a copy
42# of this software and associated documentation files (the "Software"), to deal
43# in the Software without restriction, including without limitation the rights
44# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
45# copies of the Software, and to permit persons to whom the Software is
46# furnished to do so, subject to the following conditions:
47#
48# The above copyright notice and this permission notice shall be included in all
49# copies or substantial portions of the Software.
50#
51# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
52# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
53# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
54# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
55# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
56# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
57# SOFTWARE.
58#
59# Taken from six.
60#
61if sys.version_info[0] == 3:
62    def reraise(tp, value, tb = None):
63        raise value.with_traceback(tb)
64else:
65    def exec_(_code_, _globs_ = None, _locs_ = None):
66        if _globs_ is None:
67            frame = sys._getframe(1)
68            _globs_ = frame.f_globals
69            if _locs_ is None:
70                _locs_ = frame.f_locals
71            del frame
72        elif _locs_ is None:
73            _locs_ = _globs_
74        exec("""exec _code_ in _globs_, _locs_""")
75
76    exec_("""def reraise(tp, value, tb = None):
77    raise tp, value, tb
78""")
79
80if __name__ == "__main__":
81    try:
82        import threading
83        import time
84        result = None
85        finished = False
86        def _thread():
87            global finished
88            global result
89            try:
90                raise ValueError('raised inside a thread, reaise is working')
91            except:
92                result = sys.exc_info()
93            finished = True
94        thread = threading.Thread(target = _thread, name = 'test')
95        thread.start()
96        while not finished:
97            time.sleep(0.05)
98        if result is not None:
99            reraise(*result)
100        else:
101            print('error: no exception raised and caught')
102    except ValueError as ve:
103        print('exception caught: %s' % (str(ve)))
104    except KeyboardInterrupt:
105        print('abort: user terminated')
106    except:
107        print('unknown exception caught')
Note: See TracBrowser for help on using the repository browser.