From 120d6a036607d911a58527ae43c789aa0cc348ed Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 14 Jan 2017 23:32:05 -0600 Subject: [PATCH] Fix Wikicode.matches behavior on non-list/tuple iterables. --- CHANGELOG | 1 + docs/changelog.rst | 1 + mwparserfromhell/wikicode.py | 27 ++++++++++++++------------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index f3728dd..4988112 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ v0.5 (unreleased): +- Fixed Wikicode.matches() on iterables besides lists and tuples. - Fixed len() sometimes raising ValueError on empty node lists. - Fixed release script after changes to PyPI. diff --git a/docs/changelog.rst b/docs/changelog.rst index edf5ab9..e1e8ac8 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -7,6 +7,7 @@ v0.5 Unreleased (`changes `__): +- Fixed :meth:`.Wikicode.matches` on iterables besides lists and tuples. - Fixed ``len()`` sometimes raising ``ValueError`` on empty node lists. - Fixed release script after changes to PyPI. diff --git a/mwparserfromhell/wikicode.py b/mwparserfromhell/wikicode.py index e3f6b92..447f6ff 100644 --- a/mwparserfromhell/wikicode.py +++ b/mwparserfromhell/wikicode.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2012-2016 Ben Kurtovic +# Copyright (C) 2012-2017 Ben Kurtovic # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -24,7 +24,7 @@ from __future__ import unicode_literals from itertools import chain import re -from .compat import py3k, range, str +from .compat import bytes, py3k, range, str from .nodes import (Argument, Comment, ExternalLink, Heading, HTMLEntity, Node, Tag, Template, Text, Wikilink) from .string_mixin import StringMixIn @@ -413,22 +413,23 @@ class Wikicode(StringMixIn): """Do a loose equivalency test suitable for comparing page names. *other* can be any string-like object, including :class:`.Wikicode`, or - a tuple of these. This operation is symmetric; both sides are adjusted. - Specifically, whitespace and markup is stripped and the first letter's - case is normalized. Typical usage is + an iterable of these. This operation is symmetric; both sides are + adjusted. Specifically, whitespace and markup is stripped and the first + letter's case is normalized. Typical usage is ``if template.name.matches("stub"): ...``. """ cmp = lambda a, b: (a[0].upper() + a[1:] == b[0].upper() + b[1:] if a and b else a == b) this = self.strip_code().strip() - if isinstance(other, (tuple, list)): - for obj in other: - that = parse_anything(obj).strip_code().strip() - if cmp(this, that): - return True - return False - that = parse_anything(other).strip_code().strip() - return cmp(this, that) + if isinstance(other, (str, bytes, Wikicode, Node)): + that = parse_anything(other).strip_code().strip() + return cmp(this, that) + + for obj in other: + that = parse_anything(obj).strip_code().strip() + if cmp(this, that): + return True + return False def ifilter(self, recursive=True, matches=None, flags=FLAGS, forcetype=None):