Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
f9a5a8e0
Commit
f9a5a8e0
authored
May 26, 2010
by
Mark Dickinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #2844: Make int('42', n) consistently raise ValueError for
invalid integers n (including n = -909).
parent
d5442cd3
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
9 deletions
+33
-9
Lib/test/test_long.py
Lib/test/test_long.py
+10
-0
Misc/NEWS
Misc/NEWS
+3
-0
Objects/longobject.c
Objects/longobject.c
+20
-9
No files found.
Lib/test/test_long.py
View file @
f9a5a8e0
...
...
@@ -345,6 +345,16 @@ class LongTest(unittest.TestCase):
self
.
assertRaises
(
ValueError
,
int
,
'08'
,
0
)
self
.
assertRaises
(
ValueError
,
int
,
'-012395'
,
0
)
# invalid bases
invalid_bases
=
[
-
909
,
2
**
31
-
1
,
2
**
31
,
-
2
**
31
,
-
2
**
31
-
1
,
2
**
63
-
1
,
2
**
63
,
-
2
**
63
,
-
2
**
63
-
1
,
2
**
100
,
-
2
**
100
,
]
for
base
in
invalid_bases
:
self
.
assertRaises
(
ValueError
,
int
,
'42'
,
base
)
def
test_conversion
(
self
):
class
JustLong
:
...
...
Misc/NEWS
View file @
f9a5a8e0
...
...
@@ -12,6 +12,9 @@ What's New in Python 3.2 Alpha 1?
Core and Builtins
-----------------
- Issue #2844: Make int('42', n) consistently raise ValueError for
invalid integers n (including n = -909).
- Issue #8188: Introduce a new scheme for computing hashes of numbers
(instances of int, float, complex, decimal.Decimal and
fractions.Fraction) that makes it easy to maintain the invariant
...
...
Objects/longobject.c
View file @
f9a5a8e0
...
...
@@ -4098,23 +4098,34 @@ long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static
PyObject
*
long_new
(
PyTypeObject
*
type
,
PyObject
*
args
,
PyObject
*
kwds
)
{
PyObject
*
x
=
NULL
;
int
base
=
-
909
;
/* unlikely! */
PyObject
*
obase
=
NULL
,
*
x
=
NULL
;
long
base
;
int
overflow
;
static
char
*
kwlist
[]
=
{
"x"
,
"base"
,
0
};
if
(
type
!=
&
PyLong_Type
)
return
long_subtype_new
(
type
,
args
,
kwds
);
/* Wimp out */
if
(
!
PyArg_ParseTupleAndKeywords
(
args
,
kwds
,
"|O
i
:int"
,
kwlist
,
&
x
,
&
base
))
if
(
!
PyArg_ParseTupleAndKeywords
(
args
,
kwds
,
"|O
O
:int"
,
kwlist
,
&
x
,
&
o
base
))
return
NULL
;
if
(
x
==
NULL
)
return
PyLong_FromLong
(
0L
);
if
(
base
==
-
909
)
if
(
obase
==
NULL
)
return
PyNumber_Long
(
x
);
else
if
(
PyUnicode_Check
(
x
))
base
=
PyLong_AsLongAndOverflow
(
obase
,
&
overflow
);
if
(
base
==
-
1
&&
PyErr_Occurred
())
return
NULL
;
if
(
overflow
||
(
base
!=
0
&&
base
<
2
)
||
base
>
36
)
{
PyErr_SetString
(
PyExc_ValueError
,
"int() arg 2 must be >= 2 and <= 36"
);
return
NULL
;
}
if
(
PyUnicode_Check
(
x
))
return
PyLong_FromUnicode
(
PyUnicode_AS_UNICODE
(
x
),
PyUnicode_GET_SIZE
(
x
),
base
);
(
int
)
base
);
else
if
(
PyByteArray_Check
(
x
)
||
PyBytes_Check
(
x
))
{
/* Since PyLong_FromString doesn't have a length parameter,
* check here for possible NULs in the string. */
...
...
@@ -4129,10 +4140,10 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
x is a bytes or buffer, *and* a base is given. */
PyErr_Format
(
PyExc_ValueError
,
"invalid literal for int() with base %d: %R"
,
base
,
x
);
(
int
)
base
,
x
);
return
NULL
;
}
return
PyLong_FromString
(
string
,
NULL
,
base
);
return
PyLong_FromString
(
string
,
NULL
,
(
int
)
base
);
}
else
{
PyErr_SetString
(
PyExc_TypeError
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment