jenkins-bot has submitted this change and it was merged.
Change subject: Disable decorators and wrappers when not __debug__
......................................................................
Disable decorators and wrappers when not __debug__
When python is called with -O, it optimises the code and
sets __debug__ to False, which may be used to disable
blocks of code not suitable for an optimised tool.
This patch disables decorators and wrappers, which provide
deprecation facilities or check the server version and user
rights before performing an operation. In optimise mode
the caller must explicitly login, avoid deprecated arguments,
check the server versions & features.
epydoc can be generated using:
python -O /usr/bin/epydoc pywikibot
Bug: 72424
Change-Id: I67ed6b3e79ac46ef92e2b08ef6c606fd7177bae2
---
M pywikibot/site.py
M pywikibot/tools.py
M tests/aspects.py
M tests/deprecation_tests.py
M tests/dry_site_tests.py
5 files changed, 46 insertions(+), 7 deletions(-)
Approvals:
John Vandenberg: Looks good to me, but someone else must approve
XZise: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/site.py b/pywikibot/site.py
index c727248..dd994e2 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -963,6 +963,10 @@
else:
raise Exception("Not implemented")
return fn(self, *args, **kwargs)
+
+ if not __debug__:
+ return fn
+
callee.__name__ = fn.__name__
callee.__doc__ = fn.__doc__
callee.__module__ = callee.__module__
@@ -990,6 +994,10 @@
u"isn't implemented in MediaWiki version < %s"
% (fn.__name__, version))
return fn(self, *args, **kwargs)
+
+ if not __debug__:
+ return fn
+
callee.__name__ = fn.__name__
callee.__doc__ = fn.__doc__
callee.__module__ = fn.__module__
diff --git a/pywikibot/tools.py b/pywikibot/tools.py
index ad890eb..d71ad8a 100644
--- a/pywikibot/tools.py
+++ b/pywikibot/tools.py
@@ -438,6 +438,7 @@
"""Python 3 iterator method."""
return self.next()
+
# Decorators
#
# Decorator functions without parameters are _invoked_ differently from
@@ -536,6 +537,9 @@
else:
return inner_wrapper
+ if not __debug__:
+ return obj
+
return outer_wrapper
@@ -571,6 +575,9 @@
warning(u"%s is deprecated." % (name))
return obj(*args, **kwargs)
+ if not __debug__:
+ return obj
+
wrapper.__doc__ = obj.__doc__
wrapper.__name__ = obj.__name__
wrapper.__module__ = obj.__module__
@@ -586,6 +593,9 @@
# When called as @deprecated, return a replacement function
if without_parameters:
+ if not __debug__:
+ return args[0]
+
return decorator(args[0])
# Otherwise return a decorator, which returns a replacement function
else:
@@ -646,6 +656,9 @@
del __kw[old_arg]
return obj(*__args, **__kw)
+ if not __debug__:
+ return obj
+
wrapper.__doc__ = obj.__doc__
wrapper.__name__ = obj.__name__
wrapper.__module__ = obj.__module__
@@ -704,6 +717,10 @@
old=old_name or target.__name__,
target=target_module,
source=source_module)
+
+ if not __debug__:
+ return target
+
return call
@@ -726,7 +743,9 @@
super(ModuleDeprecationWrapper, self).__setattr__('_deprecated', {})
super(ModuleDeprecationWrapper, self).__setattr__('_module', module)
super(ModuleDeprecationWrapper, self).__setattr__('__doc__',
module.__doc__)
- sys.modules[module.__name__] = self
+
+ if __debug__:
+ sys.modules[module.__name__] = self
def _add_deprecated_attr(self, name, replacement=None,
replacement_name=None):
diff --git a/tests/aspects.py b/tests/aspects.py
index f134dcc..00ee838 100644
--- a/tests/aspects.py
+++ b/tests/aspects.py
@@ -915,7 +915,19 @@
user = True
-class DeprecationTestCase(TestCase):
+class DebugOnlyTestCase(TestCase):
+
+ """Test cases that only operate in debug mode."""
+
+ @classmethod
+ def setUpClass(cls):
+ if not __debug__:
+ raise unittest.SkipTest(
+ '%s is disabled when __debug__ is disabled.' % cls.__name__)
+ super(DebugOnlyTestCase, cls).setUpClass()
+
+
+class DeprecationTestCase(DebugOnlyTestCase, TestCase):
"""Test cases for deprecation function in the tools
module."""
diff --git a/tests/deprecation_tests.py b/tests/deprecation_tests.py
index bbdf02b..98f3ccc 100644
--- a/tests/deprecation_tests.py
+++ b/tests/deprecation_tests.py
@@ -10,7 +10,7 @@
from pywikibot.tools import (
deprecated, deprecate_arg, deprecated_args, add_full_name
)
-from tests.aspects import unittest, DeprecationTestCase, TestCase
+from tests.aspects import unittest, DeprecationTestCase
@add_full_name
@@ -53,7 +53,7 @@
pass
-class DecoratorFullNameTestCase(TestCase):
+class DecoratorFullNameTestCase(DeprecationTestCase):
"""Class with methods deprecated."""
@@ -286,7 +286,6 @@
self.assertDeprecation(
__name__ + '.DeprecatedMethodClass.instance_method is deprecated.')
- #(a)unittest.expectedFailure
def test_deprecated_instance_method2(self):
f = DeprecatedMethodClass()
diff --git a/tests/dry_site_tests.py b/tests/dry_site_tests.py
index 773888f..626cf7e 100644
--- a/tests/dry_site_tests.py
+++ b/tests/dry_site_tests.py
@@ -15,8 +15,9 @@
from pywikibot.exceptions import UnknownSite
from tests.aspects import (
- unittest, TestCase,
+ unittest,
DefaultDrySiteTestCase,
+ DebugOnlyTestCase,
DeprecationTestCase,
)
@@ -96,7 +97,7 @@
user_agent(x, format_string='Foo ({script_comments})'))
-class TestMustBe(TestCase):
+class TestMustBe(DebugOnlyTestCase):
"""Test cases for the must_be decorator."""
--
To view, visit
https://gerrit.wikimedia.org/r/169788
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I67ed6b3e79ac46ef92e2b08ef6c606fd7177bae2
Gerrit-PatchSet: 9
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: jenkins-bot <>