Skip to content

Commit 9e62a31

Browse files
Merge pull request #2650 from srinivasreddy/2642
Fixed#2642: Convert py module references to six module
2 parents 40254b6 + 2e33d9b commit 9e62a31

19 files changed

+61
-57
lines changed

_pytest/_code/code.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import py
99
builtin_repr = repr
1010

11-
reprlib = py.builtin._tryimport('repr', 'reprlib')
12-
1311
if _PY3:
1412
from traceback import format_exception_only
1513
else:
@@ -235,7 +233,7 @@ def ishidden(self):
235233
except KeyError:
236234
return False
237235

238-
if py.builtin.callable(tbh):
236+
if callable(tbh):
239237
return tbh(None if self._excinfo is None else self._excinfo())
240238
else:
241239
return tbh

_pytest/_code/source.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from bisect import bisect_right
44
import sys
5+
import six
56
import inspect
67
import tokenize
78
import py
@@ -32,7 +33,7 @@ def __init__(self, *parts, **kwargs):
3233
partlines = part.lines
3334
elif isinstance(part, (tuple, list)):
3435
partlines = [x.rstrip("\n") for x in part]
35-
elif isinstance(part, py.builtin._basestring):
36+
elif isinstance(part, six.string_types):
3637
partlines = part.split('\n')
3738
if rstrip:
3839
while partlines:

_pytest/assertion/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
support for presenting detailed information in failing assertions.
33
"""
44
from __future__ import absolute_import, division, print_function
5-
import py
65
import sys
6+
import six
77

88
from _pytest.assertion import util
99
from _pytest.assertion import rewrite
@@ -126,7 +126,7 @@ def callbinrepr(op, left, right):
126126
if new_expl:
127127
new_expl = truncate.truncate_if_required(new_expl, item)
128128
new_expl = [line.replace("\n", "\\n") for line in new_expl]
129-
res = py.builtin._totext("\n~").join(new_expl)
129+
res = six.text_type("\n~").join(new_expl)
130130
if item.config.getvalue("assertmode") == "rewrite":
131131
res = res.replace("%", "%%")
132132
return res

_pytest/assertion/rewrite.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import marshal
99
import os
1010
import re
11+
import six
1112
import struct
1213
import sys
1314
import types
@@ -405,10 +406,10 @@ def _saferepr(obj):
405406
406407
"""
407408
repr = py.io.saferepr(obj)
408-
if py.builtin._istext(repr):
409-
t = py.builtin.text
409+
if isinstance(repr, six.text_type):
410+
t = six.text_type
410411
else:
411-
t = py.builtin.bytes
412+
t = six.binary_type
412413
return repr.replace(t("\n"), t("\\n"))
413414

414415

@@ -427,32 +428,32 @@ def _format_assertmsg(obj):
427428
# contains a newline it gets escaped, however if an object has a
428429
# .__repr__() which contains newlines it does not get escaped.
429430
# However in either case we want to preserve the newline.
430-
if py.builtin._istext(obj) or py.builtin._isbytes(obj):
431+
if isinstance(obj, six.text_type) or isinstance(obj, six.binary_type):
431432
s = obj
432433
is_repr = False
433434
else:
434435
s = py.io.saferepr(obj)
435436
is_repr = True
436-
if py.builtin._istext(s):
437-
t = py.builtin.text
437+
if isinstance(s, six.text_type):
438+
t = six.text_type
438439
else:
439-
t = py.builtin.bytes
440+
t = six.binary_type
440441
s = s.replace(t("\n"), t("\n~")).replace(t("%"), t("%%"))
441442
if is_repr:
442443
s = s.replace(t("\\n"), t("\n~"))
443444
return s
444445

445446

446447
def _should_repr_global_name(obj):
447-
return not hasattr(obj, "__name__") and not py.builtin.callable(obj)
448+
return not hasattr(obj, "__name__") and not callable(obj)
448449

449450

450451
def _format_boolop(explanations, is_or):
451452
explanation = "(" + (is_or and " or " or " and ").join(explanations) + ")"
452-
if py.builtin._istext(explanation):
453-
t = py.builtin.text
453+
if isinstance(explanation, six.text_type):
454+
t = six.text_type
454455
else:
455-
t = py.builtin.bytes
456+
t = six.binary_type
456457
return explanation.replace(t('%'), t('%%'))
457458

458459

