@@ -12242,6 +12242,95 @@ TEST(python_namespace_facts_type_checking_alternatives) {
1224212242 PASS();
1224312243}
1224412244
12245+ /* A string-literal statement (a module, class or attribute docstring) has no
12246+ * effect, and `from __future__ import x` binds x like any from-import. Neither
12247+ * may withdraw the names bound before it: sqlalchemy's sql/expression.py lost
12248+ * its 149 re-exports to `nullsfirst = nulls_first` followed by a triple-quoted
12249+ * docstring. An f-string with a replacement field can run code
12250+ * (__format__ / __str__) and stays an effect. */
12251+ static bool py_facts_any_unknown(const CBMFileResult *r, CBMPyNamespaceUnknownReason reason) {
12252+ for (int i = 0; r && i < r->py_namespace.count; i++) {
12253+ if (r->py_namespace.items[i].kind == CBM_PY_NS_UNKNOWN &&
12254+ r->py_namespace.items[i].reason == reason)
12255+ return true;
12256+ }
12257+ return false;
12258+ }
12259+
12260+ TEST(python_namespace_facts_string_statements_and_future_imports) {
12261+ static const char source[] = "\"\"\"Module docstring.\"\"\"\n"
12262+ "from __future__ import annotations\n"
12263+ "from __future__ import (division as div, generator_stop)\n"
12264+ "from .impl import a\n"
12265+ "\"\"\"A triple-quoted string after an import.\"\"\"\n"
12266+ "'''Single-quoted triple.'''\n"
12267+ "r\"raw \\d\"\n"
12268+ "b\"bytes\"\n"
12269+ "rb'raw bytes'\n"
12270+ "u\"unicode\"\n"
12271+ "\"implicit\" 'concatenation'\n"
12272+ "f\"no replacement field {{escaped}}\"\n"
12273+ "from .impl import b\n"
12274+ "class Documented:\n"
12275+ " \"\"\"Class docstring.\"\"\"\n"
12276+ " def method(self):\n"
12277+ " pass\n"
12278+ "x = a\n"
12279+ "\"\"\"Attribute docstring.\"\"\"\n";
12280+ CBMFileResult *r = extract(source, CBM_LANG_PYTHON, "t", "pkg/__init__.py");
12281+ bool ok = r && !r->has_error && r->py_namespace.status == CBM_PY_NS_COMPLETE &&
12282+ cbm_py_namespace_facts_valid(&r->py_namespace) && r->py_namespace.count == 7;
12283+ if (ok) {
12284+ const CBMPyNamespaceFact *f = r->py_namespace.items;
12285+ ok = f[0].kind == CBM_PY_NS_IMPORT_NAME && strcmp(f[0].module_name, "__future__") == 0 &&
12286+ f[0].relative_level == 0 && strcmp(f[0].member_name, "annotations") == 0 &&
12287+ strcmp(f[0].local_name, "annotations") == 0 && f[1].kind == CBM_PY_NS_IMPORT_NAME &&
12288+ strcmp(f[1].member_name, "division") == 0 && strcmp(f[1].local_name, "div") == 0 &&
12289+ f[2].kind == CBM_PY_NS_IMPORT_NAME && strcmp(f[2].local_name, "generator_stop") == 0 &&
12290+ f[3].kind == CBM_PY_NS_IMPORT_NAME && strcmp(f[3].local_name, "a") == 0 &&
12291+ f[4].kind == CBM_PY_NS_IMPORT_NAME && strcmp(f[4].local_name, "b") == 0 &&
12292+ f[5].kind == CBM_PY_NS_OWN_DEF && f[5].def_kind == CBM_PY_NS_DEF_CLASS &&
12293+ strcmp(f[5].local_name, "Documented") == 0 && f[6].kind == CBM_PY_NS_SHADOW &&
12294+ strcmp(f[6].local_name, "x") == 0;
12295+ }
12296+ if (!ok && r) {
12297+ for (int i = 0; i < r->py_namespace.count; i++) {
12298+ const CBMPyNamespaceFact *f = &r->py_namespace.items[i];
12299+ printf(" string/future fact %d: kind %d reason %d name %s\n", i, (int)f->kind,
12300+ (int)f->reason, f->local_name ? f->local_name : "-");
12301+ }
12302+ }
12303+ if (r)
12304+ cbm_free_result(r);
12305+ ASSERT_TRUE(ok); /* RED while a docstring is an effect and __future__ is unsupported */
12306+
12307+ /* Controls: each stays a namespace barrier. */
12308+ static const struct {
12309+ const char *name;
12310+ const char *source;
12311+ CBMPyNamespaceUnknownReason reason;
12312+ } barriers[] = {
12313+ {"f-string field", "from .impl import a\nf\"{a}\"\n", CBM_PY_NS_REASON_EFFECT},
12314+ {"concatenated f-string field", "from .impl import a\n\"x\" f\"{a!r}\"\n",
12315+ CBM_PY_NS_REASON_EFFECT},
12316+ {"call", "from .impl import a\nprint(a)\n", CBM_PY_NS_REASON_EFFECT},
12317+ {"star from __future__", "from .impl import a\nfrom __future__ import *\n",
12318+ CBM_PY_NS_REASON_UNSUPPORTED},
12319+ };
12320+ for (size_t i = 0; i < sizeof(barriers) / sizeof(barriers[0]); i++) {
12321+ r = extract(barriers[i].source, CBM_LANG_PYTHON, "t", "pkg/__init__.py");
12322+ ok = r && r->py_namespace.status == CBM_PY_NS_COMPLETE &&
12323+ cbm_py_namespace_facts_valid(&r->py_namespace) &&
12324+ py_facts_any_unknown(r, barriers[i].reason);
12325+ if (!ok)
12326+ printf(" control '%s' is no namespace barrier\n", barriers[i].name);
12327+ if (r)
12328+ cbm_free_result(r);
12329+ ASSERT_TRUE(ok);
12330+ }
12331+ PASS();
12332+ }
12333+
1224512334TEST(python_namespace_facts_distinguish_all_operations_and_unknowns) {
1224612335 static const char source[] = "__all__ = ['old']; __all__ = []\n"
1224712336 "__all__ = dynamic\n"
@@ -12629,6 +12718,7 @@ SUITE(extraction) {
1262912718 RUN_TEST(python_namespace_facts_unknown_effects_and_body_stability);
1263012719 RUN_TEST(python_namespace_facts_parse_errors_are_complete_unknown);
1263112720 RUN_TEST(python_namespace_facts_type_checking_alternatives);
12721+ RUN_TEST(python_namespace_facts_string_statements_and_future_imports);
1263212722 RUN_TEST(python_namespace_facts_capture_and_copy_fail_atomically);
1263312723 RUN_TEST(python_namespace_facts_validation_rejects_partial_and_unknown_schema);
1263412724 RUN_TEST(python_namespace_facts_compact_spill_and_copy_keep_owned_payload);
0 commit comments