Handle deprecation of inspect.getargspec

getargspec has been deprecated in py3 with plans to remove it in py3.6.
The recommendation is to move to inspect.signature, but the results
of that call are different than the existing one.

There is also getfullargspec available under py3 that was originally
deprecated, but for the sake of handling 2/3 code, it has been
un-deprecated. This call uses signature internally, but returns a mostly
compatible result with what getargspec did. This handles getargspec
deprecation by just using getfullargspec instead if it is available.

Change-Id: I32dd29bde99f9d59c2b0e14dd6782162963b43ae
This commit is contained in:
Sean McGinnis 2017-11-22 10:25:06 -06:00
parent c682bfad38
commit 7a91726588
2 changed files with 22 additions and 2 deletions

View File

@ -13,7 +13,6 @@
import contextlib
import functools
import inspect
import operator
import threading
import warnings
@ -27,6 +26,7 @@ from oslo_db import exception
from oslo_db import options
from oslo_db.sqlalchemy import engines
from oslo_db.sqlalchemy import orm
from oslo_db.sqlalchemy import utils
class _symbol(object):
@ -970,7 +970,7 @@ class _TransactionContextManager(object):
def __call__(self, fn):
"""Decorate a function."""
argspec = inspect.getargspec(fn)
argspec = utils.getargspec(fn)
if argspec.args[0] == 'self' or argspec.args[0] == 'cls':
context_index = 1
else:

View File

@ -18,6 +18,7 @@
import collections
import contextlib
import inspect as pyinspect
import itertools
import logging
import re
@ -1227,6 +1228,25 @@ def suspend_fk_constraints_for_col_alter(
)
def getargspec(fn):
"""Inspects a function for its argspec.
This is to handle a difference between py2/3. The Python 2.x getargspec
call is deprecated in Python 3.x, with the suggestion to use the signature
call instead.
To keep compatibility with the results, while avoiding deprecation
warnings, this instead will use the getfullargspec instead.
:param fn: The function to inspect.
:returns: The argspec for the function.
"""
if hasattr(pyinspect, 'getfullargspec'):
return pyinspect.getfullargspec(fn)
return pyinspect.getargspec(fn)
class NonCommittingConnectable(object):
"""A ``Connectable`` substitute which rolls all operations back.