Skip to content

Commit

Permalink
Cherry-pick with fixes, to make string warning ready
Browse files Browse the repository at this point in the history
  • Loading branch information
nanjekyejoannah committed Mar 15, 2024
1 parent bb5d72c commit 2a1224d
Show file tree
Hide file tree
Showing 50 changed files with 1,214 additions and 42 deletions.
13 changes: 12 additions & 1 deletion Doc/c-api/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@ is a separate error indicator for each thread.
and *registry* arguments may be set to *NULL* to get the default effect
described there.
.. c:function:: int PyErr_WarnExplicit_WithFix(PyObject *category, const char *message, const char *fix, const char *filename, int lineno, const char *module, PyObject *registry)
Issue a warning message and a potential fix. This warning is the
same as `PyErr_WarnExplicit` but adds a *fix* argument to allow
for `Py3xWarning` warnings to suggest potential fixes for Python
3.x incompatible code.
.. c:function:: int PyErr_WarnPy3k(char *message, int stacklevel)
Expand All @@ -327,6 +333,11 @@ is a separate error indicator for each thread.
.. versionadded:: 2.6
.. c:function:: int PyErr_WarnPy3k_WithFix(char *message, char *fix, int stacklevel)
Issue a :exc:`DeprecationWarning` with the given *message* and *stacklevel*
if the :c:data:`Py_Py3kWarningFlag` flag is enabled.
.. c:function:: int PyErr_CheckSignals()
Expand Down Expand Up @@ -715,7 +726,7 @@ the variables:
+------------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_UserWarning` | :exc:`UserWarning` | |
+------------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_3xWarning` | :exc:`Py3xWarning` | |
| :c:data:`PyExc_3xWarning` | :exc:`Py3xWarning` | |
+------------------------------------------+---------------------------------+----------+
Notes:
Expand Down
2 changes: 1 addition & 1 deletion Doc/library/warnings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ following warnings category classes are currently defined:
| | bytes and bytearray. |
+----------------------------------+-----------------------------------------------+
| :exc:`Py3xWarning` | Base class for warnings about 3.x |
| compatibility | |
| compatibility. | |
+----------------------------------+-----------------------------------------------+

