summaryrefslogtreecommitdiff
path: root/bs4/element.py
diff options
context:
space:
mode:
authorLeonard Richardson <leonardr@segfault.org>2015-06-25 07:35:32 -0400
committerLeonard Richardson <leonardr@segfault.org>2015-06-25 07:35:32 -0400
commit0ce55d794c2ed142f70674d2b48e8294ca8b1d22 (patch)
treec0602086d32b5d21017d5aaf01db1d70549f0321 /bs4/element.py
parentfee5d1d8f62254939939eb97ac56d7c4f158e6cf (diff)
__repr__ now returns an ASCII bytestring in Python 2, and a Unicode string in Python 3, instead of a UTF8-encoded bytestring in both
versions. [bug=1420131]
Diffstat (limited to 'bs4/element.py')
-rw-r--r--bs4/element.py16
1 files changed, 13 insertions, 3 deletions
diff --git a/bs4/element.py b/bs4/element.py
index 5d895ab..3c32c17 100644
--- a/bs4/element.py
+++ b/bs4/element.py
@@ -983,15 +983,25 @@ class Tag(PageElement):
as defined in __eq__."""
return not self == other
- def __repr__(self, encoding=DEFAULT_OUTPUT_ENCODING):
+ def __repr__(self, encoding="unicode-escape"):
"""Renders this tag as a string."""
- return self.encode(encoding)
+ if PY3K:
+ # "The return value must be a string object", i.e. Unicode
+ return self.decode()
+ else:
+ # "The return value must be a string object", i.e. a bytestring.
+ # By convention, the return value of __repr__ should also be
+ # an ASCII string.
+ return self.encode(encoding)
def __unicode__(self):
return self.decode()
def __str__(self):
- return self.encode()
+ if PY3K:
+ return self.decode()
+ else:
+ return self.encode()
if PY3K:
__str__ = __repr__ = __unicode__