jenkins-bot has submitted this change and it was merged.
Change subject: [FIX] Detect source module and apply it to four
functions/methods
......................................................................
[FIX] Detect source module and apply it to four functions/methods
This adds usecases and doesn't rely on __self__ anymore. With Python 3
an unbound method (which is usually used) doesn't has a __self__
attribute because it's handled like a function. So the class now must be
supplied manually and using __self__ is removed, because it's value is
already supplied.
This makes redirect_func also compatible with classmethods by getting
rid of the wrapper class, which replaced the 'self' parameter and the
new method was called like a function.
Change-Id: I9155163bb0d89707dbfa7c393ab63be56e2f616a
---
M pywikibot/__init__.py
M pywikibot/site.py
M pywikibot/tools.py
3 files changed, 34 insertions(+), 29 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index dd3d4f1..63b3294 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""The initialization file for the Pywikibot framework."""
#
-# (C) Pywikibot team, 2008-2013
+# (C) Pywikibot team, 2008-2014
#
# Distributed under the terms of the MIT license.
#
@@ -536,7 +536,8 @@
return _sites[key]
-getSite = Site # alias for backwards-compability
+# alias for backwards-compability
+getSite = pywikibot.tools.redirect_func(Site, old_name='getSite')
from .page import (
diff --git a/pywikibot/site.py b/pywikibot/site.py
index de30eda..5611585 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -28,6 +28,7 @@
import pywikibot.family
from pywikibot.tools import (
itergroup, deprecated, deprecate_arg, UnicodeMixin, ComparableMixin,
+ redirect_func,
)
from pywikibot.tools import MediaWikiVersion as LV
from pywikibot.throttle import Throttle
@@ -105,10 +106,9 @@
return 'LoginStatus(%s)' % (LoginStatus.name(self.state))
-Family = pywikibot.tools.redirect_func(pywikibot.family.Family.load,
- 'pywikibot.site',
- 'pywikibot.family.Family',
- 'Family')
+Family = redirect_func(pywikibot.family.Family.load,
+ target_module='pywikibot.family.Family',
+ old_name='Family')
class Namespace(Iterable, ComparableMixin, UnicodeMixin):
@@ -549,7 +549,9 @@
for name in self.namespaces()[ns]]:
return ns
- getNamespaceIndex = ns_index # for backwards-compatibility
+ # for backwards-compatibility
+ getNamespaceIndex = redirect_func(ns_index, old_name='getNamespaceIndex',
+ class_name='BaseSite')
def namespaces(self):
"""Return dict of valid namespaces on this wiki."""
@@ -568,7 +570,10 @@
index = self.ns_index(value)
return self.namespace(index)
- normalizeNamespace = ns_normalize # for backwards-compatibility
+ # for backwards-compatibility
+ normalizeNamespace = redirect_func(ns_normalize,
+ old_name='normalizeNamespace',
+ class_name='BaseSite')
def redirect(self, default=True):
"""Return list of localized redirect tags for the site.
@@ -1435,7 +1440,9 @@
else:
self._loginstatus = LoginStatus.NOT_LOGGED_IN # failure
- forceLogin = login # alias for backward-compatibility
+ # alias for backward-compatibility
+ forceLogin = redirect_func(login, old_name='forceLogin',
+ class_name='APISite')
def logout(self):
""" Logout of the site and load details for the logged out user.
diff --git a/pywikibot/tools.py b/pywikibot/tools.py
index c11d0d7..22e2203 100644
--- a/pywikibot/tools.py
+++ b/pywikibot/tools.py
@@ -372,7 +372,7 @@
def redirect_func(target, source_module=None, target_module=None,
- old_name=None):
+ old_name=None, class_name=None):
"""
Return a function which can be used to redirect to 'target'.
@@ -392,27 +392,17 @@
@param old_name: The old function name. If None it uses the name of the
new function.
@type old_name: basestring
+ @param class_name: The name of the class. It's added to the target and
+ source module (separated by a '.').
+ @type class_name: basestring
@return: A new function which adds a warning prior to each execution.
@rtype: callable
"""
- class Wrapper(object):
- def __init__(self):
- self._function = target
- self.parameters = {'new': target.__name__,
- 'old': old_name or target.__name__,
- 'target': target_module,
- 'source': source_module}
- self.warning = ('{source}{old} is DEPRECATED, use {target}{new} '
- 'instead.').format(**self.parameters)
-
- def call(self, *a, **kw):
- warning(self.warning)
- return self._function(*a, **kw)
-
+ def call(*a, **kw):
+ warning(warn_message)
+ return target(*a, **kw)
if target_module is None:
target_module = target.__module__
- if hasattr(target, '__self__'):
- target_module += '.' + target.__self__.__class__.__name__
if target_module and target_module[-1] != '.':
target_module += '.'
if source_module is '.':
@@ -420,9 +410,16 @@
elif source_module and source_module[-1] != '.':
source_module += '.'
else:
- source_module = (sys._getframe(1).f_code.co_filename.rsplit("/", 1)[0]
- .replace("/", ".") + ".")
- return Wrapper().call
+ source_module = sys._getframe(1).f_globals['__name__'] + '.'
+ if class_name:
+ target_module += class_name + '.'
+ source_module += class_name + '.'
+ warn_message = ('{source}{old} is DEPRECATED, use {target}{new} '
+ 'instead.').format(new=target.__name__,
+ old=old_name or target.__name__,
+ target=target_module,
+ source=source_module)
+ return call
class ModuleDeprecationWrapper(object):
--
To view, visit https://gerrit.wikimedia.org/r/163991
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I9155163bb0d89707dbfa7c393ab63be56e2f616a
Gerrit-PatchSet: 3
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <[email protected]>
Gerrit-Reviewer: John Vandenberg <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: Mpaa <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits