Skip to content

Commit 4cbd14e

Browse files
committed
Building compatibility with Python3.9
1 parent 94972a3 commit 4cbd14e

File tree

5 files changed

+398
-319
lines changed

5 files changed

+398
-319
lines changed

dapi/__init__.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,24 @@
7878
)
7979

8080

81+
from pathlib import Path
82+
8183
def _get_version():
82-
"""Read version from pyproject.toml"""
83-
import tomllib
84-
from pathlib import Path
84+
"""Read version from pyproject.toml, falling back for older Python."""
85+
try:
86+
# For Python 3.11+
87+
import tomllib
88+
except ModuleNotFoundError:
89+
# For Python < 3.11
90+
import tomli as tomllib # Use tomli and alias it as tomllib
8591

8692
try:
8793
pyproject_path = Path(__file__).parent.parent / "pyproject.toml"
88-
with open(pyproject_path, "rb") as f:
89-
pyproject = tomllib.load(f)
90-
return pyproject["tool"]["poetry"]["version"]
91-
except (FileNotFoundError, KeyError, ImportError):
92-
# Fallback version if pyproject.toml can't be read
94+
with open(pyproject_path, "rb") as f: # tomllib expects bytes
95+
data = tomllib.load(f)
96+
return data["tool"]["poetry"]["version"]
97+
except (FileNotFoundError, KeyError, ImportError, tomllib.TOMLDecodeError):
98+
# Fallback version if pyproject.toml can't be read or parsed
9399
return "unknown"
94100

95101

0 commit comments

Comments
 (0)