Skip to content

chore: rely on the default inclusions for ruff #5775

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

Merged
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
62 changes: 33 additions & 29 deletions crawl_sourcecode.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" This script can be used to test the equivalence in parsing between
"""This script can be used to test the equivalence in parsing between
rustpython and cpython.

Usage example:
Expand All @@ -8,76 +8,80 @@
$ diff cpython.txt rustpython.txt
"""


import ast
import sys
import symtable
import dis

filename = sys.argv[1]
print('Crawling file:', filename)
print("Crawling file:", filename)


with open(filename, 'r') as f:
with open(filename, "r") as f:
source = f.read()

t = ast.parse(source)
print(t)

shift = 3


def print_node(node, indent=0):
indents = ' ' * indent
indents = " " * indent
if isinstance(node, ast.AST):
lineno = 'row={}'.format(node.lineno) if hasattr(node, 'lineno') else ''
lineno = "row={}".format(node.lineno) if hasattr(node, "lineno") else ""
print(indents, "NODE", node.__class__.__name__, lineno)
for field in node._fields:
print(indents,'-', field)
print(indents, "-", field)
f = getattr(node, field)
if isinstance(f, list):
for f2 in f:
print_node(f2, indent=indent+shift)
print_node(f2, indent=indent + shift)
else:
print_node(f, indent=indent+shift)
print_node(f, indent=indent + shift)
else:
print(indents, 'OBJ', node)
print(indents, "OBJ", node)


print_node(t)

# print(ast.dump(t))
flag_names = [
'is_referenced',
'is_assigned',
'is_global',
'is_local',
'is_parameter',
'is_free',
"is_referenced",
"is_assigned",
"is_global",
"is_local",
"is_parameter",
"is_free",
]


def print_table(table, indent=0):
indents = ' ' * indent
print(indents, 'table:', table.get_name())
print(indents, ' ', 'name:', table.get_name())
print(indents, ' ', 'type:', table.get_type())
print(indents, ' ', 'line:', table.get_lineno())
print(indents, ' ', 'identifiers:', table.get_identifiers())
print(indents, ' ', 'Syms:')
indents = " " * indent
print(indents, "table:", table.get_name())
print(indents, " ", "name:", table.get_name())
print(indents, " ", "type:", table.get_type())
print(indents, " ", "line:", table.get_lineno())
print(indents, " ", "identifiers:", table.get_identifiers())
print(indents, " ", "Syms:")
for sym in table.get_symbols():
flags = []
for flag_name in flag_names:
func = getattr(sym, flag_name)
if func():
flags.append(flag_name)
print(indents, ' sym:', sym.get_name(), 'flags:', ' '.join(flags))
print(indents, " sym:", sym.get_name(), "flags:", " ".join(flags))
if table.has_children():
print(indents, ' ', 'Child tables:')
print(indents, " ", "Child tables:")
for child in table.get_children():
print_table(child, indent=indent+shift)
print_table(child, indent=indent + shift)


table = symtable.symtable(source, 'a', 'exec')
table = symtable.symtable(source, "a", "exec")
print_table(table)

print()
print('======== dis.dis ========')
print("======== dis.dis ========")
print()
co = compile(source, filename, 'exec')
co = compile(source, filename, "exec")
dis.dis(co)
5 changes: 2 additions & 3 deletions demo_closures.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@


def foo(x):
def bar(z):
return z + x

return bar


f = foo(9)
g = foo(10)

print(f(2))
print(g(2))

7 changes: 0 additions & 7 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
include = [
"examples/**/*.py",
"extra_tests/**/*.py",
"wasm/**/*.py",
]

