Xqt has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1358479?usp=email )

Change subject: [bugfix] Use the page's data repository for its coordinates
......................................................................

[bugfix] Use the page's data repository for its coordinates

Page.coordinates() created each Coordinate without a site, so
Coordinate fell back to pywikibot.Site().data_repository(). Reading
the coordinates of a dewiki page with test.wikipedia as the default
site bound them to test.wikidata and opened test.wikipedia, where the
login can fail with NoUsernameError.

Pass the data repository of the page's site to the Coordinate objects.

Bug: T363303
Change-Id: I8eebfb8dbb401652280bcce9af8bbe33e09b1a61
---
M pywikibot/data/api/_generators.py
M tests/page_tests.py
2 files changed, 47 insertions(+), 1 deletion(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified




diff --git a/pywikibot/data/api/_generators.py 
b/pywikibot/data/api/_generators.py
index 7ff9221..81e4a06 100644
--- a/pywikibot/data/api/_generators.py
+++ b/pywikibot/data/api/_generators.py
@@ -1086,6 +1086,9 @@

 def _update_coordinates(page, coordinates) -> None:
     """Update page coordinates."""
+    # use the data repository of the page's site; the default site may
+    # be another wiki where the user cannot log in (T363303)
+    repo = page.site.data_repository()
     coords = []
     for co in coordinates:
         coord = pywikibot.Coordinate(lat=co['lat'],
@@ -1094,7 +1097,8 @@
                                      name=co.get('name', ''),
                                      dim=int(co.get('dim', 0)) or None,
                                      globe=co['globe'],  # See [[gerrit:67886]]
-                                     primary='primary' in co
+                                     primary='primary' in co,
+                                     site=repo,
                                      )
         coords.append(coord)
     page._coords = coords
diff --git a/tests/page_tests.py b/tests/page_tests.py
index 8652072..f92a502 100755
--- a/tests/page_tests.py
+++ b/tests/page_tests.py
@@ -19,6 +19,7 @@
 import pywikibot
 import pywikibot.page
 from pywikibot import config
+from pywikibot.data import api
 from pywikibot.exceptions import (
     APIError,
     ApiTimeoutError,
@@ -827,6 +828,47 @@
             self.assertTrue(coord.primary)


+class TestPageCoordinatesRepository(TestCase):
+
+    """Test the data repository of Page coordinates."""
+
+    family = 'wikipedia'
+    code = 'de'
+    dry = True
+
+    def test_coordinates_repository(self) -> None:
+        """Test that coordinates do not use the default site (T363303)."""
+        page = pywikibot.Page(self.site, 'Wien')
+        repo = self.site.data_repository()
+        pagedict = {
+            'pageid': 1,
+            'coordinates': [
+                {'lat': 48.208333, 'lon': 16.373056, 'globe': 'earth',
+                 'primary': ''},
+                {'lat': 48.2, 'lon': 16.3, 'globe': 'earth'},
+            ],
+        }
+
+        def loadcoordinfo(page) -> None:
+            api.update_page(page, pagedict, ['coordinates'])
+
+        with mock.patch.object(self.site, 'loadcoordinfo', loadcoordinfo), \
+                mock.patch.object(self.site, 'data_repository',
+                                  return_value=repo), \
+                mock.patch.object(pywikibot, 'Site',
+                                  side_effect=AssertionError(
+                                      'default site was used')):
+            coords = page.coordinates()
+
+        self.assertLength(coords, 2)
+        for coord in coords:
+            self.assertIs(coord.site, repo)
+        self.assertEqual((coords[0].lat, coords[0].lon),
+                         (48.208333, 16.373056))
+        self.assertTrue(coords[0].primary)
+        self.assertFalse(coords[1].primary)
+
+
 class TestPageGetFileHistory(DefaultSiteTestCase):

     """Test the get_file_history method of the FilePage class."""

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1358479?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I8eebfb8dbb401652280bcce9af8bbe33e09b1a61
Gerrit-Change-Number: 1358479
Gerrit-PatchSet: 3
Gerrit-Owner: AkashKumar7902 <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: Mahveotm <[email protected]>
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to