Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

evaluate namespaces recursively, and function namespaces, too #2

Merged
merged 2 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions evalserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,35 @@
import re
from socket import socket, AF_INET, SOCK_STREAM
import traceback
import types
from typing import Any, Mapping


def deaddress(text: str) -> str:
return re.sub(r"0x[0-9a-f]+", "0x...", text)


def try_exec(data: bytes) -> dict[str, str | dict[str, str]]:
def get_ns(ns: Mapping[str, Any]) -> dict[str, Any]:
ret = {}
for k, v in ns.items():
child_ns: Mapping[str, Any] | None = None
if isinstance(v, type):
child_ns = v.__dict__
elif isinstance(v, types.FunctionType):
try:
child_ns = v()
except Exception as e:
child_ns = {"error": "run", "message": repr(e)}
child: Any
if child_ns is not None:
child = get_ns(child_ns)
else:
child = deaddress(repr(v))
ret[k] = child
return ret


def try_exec(data: bytes) -> dict[str, Any]:
try:
data_str = data.decode("utf-8")
except UnicodeDecodeError:
Expand All @@ -29,17 +51,12 @@ def try_exec(data: bytes) -> dict[str, str | dict[str, str]]:
except Exception as e:
return {"error": "compile", "message": repr(e)}
try:
ns = {}
ns: dict[str, Any] = {}
exec(code, ns, ns)
del ns["__builtins__"]
except Exception as e:
return {"error": "run", "message": repr(e)}
classdicts = {}
for k, v in ns.items():
if isinstance(v, type):
classdict = repr(v.__dict__)
classdicts[k] = deaddress(classdict)
return {"result": deaddress(repr(ns)), "classdicts": classdicts}
return get_ns(ns)


@dataclass
Expand Down
9 changes: 4 additions & 5 deletions test_fuzz_comps.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ def statements():
st.from_type(ast.Global),
st.from_type(ast.Expr),
st.from_type(ast.FunctionDef),
st.from_type(ast.Name),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would just generate statements of the form a, right? Not sure why that's useful.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the simplified reproducer for python/cpython#104602 a simple b after the comprehension would expose the bug. (In your form it was a print(b) but the print isn't necessary.) So it seems useful to allow these to be generated?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true. One concern is that this will just throw NameError in most cases, making the run tests less useful. But it can still help catch scoping bugs.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although with only two allowed names, the chance of NameError decreases some? You may be right, I think we will need to observe some actual runs.

classes(),
)

Expand All @@ -255,7 +256,8 @@ def functions(draw):
name = draw(identifiers())
arg_names = draw(st.sets(identifiers(), min_size=0, max_size=3))
args = ast.arguments(args=[ast.arg(name) for name in arg_names], defaults=[])
stmts = draw(st.lists(statements(), min_size=1, max_size=10))
stmts = draw(st.lists(statements(), min_size=1, max_size=5))
stmts += [ast.Return(ast.Call(ast.Name("locals"), [], []))]
return ast.FunctionDef(name, args, stmts, [])


Expand All @@ -265,10 +267,7 @@ def functions(draw):
def module_level_statements():
# require top-level class or function; plain module-level comprehensions are
# not very interesting in terms of finding scoping bugs
return st.one_of(
st.from_type(ast.ClassDef),
st.from_type(ast.FunctionDef)
)
return st.one_of(st.from_type(ast.ClassDef), st.from_type(ast.FunctionDef))


st.register_type_strategy(
Expand Down