Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Next release
https://github.com/aboutcode-org/scancode-toolkit/pull/4474
https://github.com/aboutcode-org/scancode-toolkit/issues/4101

- Replace unmaintained ``toml`` library with ``tomllib`` / ``tomli``.
https://github.com/aboutcode-org/scancode-toolkit/issues/4532

v32.4.1 - 2025-07-23
--------------------
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ install_requires =
saneyaml >= 0.6.0
spdx_tools == 0.8.2
text_unidecode >= 1.0
toml >= 0.10.0
tomli >= 2; python_version < "3.11"
urlpy
xmltodict >= 0.11.0
zipp >= 3.0.0; python_version < "3.9"
Expand Down
16 changes: 13 additions & 3 deletions src/packagedcode/cargo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@
import re
import sys

import toml
from packageurl import PackageURL

from packagedcode import models

# tomli was added to the stdlib as tomllib in Python 3.11.
# It's the same code.
# Still, prefer tomli if it's installed, as on newer Python versions, it is
# compiled with mypyc and is more performant.
try:
import tomli as tomllib
except ImportError:
import tomllib

"""
Handle Rust cargo crates
"""
Expand Down Expand Up @@ -170,7 +178,8 @@ class CargoTomlHandler(CargoBaseHandler):

@classmethod
def parse(cls, location, package_only=False):
package_data_toml = toml.load(location, _dict=dict)
with open(location, "rb") as fp:
package_data_toml = tomllib.load(fp)
workspace = package_data_toml.get('workspace', {})
core_package_data = package_data_toml.get('package', {})
extra_data = {}
Expand Down Expand Up @@ -283,7 +292,8 @@ class CargoLockHandler(CargoBaseHandler):

@classmethod
def parse(cls, location, package_only=False):
cargo_lock = toml.load(location, _dict=dict)
with open(location, "rb") as fp:
cargo_lock = tomllib.load(fp)
dependencies = []
package = cargo_lock.get('package', [])
for dep in package:
Expand Down
19 changes: 15 additions & 4 deletions src/packagedcode/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import packvers as packaging
import pip_requirements_parser
import pkginfo2
import toml
from commoncode import fileutils
from commoncode.fileutils import as_posixpath
from commoncode.resource import Resource
Expand All @@ -46,6 +45,15 @@
from packagedcode.utils import yield_dependencies_from_package_resource
from packagedcode.utils import get_base_purl

# tomli was added to the stdlib as tomllib in Python 3.11.
# It's the same code.
# Still, prefer tomli if it's installed, as on newer Python versions, it is
# compiled with mypyc and is more performant.
try:
import tomli as tomllib
except ImportError:
import tomllib

try:
from zipfile import Path as ZipPath
except ImportError:
Expand Down Expand Up @@ -463,7 +471,8 @@ def is_datafile(cls, location, filetypes=tuple()):

@classmethod
def parse(cls, location, package_only=False):
package_data = toml.load(location, _dict=dict)
with open(location, "rb") as fp:
package_data = tomllib.load(fp)
project_data = package_data.get("project")
if not project_data:
return
Expand Down Expand Up @@ -647,7 +656,8 @@ def parse_non_group_dependencies(

@classmethod
def parse(cls, location, package_only=False):
toml_data = toml.load(location, _dict=dict)
with open(location, "rb") as fp:
toml_data = tomllib.load(fp)

tool_data = toml_data.get('tool')
if not tool_data:
Expand Down Expand Up @@ -725,7 +735,8 @@ class PoetryLockHandler(BasePoetryPythonLayout):

@classmethod
def parse(cls, location, package_only=False):
toml_data = toml.load(location, _dict=dict)
with open(location, "rb") as fp:
toml_data = tomllib.load(fp)

packages = toml_data.get('package')
if not packages:
Expand Down
Loading