-
- def dump(self, filename):
- """Dump the grammar tables to a pickle file.
-
- dump() recursively changes all dict to OrderedDict, so the pickled file
- is not exactly the same as what was passed in to dump(). load() uses the
- pickled file to create the tables, but only changes OrderedDict to dict
- at the top level; it does not recursively change OrderedDict to dict.
- So, the loaded tables are different from the original tables that were
- passed to load() in that some of the OrderedDict (from the pickled file)
- are not changed back to dict. For parsing, this has no effect on
- performance because OrderedDict uses dict's __getitem__ with nothing in
- between.
- """
- with open(filename, "wb") as f:
- d = _make_deterministic(self.__dict__)
- pickle.dump(d, f, 2)
-
- def load(self, filename):
+ # Python 3.7+ parses async as a keyword, not an identifier
+ self.async_keywords = False
+
+ 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)
+
+ def _update(self, attrs: Dict[str, Any]) -> None:
+ for k, v in attrs.items():
+ setattr(self, k, v)
+
+ def load(self, filename: Path) -> None: