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
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
34864b14
Commit
34864b14
authored
Feb 12, 2012
by
Vitja Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
CyFunction's defaults getter for non-literal default arguments
parent
fc581b12
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
76 additions
and
0 deletions
+76
-0
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+56
-0
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+5
-0
Cython/Utility/CythonFunction.c
Cython/Utility/CythonFunction.c
+15
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
34864b14
...
...
@@ -6162,6 +6162,22 @@ class PyCFunctionNode(ExprNode, ModuleNameMixin):
self
.
defaults_tuple
=
TupleNode
(
self
.
pos
,
args
=
[
arg
.
default
for
arg
in
default_args
])
self
.
defaults_tuple
.
analyse_types
(
env
)
else
:
defaults_getter
=
Nodes
.
DefNode
(
self
.
pos
,
args
=
[],
star_arg
=
None
,
starstar_arg
=
None
,
body
=
Nodes
.
ReturnStatNode
(
self
.
pos
,
return_type
=
py_object_type
,
value
=
DefaultsTupleNode
(
self
.
pos
,
default_args
,
self
.
defaults_struct
)),
decorators
=
None
,
name
=
"__defaults__"
)
defaults_getter
.
analyse_declarations
(
env
)
defaults_getter
.
analyse_expressions
(
env
)
defaults_getter
.
body
.
analyse_expressions
(
defaults_getter
.
local_scope
)
defaults_getter
.
py_wrapper_required
=
False
defaults_getter
.
pymethdef_required
=
False
self
.
def_node
.
defaults_getter
=
defaults_getter
def
may_be_none
(
self
):
return
False
...
...
@@ -6258,6 +6274,9 @@ class PyCFunctionNode(ExprNode, ModuleNameMixin):
if
self
.
defaults_tuple
:
code
.
putln
(
'__Pyx_CyFunction_SetDefaultsTuple(%s, %s);'
%
(
self
.
result
(),
self
.
defaults_tuple
.
py_result
()))
if
def_node
.
defaults_getter
:
code
.
putln
(
'__Pyx_CyFunction_SetDefaultsGetter(%s, %s);'
%
(
self
.
result
(),
def_node
.
defaults_getter
.
entry
.
pyfunc_cname
))
if
self
.
specialized_cpdefs
:
self
.
generate_fused_cpdef
(
code
,
code_object_result
,
flags
)
...
...
@@ -6389,6 +6408,43 @@ class CodeObjectNode(ExprNode):
))
class
DefaultArgNode
(
ExprNode
):
# CyFunction's non-literal argument default value
subexprs
=
[]
def
__init__
(
self
,
pos
,
arg
,
defaults_struct
):
super
(
DefaultArgNode
,
self
).
__init__
(
pos
)
self
.
arg
=
arg
self
.
defaults_struct
=
defaults_struct
def
analyse_types
(
self
,
env
):
self
.
type
=
self
.
arg
.
type
self
.
is_temp
=
False
def
generate_result_code
(
self
,
code
):
pass
def
result
(
self
):
return
'__Pyx_CyFunction_Defaults(%s, %s)->%s'
%
(
self
.
defaults_struct
.
name
,
Naming
.
self_cname
,
self
.
defaults_struct
.
lookup
(
self
.
arg
.
name
).
cname
)
class
DefaultsTupleNode
(
TupleNode
):
# CyFunction's __defaults__ tuple
def
__init__
(
self
,
pos
,
defaults
,
defaults_struct
):
args
=
[]
for
arg
in
defaults
:
if
not
arg
.
default
.
is_literal
:
arg
=
DefaultArgNode
(
pos
,
arg
,
defaults_struct
)
else
:
arg
=
arg
.
default
args
.
append
(
arg
)
super
(
DefaultsTupleNode
,
self
).
__init__
(
pos
,
args
=
args
)
class
LambdaNode
(
InnerFunctionNode
):
# Lambda expression node (only used as a function reference)
#
...
...
Cython/Compiler/Nodes.py
View file @
34864b14
...
...
@@ -2704,6 +2704,8 @@ class DefNode(FuncDefNode):
py_wrapper_required
=
True
func_cname
=
None
defaults_getter
=
None
def
__init__
(
self
,
pos
,
**
kwds
):
FuncDefNode
.
__init__
(
self
,
pos
,
**
kwds
)
k
=
rk
=
r
=
0
...
...
@@ -3068,6 +3070,9 @@ class DefNode(FuncDefNode):
return
1
def
generate_function_definitions
(
self
,
env
,
code
):
if
self
.
defaults_getter
:
self
.
defaults_getter
.
generate_function_definitions
(
env
,
code
)
# Before closure cnames are mangled
if
self
.
py_wrapper_required
:
# func_cname might be modified by @cname
...
...
Cython/Utility/CythonFunction.c
View file @
34864b14
...
...
@@ -15,6 +15,8 @@
#define __Pyx_CyFunction_Defaults(type, f) \
((type *)(((__pyx_CyFunctionObject *) (f))->defaults))
#define __Pyx_CyFunction_SetDefaultsGetter(f, g) \
((__pyx_CyFunctionObject *) (f))->defaults_getter = (g)
typedef
struct
{
...
...
@@ -34,6 +36,7 @@ typedef struct {
/* Defaults info */
PyObject
*
defaults_tuple
;
/* Const defaults tuple */
PyObject
*
(
*
defaults_getter
)(
PyObject
*
);
}
__pyx_CyFunctionObject
;
static
PyTypeObject
*
__pyx_CyFunctionType
=
0
;
...
...
@@ -210,6 +213,17 @@ __Pyx_CyFunction_get_defaults(__pyx_CyFunctionObject *op)
return
op
->
defaults_tuple
;
}
if
(
op
->
defaults_getter
)
{
PyObject
*
res
=
op
->
defaults_getter
((
PyObject
*
)
op
);
/* Cache result */
if
(
res
)
{
Py_INCREF
(
res
);
op
->
defaults_tuple
=
res
;
}
return
res
;
}
Py_INCREF
(
Py_None
);
return
Py_None
;
}
...
...
@@ -281,6 +295,7 @@ static PyObject *__Pyx_CyFunction_New(PyTypeObject *type, PyMethodDef *ml, int f
op
->
defaults_pyobjects
=
0
;
op
->
defaults
=
NULL
;
op
->
defaults_tuple
=
NULL
;
op
->
defaults_getter
=
NULL
;
PyObject_GC_Track
(
op
);
return
(
PyObject
*
)
op
;
}
...
...
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