Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
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
Gwenaël Samain
cython
Commits
efd1e759
Commit
efd1e759
authored
Mar 09, 2013
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
raise NameError instead of KeyError when del-ing a non-existing name inside of a Python class body
parent
b421819a
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
5 deletions
+30
-5
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+12
-5
tests/run/py_classbody.py
tests/run/py_classbody.py
+18
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
efd1e759
...
@@ -1945,13 +1945,20 @@ class NameNode(AtomicExprNode):
...
@@ -1945,13 +1945,20 @@ class NameNode(AtomicExprNode):
elif
self
.
entry
.
is_pyclass_attr
:
elif
self
.
entry
.
is_pyclass_attr
:
namespace
=
self
.
entry
.
scope
.
namespace_cname
namespace
=
self
.
entry
.
scope
.
namespace_cname
interned_cname
=
code
.
intern_identifier
(
self
.
entry
.
name
)
interned_cname
=
code
.
intern_identifier
(
self
.
entry
.
name
)
del_code
=
'PyObject_DelItem(%s, %s)'
%
(
namespace
,
interned_cname
)
if
ignore_nonexisting
:
if
ignore_nonexisting
:
code
.
putln
(
'if (unlikely(%s < 0)) { if (likely(PyErr_ExceptionMatches(PyExc_KeyError))) PyErr_Clear(); else %s }'
%
(
key_error_code
=
'PyErr_Clear(); else'
del_code
,
code
.
error_goto
(
self
.
pos
)))
else
:
else
:
code
.
put_error_if_neg
(
self
.
pos
,
del_code
)
# minor hack: fake a NameError on KeyError
key_error_code
=
(
'{ PyErr_Clear(); PyErr_Format(PyExc_NameError, "name
\
'
%%s
\
'
is not defined", "%s"); }'
%
self
.
entry
.
name
)
code
.
putln
(
'if (unlikely(PyObject_DelItem(%s, %s) < 0)) {'
' if (likely(PyErr_ExceptionMatches(PyExc_KeyError))) %s'
' %s '
'}'
%
(
namespace
,
interned_cname
,
key_error_code
,
code
.
error_goto
(
self
.
pos
)))
elif
self
.
entry
.
is_pyglobal
:
elif
self
.
entry
.
is_pyglobal
:
code
.
globalstate
.
use_utility_code
(
code
.
globalstate
.
use_utility_code
(
UtilityCode
.
load_cached
(
"PyObjectSetAttrStr"
,
"ObjectHandling.c"
))
UtilityCode
.
load_cached
(
"PyObjectSetAttrStr"
,
"ObjectHandling.c"
))
...
...
tests/run/py_classbody.py
View file @
efd1e759
...
@@ -57,3 +57,21 @@ class ForLoopInPyClass(object):
...
@@ -57,3 +57,21 @@ class ForLoopInPyClass(object):
for
m
in
range
(
2
):
for
m
in
range
(
2
):
pass
pass
def
del_in_class
(
x
):
"""
>>> del_in_class(True)
no error
>>> del_in_class(False)
NameError
"""
try
:
class
Test
(
object
):
if
x
:
attr
=
1
del
attr
except
NameError
:
print
(
"NameError"
)
else
:
print
(
"no error"
)
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