Skip to content

Commit

Permalink
Remove rez.utils.py23 and cleanup more python 2.7 leftovers (#1678)
Browse files Browse the repository at this point in the history
* remove rez.utils.py23 module
* remove some more __nonzero__ methods

---------

Signed-off-by: wwn <1506514799@qq.com>
  • Loading branch information
wandth committed Mar 8, 2024
1 parent 153bd87 commit dc2c777
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 124 deletions.
4 changes: 2 additions & 2 deletions src/rez/cli/forward.py
Expand Up @@ -19,7 +19,7 @@ def command(opts, parser, extra_arg_groups=None):
from rez.exceptions import RezSystemError
from rez.vendor import yaml
from rez.vendor.yaml.error import YAMLError
from rez.utils import py23
from rez.util import get_function_arg_names
import os.path

# we don't usually want warnings printed in a wrapped tool. But in cases
Expand Down Expand Up @@ -63,7 +63,7 @@ def command(opts, parser, extra_arg_groups=None):
module = plugin_manager.get_plugin_module(plugin_type, plugin_name)

target_func = getattr(module, func_name)
func_args = py23.get_function_arg_names(target_func)
func_args = get_function_arg_names(target_func)

if "_script" in func_args:
kwargs["_script"] = yaml_file
Expand Down
11 changes: 2 additions & 9 deletions src/rez/package_cache.py
Expand Up @@ -185,16 +185,9 @@ def add_variant(self, variant, force=False):
"Not cached - package is local: %s" % package.uri
)

# Package is already on same disk device as package cache. Note that
# this check is skipped on Windows + Py<3.4, as os.stat does not
# support device identification.
#
dev_stat_not_supported = (
platform.system() == "Windows"
and sys.version_info[:2] < (3, 4)
)
# Package is already on same disk device as package cache

if not config.package_cache_same_device and not dev_stat_not_supported:
if not config.package_cache_same_device:
st_pkgcache = os.stat(self.path)
st_variant = os.stat(variant_root)
if st_pkgcache.st_dev == st_variant.st_dev:
Expand Down
8 changes: 2 additions & 6 deletions src/rez/package_filter.py
Expand Up @@ -157,11 +157,9 @@ def __and__(self, other):
result.add_inclusion(rule)
return result

def __nonzero__(self):
def __bool__(self):
return bool(self._excludes)

__bool__ = __nonzero__ # py3 compat

@cached_property
def cost(self):
"""Get the approximate cost of this filter.
Expand Down Expand Up @@ -304,11 +302,9 @@ def to_pod(self):
data.append(f.to_pod())
return data

def __nonzero__(self):
def __bool__(self):
return any(self.filters)

__bool__ = __nonzero__ # py3 compat

def __str__(self):
filters = sorted(self.filters, key=lambda x: (x.cost, str(x)))
return str(tuple(filters))
Expand Down
4 changes: 1 addition & 3 deletions src/rez/rex.py
Expand Up @@ -1180,14 +1180,12 @@ def __repr__(self):
return '%s(%r, %r)' % (self.__class__.__name__, self._name,
self.value())

def __nonzero__(self):
def __bool__(self):
try:
return bool(self.value())
except RexUndefinedVariableError:
return False

__bool__ = __nonzero__ # py3 compat

def __eq__(self, value):
if isinstance(value, EnvironmentVariable):
value = value.value()
Expand Down
4 changes: 2 additions & 2 deletions src/rez/serialise.py
Expand Up @@ -23,7 +23,7 @@
from rez.exceptions import ResourceError, InvalidPackageError
from rez.utils.memcached import memcached
from rez.utils.execution import add_sys_paths
from rez.utils import py23
from rez.util import get_function_arg_names
from rez.config import config
from rez.vendor.atomicwrites import atomic_write
from rez.vendor import yaml
Expand Down Expand Up @@ -332,7 +332,7 @@ def _process(value):
fn.__globals__.update(get_objects())

# execute the function
args = py23.get_function_arg_names(func)
args = get_function_arg_names(func)

if len(args) not in (0, 1):
raise ResourceError("@early decorated function must "
Expand Down
4 changes: 1 addition & 3 deletions src/rez/solver.py
Expand Up @@ -134,11 +134,9 @@ def br(self):
def pr(self, txt='', *args):
print(txt % args, file=self.buf)

def __nonzero__(self):
def __bool__(self):
return self.verbosity > 0

__bool__ = __nonzero__ # py3 compat


class SolverState(object):
"""Represent the current state of the solver instance for use with a
Expand Down
12 changes: 4 additions & 8 deletions src/rez/tests/test_utils_py23.py → src/rez/tests/test_util.py
Expand Up @@ -3,14 +3,13 @@


"""
unit tests for 'utils.py23' module
unit tests for 'util' module
"""
import os
import sys
import tempfile

import sys
from rez.tests.util import TestBase
from rez.utils import py23
from rez.util import load_module_from_file


class TestLoadModuleFromFile(TestBase):
Expand All @@ -27,8 +26,5 @@ def test_load_module(self):
with open(os.path.join(tmpdir, filename), 'w') as fd:
fd.write('')

py23.load_module_from_file(
module,
os.path.join(tmpdir, filename)
)
load_module_from_file(module, os.path.join(tmpdir, filename))
self.assertEqual(sys.modules.get(module), None, msg='Module was found in sys.modules')
32 changes: 32 additions & 0 deletions src/rez/util.py
Expand Up @@ -11,6 +11,8 @@
import os
import os.path
import re
import inspect

from rez.exceptions import RezError
from rez.vendor.progress.bar import Bar

Expand Down Expand Up @@ -156,3 +158,33 @@ def is_non_string_iterable(arg):
isinstance(arg, collections.abc.Iterable)
and not isinstance(arg, str)
)


def get_function_arg_names(func):
"""Get names of a function's args.
Gives full list of positional and keyword-only args.
"""
spec = inspect.getfullargspec(func)
return spec.args + spec.kwonlyargs


def load_module_from_file(name, filepath):
"""Load a python module from a sourcefile.
Args:
name (str): Module name.
filepath (str): Python sourcefile.
Returns:
`module`: Loaded module.
"""
# The below code will import the module _without_ adding it to
# sys.modules. We want this otherwise we can't import multiple
# versions of the same module
# See: https://github.com/AcademySoftwareFoundation/rez/issues/1483
import importlib.util
spec = importlib.util.spec_from_file_location(name, filepath)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
4 changes: 1 addition & 3 deletions src/rez/utils/logging_.py
Expand Up @@ -62,11 +62,9 @@ def __call__(self, msg, *nargs):
msg = msg % nargs
self.printer_function(msg)

def __nonzero__(self):
def __bool__(self):
return bool(self.printer_function)

__bool__ = __nonzero__ # py3 compat


@contextmanager
def log_duration(printer, msg):
Expand Down
4 changes: 2 additions & 2 deletions src/rez/utils/memcached.py
Expand Up @@ -5,7 +5,7 @@
from rez.config import config
from rez.vendor.memcache.memcache import Client as Client_, \
SERVER_MAX_KEY_LENGTH, __version__ as memcache_client_version
from rez.utils import py23
from rez.util import get_function_arg_names
from threading import local
from contextlib import contextmanager
from functools import update_wrapper
Expand Down Expand Up @@ -318,7 +318,7 @@ def _listdir(path):
"""
def default_key(func, *nargs, **kwargs):
parts = [func.__module__]
argnames = py23.get_function_arg_names(func)
argnames = get_function_arg_names(func)

if argnames:
if argnames[0] == "cls":
Expand Down
10 changes: 0 additions & 10 deletions src/rez/utils/platform_.py
Expand Up @@ -3,7 +3,6 @@


import platform
import sys
import os
import os.path
import re
Expand Down Expand Up @@ -465,15 +464,6 @@ def _difftool(self):
class WindowsPlatform(Platform):
name = "windows"

def _arch(self):
# http://stackoverflow.com/questions/7164843/in-python-how-do-you-determine-whether-the-kernel-is-running-in-32-bit-or-64-bi
if os.name == 'nt' and sys.version_info[:2] < (2, 7):
arch = os.environ.get("PROCESSOR_ARCHITEW6432",
os.environ.get('PROCESSOR_ARCHITECTURE'))
if arch:
return arch
return super(WindowsPlatform, self)._arch()

def _os(self):
release, version, csd, ptype = platform.win32_ver()
toks = []
Expand Down
71 changes: 0 additions & 71 deletions src/rez/utils/py23.py

This file was deleted.

4 changes: 2 additions & 2 deletions src/rez/utils/sourcecode.py
Expand Up @@ -5,7 +5,7 @@
from rez.utils.formatting import indent
from rez.utils.data_utils import cached_property
from rez.utils.logging_ import print_debug
from rez.utils import py23
from rez.util import load_module_from_file
from inspect import getsourcelines
from textwrap import dedent
from glob import glob
Expand Down Expand Up @@ -351,7 +351,7 @@ def load_module(self, name, package):
if config.debug("file_loads"):
print_debug("Loading include sourcefile: %s" % filepath)

module = py23.load_module_from_file(name, filepath)
module = load_module_from_file(name, filepath)
self.modules[hash_str] = module
return module

Expand Down
4 changes: 1 addition & 3 deletions src/rez/version/_version.py
Expand Up @@ -390,12 +390,10 @@ def __getitem__(self, index):
except IndexError:
raise IndexError("version token index out of range")

def __nonzero__(self):
def __bool__(self):
"""The empty version equates to False."""
return bool(self.tokens)

__bool__ = __nonzero__ # py3 compat

def __eq__(self, other):
return isinstance(other, Version) and self.tokens == other.tokens

Expand Down

0 comments on commit dc2c777

Please sign in to comment.