图片解析应用
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.

55 lines
1.4 KiB

  1. from sympy.printing.codeprinter import CodePrinter
  2. from sympy.core import symbols
  3. from sympy.core.symbol import Dummy
  4. from sympy.testing.pytest import raises
  5. def setup_test_printer(**kwargs):
  6. p = CodePrinter(settings=kwargs)
  7. p._not_supported = set()
  8. p._number_symbols = set()
  9. return p
  10. def test_print_Dummy():
  11. d = Dummy('d')
  12. p = setup_test_printer()
  13. assert p._print_Dummy(d) == "d_%i" % d.dummy_index
  14. def test_print_Symbol():
  15. x, y = symbols('x, if')
  16. p = setup_test_printer()
  17. assert p._print(x) == 'x'
  18. assert p._print(y) == 'if'
  19. p.reserved_words.update(['if'])
  20. assert p._print(y) == 'if_'
  21. p = setup_test_printer(error_on_reserved=True)
  22. p.reserved_words.update(['if'])
  23. with raises(ValueError):
  24. p._print(y)
  25. p = setup_test_printer(reserved_word_suffix='_He_Man')
  26. p.reserved_words.update(['if'])
  27. assert p._print(y) == 'if_He_Man'
  28. def test_issue_15791():
  29. class CrashingCodePrinter(CodePrinter):
  30. def emptyPrinter(self, obj):
  31. raise NotImplementedError
  32. from sympy.matrices import (
  33. MutableSparseMatrix,
  34. ImmutableSparseMatrix,
  35. )
  36. c = CrashingCodePrinter()
  37. # these should not silently succeed
  38. with raises(NotImplementedError):
  39. c.doprint(ImmutableSparseMatrix(2, 2, {}))
  40. with raises(NotImplementedError):
  41. c.doprint(MutableSparseMatrix(2, 2, {}))