Skip to content

Commit

Permalink
Merge python#7
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bors[bot] and nanjekyejoannah authored Apr 15, 2022
2 parents b32aade + 5a3c576 commit 5b876e2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions Include/warnings.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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,
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ def test_main(verbose=None):
("the cmp argument is not supported", DeprecationWarning)):
test_support.run_unittest(*test_classes)

with test_support.check_py3k_warnings(
("the cmp method is not supported in 3.x"
"implement the function to a utility library", Py3xWarning)):
test_support.run_unittest(*test_classes)

# verify reference counting
if verbose and hasattr(sys, "gettotalrefcount"):
import gc
Expand Down
7 changes: 7 additions & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +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",
"you can either provide your own alternative or use a third party library with a "
"backwards compatible fix", 1) < 0) {
return 0;
}

if (v == NULL || w == NULL) {
PyErr_BadInternalCall();
return -1;
Expand Down
41 changes: 39 additions & 2 deletions Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,23 @@ do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level)
return res;
}

static PyObject *
do_warn_with_fix(PyObject *message, PyObject *fix, PyObject *category, Py_ssize_t stack_level)
{
PyObject *filename, *module, *registry, *res;
int lineno;

if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
return NULL;

res = warn_explicit_with_fix(category, message, fix, filename, lineno, module, registry,
NULL);
Py_DECREF(filename);
Py_DECREF(registry);
Py_DECREF(module);
return res;
}

static PyObject *
warnings_warn(PyObject *self, PyObject *args, PyObject *kwds)
{
Expand Down Expand Up @@ -1029,8 +1046,28 @@ PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
return 0;
}

/* PyErr_Warn is only for backwards compatibility and will be removed.
Use PyErr_WarnEx instead. */
/* Function to issue a warning message; may raise an exception. */
int
PyErr_WarnEx_WithFix(PyObject *category, const char *text, const char *fix_txt, Py_ssize_t stack_level)
{
PyObject *res;
PyObject *message = PyString_FromString(text);
PyObject *fix = PyString_FromString(fix_txt);
if (message == NULL)
return -1;

if (category == NULL)
category = PyExc_RuntimeWarning;

res = do_warn_with_fix(message, fix, category, stack_level);
Py_DECREF(message);
Py_DECREF(fix);
if (res == NULL)
return -1;
Py_DECREF(res);

return 0;
}

#undef PyErr_Warn

Expand Down

0 comments on commit 5b876e2

Please sign in to comment.