From patchwork Wed Dec 25 00:08:31 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [1,of,3] import-checker: refactor sys.path prefix check (issue4129) From: Chris Jerdonek X-Patchwork-Id: 3236 Message-Id: To: Augie Fackler Cc: mercurial-devel Date: Tue, 24 Dec 2013 16:08:31 -0800 On Tue, Dec 24, 2013 at 2:52 PM, Augie Fackler wrote: > > On Dec 24, 2013, at 5:45 PM, Augie Fackler wrote: > >> >> On Dec 23, 2013, at 12:34 AM, Chris Jerdonek wrote: >> >>> >>> for libpath in sys.path: >>> - # We want to walk everything in sys.path that starts with >>> - # either sys.prefix or sys.exec_prefix. >>> - if not (libpath.startswith(sys.prefix) >>> - or libpath.startswith(sys.exec_prefix)): >>> + # We want to walk everything in sys.path that starts with something >>> + # in stdlib_prefixes. >>> + for prefix in stdlib_prefixes: >>> + if libpath.startswith(prefix): >>> + break >>> + else: >>> continue >> >> This file already depends on 2.6isms (the ast module), so perhaps we could use any() here and avoid the slightly-awkward-to-me for/else? I think it might read more clearly. > > After looking at the rest of the series, I'm crewing it with my own follow-up patch to use any() on a genexp instead of the for/break/else/continue trick. Thanks for following up on this! --Chris diff --git a/contrib/import-checker.py b/contrib/import-checker.py --- a/contrib/import-checker.py +++ b/contrib/import-checker.py @@ -73,10 +73,7 @@ for libpath in sys.path: # We want to walk everything in sys.path that starts with something # in stdlib_prefixes. - for prefix in stdlib_prefixes: - if libpath.startswith(prefix): - break - else: + if not any(libpath.startswith(p) for p in prefix): Oh, you want this to be "for p in stdlib_prefixes" though.