pschanely / CrossHair

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

`hashlib` requires the buffer protocol, which symbolics bytes don't provide #272

Closed Zac-HD closed 2 months ago

Zac-HD commented 2 months ago
import hashlib
from crosshair.core_and_libs import proxy_for_type, standalone_statespace

with standalone_statespace:
    buf = proxy_for_type(bytes, "buf")
    hashlib.sha256(buf)
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/crosshair/util.py", line 298, in load_file
    return import_module(module_name)
  File "/usr/local/lib/python3.10/site-packages/crosshair/util.py", line 284, in import_module
    result_module = importlib.import_module(module_name)
  File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/tmp/main.py", line 6, in <module>
    hashlib.sha256(buf)
TypeError: object supporting the buffer API required

(originally via https://github.com/HypothesisWorks/hypothesis/pull/4034, similar to #271; I suspect I'm misusing crosshair-web because it's treating this as an import failure instead of giving me a nice repro link - lovely UX though!)

pschanely commented 2 months ago

Fixed in v0.0.61! (but just realizes the inputs)

Re UX, thanks - but it's just an (old) fork of mypy playground.

Yeah, so crosshar-web is really intended to run on a function with a contract, so the equivalent is something like this. But at some point Liam discovered that you could also use it to share little snippets of code. (we use standalone_statespace internally for tests a lot b/c it sets up everything you need to use symbolics, but without the path exploration loop). In this case, any error will show as an "import" error, since crosshair is attempting to import the code before getting on to the contract testing.