_pytest/assertion/truncate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from __future__ import absolute_import, division, print_function
88
import os
99

10-
import py
10+
import six
1111

1212

1313
DEFAULT_MAX_LINES = 8
@@ -74,8 +74,8 @@ def _truncate_explanation(input_lines, max_lines=None, max_chars=None):
7474
msg += ' ({0} lines hidden)'.format(truncated_line_count)
7575
msg += ", {0}" .format(USAGE_MSG)
7676
truncated_explanation.extend([
77-
py.builtin._totext(""),
78-
py.builtin._totext(msg),
77+
six.text_type(""),
78+
six.text_type(msg),
7979
])
8080
return truncated_explanation
8181

_pytest/assertion/util.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
import _pytest._code
66
import py
7+
import six
78
try:
89
from collections import Sequence
910
except ImportError:
1011
Sequence = list
1112

1213

13-
u = py.builtin._totext
14+
u = six.text_type
1415

1516
# The _reprcompare attribute on the util module is used by the new assertion
1617
# interpretation code and assertion rewriter to detect this plugin was
@@ -174,9 +175,9 @@ def _diff_text(left, right, verbose=False):
174175
"""
175176
from difflib import ndiff
176177
explanation = []
177-
if isinstance(left, py.builtin.bytes):
178+
if isinstance(left, six.binary_type):
178179
left = u(repr(left)[1:-1]).replace(r'\n', '\n')
179-
if isinstance(right, py.builtin.bytes):
180+
if isinstance(right, six.binary_type):
180181
right = u(repr(right)[1:-1]).replace(r'\n', '\n')
181182
if not verbose:
182183
i = 0 # just in case left or right has zero length

_pytest/capture.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
from io import UnsupportedOperation
1212
from tempfile import TemporaryFile
1313

14-
import py
14+
import six
1515
import pytest
1616
from _pytest.compat import CaptureIO
1717

18-
unicode = py.builtin.text
1918

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

@@ -246,7 +245,7 @@ def __init__(self, buffer, encoding):
246245
self.encoding = encoding
247246

248247
def write(self, obj):
249-
if isinstance(obj, unicode):
248+
if isinstance(obj, six.text_type):
250249
obj = obj.encode(self.encoding, "replace")
251250
self.buffer.write(obj)
252251

@@ -377,7 +376,7 @@ def snap(self):
377376
if res:
378377
enc = getattr(f, "encoding", None)
379378
if enc and isinstance(res, bytes):
380-
res = py.builtin._totext(res, enc, "replace")
379+
res = six.text_type(res, enc, "replace")
381380
f.truncate(0)
382381
f.seek(0)
383382
return res
@@ -402,7 +401,7 @@ def resume(self):
402401

403402
def writeorg(self, data):
404403
""" write to original file descriptor. """
405-
if py.builtin._istext(data):
404+
if isinstance(data, six.text_type):
406405
data = data.encode("utf8") # XXX use encoding of original stream
407406
os.write(self.targetfd_save, data)
408407

_pytest/config.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import types
77
import warnings
88

9+
import six
910
import py
1011
# DON't import pytest here because it causes import cycle troubles
1112
import sys
@@ -149,7 +150,7 @@ def _prepareconfig(args=None, plugins=None):
149150
try:
150151
if plugins:
151152
for plugin in plugins:
152-
if isinstance(plugin, py.builtin._basestring):
153+
if isinstance(plugin, six.string_types):
153154
pluginmanager.consider_pluginarg(plugin)
154155
else:
155156
pluginmanager.register(plugin)
@@ -421,7 +422,7 @@ def import_plugin(self, modname):
421422
# "terminal" or "capture". Those plugins are registered under their
422423
# basename for historic purposes but must be imported with the
423424
# _pytest prefix.
424-
assert isinstance(modname, (py.builtin.text, str)), "module name as text required, got %r" % modname
425+
assert isinstance(modname, (six.text_type, str)), "module name as text required, got %r" % modname
425426
modname = str(modname)
426427
if self.get_plugin(modname) is not None:
427428
return
@@ -634,7 +635,7 @@ def __init__(self, *names, **attrs):
634635
pass
635636
else:
636637
# this might raise a keyerror as well, don't want to catch that
637-
if isinstance(typ, py.builtin._basestring):
638+
if isinstance(typ, six.string_types):
638639
if typ == 'choice':
639640
warnings.warn(
640641
'type argument to addoption() is a string %r.'
@@ -947,7 +948,7 @@ def notify_exception(self, excinfo, option=None):
947948
)
948949
res = self.hook.pytest_internalerror(excrepr=excrepr,
949950
excinfo=excinfo)
950-
if not py.builtin.any(res):
951+
if not any(res):
951952
for line in str(excrepr).split("\n"):
952953
sys.stderr.write("INTERNALERROR> %s\n" % line)
953954
sys.stderr.flush()

_pytest/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import functools
55
import os
6+
import six
67
import sys
78

89
import _pytest
@@ -371,7 +372,7 @@ def add_marker(self, marker):
371372
``marker`` can be a string or pytest.mark.* instance.
372373
"""
373374
from _pytest.mark import MarkDecorator, MARK_GEN
374-
if isinstance(marker, py.builtin._basestring):
375+
if isinstance(marker, six.string_types):
375376
marker = getattr(MARK_GEN, marker)
376377
elif not isinstance(marker, MarkDecorator):
377378
raise ValueError("is not a string or pytest.mark.* Marker")

