Files
openide/python/helpers/pydev/pydev_tests/test_pydevconsole_autocomplete.py
Egor Eliseev b14ff0c721 PY-38904 Fix test 'console_requests' with Python 2.7
Add 'parse_argspec_for_PY2(obj)' function to '_pydev_calltip_util.py' file.
Python2 hasn't 'inspect.getfullargspec(obj)' method and 'inspect.getargspec(obj)' can't work with buildin functions/methods(throws TypeError).
But we can try parse 'obj.__doc__' and create 'inspect.ArgSpec' by ourselves.

GitOrigin-RevId: a05046b86049daacd439187b1a0a7fbb0448f410
2022-01-24 20:15:31 +00:00

81 lines
3.1 KiB
Python

# Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
import inspect
import sys
import unittest
from _pydev_bundle._pydev_calltip_util import getargspec_py2, is_bound_method
from _pydevd_bundle import pydevd_io
from _pydevd_bundle.pydevd_constants import IS_PY2
def get_fob(obj):
"""A helper function that simulates the transformation of an object
as a function 'get_description(obj)' from '_pydev_calltip_util.py'"""
try:
ob_call = obj.__call__
except:
ob_call = None
if isinstance(obj, type) or type(obj).__name__ == 'classobj':
fob = getattr(obj, '__init__', lambda: None)
if not callable(fob):
fob = obj
elif is_bound_method(ob_call):
fob = ob_call
else:
fob = obj
return fob
class Test(unittest.TestCase):
"""Test checks the correctness of 'getargspec_py2(obj)' from '_pydev_calltip_util.py'.
The function is needed because in Python 2 we don't have 'inspect.getfullargspec(obj)'.
And 'inspect.getargspec(obj)' doesn't work with build-in functions.
Therefore, we are trying to get args from the object.__doc__"""
def test_getargspec_py2(self):
if IS_PY2:
self.original_stdout = sys.stdout
sys.stdout = pydevd_io.IOBuf()
try:
# dict -> dict()
actual = getargspec_py2(dict)
expected = inspect.ArgSpec(args=[], varargs=None, keywords=None,
defaults=())
self.assertEqual(expected, actual)
# vars -> vars([object])
fob = get_fob(vars)
actual = getargspec_py2(fob)
expected = inspect.ArgSpec(args=['[object]'], varargs=None,
keywords=None,
defaults=())
self.assertEqual(expected, actual)
# str.join -> str.join(iterable)
val = ""
fob = get_fob(val.join)
actual = getargspec_py2(fob)
expected = inspect.ArgSpec(args=['iterable'], varargs=None,
keywords=None,
defaults=())
self.assertEqual(expected, actual)
# list -> list()
actual = getargspec_py2(list)
expected = inspect.ArgSpec(args=[], varargs=None, keywords=None,
defaults=())
self.assertEqual(expected, actual)
# compile -> compile(source, filename, mode[, flags[, dont_inherit]])
fob = get_fob(compile)
actual = getargspec_py2(fob)
expected = inspect.ArgSpec(
args=['source', 'filename', 'mode[', 'flags[', 'dont_inherit]]'],
varargs=None, keywords=None, defaults=())
self.assertEqual(expected, actual)
finally:
sys.stdout = self.original_stdout