DebugWriter.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from __future__ import absolute_import
  2. import os
  3. import sys
  4. import errno
  5. try:
  6. from lxml import etree
  7. have_lxml = True
  8. except ImportError:
  9. have_lxml = False
  10. try:
  11. from xml.etree import cElementTree as etree
  12. except ImportError:
  13. try:
  14. from xml.etree import ElementTree as etree
  15. except ImportError:
  16. etree = None
  17. from ..Compiler import Errors
  18. class CythonDebugWriter(object):
  19. """
  20. Class to output debugging information for cygdb
  21. It writes debug information to cython_debug/cython_debug_info_<modulename>
  22. in the build directory.
  23. """
  24. def __init__(self, output_dir):
  25. if etree is None:
  26. raise Errors.NoElementTreeInstalledException()
  27. self.output_dir = os.path.join(output_dir or os.curdir, 'cython_debug')
  28. self.tb = etree.TreeBuilder()
  29. # set by Cython.Compiler.ParseTreeTransforms.DebugTransform
  30. self.module_name = None
  31. self.start('cython_debug', attrs=dict(version='1.0'))
  32. def start(self, name, attrs=None):
  33. self.tb.start(name, attrs or {})
  34. def end(self, name):
  35. self.tb.end(name)
  36. def add_entry(self, name, **attrs):
  37. self.tb.start(name, attrs)
  38. self.tb.end(name)
  39. def serialize(self):
  40. self.tb.end('Module')
  41. self.tb.end('cython_debug')
  42. xml_root_element = self.tb.close()
  43. try:
  44. os.makedirs(self.output_dir)
  45. except OSError as e:
  46. if e.errno != errno.EEXIST:
  47. raise
  48. et = etree.ElementTree(xml_root_element)
  49. kw = {}
  50. if have_lxml:
  51. kw['pretty_print'] = True
  52. fn = "cython_debug_info_" + self.module_name
  53. et.write(os.path.join(self.output_dir, fn), encoding="UTF-8", **kw)
  54. interpreter_path = os.path.join(self.output_dir, 'interpreter')
  55. with open(interpreter_path, 'w') as f:
  56. f.write(sys.executable)