Skip to content

Commit

Permalink
In Python 3.11 certain resources are deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
hgrecco committed Oct 22, 2022
1 parent 76aadb3 commit 031e069
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions flexparser/flexparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import logging
import pathlib
import re
import sys
import typing as ty
from collections.abc import Iterator
from dataclasses import dataclass
Expand Down Expand Up @@ -1058,8 +1059,15 @@ def parse_resource_from_file(
resource_name
name of the resource
"""
with resources.path(package, resource_name) as p:
path = p.resolve()
if sys.version_info < (3, 9):
# Remove when Python 3.8 is dropped
with resources.path(package, resource_name) as p:
path = p.resolve()
else:
with resources.as_file(
resources.files(package).joinpath(resource_name)
) as p:
path = p.resolve()

if path.exists():
return self.parse_file(path)
Expand All @@ -1076,8 +1084,13 @@ def parse_resource(self, package: str, resource_name: str) -> ParsedSource[RBT,
resource_name
name of the resource
"""
with resources.open_binary(package, resource_name) as fi:
content = fi.read()
if sys.version_info < (3, 9):
# Remove when Python 3.8 is dropped
with resources.open_binary(package, resource_name) as fi:
content = fi.read()
else:
with resources.files(package).joinpath(resource_name).open("rb") as fi:
content = fi.read()

bos = BOR(
Hash.from_bytes(self._hasher, content), package, resource_name
Expand Down

0 comments on commit 031e069

Please sign in to comment.