[Checkins] SVN: manuelpi.fakemodule/trunk/src/manuelpi/fakemodule/fakemodule. Updated comments and small restructuring improvements.

Paul Wilson paulalexwilson at gmail.com
Tue Jan 5 07:46:40 EST 2010


Log message for revision 107694:
  Updated comments and small restructuring improvements.
  
  

Changed:
  U   manuelpi.fakemodule/trunk/src/manuelpi/fakemodule/fakemodule.py
  U   manuelpi.fakemodule/trunk/src/manuelpi/fakemodule/fakemodule.txt

-=-
Modified: manuelpi.fakemodule/trunk/src/manuelpi/fakemodule/fakemodule.py
===================================================================
--- manuelpi.fakemodule/trunk/src/manuelpi/fakemodule/fakemodule.py	2010-01-05 12:30:12 UTC (rev 107693)
+++ manuelpi.fakemodule/trunk/src/manuelpi/fakemodule/fakemodule.py	2010-01-05 12:46:40 UTC (rev 107694)
@@ -5,10 +5,11 @@
 import textwrap
 
 FAKE_MODULE_START = re.compile(
-        r'^\.\.\s*module-block::?\s*(?P<module_name>[a-zA-Z_]+)',
+        r'^\.\.\s*module-block::?\s*(?P<module_name>[a-zA-Z_]+\S*)',
         re.MULTILINE)
 FAKE_MODULE_END = re.compile(r'(\n\Z|\n(?=\S))')
-MODULE_NAMESPACE = "manueltest.fake"
+MODULE_NAME = "manueltest"
+MODULE_NAMESPACE = MODULE_NAME + ".fake"
 
 # To store our fake module's lines
 class FakeModuleSnippet(object):
@@ -43,9 +44,9 @@
 
     # Make the module also available through normal import
     if not MODULE_NAMESPACE in sys.modules:
-        sys.modules['manueltest'] = new.module('manueltest')
-        sys.modules['manueltest.fake'] = new.module('manueltest.fake')
-        sys.modules['manueltest'].fake = sys.modules['manueltest.fake']
+        sys.modules[MODULE_NAME] = new.module(MODULE_NAME)
+        sys.modules[MODULE_NAMESPACE] = new.module(MODULE_NAMESPACE)
+        sys.modules[MODULE_NAME].fake = sys.modules[MODULE_NAMESPACE]
 
     # We want to be able to resolve items that are in the surrounding
     # doctest globs. To acheive this, we force all doctest globals into
@@ -53,7 +54,11 @@
     import __builtin__
     __builtin__.__dict__.update(doc_globs)
 
-    exec region.parsed.code in module.__dict__
+    try:
+        exec region.parsed.code in module.__dict__
+        module.__exception__ = None
+    except Exception, e:
+        module.__exception__ = e
 
     # When we exec code within the dictionary namespace as
     # above, the __module__ attributes of the objects created are set

Modified: manuelpi.fakemodule/trunk/src/manuelpi/fakemodule/fakemodule.txt
===================================================================
--- manuelpi.fakemodule/trunk/src/manuelpi/fakemodule/fakemodule.txt	2010-01-05 12:30:12 UTC (rev 107693)
+++ manuelpi.fakemodule/trunk/src/manuelpi/fakemodule/fakemodule.txt	2010-01-05 12:46:40 UTC (rev 107694)
@@ -116,8 +116,8 @@
 
   >>> for module in glob.values():
   ...     print dir(module)
-  ['Foo', '__builtins__', '__doc__', '__file__', '__name__']
-  ['Bar', '__builtins__', '__doc__', '__file__', '__name__']
+  ['Foo', '__builtins__', '__doc__', '__exception__', '__file__', '__name__']
+  ['Bar', '__builtins__', '__doc__', '__exception__', '__file__', '__name__']
 
 We would also want to ensure that we can import the module correctly 
 too. The `fakemodule` system also adds the modules under their own 
@@ -212,8 +212,8 @@
     >>> dc.v
     True
 
-Invalid Modules
-===============
+Invalid Module Names
+====================
 
 The module name must be a valid Python identifier. If, say, it
 begins with a number, the module block will not be matched as a
@@ -238,3 +238,32 @@
   False
 
 There are none - therefore the module definition has been ignored.
+
+Handling Exceptions
+===================
+
+There are certain scenarios where you would like to illustrate that
+a problem occurs within a module, and test that such a problem raises
+an exception. An example might be where you have a construct that can
+only occur at class scope, and would like to illustrate that an exception
+is generated in the case that this occurs at module scope. To support
+this usecase, we create all module objects with a special attribute 
+`__exception__' that stores `None` if was created successfully, or
+otherwise the exception instance of the exception that was raised. To 
+illustrate, we create a module that raises an obvious exception::
+
+.. module-block:: zerodiv
+
+    inf = 10 / 0
+
+We can now interrogate the module object that is available to us in sub-
+sequent tests::
+
+  >>> zerodiv.__exception__ is None
+  False
+  >>> print zerodiv.__exception__
+  integer division or modulo by zero
+  >>> print zerodiv.__exception__.__class__.__name__
+  ZeroDivisionError
+
+.



More information about the checkins mailing list