Commit 1f22d8c2 authored by Tim Peters's avatar Tim Peters

A sane scheme for raising deprecation warnings.

parent 847369b1
...@@ -5,11 +5,30 @@ Release date: DD-MMM-2004 ...@@ -5,11 +5,30 @@ Release date: DD-MMM-2004
DB DB
-- --
- The following optional arguments to ``DB.open()`` are deprecated: - There is no longer a hard limit on the number of connections that
``DB.open()`` will create. In other words, ``DB.open()`` never blocks
anymore waiting for an earlier connection to close, and ``DB.open()``
always returns a connection now (while it wasn't documented, it was
possible for ``DB.open()`` to return ``None`` before).
``pool_size`` continues to default to 7, but its meaning has changed:
if more than ``pool_size`` connections are obtained from ``DB.open()``
and not closed, a warning is logged; if more than twice ``pool_size``, a
critical problem is logged. ``pool_size`` should be set to the maximum
number of connections from the ``DB`` instance you expect to have open
simultaneously.
In addition, if a connection obtained from ``DB.open()`` becomes
unreachable without having been explicitly closed, when Python's garbage
collection reclaims that connection it no longer counts against the
``pool_size`` thresholds for logging messages.
The following optional arguments to ``DB.open()`` are deprecated:
``transaction``, ``waitflag``, ``force`` and ``temporary``. If one ``transaction``, ``waitflag``, ``force`` and ``temporary``. If one
is specified, its value is ignored, and ``DeprecationWarning`` is is specified, its value is ignored, and ``DeprecationWarning`` is
raised. In a future release, these optional arguments will be raised. In ZODB 3.6, these optional arguments will be removed.
removed.
Tools Tools
----- -----
......
...@@ -36,6 +36,8 @@ from ZODB.TmpStore import TmpStore ...@@ -36,6 +36,8 @@ from ZODB.TmpStore import TmpStore
from ZODB.utils import oid_repr, z64, positive_id from ZODB.utils import oid_repr, z64, positive_id
from ZODB.serialize import ObjectWriter, ConnectionObjectReader, myhasattr from ZODB.serialize import ObjectWriter, ConnectionObjectReader, myhasattr
from ZODB.interfaces import IConnection from ZODB.interfaces import IConnection
from ZODB.utils import DEPRECATED_ARGUMENT, deprecated36
from zope.interface import implements from zope.interface import implements
global_reset_counter = 0 global_reset_counter = 0
...@@ -264,9 +266,8 @@ class Connection(ExportImport, object): ...@@ -264,9 +266,8 @@ class Connection(ExportImport, object):
method. You can pass a transaction manager (TM) to DB.open() method. You can pass a transaction manager (TM) to DB.open()
to control which TM the Connection uses. to control which TM the Connection uses.
""" """
warnings.warn("getTransaction() is deprecated. " deprecated36("getTransaction() is deprecated. "
"Use the txn_mgr argument to DB.open() instead.", "Use the txn_mgr argument to DB.open() instead.")
DeprecationWarning)
return self._txn_mgr.get() return self._txn_mgr.get()
def setLocalTransaction(self): def setLocalTransaction(self):
...@@ -278,9 +279,8 @@ class Connection(ExportImport, object): ...@@ -278,9 +279,8 @@ class Connection(ExportImport, object):
can pass a transaction manager (TM) to DB.open() to control can pass a transaction manager (TM) to DB.open() to control
which TM the Connection uses. which TM the Connection uses.
""" """
warnings.warn("setLocalTransaction() is deprecated. " deprecated36("setLocalTransaction() is deprecated. "
"Use the txn_mgr argument to DB.open() instead.", "Use the txn_mgr argument to DB.open() instead.")
DeprecationWarning)
if self._txn_mgr is transaction.manager: if self._txn_mgr is transaction.manager:
if self._synch: if self._synch:
self._txn_mgr.unregisterSynch(self) self._txn_mgr.unregisterSynch(self)
...@@ -488,14 +488,14 @@ class Connection(ExportImport, object): ...@@ -488,14 +488,14 @@ class Connection(ExportImport, object):
def cacheFullSweep(self, dt=None): def cacheFullSweep(self, dt=None):
# XXX needs doc string # XXX needs doc string
warnings.warn("cacheFullSweep is deprecated. " deprecated36("cacheFullSweep is deprecated. "
"Use cacheMinimize instead.", DeprecationWarning) "Use cacheMinimize instead.")
if dt is None: if dt is None:
self._cache.full_sweep() self._cache.full_sweep()
else: else:
self._cache.full_sweep(dt) self._cache.full_sweep(dt)
def cacheMinimize(self, dt=None): def cacheMinimize(self, dt=DEPRECATED_ARGUMENT):
"""Deactivate all unmodified objects in the cache. """Deactivate all unmodified objects in the cache.
Call _p_deactivate() on each cached object, attempting to turn Call _p_deactivate() on each cached object, attempting to turn
...@@ -505,9 +505,8 @@ class Connection(ExportImport, object): ...@@ -505,9 +505,8 @@ class Connection(ExportImport, object):
:Parameters: :Parameters:
- `dt`: ignored. It is provided only for backwards compatibility. - `dt`: ignored. It is provided only for backwards compatibility.
""" """
if dt is not None: if dt is not DEPRECATED_ARGUMENT:
warnings.warn("The dt argument to cacheMinimize is ignored.", deprecated36("cacheMinimize() dt= is ignored.")
DeprecationWarning)
self._cache.minimize() self._cache.minimize()
def cacheGC(self): def cacheGC(self):
...@@ -783,8 +782,8 @@ class Connection(ExportImport, object): ...@@ -783,8 +782,8 @@ class Connection(ExportImport, object):
# an oid is being registered. I can't think of any way to # an oid is being registered. I can't think of any way to
# achieve that without assignment to _p_jar. If there is # achieve that without assignment to _p_jar. If there is
# a way, this will be a very confusing warning. # a way, this will be a very confusing warning.
warnings.warn("Assigning to _p_jar is deprecated", deprecated36("Assigning to _p_jar is deprecated, and will be "
DeprecationWarning) "changed to raise an exception.")
elif obj._p_oid in self._added: elif obj._p_oid in self._added:
# It was registered before it was added to _added. # It was registered before it was added to _added.
return return
......
...@@ -25,15 +25,12 @@ from ZODB.broken import find_global ...@@ -25,15 +25,12 @@ from ZODB.broken import find_global
from ZODB.Connection import Connection from ZODB.Connection import Connection
from ZODB.serialize import referencesf from ZODB.serialize import referencesf
from ZODB.utils import WeakSet from ZODB.utils import WeakSet
from ZODB.utils import DEPRECATED_ARGUMENT, deprecated36
import transaction import transaction
logger = logging.getLogger('ZODB.DB') logger = logging.getLogger('ZODB.DB')
# A unique marker for detecting use of deprecated arguments.
_deprecated = object()
class _ConnectionPool(object): class _ConnectionPool(object):
"""Manage a pool of connections. """Manage a pool of connections.
...@@ -189,10 +186,10 @@ class DB(object): ...@@ -189,10 +186,10 @@ class DB(object):
def __init__(self, storage, def __init__(self, storage,
pool_size=7, pool_size=7,
cache_size=400, cache_size=400,
cache_deactivate_after=None, cache_deactivate_after=DEPRECATED_ARGUMENT,
version_pool_size=3, version_pool_size=3,
version_cache_size=100, version_cache_size=100,
version_cache_deactivate_after=None, version_cache_deactivate_after=DEPRECATED_ARGUMENT,
): ):
"""Create an object database. """Create an object database.
...@@ -221,10 +218,10 @@ class DB(object): ...@@ -221,10 +218,10 @@ class DB(object):
self._version_cache_size = version_cache_size self._version_cache_size = version_cache_size
# warn about use of deprecated arguments # warn about use of deprecated arguments
if (cache_deactivate_after is not None or if cache_deactivate_after is not DEPRECATED_ARGUMENT:
version_cache_deactivate_after is not None): deprecated36("cache_deactivate_after has no effect")
warnings.warn("cache_deactivate_after has no effect", if version_cache_deactivate_after is not DEPRECATED_ARGUMENT:
DeprecationWarning) deprecated36("version_cache_deactivate_after has no effect")
self._miv_cache = {} self._miv_cache = {}
...@@ -483,8 +480,8 @@ class DB(object): ...@@ -483,8 +480,8 @@ class DB(object):
return len(self._storage) return len(self._storage)
def open(self, version='', def open(self, version='',
transaction=_deprecated, temporary=_deprecated, transaction=DEPRECATED_ARGUMENT, temporary=DEPRECATED_ARGUMENT,
force=_deprecated, waitflag=_deprecated, force=DEPRECATED_ARGUMENT, waitflag=DEPRECATED_ARGUMENT,
mvcc=True, txn_mgr=None, synch=True): mvcc=True, txn_mgr=None, synch=True):
"""Return a database Connection for use by application code. """Return a database Connection for use by application code.
...@@ -505,21 +502,20 @@ class DB(object): ...@@ -505,21 +502,20 @@ class DB(object):
register for afterCompletion() calls. register for afterCompletion() calls.
""" """
if temporary is not _deprecated: if temporary is not DEPRECATED_ARGUMENT:
warnings.warn("DB.open() temporary= has no effect", deprecated36("DB.open() temporary= ignored. "
DeprecationWarning) "open() no longer blocks.")
if force is not _deprecated: if force is not DEPRECATED_ARGUMENT:
warnings.warn("DB.open() force= has no effect", deprecated36("DB.open() force= ignored. "
DeprecationWarning) "open() no longer blocks.")
if waitflag is not _deprecated: if waitflag is not DEPRECATED_ARGUMENT:
warnings.warn("DB.open() waitflag= has no effect", deprecated36("DB.open() waitflag= ignored. "
DeprecationWarning) "open() no longer blocks.")
if transaction is not _deprecated: if transaction is not DEPRECATED_ARGUMENT:
warnings.warn("DB.open() transaction= has no effect", deprecated36("DB.open() transaction= ignored.")
DeprecationWarning)
self._a() self._a()
try: try:
...@@ -686,23 +682,19 @@ class DB(object): ...@@ -686,23 +682,19 @@ class DB(object):
def getCacheDeactivateAfter(self): def getCacheDeactivateAfter(self):
"""Deprecated""" """Deprecated"""
warnings.warn("cache_deactivate_after has no effect", deprecated36("getCacheDeactivateAfter has no effect")
DeprecationWarning)
def getVersionCacheDeactivateAfter(self): def getVersionCacheDeactivateAfter(self):
"""Deprecated""" """Deprecated"""
warnings.warn("cache_deactivate_after has no effect", deprecated36("getVersionCacheDeactivateAfter has no effect")
DeprecationWarning)
def setCacheDeactivateAfter(self, v): def setCacheDeactivateAfter(self, v):
"""Deprecated""" """Deprecated"""
warnings.warn("cache_deactivate_after has no effect", deprecated36("setCacheDeactivateAfter has no effect")
DeprecationWarning)
def setVersionCacheDeactivateAfter(self, v): def setVersionCacheDeactivateAfter(self, v):
"""Deprecated""" """Deprecated"""
warnings.warn("cache_deactivate_after has no effect", deprecated36("setVersionCacheDeactivateAfter has no effect")
DeprecationWarning)
class ResourceManager(object): class ResourceManager(object):
"""Transaction participation for a version or undo resource.""" """Transaction participation for a version or undo resource."""
......
...@@ -414,8 +414,9 @@ class UserMethodTests(unittest.TestCase): ...@@ -414,8 +414,9 @@ class UserMethodTests(unittest.TestCase):
>>> len(hook.warnings) >>> len(hook.warnings)
1 1
>>> message, category, filename, lineno = hook.warnings[0] >>> message, category, filename, lineno = hook.warnings[0]
>>> message >>> print message
'The dt argument to cacheMinimize is ignored.' This will be removed in ZODB 3.6:
cacheMinimize() dt= is ignored.
>>> category.__name__ >>> category.__name__
'DeprecationWarning' 'DeprecationWarning'
>>> hook.clear() >>> hook.clear()
...@@ -434,8 +435,9 @@ class UserMethodTests(unittest.TestCase): ...@@ -434,8 +435,9 @@ class UserMethodTests(unittest.TestCase):
>>> len(hook.warnings) >>> len(hook.warnings)
2 2
>>> message, category, filename, lineno = hook.warnings[0] >>> message, category, filename, lineno = hook.warnings[0]
>>> message >>> print message
'cacheFullSweep is deprecated. Use cacheMinimize instead.' This will be removed in ZODB 3.6:
cacheFullSweep is deprecated. Use cacheMinimize instead.
>>> category.__name__ >>> category.__name__
'DeprecationWarning' 'DeprecationWarning'
>>> message, category, filename, lineno = hook.warnings[1] >>> message, category, filename, lineno = hook.warnings[1]
......
...@@ -243,9 +243,13 @@ class ZODBTests(unittest.TestCase): ...@@ -243,9 +243,13 @@ class ZODBTests(unittest.TestCase):
self.assertEqual(r1['item'], 2) self.assertEqual(r1['item'], 2)
self.assertEqual(r2['item'], 2) self.assertEqual(r2['item'], 2)
for msg, obj, filename, lineno in hook.warnings: for msg, obj, filename, lineno in hook.warnings:
self.assert_( self.assert_(msg in [
msg.startswith("setLocalTransaction() is deprecated.") or "This will be removed in ZODB 3.6:\n"
msg.startswith("getTransaction() is deprecated.")) "setLocalTransaction() is deprecated. "
"Use the txn_mgr argument to DB.open() instead.",
"This will be removed in ZODB 3.6:\n"
"getTransaction() is deprecated. "
"Use the txn_mgr argument to DB.open() instead."])
finally: finally:
conn1.close() conn1.close()
conn2.close() conn2.close()
......
...@@ -19,6 +19,7 @@ from binascii import hexlify ...@@ -19,6 +19,7 @@ from binascii import hexlify
import cPickle import cPickle
import cStringIO import cStringIO
import weakref import weakref
import warnings
from persistent.TimeStamp import TimeStamp from persistent.TimeStamp import TimeStamp
...@@ -36,8 +37,26 @@ __all__ = ['z64', ...@@ -36,8 +37,26 @@ __all__ = ['z64',
'get_refs', 'get_refs',
'readable_tid_repr', 'readable_tid_repr',
'WeakSet', 'WeakSet',
'DEPRECATED_ARGUMENT',
'deprecated36',
] ]
# A unique marker to give as the default value for a deprecated argument.
# The method should then do a
#
# if that_arg is not DEPRECATED_ARGUMENT:
# complain
#
# dance.
DEPRECATED_ARGUMENT = object()
# Raise DeprecationWarning, noting that the deprecated thing will go
# away in ZODB 3.6. Point to the caller of our caller (i.e., at the
# code using the deprecated thing).
def deprecated36(msg):
warnings.warn("This will be removed in ZODB 3.6:\n%s" % msg,
DeprecationWarning, stacklevel=3)
z64 = '\0'*8 z64 = '\0'*8
# TODO The purpose of t32 is unclear. Code that uses it is usually # TODO The purpose of t32 is unclear. Code that uses it is usually
......
...@@ -261,9 +261,10 @@ class Transaction(object): ...@@ -261,9 +261,10 @@ class Transaction(object):
self._resources.append(adapter) self._resources.append(adapter)
def begin(self): def begin(self):
warnings.warn("Transaction.begin() should no longer be used; use " from ZODB.utils import deprecated36
"the begin() method of a transaction manager.",
DeprecationWarning, stacklevel=2) deprecated36("Transaction.begin() should no longer be used; use "
"the begin() method of a transaction manager.")
if (self._resources or if (self._resources or
self._sub or self._sub or
self._nonsub or self._nonsub or
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment