John Vandenberg has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/190512

Change subject: Log & warn about Site instantiation oddities
......................................................................

Log & warn about Site instantiation oddities

BaseSite constructor has a few branches that perform changes
which may not be expected by the end user, and may be useful
information when debugging a script.

Change-Id: I374461c8b7481e9edae7d5c5db6ef8d39416243b
---
M pywikibot/__init__.py
M pywikibot/site.py
2 files changed, 18 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/12/190512/1

diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index b7161d8..98172f9 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -21,6 +21,8 @@
 else:
     from Queue import Queue
 
+from warnings import warn
+
 # Use pywikibot. prefix for all in-package imports; this is to prevent
 # confusion with similarly-named modules in version 1 framework, for users
 # who want to continue using both
@@ -601,6 +603,11 @@
         _sites[key] = interface(code=code, fam=fam, user=user, sysop=sysop)
         debug(u"Instantiated %s object '%s'"
               % (interface.__name__, _sites[key]), _logger)
+
+        if _sites[key].code != code:
+            warn('Site %s instantiated using code %s'
+                 % (_sites[key], code), UserWarning, 2)
+
     return _sites[key]
 
 
diff --git a/pywikibot/site.py b/pywikibot/site.py
index 525e98e..e0e155a 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -493,7 +493,7 @@
         @type sysop: str
 
         """
-        self.__code = code.lower()
+        self.__code = code = code.lower()
         if isinstance(fam, basestring) or fam is None:
             self.__family = pywikibot.family.Family.load(fam)
         else:
@@ -504,17 +504,25 @@
         if self.__code in self.__family.obsolete:
             if self.__family.obsolete[self.__code] is not None:
                 self.__code = self.__family.obsolete[self.__code]
+                # Note the Site method in __init__ emits a UserWarning
+                # for this condition, showing the callers file and line no.
+                pywikibot.log('Site %s instantiated using code %s'
+                              % (self, code))
             else:
                 # no such language anymore
                 self.obsolete = True
+                pywikibot.log('Site %s instantiated and marked "obsolete" '
+                              'to prevent access' % self)
         elif self.__code not in self.languages():
             if self.__family.name in list(self.__family.langs.keys()) and \
                len(self.__family.langs) == 1:
-                oldcode = self.__code
                 self.__code = self.__family.name
                 if self.__family == pywikibot.config.family \
-                        and oldcode == pywikibot.config.mylang:
+                        and code == pywikibot.config.mylang:
                     pywikibot.config.mylang = self.__code
+                    warn('Global configuration variable "mylang" changed to '
+                         '"%s" while instantiating site %s'
+                         % (self.__code, self), UserWarning)
             else:
                 raise UnknownSite("Language '%s' does not exist in family %s"
                                   % (self.__code, self.__family.name))

-- 
To view, visit https://gerrit.wikimedia.org/r/190512
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I374461c8b7481e9edae7d5c5db6ef8d39416243b
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to