Browse files

Do the right with non-ASCII pseudo-classes. Fix #14

Make sure that getattr() with a default does not raise an
UnicodeError or a TypeError on either Py2 or 3.

Instead, all non-ASCII pseudo-classes are invalid selectors
(as an inexistant pseudo-class should.)
  • Loading branch information...
1 parent b0b4624 commit 3e5abd8e33db470edc487a518a06f44478b7c9c1 @SimonSapin SimonSapin committed Jun 29, 2012
Showing with 5 additions and 0 deletions.
  1. +1 −0 cssselect/tests.py
  2. +4 −0 cssselect/xpath.py
View
1 cssselect/tests.py
@@ -385,6 +385,7 @@ def xpath(css):
assert xpath(r'[h\]ref]') == (
"*[attribute::*[name() = 'h]ref']]")
+ self.assertRaises(ExpressionError, xpath, u(':fİrst-child'))
self.assertRaises(ExpressionError, xpath, ':first-of-type')
self.assertRaises(ExpressionError, xpath, ':only-of-type')
self.assertRaises(ExpressionError, xpath, ':last-of-type')
View
4 cssselect/xpath.py
@@ -235,6 +235,8 @@ def xpath_negation(self, negation):
def xpath_function(self, function):
"""Translate a functional pseudo-class."""
method = 'xpath_%s_function' % function.name.replace('-', '_')
+ # getattr() with a non-ASCII name fails on Python 2.x
+ method = method.encode('ascii', 'replace').decode('ascii')
method = getattr(self, method, None)
if not method:
raise ExpressionError(
@@ -244,6 +246,8 @@ def xpath_function(self, function):
def xpath_pseudo(self, pseudo):
"""Translate a pseudo-class."""
method = 'xpath_%s_pseudo' % pseudo.ident.replace('-', '_')
+ # getattr() with a non-ASCII name fails on Python 2.x
+ method = method.encode('ascii', 'replace').decode('ascii')
method = getattr(self, method, None)
if not method:
# TODO: better error message for pseudo-elements?

0 comments on commit 3e5abd8

Please sign in to comment.