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
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
12ad333d
Commit
12ad333d
authored
Apr 14, 2010
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
enable 'not/or None' annotation for builtin types
parent
846acc18
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
152 additions
and
1 deletion
+152
-1
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+1
-1
tests/run/ext_type_none_arg.pyx
tests/run/ext_type_none_arg.pyx
+151
-0
No files found.
Cython/Compiler/Nodes.py
View file @
12ad333d
...
...
@@ -1880,7 +1880,7 @@ class DefNode(FuncDefNode):
arg
.
needs_conversion
=
0
arg
.
needs_type_test
=
0
arg
.
is_generic
=
1
if
arg
.
type
.
is_extension_type
:
if
arg
.
type
.
is_extension_type
or
arg
.
type
.
is_builtin_type
:
if
arg
.
or_none
:
arg
.
accept_none
=
True
elif
arg
.
not_none
:
...
...
tests/run/ext_type_none_arg.pyx
0 → 100644
View file @
12ad333d
cimport
cython
### extension types
cdef
class
MyExtType
:
cdef
object
attr
def
__cinit__
(
self
):
self
.
attr
=
123
cdef
attr
(
MyExtType
x
):
return
x
is
None
and
321
or
x
.
attr
# defaults, without 'not/or None'
def
ext_default
(
MyExtType
x
):
"""
>>> ext_default(MyExtType())
123
>>> ext_default(None)
321
"""
return
attr
(
x
)
@
cython
.
allow_none_for_extension_args
(
True
)
def
ext_default_check_off
(
MyExtType
x
):
"""
>>> ext_default_check_off(MyExtType())
123
>>> ext_default_check_off(None)
321
"""
return
attr
(
x
)
@
cython
.
allow_none_for_extension_args
(
False
)
def
ext_default_check_on
(
MyExtType
x
):
"""
>>> ext_default_check_on(MyExtType())
123
>>> ext_default_check_on(None)
Traceback (most recent call last):
TypeError: Argument 'x' has incorrect type (expected ext_type_none_arg.MyExtType, got NoneType)
"""
return
attr
(
x
)
# with 'or/not None'
def
ext_or_none
(
MyExtType
x
or
None
):
"""
>>> ext_or_none(MyExtType())
123
>>> ext_or_none(None)
321
"""
return
attr
(
x
)
def
ext_not_none
(
MyExtType
x
not
None
):
"""
>>> ext_not_none(MyExtType())
123
>>> ext_not_none(None)
Traceback (most recent call last):
TypeError: Argument 'x' has incorrect type (expected ext_type_none_arg.MyExtType, got NoneType)
"""
return
attr
(
x
)
### builtin types (using list)
cdef
litem
(
list
L
,
int
item
):
return
L
is
None
and
321
or
L
[
item
]
# defaults, without 'not/or None'
def
builtin_default
(
list
L
):
"""
>>> builtin_default([123])
123
>>> builtin_default(None)
321
"""
return
litem
(
L
,
0
)
@
cython
.
allow_none_for_extension_args
(
True
)
def
builtin_default_check_off
(
list
L
):
"""
>>> builtin_default_check_off([123])
123
>>> builtin_default_check_off(None)
321
"""
return
litem
(
L
,
0
)
@
cython
.
allow_none_for_extension_args
(
False
)
def
builtin_default_check_on
(
list
L
):
"""
>>> builtin_default_check_on([123])
123
>>> builtin_default_check_on(None)
Traceback (most recent call last):
TypeError: Argument 'L' has incorrect type (expected list, got NoneType)
"""
return
litem
(
L
,
0
)
# with 'or/not None'
def
builtin_or_none
(
list
L
or
None
):
"""
>>> builtin_or_none([123])
123
>>> builtin_or_none(None)
321
"""
return
litem
(
L
,
0
)
def
builtin_not_none
(
list
L
not
None
):
"""
>>> builtin_not_none([123])
123
>>> builtin_not_none(None)
Traceback (most recent call last):
TypeError: Argument 'L' has incorrect type (expected list, got NoneType)
"""
return
litem
(
L
,
0
)
## builtin type 'object' (which includes None!) - currently unclear!
## def object_or_none(object o or None):
## """
## >>> object_or_none([])
## list
## >>> object_or_none(None)
## NoneType
## """
## return type(o).__name__
## def object_not_none(object o not None):
## """
## >>> object_not_none([123])
## 123
## >>> object_not_none(None)
## Traceback (most recent call last):
## TypeError: Argument 'o' has incorrect type (expected object, got NoneType)
## """
## return type(o).__name__
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