Skip to content

Commit 8f803f7

Browse files
committed
fix(python): resolve iris.cls("Pkg.X").Method() to the named class
In IRIS embedded Python, iris.cls("Pkg.X").Method(...) names its target class in the receiver's string literal. There was no receiver path for it next to the classMethod* string dispatch, so after the weak-member-call suppression in b6a2284 these calls produced no CALLS edge at all. When a Python call's receiver is iris.cls() with a plain quoted literal as its first argument, also emit "<class-literal>.<method>" as a callee, the same way classMethodValue already does. Non-literal class arguments (variables, f-strings) name nothing and get no class-qualified callee. Also add invokeClassMethod to the Python dispatch suffixes. It takes the same (class_name, method_name, *args) arguments as classMethodValue. Tests: the DeusData#1260 repro as a pipeline test (two classes sharing Run, a nonexistent class, an unrelated receiver, classMethodValue and invokeClassMethod), plus extraction tests for the synthesized callee, non-literal and f-string arguments, and invokeClassMethod. Fixes DeusData#1260 Signed-off-by: Thomas Dyar <tdyar@intersystems.com>
1 parent 64c23fa commit 8f803f7

3 files changed

Lines changed: 222 additions & 2 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 57 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).
@@ -3911,6 +3955,17 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
39113955
}
39123956
}
39133957
}
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+
}
39143969
}
39153970
}
39163971

‎tests/test_extraction.c‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3265,6 +3265,67 @@ 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+
TEST(python_iris_cls_receiver_is_class_aware) {
3289+
CBMFileResult *r = extract("import iris\n"
3290+
"def want_a():\n"
3291+
" return iris.cls(\"Pkg.A\").Run(\"x\")\n"
3292+
"def want_b():\n"
3293+
" return iris.cls('Pkg.B').Run('x')\n"
3294+
"def dynamic(name):\n"
3295+
" return iris.cls(name).Run('x')\n"
3296+
"def fstring(n):\n"
3297+
" return iris.cls(f'Pkg.{n}').Run('x')\n"
3298+
"def unrelated_receiver():\n"
3299+
" return some_random_thing.Run('x')\n",
3300+
CBM_LANG_PYTHON, "t", "caller.py");
3301+
ASSERT_NOT_NULL(r);
3302+
ASSERT_FALSE(r->has_error);
3303+
ASSERT_EQ(count_calls_in_func(r, "Pkg.A.Run", "want_a"), 1);
3304+
ASSERT_EQ(count_calls_in_func(r, "Pkg.B.Run", "want_b"), 1);
3305+
ASSERT_EQ(count_calls_in_func(r, "Pkg.A.Run", "want_b"), 0);
3306+
/* A non-literal class argument names nothing: no class-qualified callee. */
3307+
ASSERT_EQ(count_calls_in_func(r, "name.Run", "dynamic"), 0);
3308+
/* An f-string is a Python "string" node but not a class name. */
3309+
ASSERT_EQ(count_calls_in_func(r, "f'Pkg.{n}'.Run", "fstring"), 0);
3310+
ASSERT_EQ(count_calls_in_func(r, "Pkg.{n}.Run", "fstring"), 0);
3311+
ASSERT_EQ(count_calls_in_func(r, "Pkg.A.Run", "unrelated_receiver"), 0);
3312+
ASSERT_EQ(count_calls_in_func(r, "Pkg.B.Run", "unrelated_receiver"), 0);
3313+
cbm_free_result(r);
3314+
PASS();
3315+
}
3316+
3317+
TEST(python_iris_invokeClassMethod) {
3318+
CBMFileResult *r =
3319+
extract("def call(db):\n"
3320+
" return db.invokeClassMethod('MyApp.Service', 'SomeClassMethod', 1)\n",
3321+
CBM_LANG_PYTHON, "t", "svc.py");
3322+
ASSERT_NOT_NULL(r);
3323+
ASSERT_FALSE(r->has_error);
3324+
ASSERT_EQ(count_calls_in_func(r, "MyApp.Service.SomeClassMethod", "call"), 1);
3325+
cbm_free_result(r);
3326+
PASS();
3327+
}
3328+
32683329
TEST(go_calls) {
32693330
CBMFileResult *r =
32703331
extract("package main\nimport \"fmt\"\nfunc main() { fmt.Println(\"hello\") }\n",
@@ -8535,6 +8596,8 @@ SUITE(extraction) {
85358596
/* Cross-cutting */
85368597
RUN_TEST(python_calls);
85378598
RUN_TEST(python_iris_classMethodValue);
8599+
RUN_TEST(python_iris_cls_receiver_is_class_aware);
8600+
RUN_TEST(python_iris_invokeClassMethod);
85388601
RUN_TEST(go_calls);
85398602
RUN_TEST(python_imports);
85408603
RUN_TEST(js_imports);

‎tests/test_pipeline.c‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14674,6 +14674,107 @@ TEST(pipeline_ensemble_routing_method_scoping) {
1467414674
PASS();
1467514675
}
1467614676

14677+
/* #1260: CALLS edges from the function named `func` whose target qn ends
14678+
* with ".<target_suffix>" (segment-anchored). A NULL suffix counts every
14679+
* CALLS edge to a node named "Run". */
14680+
static int iris_cls_calls(cbm_store_t *s, const char *project, const char *func,
14681+
const char *target_suffix) {
14682+
cbm_node_t *fn = NULL;
14683+
int fn_count = 0;
14684+
cbm_store_find_nodes_by_name(s, project, func, &fn, &fn_count);
14685+
int n = 0;
14686+
for (int fi = 0; fi < fn_count; fi++) {
14687+
cbm_edge_t *edges = NULL;
14688+
int ec = 0;
14689+
cbm_store_find_edges_by_source_type(s, fn[fi].id, "CALLS", &edges, &ec);
14690+
for (int ei = 0; ei < ec; ei++) {
14691+
cbm_node_t tgt = {0};
14692+
if (cbm_store_find_node_by_id(s, edges[ei].target_id, &tgt) != 0) {
14693+
continue;
14694+
}
14695+
if (!target_suffix) {
14696+
if (tgt.name && strcmp(tgt.name, "Run") == 0) {
14697+
n++;
14698+
}
14699+
} else if (tgt.qualified_name) {
14700+
size_t ql = strlen(tgt.qualified_name);
14701+
size_t sl = strlen(target_suffix);
14702+
if (ql > sl && tgt.qualified_name[ql - sl - 1] == '.' &&
14703+
strcmp(tgt.qualified_name + ql - sl, target_suffix) == 0) {
14704+
n++;
14705+
}
14706+
}
14707+
cbm_node_free_fields(&tgt);
14708+
}
14709+
cbm_store_free_edges(edges, ec);
14710+
}
14711+
cbm_store_free_nodes(fn, fn_count);
14712+
return n;
14713+
}
14714+
14715+
TEST(pipeline_python_iris_cls_class_aware_calls) {
14716+
/* The #1260 repro: two ObjectScript classes share a method name. Python
14717+
* callers must reach the class named in iris.cls("<literal>"), and a call
14718+
* that names no indexed class must not borrow an edge from one that does. */
14719+
char *tmp = th_mktempdir("cbm_iris_cls");
14720+
ASSERT_NOT_NULL(tmp);
14721+
const char *run_body = "{\n"
14722+
"ClassMethod Run(x As %String) As %String\n"
14723+
"{\n"
14724+
" Quit x\n"
14725+
"}\n"
14726+
"}\n";
14727+
char cls[256];
14728+
snprintf(cls, sizeof(cls), "Class Pkg.A Extends %%RegisteredObject\n%s", run_body);
14729+
write_temp_file(tmp, "A.cls", cls);
14730+
snprintf(cls, sizeof(cls), "Class Pkg.B Extends %%RegisteredObject\n%s", run_body);
14731+
write_temp_file(tmp, "B.cls", cls);
14732+
write_temp_file(tmp, "caller.py",
14733+
"import iris\n"
14734+
"\n"
14735+
"def want_a():\n"
14736+
" return iris.cls(\"Pkg.A\").Run(\"x\")\n"
14737+
"\n"
14738+
"def want_b():\n"
14739+
" return iris.cls(\"Pkg.B\").Run(\"x\")\n"
14740+
"\n"
14741+
"def want_nonexistent():\n"
14742+
" return iris.cls(\"Pkg.DoesNotExist\").Run(\"x\")\n"
14743+
"\n"
14744+
"def unrelated_receiver():\n"
14745+
" return some_random_thing.Run(\"x\")\n"
14746+
"\n"
14747+
"def via_classmethodvalue(db):\n"
14748+
" return db.classMethodValue(\"Pkg.B\", \"Run\", \"x\")\n"
14749+
"\n"
14750+
"def via_invoke(db):\n"
14751+
" return db.invokeClassMethod(\"Pkg.A\", \"Run\", \"x\")\n");
14752+
14753+
char db_path[512];
14754+
snprintf(db_path, sizeof(db_path), "%s/iris_cls.db", tmp);
14755+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
14756+
ASSERT_NOT_NULL(p);
14757+
ASSERT_EQ(cbm_pipeline_run(p), 0);
14758+
cbm_store_t *s = cbm_store_open_path(db_path);
14759+
ASSERT_NOT_NULL(s);
14760+
const char *project = cbm_pipeline_project_name(p);
14761+
14762+
ASSERT_EQ(iris_cls_calls(s, project, "want_a", "Pkg.A.Run"), 1);
14763+
ASSERT_EQ(iris_cls_calls(s, project, "want_a", NULL), 1);
14764+
ASSERT_EQ(iris_cls_calls(s, project, "want_b", "Pkg.B.Run"), 1);
14765+
ASSERT_EQ(iris_cls_calls(s, project, "want_b", NULL), 1);
14766+
ASSERT_EQ(iris_cls_calls(s, project, "want_nonexistent", NULL), 0);
14767+
ASSERT_EQ(iris_cls_calls(s, project, "unrelated_receiver", NULL), 0);
14768+
ASSERT_EQ(iris_cls_calls(s, project, "via_classmethodvalue", "Pkg.B.Run"), 1);
14769+
ASSERT_EQ(iris_cls_calls(s, project, "via_invoke", "Pkg.A.Run"), 1);
14770+
ASSERT_EQ(iris_cls_calls(s, project, "via_invoke", NULL), 1);
14771+
14772+
cbm_store_close(s);
14773+
cbm_pipeline_free(p);
14774+
th_rmtree(tmp);
14775+
PASS();
14776+
}
14777+
1467714778
/* #518/#519 item-7 regression: the DELTA merge is the warm path most users
1467814779
* hit. It used to write nodes_fts with a hand-rolled four-column INSERT of
1467914780
* its own; with a fifth `body` column that literal leaves prose NULL for every
@@ -15428,6 +15529,7 @@ SUITE(pipeline) {
1542815529
/* Ensemble routing pass */
1542915530
RUN_TEST(pipeline_ensemble_routing_edges);
1543015531
RUN_TEST(pipeline_ensemble_routing_method_scoping);
15532+
RUN_TEST(pipeline_python_iris_cls_class_aware_calls);
1543115533
RUN_TEST(pipeline_ensemble_routing_attr_does_not_leak_across_items);
1543215534
RUN_TEST(pipeline_ensemble_routing_settings_targets);
1543315535
RUN_TEST(pipeline_ensemble_routing_unterminated_item_is_safe);

0 commit comments

Comments
 (0)