Skip to content
Browse files

[svn r3784] r4429@delle: sbehnel | 2008-05-31 16:53:51 +0200

 fix doctestcompare in Py3

--HG--
branch : trunk
  • Loading branch information...
1 parent 72b7cc9 commit e6f9163d13dd9ac14c1cffc5c2f0c3931f15d020 scoder committed May 31, 2008
Showing with 17 additions and 21 deletions.
  1. +15 −15 src/lxml/doctestcompare.py
  2. +1 −3 src/lxml/html/usedoctest.py
  3. +1 −3 src/lxml/usedoctest.py
View
30 src/lxml/doctestcompare.py
@@ -36,6 +36,7 @@
"""
from lxml import etree
+import sys
import re
import doctest
import cgi
@@ -48,6 +49,8 @@
except NameError:
_basestring = (str, bytes)
+_IS_PYTHON_3 = sys.version_info[0] >= 3
+
PARSE_HTML = doctest.register_optionflag('PARSE_HTML')
PARSE_XML = doctest.register_optionflag('PARSE_XML')
NOPARSE_MARKUP = doctest.register_optionflag('NOPARSE_MARKUP')
@@ -403,13 +406,12 @@ def temp_install(html=False, del_module=None):
# __record_outcome to be run, which signals the end of the __run
# method, at which point we restore the previous check_output
# implementation.
- try:
- check_func = frame.f_locals['check'].im_func
- checker_check_func = checker.check_output.im_func
- except AttributeError:
- # Python 3
+ if _IS_PYTHON_3:
check_func = frame.f_locals['check'].__func__
checker_check_func = checker.check_output.__func__
+ else:
+ check_func = frame.f_locals['check'].im_func
+ checker_check_func = checker.check_output.im_func
# Because we can't patch up func_globals, this is the only global
# in check_output that we care about:
doctest.etree = etree
@@ -430,21 +432,19 @@ def __init__(self, dt_self, old_checker, new_checker, check_func, clone_func,
self.install_clone()
self.install_dt_self()
def install_clone(self):
- try:
- self.func_code = self.check_func.func_code
- self.func_globals = self.check_func.func_globals
- self.check_func.func_code = self.clone_func.func_code
- except AttributeError:
- # Python 3
+ if _IS_PYTHON_3:
self.func_code = self.check_func.__code__
self.func_globals = self.check_func.__globals__
self.check_func.__code__ = self.clone_func.__code__
- def uninstall_clone(self):
- if hasattr(self.check_func, "func_code"):
- self.check_func.func_code = self.func_code
else:
- # Python 3
+ self.func_code = self.check_func.func_code
+ self.func_globals = self.check_func.func_globals
+ self.check_func.func_code = self.clone_func.func_code
+ def uninstall_clone(self):
+ if _IS_PYTHON_3:
self.check_func.__code__ = self.func_code
+ else:
+ self.check_func.func_code = self.func_code
def install_dt_self(self):
self.prev_func = self.dt_self._DocTestRunner__record_outcome
self.dt_self._DocTestRunner__record_outcome = self
View
4 src/lxml/html/usedoctest.py
@@ -8,8 +8,6 @@
See `lxml.doctestcompare`.
"""
-import sys
from lxml import doctestcompare
-if sys.version_info[0] < 3:
- doctestcompare.temp_install(html=True, del_module=__name__)
+doctestcompare.temp_install(html=True, del_module=__name__)
View
4 src/lxml/usedoctest.py
@@ -8,8 +8,6 @@
See `lxml.doctestcompare`
"""
-import sys
from lxml import doctestcompare
-if sys.version_info[0] < 3:
- doctestcompare.temp_install(del_module=__name__)
+doctestcompare.temp_install(del_module=__name__)

0 comments on commit e6f9163

Please sign in to comment.
Something went wrong with that request. Please try again.