Skip to content

Instantly share code, notes, and snippets.

@carljm
Created September 13, 2023 14:47
Show Gist options
  • Save carljm/ecf76f8d0b4d77b00a5ae79b93d1418a to your computer and use it in GitHub Desktop.
Save carljm/ecf76f8d0b4d77b00a5ae79b93d1418a to your computer and use it in GitHub Desktop.

Revisions

  1. carljm created this gist Sep 13, 2023.
    83 changes: 83 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,83 @@
    void _PyST_Dump(PySTEntryObject* ste, PyObject* prefix)
    {
    const char *blocktype;
    switch(ste->ste_type) {
    case FunctionBlock: blocktype = "FunctionBlock"; break;
    case ClassBlock: blocktype = "ClassBlock"; break;
    case ModuleBlock: blocktype = "ModuleBlock"; break;
    case AnnotationBlock: blocktype = "AnnotationBlock"; break;
    case TypeVarBoundBlock: blocktype = "TypeVarBoundBlock"; break;
    case TypeAliasBlock: blocktype = "TypeAliasBlock"; break;
    case TypeParamBlock: blocktype = "TypeParamBlock"; break;
    }
    PyObject* msg = PyUnicode_FromFormat(
    (
    "%U=== Symtable for %U ===\n"
    "%U%s\n"
    "%U%s%s%s%s%s%s%s%s%s%s%s%s\n"
    "%Ulineno: %d col_offset: %d\n"
    "%U--- Symbols ---\n"
    ),
    prefix,
    ste->ste_name,
    prefix,
    blocktype,
    prefix,
    ste->ste_free ? " free" : "",
    ste->ste_child_free ? " child_free" : "",
    ste->ste_generator ? " generator" : "",
    ste->ste_coroutine ? " coroutine" : "",
    ste->ste_varargs ? " varargs" : "",
    ste->ste_varkeywords ? " varkeywords" : "",
    ste->ste_returns_value ? " returns_value" : "",
    ste->ste_needs_class_closure ? " needs_class_closure" : "",
    ste->ste_needs_classdict ? " needs_classdict" : "",
    ste->ste_comp_inlined ? " comp_inlined" : "",
    ste->ste_comp_iter_target ? " comp_iter_target" : "",
    ste->ste_can_see_class_scope ? " can_see_class_scope" : "",
    prefix,
    ste->ste_lineno,
    ste->ste_col_offset,
    prefix
    );
    printf(PyUnicode_AsUTF8(msg));
    PyObject *name, *value;
    Py_ssize_t pos = 0;
    while(PyDict_Next(ste->ste_symbols, &pos, &name, &value)) {
    int scope = _PyST_GetScope(ste, name);
    long flags = _PyST_GetSymbol(ste, name);
    printf("%s %s: ", PyUnicode_AsUTF8(prefix), PyUnicode_AsUTF8(name));
    if (flags & DEF_GLOBAL) printf(" DEF_GLOBAL");
    if (flags & DEF_LOCAL) printf(" DEF_LOCAL");
    if (flags & DEF_PARAM) printf(" DEF_PARAM");
    if (flags & DEF_NONLOCAL) printf(" DEF_NONLOCAL");
    if (flags & USE) printf(" USE");
    if (flags & DEF_FREE) printf(" DEF_FREE");
    if (flags & DEF_FREE_CLASS) printf(" DEF_FREE_CLASS");
    if (flags & DEF_IMPORT) printf(" DEF_IMPORT");
    if (flags & DEF_ANNOT) printf(" DEF_ANNOT");
    if (flags & DEF_COMP_ITER) printf(" DEF_COMP_ITER");
    if (flags & DEF_TYPE_PARAM) printf(" DEF_TYPE_PARAM");
    if (flags & DEF_COMP_CELL) printf(" DEF_COMP_CELL");
    switch (scope) {
    case LOCAL: printf(" LOCAL"); break;
    case GLOBAL_EXPLICIT: printf(" GLOBAL_EXPLICIT"); break;
    case GLOBAL_IMPLICIT: printf(" GLOBAL_IMPLICIT"); break;
    case FREE: printf(" FREE"); break;
    case CELL: printf(" CELL"); break;
    }
    printf("\n");
    }
    printf("%s--- Children ---\n", PyUnicode_AsUTF8(prefix));
    PyObject *new_prefix = PyUnicode_FromFormat(" %U", prefix);
    for (Py_ssize_t i = 0; i < PyList_GET_SIZE(ste->ste_children); i++) {
    PyObject *child = PyList_GetItem(ste->ste_children, i);
    assert(PySTEntry_Check(child));
    _PyST_Dump((PySTEntryObject*)child, new_prefix);
    }
    }

    void PyST_Dump(PySTEntryObject* ste)
    {
    _PyST_Dump(ste, PyUnicode_FromString(""));
    }