Skip to content

Commit

Permalink
Improve TOML library compatibility (#44)
Browse files Browse the repository at this point in the history
Python 3.11 adds a TOML parser to the standard library. For older
versions of Python, the 'tomli' package is nearly perfectly compatible.

For platforms which don't package tomli or a new enough version of
Python (i.e. Ubuntu Focal), we can fall back to 'toml'.
  • Loading branch information
cottsay authored Dec 4, 2024
1 parent 1b28c41 commit b12c637
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
18 changes: 15 additions & 3 deletions colcon_cargo/package_identification/cargo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@
from colcon_core.package_identification \
import PackageIdentificationExtensionPoint
from colcon_core.plugin_system import satisfies_version
import toml

try:
# Python 3.11+
from tomllib import loads as toml_loads
from tomllib import TOMLDecodeError
except ImportError:
try:
from tomli import loads as toml_loads
from tomli import TOMLDecodeError
except ImportError:
from toml import loads as toml_loads
from toml import TomlDecodeError as TOMLDecodeError

logger = colcon_logger.getChild(__name__)
WORKSPACE = 'WORKSPACE'
Expand Down Expand Up @@ -52,8 +63,9 @@ def extract_data(cargo_toml):
"""
content = {}
try:
content = toml.load(str(cargo_toml))
except toml.TomlDecodeError:
with cargo_toml.open('rb') as f:
content = toml_loads(f.read().decode())
except TOMLDecodeError:
logger.error('Decoding error when processing "%s"'
% cargo_toml.absolute())
return
Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ keywords = colcon
python_requires = >=3.6
install_requires =
colcon-core
toml
# toml is also supported but deprecated
tomli>=1.0.0; python_version < "3.11"
packages = find:
zip_safe = true

Expand Down
2 changes: 1 addition & 1 deletion stdeb.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[colcon-cargo]
No-Python2:
Depends3: python3-colcon-core, python3-toml
Depends3: python3-colcon-core, python3 (>= 3.11) | python3-tomli (>= 1) | python3-toml
Suite: focal jammy noble bookworm trixie
X-Python3-Version: >= 3.6
Debian-Version: 100
2 changes: 2 additions & 0 deletions test/spell_check.words
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ thomas
tmpdir
todo
toml
tomli
tomllib
toprettyxml
tostring
xmlstr

0 comments on commit b12c637

Please sign in to comment.