Post-pass: extract docstrings and rationale comments from Python source. Mutates result in-place by appending to result['nodes'] and result['edges']. ``parsed`` is the ``(source, tree)`` ``extract_python`` already has for this file; see ``engine.take_python_parse``. Without it this re-r
(path: Path, result: dict, parsed=None)
| 2029 | |
| 2030 | |
| 2031 | def _extract_python_rationale(path: Path, result: dict, parsed=None) -> None: |
| 2032 | """Post-pass: extract docstrings and rationale comments from Python source. |
| 2033 | Mutates result in-place by appending to result['nodes'] and result['edges']. |
| 2034 | |
| 2035 | ``parsed`` is the ``(source, tree)`` ``extract_python`` already has for this |
| 2036 | file; see ``engine.take_python_parse``. Without it this re-reads and |
| 2037 | re-parses the file, which made every .py in the corpus parse twice (6.6 s of |
| 2038 | a 102.6 s profiled extraction of graphify's own source, for 748 files). |
| 2039 | """ |
| 2040 | if parsed is not None: |
| 2041 | source, tree = parsed |
| 2042 | root = tree.root_node |
| 2043 | else: |
| 2044 | try: |
| 2045 | import tree_sitter_python as tspython |
| 2046 | from tree_sitter import Language, Parser |
| 2047 | language = Language(tspython.language()) |
| 2048 | parser = Parser(language) |
| 2049 | source = _read_source_bytes(path, warn=False) |
| 2050 | tree = parser.parse(source) |
| 2051 | root = tree.root_node |
| 2052 | except Exception: |
| 2053 | return |
| 2054 | |
| 2055 | # Extract dynamic sys.path modifications (#5253): sys.path.insert / sys.path.append |
| 2056 | sys_path_entries: list[str] = [] |
| 2057 | for stmt in root.children: |
| 2058 | if stmt.type != "expression_statement": |
| 2059 | continue |
| 2060 | for child in stmt.children: |
| 2061 | if child.type == "call": |
| 2062 | fn = child.child_by_field_name("function") |
| 2063 | if fn and fn.type == "attribute": |
| 2064 | attr = fn.child_by_field_name("attribute") |
| 2065 | obj = fn.child_by_field_name("object") |
| 2066 | if attr and obj and obj.type == "attribute": |
| 2067 | sub_attr = obj.child_by_field_name("attribute") |
| 2068 | sub_obj = obj.child_by_field_name("object") |
| 2069 | if ( |
| 2070 | source[sub_obj.start_byte:sub_obj.end_byte] == b"sys" |
| 2071 | and source[sub_attr.start_byte:sub_attr.end_byte] == b"path" |
| 2072 | and source[attr.start_byte:attr.end_byte] in (b"insert", b"append") |
| 2073 | ): |
| 2074 | args = child.child_by_field_name("arguments") |
| 2075 | if args: |
| 2076 | for a in args.children: |
| 2077 | if a.type in ("string", "concatenated_string"): |
| 2078 | val = source[a.start_byte:a.end_byte].decode("utf-8", errors="replace") |
| 2079 | val = val.strip("\"'") |
| 2080 | if val: |
| 2081 | sys_path_entries.append(val) |
| 2082 | if sys_path_entries: |
| 2083 | result["_python_sys_path_entries"] = sys_path_entries |
| 2084 | |
| 2085 | stem = _file_stem(path) |
| 2086 | str_path = str(path) |
| 2087 | nodes = result["nodes"] |
| 2088 | edges = result["edges"] |
no test coverage detected