While these are technically built-in exceptions, they are documented here,
Expand Down
6 changes: 6 additions & 0 deletions Include/warnings.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ extern "C" {
PyAPI_FUNC(void) _PyWarnings_Init(void);

PyAPI_FUNC(int) PyErr_WarnEx(PyObject *, const char *, Py_ssize_t);
PyAPI_FUNC(int) PyErr_WarnEx_WithFix(PyObject *, const char *, const char *, Py_ssize_t);
PyAPI_FUNC(int) PyErr_WarnExplicit(PyObject *, const char *, const char *, int,
const char *, PyObject *);
PyAPI_FUNC(int) PyErr_WarnExplicit_WithFix(PyObject *, const char *, const char *, const char *, int,
const char *, PyObject *);

#define PyErr_WarnPy3k(msg, stacklevel) \
(Py_Py3kWarningFlag ? PyErr_WarnEx(PyExc_DeprecationWarning, msg, stacklevel) : 0)

#define PyErr_WarnPy3k_WithFix(msg, fix, stacklevel) \
(Py_Py3kWarningFlag ? PyErr_WarnEx_WithFix(PyExc_DeprecationWarning, msg, fix, stacklevel) : 0)

/* DEPRECATED: Use PyErr_WarnEx() instead. */
#define PyErr_Warn(category, msg) PyErr_WarnEx(category, msg, 1)

Expand Down
6 changes: 5 additions & 1 deletion Lib/base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import struct
import string
import binascii
from warnings import warnpy3k_with_fix


__all__ = [
Expand All @@ -29,7 +30,6 @@
_translation = [chr(_x) for _x in range(256)]
EMPTYSTRING = ''


def _translate(s, altchars):
translation = _translation[:]
for k, v in altchars.items():
Expand Down Expand Up @@ -316,6 +316,8 @@ def decode(input, output):

def encodestring(s):
"""Encode a string into multiple lines of base-64 data."""
warnpy3k_with_fix("base64.encodestring is not supported in 3.x",
"use base64.encodebytes instead", stacklevel=2)
pieces = []
for i in range(0, len(s), MAXBINSIZE):
chunk = s[i : i + MAXBINSIZE]
Expand All @@ -325,6 +327,8 @@ def encodestring(s):

def decodestring(s):
"""Decode a string."""
warnpy3k_with_fix("base64.decodestring is not supported in 3.x",
"use base64.decodebytes instead", stacklevel=2)
return binascii.a2b_base64(s)


Expand Down
1 change: 1 addition & 0 deletions Lib/email/test/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def test_get_decoded_uu_payload(self):
msg.set_payload('foo')
eq(msg.get_payload(decode=True), 'foo')

@unittest.skipIf(sys.py3kwarning, "messaging confuses log")
def test_decode_bogus_uu_payload_quietly(self):
msg = Message()
msg.set_payload('begin 664 foo.txt\n%<W1F=0000H \n \nend\n')
Expand Down
6 changes: 6 additions & 0 deletions Lib/hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"""

import warnings as _warnings
import sys


from operator import _compare_digest as compare_digest

Expand Down Expand Up @@ -41,6 +43,10 @@ def __init__(self, key, msg = None, digestmod = None):
return

if digestmod is None:
if sys.py3kwarning:
_warnings.warnpy3k_with_fix('the digestmod paramemer is required in 3.x',
'generate a digest with hashlib module',
DeprecationWarning, stacklevel=4)
import hashlib
digestmod = hashlib.md5

Expand Down
3 changes: 3 additions & 0 deletions Lib/idlelib/idle_test/test_warning.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import unittest
from test.test_support import captured_stderr
import sys

import warnings
# Try to capture default showwarning before Idle modules are imported.
Expand Down Expand Up @@ -39,6 +40,7 @@ def test_showwarnings(self):
run.capture_warnings(False)
self.assertIs(warnings.showwarning, showwarning)

@unittest.skipIf(sys.py3kwarning, "messaging confuses log")
def test_run_show(self):
with captured_stderr() as f:
run.idle_showwarning_subproc(
Expand All @@ -62,6 +64,7 @@ def test_idle_formatter(self):
'Test', UserWarning, 'test_warning.py', 99, 'Line of code')
self.assertEqual(idlemsg, s)

@unittest.skipIf(sys.py3kwarning, "messaging confuses log")
def test_shell_show(self):
with captured_stderr() as f:
shell.idle_showwarning(
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/string_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import unittest, string, sys, struct
from test import test_support
from UserList import UserList
import warnings

class Sequence:
def __init__(self, seq='wxyz'): self.seq = seq
Expand Down Expand Up @@ -1058,6 +1059,24 @@ def test_slice(self):

self.checkraises(TypeError, 'abc', '__getslice__', 'def')

def test_py3x_warnings_isinstance(self):
if sys.py3kwarning:
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', category=Py3xWarning)
isinstance(u"fix", basestring)
isinstance(b"fix", basestring)
isinstance("fix", basestring)

def test_py3x_warnings_join(self):
if sys.py3kwarning:
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', category=Py3xWarning)
x = 'foo'
y = b'foo'
z = x + y
b = y + x
v = x.__add__(y)

def test_extended_getslice(self):
# Test extended slicing by comparing with list slicing.
s = string.ascii_letters + string.digits
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_asyncore.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ def test_repr(self):
d = asyncore.dispatcher()
self.assertEqual(repr(d), '<asyncore.dispatcher at %#x>' % id(d))

@unittest.skipIf(sys.py3kwarning, "messaging confuses log")
def test_log(self):
d = asyncore.dispatcher()

Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_atexit.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def tearDown(self):
sys.stderr = self.save_stderr
atexit._exithandlers = self.save_handlers

@unittest.skipIf(sys.py3kwarning, "messaging confuses log")
def test_args(self):
atexit.register(self.h1)
atexit.register(self.h4)
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_base64.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import unittest
from test import test_support
import base64
import sys
import warnings



Expand All @@ -21,6 +23,11 @@ def test_encodestring(self):
# Non-bytes
eq(base64.encodestring(bytearray('abc')), 'YWJj\n')

if sys.py3kwarning:
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', category=Py3xWarning)
base64.encodestring("")

def test_decodestring(self):
eq = self.assertEqual
eq(base64.decodestring("d3d3LnB5dGhvbi5vcmc=\n"), "www.python.org")
Expand All @@ -37,6 +44,11 @@ def test_decodestring(self):
# Non-bytes
eq(base64.decodestring(bytearray("YWJj\n")), "abc")

if sys.py3kwarning:
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', category=Py3xWarning)
base64.decodestring('')

def test_encode(self):
eq = self.assertEqual
from cStringIO import StringIO
Expand Down
11 changes: 8 additions & 3 deletions Lib/test/test_binascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import unittest
import binascii
import array
import sys
import warnings

# Note: "*_hex" functions are aliases for "(un)hexlify"
b2a_functions = ['b2a_base64', 'b2a_hex', 'b2a_hqx', 'b2a_qp', 'b2a_uu',
Expand Down Expand Up @@ -171,9 +173,12 @@ def test_hex(self):
self.assertRaises(TypeError, binascii.a2b_hex, t[:-1])
self.assertRaises(TypeError, binascii.a2b_hex, t[:-1] + 'q')

# Verify the treatment of Unicode strings
if test_support.have_unicode:
self.assertEqual(binascii.hexlify(unicode('a', 'ascii')), '61')
if sys.py3kwarning:
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', category=Py3xWarning)
if test_support.have_unicode:
self.assertEqual(binascii.hexlify(unicode('a', 'ascii')), '61')
self.assertEqual(binascii.b2a_hex(unicode('a', 'ascii')), '61')

def test_qp(self):
type2test = self.type2test
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from test import test_support


testmeths = [

# Binary operations
Expand Down Expand Up @@ -549,7 +550,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
1 change: 1 addition & 0 deletions Lib/test/test_future5.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class TestMultipleFeatures(unittest.TestCase):
def test_unicode_literals(self):
self.assertIsInstance("", unicode)

@unittest.skipIf(sys.py3kwarning, "messaging confuses log")
def test_print_function(self):
with test_support.captured_output("stderr") as s:
print("foo", file=sys.stderr)
Expand Down
Loading

0 comments on commit 2a1224d

Please sign in to comment.