Skip to content

Commit

Permalink
Disable fancy printing of Yap expressions
Browse files Browse the repository at this point in the history
on macOS there is a strange segfault that we cannot reproduce nicely.
This is a nice feature but it's not worth maintaining at this cost.
  • Loading branch information
saraedum committed Dec 29, 2023
1 parent 22add54 commit 31733f0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 31 deletions.
17 changes: 2 additions & 15 deletions libexactreal/exact-real/cppyy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <boost/yap/print.hpp>
#include <iosfwd>
#include <memory>
#include <strstream>
#include <sstream>

#include "element.hpp"
Expand Down Expand Up @@ -96,15 +95,11 @@ auto optional_rational(const Element<Ring> &element) { return static_cast<std::o
// A helper to get RAII that cereal needs to make sure that its output has been flushed.
template <typename T, typename Archive>
std::string serialize(const T &value) {
// We use the deprecated strstream to work around a segfault on macOS. The
// segfault does not happen here on macOS but with the print() below. In any
// case, we are trying to use the same code path for both use cases.
std::strstream serialized;
std::ostringstream serialized;
{
Archive archive(serialized);
archive(value);
}
serialized << std::ends;
return serialized.str();
}

Expand All @@ -123,17 +118,9 @@ T deserialize(const std::string &serialized) {
// boost::yap::print fails to instantiate on macOS so we wrap it here.
template <typename Expr>
std::string print(const Expr& expr) {
// Mysteriously, when using a stringstream on macOS, we get a segfault with
// this code unless we explicitly terminate the stream with an std::ends.
// (However, the ends then shows up as an explicit null terminator in the
// string and no such null terminator should be necessary with a
// stringstream. With a (deprecated) strstream, the null terminator is
// however necessary and everything just works, so we keep this as a
// workaround for the time being.)
std::strstream os;
std::ostringstream os;

boost::yap::print(os, expr);
os << std::ends;
return os.str();
}
} // namespace cppyy
Expand Down
23 changes: 9 additions & 14 deletions pyexactreal/src/pyexactreal/cppyy_exactreal.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ def enable_yap(proxy, name):
>>> from pyexactreal import exactreal
>>> a = exactreal.Arb(1)
>>> b = a + a; b
expr<+>
term<exactreal::Arb ... &>[=1.00000]
term<exactreal::Arb ... &>[=1.00000]
arithmetic expression
>>> b(64)
2.00000
>>> b += b
Expand Down Expand Up @@ -226,11 +224,7 @@ class Yap(object):
>>> a += a
>>> a += a
>>> a
expr<+>
expr<+> const &
expr<+> const &
expr<+> const &
...
arithmetic expression
>>> a(64)
16.0000
Expand Down Expand Up @@ -369,19 +363,20 @@ def __repr__(self):
r"""
Return a printable representation of this Yap expression.
EXAMPLES:
.. NOTE:
We use Yap's own debug printer. It's quite verbose::
Previously, we used Yap's debug printer. While it works fine most of
the time, we ran into spurious segfauls on macOS in CI runs. Therefore,
we disabled such fancy printing here, in particular, because we
probably want to get rid of most of this overly fancy Arb wrap.
>>> from pyexactreal import exactreal
>>> a = exactreal.Arb(1)
>>> a + a
expr<+>
term<exactreal::Arb ... &>[=1.00000]
term<exactreal::Arb ... &>[=1.00000]
arithmetic expression
"""
return cppyy.gbl.exactreal.cppyy.print(self.value).strip()
return "arithmetic expression"

def makeModule(traits, gens, ring=None):
r"""
Expand Down
2 changes: 1 addition & 1 deletion pyexactreal/test/arb.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_arithmetic():
b += a

# expressions do not get evaluated without specifying a precision
assert str(a + a).startswith("expr<+>")
assert str(a + a) == "arithmetic expression"

assert -(-a) == a

Expand Down
2 changes: 1 addition & 1 deletion pyexactreal/test/arf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_arithmetic():
b = Arf(2)

# Expressions do not get evaluated without specifying a precision/rounding
assert str(a + a).startswith("expr<+>")
assert str(a + a) == "arithmetic expression"

assert (a + a)(64, Arf.Round.DOWN) == b

Expand Down

0 comments on commit 31733f0

Please sign in to comment.