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