stacktrace.py 838 B

1234567891011121314151617181920212223242526272829303132
  1. def get_context(lineno, context_line, pre_context=None, post_context=None):
  2. if lineno is None:
  3. return []
  4. if context_line is None and not (pre_context or post_context):
  5. return []
  6. lineno = int(lineno)
  7. context = []
  8. start_lineno = lineno - len(pre_context or [])
  9. if pre_context:
  10. start_lineno = lineno - len(pre_context)
  11. at_lineno = start_lineno
  12. for line in pre_context:
  13. context.append([at_lineno, line])
  14. at_lineno += 1
  15. else:
  16. start_lineno = lineno
  17. at_lineno = lineno
  18. if start_lineno < 0:
  19. start_lineno = 0
  20. context.append([at_lineno, context_line])
  21. at_lineno += 1
  22. if post_context:
  23. for line in post_context:
  24. context.append([at_lineno, line])
  25. at_lineno += 1
  26. return context