[Zope3-checkins] SVN: Zope3/branches/jim-adapter/src/zope/deprecation/ Changed the way moved modules are handled.

Jim Fulton jim at zope.com
Wed Apr 26 07:34:51 EDT 2006


Log message for revision 67616:
  Changed the way moved modules are handled.
  
  Rather than making the old locations aliases for the new locations, we 
  copy objects from the new locations.  The copied objects of interest,
  namely classes and functions retain __module__ attributes that point
  to the new location, so they will have the correct data when they are
  re-pickled.  Making module aliases caused subtle bugs when packages
  were moved.  Moved package sub-modules ended up being suplication
  under some circumstances.
  

Changed:
  U   Zope3/branches/jim-adapter/src/zope/deprecation/README.txt
  U   Zope3/branches/jim-adapter/src/zope/deprecation/deprecation.py
  U   Zope3/branches/jim-adapter/src/zope/deprecation/tests.py

-=-
Modified: Zope3/branches/jim-adapter/src/zope/deprecation/README.txt
===================================================================
--- Zope3/branches/jim-adapter/src/zope/deprecation/README.txt	2006-04-26 10:54:16 UTC (rev 67615)
+++ Zope3/branches/jim-adapter/src/zope/deprecation/README.txt	2006-04-26 11:34:51 UTC (rev 67616)
@@ -191,12 +191,103 @@
   >>> zope.deprecation.old_location.x
   42
 
-and the old module will be an alias for the new:
+Moving packages
+---------------
 
-  >>> (sys.modules['zope.deprecation.old_location']
-  ...  is sys.modules['zope.deprecation.new_location'])
+When moving packages, you need to leave placeholders for each 
+module.  Let's look at an example:
+
+  >>> create_module({
+  ... 'new_package.__init__': '''\
+  ... print __name__, 'imported'
+  ... x=0
+  ... ''',
+  ... 'new_package.m1': '''\
+  ... print __name__, 'imported'
+  ... x=1
+  ... ''',
+  ... 'new_package.m2': '''\
+  ... print __name__, 'imported'
+  ... def x():
+  ...     pass
+  ... ''',
+  ... 'new_package.m3': '''\
+  ... print __name__, 'imported'
+  ... x=3
+  ... ''',
+  ... 'old_package.__init__': '''\
+  ... import zope.deprecation
+  ... zope.deprecation.moved('zope.deprecation.new_package', 'version 2')
+  ... ''',
+  ... 'old_package.m1': '''\
+  ... import zope.deprecation
+  ... zope.deprecation.moved('zope.deprecation.new_package.m1', 'version 2')
+  ... ''',
+  ... 'old_package.m2': '''\
+  ... import zope.deprecation
+  ... zope.deprecation.moved('zope.deprecation.new_package.m2', 'version 2')
+  ... ''',
+  ... })
+
+
+
+Now, if we import the old modules, we'll get warnings:
+
+  >>> import zope.deprecation.old_package
+  ... # doctest: +NORMALIZE_WHITESPACE
+  From tests.py's showwarning():
+  ...zope/deprecation/README.txt:1: DeprecationWarning:
+  zope.deprecation.old_package has moved to zope.deprecation.new_package.
+  Import of zope.deprecation.old_package will become unsupported in version 2
+    ===============
+  zope.deprecation.new_package imported
+
+  >>> zope.deprecation.old_package.x
+  0
+
+  >>> import zope.deprecation.old_package.m1
+  ... # doctest: +NORMALIZE_WHITESPACE
+  From tests.py's showwarning():
+  ...zope/deprecation/README.txt:1: DeprecationWarning:
+  zope.deprecation.old_package.m1 has moved to zope.deprecation.new_package.m1.
+  Import of zope.deprecation.old_package.m1 will become unsupported in
+  version 2
+    ===============
+  zope.deprecation.new_package.m1 imported
+
+  >>> zope.deprecation.old_package.m1.x
+  1
+
+  >>> import zope.deprecation.old_package.m2
+  ... # doctest: +NORMALIZE_WHITESPACE
+  From tests.py's showwarning():
+  ...zope/deprecation/README.txt:1: DeprecationWarning:
+  zope.deprecation.old_package.m2 has moved to zope.deprecation.new_package.m2.
+  Import of zope.deprecation.old_package.m2 will become unsupported in
+  version 2
+    ===============
+  zope.deprecation.new_package.m2 imported
+
+  >>> zope.deprecation.old_package.m2.x is zope.deprecation.new_package.m2.x
   True
 
+  >>> (zope.deprecation.old_package.m2.x.func_globals
+  ...  is zope.deprecation.new_package.m2.__dict__)
+  True
+
+  >>> zope.deprecation.old_package.m2.x.__module__
+  'zope.deprecation.new_package.m2'
+
+We'll get an error if we try to import m3, because we didn't create a
+placeholder for it:
+
+  >>> import  zope.deprecation.old_package.m3
+  Traceback (most recent call last):
+  ...
+  ImportError: No module named m3
+
+
+
 Temporarily turning off deprecation warnings
 --------------------------------------------
 

Modified: Zope3/branches/jim-adapter/src/zope/deprecation/deprecation.py
===================================================================
--- Zope3/branches/jim-adapter/src/zope/deprecation/deprecation.py	2006-04-26 10:54:16 UTC (rev 67615)
+++ Zope3/branches/jim-adapter/src/zope/deprecation/deprecation.py	2006-04-26 11:34:51 UTC (rev 67616)
@@ -203,4 +203,14 @@
     
     warnings.warn(message, DeprecationWarning, 3)
     __import__(to_location)
-    sys.modules[old] = sys.modules[to_location]
+
+    fromdict = sys.modules[to_location].__dict__
+    tomod = sys.modules[old]
+    tomod.__doc__ = message
+    todict = tomod.__dict__
+
+    for name, v in fromdict.iteritems():
+        if name not in tomod.__dict__:
+            setattr(tomod, name, v)
+
+    

Modified: Zope3/branches/jim-adapter/src/zope/deprecation/tests.py
===================================================================
--- Zope3/branches/jim-adapter/src/zope/deprecation/tests.py	2006-04-26 10:54:16 UTC (rev 67615)
+++ Zope3/branches/jim-adapter/src/zope/deprecation/tests.py	2006-04-26 11:34:51 UTC (rev 67616)
@@ -63,11 +63,15 @@
 def setUpCreateModule(test):
     d = test.globs['tmp_d'] = tempfile.mkdtemp('deprecation')
 
-    def create_module(**modules):
+    def create_module(modules=(), **kw):
+        modules = dict(modules)
+        modules.update(kw)
         for name, src in modules.iteritems():
-            f = open(os.path.join(d, name+'.py'), 'w')
-            f.write(src)
-            f.close()
+            pname = name.split('.')
+            if pname[-1] == '__init__':
+                os.mkdir(os.path.join(d, *pname[:-1]))
+                name = '.'.join(pname[:-1])
+            open(os.path.join(d, *pname)+'.py', 'w').write(src)
             test.globs['created_modules'].append(name)
 
     test.globs['created_modules'] = []



More information about the Zope3-Checkins mailing list