Skip to content

Commit c076906

Browse files
committed
Use functools.cached_property instead of the local copy
1 parent cb2aa2c commit c076906

File tree

10 files changed

+17
-67
lines changed

10 files changed

+17
-67
lines changed

pycheribuild/config/chericonfig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import shutil
3737
import typing
3838
from enum import Enum
39+
from functools import cached_property
3940
from pathlib import Path
4041
from typing import Optional
4142

@@ -45,7 +46,6 @@
4546
from ..utils import (
4647
ConfigBase,
4748
DoNotUseInIfStmt,
48-
cached_property,
4949
fatal_error,
5050
have_working_internet_connection,
5151
status_update,

pycheribuild/config/compilation_targets.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import sys
3737
import typing
3838
from abc import ABC, abstractmethod
39+
from functools import cached_property
3940
from pathlib import Path
4041
from typing import Optional
4142

@@ -56,7 +57,7 @@
5657
)
5758
from ..processutils import extract_version, get_compiler_info, get_version_output
5859
from ..projects.simple_project import SimpleProject
59-
from ..utils import cached_property, is_jenkins_build, warning_message
60+
from ..utils import is_jenkins_build, warning_message
6061

6162

6263
class BuildLLVMInterface(SimpleProject if typing.TYPE_CHECKING else object):

pycheribuild/config/target_info.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333
import typing
3434
from abc import ABC, abstractmethod
3535
from enum import Enum
36+
from functools import cached_property
3637
from pathlib import Path
3738
from typing import ClassVar, Optional
3839

3940
from .chericonfig import AArch64FloatSimdOptions, CheriConfig, MipsFloatAbi
4041
from ..filesystemutils import FileSystemUtils
4142
from ..processutils import CompilerInfo, get_compiler_info
42-
from ..utils import OSInfo, cached_property, fatal_error, final, status_update, warning_message
43+
from ..utils import OSInfo, fatal_error, final, status_update, warning_message
4344

4445
__all__ = [
4546
"AArch64FloatSimdOptions",

pycheribuild/projects/cross/cheribsd.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import typing
3838
from collections import OrderedDict
3939
from enum import Enum
40+
from functools import cached_property
4041
from pathlib import Path
4142
from typing import ClassVar, Optional, Union
4243

@@ -59,7 +60,7 @@
5960
from ...config.loader import ConfigOptionHandle
6061
from ...config.target_info import AutoVarInit, CompilerType, CrossCompileTarget
6162
from ...processutils import latest_system_clang_tool, print_command
62-
from ...utils import OSInfo, ThreadJoiner, cached_property, classproperty, is_jenkins_build
63+
from ...utils import OSInfo, ThreadJoiner, classproperty, is_jenkins_build
6364

6465

6566
def _arch_suffixed_custom_install_dir(prefix: str) -> "ComputedDefaultValue[Path]":

pycheribuild/projects/cross/llvm_test_suite.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
# SUCH DAMAGE.
2929
#
3030
import shutil
31+
from functools import cached_property
3132
from pathlib import Path
3233

3334
from .benchmark_mixin import BenchmarkMixin
@@ -42,7 +43,7 @@
4243
from ..project import ReuseOtherProjectRepository
4344
from ..simple_project import BoolConfigOption
4445
from ...config.compilation_targets import FreeBSDTargetInfo
45-
from ...utils import cached_property, classproperty, is_jenkins_build
46+
from ...utils import classproperty, is_jenkins_build
4647

4748

4849
class BuildLLVMTestSuiteBase(BenchmarkMixin, CrossCompileCMakeProject):

pycheribuild/projects/disk_image.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import sys
3535
import tempfile
3636
from enum import Enum
37+
from functools import cached_property
3738
from pathlib import Path
3839
from typing import Optional
3940

@@ -53,7 +54,7 @@
5354
from .simple_project import SimpleProject
5455
from ..config.compilation_targets import CompilationTargets
5556
from ..mtree import MtreeFile
56-
from ..utils import AnsiColour, cached_property, classproperty, coloured, include_local_file
57+
from ..utils import AnsiColour, classproperty, coloured, include_local_file
5758

5859
# Notes:
5960
# Mount the filesystem of a BSD VM: guestmount -a /foo/bar.qcow2 -m /dev/sda1:/:ufstype=ufs2:ufs --ro /mnt/foo

pycheribuild/projects/project.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import typing
4141
from collections import OrderedDict
4242
from enum import Enum
43+
from functools import cached_property
4344
from pathlib import Path
4445
from typing import Callable, Optional, Sequence, Union
4546

@@ -77,7 +78,6 @@
7778
InstallInstructions,
7879
OSInfo,
7980
ThreadJoiner,
80-
cached_property,
8181
classproperty,
8282
coloured,
8383
remove_duplicates,

pycheribuild/projects/run_fvp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import subprocess
3333
import tempfile
3434
import typing
35+
from functools import cached_property
3536
from pathlib import Path
3637
from subprocess import CompletedProcess
3738
from typing import Optional
@@ -47,7 +48,6 @@
4748
AnsiColour,
4849
OSInfo,
4950
SocketAndPort,
50-
cached_property,
5151
classproperty,
5252
coloured,
5353
find_free_port,

pycheribuild/projects/testrig.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import time
2828
import typing
2929
from abc import ABC, abstractmethod
30+
from functools import cached_property
3031
from pathlib import Path
3132
from typing import Optional
3233

@@ -36,7 +37,7 @@
3637
from .sail import BuildSailCheriRISCV
3738
from .simple_project import BoolConfigOption, IntConfigOption, OptionalIntConfigOption, SimpleProject
3839
from ..processutils import FakePopen, commandline_to_str, popen
39-
from ..utils import cached_property, find_free_port
40+
from ..utils import find_free_port
4041

4142

4243
# This repository contains various implementations and QuickCheckVEngine

pycheribuild/utils.py

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import traceback
4040
import typing
4141
from pathlib import Path
42-
from threading import RLock
4342
from typing import Callable, Optional, Union
4443

4544
from .colour import AnsiColour, coloured
@@ -56,7 +55,6 @@
5655
"ThreadJoiner",
5756
"Type_T",
5857
"add_error_context",
59-
"cached_property",
6058
"classproperty",
6159
"coloured",
6260
"default_make_jobs_count",
@@ -144,60 +142,6 @@ def get_global_config() -> ConfigBase:
144142
return GlobalConfig
145143

146144

147-
if False and sys.version_info >= (3, 8, 0):
148-
# TODO: once we depend on 3.8 use functools version instead
149-
# from functools import cached_property
150-
pass
151-
else:
152-
# Note: this is a copy of the python 3.8.6 implementation with f-strings removed for python 3.5.2 compat.
153-
_NOT_FOUND: object = object()
154-
155-
# noinspection PyPep8Naming
156-
class cached_property(typing.Generic[Type_T]): # noqa: N801
157-
def __init__(self, func: "Callable[[typing.Any], Type_T]") -> None:
158-
self.func = func
159-
self.attrname = func.__name__ if sys.version_info < (3, 6) else None
160-
self.__doc__ = func.__doc__
161-
self.lock = RLock()
162-
163-
def __set_name__(self, _, name) -> None: # XXX: requires python 3.6
164-
if self.attrname is None:
165-
self.attrname = name
166-
elif name != self.attrname:
167-
raise TypeError(
168-
f"Cannot assign the same cached_property to two different names ({self.attrname} and {name})."
169-
)
170-
171-
def __get__(self, instance, owner=None) -> Type_T:
172-
if instance is None:
173-
return self
174-
if self.attrname is None:
175-
raise TypeError("Cannot use cached_property instance without calling __set_name__ on it.")
176-
try:
177-
cache = instance.__dict__
178-
except AttributeError: # not all objects have __dict__ (e.g. class defines slots)
179-
msg = (
180-
f"No '__dict__' attribute on {type(instance).__name__} instance to cache {self.attrname} property."
181-
)
182-
raise TypeError(msg) from None
183-
val = cache.get(self.attrname, _NOT_FOUND)
184-
if val is _NOT_FOUND:
185-
with self.lock:
186-
# check if another thread filled cache while we awaited lock
187-
val = cache.get(self.attrname, _NOT_FOUND)
188-
if val is _NOT_FOUND:
189-
val = self.func(instance)
190-
try:
191-
cache[self.attrname] = val
192-
except TypeError:
193-
msg = (
194-
f"The '__dict__' attribute on {type(instance).__name__} instance does not support "
195-
f"item assignment for caching {self.attrname} property."
196-
)
197-
raise TypeError(msg) from None
198-
return val
199-
200-
201145
def is_jenkins_build() -> bool:
202146
return os.getenv("_CHERIBUILD_JENKINS_BUILD") is not None
203147

@@ -600,12 +544,12 @@ def replace_one(s: str, old, new) -> str:
600544

601545

602546
def remove_duplicates(items: "typing.Iterable[Type_T]") -> "list[Type_T]":
603-
# Convert to a dict to remove duplicates (retains order since python 3.6, which is our minimum)
547+
# Convert to a dict to remove duplicates (retains order since python 3.6, which is older than our minimum)
604548
return list(dict.fromkeys(items))
605549

606550

607551
def remove_tuple_duplicates(items: "typing.Iterable[Type_T]") -> "tuple[Type_T, ...]":
608-
# Convert to a dict to remove duplicates (retains order since python 3.6, which is our minimum)
552+
# Convert to a dict to remove duplicates (retains order since python 3.6, which is older than our minimum)
609553
return tuple(dict.fromkeys(items))
610554

611555

0 commit comments

Comments
 (0)