Commit 52035b92 authored by Stefan Behnel's avatar Stefan Behnel

Clean up default value handling of memory view type.

parent 2b03bda0
...@@ -2047,8 +2047,7 @@ class CCodeWriter(object): ...@@ -2047,8 +2047,7 @@ class CCodeWriter(object):
if type.is_pyobject: if type.is_pyobject:
self.putln("%s = NULL;" % decl) self.putln("%s = NULL;" % decl)
elif type.is_memoryviewslice: elif type.is_memoryviewslice:
from . import MemoryView self.putln("%s = %s;" % (decl, type.literal_code(type.default_value)))
self.putln("%s = %s;" % (decl, MemoryView.memslice_entry_init))
else: else:
self.putln("%s%s;" % (static and "static " or "", decl)) self.putln("%s%s;" % (static and "static " or "", decl))
......
...@@ -808,7 +808,7 @@ context = { ...@@ -808,7 +808,7 @@ context = {
'memview_struct_name': memview_objstruct_cname, 'memview_struct_name': memview_objstruct_cname,
'max_dims': Options.buffer_max_dims, 'max_dims': Options.buffer_max_dims,
'memviewslice_name': memviewslice_cname, 'memviewslice_name': memviewslice_cname,
'memslice_init': memslice_entry_init, 'memslice_init': PyrexTypes.MemoryViewSliceType.default_value,
} }
memviewslice_declare_code = load_memview_c_utility( memviewslice_declare_code = load_memview_c_utility(
"MemviewSliceStruct", "MemviewSliceStruct",
......
...@@ -1734,9 +1734,6 @@ class FuncDefNode(StatNode, BlockNode): ...@@ -1734,9 +1734,6 @@ class FuncDefNode(StatNode, BlockNode):
# no code to generate # no code to generate
return return
if self.return_type.is_memoryviewslice:
from . import MemoryView
lenv = self.local_scope lenv = self.local_scope
if lenv.is_closure_scope and not lenv.is_passthrough: if lenv.is_closure_scope and not lenv.is_passthrough:
outer_scope_cname = "%s->%s" % (Naming.cur_scope_cname, outer_scope_cname = "%s->%s" % (Naming.cur_scope_cname,
...@@ -1810,14 +1807,15 @@ class FuncDefNode(StatNode, BlockNode): ...@@ -1810,14 +1807,15 @@ class FuncDefNode(StatNode, BlockNode):
# Initialize the return variable __pyx_r # Initialize the return variable __pyx_r
init = "" init = ""
if not self.return_type.is_void: return_type = self.return_type
if self.return_type.is_pyobject: if not return_type.is_void:
if return_type.is_pyobject:
init = " = NULL" init = " = NULL"
elif self.return_type.is_memoryviewslice: elif return_type.is_memoryviewslice:
init = ' = ' + MemoryView.memslice_entry_init init = ' = ' + return_type.literal_code(return_type.default_value)
code.putln("%s%s;" % ( code.putln("%s%s;" % (
self.return_type.declaration_code(Naming.retval_cname), return_type.declaration_code(Naming.retval_cname),
init)) init))
tempvardecl_code = code.insertion_point() tempvardecl_code = code.insertion_point()
...@@ -1977,18 +1975,20 @@ class FuncDefNode(StatNode, BlockNode): ...@@ -1977,18 +1975,20 @@ class FuncDefNode(StatNode, BlockNode):
code.putln("/* function exit code */") code.putln("/* function exit code */")
# ----- Default return value # ----- Default return value
return_type = self.return_type
if not self.body.is_terminator: if not self.body.is_terminator:
if self.return_type.is_pyobject: if return_type.is_pyobject:
#if self.return_type.is_extension_type: #if return_type.is_extension_type:
# lhs = "(PyObject *)%s" % Naming.retval_cname # lhs = "(PyObject *)%s" % Naming.retval_cname
#else: #else:
lhs = Naming.retval_cname lhs = Naming.retval_cname
code.put_init_to_py_none(lhs, self.return_type) code.put_init_to_py_none(lhs, return_type)
else: elif not return_type.is_memoryviewslice:
val = self.return_type.default_value # memory view structs receive their default value on initialisation
val = return_type.default_value
if val: if val:
code.putln("%s = %s;" % (Naming.retval_cname, val)) code.putln("%s = %s;" % (Naming.retval_cname, val))
elif not self.return_type.is_void: elif not return_type.is_void:
code.putln("__Pyx_pretend_to_initialize(&%s);" % Naming.retval_cname) code.putln("__Pyx_pretend_to_initialize(&%s);" % Naming.retval_cname)
# ----- Error cleanup # ----- Error cleanup
if code.error_label in code.labels_used: if code.error_label in code.labels_used:
...@@ -2013,7 +2013,8 @@ class FuncDefNode(StatNode, BlockNode): ...@@ -2013,7 +2013,8 @@ class FuncDefNode(StatNode, BlockNode):
#code.putln("%s = 0;" % entry.cname) #code.putln("%s = 0;" % entry.cname)
code.putln("__Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}") code.putln("__Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}")
if self.return_type.is_memoryviewslice: if return_type.is_memoryviewslice:
from . import MemoryView
MemoryView.put_init_entry(Naming.retval_cname, code) MemoryView.put_init_entry(Naming.retval_cname, code)
err_val = Naming.retval_cname err_val = Naming.retval_cname
else: else:
...@@ -2039,13 +2040,13 @@ class FuncDefNode(StatNode, BlockNode): ...@@ -2039,13 +2040,13 @@ class FuncDefNode(StatNode, BlockNode):
"Unraisable exception in function '%s'." % "Unraisable exception in function '%s'." %
self.entry.qualified_name, 0) self.entry.qualified_name, 0)
code.put_unraisable(self.entry.qualified_name, lenv.nogil) code.put_unraisable(self.entry.qualified_name, lenv.nogil)
default_retval = self.return_type.default_value default_retval = return_type.default_value
if err_val is None and default_retval: if err_val is None and default_retval:
err_val = default_retval err_val = default_retval
if err_val is not None: if err_val is not None:
if err_val != Naming.retval_cname: if err_val != Naming.retval_cname:
code.putln("%s = %s;" % (Naming.retval_cname, err_val)) code.putln("%s = %s;" % (Naming.retval_cname, err_val))
elif not self.return_type.is_void: elif not return_type.is_void:
code.putln("__Pyx_pretend_to_initialize(&%s);" % Naming.retval_cname) code.putln("__Pyx_pretend_to_initialize(&%s);" % Naming.retval_cname)
if is_getbuffer_slot: if is_getbuffer_slot:
...@@ -2054,7 +2055,7 @@ class FuncDefNode(StatNode, BlockNode): ...@@ -2054,7 +2055,7 @@ class FuncDefNode(StatNode, BlockNode):
# If we are using the non-error cleanup section we should # If we are using the non-error cleanup section we should
# jump past it if we have an error. The if-test below determine # jump past it if we have an error. The if-test below determine
# whether this section is used. # whether this section is used.
if buffers_present or is_getbuffer_slot or self.return_type.is_memoryviewslice: if buffers_present or is_getbuffer_slot or return_type.is_memoryviewslice:
code.put_goto(code.return_from_error_cleanup_label) code.put_goto(code.return_from_error_cleanup_label)
# ----- Non-error return cleanup # ----- Non-error return cleanup
...@@ -2064,11 +2065,11 @@ class FuncDefNode(StatNode, BlockNode): ...@@ -2064,11 +2065,11 @@ class FuncDefNode(StatNode, BlockNode):
if is_getbuffer_slot: if is_getbuffer_slot:
self.getbuffer_normal_cleanup(code) self.getbuffer_normal_cleanup(code)
if self.return_type.is_memoryviewslice: if return_type.is_memoryviewslice:
# See if our return value is uninitialized on non-error return # See if our return value is uninitialized on non-error return
# from . import MemoryView # from . import MemoryView
# MemoryView.err_if_nogil_initialized_check(self.pos, env) # MemoryView.err_if_nogil_initialized_check(self.pos, env)
cond = code.unlikely(self.return_type.error_condition(Naming.retval_cname)) cond = code.unlikely(return_type.error_condition(Naming.retval_cname))
code.putln( code.putln(
'if (%s) {' % cond) 'if (%s) {' % cond)
if env.nogil: if env.nogil:
...@@ -2114,11 +2115,11 @@ class FuncDefNode(StatNode, BlockNode): ...@@ -2114,11 +2115,11 @@ class FuncDefNode(StatNode, BlockNode):
# ----- Return # ----- Return
# This code is duplicated in ModuleNode.generate_module_init_func # This code is duplicated in ModuleNode.generate_module_init_func
if not lenv.nogil: if not lenv.nogil:
default_retval = self.return_type.default_value default_retval = return_type.default_value
err_val = self.error_value() err_val = self.error_value()
if err_val is None and default_retval: if err_val is None and default_retval:
err_val = default_retval # FIXME: why is err_val not used? err_val = default_retval # FIXME: why is err_val not used?
code.put_xgiveref(Naming.retval_cname, self.return_type) code.put_xgiveref(Naming.retval_cname, return_type)
if self.entry.is_special and self.entry.name == "__hash__": if self.entry.is_special and self.entry.name == "__hash__":
# Returning -1 for __hash__ is supposed to signal an error # Returning -1 for __hash__ is supposed to signal an error
...@@ -2130,7 +2131,7 @@ class FuncDefNode(StatNode, BlockNode): ...@@ -2130,7 +2131,7 @@ class FuncDefNode(StatNode, BlockNode):
code.funcstate.can_trace = False code.funcstate.can_trace = False
if not self.is_generator: if not self.is_generator:
# generators are traced when iterated, not at creation # generators are traced when iterated, not at creation
if self.return_type.is_pyobject: if return_type.is_pyobject:
code.put_trace_return( code.put_trace_return(
Naming.retval_cname, nogil=not code.funcstate.gil_owned) Naming.retval_cname, nogil=not code.funcstate.gil_owned)
else: else:
...@@ -2146,7 +2147,7 @@ class FuncDefNode(StatNode, BlockNode): ...@@ -2146,7 +2147,7 @@ class FuncDefNode(StatNode, BlockNode):
code.put_release_ensured_gil() code.put_release_ensured_gil()
code.funcstate.gil_owned = False code.funcstate.gil_owned = False
if not self.return_type.is_void: if not return_type.is_void:
code.putln("return %s;" % Naming.retval_cname) code.putln("return %s;" % Naming.retval_cname)
code.putln("}") code.putln("}")
......
...@@ -593,6 +593,7 @@ class CTypedefType(BaseType): ...@@ -593,6 +593,7 @@ class CTypedefType(BaseType):
class MemoryViewSliceType(PyrexType): class MemoryViewSliceType(PyrexType):
is_memoryviewslice = 1 is_memoryviewslice = 1
default_value = "{ 0, 0, { 0 }, { 0 }, { 0 } }"
has_attributes = 1 has_attributes = 1
needs_refcounting = 1 # Ideally this would be true and reference counting for needs_refcounting = 1 # Ideally this would be true and reference counting for
......
...@@ -515,8 +515,7 @@ class Scope(object): ...@@ -515,8 +515,7 @@ class Scope(object):
entries[name] = entry entries[name] = entry
if type.is_memoryviewslice: if type.is_memoryviewslice:
from . import MemoryView entry.init = type.default_value
entry.init = MemoryView.memslice_entry_init
entry.scope = self entry.scope = self
entry.visibility = visibility entry.visibility = visibility
......
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