_pytest/monkeypatch.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import os
55
import sys
66
import re
7-
8-
from py.builtin import _basestring
7+
import six
98
from _pytest.fixtures import fixture
109

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

8079

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

127126
if value is notset:
128-
if not isinstance(target, _basestring):
127+
if not isinstance(target, six.string_types):
129128
raise TypeError("use setattr(target, name, value) or "
130129
"setattr(target, value) with target being a dotted "
131130
"import string")
@@ -155,7 +154,7 @@ def delattr(self, target, name=notset, raising=True):
155154
"""
156155
__tracebackhide__ = True
157156
if name is notset:
158-
if not isinstance(target, _basestring):
157+
if not isinstance(target, six.string_types):
159158
raise TypeError("use delattr(target, name) or "
160159
"delattr(target) with target being a dotted "
161160
"import string")

_pytest/nose.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import sys
55

6-
import py
76
from _pytest import unittest, runner, python
87
from _pytest.config import hookimpl
98

@@ -66,7 +65,7 @@ def is_potential_nosetest(item):
6665
def call_optional(obj, name):
6766
method = getattr(obj, name, None)
6867
isfixture = hasattr(method, "_pytestfixturefunction")
69-
if method is not None and not isfixture and py.builtin.callable(method):
68+
if method is not None and not isfixture and callable(method):
7069
# If there's any problems allow the exception to raise rather than
7170
# silently ignoring them
7271
method()

_pytest/pastebin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import absolute_import, division, print_function
33

44
import pytest
5+
import six
56
import sys
67
import tempfile
78

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

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

3030
def tee_write(s, **kwargs):
3131
oldwrite(s, **kwargs)
32-
if py.builtin._istext(s):
32+
if isinstance(s, six.text_type):
3333
s = s.encode('utf-8')
3434
config._pastebinfile.write(s)
3535

_pytest/pytester.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import platform
88
import re
99
import subprocess
10+
import six
1011
import sys
1112
import time
1213
import traceback
@@ -482,8 +483,8 @@ def chdir(self):
482483
def _makefile(self, ext, args, kwargs, encoding="utf-8"):
483484
items = list(kwargs.items())
484485
if args:
485-
source = py.builtin._totext("\n").join(
486-
map(py.builtin._totext, args)) + py.builtin._totext("\n")
486+
source = six.text_type("\n").join(
487+
map(six.text_type, args)) + six.text_type("\n")
487488
basename = self.request.function.__name__
488489
items.insert(0, (basename, source))
489490
ret = None
@@ -493,12 +494,12 @@ def _makefile(self, ext, args, kwargs, encoding="utf-8"):
493494
source = Source(value)
494495

495496
def my_totext(s, encoding="utf-8"):
496-
if py.builtin._isbytes(s):
497-
s = py.builtin._totext(s, encoding=encoding)
497+
if isinstance(s, six.binary_type):
498+
s = six.text_type(s, encoding=encoding)
498499
return s
499500

500501
source_unicode = "\n".join([my_totext(line) for line in source.lines])
501-
source = py.builtin._totext(source_unicode)
502+
source = six.text_type(source_unicode)
502503
content = source.strip().encode(encoding) # + "\n"
503504
# content = content.rstrip() + "\n"
504505
p.write(content, "wb")

0 commit comments

Comments
 (0)