github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/lib/sys_test.py (about) 1 # Copyright 2016 Google Inc. All Rights Reserved. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 15 # pylint: disable=bare-except 16 17 import sys 18 import types 19 20 import weetest 21 22 23 def TestArgv(): 24 assert sys.argv 25 26 27 def TestMaxInt(): 28 assert sys.maxint > 2000000000 29 30 31 def TestSysModules(): 32 assert sys.modules['sys'] is not None 33 34 35 def TestExcClear(): 36 try: 37 raise RuntimeError 38 except: 39 assert all(sys.exc_info()), sys.exc_info() 40 sys.exc_clear() 41 assert not any(sys.exc_info()) 42 else: 43 assert False 44 45 46 def TestExcInfoNoException(): 47 assert sys.exc_info() == (None, None, None) 48 49 50 def TestExcInfoWithException(): 51 try: 52 raise RuntimeError 53 except: 54 t, e, tb = sys.exc_info() 55 else: 56 assert False 57 assert t is RuntimeError 58 assert isinstance(e, t) 59 assert isinstance(tb, types.TracebackType) 60 61 62 def TestExitEmpty(): 63 try: 64 sys.exit() 65 except SystemExit as e: 66 assert e.code == None, e.code # pylint: disable=g-equals-none 67 except: 68 assert False 69 70 71 def TestExitCode(): 72 try: 73 sys.exit(42) 74 except SystemExit as e: 75 assert e.code == 42, e.code 76 except: 77 assert False 78 79 80 def TestExitInvalidArgs(): 81 try: 82 sys.exit(1, 2, 3) 83 except TypeError as e: 84 assert str(e) == 'exit() takes 1 arguments (3 given)', str(e) 85 except: 86 assert False 87 88 89 def TestGetFrame(): 90 try: 91 sys._getframe(42, 42) 92 except TypeError: 93 pass 94 else: 95 assert False 96 try: 97 sys._getframe(2000000000) 98 except ValueError: 99 pass 100 else: 101 assert False 102 assert sys._getframe().f_code.co_name == '_getframe' 103 assert sys._getframe(1).f_code.co_name == 'TestGetFrame' 104 105 106 if __name__ == '__main__': 107 # This call will incidentally test sys.exit(). 108 weetest.RunTests()