CAT-SOOP is a flexible, programmable learning management system based on the Python programming language. https://catsoop.mit.edu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

87 lines
2.5 KiB

  1. # This file is part of CAT-SOOP
  2. # Copyright (c) 2011-2020 by The CAT-SOOP Developers <catsoop-dev@mit.edu>
  3. #
  4. # This program is free software: you can redistribute it and/or modify it under
  5. # the terms of the GNU Affero General Public License as published by the Free
  6. # Software Foundation, either version 3 of the License, or (at your option) any
  7. # later version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  12. # details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import sys
  17. import time
  18. class OpcodeLimitReached(Exception):
  19. pass
  20. RESULT_AS_STRING = %(result_as_string)r
  21. OPCODE_TRACING_ENABLED = sys.version_info > (3, 7) and %(enable_opcode_count)s
  22. if OPCODE_TRACING_ENABLED:
  23. def trace_closure(limit=float("inf")):
  24. executed_opcodes = 0
  25. limit_reached = False
  26. def tracer(frame, event, arg):
  27. nonlocal executed_opcodes, limit_reached
  28. frame.f_trace_opcodes = True
  29. if event == "opcode":
  30. executed_opcodes += 1
  31. if executed_opcodes >= limit:
  32. limit_reached = True
  33. raise OpcodeLimitReached
  34. return tracer
  35. def get():
  36. return executed_opcodes
  37. def killed():
  38. return limit_reached
  39. names = {"tracer": tracer, "get": get, "killed": killed}
  40. return lambda n: names[n]
  41. inf = float("inf")
  42. tracer = trace_closure(limit=%(opcode_limit)r)
  43. sys.settrace(tracer("tracer"))
  44. class NoAnswerGiven:
  45. pass
  46. results = {}
  47. start_time = time.time()
  48. try:
  49. import %(test_module)s as test_module
  50. ans = getattr(test_module, "_catsoop_answer", NoAnswerGiven)
  51. if ans is not NoAnswerGiven: # we got a result back
  52. if RESULT_AS_STRING:
  53. ans = repr(ans)
  54. results["result"] = ans
  55. results["duration"] = time.time() - start_time
  56. results["complete"] = True
  57. except OpcodeLimitReached:
  58. pass
  59. except Exception as e:
  60. results["exception_type"] = e.__class__.__name__
  61. results["exception_args"] = e.args
  62. raise
  63. finally:
  64. if OPCODE_TRACING_ENABLED:
  65. results["opcode_count"] = tracer("get")()
  66. results["opcode_limit_reached"] = tracer("killed")()
  67. print("---")
  68. print(results)