Skip to content

Commit 4861863

Browse files
committed
mpl no_debug_ranges
1 parent d561119 commit 4861863

7 files changed

Lines changed: 52 additions & 16 deletions

File tree

Lib/test/test_code.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,6 @@ def test_co_positions_artificial_instructions(self):
425425
]
426426
)
427427

428-
# TODO: RUSTPYTHON; no_debug_ranges option not supported
429-
@unittest.expectedFailure
430428
def test_endline_and_columntable_none_when_no_debug_ranges(self):
431429
# Make sure that if `-X no_debug_ranges` is used, there is
432430
# minimal debug info
@@ -442,8 +440,6 @@ def f():
442440
""")
443441
assert_python_ok('-X', 'no_debug_ranges', '-c', code)
444442

445-
# TODO: RUSTPYTHON; no_debug_ranges option not supported
446-
@unittest.expectedFailure
447443
def test_endline_and_columntable_none_when_no_debug_ranges_env(self):
448444
# Same as above but using the environment variable opt out.
449445
code = textwrap.dedent("""

Lib/test/test_traceback.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ def test_no_caret_with_no_debug_ranges_flag(self):
147147
finally:
148148
unlink(TESTFN)
149149

150-
# TODO: RUSTPYTHON; no_debug_ranges option not supported
151-
@unittest.expectedFailure
152150
def test_no_caret_with_no_debug_ranges_flag_python_traceback(self):
153151
code = textwrap.dedent("""
154152
import traceback

crates/codegen/src/compile.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,22 @@ enum DoneWithFuture {
115115
Yes,
116116
}
117117

118-
#[derive(Debug, Clone, Default)]
118+
#[derive(Debug, Clone)]
119119
pub struct CompileOpts {
120120
/// How optimized the bytecode output should be; any optimize > 0 does
121121
/// not emit assert statements
122122
pub optimize: u8,
123+
/// Include column info in bytecode (-X no_debug_ranges disables)
124+
pub debug_ranges: bool,
125+
}
126+
127+
impl Default for CompileOpts {
128+
fn default() -> Self {
129+
Self {
130+
optimize: 0,
131+
debug_ranges: true,
132+
}
133+
}
123134
}
124135

125136
#[derive(Debug, Clone, Copy)]
@@ -859,7 +870,7 @@ impl Compiler {
859870
let pop = self.code_stack.pop();
860871
let stack_top = compiler_unwrap_option(self, pop);
861872
// No parent scope stack to maintain
862-
unwrap_internal(self, stack_top.finalize_code(self.opts.optimize))
873+
unwrap_internal(self, stack_top.finalize_code(&self.opts))
863874
}
864875

865876
/// Push a new fblock

crates/codegen/src/ir.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,11 @@ pub struct CodeInfo {
132132
}
133133

134134
impl CodeInfo {
135-
pub fn finalize_code(mut self, optimize: u8) -> crate::InternalResult<CodeObject> {
136-
if optimize > 0 {
135+
pub fn finalize_code(
136+
mut self,
137+
opts: &crate::compile::CompileOpts,
138+
) -> crate::InternalResult<CodeObject> {
139+
if opts.optimize > 0 {
137140
self.dce();
138141
}
139142

@@ -219,7 +222,11 @@ impl CodeInfo {
219222
}
220223

221224
// Generate linetable from locations
222-
let linetable = generate_linetable(&locations, first_line_number.get() as i32);
225+
let linetable = generate_linetable(
226+
&locations,
227+
first_line_number.get() as i32,
228+
opts.debug_ranges,
229+
);
223230

224231
Ok(CodeObject {
225232
flags,
@@ -417,6 +424,7 @@ fn iter_blocks(blocks: &[Block]) -> impl Iterator<Item = (BlockIdx, &Block)> + '
417424
fn generate_linetable(
418425
locations: &[(SourceLocation, SourceLocation)],
419426
first_line: i32,
427+
debug_ranges: bool,
420428
) -> Box<[u8]> {
421429
if locations.is_empty() {
422430
return Box::new([]);
@@ -441,15 +449,31 @@ fn generate_linetable(
441449
while length > 0 {
442450
let entry_length = length.min(8);
443451

444-
// Get line and column information
452+
// Get line information
445453
let line = loc.line.get() as i32;
446-
let col = loc.character_offset.to_zero_indexed() as i32;
447454
let end_line = end_loc.line.get() as i32;
448-
let end_col = end_loc.character_offset.to_zero_indexed() as i32;
449-
450455
let line_delta = line - prev_line;
451456
let end_line_delta = end_line - line;
452457

458+
// When debug_ranges is disabled, only emit line info (NoColumns format)
459+
if !debug_ranges {
460+
// NoColumns format (code 13): line info only, no column data
461+
linetable.push(
462+
0x80 | ((PyCodeLocationInfoKind::NoColumns as u8) << 3)
463+
| ((entry_length - 1) as u8),
464+
);
465+
write_signed_varint(&mut linetable, line_delta);
466+
467+
prev_line = line;
468+
length -= entry_length;
469+
i += entry_length;
470+
continue;
471+
}
472+
473+
// Get column information (only when debug_ranges is enabled)
474+
let col = loc.character_offset.to_zero_indexed() as i32;
475+
let end_col = end_loc.character_offset.to_zero_indexed() as i32;
476+
453477
// Choose the appropriate encoding based on line delta and column info
454478
if line_delta == 0 && end_line_delta == 0 {
455479
if col < 80 && end_col - col < 16 && end_col >= col {

crates/vm/src/vm/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ impl VirtualMachine {
508508
pub fn compile_opts(&self) -> crate::compiler::CompileOpts {
509509
crate::compiler::CompileOpts {
510510
optimize: self.state.config.settings.optimize,
511+
debug_ranges: self.state.config.settings.code_debug_ranges,
511512
}
512513
}
513514

crates/vm/src/vm/setting.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ pub struct Settings {
5757
// int tracemalloc;
5858
// int perf_profiling;
5959
// int import_time;
60-
// int code_debug_ranges;
60+
/// -X no_debug_ranges: disable column info in bytecode
61+
pub code_debug_ranges: bool,
6162
// int show_ref_count;
6263
// int dump_refs;
6364
// wchar_t *dump_refs_file;
@@ -192,6 +193,7 @@ impl Default for Settings {
192193
argv: vec![],
193194
hash_seed: None,
194195
faulthandler: false,
196+
code_debug_ranges: true,
195197
buffered_stdio: true,
196198
check_hash_pycs_mode: CheckHashPycsMode::Default,
197199
allow_external_library: cfg!(feature = "importlib"),

src/settings.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ pub fn parse_opts() -> Result<(Settings, RunMode), lexopt::Error> {
270270
"faulthandler" => settings.faulthandler = true,
271271
"warn_default_encoding" => settings.warn_default_encoding = true,
272272
"no_sig_int" => settings.install_signal_handlers = false,
273+
"no_debug_ranges" => settings.code_debug_ranges = false,
273274
"int_max_str_digits" => {
274275
settings.int_max_str_digits = match value.unwrap().parse() {
275276
Ok(digits) if digits == 0 || digits >= 640 => digits,
@@ -293,6 +294,9 @@ pub fn parse_opts() -> Result<(Settings, RunMode), lexopt::Error> {
293294
settings.warn_default_encoding =
294295
settings.warn_default_encoding || env_bool("PYTHONWARNDEFAULTENCODING");
295296
settings.faulthandler = settings.faulthandler || env_bool("PYTHONFAULTHANDLER");
297+
if env_bool("PYTHONNODEBUGRANGES") {
298+
settings.code_debug_ranges = false;
299+
}
296300

297301
if settings.dev_mode {
298302
settings.warnoptions.push("default".to_owned());

0 commit comments

Comments
 (0)