Skip to content

Commit

Permalink
Merge python#19
Browse files Browse the repository at this point in the history
19: Warn for range and xrange r=ltratt a=nanjekyejoannah

See notes:

```
Py2.x:

>>> x = xrange(1, 10000)
>>> a = range(1,10000)
>>> type(a)
<type 'list'>
>>> 

Py3.x:

>>> x = xrange(1, 10000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'xrange' is not defined
>>> a = range(1,10000)
>>> type(a)
<class 'range'>
>>> 
```

Co-authored-by: Joannah Nanjekye <[email protected]>
  • Loading branch information
bors[bot] and nanjekyejoannah authored Jan 12, 2023
2 parents 3e44bb0 + af6a3d4 commit 5f5332a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Lib/test/test_xrange.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Python test set -- built-in functions

import test.test_support, unittest
from test.test_support import check_py3k_warnings
import sys
import pickle
import itertools
Expand Down Expand Up @@ -179,6 +180,11 @@ def test_repr(self):
r_out = eval(repr(r))
self.assert_xranges_equivalent(r, r_out)

def test_xrange_3k(self):
expected = "xrange() is not supported in 3.x: use range() instead"
with check_py3k_warnings((expected, DeprecationWarning)):
xrange(1, 10000)

def test_range_iterators(self):
# see issue 7298
limits = [base + jiggle
Expand Down
3 changes: 3 additions & 0 deletions Objects/rangeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ range_new(PyTypeObject *type, PyObject *args, PyObject *kw)
long ilow = 0, ihigh = 0, istep = 1;
unsigned long n;

if (PyErr_WarnPy3k_WithFix("xrange() is not supported in 3.x", "use range() instead", 1) < 0)
return NULL;

if (!_PyArg_NoKeywords("xrange()", kw))
return NULL;

Expand Down

0 comments on commit 5f5332a

Please sign in to comment.