Skip to content

Commit 64cf275

Browse files
authored
Merge pull request DeusData#2367 from isc-tdyar/fix/iris-cls-dispatch
fix(python): resolve iris.cls("Pkg.X").Method() to the named class
2 parents 9d65f4c + 075f40e commit 64cf275

3 files changed

Lines changed: 259 additions & 2 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ static const char *strip_quotes(CBMArena *a, const char *text) {
7979

8080
// Callee suffixes for IRIS Python interop string-dispatch. Kept at file scope
8181
// (not inside the function) to satisfy cppcheck variableScope.
82-
static const char *s_py_dispatch_suffixes[] = {".classMethodValue", ".classMethodVoid",
83-
".classMethodBoolean", ".classMethodObject", NULL};
82+
static const char *s_py_dispatch_suffixes[] = {".classMethodValue", ".classMethodVoid",
83+
".classMethodBoolean", ".classMethodObject",
84+
".invokeClassMethod", NULL};
8485

8586
// Per-language callee-suffix dispatch table — returns a NULL-terminated list of
8687
// method-name suffixes whose calls should be resolved by extracting class+method
@@ -2039,6 +2040,49 @@ static const char *extract_nth_string_arg(CBMExtractCtx *ctx, TSNode args, uint3
20392040
return NULL;
20402041
}
20412042

2043+
// IRIS embedded Python names the target class in the receiver:
2044+
// iris.cls("Pkg.Class").Method(...) calls ClassMethod Pkg.Class.Method.
2045+
// Returns "Pkg.Class.Method", or NULL unless the receiver is iris.cls() with
2046+
// a string-literal first argument. A non-literal class names nothing, so it
2047+
// gets no class-qualified callee rather than a guessed one.
2048+
static const char *python_iris_cls_callee(CBMExtractCtx *ctx, TSNode call_node) {
2049+
TSNode fn = ts_node_child_by_field_name(call_node, TS_FIELD("function"));
2050+
if (ts_node_is_null(fn) || strcmp(ts_node_type(fn), "attribute") != 0) {
2051+
return NULL;
2052+
}
2053+
TSNode recv = ts_node_child_by_field_name(fn, TS_FIELD("object"));
2054+
TSNode meth = ts_node_child_by_field_name(fn, TS_FIELD("attribute"));
2055+
if (ts_node_is_null(recv) || ts_node_is_null(meth) || strcmp(ts_node_type(recv), "call") != 0) {
2056+
return NULL;
2057+
}
2058+
TSNode recv_fn = ts_node_child_by_field_name(recv, TS_FIELD("function"));
2059+
TSNode recv_args = ts_node_child_by_field_name(recv, TS_FIELD("arguments"));
2060+
if (ts_node_is_null(recv_fn) || ts_node_is_null(recv_args) ||
2061+
ts_node_named_child_count(recv_args) == 0) {
2062+
return NULL;
2063+
}
2064+
const char *recv_text = cbm_node_text(ctx->arena, recv_fn, ctx->source);
2065+
if (!recv_text || strcmp(recv_text, "iris.cls") != 0) {
2066+
return NULL;
2067+
}
2068+
TSNode cls_arg = ts_node_named_child(recv_args, 0);
2069+
if (!is_string_like(ts_node_type(cls_arg))) {
2070+
return NULL;
2071+
}
2072+
// Plain quoted literals only: an f-string or other prefixed string is a
2073+
// "string" node too, but its text is not a class name.
2074+
char *cls_text = cbm_node_text(ctx->arena, cls_arg, ctx->source);
2075+
if (!cls_text || (cls_text[0] != '"' && cls_text[0] != '\'')) {
2076+
return NULL;
2077+
}
2078+
const char *cls = strip_and_validate_string_arg(ctx->arena, cls_text);
2079+
const char *mth = cbm_node_text(ctx->arena, meth, ctx->source);
2080+
if (!cls || !cls[0] || !mth || !mth[0]) {
2081+
return NULL;
2082+
}
2083+
return cbm_arena_sprintf(ctx->arena, "%s.%s", cls, mth);
2084+
}
2085+
20422086
// --- Unified handler: called once per node by the cursor walk ---
20432087

20442088
// Process a keyword argument (keyword_argument or pair node).
@@ -3887,6 +3931,16 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
38873931
}
38883932
}
38893933

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+
}
38903944
cbm_calls_push(&ctx->result->calls, ctx->arena, call);
38913945
invocation = describe_emitted_primary_call(node, &callee);
38923946

