Last modified: 2014-11-09 17:44:53 UTC

Wikimedia Bugzilla is closed!

Wikimedia migrated from Bugzilla to Phabricator. Bug reports are handled in Wikimedia Phabricator.
This static website is read-only and for historical purposes. It is not possible to log in and except for displaying bug reports and their history, links might be broken. See T74120, the corresponding Phabricator task for complete and up-to-date bug report information.
Bug 72120 - pagegenerators.GeneratorFactory() uses wrong site if instantiated before command line args have been processed
pagegenerators.GeneratorFactory() uses wrong site if instantiated before comm...
Status: NEW
Product: Pywikibot
Classification: Unclassified
General (Other open bugs)
core-(2.0)
All All
: Normal minor
: ---
Assigned To: Pywikipedia bugs
:
Depends on:
Blocks: pwb30
  Show dependency treegraph
 
Reported: 2014-10-16 01:43 UTC by John Mark Vandenberg
Modified: 2014-11-09 17:44 UTC (History)
1 user (show)

See Also:
Web browser: ---
Mobile Platform: ---
Assignee Huggle Beta Tester: ---


Attachments

Description John Mark Vandenberg 2014-10-16 01:43:30 UTC
In the current code, pywikibot.handleArgs() must be called before pagegenerators.GeneratorFactory(), as pagegenerators.GeneratorFactory.__init__() calls pywikibot.Site().

If pagegenerators.GeneratorFactory() is called first, the default site per user-config is used, and command line args (-family -lang -user) are ignored.  See bug 63800.

This could be almost completely fixed by changing GeneratorFactory.site to be a property, loaded on access.  That prevents the typical coding bug which look like:

  genFactory = pagegenerators.GeneratorFactory()
  for arg in pywikibot.handleArgs():
    if genFactory.handleArg(arg):
       pass

The current solution is to use and promote the pattern:

  local_args = pywikibot.handleArgs()
  genFactory = pagegenerators.GeneratorFactory()
  for arg in local_args:
    if genFactory.handleArg(arg):
       pass

However it doesnt prevent this:

  genFactory = pagegenerators.GeneratorFactory()
  genFactory.handleArg('-file:' + filename):
  ...
  pywikibot.handleArgs()

One way to prevent that is to raise an exception in pywikibot.handleArgs if it is called after pywikibot.Site() has instantiated a default site, and possibly only if -family/-lang/-user are supplied on the command line.

Another approach (very dodgy) is for pywikibot to know which Site object is the 'default' site, and pywikibot.handleArgs() change that object if -family/-lang/-user are supplied on the command line.
Comment 1 John Mark Vandenberg 2014-10-16 01:51:23 UTC
Enforcing the order of initialisation by adding an exception in pywikibot.handle_args() (previously: handleArgs) or in GeneratorFactory.__init__() would need to be post 2.0
Comment 2 John Mark Vandenberg 2014-10-16 03:14:42 UTC
A better pattern to use and promote is:

  local_args = pywikibot.handleArgs()
  site = pywikibot.Site()
  genFactory = pagegenerators.GeneratorFactory(site)
  for arg in local_args:
    if genFactory.handleArg(arg):
       pass
    ...

As that shows the fact that genFactory needs a site object.
Comment 3 Gerrit Notification Bot 2014-10-16 04:15:11 UTC
Change 166942 had a related patch set uploaded by John Vandenberg:
Use correct site for pagegenerators

https://gerrit.wikimedia.org/r/166942
Comment 4 Gerrit Notification Bot 2014-10-16 15:43:00 UTC
Change 166942 merged by jenkins-bot:
Use correct site for pagegenerators

https://gerrit.wikimedia.org/r/166942
Comment 5 John Mark Vandenberg 2014-10-31 05:24:08 UTC
The last patch is almost a 100% solution, but it doesnt ensure pywikibot.Site() is only being called after handle_args.

Note You need to log in before you can comment on or make changes to this bug.


Navigation
Links