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
422a77f8
Commit
422a77f8
authored
Oct 15, 2009
by
Peter Alexander
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
naming references changed from 'option' to 'directive'
parent
49ef0f6b
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
87 additions
and
87 deletions
+87
-87
Cython/Compiler/CmdLine.py
Cython/Compiler/CmdLine.py
+1
-1
Cython/Compiler/Options.py
Cython/Compiler/Options.py
+18
-18
Cython/Compiler/ParseTreeTransforms.py
Cython/Compiler/ParseTreeTransforms.py
+65
-65
Cython/Compiler/Parsing.py
Cython/Compiler/Parsing.py
+3
-3
No files found.
Cython/Compiler/CmdLine.py
View file @
422a77f8
...
...
@@ -126,7 +126,7 @@ def parse_command_line(args):
options
.
emit_linenums
=
True
elif
option
in
(
"-X"
,
"--directive"
):
try
:
options
.
compiler_directives
=
Options
.
parse_
option
_list
(
pop_arg
())
options
.
compiler_directives
=
Options
.
parse_
directive
_list
(
pop_arg
())
except
ValueError
,
e
:
sys
.
stderr
.
write
(
"Error in compiler directive: %s
\
n
"
%
e
.
message
)
sys
.
exit
(
1
)
...
...
Cython/Compiler/Options.py
View file @
422a77f8
...
...
@@ -55,7 +55,7 @@ embed = False
# Declare compiler directives
option
_defaults
=
{
directive
_defaults
=
{
'boundscheck'
:
True
,
'nonecheck'
:
False
,
'embedsignature'
:
False
,
...
...
@@ -77,35 +77,35 @@ option_defaults = {
}
# Override types possibilities above, if needed
option
_types
=
{}
directive
_types
=
{}
for
key
,
val
in
option
_defaults
.
items
():
if
key
not
in
option
_types
:
option
_types
[
key
]
=
type
(
val
)
for
key
,
val
in
directive
_defaults
.
items
():
if
key
not
in
directive
_types
:
directive
_types
[
key
]
=
type
(
val
)
option
_scopes
=
{
# defaults to available everywhere
directive
_scopes
=
{
# defaults to available everywhere
# 'module', 'function', 'class', 'with statement'
'autotestdict'
:
(
'module'
,),
'test_assert_path_exists'
:
(
'function'
,),
'test_fail_if_path_exists'
:
(
'function'
,),
}
def
parse_
option
_value
(
name
,
value
):
def
parse_
directive
_value
(
name
,
value
):
"""
Parses value as an option value for the given name and returns
the interpreted value. None is returned if the option does not exist.
>>> print parse_
option
_value('nonexisting', 'asdf asdfd')
>>> print parse_
directive
_value('nonexisting', 'asdf asdfd')
None
>>> parse_
option
_value('boundscheck', 'True')
>>> parse_
directive
_value('boundscheck', 'True')
True
>>> parse_
option
_value('boundscheck', 'true')
>>> parse_
directive
_value('boundscheck', 'true')
Traceback (most recent call last):
...
ValueError: boundscheck directive must be set to True or False
"""
type
=
option
_types
.
get
(
name
)
type
=
directive
_types
.
get
(
name
)
if
not
type
:
return
None
if
type
is
bool
:
if
value
==
"True"
:
return
True
...
...
@@ -119,25 +119,25 @@ def parse_option_value(name, value):
else
:
assert
False
def
parse_
option
_list
(
s
):
def
parse_
directive
_list
(
s
):
"""
Parses a comma-seperated list of pragma options. Whitespace
is not considered.
>>> parse_
option
_list(' ')
>>> parse_
directive
_list(' ')
{}
>>> (parse_
option
_list('boundscheck=True') ==
>>> (parse_
directive
_list('boundscheck=True') ==
... {'boundscheck': True})
True
>>> parse_
option
_list(' asdf')
>>> parse_
directive
_list(' asdf')
Traceback (most recent call last):
...
ValueError: Expected "=" in option "asdf"
>>> parse_
option
_list('boundscheck=hey')
>>> parse_
directive
_list('boundscheck=hey')
Traceback (most recent call last):
...
ValueError: Must pass a boolean value for option "boundscheck"
>>> parse_
option
_list('unknown=True')
>>> parse_
directive
_list('unknown=True')
Traceback (most recent call last):
...
ValueError: Unknown option: "unknown"
...
...
@@ -149,7 +149,7 @@ def parse_option_list(s):
if
not
'='
in
item
:
raise
ValueError
(
'Expected "=" in option "%s"'
%
item
)
name
,
value
=
item
.
strip
().
split
(
'='
)
try
:
type
=
option
_types
[
name
]
type
=
directive
_types
[
name
]
except
KeyError
:
raise
ValueError
(
'Unknown option: "%s"'
%
name
)
if
type
is
bool
:
...
...
Cython/Compiler/ParseTreeTransforms.py
View file @
422a77f8
...
...
@@ -153,10 +153,10 @@ class PostParse(CythonTransform):
- For __cythonbufferdefaults__ the arguments are checked for
validity.
CBufferAccessTypeNode has its
option
s interpreted:
CBufferAccessTypeNode has its
directive
s interpreted:
Any first positional argument goes into the "dtype" attribute,
any "ndim" keyword argument goes into the "ndim" attribute and
so on. Also it is checked that the
option
combination is valid.
so on. Also it is checked that the
directive
combination is valid.
- __cythonbufferdefaults__ attributes are parsed and put into the
type information.
...
...
@@ -304,14 +304,14 @@ class PxdPostParse(CythonTransform, SkipDeclarations):
class
InterpretCompilerDirectives
(
CythonTransform
,
SkipDeclarations
):
"""
After parsing,
option
s can be stored in a number of places:
After parsing,
directive
s can be stored in a number of places:
- #cython-comments at the top of the file (stored in ModuleNode)
- Command-line arguments overriding these
- @cython.
option
name decorators
- with cython.
option
name: statements
- @cython.
directive
name decorators
- with cython.
directive
name: statements
This transform is responsible for interpreting these various sources
and store the
option
in two ways:
and store the
directive
in two ways:
- Set the directives attribute of the ModuleNode for global directives.
- Use a CompilerDirectivesNode to override directives for a subtree.
...
...
@@ -330,16 +330,16 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
"""
special_methods
=
set
([
'declare'
,
'union'
,
'struct'
,
'typedef'
,
'sizeof'
,
'typeof'
,
'cast'
,
'address'
,
'pointer'
,
'compiled'
,
'NULL'
])
def
__init__
(
self
,
context
,
compilation_
option
_overrides
):
def
__init__
(
self
,
context
,
compilation_
directive
_overrides
):
super
(
InterpretCompilerDirectives
,
self
).
__init__
(
context
)
self
.
compilation_
option
_overrides
=
{}
for
key
,
value
in
compilation_
option
_overrides
.
iteritems
():
self
.
compilation_
option
_overrides
[
unicode
(
key
)]
=
value
self
.
compilation_
directive
_overrides
=
{}
for
key
,
value
in
compilation_
directive
_overrides
.
iteritems
():
self
.
compilation_
directive
_overrides
[
unicode
(
key
)]
=
value
self
.
cython_module_names
=
set
()
self
.
option
_names
=
{}
self
.
directive
_names
=
{}
def
check_directive_scope
(
self
,
pos
,
directive
,
scope
):
legal_scopes
=
Options
.
option
_scopes
.
get
(
directive
,
None
)
legal_scopes
=
Options
.
directive
_scopes
.
get
(
directive
,
None
)
if
legal_scopes
and
scope
not
in
legal_scopes
:
self
.
context
.
nonfatal_error
(
PostParseError
(
pos
,
'The %s compiler directive '
'is not allowed in %s scope'
%
(
directive
,
scope
)))
...
...
@@ -349,19 +349,19 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
# Set up processing and handle the cython: comments.
def
visit_ModuleNode
(
self
,
node
):
options
=
copy
.
copy
(
Options
.
option
_defaults
)
for
key
,
value
in
self
.
compilation_
option
_overrides
.
iteritems
():
directives
=
copy
.
copy
(
Options
.
directive
_defaults
)
for
key
,
value
in
self
.
compilation_
directive
_overrides
.
iteritems
():
if
not
self
.
check_directive_scope
(
node
.
pos
,
key
,
'module'
):
self
.
wrong_scope_error
(
node
.
pos
,
key
,
'module'
)
del
self
.
compilation_
option
_overrides
[
key
]
del
self
.
compilation_
directive
_overrides
[
key
]
continue
if
key
in
node
.
option_comments
and
node
.
option
_comments
[
key
]
!=
value
:
if
key
in
node
.
directive_comments
and
node
.
directive
_comments
[
key
]
!=
value
:
warning
(
node
.
pos
,
"Compiler directive differs between environment and file header; this will change "
"in Cython 0.12. See http://article.gmane.org/gmane.comp.python.cython.devel/5233"
,
2
)
options
.
update
(
node
.
option
_comments
)
options
.
update
(
self
.
compilation_option
_overrides
)
self
.
options
=
option
s
node
.
directives
=
option
s
directives
.
update
(
node
.
directive
_comments
)
directives
.
update
(
self
.
compilation_directive
_overrides
)
self
.
directives
=
directive
s
node
.
directives
=
directive
s
self
.
visitchildren
(
node
)
node
.
cython_module_names
=
self
.
cython_module_names
return
node
...
...
@@ -380,15 +380,15 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
if
node
.
module_name
==
u"cython"
:
newimp
=
[]
for
pos
,
name
,
as_name
,
kind
in
node
.
imported_names
:
if
(
name
in
Options
.
option
_types
or
if
(
name
in
Options
.
directive
_types
or
name
in
self
.
special_methods
or
PyrexTypes
.
parse_basic_type
(
name
)):
if
as_name
is
None
:
as_name
=
name
self
.
option
_names
[
as_name
]
=
name
self
.
directive
_names
[
as_name
]
=
name
if
kind
is
not
None
:
self
.
context
.
nonfatal_error
(
PostParseError
(
pos
,
"Compiler
option
imports must be plain imports"
))
"Compiler
directive
imports must be plain imports"
))
else
:
newimp
.
append
((
pos
,
name
,
as_name
,
kind
))
if
not
newimp
:
...
...
@@ -400,10 +400,10 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
if
node
.
module
.
module_name
.
value
==
u"cython"
:
newimp
=
[]
for
name
,
name_node
in
node
.
items
:
if
(
name
in
Options
.
option
_types
or
if
(
name
in
Options
.
directive
_types
or
name
in
self
.
special_methods
or
PyrexTypes
.
parse_basic_type
(
name
)):
self
.
option
_names
[
name_node
.
name
]
=
name
self
.
directive
_names
[
name_node
.
name
]
=
name
else
:
newimp
.
append
((
name
,
name_node
))
if
not
newimp
:
...
...
@@ -426,12 +426,12 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
if
node
.
name
in
self
.
cython_module_names
:
node
.
is_cython_module
=
True
else
:
node
.
cython_attribute
=
self
.
option
_names
.
get
(
node
.
name
)
node
.
cython_attribute
=
self
.
directive
_names
.
get
(
node
.
name
)
return
node
def
try_to_parse_
option
(
self
,
node
):
# If node is the contents of an
option
(in a with statement or
# decorator), returns (
option
name, value).
def
try_to_parse_
directive
(
self
,
node
):
# If node is the contents of an
directive
(in a with statement or
# decorator), returns (
directive
name, value).
# Otherwise, returns None
optname
=
None
if
isinstance
(
node
,
CallNode
):
...
...
@@ -439,56 +439,56 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
optname
=
node
.
function
.
as_cython_attribute
()
if
optname
:
optiontype
=
Options
.
option
_types
.
get
(
optname
)
if
option
type
:
directivetype
=
Options
.
directive
_types
.
get
(
optname
)
if
directive
type
:
args
,
kwds
=
node
.
explicit_args_kwds
()
if
option
type
is
bool
:
if
directive
type
is
bool
:
if
kwds
is
not
None
or
len
(
args
)
!=
1
or
not
isinstance
(
args
[
0
],
BoolNode
):
raise
PostParseError
(
node
.
function
.
pos
,
'The %s
option
takes one compile-time boolean argument'
%
optname
)
'The %s
directive
takes one compile-time boolean argument'
%
optname
)
return
(
optname
,
args
[
0
].
value
)
elif
option
type
is
str
:
elif
directive
type
is
str
:
if
kwds
is
not
None
or
len
(
args
)
!=
1
or
not
isinstance
(
args
[
0
],
(
StringNode
,
UnicodeNode
)):
raise
PostParseError
(
node
.
function
.
pos
,
'The %s
option
takes one compile-time string argument'
%
optname
)
'The %s
directive
takes one compile-time string argument'
%
optname
)
return
(
optname
,
str
(
args
[
0
].
value
))
elif
option
type
is
dict
:
elif
directive
type
is
dict
:
if
len
(
args
)
!=
0
:
raise
PostParseError
(
node
.
function
.
pos
,
'The %s
option
takes no prepositional arguments'
%
optname
)
'The %s
directive
takes no prepositional arguments'
%
optname
)
return
optname
,
dict
([(
key
.
value
,
value
)
for
key
,
value
in
kwds
.
key_value_pairs
])
elif
option
type
is
list
:
elif
directive
type
is
list
:
if
kwds
and
len
(
kwds
)
!=
0
:
raise
PostParseError
(
node
.
function
.
pos
,
'The %s
option
takes no keyword arguments'
%
optname
)
'The %s
directive
takes no keyword arguments'
%
optname
)
return
optname
,
[
str
(
arg
.
value
)
for
arg
in
args
]
else
:
assert
False
return
None
def
visit_with_
options
(
self
,
body
,
option
s
):
old
options
=
self
.
option
s
new
options
=
copy
.
copy
(
oldoption
s
)
new
options
.
update
(
option
s
)
self
.
options
=
newoption
s
def
visit_with_
directives
(
self
,
body
,
directive
s
):
old
directives
=
self
.
directive
s
new
directives
=
copy
.
copy
(
olddirective
s
)
new
directives
.
update
(
directive
s
)
self
.
directives
=
newdirective
s
assert
isinstance
(
body
,
StatListNode
),
body
retbody
=
self
.
visit_Node
(
body
)
directive
=
CompilerDirectivesNode
(
pos
=
retbody
.
pos
,
body
=
retbody
,
directives
=
new
option
s
)
self
.
options
=
oldoption
s
directives
=
new
directive
s
)
self
.
directives
=
olddirective
s
return
directive
# Handle decorators
def
visit_FuncDefNode
(
self
,
node
):
option
s
=
[]
directive
s
=
[]
if
node
.
decorators
:
# Split the decorators into two lists -- real decorators and
option
s
# Split the decorators into two lists -- real decorators and
directive
s
realdecs
=
[]
for
dec
in
node
.
decorators
:
option
=
self
.
try_to_parse_option
(
dec
.
decorator
)
if
option
is
not
None
:
options
.
append
(
option
)
directive
=
self
.
try_to_parse_directive
(
dec
.
decorator
)
if
directive
is
not
None
:
directives
.
append
(
directive
)
else
:
realdecs
.
append
(
dec
)
if
realdecs
and
isinstance
(
node
,
CFuncDefNode
):
...
...
@@ -496,12 +496,12 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
else
:
node
.
decorators
=
realdecs
if
option
s
:
if
directive
s
:
optdict
=
{}
option
s
.
reverse
()
# Decorators coming first take precedence
for
option
in
option
s
:
name
,
value
=
option
legal_scopes
=
Options
.
option
_scopes
.
get
(
name
,
None
)
directive
s
.
reverse
()
# Decorators coming first take precedence
for
directive
in
directive
s
:
name
,
value
=
directive
legal_scopes
=
Options
.
directive
_scopes
.
get
(
name
,
None
)
if
not
self
.
check_directive_scope
(
node
.
pos
,
name
,
'function'
):
continue
if
name
in
optdict
:
...
...
@@ -517,16 +517,16 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
else
:
optdict
[
name
]
=
value
body
=
StatListNode
(
node
.
pos
,
stats
=
[
node
])
return
self
.
visit_with_
option
s
(
body
,
optdict
)
return
self
.
visit_with_
directive
s
(
body
,
optdict
)
else
:
return
self
.
visit_Node
(
node
)
def
visit_CVarDefNode
(
self
,
node
):
if
node
.
decorators
:
for
dec
in
node
.
decorators
:
option
=
self
.
try_to_parse_option
(
dec
.
decorator
)
if
option
is
not
None
and
option
[
0
]
==
u'locals'
:
node
.
directive_locals
=
option
[
1
]
directive
=
self
.
try_to_parse_directive
(
dec
.
decorator
)
if
directive
is
not
None
and
directive
[
0
]
==
u'locals'
:
node
.
directive_locals
=
directive
[
1
]
else
:
self
.
context
.
nonfatal_error
(
PostParseError
(
dec
.
pos
,
"Cdef functions can only take cython.locals() decorator."
))
...
...
@@ -535,15 +535,15 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
# Handle with statements
def
visit_WithStatNode
(
self
,
node
):
option
=
self
.
try_to_parse_option
(
node
.
manager
)
if
option
is
not
None
:
directive
=
self
.
try_to_parse_directive
(
node
.
manager
)
if
directive
is
not
None
:
if
node
.
target
is
not
None
:
self
.
context
.
nonfatal_error
(
PostParseError
(
node
.
pos
,
"Compiler
option
with statements cannot contain 'as'"
))
PostParseError
(
node
.
pos
,
"Compiler
directive
with statements cannot contain 'as'"
))
else
:
name
,
value
=
option
name
,
value
=
directive
if
self
.
check_directive_scope
(
node
.
pos
,
name
,
'with statement'
):
return
self
.
visit_with_
option
s
(
node
.
body
,
{
name
:
value
})
return
self
.
visit_with_
directive
s
(
node
.
body
,
{
name
:
value
})
return
self
.
visit_Node
(
node
)
class
WithTransform
(
CythonTransform
,
SkipDeclarations
):
...
...
Cython/Compiler/Parsing.py
View file @
422a77f8
...
...
@@ -2582,7 +2582,7 @@ def p_compiler_directive_comments(s):
if m:
name = m.group(1)
try:
value = Options.parse_
option
_value(str(name), str(m.group(2).strip()))
value = Options.parse_
directive
_value(str(name), str(m.group(2).strip()))
if value is not None: # can be False!
result[name] = value
except ValueError, e:
...
...
@@ -2593,7 +2593,7 @@ def p_compiler_directive_comments(s):
def p_module(s, pxd, full_module_name):
pos = s.position()
option
_comments = p_compiler_directive_comments(s)
directive
_comments = p_compiler_directive_comments(s)
s.parse_comments = False
doc = p_doc_string(s)
...
...
@@ -2608,7 +2608,7 @@ def p_module(s, pxd, full_module_name):
repr(s.sy), repr(s.systring)))
return ModuleNode(pos, doc = doc, body = body,
full_module_name = full_module_name,
option_comments = option
_comments)
directive_comments = directive
_comments)
#----------------------------------------------
#
...
...
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