exclude = [
".*",
"Lib",
"vm/Lib",
"benches",
Expand Down
9 changes: 6 additions & 3 deletions scripts/cargo-llvm-cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@

TARGET = "extra_tests/snippets"


def run_llvm_cov(file_path: str):
""" Run cargo llvm-cov on a file. """
"""Run cargo llvm-cov on a file."""
if file_path.endswith(".py"):
command = ["cargo", "llvm-cov", "--no-report", "run", "--", file_path]
subprocess.call(command)


def iterate_files(folder: str):
""" Iterate over all files in a folder. """
"""Iterate over all files in a folder."""
for root, _, files in os.walk(folder):
for file in files:
file_path = os.path.join(root, file)
run_llvm_cov(file_path)


if __name__ == "__main__":
iterate_files(TARGET)
iterate_files(TARGET)
34 changes: 29 additions & 5 deletions scripts/fix_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,26 @@
4. Ensure that there are no unexpected successes in the test.
5. Actually fix the test.
"""

import argparse
import ast
import itertools
import platform
from pathlib import Path


def parse_args():
parser = argparse.ArgumentParser(description="Fix test.")
parser.add_argument("--path", type=Path, help="Path to test file")
parser.add_argument("--force", action="store_true", help="Force modification")
parser.add_argument("--platform", action="store_true", help="Platform specific failure")
parser.add_argument(
"--platform", action="store_true", help="Platform specific failure"
)

args = parser.parse_args()
return args


class Test:
name: str = ""
path: str = ""
Expand All @@ -33,6 +38,7 @@ class Test:
def __str__(self):
return f"Test(name={self.name}, path={self.path}, result={self.result})"


class TestResult:
tests_result: str = ""
tests = []
Expand All @@ -52,7 +58,11 @@ def parse_results(result):
in_test_results = True
elif line.startswith("-----------"):
in_test_results = False
if in_test_results and not line.startswith("tests") and not line.startswith("["):
if (
in_test_results
and not line.startswith("tests")
and not line.startswith("[")
):
line = line.split(" ")
if line != [] and len(line) > 3:
test = Test()
Expand All @@ -67,9 +77,11 @@ def parse_results(result):
test_results.tests_result = res
return test_results


def path_to_test(path) -> list[str]:
return path.split(".")[2:]


def modify_test(file: str, test: list[str], for_platform: bool = False) -> str:
a = ast.parse(file)
lines = file.splitlines()
Expand All @@ -84,6 +96,7 @@ def modify_test(file: str, test: list[str], for_platform: bool = False) -> str:
break
return "\n".join(lines)


def modify_test_v2(file: str, test: list[str], for_platform: bool = False) -> str:
a = ast.parse(file)
lines = file.splitlines()
Expand All @@ -101,8 +114,13 @@ def modify_test_v2(file: str, test: list[str], for_platform: bool = False) -> st
if fn.name == test[-1]:
assert not for_platform
indent = " " * fn.col_offset
lines.insert(fn.lineno - 1, indent + fixture)
lines.insert(fn.lineno - 1, indent + "# TODO: RUSTPYTHON")
lines.insert(
fn.lineno - 1, indent + fixture
)
lines.insert(
fn.lineno - 1,
indent + "# TODO: RUSTPYTHON",
)
break
case ast.FunctionDef():
if n.name == test[0] and len(test) == 1:
Expand All @@ -115,11 +133,17 @@ def modify_test_v2(file: str, test: list[str], for_platform: bool = False) -> st
exit()
return "\n".join(lines)


def run_test(test_name):
print(f"Running test: {test_name}")
rustpython_location = "./target/release/rustpython"
import subprocess
result = subprocess.run([rustpython_location, "-m", "test", "-v", test_name], capture_output=True, text=True)

result = subprocess.run(
[rustpython_location, "-m", "test", "-v", test_name],
capture_output=True,
text=True,
)
return parse_results(result)


Expand Down
33 changes: 22 additions & 11 deletions vm/sre_engine/generate_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

assert re._constants.MAGIC == sre_engine_magic


class CompiledPattern:
@classmethod
def compile(cls, pattern, flags=0):
Expand All @@ -21,40 +22,50 @@ def compile(cls, pattern, flags=0):
self.flags = re.RegexFlag(flags | p.state.flags)
return self


for k, v in re.RegexFlag.__members__.items():
setattr(CompiledPattern, k, v)


class EscapeRustStr:
hardcoded = {
ord('\r'): '\\r',
ord('\t'): '\\t',
ord('\r'): '\\r',
ord('\n'): '\\n',
ord('\\'): '\\\\',
ord('\''): '\\\'',
ord('\"'): '\\\"',
ord("\r"): "\\r",
ord("\t"): "\\t",
ord("\r"): "\\r",
ord("\n"): "\\n",
ord("\\"): "\\\\",
ord("'"): "\\'",
ord('"'): '\\"',
}

@classmethod
def __class_getitem__(cls, ch):
if (rpl := cls.hardcoded.get(ch)) is not None:
return rpl
if ch in range(0x20, 0x7f):
if ch in range(0x20, 0x7F):
return ch
return f"\\u{{{ch:x}}}"


def rust_str(s):
return '"' + s.translate(EscapeRustStr) + '"'


# matches `// pattern {varname} = re.compile(...)`
pattern_pattern = re.compile(r"^((\s*)\/\/\s*pattern\s+(\w+)\s+=\s+(.+?))$(?:.+?END GENERATED)?", re.M | re.S)
pattern_pattern = re.compile(
r"^((\s*)\/\/\s*pattern\s+(\w+)\s+=\s+(.+?))$(?:.+?END GENERATED)?", re.M | re.S
)


def replace_compiled(m):
line, indent, varname, pattern = m.groups()
pattern = eval(pattern, {"re": CompiledPattern})
pattern = f"Pattern {{ pattern: {rust_str(pattern.pattern)}, code: &{json.dumps(pattern.code)} }}"
return f'''{line}
return f"""{line}
{indent}// START GENERATED by generate_tests.py
{indent}#[rustfmt::skip] let {varname} = {pattern};
{indent}// END GENERATED'''
{indent}// END GENERATED"""


with os.scandir("tests") as t, os.scandir("benches") as b:
for f in chain(t, b):
Expand Down
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy