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
f85a856f
Commit
f85a856f
authored
Oct 10, 2014
by
Petri Lehtinen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #11694: Raise ConversionError in xdrlib as documented
parent
c305ad73
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
8 deletions
+52
-8
Lib/test/test_xdrlib.py
Lib/test/test_xdrlib.py
+24
-0
Lib/xdrlib.py
Lib/xdrlib.py
+25
-8
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/test/test_xdrlib.py
View file @
f85a856f
...
...
@@ -51,8 +51,32 @@ class XDRTest(unittest.TestCase):
up
.
done
()
self
.
assertRaises
(
EOFError
,
up
.
unpack_uint
)
class
ConversionErrorTest
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
packer
=
xdrlib
.
Packer
()
def
assertRaisesConversion
(
self
,
*
args
):
self
.
assertRaises
(
xdrlib
.
ConversionError
,
*
args
)
def
test_pack_int
(
self
):
self
.
assertRaisesConversion
(
self
.
packer
.
pack_int
,
'string'
)
def
test_pack_uint
(
self
):
self
.
assertRaisesConversion
(
self
.
packer
.
pack_uint
,
'string'
)
def
test_float
(
self
):
self
.
assertRaisesConversion
(
self
.
packer
.
pack_float
,
'string'
)
def
test_double
(
self
):
self
.
assertRaisesConversion
(
self
.
packer
.
pack_double
,
'string'
)
def
test_uhyper
(
self
):
self
.
assertRaisesConversion
(
self
.
packer
.
pack_uhyper
,
'string'
)
def
test_main
():
test_support
.
run_unittest
(
XDRTest
)
test_support
.
run_unittest
(
ConversionErrorTest
)
if
__name__
==
"__main__"
:
test_main
()
Lib/xdrlib.py
View file @
f85a856f
...
...
@@ -9,6 +9,7 @@ try:
from
cStringIO
import
StringIO
as
_StringIO
except
ImportError
:
from
StringIO
import
StringIO
as
_StringIO
from
functools
import
wraps
__all__
=
[
"Error"
,
"Packer"
,
"Unpacker"
,
"ConversionError"
]
...
...
@@ -34,6 +35,16 @@ class Error(Exception):
class
ConversionError
(
Error
):
pass
def
raise_conversion_error
(
function
):
""" Wrap any raised struct.errors in a ConversionError. """
@
wraps
(
function
)
def
result
(
self
,
value
):
try
:
return
function
(
self
,
value
)
except
struct
.
error
as
e
:
raise
ConversionError
(
e
.
args
[
0
])
return
result
class
Packer
:
...
...
@@ -50,9 +61,11 @@ class Packer:
# backwards compatibility
get_buf
=
get_buffer
@
raise_conversion_error
def
pack_uint
(
self
,
x
):
self
.
__buf
.
write
(
struct
.
pack
(
'>L'
,
x
))
@
raise_conversion_error
def
pack_int
(
self
,
x
):
self
.
__buf
.
write
(
struct
.
pack
(
'>l'
,
x
))
...
...
@@ -63,20 +76,24 @@ class Packer:
else
:
self
.
__buf
.
write
(
'
\
0
\
0
\
0
\
0
'
)
def
pack_uhyper
(
self
,
x
):
self
.
pack_uint
(
x
>>
32
&
0xffffffff
L
)
self
.
pack_uint
(
x
&
0xffffffff
L
)
try
:
self
.
pack_uint
(
x
>>
32
&
0xffffffff
L
)
except
(
TypeError
,
struct
.
error
)
as
e
:
raise
ConversionError
(
e
.
args
[
0
])
try
:
self
.
pack_uint
(
x
&
0xffffffff
L
)
except
(
TypeError
,
struct
.
error
)
as
e
:
raise
ConversionError
(
e
.
args
[
0
])
pack_hyper
=
pack_uhyper
@
raise_conversion_error
def
pack_float
(
self
,
x
):
try
:
self
.
__buf
.
write
(
struct
.
pack
(
'>f'
,
x
))
except
struct
.
error
,
msg
:
raise
ConversionError
,
msg
self
.
__buf
.
write
(
struct
.
pack
(
'>f'
,
x
))
@
raise_conversion_error
def
pack_double
(
self
,
x
):
try
:
self
.
__buf
.
write
(
struct
.
pack
(
'>d'
,
x
))
except
struct
.
error
,
msg
:
raise
ConversionError
,
msg
self
.
__buf
.
write
(
struct
.
pack
(
'>d'
,
x
))
def
pack_fstring
(
self
,
n
,
s
):
if
n
<
0
:
...
...
Misc/NEWS
View file @
f85a856f
...
...
@@ -34,6 +34,9 @@ Core and Builtins
Library
-------
-
Issue
#
11694
:
Raise
ConversionError
in
xdrlib
as
documented
.
Patch
by
Filip
Gruszczy
ń
ski
and
Claudiu
Popa
.
-
Issue
#
1686
:
Fix
string
.
Template
when
overriding
the
pattern
attribute
.
-
Issue
#
11866
:
Eliminated
race
condition
in
the
computation
of
names
...
...
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