Repository: sagemath/sage

Issue:
ascii_art fails on empty chain complex
### Steps To Reproduce

1. I'm on Sage 10.2 on MacOS 12.4, working in Jupyter via sage --notebook.
2. Creating the trivial chain complex `E=ChainComplex(base_ring=ZZ)` works, but `ascii_art(E)` raises the index error listed below.



### Expected Behavior

It should return either `0` or an empty string.

### Actual Behavior

```---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In [81], line 1
----> 1 ascii_art(E)

File /private/var/tmp/sage-10.2-current/local/var/lib/sage/venv-python3.11.1/lib/python3.11/site-packages/sage/typeset/ascii_art.py:288, in ascii_art(*obj, **kwds)
    286     raise ValueError('unknown keyword arguments: {0}'.format(list(kwds)))
    287 if len(obj) == 1:
--> 288     return _ascii_art_factory.build(obj[0], baseline=baseline)
    289 if not isinstance(separator, AsciiArt):
    290     separator = _ascii_art_factory.build(separator, baseline=sep_baseline)

File /private/var/tmp/sage-10.2-current/local/var/lib/sage/venv-python3.11.1/lib/python3.11/site-packages/sage/typeset/character_art_factory.py:123, in CharacterArtFactory.build(self, obj, baseline)
    121     return obj
    122 if isinstance(obj, SageObject):
--> 123     return self.build_from_magic_method(obj, baseline)
    124 if baseline is None:
    125     baseline = 0

File /private/var/tmp/sage-10.2-current/local/var/lib/sage/venv-python3.11.1/lib/python3.11/site-packages/sage/typeset/character_art_factory.py:171, in CharacterArtFactory.build_from_magic_method(self, obj, baseline)
    154 """
    155 Return the character art object created by the object's magic method
    156 
   (...)
    168     <class 'sage.typeset.ascii_art.AsciiArt'>
    169 """
    170 magic_method = getattr(obj, self.magic_method_name)
--> 171 ret = magic_method()
    172 if baseline is not None:
    173     ret._baseline = baseline

File /private/var/tmp/sage-10.2-current/local/var/lib/sage/venv-python3.11.1/lib/python3.11/site-packages/sage/homology/chain_complex.py:1806, in ChainComplex_class._ascii_art_(self)
   1804         result_ordered += arrow_art(n) + module_art(n)
   1805     result = [result_ordered] + result
-> 1806 concatenated = result[0]
   1807 for r in result[1:]:
   1808     concatenated += AsciiArt([' ... ']) + r

IndexError: list index out of range```

### Additional Information

_No response_

### Environment

```markdown
- **MacOS 10.2**:
- **Sage 10.2**:
```


### Checklist

- [X] I have searched the existing issues for a bug report that matches the one I want to file, without success.
- [X] I have read the documentation and troubleshoot guide


Discussion:
looks like 
`  1807 for r in result[1:]:` assumes `result` to be of length at most 2, but it's not the case here 
same error in the last beta

Resolving patch:
diff --git a/src/sage/homology/chain_complex.py b/src/sage/homology/chain_complex.py
--- a/src/sage/homology/chain_complex.py
+++ b/src/sage/homology/chain_complex.py
@@ -418,6 +418,15 @@ def _ascii_art_(self):
                d_2       d_1       d_0  [1]  d_-1
             0 <---- [0] <---- [4] <---- [2] <----- 0
                               [5]       [3]
+
+        TESTS:
+
+        check that :issue:`37678` is fixed::
+
+            sage: C = ChainComplex(base_ring=ZZ)
+            sage: ascii_art(C())
+            0
+
         """
         from sage.typeset.ascii_art import AsciiArt
 
@@ -444,6 +453,8 @@ def vector_art(d):
             for n in ordered:
                 result_ordered += arrow_art(n) + vector_art(n)
             result = [result_ordered] + result
+        if len(result) == 0:
+            return AsciiArt(['0'])
         concatenated = result[0]
         for r in result[1:]:
             concatenated += AsciiArt([' ... ']) + r
@@ -465,6 +476,12 @@ def _unicode_art_(self):
                                         ⎛1⎞
                d_2       d_1  ⎛4⎞  d_0  ⎜2⎟  d_-1
             0 <──── (0) <──── ⎝5⎠ <──── ⎝3⎠ <───── 0
+            sage: unicode_art(C())
+                                        ⎛0⎞
+               d_2       d_1  ⎛0⎞  d_0  ⎜0⎟  d_-1
+            0 <──── (0) <──── ⎝0⎠ <──── ⎝0⎠ <───── 0
+            sage: unicode_art(ChainComplex())
+            0
         """
         from sage.typeset.unicode_art import UnicodeArt
 
@@ -492,6 +509,8 @@ def vector_art(d):
             for n in ordered:
                 result_ordered += arrow_art(n) + vector_art(n)
             result = [result_ordered] + result
+        if len(result) == 0:
+            return UnicodeArt([u'0'])
         concatenated = result[0]
         for r in result[1:]:
             concatenated += UnicodeArt([u' ... ']) + r
@@ -1775,6 +1794,8 @@ def _ascii_art_(self):
             sage: ascii_art(D)
                         [1]                             [1]       [0]       [1]
              0 <-- C_7 <---- C_6 <-- 0  ...  0 <-- C_3 <---- C_2 <---- C_1 <---- C_0 <-- 0
+             sage: ascii_art(ChainComplex(base_ring=ZZ))
+             0
         """
         from sage.typeset.ascii_art import AsciiArt
 
@@ -1803,6 +1824,8 @@ def module_art(n):
             for n in ordered:
                 result_ordered += arrow_art(n) + module_art(n)
             result = [result_ordered] + result
+        if len(result) == 0:
+            return AsciiArt(['0'])
         concatenated = result[0]
         for r in result[1:]:
             concatenated += AsciiArt([' ... ']) + r
@@ -1828,6 +1851,14 @@ def _unicode_art_(self):
             sage: unicode_art(D)
                       (1)                           (1)     (0)     (1)
             0 <── C_7 <── C_6 <── 0  ...  0 <── C_3 <── C_2 <── C_1 <── C_0 <── 0
+
+        TESTS:
+
+        check that :issue:`37678` is fixed::
+
+            sage: C = ChainComplex(base_ring=ZZ)
+            sage: unicode_art(C)
+            0
         """
         from sage.typeset.unicode_art import UnicodeArt
 
@@ -1856,6 +1887,8 @@ def module_art(n):
             for n in ordered:
                 result_ordered += arrow_art(n) + module_art(n)
             result = [result_ordered] + result
+        if len(result) == 0:
+            return UnicodeArt([u'0'])
         concatenated = result[0]
         for r in result[1:]:
             concatenated += UnicodeArt([u' ... ']) + r


Test patch: