From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id C960315808B for ; Sun, 27 Mar 2022 23:07:16 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 9B919E07D0; Sun, 27 Mar 2022 23:07:15 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 13516E097A for ; Sun, 27 Mar 2022 23:07:13 +0000 (UTC) Received: from oystercatcher.gentoo.org (unknown [IPv6:2a01:4f8:202:4333:225:90ff:fed9:fc84]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id F0DF1343695 for ; Sun, 27 Mar 2022 23:07:11 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 4C527343 for ; Sun, 27 Mar 2022 23:07:10 +0000 (UTC) From: "Sam James" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" Message-ID: <1648422397.13740f43bcc38e787153d9efeabc4f5d878e7af0.sam@gentoo> Subject: [gentoo-commits] proj/portage:master commit in: lib/portage/ X-VCS-Repository: proj/portage X-VCS-Files: lib/portage/__init__.py lib/portage/data.py lib/portage/dispatch_conf.py lib/portage/eclass_cache.py lib/portage/getbinpkg.py lib/portage/glsa.py X-VCS-Directories: lib/portage/ X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: 13740f43bcc38e787153d9efeabc4f5d878e7af0 X-VCS-Branch: master Date: Sun, 27 Mar 2022 23:07:10 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 4e02a116-882a-46e5-a4ce-97253237da5e X-Archives-Hash: 25566366f3f72257753e2a2faaa3bdde commit: 13740f43bcc38e787153d9efeabc4f5d878e7af0 Author: Kenneth Raplee kennethraplee com> AuthorDate: Tue Mar 22 07:22:42 2022 +0000 Commit: Sam James gentoo org> CommitDate: Sun Mar 27 23:06:37 2022 +0000 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=13740f43 Prefer generator expressions over list comprehensions List comprehensions do improve the performance compared to using for loops to build the same lists in Python. However, they are eagerly evaluated and will create a sequence ahead of time. The problem is, a fair amount of code would only use these lists as iterators which does not require them to be sequences. Unless there is a need to know a list's length, access its items, or alter its size, it's better to use generators which are lazily evaluated and can be iterated when necessary. Avoiding the eagerness of list comprehensions will improve the runtime performance. Signed-off-by: Kenneth Raplee kennethraplee.com> Signed-off-by: Sam James gentoo.org> lib/portage/__init__.py | 22 +++++++++++++--------- lib/portage/data.py | 25 ++++++++++++++----------- lib/portage/dispatch_conf.py | 2 +- lib/portage/eclass_cache.py | 2 +- lib/portage/getbinpkg.py | 15 ++++++++------- lib/portage/glsa.py | 14 ++++++++++---- 6 files changed, 47 insertions(+), 33 deletions(-) diff --git a/lib/portage/__init__.py b/lib/portage/__init__.py index 82e47d0ff..e6dff28ee 100644 --- a/lib/portage/__init__.py +++ b/lib/portage/__init__.py @@ -555,10 +555,15 @@ _deprecated_eapis = frozenset( "7_pre1", ] ) + +from itertools import chain + _supported_eapis = frozenset( - [str(x) for x in range(portage.const.EAPI + 1)] - + list(_testing_eapis) - + list(_deprecated_eapis) + chain( + (str(x) for x in range(portage.const.EAPI + 1)), + _testing_eapis, + _deprecated_eapis, + ) ) @@ -672,8 +677,7 @@ def create_trees( # When ROOT != "/" we only want overrides from the calling # environment to apply to the config that's associated # with ROOT != "/", so pass a nearly empty dict for the env parameter. - clean_env = {} - for k in ( + env_sequence = ( "PATH", "PORTAGE_GRPNAME", "PORTAGE_REPOSITORIES", @@ -687,10 +691,10 @@ def create_trees( "https_proxy", "no_proxy", "__PORTAGE_TEST_HARDLINK_LOCKS", - ): - v = settings.get(k) - if v is not None: - clean_env[k] = v + ) + env = ((k, settings.get(k)) for k in env_sequence) + clean_env = {k: v for k, v in env if v is not None} + if depcachedir is not None: clean_env["PORTAGE_DEPCACHEDIR"] = depcachedir settings = config( diff --git a/lib/portage/data.py b/lib/portage/data.py index e1457c566..8c58ad0fc 100644 --- a/lib/portage/data.py +++ b/lib/portage/data.py @@ -229,27 +229,30 @@ def _get_global(k): # Get a list of group IDs for the portage user. Do not use # grp.getgrall() since it is known to trigger spurious # SIGPIPE problems with nss_ldap. - cmd = ["id", "-G", _portage_username] - encoding = portage._encodings["content"] - cmd = [ + cmd = ( portage._unicode_encode(x, encoding=encoding, errors="strict") - for x in cmd - ] + for x in ("id", "-G", _portage_username) + ) proc = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT ) myoutput = proc.communicate()[0] status = proc.wait() if os.WIFEXITED(status) and os.WEXITSTATUS(status) == os.EX_OK: - for x in portage._unicode_decode( - myoutput, encoding=encoding, errors="strict" - ).split(): + + def check(x): try: - v.append(int(x)) + return int(x) except ValueError: - pass - v = sorted(set(v)) + return None + + unicode_decode = portage._unicode_decode( + myoutput, encoding=encoding, errors="strict" + ) + checked_v = (check(x) for x in unicode_decode.split()) + filtered_v = (x for x in checked_v if x) + v = sorted(set(filtered_v)) # Avoid instantiating portage.settings when the desired # variable is set in os.environ. diff --git a/lib/portage/dispatch_conf.py b/lib/portage/dispatch_conf.py index 6c6036c4e..c89caf087 100644 --- a/lib/portage/dispatch_conf.py +++ b/lib/portage/dispatch_conf.py @@ -37,7 +37,7 @@ def diffstatusoutput(cmd, file1, file2): # raise a UnicodeDecodeError which makes the output inaccessible. args = shlex_split(cmd % (file1, file2)) - args = [portage._unicode_encode(x, errors="strict") for x in args] + args = (portage._unicode_encode(x, errors="strict") for x in args) proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output = portage._unicode_decode(proc.communicate()[0]) if output and output[-1] == "\n": diff --git a/lib/portage/eclass_cache.py b/lib/portage/eclass_cache.py index f1729326d..2545ed9b2 100644 --- a/lib/portage/eclass_cache.py +++ b/lib/portage/eclass_cache.py @@ -110,7 +110,7 @@ class cache: master_eclasses = {} eclass_len = len(".eclass") ignored_listdir_errnos = (errno.ENOENT, errno.ENOTDIR) - for x in [normalize_path(os.path.join(y, "eclass")) for y in self.porttrees]: + for x in (normalize_path(os.path.join(y, "eclass")) for y in self.porttrees): try: eclass_filenames = os.listdir(x) except OSError as e: diff --git a/lib/portage/getbinpkg.py b/lib/portage/getbinpkg.py index 1e89119fb..bf99fd2be 100644 --- a/lib/portage/getbinpkg.py +++ b/lib/portage/getbinpkg.py @@ -112,10 +112,12 @@ class ParseLinks(html_parser_HTMLParser): def handle_starttag(self, tag, attrs): if tag == "a": - for x in attrs: - if x[0] == "href": - if x[1] not in self.PL_anchors: - self.PL_anchors.append(urllib_parse_unquote(x[1])) + myarchors = ( + urllib_parse_unquote(x[1]) + for x in attrs + if x[0] == "href" and x[1] not in self.PL_anchors + ) + self.PL_anchors.extend(myarchors) def create_conn(baseurl, conn=None): @@ -533,8 +535,7 @@ def file_get( from portage.util import varexpand from portage.process import spawn - myfetch = portage.util.shlex_split(fcmd) - myfetch = [varexpand(x, mydict=variables) for x in myfetch] + myfetch = (varexpand(x, mydict=variables) for x in portage.util.shlex_split(fcmd)) fd_pipes = { 0: portage._get_stdin().fileno(), 1: sys.__stdout__.fileno(), @@ -986,5 +987,5 @@ class PackageIndex: keys = list(metadata) keys.sort() self._writepkgindex( - pkgfile, [(k, metadata[k]) for k in keys if metadata[k]] + pkgfile, ((k, metadata[k]) for k in keys if metadata[k]) ) diff --git a/lib/portage/glsa.py b/lib/portage/glsa.py index 05de3ade6..4b92d52e0 100644 --- a/lib/portage/glsa.py +++ b/lib/portage/glsa.py @@ -126,14 +126,20 @@ def get_glsa_list(myconfig): return [] dirlist = os.listdir(repository) prefix = "glsa-" + prefix_size = len(prefix) suffix = ".xml" + suffix_size = len(suffix) - for f in dirlist: + def check(value): try: - if f[: len(prefix)] == prefix and f[-1 * len(suffix) :] == suffix: - rValue.append(f[len(prefix) : -1 * len(suffix)]) + if value[:prefix_size] == prefix and value[-suffix_size:] == suffix: + return value[prefix_size:-suffix_size] except IndexError: - pass + return None + return None + + checked_dirlist = (check(f) for f in dirlist) + rValue = [f for f in checked_dirlist if f] return rValue