‎tests/test_extraction.c‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3265,6 +3265,93 @@ TEST(python_iris_classMethodValue) {
32653265
PASS();
32663266
}
32673267

3268+
/* #1260: count calls with an exact callee whose enclosing function qn ends
3269+
* with ".<func>". has_call() is a substring match, which cannot tell
3270+
* "Pkg.A.Run" emitted from want_a apart from the same callee in want_b. */
3271+
static int count_calls_in_func(CBMFileResult *r, const char *callee, const char *func) {
3272+
int n = 0;
3273+
size_t flen = strlen(func);
3274+
for (int i = 0; i < r->calls.count; i++) {
3275+
const CBMCall *c = &r->calls.items[i];
3276+
if (!c->callee_name || strcmp(c->callee_name, callee) != 0 || !c->enclosing_func_qn) {
3277+
continue;
3278+
}
3279+
size_t qlen = strlen(c->enclosing_func_qn);
3280+
if (qlen > flen && c->enclosing_func_qn[qlen - flen - 1] == '.' &&
3281+
strcmp(c->enclosing_func_qn + qlen - flen, func) == 0) {
3282+
n++;
3283+
}
3284+
}
3285+
return n;
3286+
}
3287+
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+
3310+
TEST(python_iris_cls_receiver_is_class_aware) {
3311+
CBMFileResult *r = extract("import iris\n"
3312+
"def want_a():\n"
3313+
" return iris.cls(\"Pkg.A\").Run(\"x\")\n"
3314+
"def want_b():\n"
3315+
" return iris.cls('Pkg.B').Run('x')\n"
3316+
"def dynamic(name):\n"
3317+
" return iris.cls(name).Run('x')\n"
3318+
"def fstring(n):\n"
3319+
" return iris.cls(f'Pkg.{n}').Run('x')\n"
3320+
"def unrelated_receiver():\n"
3321+
" return some_random_thing.Run('x')\n",
3322+
CBM_LANG_PYTHON, "t", "caller.py");
3323+
ASSERT_NOT_NULL(r);
3324+
ASSERT_FALSE(r->has_error);
3325+
ASSERT_EQ(count_calls_in_func(r, "Pkg.A.Run", "want_a"), 1);
3326+
ASSERT_EQ(count_calls_in_func(r, "Pkg.B.Run", "want_b"), 1);
3327+
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);
3332+
/* A non-literal class argument names nothing: no class-qualified callee. */
3333+
ASSERT_EQ(count_calls_in_func(r, "name.Run", "dynamic"), 0);
3334+
/* An f-string is a Python "string" node but not a class name. */
3335+
ASSERT_EQ(count_calls_in_func(r, "f'Pkg.{n}'.Run", "fstring"), 0);
3336+
ASSERT_EQ(count_calls_in_func(r, "Pkg.{n}.Run", "fstring"), 0);
3337+
ASSERT_EQ(count_calls_in_func(r, "Pkg.A.Run", "unrelated_receiver"), 0);
3338+
ASSERT_EQ(count_calls_in_func(r, "Pkg.B.Run", "unrelated_receiver"), 0);
3339+
cbm_free_result(r);
3340+
PASS();
3341+
}
3342+
3343+
TEST(python_iris_invokeClassMethod) {
3344+
CBMFileResult *r =
3345+
extract("def call(db):\n"
3346+
" return db.invokeClassMethod('MyApp.Service', 'SomeClassMethod', 1)\n",
3347+
CBM_LANG_PYTHON, "t", "svc.py");
3348+
ASSERT_NOT_NULL(r);
3349+
ASSERT_FALSE(r->has_error);
3350+
ASSERT_EQ(count_calls_in_func(r, "MyApp.Service.SomeClassMethod", "call"), 1);
3351+
cbm_free_result(r);
3352+
PASS();
3353+
}
3354+
32683355
TEST(go_calls) {
32693356
CBMFileResult *r =
32703357
extract("package main\nimport \"fmt\"\nfunc main() { fmt.Println(\"hello\") }\n",
@@ -8667,6 +8754,8 @@ SUITE(extraction) {
86678754
/* Cross-cutting */
86688755
RUN_TEST(python_calls);
86698756
RUN_TEST(python_iris_classMethodValue);
8757+
RUN_TEST(python_iris_cls_receiver_is_class_aware);
8758+
RUN_TEST(python_iris_invokeClassMethod);
86708759
RUN_TEST(go_calls);
86718760
RUN_TEST(python_imports);
86728761
RUN_TEST(js_imports);

‎tests/test_pipeline.c‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14711,6 +14711,119 @@ TEST(pipeline_ensemble_routing_method_scoping) {
1471114711
PASS();
1471214712
}
1471314713

14714+
/* #1260: CALLS edges from the function named `func` whose target qn ends
14715+
* with ".<target_suffix>" (segment-anchored). A NULL suffix counts every
14716+
* CALLS edge to a node named "Run". */
14717+
static int iris_cls_calls(cbm_store_t *s, const char *project, const char *func,
14718+
const char *target_suffix) {
14719+
cbm_node_t *fn = NULL;
14720+
int fn_count = 0;
14721+
cbm_store_find_nodes_by_name(s, project, func, &fn, &fn_count);
14722+
int n = 0;
14723+
for (int fi = 0; fi < fn_count; fi++) {
14724+
cbm_edge_t *edges = NULL;
14725+
int ec = 0;
14726+
cbm_store_find_edges_by_source_type(s, fn[fi].id, "CALLS", &edges, &ec);
14727+
for (int ei = 0; ei < ec; ei++) {
14728+
cbm_node_t tgt = {0};
14729+
if (cbm_store_find_node_by_id(s, edges[ei].target_id, &tgt) != 0) {
14730+
continue;
14731+
}
14732+
if (!target_suffix) {
14733+
if (tgt.name && strcmp(tgt.name, "Run") == 0) {
14734+
n++;
14735+
}
14736+
} else if (tgt.qualified_name) {
14737+
size_t ql = strlen(tgt.qualified_name);
14738+
size_t sl = strlen(target_suffix);
14739+
if (ql > sl && tgt.qualified_name[ql - sl - 1] == '.' &&
14740+
strcmp(tgt.qualified_name + ql - sl, target_suffix) == 0) {
14741+
n++;
14742+
}
14743+
}
14744+
cbm_node_free_fields(&tgt);
14745+
}
14746+
cbm_store_free_edges(edges, ec);
14747+
}
14748+
cbm_store_free_nodes(fn, fn_count);
14749+
return n;
14750+
}
14751+
14752+
TEST(pipeline_python_iris_cls_class_aware_calls) {
14753+
/* The #1260 repro: two ObjectScript classes share a method name. Python
14754+
* callers must reach the class named in iris.cls("<literal>"), and a call
14755+
* that names no indexed class must not borrow an edge from one that does. */
14756+
char *tmp = th_mktempdir("cbm_iris_cls");
14757+
ASSERT_NOT_NULL(tmp);
14758+
const char *run_body = "{\n"
14759+
"ClassMethod Run(x As %String) As %String\n"
14760+
"{\n"
14761+
" Quit x\n"
14762+
"}\n"
14763+
"}\n";
14764+
char cls[256];
14765+
snprintf(cls, sizeof(cls), "Class Pkg.A Extends %%RegisteredObject\n%s", run_body);
14766+
write_temp_file(tmp, "A.cls", cls);
14767+
snprintf(cls, sizeof(cls), "Class Pkg.B Extends %%RegisteredObject\n%s", run_body);
14768+
write_temp_file(tmp, "B.cls", cls);
14769+
/* A third, unrelated Run: a bare "Run" callee could weak-match it. */
14770+
write_temp_file(tmp, "util.py",
14771+
"def Run(x):\n"
14772+
" return x\n");
14773+
write_temp_file(tmp, "caller.py",
14774+
"import iris\n"
14775+
"\n"
14776+
"class Helper:\n"
14777+
" def Run(self, x):\n"
14778+
" return x\n"
14779+
"\n"
14780+
"def want_a():\n"
14781+
" return iris.cls(\"Pkg.A\").Run(\"x\")\n"
14782+
"\n"
14783+
"def want_b():\n"
14784+
" return iris.cls(\"Pkg.B\").Run(\"x\")\n"
14785+
"\n"
14786+
"def want_nonexistent():\n"
14787+
" return iris.cls(\"Pkg.DoesNotExist\").Run(\"x\")\n"
14788+
"\n"
14789+
"def unrelated_receiver():\n"
14790+
" return some_random_thing.Run(\"x\")\n"
14791+
"\n"
14792+
"def via_classmethodvalue(db):\n"
14793+
" return db.classMethodValue(\"Pkg.B\", \"Run\", \"x\")\n"
14794+
"\n"
14795+
"def via_invoke(db):\n"
14796+
" return db.invokeClassMethod(\"Pkg.A\", \"Run\", \"x\")\n");
14797+
14798+
char db_path[512];
14799+
snprintf(db_path, sizeof(db_path), "%s/iris_cls.db", tmp);
14800+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
14801+
ASSERT_NOT_NULL(p);
14802+
ASSERT_EQ(cbm_pipeline_run(p), 0);
14803+
cbm_store_t *s = cbm_store_open_path(db_path);
14804+
ASSERT_NOT_NULL(s);
14805+
const char *project = cbm_pipeline_project_name(p);
14806+
14807+
ASSERT_EQ(iris_cls_calls(s, project, "want_a", "Pkg.A.Run"), 1);
14808+
ASSERT_EQ(iris_cls_calls(s, project, "want_a", NULL), 1);
14809+
ASSERT_EQ(iris_cls_calls(s, project, "want_b", "Pkg.B.Run"), 1);
14810+
ASSERT_EQ(iris_cls_calls(s, project, "want_b", NULL), 1);
14811+
ASSERT_EQ(iris_cls_calls(s, project, "want_a", "util.Run"), 0);
14812+
ASSERT_EQ(iris_cls_calls(s, project, "want_b", "util.Run"), 0);
14813+
ASSERT_EQ(iris_cls_calls(s, project, "want_a", "Helper.Run"), 0);
14814+
ASSERT_EQ(iris_cls_calls(s, project, "want_b", "Helper.Run"), 0);
14815+
ASSERT_EQ(iris_cls_calls(s, project, "want_nonexistent", NULL), 0);
14816+
ASSERT_EQ(iris_cls_calls(s, project, "unrelated_receiver", NULL), 0);
14817+
ASSERT_EQ(iris_cls_calls(s, project, "via_classmethodvalue", "Pkg.B.Run"), 1);
14818+
ASSERT_EQ(iris_cls_calls(s, project, "via_invoke", "Pkg.A.Run"), 1);
14819+
ASSERT_EQ(iris_cls_calls(s, project, "via_invoke", NULL), 1);
14820+
14821+
cbm_store_close(s);
14822+
cbm_pipeline_free(p);
14823+
th_rmtree(tmp);
14824+
PASS();
14825+
}
14826+
1471414827
/* #518/#519 item-7 regression: the DELTA merge is the warm path most users
1471514828
* hit. It used to write nodes_fts with a hand-rolled four-column INSERT of
1471614829
* its own; with a fifth `body` column that literal leaves prose NULL for every
@@ -15465,6 +15578,7 @@ SUITE(pipeline) {
1546515578
/* Ensemble routing pass */
1546615579
RUN_TEST(pipeline_ensemble_routing_edges);
1546715580
RUN_TEST(pipeline_ensemble_routing_method_scoping);
15581+
RUN_TEST(pipeline_python_iris_cls_class_aware_calls);
1546815582
RUN_TEST(pipeline_ensemble_routing_attr_does_not_leak_across_items);
1546915583
RUN_TEST(pipeline_ensemble_routing_settings_targets);
1547015584
RUN_TEST(pipeline_ensemble_routing_unterminated_item_is_safe);

0 commit comments

Comments
 (0)