Page MenuHomePhabricator

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


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 = ""

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 "", line 262, in <module>
    if not main():
  File "", line 255, in main
    run_python_file(filename, [filename] + args, argvu, file_package)
  File "", line 121, in run_python_file
  File "../scripts/", line 9, in <module>
    comment="Higher-resolution version from Geograph.")
  File "/home/bjh21/geograph-update-bot/pywikibot-core/pywikibot/tools/", line 1458, in wrapper
    return obj(*__args, **__kw)
  File "/home/bjh21/geograph-update-bot/pywikibot-core/pywikibot/", line 6363, in upload
    result['offset'], report_success=False)
  File "/home/bjh21/geograph-update-bot/pywikibot-core/pywikibot/tools/", line 1458, in wrapper
    return obj(*__args, **__kw)
  File "/home/bjh21/geograph-update-bot/pywikibot-core/pywikibot/", 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. 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]
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/ b/pywikibot/
index 0e5efbd..cd0927b 100644
--- a/pywikibot/
+++ b/pywikibot/
@@ -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,