-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid using strings for exception_value of func_type #5710
Changes from 2 commits
4c0d095
3f29ce4
f3f3dd1
dab7347
b9fe05c
63dc932
9417b32
9666e11
30325f0
2562ad2
0ffd487
1abb981
9dd95fa
e49988c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -740,8 +740,8 @@ def analyse(self, return_type, env, nonempty=0, directive_locals=None, visibilit | |||||
# for now. | ||||||
if not env.is_c_class_scope and not isinstance(self.base, CPtrDeclaratorNode): | ||||||
from .ExprNodes import ConstNode | ||||||
self.exception_value = ConstNode( | ||||||
self.pos, value=return_type.exception_value, type=return_type) | ||||||
self.exception_value = ConstNode.make_specific_const_node( | ||||||
self.pos, value=str(return_type.exception_value), type=return_type) | ||||||
if self.exception_value: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You changed this to
Suggested change
|
||||||
if self.exception_check == '+': | ||||||
self.exception_value = self.exception_value.analyse_const_expression(env) | ||||||
|
@@ -759,8 +759,10 @@ def analyse(self, return_type, env, nonempty=0, directive_locals=None, visibilit | |||||
else: | ||||||
self.exception_value = self.exception_value.analyse_types(env).coerce_to( | ||||||
return_type, env).analyse_const_expression(env) | ||||||
exc_val = self.exception_value.get_constant_c_result_code() | ||||||
if exc_val is None: | ||||||
self.exception_value.calculate_constant_result() | ||||||
exc_val = self.exception_value.constant_result | ||||||
from .ExprNodes import constant_value_not_set | ||||||
if exc_val is None or exc_val is constant_value_not_set: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this something that We should also make sure that this works for enum values etc., anything that is usable as exception value. |
||||||
error(self.exception_value.pos, "Exception value must be constant") | ||||||
if not return_type.assignable_from(self.exception_value.type): | ||||||
error(self.exception_value.pos, | ||||||
|
@@ -3125,7 +3127,7 @@ def as_cfunction(self, cfunc=None, scope=None, overridable=True, returns=None, e | |||||
|
||||||
if exception_value is None and cfunc_type.exception_value is not None: | ||||||
from .ExprNodes import ConstNode | ||||||
exception_value = ConstNode( | ||||||
exception_value = ConstNode.make_specific_const_node( | ||||||
self.pos, value=cfunc_type.exception_value, type=cfunc_type.return_type) | ||||||
declarator = CFuncDeclaratorNode(self.pos, | ||||||
base=CNameDeclaratorNode(self.pos, name=self.name, cname=None), | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can probably build this automatically from the subclasses of
ConstNode
and their types. That reduces the chance of forgetting to update this method. You forgotstr_type
, at least.https://stackoverflow.com/questions/3862310/how-to-find-all-the-subclasses-of-a-class-given-its-name
I think a less redundant name would be
ConstNode.for_type()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StringNode
is a subclass ofPyConstNode
rather thanConstNode
so wouldn't be caught by the subclasses check. I also think it's difficult to do while still supporting Python 2.I've changed the name though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are looking at the Cython literal type here, right? Not the Python object type? Then it's easy to do because we see these types in the source code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I more meant the
__subclasses__
mechanism that is used by the stack overflow page that you linked doesn't exist in Python 2, so it's difficult to get a list of all the subclasses ofConstNode