Revision: 6643 Author: russblau Date: 2009-04-20 17:37:31 +0000 (Mon, 20 Apr 2009)
Log Message: ----------- Fix bug in previous commit to fix Bug #2716315 (undetected session termination); if session times out while bot is operating, it will ask the user to log in again (not ideal for automated bots, but apparently unavoidable), unless you have a password file on your local machine.
Modified Paths: -------------- branches/rewrite/pywikibot/data/api.py branches/rewrite/pywikibot/site.py
Modified: branches/rewrite/pywikibot/data/api.py =================================================================== --- branches/rewrite/pywikibot/data/api.py 2009-04-20 15:29:45 UTC (rev 6642) +++ branches/rewrite/pywikibot/data/api.py 2009-04-20 17:37:31 UTC (rev 6643) @@ -229,9 +229,19 @@ self.site._userinfo.update(result['query']['userinfo']) else: self.site._userinfo = result['query']['userinfo'] - if self.site._userinfo['name'] != self.site.user(): + status = self.site._loginstatus # save previous login status + if ( ("error" in result + and result["error"]["code"].endswith("limit")) + or (status != -1 + and self.site._userinfo['name'] + != self.site._username[status])): # user is no longer logged in (session expired?) - self.site.login(self.site._username.index(self.site.user())) + # reset userinfo, then make user log in again + del self.site._userinfo + self.site._loginstatus = -1 + if status == -1: + status = 0 # default to non-sysop login + self.site.login(status) # retry the previous query continue if "warnings" in result:
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2009-04-20 15:29:45 UTC (rev 6642) +++ branches/rewrite/pywikibot/site.py 2009-04-20 17:37:31 UTC (rev 6643) @@ -615,6 +615,9 @@ self.sitelock = threading.Lock() self._msgcache = {} self.nocapitalize = self.code in self.family.nocapitalize + # _loginstatus: -1 means not logged in, 0 means logged in as user, + # 1 means logged in as sysop + self._loginstatus = -1 return
# ANYTHING BELOW THIS POINT IS NOT YET IMPLEMENTED IN __init__() @@ -646,20 +649,25 @@
def login(self, sysop=False): """Log the user in if not already logged in.""" - if not hasattr(self, "_siteinfo"): - self._getsiteinfo() # check whether a login cookie already exists for this user - if hasattr(self, "_userinfo"): - if self.userinfo['name'] == self._username[sysop]: - return + if not hasattr(self, "_userinfo"): + self.getuserinfo() + if self._userinfo['name'] == self._username[sysop]: + self._loginstatus = sysop + return if not self.logged_in(sysop): loginMan = api.LoginManager(site=self, sysop=sysop, user=self._username[sysop]) if loginMan.login(retry = True): self._username[sysop] = loginMan.username + self._loginstatus = sysop if hasattr(self, "_userinfo"): del self._userinfo self.getuserinfo() + else: + self._loginstatus = -1 # failure + if not hasattr(self, "_siteinfo"): + self._getsiteinfo()
forceLogin = login # alias for backward-compatibility