remove more Python2 hybridation

This commit is contained in:
Alexandre Detiste
2024-03-22 00:25:15 +01:00
committed by Scott Talbert
parent 0257f755cf
commit 323e78c085
21 changed files with 54 additions and 234 deletions

View File

@@ -24,13 +24,9 @@ from .constants import INHERITANCEROOT
ENOENT = getattr(errno, 'ENOENT', 0)
EPIPE = getattr(errno, 'EPIPE', 0)
if sys.version_info < (3, ):
string_base = basestring
else:
string_base = str
class InheritanceDiagram(object):
class InheritanceDiagram:
"""
Given a list of classes, determines the set of classes that they inherit
from all the way to the root "object", and then is able to generate a
@@ -239,7 +235,7 @@ class InheritanceDiagram(object):
code = self.generate_dot(class_summary)
# graphviz expects UTF-8 by default
if isinstance(code, string_base):
if isinstance(code, str):
code = code.encode('utf-8')
dot_args = ['dot']

View File

@@ -1,25 +1,17 @@
import sys
import os
import re
if sys.version_info < (3,):
from StringIO import StringIO
else:
from io import StringIO
from io import StringIO
from inspect import getmro, getclasstree, getdoc, getcomments
from .utilities import makeSummary, chopDescription, writeSphinxOutput, PickleFile
from .utilities import findControlImages, formatExternalLink, isPython3
from .utilities import findControlImages, formatExternalLink
from .constants import object_types, MODULE_TO_ICON, DOXY_2_REST, SPHINXROOT
from . import templates
EPYDOC_PATTERN = re.compile(r'\S+{\S+}', re.DOTALL)
if sys.version_info < (3,):
reload(sys)
sys.setdefaultencoding('utf-8')
def make_class_tree(tree):
@@ -1016,12 +1008,7 @@ class Property(ChildrenBase):
class Attribute(ChildrenBase):
def __init__(self, name, specs, value):
if isPython3():
specs = str(specs)
else:
specs = unicode(specs)
specs = str(specs)
start, end = specs.find("'"), specs.rfind("'")
specs = specs[start+1:end]

View File

@@ -148,10 +148,7 @@ def analyze_params(obj, signature):
pvalue = pvalue.strip()
if pname in pevals:
try:
if isPython3():
peval = str(pevals[pname])
else:
peval = unicode(pevals[pname])
peval = str(pevals[pname])
except UnicodeDecodeError:
peval = repr(pevals[pname])
except TypeError:
@@ -216,7 +213,7 @@ def inspect_source(method_class, obj, source):
def is_classmethod(instancemethod):
""" Determine if an instancemethod is a classmethod. """
# attribute = (isPython3() and ['__self__'] or ['im_self'])[0]
# attribute = (['__self__'] or ['im_self'])[0]
# if hasattr(instancemethod, attribute):
# return getattr(instancemethod, attribute) is not None
# return False
@@ -284,20 +281,11 @@ def describe_func(obj, parent_class, module_name):
try:
code = None
if method in [object_types.METHOD, object_types.METHOD_DESCRIPTOR, object_types.INSTANCE_METHOD]:
if isPython3():
code = obj.__func__.__code__
else:
code = obj.im_func.func_code
code = obj.__func__.__code__
elif method == object_types.STATIC_METHOD:
if isPython3():
code = obj.__func__.__code__
else:
code = obj.im_func.func_code
code = obj.__func__.__code__
else:
if isPython3():
code = obj.__code__
else:
code = obj.func_code
code = obj.__code__
except AttributeError:
code = None

View File

@@ -25,9 +25,6 @@ from .constants import HTML_REPLACE, TODAY, SPHINXROOT, SECTIONS_EXCLUDE
from .constants import CONSTANT_INSTANCES, WIDGETS_IMAGES_ROOT, SPHINX_IMAGES_ROOT
from .constants import DOCSTRING_KEY
PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3
# ----------------------------------------------------------------------- #
@@ -750,7 +747,7 @@ def removeHeaderImage(text, options):
tag = soup.find('div', 'headerimage')
if tag:
tag.extract()
text = unicode(soup) if PY2 else str(soup)
text = str(soup)
return text
@@ -762,7 +759,7 @@ def tweakModuleIndex(text):
href = tag['href'].split('.html#')
if len(href) == 2 and href[0] == href[1]:
tag['href'] = href[0] + '.html'
return unicode(soup) if PY2 else str(soup)
return str(soup)
def tooltipsOnInheritance(text, class_summary):

View File

@@ -16,14 +16,8 @@ import codecs
import shutil
import re
if sys.version_info < (3,):
import cPickle as pickle
from UserDict import UserDict
string_base = basestring
else:
import pickle
from collections import UserDict
string_base = str
import pickle
from collections import UserDict
# Phoenix-specific imports
from .templates import TEMPLATE_CONTRIB
@@ -448,7 +442,7 @@ def findControlImages(elementOrString):
"""
from etgtools.tweaker_tools import removeWxPrefix
if isinstance(elementOrString, string_base):
if isinstance(elementOrString, str):
class_name = py_class_name = elementOrString.lower()
else:
element = elementOrString
@@ -860,12 +854,6 @@ def formatExternalLink(fullname, inheritance=False):
return full_page
def isPython3():
""" Returns ``True`` if we are using Python 3.x. """
return sys.version_info >= (3, )
def textfile_open(filename, mode='rt'):
"""
Simple wrapper around open() that will use codecs.open on Python2 and
@@ -873,9 +861,4 @@ def textfile_open(filename, mode='rt'):
mode parameter must include the 't' to put the stream into text mode.
"""
assert 't' in mode
if sys.version_info < (3,):
import codecs
mode = mode.replace('t', '')
return codecs.open(filename, mode, encoding='utf-8')
else:
return open(filename, mode, encoding='utf-8')
return open(filename, mode, encoding='utf-8')