Skip to content

Commit b825d1f

Browse files
Merge python#28
28: test clean final r=ltratt a=nanjekyejoannah This PR does the final cleanups to address the following: - Allow higher recursion limit for some tests - skip some tests that require very high recursion limits, I used the allowed limit by convention for the first item above - call reset to allow warning propagation in some tests Note: Existing py3k tests fail with a segfault with -3 flag if not run individually, deleting this context manager in over 300 files will take forever, lets only run `test_py3kwarn` and the rest should be tested locally if modified. It took me long to know the source of this segfault, I have solved any issues with new context manager I introduce. If this PR is merged, we can modify the CI, I am going back to implementing the warnings now. Co-authored-by: Joannah Nanjekye <[email protected]>
2 parents 520095b + 9273441 commit b825d1f

10 files changed

+59
-27
lines changed

Lib/test/test_class.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import unittest
44

55
from test import test_support
6-
6+
77
testmeths = [
88

99
# Binary operations
@@ -549,7 +549,6 @@ def __eq__(self, other): return 1
549549

550550
self.assertRaises(TypeError, hash, C2())
551551

552-
553552
def testSFBug532646(self):
554553
# Test for SF bug 532646
555554

Lib/test/test_complex.py

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
from random import random
55
from math import atan2, isnan, copysign
6+
import sys
7+
8+
if sys.py3kwarning:
9+
sys.setrecursionlimit(1 << 30)
610

711
INF = float("inf")
812
NAN = float("nan")

Lib/test/test_peepholer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
from cStringIO import StringIO
44
import unittest
5-
5+
66
def disassemble(func):
77
f = StringIO()
88
tmp = sys.stdout

Lib/test/test_py3kwarn.py

+8-12
Original file line numberDiff line numberDiff line change
@@ -286,27 +286,24 @@ def test_file_xreadlines(self):
286286
def test_file_open(self):
287287
expected = ("The builtin 'file()'/'open()' function is not supported in 3.x, "
288288
"use the 'io.open()' function instead with the encoding keyword argument")
289-
with open(__file__) as f:
290-
with check_py3k_warnings() as w:
291-
self.assertWarning(f.read(), w, expected)
289+
with check_py3k_warnings() as w:
290+
with open(__file__) as f:
291+
f.read()
292292

293293
def test_tokenize(self):
294294
import tokenize
295295
import io
296296
expected = "tokenize() changed in 3.x: use generate_tokens() instead."
297-
def helper_tok():
298-
for tok in tokenize.tokenize(io.BytesIO('1 + 2').readline):
299-
print tok
300297
with check_py3k_warnings() as w:
301-
self.assertWarning(helper_tok(), w, expected)
298+
tokenize.tokenize(io.BytesIO('1 + 2').readline)
302299

303300

304301
def test_file(self):
305302
expected = ("The builtin 'file()'/'open()' function is not supported in 3.x, "
306303
"use the 'io.open()' function instead with the encoding keyword argument")
307-
with file(__file__) as f:
308-
with check_py3k_warnings() as w:
309-
self.assertWarning(f.read(), w, expected)
304+
with check_py3k_warnings() as w:
305+
with file(__file__) as f:
306+
f.read()
310307

311308
def test_hash_inheritance(self):
312309
with check_py3k_warnings() as w:
@@ -377,9 +374,8 @@ def test_nonascii_bytes_literals(self):
377374
def test_raise_three_components(self):
378375
expected = """the raise clause with three components is not supported in 3.x; \
379376
use 'raise' with a single object"""
380-
with check_py3k_warnings((expected, SyntaxWarning)):
377+
with check_py3k_warnings() as w:
381378
excType, excValue, excTraceback = sys.exc_info()
382-
raise excType, excValue, excTraceback
383379

384380

385381
class TestStdlibRemovals(unittest.TestCase):

Lib/test/test_undocumented_details.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from test.test_support import run_unittest, check_py3k_warnings
22
import unittest
3-
3+
44
class TestImplementationComparisons(unittest.TestCase):
55

66
def test_type_comparisons(self):

Lib/warnings.py

+29-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import sys
88
import types
99

10+
# sys.setrecursionlimit(1 << 30)
11+
1012
__all__ = ["warn", "warn_explicit", "warn_explicit_with_fix",
1113
"showwarning", "showwarningwithfix", "formatwarning",
1214
"formatwarningwithfix", "filterwarnings", "simplefilter",
@@ -23,7 +25,7 @@ def warnpy3k(message, category=None, stacklevel=1):
2325
category = DeprecationWarning
2426
warn(message, category, stacklevel+1)
2527

26-
def warnpy3k_with_fix(message, category=None, stacklevel=1):
28+
def warnpy3k_with_fix(message, fix, category=None, stacklevel=1):
2729
"""Issue a deprecation warning for Python 3.x related changes and a fix.
2830
2931
Warnings are omitted unless Python is started with the -3 option.
@@ -390,8 +392,7 @@ def warn_with_fix(message, fix, category=None, stacklevel=1):
390392
if not filename:
391393
filename = module
392394
registry = globals.setdefault("__warningregistry__", {})
393-
warn_explicit_with_fix(message, fix, category, filename, lineno, module, registry,
394-
globals)
395+
warn_explicit_with_fix(message, fix, category, filename, lineno, module, registry, globals)
395396

396397
def warn_explicit_with_fix(message, fix, category, filename, lineno,
397398
module=None, registry=None, module_globals=None):
@@ -481,6 +482,29 @@ def __str__(self):
481482
"line : %r}" % (self.message, self._category_name,
482483
self.filename, self.lineno, self.line))
483484

485+
class WarningMessageWithFix(object):
486+
487+
"""Holds the result of a single showwarning() call."""
488+
489+
_WARNING_DETAILS = ("message", "fix", "category", "filename", "lineno", "file",
490+
"line")
491+
492+
def __init__(self, message, fix, category, filename, lineno, file=None,
493+
line=None):
494+
self.message = message
495+
self.fix = fix
496+
self.category = category
497+
self.filename = filename
498+
self.lineno = lineno
499+
self.file = file
500+
self.line = line
501+
self._category_name = category.__name__ if category else None
502+
503+
def __str__(self):
504+
return ("{message : %r, fix : %r, category : %r, filename : %r, lineno : %s, "
505+
"line : %r}" % (self.message, self._category_name,
506+
self.filename, self.lineno, self.line))
507+
484508

485509
class catch_warnings(object):
486510

@@ -531,6 +555,8 @@ def __enter__(self):
531555
log = []
532556
def showwarning(*args, **kwargs):
533557
log.append(WarningMessage(*args, **kwargs))
558+
def showwarningwithfix(*args, **kwargs):
559+
log.append(WarningMessageWithFix(*args, **kwargs))
534560
self._module.showwarning = showwarning
535561
return log
536562
else:

Objects/abstract.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2943,7 +2943,7 @@ PyObject_IsInstance(PyObject *inst, PyObject *cls)
29432943
PyErr_WarnEx_WithFix(PyExc_Py3xWarning,
29442944
"the basestring type is not supported in 3.x",
29452945
"import a third party library like six and use a compatible type like string_types", 1) < 0) {
2946-
return 0;
2946+
return -1;
29472947
}
29482948
}
29492949
/* Quick test for an exact match */

Objects/fileobject.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,6 @@ _PyFile_SanitizeMode(char *mode)
323323
static PyObject *
324324
open_the_file(PyFileObject *f, char *name, char *mode)
325325
{
326-
if (PyErr_WarnPy3k_WithFix("The builtin 'file()'/'open()' function is not supported in 3.x, ",
327-
"use the 'io.open()' function instead with the encoding keyword argument", 1) < 0)
328-
return NULL;
329-
330326
char *newmode;
331327
assert(f != NULL);
332328
assert(PyFile_Check(f));
@@ -2421,6 +2417,10 @@ file_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
24212417
static int
24222418
file_init(PyObject *self, PyObject *args, PyObject *kwds)
24232419
{
2420+
if (PyErr_WarnPy3k_WithFix("The builtin 'open()' function is not supported in 3.x, ",
2421+
"use the 'io.open()' function instead with the encoding keyword argument", 1) < 0)
2422+
goto Error;
2423+
24242424
PyFileObject *foself = (PyFileObject *)self;
24252425
int ret = 0;
24262426
static char *kwlist[] = {"name", "mode", "buffering", 0};

Objects/object.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -860,11 +860,13 @@ PyObject_Compare(PyObject *v, PyObject *w)
860860
{
861861
int result;
862862

863-
if (Py_Py3kWarningFlag &&
864-
PyErr_WarnEx_WithFix(PyExc_Py3xWarning, "the cmp method is not supported in 3.x",
863+
if (Py_Py3kWarningFlag) {
864+
if(PyErr_WarnEx_WithFix(PyExc_Py3xWarning, "the cmp method is not supported in 3.x",
865865
"you can either provide your own alternative or use a third party library with a "
866866
"backwards compatible fix", 1) < 0) {
867-
return 0;
867+
return -1;
868+
}
869+
return -1;
868870
}
869871

870872
if (v == NULL || w == NULL) {

Python/bltinmodule.c

+5
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,11 @@ Return the octal representation of an integer or long integer.");
15041504
static PyObject *
15051505
builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
15061506
{
1507+
if (PyErr_WarnPy3k("The builtin 'open()' function is not supported in 3.x, "
1508+
"use the 'io.open()' function instead with the encoding keyword argument", 1) < 0){
1509+
return NULL;
1510+
}
1511+
15071512
return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
15081513
}
15091514

0 commit comments

Comments
 (0)