Skip to content

Commit 075f40e

Browse files
committed
fix(python): make iris.cls() callee primary, drop the extra bare call
When the receiver is iris.cls("<literal>"), the class-qualified name now replaces the primary call's callee instead of being pushed as a second call. Keeping the bare name let it weak-match an unrelated same-named method. Tests assert exactly one Run call per iris.cls() site, and the pipeline fixture adds two unrelated Run definitions that must receive no edges. Refs DeusData#1260 Signed-off-by: Thomas Dyar <tdyar@intersystems.com>
1 parent 8f803f7 commit 075f40e

3 files changed

Lines changed: 48 additions & 11 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3931,6 +3931,16 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
39313931
}
39323932
}
39333933

3934+
// #1260: iris.cls("Pkg.X").M() names its class, so the qualified
3935+
// callee replaces the bare one. Keeping both would let the bare
3936+
// name weak-match an unrelated same-named method.
3937+
if (ctx->language == CBM_LANG_PYTHON) {
3938+
const char *iris_callee = python_iris_cls_callee(ctx, node);
3939+
if (iris_callee) {
3940+
call.callee_name = iris_callee;
3941+
call.is_method = false;
3942+
}
3943+
}
39343944
cbm_calls_push(&ctx->result->calls, ctx->arena, call);
39353945
invocation = describe_emitted_primary_call(node, &callee);
39363946

@@ -3955,17 +3965,6 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
39553965
}
39563966
}
39573967
}
3958-
if (ctx->language == CBM_LANG_PYTHON) {
3959-
const char *iris_callee = python_iris_cls_callee(ctx, node);
3960-
if (iris_callee) {
3961-
CBMCall xcall = {0};
3962-
xcall.callee_name = iris_callee;
3963-
xcall.enclosing_func_qn = call.enclosing_func_qn;
3964-
xcall.site_start_byte = call.site_start_byte;
3965-
xcall.site_end_byte = call.site_end_byte;
3966-
cbm_calls_push(&ctx->result->calls, ctx->arena, xcall);
3967-
}
3968-
}
39693968
}
39703969
}
39713970

‎tests/test_extraction.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3285,6 +3285,28 @@ static int count_calls_in_func(CBMFileResult *r, const char *callee, const char
32853285
return n;
32863286
}
32873287

3288+
/* #1260: calls in `func` whose callee is "Run" or ends in ".Run", whatever
3289+
* the qualifier. One iris.cls("X").Run() site must emit exactly one. */
3290+
static int count_run_calls_in_func(CBMFileResult *r, const char *func) {
3291+
int n = 0;
3292+
size_t flen = strlen(func);
3293+
for (int i = 0; i < r->calls.count; i++) {
3294+
const CBMCall *c = &r->calls.items[i];
3295+
if (!c->callee_name || !c->enclosing_func_qn) {
3296+
continue;
3297+
}
3298+
size_t cl = strlen(c->callee_name);
3299+
bool is_run = strcmp(c->callee_name, "Run") == 0 ||
3300+
(cl > 4 && strcmp(c->callee_name + cl - 4, ".Run") == 0);
3301+
size_t qlen = strlen(c->enclosing_func_qn);
3302+
if (is_run && qlen > flen && c->enclosing_func_qn[qlen - flen - 1] == '.' &&
3303+
strcmp(c->enclosing_func_qn + qlen - flen, func) == 0) {
3304+
n++;
3305+
}
3306+
}
3307+
return n;
3308+
}
3309+
32883310
TEST(python_iris_cls_receiver_is_class_aware) {
32893311
CBMFileResult *r = extract("import iris\n"
32903312
"def want_a():\n"
@@ -3303,6 +3325,10 @@ TEST(python_iris_cls_receiver_is_class_aware) {
33033325
ASSERT_EQ(count_calls_in_func(r, "Pkg.A.Run", "want_a"), 1);
33043326
ASSERT_EQ(count_calls_in_func(r, "Pkg.B.Run", "want_b"), 1);
33053327
ASSERT_EQ(count_calls_in_func(r, "Pkg.A.Run", "want_b"), 0);
3328+
/* The class-qualified callee replaces the bare one: no second call per site. */
3329+
ASSERT_EQ(count_run_calls_in_func(r, "want_a"), 1);
3330+
ASSERT_EQ(count_run_calls_in_func(r, "want_b"), 1);
3331+
ASSERT_EQ(count_run_calls_in_func(r, "dynamic"), 1);
33063332
/* A non-literal class argument names nothing: no class-qualified callee. */
33073333
ASSERT_EQ(count_calls_in_func(r, "name.Run", "dynamic"), 0);
33083334
/* An f-string is a Python "string" node but not a class name. */

‎tests/test_pipeline.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14729,9 +14729,17 @@ TEST(pipeline_python_iris_cls_class_aware_calls) {
1472914729
write_temp_file(tmp, "A.cls", cls);
1473014730
snprintf(cls, sizeof(cls), "Class Pkg.B Extends %%RegisteredObject\n%s", run_body);
1473114731
write_temp_file(tmp, "B.cls", cls);
14732+
/* A third, unrelated Run: a bare "Run" callee could weak-match it. */
14733+
write_temp_file(tmp, "util.py",
14734+
"def Run(x):\n"
14735+
" return x\n");
1473214736
write_temp_file(tmp, "caller.py",
1473314737
"import iris\n"
1473414738
"\n"
14739+
"class Helper:\n"
14740+
" def Run(self, x):\n"
14741+
" return x\n"
14742+
"\n"
1473514743
"def want_a():\n"
1473614744
" return iris.cls(\"Pkg.A\").Run(\"x\")\n"
1473714745
"\n"
@@ -14763,6 +14771,10 @@ TEST(pipeline_python_iris_cls_class_aware_calls) {
1476314771
ASSERT_EQ(iris_cls_calls(s, project, "want_a", NULL), 1);
1476414772
ASSERT_EQ(iris_cls_calls(s, project, "want_b", "Pkg.B.Run"), 1);
1476514773
ASSERT_EQ(iris_cls_calls(s, project, "want_b", NULL), 1);
14774+
ASSERT_EQ(iris_cls_calls(s, project, "want_a", "util.Run"), 0);
14775+
ASSERT_EQ(iris_cls_calls(s, project, "want_b", "util.Run"), 0);
14776+
ASSERT_EQ(iris_cls_calls(s, project, "want_a", "Helper.Run"), 0);
14777+
ASSERT_EQ(iris_cls_calls(s, project, "want_b", "Helper.Run"), 0);
1476614778
ASSERT_EQ(iris_cls_calls(s, project, "want_nonexistent", NULL), 0);
1476714779
ASSERT_EQ(iris_cls_calls(s, project, "unrelated_receiver", NULL), 0);
1476814780
ASSERT_EQ(iris_cls_calls(s, project, "via_classmethodvalue", "Pkg.B.Run"), 1);

0 commit comments

Comments
 (0)