Delete `names` from kernel, except for `exclude_defs`
(
self,
variables: dict[Name, list[VariableData]],
exclude_defs: set[Name],
)
| 926 | return previous_children - children, error |
| 927 | |
| 928 | def _delete_variables( |
| 929 | self, |
| 930 | variables: dict[Name, list[VariableData]], |
| 931 | exclude_defs: set[Name], |
| 932 | ) -> None: |
| 933 | """Delete `names` from kernel, except for `exclude_defs`""" |
| 934 | for name, variable_data in variables.items(): |
| 935 | # Take the last definition of the variable |
| 936 | variable = variable_data[-1] |
| 937 | if name in exclude_defs: |
| 938 | continue |
| 939 | |
| 940 | if variable.kind == "table" and DependencyManager.duckdb.has(): |
| 941 | import duckdb |
| 942 | |
| 943 | # We only drop in-memory tables: we don't want to drop tables |
| 944 | # on databases! |
| 945 | try: |
| 946 | duckdb.execute(f"DROP TABLE IF EXISTS memory.main.{name}") |
| 947 | except Exception as e: |
| 948 | LOGGER.warning("Failed to drop table %s: %s", name, str(e)) |
| 949 | elif variable.kind == "view" and DependencyManager.duckdb.has(): |
| 950 | import duckdb |
| 951 | |
| 952 | # We only drop in-memory views for the same reason. |
| 953 | try: |
| 954 | duckdb.execute(f"DROP VIEW IF EXISTS memory.main.{name}") |
| 955 | except Exception as e: |
| 956 | LOGGER.warning("Failed to drop view %s: %s", name, str(e)) |
| 957 | elif variable.kind == "catalog" and DependencyManager.duckdb.has(): |
| 958 | import duckdb |
| 959 | |
| 960 | try: |
| 961 | duckdb.execute(f"DETACH DATABASE IF EXISTS {name}") |
| 962 | except Exception as e: |
| 963 | LOGGER.warning( |
| 964 | "Failed to detach catalog %s: %s", name, str(e) |
| 965 | ) |
| 966 | else: |
| 967 | if name in self.globals: |
| 968 | del self.globals[name] |
| 969 | |
| 970 | # Restore module-level __doc__ so it doesn't fall |
| 971 | # through to builtins.__doc__ |
| 972 | if name == "__doc__": |
| 973 | self.globals["__doc__"] = ( |
| 974 | self.app_metadata.docstring |
| 975 | or "Created for the marimo kernel." |
| 976 | ) |
| 977 | |
| 978 | if ( |
| 979 | "__annotations__" in self.globals |
| 980 | and name in self.globals["__annotations__"] |
| 981 | ): |
| 982 | del self.globals["__annotations__"][name] |
| 983 | |
| 984 | def _invalidate_cell_state( |
| 985 | self, |
no test coverage detected