pschanely / CrossHair

An analysis tool for Python that blurs the line between testing and type systems.
Other
996 stars 47 forks source link

Auto-realize encode/decode arguments for unsupported codecs #271

Closed pschanely closed 1 month ago

pschanely commented 2 months ago

Found via hypothesis:

Looks like string-encoding wants to receive exactly a str, meaning it crashes under crosshair:

from encodings.aliases import aliases
from hypothesis import Verbosity, given, settings, strategies as st

def _enc(cdc):
    try:
        "".encode(cdc)
        return True
    except Exception:
        return False

lots_of_encodings = sorted(x for x in set(aliases).union(aliases.values()) if _enc(x))
assert len(lots_of_encodings) > 100  # sanity-check

@settings(backend="crosshair", verbosity=Verbosity.verbose)
@given(st.text(), st.sampled_from(lots_of_encodings))
def test_b(string, codec_name):
    string.encode(codec_name)

representative traceback:

Trying example: test_b(
    string='',
)
Traceback (most recent call last):
  File ".../demo.py", line 14, in test_b
    string.encode("037")
  File ".venv/lib/python3.10/site-packages/crosshair/libimpl/builtinslib.py", line 2691, in encode
    return codecs.encode(self, encoding, errors)
  File ".venv/lib/python3.10/site-packages/crosshair/libimpl/codecslib.py", line 19, in _encode
    (out, _len_consumed) = _getencoder(encoding)(obj, errors)
  File ".../3.10.11/lib/python3.10/encodings/cp037.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
TypeError: charmap_encode() argument 1 must be str, not LazyIntSymbolicStr

...
hypothesis.errors.Flaky: Inconsistent results from replaying a failing test case!
  last: INTERESTING from TypeError at .../3.10.11/lib/python3.10/encodings/cp037.py:12
  this: VALID

Originally posted by @Zac-HD in https://github.com/HypothesisWorks/hypothesis/issues/4034#issuecomment-2227004901

pschanely commented 1 month ago

This was fixed in 0.0.62, but I neglected to updated this issue (or add it to the changelog). Doing so now!