Flush all the alive leaves in object tree and close the file.
(self)
| 2884 | self._flush_file(0) # 0 means local scope, 1 global (virtual) scope |
| 2885 | |
| 2886 | def close(self) -> None: |
| 2887 | """Flush all the alive leaves in object tree and close the file.""" |
| 2888 | # If the file is already closed, return immediately |
| 2889 | if not self.isopen: |
| 2890 | return |
| 2891 | |
| 2892 | # If this file has been opened more than once, decrease the |
| 2893 | # counter and return |
| 2894 | if self._open_count > 1: |
| 2895 | self._open_count -= 1 |
| 2896 | return |
| 2897 | |
| 2898 | filename = self.filename |
| 2899 | |
| 2900 | if self._undoEnabled and self._iswritable(): |
| 2901 | # Save the current mark and current action |
| 2902 | self._actionlog.attrs._g__setattr("CURMARK", self._curmark) |
| 2903 | self._actionlog.attrs._g__setattr("CURACTION", self._curaction) |
| 2904 | |
| 2905 | # Close all loaded nodes. |
| 2906 | self.root._f_close() |
| 2907 | |
| 2908 | self._node_manager.shutdown() |
| 2909 | |
| 2910 | # Post-conditions |
| 2911 | assert ( |
| 2912 | len(self._node_manager.cache) == 0 |
| 2913 | ), "cached nodes remain after closing: %s" % list( |
| 2914 | self._node_manager.cache |
| 2915 | ) |
| 2916 | |
| 2917 | # No other nodes should have been revived. |
| 2918 | assert ( |
| 2919 | len(self._node_manager.registry) == 0 |
| 2920 | ), "alive nodes remain after closing: %s" % list( |
| 2921 | self._node_manager.registry |
| 2922 | ) |
| 2923 | |
| 2924 | # Close the file |
| 2925 | self._close_file() |
| 2926 | |
| 2927 | # After the objects are disconnected, destroy the |
| 2928 | # object dictionary using the brute force ;-) |
| 2929 | # This should help to the garbage collector |
| 2930 | self.__dict__.clear() |
| 2931 | |
| 2932 | # Set the flag to indicate that the file is closed |
| 2933 | self.isopen = 0 |
| 2934 | |
| 2935 | # Restore the filename attribute that is used by _FileRegistry |
| 2936 | self.filename = filename |
| 2937 | |
| 2938 | # Delete the entry from the registry of opened files |
| 2939 | _open_files.remove(self) |
| 2940 | |
| 2941 | def __enter__(self) -> File: |
| 2942 | """Enter a context and return the same file.""" |
no test coverage detected