Commit 422a77f8 authored by Peter Alexander's avatar Peter Alexander

naming references changed from 'option' to 'directive'

parent 49ef0f6b
...@@ -126,7 +126,7 @@ def parse_command_line(args): ...@@ -126,7 +126,7 @@ def parse_command_line(args):
options.emit_linenums = True options.emit_linenums = True
elif option in ("-X", "--directive"): elif option in ("-X", "--directive"):
try: try:
options.compiler_directives = Options.parse_option_list(pop_arg()) options.compiler_directives = Options.parse_directive_list(pop_arg())
except ValueError, e: except ValueError, e:
sys.stderr.write("Error in compiler directive: %s\n" % e.message) sys.stderr.write("Error in compiler directive: %s\n" % e.message)
sys.exit(1) sys.exit(1)
......
...@@ -55,7 +55,7 @@ embed = False ...@@ -55,7 +55,7 @@ embed = False
# Declare compiler directives # Declare compiler directives
option_defaults = { directive_defaults = {
'boundscheck' : True, 'boundscheck' : True,
'nonecheck' : False, 'nonecheck' : False,
'embedsignature' : False, 'embedsignature' : False,
...@@ -77,35 +77,35 @@ option_defaults = { ...@@ -77,35 +77,35 @@ option_defaults = {
} }
# Override types possibilities above, if needed # Override types possibilities above, if needed
option_types = {} directive_types = {}
for key, val in option_defaults.items(): for key, val in directive_defaults.items():
if key not in option_types: if key not in directive_types:
option_types[key] = type(val) directive_types[key] = type(val)
option_scopes = { # defaults to available everywhere directive_scopes = { # defaults to available everywhere
# 'module', 'function', 'class', 'with statement' # 'module', 'function', 'class', 'with statement'
'autotestdict' : ('module',), 'autotestdict' : ('module',),
'test_assert_path_exists' : ('function',), 'test_assert_path_exists' : ('function',),
'test_fail_if_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 Parses value as an option value for the given name and returns
the interpreted value. None is returned if the option does not exist. 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 None
>>> parse_option_value('boundscheck', 'True') >>> parse_directive_value('boundscheck', 'True')
True True
>>> parse_option_value('boundscheck', 'true') >>> parse_directive_value('boundscheck', 'true')
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: boundscheck directive must be set to True or False 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 not type: return None
if type is bool: if type is bool:
if value == "True": return True if value == "True": return True
...@@ -119,25 +119,25 @@ def parse_option_value(name, value): ...@@ -119,25 +119,25 @@ def parse_option_value(name, value):
else: else:
assert False assert False
def parse_option_list(s): def parse_directive_list(s):
""" """
Parses a comma-seperated list of pragma options. Whitespace Parses a comma-seperated list of pragma options. Whitespace
is not considered. is not considered.
>>> parse_option_list(' ') >>> parse_directive_list(' ')
{} {}
>>> (parse_option_list('boundscheck=True') == >>> (parse_directive_list('boundscheck=True') ==
... {'boundscheck': True}) ... {'boundscheck': True})
True True
>>> parse_option_list(' asdf') >>> parse_directive_list(' asdf')
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: Expected "=" in option "asdf" ValueError: Expected "=" in option "asdf"
>>> parse_option_list('boundscheck=hey') >>> parse_directive_list('boundscheck=hey')
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: Must pass a boolean value for option "boundscheck" ValueError: Must pass a boolean value for option "boundscheck"
>>> parse_option_list('unknown=True') >>> parse_directive_list('unknown=True')
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: Unknown option: "unknown" ValueError: Unknown option: "unknown"
...@@ -149,7 +149,7 @@ def parse_option_list(s): ...@@ -149,7 +149,7 @@ def parse_option_list(s):
if not '=' in item: raise ValueError('Expected "=" in option "%s"' % item) if not '=' in item: raise ValueError('Expected "=" in option "%s"' % item)
name, value = item.strip().split('=') name, value = item.strip().split('=')
try: try:
type = option_types[name] type = directive_types[name]
except KeyError: except KeyError:
raise ValueError('Unknown option: "%s"' % name) raise ValueError('Unknown option: "%s"' % name)
if type is bool: if type is bool:
......
...@@ -153,10 +153,10 @@ class PostParse(CythonTransform): ...@@ -153,10 +153,10 @@ class PostParse(CythonTransform):
- For __cythonbufferdefaults__ the arguments are checked for - For __cythonbufferdefaults__ the arguments are checked for
validity. validity.
CBufferAccessTypeNode has its options interpreted: CBufferAccessTypeNode has its directives interpreted:
Any first positional argument goes into the "dtype" attribute, Any first positional argument goes into the "dtype" attribute,
any "ndim" keyword argument goes into the "ndim" attribute and 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 - __cythonbufferdefaults__ attributes are parsed and put into the
type information. type information.
...@@ -304,14 +304,14 @@ class PxdPostParse(CythonTransform, SkipDeclarations): ...@@ -304,14 +304,14 @@ class PxdPostParse(CythonTransform, SkipDeclarations):
class InterpretCompilerDirectives(CythonTransform, SkipDeclarations): class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
""" """
After parsing, options can be stored in a number of places: After parsing, directives can be stored in a number of places:
- #cython-comments at the top of the file (stored in ModuleNode) - #cython-comments at the top of the file (stored in ModuleNode)
- Command-line arguments overriding these - Command-line arguments overriding these
- @cython.optionname decorators - @cython.directivename decorators
- with cython.optionname: statements - with cython.directivename: statements
This transform is responsible for interpreting these various sources 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. - Set the directives attribute of the ModuleNode for global directives.
- Use a CompilerDirectivesNode to override directives for a subtree. - Use a CompilerDirectivesNode to override directives for a subtree.
...@@ -330,16 +330,16 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations): ...@@ -330,16 +330,16 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
""" """
special_methods = set(['declare', 'union', 'struct', 'typedef', 'sizeof', 'typeof', 'cast', 'address', 'pointer', 'compiled', 'NULL']) 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) super(InterpretCompilerDirectives, self).__init__(context)
self.compilation_option_overrides = {} self.compilation_directive_overrides = {}
for key, value in compilation_option_overrides.iteritems(): for key, value in compilation_directive_overrides.iteritems():
self.compilation_option_overrides[unicode(key)] = value self.compilation_directive_overrides[unicode(key)] = value
self.cython_module_names = set() self.cython_module_names = set()
self.option_names = {} self.directive_names = {}
def check_directive_scope(self, pos, directive, scope): 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: if legal_scopes and scope not in legal_scopes:
self.context.nonfatal_error(PostParseError(pos, 'The %s compiler directive ' self.context.nonfatal_error(PostParseError(pos, 'The %s compiler directive '
'is not allowed in %s scope' % (directive, scope))) 'is not allowed in %s scope' % (directive, scope)))
...@@ -349,19 +349,19 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations): ...@@ -349,19 +349,19 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
# Set up processing and handle the cython: comments. # Set up processing and handle the cython: comments.
def visit_ModuleNode(self, node): def visit_ModuleNode(self, node):
options = copy.copy(Options.option_defaults) directives = copy.copy(Options.directive_defaults)
for key, value in self.compilation_option_overrides.iteritems(): for key, value in self.compilation_directive_overrides.iteritems():
if not self.check_directive_scope(node.pos, key, 'module'): if not self.check_directive_scope(node.pos, key, 'module'):
self.wrong_scope_error(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 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 " 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) "in Cython 0.12. See http://article.gmane.org/gmane.comp.python.cython.devel/5233", 2)
options.update(node.option_comments) directives.update(node.directive_comments)
options.update(self.compilation_option_overrides) directives.update(self.compilation_directive_overrides)
self.options = options self.directives = directives
node.directives = options node.directives = directives
self.visitchildren(node) self.visitchildren(node)
node.cython_module_names = self.cython_module_names node.cython_module_names = self.cython_module_names
return node return node
...@@ -380,15 +380,15 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations): ...@@ -380,15 +380,15 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
if node.module_name == u"cython": if node.module_name == u"cython":
newimp = [] newimp = []
for pos, name, as_name, kind in node.imported_names: 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 name in self.special_methods or
PyrexTypes.parse_basic_type(name)): PyrexTypes.parse_basic_type(name)):
if as_name is None: if as_name is None:
as_name = name as_name = name
self.option_names[as_name] = name self.directive_names[as_name] = name
if kind is not None: if kind is not None:
self.context.nonfatal_error(PostParseError(pos, self.context.nonfatal_error(PostParseError(pos,
"Compiler option imports must be plain imports")) "Compiler directive imports must be plain imports"))
else: else:
newimp.append((pos, name, as_name, kind)) newimp.append((pos, name, as_name, kind))
if not newimp: if not newimp:
...@@ -400,10 +400,10 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations): ...@@ -400,10 +400,10 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
if node.module.module_name.value == u"cython": if node.module.module_name.value == u"cython":
newimp = [] newimp = []
for name, name_node in node.items: 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 name in self.special_methods or
PyrexTypes.parse_basic_type(name)): PyrexTypes.parse_basic_type(name)):
self.option_names[name_node.name] = name self.directive_names[name_node.name] = name
else: else:
newimp.append((name, name_node)) newimp.append((name, name_node))
if not newimp: if not newimp:
...@@ -426,12 +426,12 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations): ...@@ -426,12 +426,12 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
if node.name in self.cython_module_names: if node.name in self.cython_module_names:
node.is_cython_module = True node.is_cython_module = True
else: else:
node.cython_attribute = self.option_names.get(node.name) node.cython_attribute = self.directive_names.get(node.name)
return node return node
def try_to_parse_option(self, node): def try_to_parse_directive(self, node):
# If node is the contents of an option (in a with statement or # If node is the contents of an directive (in a with statement or
# decorator), returns (optionname, value). # decorator), returns (directivename, value).
# Otherwise, returns None # Otherwise, returns None
optname = None optname = None
if isinstance(node, CallNode): if isinstance(node, CallNode):
...@@ -439,56 +439,56 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations): ...@@ -439,56 +439,56 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
optname = node.function.as_cython_attribute() optname = node.function.as_cython_attribute()
if optname: if optname:
optiontype = Options.option_types.get(optname) directivetype = Options.directive_types.get(optname)
if optiontype: if directivetype:
args, kwds = node.explicit_args_kwds() args, kwds = node.explicit_args_kwds()
if optiontype is bool: if directivetype is bool:
if kwds is not None or len(args) != 1 or not isinstance(args[0], BoolNode): if kwds is not None or len(args) != 1 or not isinstance(args[0], BoolNode):
raise PostParseError(node.function.pos, 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) return (optname, args[0].value)
elif optiontype is str: elif directivetype is str:
if kwds is not None or len(args) != 1 or not isinstance(args[0], (StringNode, UnicodeNode)): if kwds is not None or len(args) != 1 or not isinstance(args[0], (StringNode, UnicodeNode)):
raise PostParseError(node.function.pos, 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)) return (optname, str(args[0].value))
elif optiontype is dict: elif directivetype is dict:
if len(args) != 0: if len(args) != 0:
raise PostParseError(node.function.pos, 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]) return optname, dict([(key.value, value) for key, value in kwds.key_value_pairs])
elif optiontype is list: elif directivetype is list:
if kwds and len(kwds) != 0: if kwds and len(kwds) != 0:
raise PostParseError(node.function.pos, 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 ] return optname, [ str(arg.value) for arg in args ]
else: else:
assert False assert False
return None return None
def visit_with_options(self, body, options): def visit_with_directives(self, body, directives):
oldoptions = self.options olddirectives = self.directives
newoptions = copy.copy(oldoptions) newdirectives = copy.copy(olddirectives)
newoptions.update(options) newdirectives.update(directives)
self.options = newoptions self.directives = newdirectives
assert isinstance(body, StatListNode), body assert isinstance(body, StatListNode), body
retbody = self.visit_Node(body) retbody = self.visit_Node(body)
directive = CompilerDirectivesNode(pos=retbody.pos, body=retbody, directive = CompilerDirectivesNode(pos=retbody.pos, body=retbody,
directives=newoptions) directives=newdirectives)
self.options = oldoptions self.directives = olddirectives
return directive return directive
# Handle decorators # Handle decorators
def visit_FuncDefNode(self, node): def visit_FuncDefNode(self, node):
options = [] directives = []
if node.decorators: if node.decorators:
# Split the decorators into two lists -- real decorators and options # Split the decorators into two lists -- real decorators and directives
realdecs = [] realdecs = []
for dec in node.decorators: for dec in node.decorators:
option = self.try_to_parse_option(dec.decorator) directive = self.try_to_parse_directive(dec.decorator)
if option is not None: if directive is not None:
options.append(option) directives.append(directive)
else: else:
realdecs.append(dec) realdecs.append(dec)
if realdecs and isinstance(node, CFuncDefNode): if realdecs and isinstance(node, CFuncDefNode):
...@@ -496,12 +496,12 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations): ...@@ -496,12 +496,12 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
else: else:
node.decorators = realdecs node.decorators = realdecs
if options: if directives:
optdict = {} optdict = {}
options.reverse() # Decorators coming first take precedence directives.reverse() # Decorators coming first take precedence
for option in options: for directive in directives:
name, value = option name, value = directive
legal_scopes = Options.option_scopes.get(name, None) legal_scopes = Options.directive_scopes.get(name, None)
if not self.check_directive_scope(node.pos, name, 'function'): if not self.check_directive_scope(node.pos, name, 'function'):
continue continue
if name in optdict: if name in optdict:
...@@ -517,16 +517,16 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations): ...@@ -517,16 +517,16 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
else: else:
optdict[name] = value optdict[name] = value
body = StatListNode(node.pos, stats=[node]) body = StatListNode(node.pos, stats=[node])
return self.visit_with_options(body, optdict) return self.visit_with_directives(body, optdict)
else: else:
return self.visit_Node(node) return self.visit_Node(node)
def visit_CVarDefNode(self, node): def visit_CVarDefNode(self, node):
if node.decorators: if node.decorators:
for dec in node.decorators: for dec in node.decorators:
option = self.try_to_parse_option(dec.decorator) directive = self.try_to_parse_directive(dec.decorator)
if option is not None and option[0] == u'locals': if directive is not None and directive[0] == u'locals':
node.directive_locals = option[1] node.directive_locals = directive[1]
else: else:
self.context.nonfatal_error(PostParseError(dec.pos, self.context.nonfatal_error(PostParseError(dec.pos,
"Cdef functions can only take cython.locals() decorator.")) "Cdef functions can only take cython.locals() decorator."))
...@@ -535,15 +535,15 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations): ...@@ -535,15 +535,15 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
# Handle with statements # Handle with statements
def visit_WithStatNode(self, node): def visit_WithStatNode(self, node):
option = self.try_to_parse_option(node.manager) directive = self.try_to_parse_directive(node.manager)
if option is not None: if directive is not None:
if node.target is not None: if node.target is not None:
self.context.nonfatal_error( 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: else:
name, value = option name, value = directive
if self.check_directive_scope(node.pos, name, 'with statement'): if self.check_directive_scope(node.pos, name, 'with statement'):
return self.visit_with_options(node.body, {name:value}) return self.visit_with_directives(node.body, {name:value})
return self.visit_Node(node) return self.visit_Node(node)
class WithTransform(CythonTransform, SkipDeclarations): class WithTransform(CythonTransform, SkipDeclarations):
......
...@@ -2582,7 +2582,7 @@ def p_compiler_directive_comments(s): ...@@ -2582,7 +2582,7 @@ def p_compiler_directive_comments(s):
if m: if m:
name = m.group(1) name = m.group(1)
try: 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! if value is not None: # can be False!
result[name] = value result[name] = value
except ValueError, e: except ValueError, e:
...@@ -2593,7 +2593,7 @@ def p_compiler_directive_comments(s): ...@@ -2593,7 +2593,7 @@ def p_compiler_directive_comments(s):
def p_module(s, pxd, full_module_name): def p_module(s, pxd, full_module_name):
pos = s.position() pos = s.position()
option_comments = p_compiler_directive_comments(s) directive_comments = p_compiler_directive_comments(s)
s.parse_comments = False s.parse_comments = False
doc = p_doc_string(s) doc = p_doc_string(s)
...@@ -2608,7 +2608,7 @@ def p_module(s, pxd, full_module_name): ...@@ -2608,7 +2608,7 @@ def p_module(s, pxd, full_module_name):
repr(s.sy), repr(s.systring))) repr(s.sy), repr(s.systring)))
return ModuleNode(pos, doc = doc, body = body, return ModuleNode(pos, doc = doc, body = body,
full_module_name = full_module_name, full_module_name = full_module_name,
option_comments = option_comments) directive_comments = directive_comments)
#---------------------------------------------- #----------------------------------------------
# #
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment