Page MenuHomePhabricator

Upload by URL fails when overwriting a file: "Can request stash information only when using a file name."
Closed, ResolvedPublic

Description

When I try to use FilePage.upload() (or APISite.upload()) to replace an existing file from a URL, I get a ValueError, "Can request stash information only when using a file name." Here's a trivial test case (running in a valid bot account, targetting Commons):

import pywikibot

site = pywikibot.Site()

page = pywikibot.FilePage(site, "File:Barth Bridge.jpg")
url = "http://www.geograph.org.uk/reuse.php?id=3629869&download=b6f1d3b9&size=original"

site.upload(page, source_url=url, ignore_warnings=["exists"],
            comment="Higher-resolution version from Geograph.")

I'd expect this to successfully overwrite the file, but instead it does this:

Sleeping for 7.9 seconds, 2017-08-28 16:23:54
Traceback (most recent call last):
  File "pwb.py", line 262, in <module>
    if not main():
  File "pwb.py", line 255, in main
    run_python_file(filename, [filename] + args, argvu, file_package)
  File "pwb.py", line 121, in run_python_file
    main_mod.__dict__)
  File "../scripts/uploadtest.py", line 9, in <module>
    comment="Higher-resolution version from Geograph.")
  File "/home/bjh21/geograph-update-bot/pywikibot-core/pywikibot/tools/__init__.py", line 1458, in wrapper
    return obj(*__args, **__kw)
  File "/home/bjh21/geograph-update-bot/pywikibot-core/pywikibot/site.py", line 6363, in upload
    result['offset'], report_success=False)
  File "/home/bjh21/geograph-update-bot/pywikibot-core/pywikibot/tools/__init__.py", line 1458, in wrapper
    return obj(*__args, **__kw)
  File "/home/bjh21/geograph-update-bot/pywikibot-core/pywikibot/site.py", line 6149, in upload
    raise ValueError('Can request stash information only when '
ValueError: Can request stash information only when using a file name.
<class 'ValueError'>
CRITICAL: Closing network session.

version.py output:

Pywikibot: [https] r-pywikibot-core.git (f07e4bb, g8564, 2017/08/27, 22:45:37, n/a)
Release version: 3.0-dev
requests version: 2.9.1
  cacerts: /etc/ssl/certs/ca-certificates.crt
    certificate test: ok
Python: 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609]
PYWIKIBOT2_DIR: Not set
PYWIKIBOT2_DIR_PWB: 
PYWIKIBOT2_NO_USER_CONFIG: Not set
Config base dir: /home/bjh21/geograph-update-bot/pywikibot-core
Usernames for family "commons":
	commons: Geograph Update Bot (no sysop configured)

This patch works around the problem for me, but I really don't think it's correct. I think it causes pywikibot to assume that the stashed upload is valid rather than trying to check it against a local file:

diff --git a/pywikibot/site.py b/pywikibot/site.py
index 0e5efbd..cd0927b 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -6355,7 +6355,7 @@ class APISite(BaseSite):
                 pywikibot.warning('No filekey defined.')
             if not report_success:
                 if 'offset' not in result:
-                    result['offset'] = True
+                    result['offset'] = False
                 if ignore_warnings(create_warnings_list(result)):
                     return self.upload(
                         filepage, source_filename, source_url, comment, text,