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
43ba295b
Commit
43ba295b
authored
15 years ago
by
Robert Bradshaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
CDP 516 warnings
parent
33784d77
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
105 additions
and
10 deletions
+105
-10
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+85
-9
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+2
-1
Cython/Compiler/Options.py
Cython/Compiler/Options.py
+1
-0
tests/run/cdivision_CEP_516.pyx
tests/run/cdivision_CEP_516.pyx
+17
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
43ba295b
...
...
@@ -9,6 +9,7 @@ from Errors import hold_errors, release_errors, held_errors, report_error
from
Cython.Utils
import
UtilityCode
import
StringEncoding
import
Naming
import
Nodes
from
Nodes
import
Node
import
PyrexTypes
from
PyrexTypes
import
py_object_type
,
c_long_type
,
typecast
,
error_type
...
...
@@ -4233,6 +4234,7 @@ class DivNode(NumBinopNode):
# '/' or '//' operator.
cdivision
=
None
cdivision_warnings
=
False
def
generate_evaluation_code
(
self
,
code
):
if
not
self
.
type
.
is_pyobject
:
...
...
@@ -4240,12 +4242,22 @@ class DivNode(NumBinopNode):
self
.
cdivision
=
(
code
.
globalstate
.
directives
[
'cdivision'
]
or
not
self
.
type
.
signed
or
self
.
type
.
is_float
)
if
not
self
.
cdivision
:
if
code
.
globalstate
.
directives
[
'cdivision_warnings'
]:
self
.
cdivision_warnings
=
True
code
.
globalstate
.
use_utility_code
(
div_mod_print_warning_utility_code
)
code
.
globalstate
.
use_utility_code
(
div_warn_utility_code
.
specialize
(
self
.
type
))
elif
not
self
.
cdivision
:
code
.
globalstate
.
use_utility_code
(
div_utility_code
.
specialize
(
self
.
type
))
NumBinopNode
.
generate_evaluation_code
(
self
,
code
)
def
calculate_result_code
(
self
):
if
self
.
cdivision
:
if
self
.
cdivision_warnings
:
return
"__Pyx_div_warn_%s(%s, %s, %s)"
%
(
self
.
type
.
specalization_name
(),
self
.
operand1
.
result
(),
self
.
operand2
.
result
(),
int
(
self
.
cdivision
))
elif
self
.
cdivision
:
return
"(%s / %s)"
%
(
self
.
operand1
.
result
(),
self
.
operand2
.
result
())
...
...
@@ -4256,11 +4268,9 @@ class DivNode(NumBinopNode):
self
.
operand2
.
result
())
class
ModNode
(
NumBinop
Node
):
class
ModNode
(
Div
Node
):
# '%' operator.
cdivision
=
None
def
is_py_operation
(
self
):
return
(
self
.
operand1
.
type
.
is_string
or
self
.
operand2
.
type
.
is_string
...
...
@@ -4270,15 +4280,27 @@ class ModNode(NumBinopNode):
if
not
self
.
type
.
is_pyobject
:
if
self
.
cdivision
is
None
:
self
.
cdivision
=
code
.
globalstate
.
directives
[
'cdivision'
]
or
not
self
.
type
.
signed
if
not
self
.
cdivision
:
math_h_modifier
=
getattr
(
self
.
type
,
'math_h_modifier'
,
'__Pyx_INT'
)
if
code
.
globalstate
.
directives
[
'cdivision_warnings'
]:
self
.
cdivision_warnings
=
True
if
self
.
type
.
is_int
:
code
.
globalstate
.
use_utility_code
(
mod_int_helper_macro
)
code
.
globalstate
.
use_utility_code
(
div_mod_print_warning_utility_code
)
code
.
globalstate
.
use_utility_code
(
mod_warn_utility_code
.
specialize
(
self
.
type
,
math_h_modifier
=
math_h_modifier
))
elif
not
self
.
cdivision
:
if
self
.
type
.
is_int
:
code
.
globalstate
.
use_utility_code
(
mod_int_helper_macro
)
code
.
globalstate
.
use_utility_code
(
mod_utility_code
.
specialize
(
self
.
type
,
math_h_modifier
=
math_h_modifier
))
NumBinopNode
.
generate_evaluation_code
(
self
,
code
)
def
calculate_result_code
(
self
):
if
self
.
cdivision
:
if
self
.
cdivision_warnings
:
return
"__Pyx_mod_warn_%s(%s, %s, %s)"
%
(
self
.
type
.
specalization_name
(),
self
.
operand1
.
result
(),
self
.
operand2
.
result
(),
int
(
self
.
cdivision
))
elif
self
.
cdivision
:
if
self
.
type
.
is_float
:
return
"fmod%s(%s, %s)"
%
(
self
.
type
.
math_h_modifier
,
...
...
@@ -5707,3 +5729,57 @@ static INLINE %(type)s __Pyx_div_%(type_name)s(%(type)s a, %(type)s b) {
return res;
}
"""
)
mod_warn_utility_code
=
UtilityCode
(
proto
=
"""
static INLINE %(type)s __Pyx_mod_warn_%(type_name)s(%(type)s, %(type)s, int cdivision); /* proto */
"""
,
impl
=
"""
static INLINE %(type)s __Pyx_mod_warn_%(type_name)s(%(type)s a, %(type)s b, int cdivision) {
%(type)s res = fmod%(math_h_modifier)s(a, b);
if (res * b < 0) {
__Pyx_div_mod_warning();
if (!cdivision) res += b;
}
return res;
}
"""
)
div_warn_utility_code
=
UtilityCode
(
proto
=
"""
static INLINE %(type)s __Pyx_div_warn_%(type_name)s(%(type)s, %(type)s, int cdivision); /* proto */
"""
,
impl
=
"""
static INLINE %(type)s __Pyx_div_warn_%(type_name)s(%(type)s a, %(type)s b, int cdivision) {
%(type)s res = a / b;
if (res < 0) {
if (__Pyx_div_mod_warning()) ;
if (!cdivision) res -= 1;
}
return res;
}
"""
)
div_mod_print_warning_utility_code
=
UtilityCode
(
proto
=
"""
static int __Pyx_div_mod_warning(void); /* proto */
"""
,
impl
=
"""
static int __Pyx_div_mod_warning(void) {
int r;
PyObject* s;
#if PY_MAJOR_VERSION < 3
s = PyString_FromString("Warning: division with oppositely signed operands, C and Python semantics differ");
#else
s = PyUnicode_FromString("Warning: division with oppositely signed operands, C and Python semantics differ");
#endif
if (s) {
r = __Pyx_PrintOne(s);
Py_DECREF(s);
}
else r = -1;
return r;
}
"""
,
requires
=
[
Nodes
.
printing_one_utility_code
])
This diff is collapsed.
Click to expand it.
Cython/Compiler/Nodes.py
View file @
43ba295b
...
...
@@ -4976,7 +4976,8 @@ static int __Pyx_PrintOne(PyObject *o) {
}
#endif
"""
)
"""
,
requires
=
[
printing_utility_code
])
...
...
This diff is collapsed.
Click to expand it.
Cython/Compiler/Options.py
View file @
43ba295b
...
...
@@ -62,6 +62,7 @@ option_defaults = {
'locals'
:
{},
'auto_cpdef'
:
False
,
'cdivision'
:
True
,
# Will be False in 0.12
'cdivision_warnings'
:
False
,
}
# Override types possibilities above, if needed
...
...
This diff is collapsed.
Click to expand it.
tests/run/cdivision_CEP_516.pyx
View file @
43ba295b
...
...
@@ -26,6 +26,13 @@ True
>>> [test_cdiv_cmod(a, b) for a, b in v]
[(1, 7), (-1, -7), (1, -7), (-1, 7)]
>>> mod_int_py_warn(-17, 10)
Warning: division with oppositely signed operands, C and Python semantics differ
-7
>>> div_int_py_warn(-17, 10)
Warning: division with oppositely signed operands, C and Python semantics differ
-1
"""
cimport
cython
...
...
@@ -73,3 +80,13 @@ def test_cdiv_cmod(short a, short b):
cdef
short
q
=
cython
.
cdiv
(
a
,
b
)
cdef
short
r
=
cython
.
cmod
(
a
,
b
)
return
q
,
r
@
cython
.
cdivision
(
True
)
@
cython
.
cdivision_warnings
(
True
)
def
mod_int_py_warn
(
int
a
,
int
b
):
return
a
%
b
@
cython
.
cdivision
(
True
)
@
cython
.
cdivision_warnings
(
True
)
def
div_int_py_warn
(
int
a
,
int
b
):
return
a
//
b
This diff is collapsed.
Click to expand it.
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