Page MenuHomePhabricator

Using imagefile.touch() with pwb.py will throw pywikibot.i18n.TranslationError
Closed, ResolvedPublicBUG REPORT

Description

I use pwb.py as a wrapper to run scripts. If I do an imagefile.touch(). Since recently this throws a :

  File "/home/me/pywikibot/pywikibot/page/__init__.py", line 1459, in touch
    summary = i18n.twtranslate(self.site, 'pywikibot-touch')
  File "/home/me/pywikibot/pywikibot/tools/__init__.py", line 1790, in wrapper
    return obj(*__args, **__kw)
  File "/home/me/pywikibot/pywikibot/i18n.py", line 721, in twtranslate
    % (_messages_package_name, twtitle, __url__))
pywikibot.i18n.TranslationError: Unable to load messages package scripts.i18n for bundle pywikibot-touch
It can happen due to lack of i18n submodule or files. See https://www.mediawiki.org/wiki/Manual:Pywikibot/i18n
CRITICAL: Exiting due to uncaught exception <class 'pywikibot.i18n.TranslationError'>

This used to work until quite recently. Just python directly is no problem

>>> import pywikibot
>>> site = pywikibot.Site('commons', 'commons')
>>> imagefile = pywikibot.FilePage(site, title='File:Jan_Steen_-_The_Drawing_Lesson_-_83.PB.388_-_J._Paul_Getty_Museum.jpg')
>>> imagefile.touch()
Page [[commons:File:Jan Steen - The Drawing Lesson - 83.PB.388 - J. Paul Getty Museum.jpg]] saved
>>>

If I throw these 4 lines in a file and run it:

$ python3 pwb.py ~/temp/touch_i18n.problem.py 

I get the exception.

Using the latest git version of pywikibot and the i18n part:

Pywikibot: [ssh] pywikibot-core.git (0a544a2, g12235, 2020/04/04, 15:23:52, ok)

Probably recently the logic of pwb.py got altered a bit?

Details

Event Timeline

Dvorapa changed the subtype of this task from "Task" to "Bug Report".Apr 4 2020, 6:10 PM

Cannot reproduce (also latest version of pywikibot and i18n):

$ python pwb.py shell
>>> site = pywikibot.Site('commons', 'commons')
>>> imagefile = pywikibot.FilePage(site, title='File:Jan_Steen_-_The_Drawing_Lesson_-_83.PB.388_-_J._Paul_Getty_Museum.jpg')
>>> imagefile.touch()
Page [[commons:File:Jan Steen - The Drawing Lesson - 83.PB.388 - J. Paul Getty Museum.jpg]] saved
>>> exit()
$ python pwb.py test
Page [[commons:File:Jan Steen - The Drawing Lesson - 83.PB.388 - J. Paul Getty Museum.jpg]] saved
test.py
import pywikibot
site = pywikibot.Site('commons', 'commons')
imagefile = pywikibot.FilePage(site, title='File:Jan_Steen_-_The_Drawing_Lesson_-_83.PB.388_-_J._Paul_Getty_Museum.jpg')
imagefile.touch()

Oh, I see. The script is outside Pywikibot folder!

Xqt triaged this task as High priority.EditedApr 5 2020, 8:22 AM

This problem was introduced after 3.0.2020306. It was caused by this change: rPWBC856e3c87

Xqt removed Xqt as the assignee of this task.Apr 5 2020, 9:09 AM

This is very ugly. Currently the sys.path is

['C:\\pwb\\GIT\\core',
 'C:\\Python38\\python38.zip',
 'C:\\Python38\\DLLs',
 'C:\\Python38\\lib',
 'C:\\Python38',
 'C:\\Python38\\lib\\site-packages']

and the script a outside scripts or userpath fails as described above. Duplicating the absolute_path like

['C:\\pwb\\GIT\\core',
 'C:\\pwb\\GIT\\core',
 'C:\\Python38\\python38.zip',
 'C:\\Python38\\DLLs',
 'C:\\Python38\\lib',
 'C:\\Python38',
 'C:\\Python38\\lib\\site-packages']

works for me.

Ah, I see: sys.path[0] is overwritten in pwb.py line 95:

sys.path[0] = os.path.dirname(filename)

Change 586017 had a related patch set uploaded (by Xqt; owner: Xqt):
[pywikibot/core@master] [bugfix] insert absolute path to sys.path

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

Change 586017 merged by jenkins-bot:
[pywikibot/core@master] [bugfix] insert absolute path to sys.path

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

Patch quickly merged, but I don't like the fact the first path is overridden. Is there any reason for this? Could we only add paths instead of overriding?

Patch quickly merged, but I don't like the fact the first path is overridden. Is there any reason for this? Could we only add paths instead of overriding?

I agree, this was a q&d hack to solve this shortly. I also plan to push a new relaise due to this mayor regression bug today. But we have to investigate a bit more why the file path is replaced instead of just inserted.