1# Locations table 2 3For versions up to 3.10 see ./lnotab_notes.txt 4 5In version 3.11 the `co_linetable` bytes object of code objects contains a compact representation of the positions returned by the `co_positions()` iterator. 6 7The `co_linetable` consists of a sequence of location entries. 8Each entry starts with a byte with the most significant bit set, followed by zero or more bytes with most significant bit unset. 9 10Each entry contains the following information: 11* The number of code units covered by this entry (length) 12* The start line 13* The end line 14* The start column 15* The end column 16 17The first byte has the following format: 18 19Bit 7 | Bits 3-6 | Bits 0-2 20 ---- | ---- | ---- 21 1 | Code | Length (in code units) - 1 22 23The codes are enumerated in the `_PyCodeLocationInfoKind` enum. 24 25## Variable length integer encodings 26 27Integers are often encoded using a variable length integer encoding 28 29### Unsigned integers (varint) 30 31Unsigned integers are encoded in 6 bit chunks, least significant first. 32Each chunk but the last has bit 6 set. 33For example: 34 35* 63 is encoded as `0x3f` 36* 200 is encoded as `0x48`, `0x03` 37 38### Signed integers (svarint) 39 40Signed integers are encoded by converting them to unsigned integers, using the following function: 41```Python 42def convert(s): 43 if s < 0: 44 return ((-s)<<1) | 1 45 else: 46 return (s<<1) 47``` 48 49## Location entries 50 51The meaning of the codes and the following bytes are as follows: 52 53Code | Meaning | Start line | End line | Start column | End column 54 ---- | ---- | ---- | ---- | ---- | ---- 55 0-9 | Short form | Δ 0 | Δ 0 | See below | See below 56 10-12 | One line form | Δ (code - 10) | Δ 0 | unsigned byte | unsigned byte 57 13 | No column info | Δ svarint | Δ 0 | None | None 58 14 | Long form | Δ svarint | Δ varint | varint | varint 59 15 | No location | None | None | None | None 60 61The Δ means the value is encoded as a delta from another value: 62* Start line: Delta from the previous start line, or `co_firstlineno` for the first entry. 63* End line: Delta from the start line 64 65### The short forms 66 67Codes 0-9 are the short forms. The short form consists of two bytes, the second byte holding additional column information. The code is the start column divided by 8 (and rounded down). 68* Start column: `(code*8) + ((second_byte>>4)&7)` 69* End column: `start_column + (second_byte&15)` 70