Skip to content

Commit 902985d

Browse files
Copilotyouknowone
andauthored
Fix inspect.getsource returning truncated source for multi-line function definitions (#7519)
* Initial plan * fix: restore def-line source range before entering function scope so co_firstlineno is correct Agent-Logs-Url: https://github.com/RustPython/RustPython/sessions/94701403-2011-4525-88f1-6e06891da6a4 Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com> * fix: remove pre-existing expectedFailure decorators from test_gettext plural form tests Agent-Logs-Url: https://github.com/RustPython/RustPython/sessions/ce27bf53-569f-45a0-ad5a-08e8f322c717 Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com> * remove extra_tests/snippets/inspect_getsource.py (covered by test_inspect) Agent-Logs-Url: https://github.com/RustPython/RustPython/sessions/2b64da1b-8aab-4fec-8b28-3a21d46ac2f9 Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
1 parent 90c5464 commit 902985d

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

Lib/test/test_gettext.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,6 @@ def _test_plural_forms(self, ngettext, gettext,
385385
x = ngettext(singular, plural, None)
386386
self.assertEqual(x, tplural)
387387

388-
@unittest.expectedFailure # TODO: RUSTPYTHON
389388
def test_plural_forms(self):
390389
self._test_plural_forms(
391390
self.ngettext, self.gettext,
@@ -396,7 +395,6 @@ def test_plural_forms(self):
396395
'%d file deleted', '%d files deleted',
397396
'%d file deleted', '%d files deleted')
398397

399-
@unittest.expectedFailure # TODO: RUSTPYTHON
400398
def test_plural_context_forms(self):
401399
ngettext = partial(self.npgettext, 'With context')
402400
gettext = partial(self.pgettext, 'With context')
@@ -409,7 +407,6 @@ def test_plural_context_forms(self):
409407
'%d file deleted', '%d files deleted',
410408
'%d file deleted', '%d files deleted')
411409

412-
@unittest.expectedFailure # TODO: RUSTPYTHON
413410
def test_plural_wrong_context_forms(self):
414411
self._test_plural_forms(
415412
partial(self.npgettext, 'Unknown context'),
@@ -442,7 +439,6 @@ def setUp(self):
442439
self.pgettext = partial(gettext.dpgettext, 'gettext')
443440
self.npgettext = partial(gettext.dnpgettext, 'gettext')
444441

445-
@unittest.expectedFailure # TODO: RUSTPYTHON
446442
def test_plural_forms_wrong_domain(self):
447443
self._test_plural_forms(
448444
partial(gettext.dngettext, 'unknown'),
@@ -451,7 +447,6 @@ def test_plural_forms_wrong_domain(self):
451447
'There is %s file', 'There are %s files',
452448
numbers_only=False)
453449

454-
@unittest.expectedFailure # TODO: RUSTPYTHON
455450
def test_plural_context_forms_wrong_domain(self):
456451
self._test_plural_forms(
457452
partial(gettext.dnpgettext, 'unknown', 'With context'),
@@ -472,7 +467,6 @@ def setUp(self):
472467
self.pgettext = t.pgettext
473468
self.npgettext = t.npgettext
474469

475-
@unittest.expectedFailure # TODO: RUSTPYTHON
476470
def test_plural_forms_null_translations(self):
477471
t = gettext.NullTranslations()
478472
self._test_plural_forms(
@@ -481,7 +475,6 @@ def test_plural_forms_null_translations(self):
481475
'There is %s file', 'There are %s files',
482476
numbers_only=False)
483477

484-
@unittest.expectedFailure # TODO: RUSTPYTHON
485478
def test_plural_context_forms_null_translations(self):
486479
t = gettext.NullTranslations()
487480
self._test_plural_forms(

Lib/test/test_inspect/test_inspect.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,6 @@ def test_range_traceback_toplevel_frame(self):
961961
class TestDecorators(GetSourceBase):
962962
fodderModule = mod2
963963

964-
@unittest.expectedFailure # TODO: RUSTPYTHON; pass
965964
def test_wrapped_decorator(self):
966965
self.assertSourceEqual(mod2.wrapped, 14, 17)
967966

@@ -1259,7 +1258,6 @@ def test_class(self):
12591258
class TestComplexDecorator(GetSourceBase):
12601259
fodderModule = mod2
12611260

1262-
@unittest.expectedFailure # TODO: RUSTPYTHON; return foo + bar()
12631261
def test_parens_in_decorator(self):
12641262
self.assertSourceEqual(self.fodderModule.complex_decorated, 273, 275)
12651263

crates/codegen/src/compile.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4246,11 +4246,19 @@ impl Compiler {
42464246
is_async: bool,
42474247
type_params: Option<&ast::TypeParams>,
42484248
) -> CompileResult<()> {
4249+
// Save the source range of the `def` line before compiling decorators/defaults,
4250+
// so that the function code object gets the correct co_firstlineno.
4251+
let def_source_range = self.current_source_range;
4252+
42494253
self.prepare_decorators(decorator_list)?;
42504254

42514255
// compile defaults and return funcflags
42524256
let funcflags = self.compile_default_arguments(parameters)?;
42534257

4258+
// Restore the `def` line range so that enter_function → push_output → get_source_line_number()
4259+
// records the `def` keyword's line as co_firstlineno, not the last default-argument line.
4260+
self.set_source_range(def_source_range);
4261+
42544262
let is_generic = type_params.is_some();
42554263
let mut num_typeparam_args = 0;
42564264

0 commit comments

Comments
 (0)