Skip to content

Fixed#2642: Convert py module references to six module #2650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions _pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import py
builtin_repr = repr

reprlib = py.builtin._tryimport('repr', 'reprlib')

if _PY3:
from traceback import format_exception_only
else:
Expand Down Expand Up @@ -235,7 +233,7 @@ def ishidden(self):
except KeyError:
return False

if py.builtin.callable(tbh):
if callable(tbh):
return tbh(None if self._excinfo is None else self._excinfo())
else:
return tbh
Expand Down
3 changes: 2 additions & 1 deletion _pytest/_code/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from bisect import bisect_right
import sys
import six
import inspect
import tokenize
import py
Expand Down Expand Up @@ -32,7 +33,7 @@ def __init__(self, *parts, **kwargs):
partlines = part.lines
elif isinstance(part, (tuple, list)):
partlines = [x.rstrip("\n") for x in part]
elif isinstance(part, py.builtin._basestring):
elif isinstance(part, six.string_types):
partlines = part.split('\n')
if rstrip:
while partlines:
Expand Down
4 changes: 2 additions & 2 deletions _pytest/assertion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
support for presenting detailed information in failing assertions.
"""
from __future__ import absolute_import, division, print_function
import py
import sys
import six

from _pytest.assertion import util
from _pytest.assertion import rewrite
Expand Down Expand Up @@ -126,7 +126,7 @@ def callbinrepr(op, left, right):
if new_expl:
new_expl = truncate.truncate_if_required(new_expl, item)
new_expl = [line.replace("\n", "\\n") for line in new_expl]
res = py.builtin._totext("\n~").join(new_expl)
res = six.text_type("\n~").join(new_expl)
if item.config.getvalue("assertmode") == "rewrite":
res = res.replace("%", "%%")
return res
Expand Down
23 changes: 12 additions & 11 deletions _pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import marshal
import os
import re
import six
import struct
import sys
import types
Expand Down Expand Up @@ -405,10 +406,10 @@ def _saferepr(obj):

"""
repr = py.io.saferepr(obj)
if py.builtin._istext(repr):
t = py.builtin.text
if isinstance(repr, six.text_type):
t = six.text_type
else:
t = py.builtin.bytes
t = six.binary_type
return repr.replace(t("\n"), t("\\n"))


Expand All @@ -427,32 +428,32 @@ def _format_assertmsg(obj):
# contains a newline it gets escaped, however if an object has a
# .__repr__() which contains newlines it does not get escaped.
# However in either case we want to preserve the newline.
if py.builtin._istext(obj) or py.builtin._isbytes(obj):
if isinstance(obj, six.text_type) or isinstance(obj, six.binary_type):
s = obj
is_repr = False
else:
s = py.io.saferepr(obj)
is_repr = True
if py.builtin._istext(s):
t = py.builtin.text
if isinstance(s, six.text_type):
t = six.text_type
else:
t = py.builtin.bytes
t = six.binary_type
s = s.replace(t("\n"), t("\n~")).replace(t("%"), t("%%"))
if is_repr:
s = s.replace(t("\\n"), t("\n~"))
return s


def _should_repr_global_name(obj):
return not hasattr(obj, "__name__") and not py.builtin.callable(obj)
return not hasattr(obj, "__name__") and not callable(obj)


def _format_boolop(explanations, is_or):
explanation = "(" + (is_or and " or " or " and ").join(explanations) + ")"
if py.builtin._istext(explanation):
t = py.builtin.text
if isinstance(explanation, six.text_type):
t = six.text_type
else:
t = py.builtin.bytes
t = six.binary_type
return explanation.replace(t('%'), t('%%'))


Expand Down
6 changes: 3 additions & 3 deletions _pytest/assertion/truncate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from __future__ import absolute_import, division, print_function
import os

import py
import six


DEFAULT_MAX_LINES = 8
Expand Down Expand Up @@ -74,8 +74,8 @@ def _truncate_explanation(input_lines, max_lines=None, max_chars=None):
msg += ' ({0} lines hidden)'.format(truncated_line_count)
msg += ", {0}" .format(USAGE_MSG)
truncated_explanation.extend([
py.builtin._totext(""),
py.builtin._totext(msg),
six.text_type(""),
six.text_type(msg),
])
return truncated_explanation

