Xqt has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/766175 )
Change subject: [bugfix] Do not create a Site object to create a Throttle object
......................................................................
[bugfix] Do not create a Site object to create a Throttle object
- Throttle needs a site but a sitename is also valid and an empty site
parameter can also be used. In such a case do not write the process
to the throttle.ctrl file but read the file to get the PID.
- create such a dummy Throttle to read the PID in bot.init_handlers()
- remove Site.__doc__ assignment in tests/aspects.py
Bug: T302173
Change-Id: I201f8c6e7a57eb9417ab24561d597aea6de03187
---
M pywikibot/bot.py
M pywikibot/throttle.py
M tests/aspects.py
3 files changed, 21 insertions(+), 21 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index eeb1edb..16a02bf 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -172,6 +172,7 @@
stdout,
warning,
)
+from pywikibot.throttle import Throttle
from pywikibot.tools import (
PYTHON_VERSION,
deprecated,
@@ -389,23 +390,11 @@
# if user has enabled file logging, configure file handler
if module_name in config.log or '*' in config.log:
- pid = ''
-
- try: # T286848
- site = pywikibot.Site()
- if pywikibot.Site.__doc__ == 'TEST':
- raise ValueError('Running test with aspects.DisableSiteMixin')
- if site is None: # T289427
- raise ValueError('Running script_test with net=False')
- except ValueError:
- pass
- except AttributeError:
- # Insufficient import, pywikibot.Site not available (T298384)
- pass
- else: # get PID
- throttle = site.throttle # initialize a Throttle obj
- pid_int = throttle.get_pid(module_name) # get the global PID
- pid = str(pid_int) + '-' if pid_int > 1 else ''
+ # Use a dummy Throttle to get a PID.
+ # This is necessary because tests may have site disabled.
+ throttle = Throttle('')
+ pid_int = throttle.get_pid(module_name) # get the global PID
+ pid = str(pid_int) + '-' if pid_int > 1 else ''
if config.logfilename:
# keep config.logfilename unchanged
diff --git a/pywikibot/throttle.py b/pywikibot/throttle.py
index 55dce81..8144f70 100644
--- a/pywikibot/throttle.py
+++ b/pywikibot/throttle.py
@@ -47,9 +47,14 @@
Each Site initiates one Throttle object (`site.throttle`) to control
the rate of access.
+ :param site: site or sitename for this Throttle. If site is an empty
+ string, it will not be written to the throttle.ctrl file.
+ :param mindelay: The minimal delay, also used for read access
+ :param maxdelay: The maximal delay
+ :param writedelay: The write delay
"""
- def __init__(self, site, *,
+ def __init__(self, site: Union['pywikibot.site.BaseSite', str], *,
mindelay: Optional[int] = None,
maxdelay: Optional[int] = None,
writedelay: Union[int, float, None] = None):
@@ -146,7 +151,11 @@
f.write(FORMAT_LINE.format_map(p._asdict()))
def checkMultiplicity(self):
- """Count running processes for site and set process_multiplicity."""
+ """Count running processes for site and set process_multiplicity.
+
+ .. versionchanged:: 7.0
+ process is not written to throttle.ctrl file is site is empty
+ """
global pid
mysite = self.mysite
pywikibot.debug('Checking multiplicity: pid = {pid}'.format(pid=pid),
@@ -165,7 +174,7 @@
and proc.site == mysite \
and proc.pid != pid:
count += 1
- if proc.site != self.mysite or proc.pid != pid:
+ if proc.site != mysite or proc.pid != pid:
processes.append(proc)
free_pid = (i for i in itertools.count(start=1)
@@ -179,6 +188,9 @@
time=self.checktime, site=mysite))
self.modules = Counter(p.module_id for p in processes)
+ if not mysite:
+ del processes[-1]
+
self._write_file(sorted(processes, key=lambda p: p.pid))
self.process_multiplicity = count
diff --git a/tests/aspects.py b/tests/aspects.py
index 941136e..c4e4ca1 100644
--- a/tests/aspects.py
+++ b/tests/aspects.py
@@ -326,7 +326,6 @@
self.old_Site_lookup_method = pywikibot.Site
pywikibot.Site = lambda *args: self.fail(
'{}: Site() not permitted'.format(self.__class__.__name__))
- pywikibot.Site.__doc__ = 'TEST'
super().setUp()
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/766175
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I201f8c6e7a57eb9417ab24561d597aea6de03187
Gerrit-Change-Number: 766175
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]