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
5117b05e
Commit
5117b05e
authored
Oct 01, 2009
by
Dag Sverre Seljebotn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
doctesthack directive works for global functions
parent
d12a8228
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
163 additions
and
2 deletions
+163
-2
Cython/Compiler/AnalysedTreeTransforms.py
Cython/Compiler/AnalysedTreeTransforms.py
+90
-0
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+2
-2
Cython/Compiler/Main.py
Cython/Compiler/Main.py
+2
-0
Cython/Compiler/Visitor.py
Cython/Compiler/Visitor.py
+3
-0
tests/run/doctesthack.pyx
tests/run/doctesthack.pyx
+66
-0
No files found.
Cython/Compiler/AnalysedTreeTransforms.py
0 → 100644
View file @
5117b05e
from
Cython.Compiler.Visitor
import
VisitorTransform
,
CythonTransform
,
TreeVisitor
from
Nodes
import
StatListNode
,
SingleAssignmentNode
from
ExprNodes
import
(
DictNode
,
DictItemNode
,
NameNode
,
UnicodeNode
,
NoneNode
,
ExprNode
,
AttributeNode
)
from
PyrexTypes
import
py_object_type
from
Builtin
import
dict_type
from
StringEncoding
import
EncodedString
import
Naming
class
DoctestHackTransform
(
CythonTransform
):
# Handles doctesthack directive
def
visit_ModuleNode
(
self
,
node
):
if
self
.
current_directives
[
'doctesthack'
]:
assert
isinstance
(
node
.
body
,
StatListNode
)
pos
=
node
.
pos
self
.
tests
=
[]
self
.
testspos
=
node
.
pos
test_dict_entry
=
node
.
scope
.
declare_var
(
EncodedString
(
u'__test__'
),
py_object_type
,
pos
,
visibility
=
'public'
)
create_test_dict_assignment
=
SingleAssignmentNode
(
pos
,
lhs
=
NameNode
(
pos
,
name
=
EncodedString
(
u'__test__'
),
entry
=
test_dict_entry
),
rhs
=
DictNode
(
pos
,
key_value_pairs
=
self
.
tests
))
self
.
visitchildren
(
node
)
node
.
body
.
stats
.
append
(
create_test_dict_assignment
)
return
node
def
add_test
(
self
,
testpos
,
name
,
funcname
):
pos
=
self
.
testspos
keystr
=
u'%s (line %d)'
%
(
name
,
testpos
[
1
])
key
=
UnicodeNode
(
pos
,
value
=
EncodedString
(
keystr
))
getfunc
=
AttributeNode
(
pos
,
obj
=
ModuleRefNode
(
pos
),
attribute
=
funcname
,
type
=
py_object_type
,
is_py_attr
=
True
,
is_temp
=
True
)
value
=
DocstringRefNode
(
pos
,
getfunc
)
self
.
tests
.
append
(
DictItemNode
(
pos
,
key
=
key
,
value
=
value
))
def
visit_ClassDefNode
(
self
,
node
):
return
node
def
visit_FuncDefNode
(
self
,
node
):
if
node
.
doc
:
self
.
add_test
(
node
.
pos
,
node
.
entry
.
name
,
node
.
entry
.
name
)
return
node
class
ModuleRefNode
(
ExprNode
):
type
=
py_object_type
is_temp
=
False
subexprs
=
[]
def
analyse_types
(
self
,
env
):
pass
def
calculate_result_code
(
self
):
return
Naming
.
module_cname
def
generate_result_code
(
self
,
code
):
pass
class
DocstringRefNode
(
ExprNode
):
# Extracts the docstring of the body element
subexprs
=
[
'body'
]
type
=
py_object_type
is_temp
=
True
def
__init__
(
self
,
pos
,
body
):
ExprNode
.
__init__
(
self
,
pos
)
assert
body
.
type
.
is_pyobject
self
.
body
=
body
def
analyse_types
(
self
,
env
):
pass
def
generate_result_code
(
self
,
code
):
code
.
putln
(
'%s = __Pyx_GetAttrString(%s, "__doc__");'
%
(
self
.
result
(),
self
.
body
.
result
()))
code
.
put_gotref
(
self
.
result
())
Cython/Compiler/ExprNodes.py
View file @
5117b05e
...
...
@@ -3501,6 +3501,8 @@ class DictNode(ExprNode):
# obj_conversion_errors [PyrexError] used internally
subexprs
=
[
'key_value_pairs'
]
is_temp
=
1
type
=
dict_type
def
calculate_constant_result
(
self
):
self
.
constant_result
=
dict
([
...
...
@@ -3516,12 +3518,10 @@ class DictNode(ExprNode):
def
analyse_types
(
self
,
env
):
hold_errors
()
self
.
type
=
dict_type
for
item
in
self
.
key_value_pairs
:
item
.
analyse_types
(
env
)
self
.
obj_conversion_errors
=
held_errors
()
release_errors
(
ignore
=
True
)
self
.
is_temp
=
1
def
coerce_to
(
self
,
dst_type
,
env
):
if
dst_type
.
is_pyobject
:
...
...
Cython/Compiler/Main.py
View file @
5117b05e
...
...
@@ -88,6 +88,7 @@ class Context(object):
from
ParseTreeTransforms
import
CreateClosureClasses
,
MarkClosureVisitor
,
DecoratorTransform
from
ParseTreeTransforms
import
InterpretCompilerDirectives
,
TransformBuiltinMethods
from
ParseTreeTransforms
import
AlignFunctionDefinitions
,
GilCheck
from
AnalysedTreeTransforms
import
DoctestHackTransform
from
AutoDocTransforms
import
EmbedSignature
from
Optimize
import
FlattenInListTransform
,
SwitchTransform
,
IterationTransform
from
Optimize
import
OptimizeBuiltinCalls
,
ConstantFolding
,
FinalOptimizePhase
...
...
@@ -126,6 +127,7 @@ class Context(object):
WithTransform
(
self
),
DecoratorTransform
(
self
),
AnalyseDeclarationsTransform
(
self
),
DoctestHackTransform
(
self
),
EmbedSignature
(
self
),
TransformBuiltinMethods
(
self
),
IntroduceBufferAuxiliaryVars
(
self
),
...
...
Cython/Compiler/Visitor.py
View file @
5117b05e
...
...
@@ -250,6 +250,9 @@ class VisitorTransform(TreeVisitor):
class
CythonTransform
(
VisitorTransform
):
"""
Certain common conventions and utilitues for Cython transforms.
- Sets up the context of the pipeline in self.context
- Tracks directives in effect in self.current_directives
"""
def
__init__
(
self
,
context
):
super
(
CythonTransform
,
self
).
__init__
()
...
...
tests/run/doctesthack.pyx
0 → 100644
View file @
5117b05e
#cython: doctesthack=True
"""
Tests doctesthack compiler directive.
The doctests are actually run as part of this test;
which makes the test flow a bit untraditional. Both
module test and individual tests are run; finally,
all_tests_run() is executed which does final validation.
>>> items = __test__.items()
>>> items.sort()
>>> for key, value in items:
... print key, ';', value
mycpdeffunc (line 40) ; >>> add_log("cpdef")
myfunc (line 34) ; >>> add_log("def")
"""
log
=
[]
#__test__ = {'a':'445', 'b':'3'}
def
all_tests_run
():
log
.
sort
()
assert
log
==
[
u'cpdef'
,
u'def'
],
log
def
add_log
(
s
):
log
.
append
(
unicode
(
s
))
if
len
(
log
)
==
len
(
__test__
):
# Final per-function doctest executed
all_tests_run
()
def
myfunc
():
""">>> add_log("def")"""
def
nodocstring
():
pass
cpdef
mycpdeffunc
():
""">>> add_log("cpdef")"""
class
MyClass
:
"""
Needs no hack
>>> True
True
"""
def
method
(
self
):
"""
>>> True
False
"""
## cdef class MyCdefClass:
## """
## >>> add_log("cdef class")
## """
## def method(self):
## """
## >>> add_log("cdef class method")
## """
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