Skip to content

Commit

Permalink
Merge python#28
Browse files Browse the repository at this point in the history
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 <jnanjeky@unb.ca>
  • Loading branch information
bors[bot] and nanjekyejoannah authored Apr 13, 2023
2 parents 520095b + 9273441 commit b825d1f
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 27 deletions.
3 changes: 1 addition & 2 deletions Lib/test/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import unittest

from test import test_support

testmeths = [

# Binary operations
Expand Down Expand Up @@ -549,7 +549,6 @@ def __eq__(self, other): return 1

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


def testSFBug532646(self):
# Test for SF bug 532646

Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

from random import random
from math import atan2, isnan, copysign
import sys

if sys.py3kwarning:
sys.setrecursionlimit(1 << 30)

INF = float("inf")
NAN = float("nan")
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
from cStringIO import StringIO
import unittest

def disassemble(func):
f = StringIO()
tmp = sys.stdout
Expand Down
20 changes: 8 additions & 12 deletions Lib/test/test_py3kwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,27 +286,24 @@ def test_file_xreadlines(self):
def test_file_open(self):
expected = ("The builtin 'file()'/'open()' function is not supported in 3.x, "
"use the 'io.open()' function instead with the encoding keyword argument")
with open(__file__) as f:
with check_py3k_warnings() as w:
self.assertWarning(f.read(), w, expected)
with check_py3k_warnings() as w:
with open(__file__) as f:
f.read()

def test_tokenize(self):
import tokenize
import io
expected = "tokenize() changed in 3.x: use generate_tokens() instead."
def helper_tok():
for tok in tokenize.tokenize(io.BytesIO('1 + 2').readline):
print tok
with check_py3k_warnings() as w:
self.assertWarning(helper_tok(), w, expected)
tokenize.tokenize(io.BytesIO('1 + 2').readline)


def test_file(self):
expected = ("The builtin 'file()'/'open()' function is not supported in 3.x, "
"use the 'io.open()' function instead with the encoding keyword argument")
with file(__file__) as f:
with check_py3k_warnings() as w:
self.assertWarning(f.read(), w, expected)
with check_py3k_warnings() as w:
with file(__file__) as f:
f.read()

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


class TestStdlibRemovals(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_undocumented_details.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from test.test_support import run_unittest, check_py3k_warnings
import unittest

class TestImplementationComparisons(unittest.TestCase):

def test_type_comparisons(self):
Expand Down
32 changes: 29 additions & 3 deletions Lib/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import sys
import types

# sys.setrecursionlimit(1 << 30)

__all__ = ["warn", "warn_explicit", "warn_explicit_with_fix",
"showwarning", "showwarningwithfix", "formatwarning",
"formatwarningwithfix", "filterwarnings", "simplefilter",
Expand All @@ -23,7 +25,7 @@ def warnpy3k(message, category=None, stacklevel=1):
category = DeprecationWarning
warn(message, category, stacklevel+1)

def warnpy3k_with_fix(message, category=None, stacklevel=1):
def warnpy3k_with_fix(message, fix, category=None, stacklevel=1):
"""Issue a deprecation warning for Python 3.x related changes and a fix.
Warnings are omitted unless Python is started with the -3 option.
Expand Down Expand Up @@ -390,8 +392,7 @@ def warn_with_fix(message, fix, category=None, stacklevel=1):
if not filename:
filename = module
registry = globals.setdefault("__warningregistry__", {})
warn_explicit_with_fix(message, fix, category, filename, lineno, module, registry,
globals)
warn_explicit_with_fix(message, fix, category, filename, lineno, module, registry, globals)

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

class WarningMessageWithFix(object):

"""Holds the result of a single showwarning() call."""

_WARNING_DETAILS = ("message", "fix", "category", "filename", "lineno", "file",
"line")

def __init__(self, message, fix, category, filename, lineno, file=None,
line=None):
self.message = message
self.fix = fix
self.category = category
self.filename = filename
self.lineno = lineno
self.file = file
self.line = line
self._category_name = category.__name__ if category else None

def __str__(self):
return ("{message : %r, fix : %r, category : %r, filename : %r, lineno : %s, "
"line : %r}" % (self.message, self._category_name,
self.filename, self.lineno, self.line))


class catch_warnings(object):

Expand Down Expand Up @@ -531,6 +555,8 @@ def __enter__(self):
log = []
def showwarning(*args, **kwargs):
log.append(WarningMessage(*args, **kwargs))
def showwarningwithfix(*args, **kwargs):
log.append(WarningMessageWithFix(*args, **kwargs))
self._module.showwarning = showwarning
return log
else:
Expand Down
2 changes: 1 addition & 1 deletion Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -2943,7 +2943,7 @@ PyObject_IsInstance(PyObject *inst, PyObject *cls)
PyErr_WarnEx_WithFix(PyExc_Py3xWarning,
"the basestring type is not supported in 3.x",
"import a third party library like six and use a compatible type like string_types", 1) < 0) {
return 0;
return -1;
}
}
/* Quick test for an exact match */
Expand Down
8 changes: 4 additions & 4 deletions Objects/fileobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,6 @@ _PyFile_SanitizeMode(char *mode)
static PyObject *
open_the_file(PyFileObject *f, char *name, char *mode)
{
if (PyErr_WarnPy3k_WithFix("The builtin 'file()'/'open()' function is not supported in 3.x, ",
"use the 'io.open()' function instead with the encoding keyword argument", 1) < 0)
return NULL;

char *newmode;
assert(f != NULL);
assert(PyFile_Check(f));
Expand Down Expand Up @@ -2421,6 +2417,10 @@ file_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static int
file_init(PyObject *self, PyObject *args, PyObject *kwds)
{
if (PyErr_WarnPy3k_WithFix("The builtin 'open()' function is not supported in 3.x, ",
"use the 'io.open()' function instead with the encoding keyword argument", 1) < 0)
goto Error;

PyFileObject *foself = (PyFileObject *)self;
int ret = 0;
static char *kwlist[] = {"name", "mode", "buffering", 0};
Expand Down
8 changes: 5 additions & 3 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -860,11 +860,13 @@ PyObject_Compare(PyObject *v, PyObject *w)
{
int result;

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

if (v == NULL || w == NULL) {
Expand Down
5 changes: 5 additions & 0 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,11 @@ Return the octal representation of an integer or long integer.");
static PyObject *
builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
{
if (PyErr_WarnPy3k("The builtin 'open()' function is not supported in 3.x, "
"use the 'io.open()' function instead with the encoding keyword argument", 1) < 0){
return NULL;
}

return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
}

Expand Down

0 comments on commit b825d1f

Please sign in to comment.