| 
29 | 29 | import packvers as packaging  | 
30 | 30 | import pip_requirements_parser  | 
31 | 31 | import pkginfo2  | 
32 |  | -import toml  | 
33 | 32 | from commoncode import fileutils  | 
34 | 33 | from commoncode.fileutils import as_posixpath  | 
35 | 34 | from commoncode.resource import Resource  | 
 | 
46 | 45 | from packagedcode.utils import yield_dependencies_from_package_resource  | 
47 | 46 | from packagedcode.utils import get_base_purl  | 
48 | 47 | 
 
  | 
 | 48 | +# tomli was added to the stdlib as tomllib in Python 3.11.  | 
 | 49 | +# It's the same code.  | 
 | 50 | +# Still, prefer tomli if it's installed, as on newer Python versions, it is  | 
 | 51 | +# compiled with mypyc and is more performant.  | 
 | 52 | +try:  | 
 | 53 | +    import tomli as tomllib  | 
 | 54 | +except ImportError:  | 
 | 55 | +    import tomllib  | 
 | 56 | + | 
49 | 57 | try:  | 
50 | 58 |     from zipfile import Path as ZipPath  | 
51 | 59 | except ImportError:  | 
@@ -463,7 +471,8 @@ def is_datafile(cls, location, filetypes=tuple()):  | 
463 | 471 | 
 
  | 
464 | 472 |     @classmethod  | 
465 | 473 |     def parse(cls, location, package_only=False):  | 
466 |  | -        package_data = toml.load(location, _dict=dict)  | 
 | 474 | +        with open(location, "rb") as fp:  | 
 | 475 | +            package_data = tomllib.load(fp)  | 
467 | 476 |         project_data = package_data.get("project")  | 
468 | 477 |         if not project_data:  | 
469 | 478 |             return  | 
@@ -647,7 +656,8 @@ def parse_non_group_dependencies(  | 
647 | 656 | 
 
  | 
648 | 657 |     @classmethod  | 
649 | 658 |     def parse(cls, location, package_only=False):  | 
650 |  | -        toml_data = toml.load(location, _dict=dict)  | 
 | 659 | +        with open(location, "rb") as fp:  | 
 | 660 | +            toml_data = tomllib.load(fp)  | 
651 | 661 | 
 
  | 
652 | 662 |         tool_data = toml_data.get('tool')  | 
653 | 663 |         if not tool_data:  | 
@@ -725,7 +735,8 @@ class PoetryLockHandler(BasePoetryPythonLayout):  | 
725 | 735 | 
 
  | 
726 | 736 |     @classmethod  | 
727 | 737 |     def parse(cls, location, package_only=False):  | 
728 |  | -        toml_data = toml.load(location, _dict=dict)  | 
 | 738 | +        with open(location, "rb") as fp:  | 
 | 739 | +            toml_data = tomllib.load(fp)  | 
729 | 740 | 
 
  | 
730 | 741 |         packages = toml_data.get('package')  | 
731 | 742 |         if not packages:  | 
 | 
0 commit comments