Coverage for scripts/lean_smoke/exception_scope.py: 100%

39 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 12:55 +0000

1"""Does `except Exception` catch a Python error behind Lean's star import? (V6.5.0) One SMOKE_JSON line, then the run ends.""" 

2 

3import builtins 

4import json 

5 

6from AlgorithmImports import * # noqa: F403 

7 

8 

9class ExceptionScopeSmoke(QCAlgorithm): # noqa: F405 

10 def initialize(self): 

11 self.set_start_date(2014, 6, 6) 

12 self.set_end_date(2014, 6, 6) 

13 self.set_cash(100000) 

14 report = { 

15 "kind": "exception_scope", 

16 "name_in_module": repr(Exception), 

17 "is_builtin": Exception is builtins.Exception, 

18 "mro": [repr(item) for item in getattr(Exception, "__mro__", [])], 

19 } 

20 try: 

21 try: 

22 None.Symbol # noqa: B018 # raises AttributeError on purpose 

23 except Exception: # the star import's Exception, as main.py has it 

24 report["star_exception_catches_python_error"] = True 

25 except BaseException as error: 

26 report["star_exception_catches_python_error"] = False 

27 report["escaped_as"] = type(error).__name__ 

28 try: 

29 try: 

30 self.add_equity("AAPL", "not a resolution") # a .NET binding error 

31 except builtins.Exception as error: 

32 report["builtin_exception_catches_clr_error"] = True 

33 report["clr_error_type"] = type(error).__name__ 

34 except BaseException as error: 

35 report["builtin_exception_catches_clr_error"] = False 

36 report["clr_escaped_as"] = type(error).__name__ 

37 try: 

38 from System import ArgumentException 

39 

40 try: 

41 raise ArgumentException("from .NET") 

42 except builtins.Exception as error: 

43 report["builtin_exception_catches_raised_clr"] = True 

44 report["raised_clr_type"] = type(error).__name__ 

45 except BaseException as error: 

46 report["builtin_exception_catches_raised_clr"] = False 

47 report["raised_clr_escaped_as"] = type(error).__name__ 

48 self.debug("SMOKE_JSON " + json.dumps(report, default=str)) 

49 

50 def on_data(self, slice): 

51 pass