Expand Down
7 changes: 4 additions & 3 deletions _pytest/assertion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

import _pytest._code
import py
import six
try:
from collections import Sequence
except ImportError:
Sequence = list


u = py.builtin._totext
u = six.text_type

# The _reprcompare attribute on the util module is used by the new assertion
# interpretation code and assertion rewriter to detect this plugin was
Expand Down Expand Up @@ -174,9 +175,9 @@ def _diff_text(left, right, verbose=False):
"""
from difflib import ndiff
explanation = []
if isinstance(left, py.builtin.bytes):
if isinstance(left, six.binary_type):
left = u(repr(left)[1:-1]).replace(r'\n', '\n')
if isinstance(right, py.builtin.bytes):
if isinstance(right, six.binary_type):
right = u(repr(right)[1:-1]).replace(r'\n', '\n')
if not verbose:
i = 0 # just in case left or right has zero length
Expand Down
9 changes: 4 additions & 5 deletions _pytest/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
from io import UnsupportedOperation
from tempfile import TemporaryFile

import py
import six
import pytest
from _pytest.compat import CaptureIO

unicode = py.builtin.text

patchsysdict = {0: 'stdin', 1: 'stdout', 2: 'stderr'}

Expand Down Expand Up @@ -246,7 +245,7 @@ def __init__(self, buffer, encoding):
self.encoding = encoding

def write(self, obj):
if isinstance(obj, unicode):
if isinstance(obj, six.text_type):
obj = obj.encode(self.encoding, "replace")
self.buffer.write(obj)

Expand Down Expand Up @@ -377,7 +376,7 @@ def snap(self):
if res:
enc = getattr(f, "encoding", None)
if enc and isinstance(res, bytes):
res = py.builtin._totext(res, enc, "replace")
res = six.text_type(res, enc, "replace")
f.truncate(0)
f.seek(0)
return res
Expand All @@ -402,7 +401,7 @@ def resume(self):

def writeorg(self, data):
""" write to original file descriptor. """
if py.builtin._istext(data):
if isinstance(data, six.text_type):
data = data.encode("utf8") # XXX use encoding of original stream
os.write(self.targetfd_save, data)

Expand Down
9 changes: 5 additions & 4 deletions _pytest/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import types
import warnings

import six
import py
# DON't import pytest here because it causes import cycle troubles
import sys
Expand Down Expand Up @@ -158,7 +159,7 @@ def _prepareconfig(args=None, plugins=None):
try:
if plugins:
for plugin in plugins:
if isinstance(plugin, py.builtin._basestring):
if isinstance(plugin, six.string_types):
pluginmanager.consider_pluginarg(plugin)
else:
pluginmanager.register(plugin)
Expand Down Expand Up @@ -430,7 +431,7 @@ def import_plugin(self, modname):
# "terminal" or "capture". Those plugins are registered under their
# basename for historic purposes but must be imported with the
# _pytest prefix.
assert isinstance(modname, (py.builtin.text, str)), "module name as text required, got %r" % modname
assert isinstance(modname, (six.text_type, str)), "module name as text required, got %r" % modname
modname = str(modname)
if self.get_plugin(modname) is not None:
return
Expand Down Expand Up @@ -643,7 +644,7 @@ def __init__(self, *names, **attrs):
pass
else:
# this might raise a keyerror as well, don't want to catch that
if isinstance(typ, py.builtin._basestring):
if isinstance(typ, six.string_types):
if typ == 'choice':
warnings.warn(
'type argument to addoption() is a string %r.'
Expand Down Expand Up @@ -956,7 +957,7 @@ def notify_exception(self, excinfo, option=None):
)
res = self.hook.pytest_internalerror(excrepr=excrepr,
excinfo=excinfo)
if not py.builtin.any(res):
if not any(res):
for line in str(excrepr).split("\n"):
sys.stderr.write("INTERNALERROR> %s\n" % line)
sys.stderr.flush()
Expand Down
3 changes: 2 additions & 1 deletion _pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import functools
import os
import six
import sys

import _pytest
Expand Down Expand Up @@ -397,7 +398,7 @@ def add_marker(self, marker):
``marker`` can be a string or pytest.mark.* instance.
"""
from _pytest.mark import MarkDecorator, MARK_GEN
if isinstance(marker, py.builtin._basestring):
if isinstance(marker, six.string_types):
marker = getattr(MARK_GEN, marker)
elif not isinstance(marker, MarkDecorator):
raise ValueError("is not a string or pytest.mark.* Marker")
Expand Down
9 changes: 4 additions & 5 deletions _pytest/monkeypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import os
import sys
import re

from py.builtin import _basestring
import six
from _pytest.fixtures import fixture

RE_IMPORT_ERROR_NAME = re.compile("^No module named (.*)$")
Expand Down Expand Up @@ -79,7 +78,7 @@ def annotated_getattr(obj, name, ann):


def derive_importpath(import_path, raising):
if not isinstance(import_path, _basestring) or "." not in import_path:
if not isinstance(import_path, six.string_types) or "." not in import_path:
raise TypeError("must be absolute import path string, not %r" %
(import_path,))
module, attr = import_path.rsplit('.', 1)
Expand Down Expand Up @@ -125,7 +124,7 @@ def setattr(self, target, name, value=notset, raising=True):
import inspect

if value is notset:
if not isinstance(target, _basestring):
if not isinstance(target, six.string_types):
raise TypeError("use setattr(target, name, value) or "
"setattr(target, value) with target being a dotted "
"import string")
Expand Down Expand Up @@ -155,7 +154,7 @@ def delattr(self, target, name=notset, raising=True):
"""
__tracebackhide__ = True
if name is notset:
if not isinstance(target, _basestring):
if not isinstance(target, six.string_types):
raise TypeError("use delattr(target, name) or "
"delattr(target) with target being a dotted "
"import string")
Expand Down
3 changes: 1 addition & 2 deletions _pytest/nose.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import sys

import py
from _pytest import unittest, runner, python
from _pytest.config import hookimpl

Expand Down Expand Up @@ -66,7 +65,7 @@ def is_potential_nosetest(item):
def call_optional(obj, name):
method = getattr(obj, name, None)
isfixture = hasattr(method, "_pytestfixturefunction")
if method is not None and not isfixture and py.builtin.callable(method):
if method is not None and not isfixture and callable(method):
# If there's any problems allow the exception to raise rather than
# silently ignoring them
method()
Expand Down
4 changes: 2 additions & 2 deletions _pytest/pastebin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import absolute_import, division, print_function

import pytest
import six
import sys
import tempfile

Expand All @@ -16,7 +17,6 @@ def pytest_addoption(parser):

@pytest.hookimpl(trylast=True)
def pytest_configure(config):
import py
if config.option.pastebin == "all":
tr = config.pluginmanager.getplugin('terminalreporter')
# if no terminal reporter plugin is present, nothing we can do here;
Expand All @@ -29,7 +29,7 @@ def pytest_configure(config):

def tee_write(s, **kwargs):
oldwrite(s, **kwargs)
if py.builtin._istext(s):
if isinstance(s, six.text_type):
s = s.encode('utf-8')
config._pastebinfile.write(s)

Expand Down
11 changes: 6 additions & 5 deletions _pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import platform
import re
import subprocess
import six
import sys
import time
import traceback
Expand Down Expand Up @@ -485,8 +486,8 @@ def chdir(self):
def _makefile(self, ext, args, kwargs, encoding="utf-8"):
items = list(kwargs.items())
if args:
source = py.builtin._totext("\n").join(
map(py.builtin._totext, args)) + py.builtin._totext("\n")
source = six.text_type("\n").join(
map(six.text_type, args)) + six.text_type("\n")
basename = self.request.function.__name__
items.insert(0, (basename, source))
ret = None
Expand All @@ -496,12 +497,12 @@ def _makefile(self, ext, args, kwargs, encoding="utf-8"):
source = Source(value)

def my_totext(s, encoding="utf-8"):
if py.builtin._isbytes(s):
s = py.builtin._totext(s, encoding=encoding)
if isinstance(s, six.binary_type):
s = six.text_type(s, encoding=encoding)
return s

source_unicode = "\n".join([my_totext(line) for line in source.lines])
source = py.builtin._totext(source_unicode)
source = six.text_type(source_unicode)
content = source.strip().encode(encoding) # + "\n"
# content = content.rstrip() + "\n"
p.write(content, "wb")
Expand Down
Loading