Commits

Barry Warsaw committed 385f1b8

Add some clarification and examples based on Guido's feedback.

  • Participants
  • Parent commits 0bb2934

Comments (0)

Files changed (1)

File pep-0420.txt

  * If the module has an ``__loader__`` and that loader has a ``module_repr()``
    method, call it with a single argument, which is the module object.  The
    value returned is used as the module's repr.
- * Exceptions from ``module_repr()`` are ignored, and the following steps
-   are used instead.
+ * If an exception occurs in ``module_repr()``, the exception is
+   caught and discarded, and the calculation of the module's repr
+   continues as if ``module_repr()`` did not exist.
  * If the module has an ``__file__`` attribute, this is used as part of the
    module's repr.
  * If the module has no ``__file__`` but does have an ``__loader__``, then the
    loader's repr is used as part of the module's repr.
  * Otherwise, just use the module's ``__name__`` in the repr.
 
+Here is a snippet showing how namespace module reprs are calculated
+from its loader::
+
+    class NamespaceLoader:
+        @classmethod
+        def module_repr(cls, module):
+            return "<module '{}' (namespace)>".format(module.__name__)
+
+Built-in module reprs would no longer need to be hard-coded, but
+instead would come from their loader as well::
+
+    class BuiltinImporter:
+        @classmethod
+        def module_repr(cls, module):
+            return "<module '{}' (built-in)>".format(module.__name__)
+
+Here are some example reprs of different types of modules with
+different sets of the related attributes::
+
+    >>> import email
+    >>> email
+    <module 'email' from '/home/barry/projects/python/pep-420/Lib/email/__init__.py'>
+    >>> m = type(email)('foo')
+    >>> m
+    <module 'foo'>
+    >>> m.__file__ = 'zippy:/de/do/dah'
+    >>> m
+    <module 'foo' from 'zippy:/de/do/dah'>
+    >>> class Loader: pass
+    ... 
+    >>> m.__loader__ = Loader
+    >>> del m.__file__
+    >>> m
+    <module 'foo' (<class '__main__.Loader'>)>
+    >>> class NewLoader:
+    ...   @classmethod
+    ...   def module_repr(cls, module):
+    ...      return '<mystery module!>'
+    ... 
+    >>> m.__loader__ = NewLoader
+    >>> m
+    <mystery module!>
+    >>> 
+
 
 References
 ==========