jenkins-bot has submitted this change and it was merged.

Change subject: Page.namespace() shall return Namespace() for pages in Main ns
......................................................................


Page.namespace() shall return Namespace() for pages in Main ns

For pages in Main ns, page.namespace() shall return a Namespace()
object instead on an int().

Note: bool(namespace) is now True when namespace is Main.
So explicit test vs. Namespace.MAIN is needed to discriminate if
namespace is Main or not.

Bug: T104864
Change-Id: I15667db612b5e3d7a65319e203065a0b33aa45ad
---
M pywikibot/page.py
1 file changed, 12 insertions(+), 14 deletions(-)

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



diff --git a/pywikibot/page.py b/pywikibot/page.py
index c0c8294..d2793ac 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -4873,7 +4873,7 @@
             self._source = source or pywikibot.Site()
 
         self._text = text
-        self._defaultns = defaultNamespace
+        self._defaultns = self._source.namespaces[defaultNamespace]
 
         # preprocess text (these changes aren't site-dependent)
         # First remove anchor, which is stored unchanged, if there is one
@@ -4988,7 +4988,7 @@
         while u":" in t:
             # Initial colon indicates main namespace rather than default
             if t.startswith(u":"):
-                self._namespace = 0
+                self._namespace = self._site.namespaces[0]
                 # remove the colon but continue processing
                 # remove any subsequent whitespace
                 t = t.lstrip(u":").lstrip(u" ")
@@ -5146,7 +5146,8 @@
 
     def canonical_title(self):
         """Return full page title, including localized namespace."""
-        if self.namespace:
+        # Avoid that ':' will be added to the title for Main ns.
+        if self.namespace != Namespace.MAIN:
             return "%s:%s" % (self.site.namespace(self.namespace),
                               self.title)
         else:
@@ -5162,26 +5163,23 @@
 
         @raise pywikibot.Error: no corresponding namespace is found in onsite
         """
-        ns_id = self.namespace
-        ns = self.site.namespaces[ns_id]
-
         if onsite is None:
-            namespace = ns.canonical_name
+            name = self.namespace.canonical_name
         else:
             # look for corresponding ns in onsite by name comparison
-            for alias in ns:
+            for alias in self.namespace:
                 namespace = onsite.namespaces.lookup_name(alias)
-                if namespace:
-                    namespace = namespace.custom_name
+                if namespace is not None:
+                    name = namespace.custom_name
                     break
             else:
                 # not found
                 raise pywikibot.Error(
                     u'No corresponding namespace found for namespace %s on %s.'
-                    % (self.site.namespaces[ns_id], onsite))
+                    % (self.namespace, onsite))
 
-        if namespace:
-            return u'%s:%s' % (namespace, self.title)
+        if self.namespace != Namespace.MAIN:
+            return u'%s:%s' % (name, self.title)
         else:
             return self.title
 
@@ -5196,7 +5194,7 @@
         if onsite is None:
             onsite = self._source
         title = self.title
-        if self.namespace:
+        if self.namespace != Namespace.MAIN:
             title = onsite.namespace(self.namespace) + ":" + title
         if self.section:
             title = title + "#" + self.section

-- 
To view, visit https://gerrit.wikimedia.org/r/279726
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I15667db612b5e3d7a65319e203065a0b33aa45ad
Gerrit-PatchSet: 8
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Mpaa <[email protected]>
Gerrit-Reviewer: John Vandenberg <[email protected]>
Gerrit-Reviewer: Mpaa <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to