← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:py3-formatter-registration into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:py3-formatter-registration into launchpad:master.

Commit message:
Adjust formatter registration for Python 3

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/384859

Register lp.app.browser.tales.NumberFormatterAPI and lp.app.browser.stringformatter.FormattersAPI for the appropriate interfaces in zope.interface.common rather than directly for the corresponding builtin types; this allows the registrations to work on Python 3 as well, where the builtin type names are different.

lp.app.browser.tales.NoneFormatter needs a slightly different adjustment: types.NoneType doesn't exist in Python 3, but we can define it ourselves as type(None).
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-formatter-registration into launchpad:master.
diff --git a/lib/lp/app/browser/configure.zcml b/lib/lp/app/browser/configure.zcml
index bd4795c..be9d898 100644
--- a/lib/lp/app/browser/configure.zcml
+++ b/lib/lp/app/browser/configure.zcml
@@ -664,28 +664,14 @@
       />
 
   <adapter
-      for="int"
-      provides="zope.traversing.interfaces.IPathAdapter"
-      factory="lp.app.browser.tales.NumberFormatterAPI"
-      name="fmt"
-      />
-
-  <adapter
-      for="long"
-      provides="zope.traversing.interfaces.IPathAdapter"
-      factory="lp.app.browser.tales.NumberFormatterAPI"
-      name="fmt"
-      />
-
-  <adapter
-      for="float"
+      for="zope.interface.common.numbers.IReal"
       provides="zope.traversing.interfaces.IPathAdapter"
       factory="lp.app.browser.tales.NumberFormatterAPI"
       name="fmt"
       />
 
   <adapter
-      for="types.NoneType"
+      for="lp.app.browser.tales.NoneType"
       provides="zope.traversing.interfaces.IPathAdapter"
       factory="lp.app.browser.tales.NoneFormatter"
       name="fmt"
@@ -964,8 +950,17 @@
       />
 
   <!-- TALES fmt: namespace for strings -->
+  <!-- On Python 2, registering this adapter for both INativeString and
+       ITextString covers both str (bytes) and unicode.  On Python 3, this
+       covers only str; the redundancy is harmless. -->
+  <adapter
+      for="zope.interface.common.builtins.INativeString"
+      provides="zope.traversing.interfaces.IPathAdapter"
+      factory="lp.app.browser.stringformatter.FormattersAPI"
+      name="fmt"
+      />
   <adapter
-      for="basestring"
+      for="zope.interface.common.builtins.ITextString"
       provides="zope.traversing.interfaces.IPathAdapter"
       factory="lp.app.browser.stringformatter.FormattersAPI"
       name="fmt"
diff --git a/lib/lp/app/browser/tales.py b/lib/lp/app/browser/tales.py
index 8e4921e..89dee84 100644
--- a/lib/lp/app/browser/tales.py
+++ b/lib/lp/app/browser/tales.py
@@ -485,6 +485,12 @@ class DBSchemaAPI:
             raise TraversalError(name)
 
 
+# Python 3 doesn't have types.NoneType, but we still need to be able to
+# refer to the type of None from ZCML so that we can register the
+# NoneFormatter adapter.
+NoneType = type(None)
+
+
 @implementer(ITraversable)
 class NoneFormatter:
     """Adapter from None to various string formats.