gh-35307: Use StringIO to format polynomials
This patch modifies the repr()/str() method of polynomials to use a
StringIO buffer instead of `s += ...` to build the result string. It is
known that repeatedly calling `+=` has quadratic complexity for large
Python strings.
Currently calling `str()` on a high degree polynomial takes a very long
amount of time and it may happen accidentally (a script was stuck during
2 hours trying to format a degree 300000 polynomial for an exception
message).
```
sage: p = random_prime(2**96)
sage: K = GF(p**24, 'a')
sage: pol = K["x"].random_element(30000)
# Before patch
sage: %time _ = str(pol)
CPU times: user 2min 22s, sys: 1min 43s, total: 4min 5s
Wall time: 4min 5s
# After patch
sage: %time _ = str(pol)
CPU times: user 692 ms, sys: 21 ms, total: 713 ms
Wall time: 713 ms
```
### 📝 Checklist
<!-- Put an `x` in all the boxes that apply. -->
<!-- If your change requires a documentation PR, please link it
appropriately -->
<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
- [x] I have made sure that the title is self-explanatory and the
description concisely explains the PR.
- [ ] I have linked an issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation accordingly.
### ⌛ Dependencies
None.
URL: https://github.com/sagemath/sage/pull/35307
Reported by: Rémy Oudompheng
Reviewer(s): Lorenz Panny