Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Z
Zope
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
Zope
Commits
92754b71
Commit
92754b71
authored
May 06, 2009
by
Tres Seaver
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Break out logic for converting string name to exception type.
parent
190edec1
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
18 deletions
+77
-18
src/zExceptions/__init__.py
src/zExceptions/__init__.py
+23
-18
src/zExceptions/tests/test___init__.py
src/zExceptions/tests/test___init__.py
+54
-0
No files found.
src/zExceptions/__init__.py
View file @
92754b71
...
@@ -18,6 +18,7 @@ application-specific packages.
...
@@ -18,6 +18,7 @@ application-specific packages.
$Id$
$Id$
"""
"""
from
types
import
ClassType
import
warnings
import
warnings
from
zope.interface
import
implements
from
zope.interface
import
implements
...
@@ -44,27 +45,31 @@ class MethodNotAllowed(Exception):
...
@@ -44,27 +45,31 @@ class MethodNotAllowed(Exception):
class
Redirect
(
Exception
):
class
Redirect
(
Exception
):
pass
pass
def
convertExceptionType
(
name
):
import
zExceptions
etype
=
None
if
__builtins__
.
has_key
(
name
):
etype
=
__builtins__
[
name
]
elif
hasattr
(
zExceptions
,
name
):
etype
=
getattr
(
zExceptions
,
name
)
if
(
etype
is
not
None
and
isinstance
(
etype
,
(
type
,
ClassType
))
and
issubclass
(
etype
,
Exception
)):
return
etype
def
upgradeException
(
t
,
v
):
def
upgradeException
(
t
,
v
):
# If a string exception is found, convert it to an equivalent
# If a string exception is found, convert it to an equivalent
# exception defined either in builtins or zExceptions. If none of
# exception defined either in builtins or zExceptions. If none of
# that works, tehn convert it to an InternalError and keep the
# that works, tehn convert it to an InternalError and keep the
# original exception name as part of the exception value.
# original exception name as part of the exception value.
import
zExceptions
if
isinstance
(
t
,
basestring
):
if
not
isinstance
(
t
,
basestring
):
return
t
,
v
warnings
.
warn
(
'String exceptions are deprecated starting '
warnings
.
warn
(
'String exceptions are deprecated starting '
'with Python 2.5 and will be removed in a '
'with Python 2.5 and will be removed in a '
'future release'
,
DeprecationWarning
,
stacklevel
=
2
)
'future release'
,
DeprecationWarning
,
stacklevel
=
2
)
n
=
None
etype
=
convertExceptionType
(
t
)
if
__builtins__
.
has_key
(
t
):
if
etype
is
not
None
:
n
=
__builtins__
[
t
]
t
=
etype
elif
hasattr
(
zExceptions
,
t
):
n
=
getattr
(
zExceptions
,
t
)
if
n
is
not
None
and
issubclass
(
n
,
Exception
):
t
=
n
else
:
else
:
v
=
t
,
v
v
=
t
,
v
t
=
InternalError
t
=
InternalError
...
...
src/zExceptions/tests/test___init__.py
0 → 100644
View file @
92754b71
import
unittest
class
Test_convertExceptionType
(
unittest
.
TestCase
):
def
_callFUT
(
self
,
name
):
from
zExceptions
import
convertExceptionType
return
convertExceptionType
(
name
)
def
test_name_in___builtins__
(
self
):
self
.
failUnless
(
self
.
_callFUT
(
'SyntaxError'
)
is
SyntaxError
)
def
test_name_in___builtins___not_an_exception_returns_None
(
self
):
self
.
failUnless
(
self
.
_callFUT
(
'unichr'
)
is
None
)
def
test_name_in_zExceptions
(
self
):
from
zExceptions
import
Redirect
self
.
failUnless
(
self
.
_callFUT
(
'Redirect'
)
is
Redirect
)
def
test_name_in_zExceptions_not_an_exception_returns_None
(
self
):
self
.
failUnless
(
self
.
_callFUT
(
'convertExceptionType'
)
is
None
)
class
Test_upgradeException
(
unittest
.
TestCase
):
def
_callFUT
(
self
,
t
,
v
):
from
zExceptions
import
upgradeException
return
upgradeException
(
t
,
v
)
def
test_non_string
(
self
):
t
,
v
=
self
.
_callFUT
(
SyntaxError
,
'TEST'
)
self
.
assertEqual
(
t
,
SyntaxError
)
self
.
assertEqual
(
v
,
'TEST'
)
def
test_string_in___builtins__
(
self
):
t
,
v
=
self
.
_callFUT
(
'SyntaxError'
,
'TEST'
)
self
.
assertEqual
(
t
,
SyntaxError
)
self
.
assertEqual
(
v
,
'TEST'
)
def
test_string_in_zExceptions
(
self
):
from
zExceptions
import
Redirect
t
,
v
=
self
.
_callFUT
(
'Redirect'
,
'http://example.com/'
)
self
.
assertEqual
(
t
,
Redirect
)
self
.
assertEqual
(
v
,
'http://example.com/'
)
def
test_string_miss_returns_InternalError
(
self
):
from
zExceptions
import
InternalError
t
,
v
=
self
.
_callFUT
(
'Nonesuch'
,
'TEST'
)
self
.
assertEqual
(
t
,
InternalError
)
self
.
assertEqual
(
v
,
(
'Nonesuch'
,
'TEST'
))
def
test_suite
():
return
unittest
.
TestSuite
((
unittest
.
makeSuite
(
Test_convertExceptionType
),
unittest
.
makeSuite
(
Test_upgradeException
),
))
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