Skip to content

Commit 5b876e2

Browse files
Merge python#7
7: Add warnings for sorting and comparison r=ltratt a=nanjekyejoannah Most of the warnings are covered on the list sort method. I added the missing warnings for the `cmp` and `__cmp__` method. This replaces python#4 Co-authored-by: Joannah Nanjekye <jnanjekye@python.org>
2 parents b32aade + 5a3c576 commit 5b876e2

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

Include/warnings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ extern "C" {
77
PyAPI_FUNC(void) _PyWarnings_Init(void);
88

99
PyAPI_FUNC(int) PyErr_WarnEx(PyObject *, const char *, Py_ssize_t);
10+
PyAPI_FUNC(int) PyErr_WarnEx_WithFix(PyObject *, const char *, const char *, Py_ssize_t);
1011
PyAPI_FUNC(int) PyErr_WarnExplicit(PyObject *, const char *, const char *, int,
1112
const char *, PyObject *);
1213
PyAPI_FUNC(int) PyErr_WarnExplicit_WithFix(PyObject *, const char *, const char *, const char *, int,

Lib/test/test_sort.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ def test_main(verbose=None):
278278
("the cmp argument is not supported", DeprecationWarning)):
279279
test_support.run_unittest(*test_classes)
280280

281+
with test_support.check_py3k_warnings(
282+
("the cmp method is not supported in 3.x"
283+
"implement the function to a utility library", Py3xWarning)):
284+
test_support.run_unittest(*test_classes)
285+
281286
# verify reference counting
282287
if verbose and hasattr(sys, "gettotalrefcount"):
283288
import gc

Objects/object.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +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",
865+
"you can either provide your own alternative or use a third party library with a "
866+
"backwards compatible fix", 1) < 0) {
867+
return 0;
868+
}
869+
863870
if (v == NULL || w == NULL) {
864871
PyErr_BadInternalCall();
865872
return -1;

Python/_warnings.c

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,23 @@ do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level)
817817
return res;
818818
}
819819

820+
static PyObject *
821+
do_warn_with_fix(PyObject *message, PyObject *fix, PyObject *category, Py_ssize_t stack_level)
822+
{
823+
PyObject *filename, *module, *registry, *res;
824+
int lineno;
825+
826+
if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
827+
return NULL;
828+
829+
res = warn_explicit_with_fix(category, message, fix, filename, lineno, module, registry,
830+
NULL);
831+
Py_DECREF(filename);
832+
Py_DECREF(registry);
833+
Py_DECREF(module);
834+
return res;
835+
}
836+
820837
static PyObject *
821838
warnings_warn(PyObject *self, PyObject *args, PyObject *kwds)
822839
{
@@ -1029,8 +1046,28 @@ PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
10291046
return 0;
10301047
}
10311048

1032-
/* PyErr_Warn is only for backwards compatibility and will be removed.
1033-
Use PyErr_WarnEx instead. */
1049+
/* Function to issue a warning message; may raise an exception. */
1050+
int
1051+
PyErr_WarnEx_WithFix(PyObject *category, const char *text, const char *fix_txt, Py_ssize_t stack_level)
1052+
{
1053+
PyObject *res;
1054+
PyObject *message = PyString_FromString(text);
1055+
PyObject *fix = PyString_FromString(fix_txt);
1056+
if (message == NULL)
1057+
return -1;
1058+
1059+
if (category == NULL)
1060+
category = PyExc_RuntimeWarning;
1061+
1062+
res = do_warn_with_fix(message, fix, category, stack_level);
1063+
Py_DECREF(message);
1064+
Py_DECREF(fix);
1065+
if (res == NULL)
1066+
return -1;
1067+
Py_DECREF(res);
1068+
1069+
return 0;
1070+
}
10341071

10351072
#undef PyErr_Warn
10361073

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy