Skip to content

Commit 4a057e4

Browse files
authored
Merge pull request #2690 from DeusData/fix/py-decorated-nested-class
fix(python): extract a decorated class nested in a class body
2 parents 5dc83ac + dcb6d49 commit 4a057e4

2 files changed

Lines changed: 94 additions & 1 deletion

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11493,9 +11493,12 @@ static void push_nested_class_nodes(TSNode body, const CBMLangSpec *spec, wd_sta
1149311493
wd_push(s, child, enclosing_qn);
1149411494
} else {
1149511495
const char *ck = ts_node_type(child);
11496-
/* A nested class inside #if/#else is still nested here. */
11496+
/* A nested class inside #if/#else is still nested here, and so is
11497+
* a decorated one (Python `@dataclass class Meta:` in a class
11498+
* body): the decorated_definition wraps the class_definition. */
1149711499
if (strcmp(ck, "field_declaration") == 0 ||
1149811500
strcmp(ck, "template_declaration") == 0 || strcmp(ck, "declaration") == 0 ||
11501+
(ctx->language == CBM_LANG_PYTHON && strcmp(ck, "decorated_definition") == 0) ||
1149911502
cbm_kind_in_set(child, cbm_conditional_block_types(spec->language))) {
1150011503
ts_nstack_push(&nc_stack, child);
1150111504
}

‎tests/test_pipeline.c‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15511,6 +15511,94 @@ TEST(pipeline_python_nested_helper_keeps_tests_parallel_issue1548) {
1551115511
return pyn_run_nested_helper_tests(52);
1551215512
}
1551315513

15514+
/* A decorated class nested in a class body (`@dataclass class Meta:` inside a
15515+
* class) is a nested class like an undecorated one: it gets its Class node and
15516+
* its methods their Method nodes. push_nested_class_nodes looked through
15517+
* field/template/declaration and conditional wrappers but not through Python's
15518+
* decorated_definition, so the decorated class and everything in it was never
15519+
* extracted. Both resolvers. */
15520+
static const char pyn_decorated_nested_py[] = "def deco(cls):\n"
15521+
" return cls\n"
15522+
"\n"
15523+
"class Outer:\n"
15524+
" @deco\n"
15525+
" class Decorated:\n"
15526+
" def run(self):\n"
15527+
" return 1\n"
15528+
"\n"
15529+
" class Plain:\n"
15530+
" def go(self):\n"
15531+
" return 2\n";
15532+
15533+
static bool pyn_node_exists(cbm_store_t *s, const char *project, const char *rel_qn,
15534+
const char *label) {
15535+
char qn[512];
15536+
pyn_qn(qn, sizeof(qn), project, rel_qn);
15537+
cbm_node_t n = {0};
15538+
bool found = cbm_store_find_node_by_qn(s, project, qn, &n) == CBM_STORE_OK && n.label &&
15539+
strcmp(n.label, label) == 0;
15540+
cbm_node_free_fields(&n);
15541+
return found;
15542+
}
15543+
15544+
static int pyn_run_decorated_nested(int fillers) {
15545+
char tmp[256];
15546+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_py_decnest_XXXXXX");
15547+
if (!cbm_mkdtemp(tmp)) {
15548+
FAIL("tmpdir");
15549+
}
15550+
write_temp_file(tmp, "sd.py", pyn_decorated_nested_py);
15551+
for (int i = 0; i < fillers; i++) {
15552+
char name[64];
15553+
char body[128];
15554+
snprintf(name, sizeof(name), "filler%d.py", i);
15555+
snprintf(body, sizeof(body), "def filler%d():\n return %d\n", i, i);
15556+
write_temp_file(tmp, name, body);
15557+
}
15558+
char *old_workers = getenv("CBM_WORKERS");
15559+
char *saved = old_workers ? strdup(old_workers) : NULL;
15560+
if (fillers > 0) {
15561+
cbm_setenv("CBM_WORKERS", "4", 1);
15562+
}
15563+
char db_path[512];
15564+
snprintf(db_path, sizeof(db_path), "%s/decnest.db", tmp);
15565+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
15566+
int rc = p ? cbm_pipeline_run(p) : -1;
15567+
cbm_store_t *s = rc == 0 ? cbm_store_open_path(db_path) : NULL;
15568+
const char *project = p ? cbm_pipeline_project_name(p) : NULL;
15569+
bool dec_class = s && pyn_node_exists(s, project, "sd.Outer.Decorated", "Class");
15570+
bool dec_method = s && pyn_node_exists(s, project, "sd.Outer.Decorated.run", "Method");
15571+
bool plain_class = s && pyn_node_exists(s, project, "sd.Outer.Plain", "Class");
15572+
bool plain_method = s && pyn_node_exists(s, project, "sd.Outer.Plain.go", "Method");
15573+
if (s) {
15574+
cbm_store_close(s);
15575+
}
15576+
if (p) {
15577+
cbm_pipeline_free(p);
15578+
}
15579+
if (saved) {
15580+
cbm_setenv("CBM_WORKERS", saved, 1);
15581+
free(saved);
15582+
} else {
15583+
cbm_unsetenv("CBM_WORKERS");
15584+
}
15585+
th_rmtree(tmp);
15586+
ASSERT_EQ(rc, 0);
15587+
ASSERT_TRUE(dec_class); /* the decorated nested class is extracted */
15588+
ASSERT_TRUE(dec_method); /* ... and its method */
15589+
ASSERT_TRUE(plain_class); /* control: undecorated nested class */
15590+
ASSERT_TRUE(plain_method); /* control */
15591+
PASS();
15592+
}
15593+
15594+
TEST(pipeline_python_decorated_nested_class_extracted) {
15595+
return pyn_run_decorated_nested(0);
15596+
}
15597+
15598+
TEST(pipeline_python_decorated_nested_class_extracted_parallel) {
15599+
return pyn_run_decorated_nested(52);
15600+
}
15601+
1551415602
TEST(pipeline_python_project) {
1551515603
/* Port of TestPipelinePythonProject */
1551615604
const char *files[] = {"main.py", "utils.py"};
@@ -25028,6 +25116,8 @@ SUITE(pipeline) {
2502825116
RUN_TEST(pipeline_python_init_function_shares_submodule_qn_parallel_issue1548);
2502925117
RUN_TEST(pipeline_python_nested_helper_keeps_tests_issue1548);
2503025118
RUN_TEST(pipeline_python_nested_helper_keeps_tests_parallel_issue1548);
25119+
RUN_TEST(pipeline_python_decorated_nested_class_extracted);
25120+
RUN_TEST(pipeline_python_decorated_nested_class_extracted_parallel);
2503125121
RUN_TEST(pipeline_header_include_target_is_independent_of_registration_order);
2503225122
RUN_TEST(pipeline_imports_multi_symbol_edges);
2503325123
RUN_TEST(pipeline_go_cross_package_call);

0 commit comments

Comments
 (0)