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
Xavier Thompson
cython
Commits
9170a5ad
Commit
9170a5ad
authored
Aug 27, 2019
by
Jeroen Demeyer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Special-case setting __new__ in class
parent
3ac72f5f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
2 deletions
+49
-2
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+4
-2
Cython/Utility/ObjectHandling.c
Cython/Utility/ObjectHandling.c
+24
-0
tests/run/set_new.py
tests/run/set_new.py
+21
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
9170a5ad
...
...
@@ -2295,8 +2295,10 @@ class NameNode(AtomicExprNode):
setter
=
'PyDict_SetItem'
namespace
=
Naming
.
moddict_cname
elif
entry
.
is_pyclass_attr
:
code
.
globalstate
.
use_utility_code
(
UtilityCode
.
load_cached
(
"SetNameInClass"
,
"ObjectHandling.c"
))
setter
=
'__Pyx_SetNameInClass'
# Special-case setting __new__
n
=
"SetNewInClass"
if
self
.
name
==
"__new__"
else
"SetNameInClass"
code
.
globalstate
.
use_utility_code
(
UtilityCode
.
load_cached
(
n
,
"ObjectHandling.c"
))
setter
=
'__Pyx_'
+
n
else
:
assert
False
,
repr
(
entry
)
code
.
put_error_if_neg
(
...
...
Cython/Utility/ObjectHandling.c
View file @
9170a5ad
...
...
@@ -1209,6 +1209,30 @@ static PyObject *__Pyx__GetNameInClass(PyObject *nmspace, PyObject *name) {
#define __Pyx_SetNameInClass(ns, name, value) PyObject_SetItem(ns, name, value)
#endif
/////////////// SetNewInClass.proto ///////////////
static
int
__Pyx_SetNewInClass
(
PyObject
*
ns
,
PyObject
*
name
,
PyObject
*
value
);
/////////////// SetNewInClass ///////////////
//@requires: SetNameInClass
// Special-case setting __new__: if it's a Cython function, wrap it in a
// staticmethod. This is similar to what Python does for a Python function
// called __new__.
static
int
__Pyx_SetNewInClass
(
PyObject
*
ns
,
PyObject
*
name
,
PyObject
*
value
)
{
#ifdef __Pyx_CyFunction_USED
int
ret
;
if
(
__Pyx_CyFunction_Check
(
value
))
{
PyObject
*
staticnew
=
PyStaticMethod_New
(
value
);
if
(
unlikely
(
!
staticnew
))
return
-
1
;
ret
=
__Pyx_SetNameInClass
(
ns
,
name
,
staticnew
);
Py_DECREF
(
staticnew
);
return
ret
;
}
#endif
return
__Pyx_SetNameInClass
(
ns
,
name
,
value
);
}
/////////////// GetModuleGlobalName.proto ///////////////
//@requires: PyDictVersioning
...
...
tests/run/set_new.py
0 → 100644
View file @
9170a5ad
"""
>>> X = make_class_with_new(cynew)
>>> X.__new__ is cynew
True
>>> X().__new__ is cynew
True
>>> def pynew(cls): return object.__new__(cls)
>>> X = make_class_with_new(pynew)
>>> X.__new__ is pynew
True
>>> X().__new__ is pynew
True
"""
def
make_class_with_new
(
n
):
class
X
(
object
):
__new__
=
n
return
X
def
cynew
(
cls
):
return
object
.
__new__
(
cls
)
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