|
| 1 | +# (C) Copyright 2025 by Rocky Bernstein |
| 2 | +# |
| 3 | +# This program is free software; you can redistribute it and/or |
| 4 | +# modify it under the terms of the GNU General Public License |
| 5 | +# as published by the Free Software Foundation; either version 2 |
| 6 | +# of the License, or (at your option) any later version. |
| 7 | +# |
| 8 | +# This program is distributed in the hope that it will be useful, |
| 9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +# GNU General Public License for more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU General Public License |
| 14 | +# along with this program; if not, write to the Free Software |
| 15 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | + |
| 17 | +from dataclasses import dataclass |
| 18 | +from typing import Any, Dict, Tuple, Union |
| 19 | + |
| 20 | +from xdis.codetype.code311 import Code311 |
| 21 | + |
| 22 | + |
| 23 | +@dataclass |
| 24 | +class SourceLocation: |
| 25 | + # 1-based integer line number |
| 26 | + line_number: int |
| 27 | + # column offset in line: 1-based int (constructed from a zero-indexed stored value) |
| 28 | + column_offset: int |
| 29 | + |
| 30 | + |
| 31 | +class Code312Rust(Code311): |
| 32 | + """Class for a RustPython 3.12 code object used when a Python |
| 33 | + interpreter is not RustPython 3.12 but working on RustPython 3.12 bytecode. It |
| 34 | + also functions as an object that can be used to build or write a |
| 35 | + Python3 code object, since we allow mutable structures. |
| 36 | +
|
| 37 | + When done mutating, call method to_native(). |
| 38 | +
|
| 39 | + For convenience in generating code objects, fields like |
| 40 | + `co_consts`, co_names which are (immutable) tuples in the end-result can be stored |
| 41 | + instead as (mutable) lists. Likewise, the line number table `co_lnotab` |
| 42 | + can be stored as a simple list of offset, line_number tuples. |
| 43 | +
|
| 44 | + """ |
| 45 | + def __init__( |
| 46 | + self, |
| 47 | + co_argcount: int, |
| 48 | + co_posonlyargcount: int, |
| 49 | + co_kwonlyargcount: int, |
| 50 | + co_stacksize: int, |
| 51 | + co_flags: int, |
| 52 | + co_code: bytes, |
| 53 | + co_consts: tuple, |
| 54 | + co_names: tuple[str], |
| 55 | + co_varnames: tuple[str], |
| 56 | + co_filename: str, |
| 57 | + co_name: str, |
| 58 | + co_firstlineno: int, |
| 59 | + co_freevars: tuple, |
| 60 | + co_cellvars: tuple, |
| 61 | + version_triple: Tuple[int, int, int], |
| 62 | + locations: tuple, |
| 63 | + collection_order: Dict[Union[set, frozenset, dict], Tuple[Any]] = {}, |
| 64 | + ) -> None: |
| 65 | + self.co_argcount = co_argcount |
| 66 | + self.co_posonlyargcount = co_posonlyargcount |
| 67 | + self.co_kwonlyargcount = co_kwonlyargcount |
| 68 | + self.co_lnotab={} # FIXME: compute this from locations. |
| 69 | + self.co_stacksize = co_stacksize |
| 70 | + self.co_flags = co_flags |
| 71 | + self.co_code = co_code |
| 72 | + self.co_consts = co_consts |
| 73 | + self.co_names = co_names |
| 74 | + self.co_varnames = co_varnames |
| 75 | + self.co_filename = co_filename |
| 76 | + self.co_name = co_name |
| 77 | + self.co_firstlineno = co_firstlineno # None if 0 in serialized form |
| 78 | + self.co_cellvars = co_cellvars |
| 79 | + self.co_freevars= co_freevars |
| 80 | + self.co_collection_order = collection_order |
| 81 | + self.version_triple = version_triple |
| 82 | + self.locations = locations |
0 commit comments