def dump(self, filename: Path) -> None:
        """Dump the grammar tables to a pickle file."""

        # mypyc generates objects that don't have a __dict__, but they
        # do have __getstate__ methods that will return an equivalent
        # dictionary
        if hasattr(self, "__dict__"):
            d = self.__dict__
        else:
            d = self.__getstate__()  # type: ignore

        with tempfile.NamedTemporaryFile(
            dir=os.path.dirname(filename), delete=False
        ) as f:
            pickle.dump(d, f, pickle.HIGHEST_PROTOCOL)
        os.replace(f.name, filename)
Dump the grammar tables to a pickle file.
src/blib2to3/pgen2/grammar.py