diff --git a/benchmark/run.py b/benchmark/run.py index 5120913..783aa07 100644 --- a/benchmark/run.py +++ b/benchmark/run.py @@ -1,10 +1,9 @@ import timeit from pathlib import Path -_parsers = [] import pytomlpp -_parsers.append(('pytomlpp', pytomlpp.loads)) +_parsers = [('pytomlpp', pytomlpp.loads)] try: import rtoml diff --git a/src/pytomlpp/_io.py b/src/pytomlpp/_io.py index 491741f..bfbf9f6 100644 --- a/src/pytomlpp/_io.py +++ b/src/pytomlpp/_io.py @@ -20,14 +20,15 @@ def dumps(data: Dict[Any, Any]) -> str: return _impl.dumps(data) -def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w", encoding: Optional[str] = "utf-8") -> None: +def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w", encoding: Optional[str] = None) -> None: """Serialise data to TOML file Args: data (Dict[Any, Any]): input data fl (FilePathOrObject): file like object or path mode (str, optional): mode to write the file, support "w", "wt" (text) or "wb" (binary). Defaults to "w". - encoding (str): defaults to utf-8, if None, local encoding is selected. + encoding (str): defaults to None. If None, local enconding will be selected. + NOTE: ``If mode is binary mode, encoding optional argument will be negligible.`` """ data = _impl.dumps(data) if mode == "wb": @@ -35,7 +36,7 @@ def dump(data: Dict[Any, Any], fl: FilePathOrObject, mode: str = "w", encoding: if hasattr(fl, "write"): fl.write(data) return - with open(fl, mode=mode, encoding=encoding or None) as fh: + with open(fl, mode=mode, encoding=encoding) as fh: fh.write(data) @@ -51,21 +52,22 @@ def loads(data: str) -> Dict[Any, Any]: return _impl.loads(data) -def load(fl: FilePathOrObject, mode: str = "r", encoding: Optional[str] = "utf-8") -> Dict[Any, Any]: +def load(fl: FilePathOrObject, mode: str = "r", encoding: Optional[str] = None) -> Dict[Any, Any]: """Deserialise from TOML file to python dict. Args: fl (FilePathOrObject): file like object or path mode (str, optional): mode to read the file, support "r", "rt" (text) or "rb" (binary). Defaults to "r". - + encoding (str): defaults to None. If None, local enconding will be selected. + NOTE: ``If mode is binary mode, encoding optional argument will be negligible.`` + Returns: Dict[Any, Any]: deserialised data """ - if hasattr(fl, "read"): data = fl.read() else: - with open(fl, mode=mode, encoding=encoding or None) as fh: + with open(fl, mode=mode, encoding=encoding) as fh: data = fh.read() if isinstance(data, bytes): return _impl.loads(data.decode("utf-8"))