Mercurial > hg
changeset 2579:0875cda033fd
use __contains__, index or split instead of str.find
str.find return -1 when the substring is not found, -1 evaluate
to True and is a valid index, which can lead to bugs.
Using alternatives when possible makes the code clearer and less
prone to bugs. (and __contains__ is faster in microbenchmarks)
author | Benoit Boissinot <benoit.boissinot@ens-lyon.org> |
---|---|
date | Sun, 09 Jul 2006 01:30:30 +0200 |
parents | cf4f0322851d |
children | a20a1bb0c396 |
files | mercurial/commands.py mercurial/filelog.py mercurial/hg.py mercurial/hgweb/hgweb_mod.py mercurial/lock.py mercurial/ui.py mercurial/util.py |
diffstat | 7 files changed, 13 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/commands.py Sat Jul 08 16:55:49 2006 +0200 +++ b/mercurial/commands.py Sun Jul 09 01:30:30 2006 +0200 @@ -231,7 +231,7 @@ """Yield revision as strings from a list of revision specifications.""" seen = {} for spec in revs: - if spec.find(revrangesep) >= 0: + if revrangesep in spec: start, end = spec.split(revrangesep, 1) start = revfix(repo, start, 0) end = revfix(repo, end, repo.changelog.count() - 1) @@ -2742,7 +2742,7 @@ disallowed = (revrangesep, '\r', '\n') for c in disallowed: - if name.find(c) >= 0: + if c in name: raise util.Abort(_("%s cannot be used in a tag name") % repr(c)) repo.hook('pretag', throw=True, node=r, tag=name,
--- a/mercurial/filelog.py Sat Jul 08 16:55:49 2006 +0200 +++ b/mercurial/filelog.py Sun Jul 09 01:30:30 2006 +0200 @@ -34,14 +34,14 @@ t = self.revision(node) if not t.startswith('\1\n'): return t - s = t.find('\1\n', 2) + s = t.index('\1\n', 2) return t[s+2:] def readmeta(self, node): t = self.revision(node) if not t.startswith('\1\n'): return {} - s = t.find('\1\n', 2) + s = t.index('\1\n', 2) mt = t[2:s] m = {} for l in mt.splitlines():
--- a/mercurial/hg.py Sat Jul 08 16:55:49 2006 +0200 +++ b/mercurial/hg.py Sun Jul 09 01:30:30 2006 +0200 @@ -61,8 +61,7 @@ if not path: path = '' scheme = path if scheme: - c = scheme.find(':') - scheme = c >= 0 and scheme[:c] + scheme = scheme.split(":", 1)[0] ctor = schemes.get(scheme) or schemes['file'] if create: try:
--- a/mercurial/hgweb/hgweb_mod.py Sat Jul 08 16:55:49 2006 +0200 +++ b/mercurial/hgweb/hgweb_mod.py Sun Jul 09 01:30:30 2006 +0200 @@ -462,7 +462,7 @@ continue remain = f[l:] if "/" in remain: - short = remain[:remain.find("/") + 1] # bleah + short = remain[:remain.index("/") + 1] # bleah files[short] = (f, None) else: short = os.path.basename(remain)
--- a/mercurial/lock.py Sat Jul 08 16:55:49 2006 +0200 +++ b/mercurial/lock.py Sun Jul 09 01:30:30 2006 +0200 @@ -85,14 +85,14 @@ # see if locker is alive. if locker is on this machine but # not alive, we can safely break lock. locker = util.readlock(self.f) - c = locker.find(':') - if c == -1: + try: + host, pid = locker.split(":", 1) + except ValueError: return locker - host = locker[:c] if host != self.host: return locker try: - pid = int(locker[c+1:]) + pid = int(pid) except: return locker if util.testpid(pid):
--- a/mercurial/ui.py Sat Jul 08 16:55:49 2006 +0200 +++ b/mercurial/ui.py Sun Jul 09 01:30:30 2006 +0200 @@ -76,7 +76,7 @@ if root is None: root = os.path.expanduser('~') for name, path in self.configitems("paths"): - if path and path.find("://") == -1 and not os.path.isabs(path): + if path and "://" not in path and not os.path.isabs(path): self.cdata.set("paths", name, os.path.join(root, path)) def setconfig(self, section, name, val): @@ -208,7 +208,7 @@ def expandpath(self, loc, default=None): """Return repository location relative to cwd or from [paths]""" - if loc.find("://") != -1 or os.path.exists(loc): + if "://" in loc or os.path.exists(loc): return loc path = self.config("paths", loc)
--- a/mercurial/util.py Sat Jul 08 16:55:49 2006 +0200 +++ b/mercurial/util.py Sun Jul 09 01:30:30 2006 +0200 @@ -620,7 +620,7 @@ def parse_patch_output(output_line): """parses the output produced by patch and returns the file name""" pf = output_line[14:] - if pf.startswith("'") and pf.endswith("'") and pf.find(" ") >= 0: + if pf.startswith("'") and pf.endswith("'") and " " in pf: pf = pf[1:-1] # Remove the quotes return pf