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
77eb0f6f
Commit
77eb0f6f
authored
Jan 01, 2014
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support non-types as Py2 metaclasses
parent
455e0357
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
24 deletions
+31
-24
Cython/Utility/ObjectHandling.c
Cython/Utility/ObjectHandling.c
+11
-24
tests/run/metaclass.pyx
tests/run/metaclass.pyx
+20
-0
No files found.
Cython/Utility/ObjectHandling.c
View file @
77eb0f6f
...
...
@@ -868,35 +868,24 @@ static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObj
PyObject
*
dict
,
PyObject
*
mkw
,
int
calculate_metaclass
,
int
allow_py2_metaclass
)
{
PyObject
*
result
,
*
margs
;
PyObject
*
py2
_metaclass
=
NULL
;
PyObject
*
owned
_metaclass
=
NULL
;
if
(
allow_py2_metaclass
)
{
/* honour Python2 __metaclass__ for backward compatibility */
py2_metaclass
=
PyObject_GetItem
(
dict
,
PYIDENT
(
"__metaclass__"
));
if
(
py2_metaclass
)
{
if
(
likely
(
PyType_Check
(
py2_metaclass
)))
{
metaclass
=
py2_metaclass
;
calculate_metaclass
=
1
;
}
else
{
/* py2_metaclass != NULL => calculate_metaclass != 0 */
Py_DECREF
(
py2_metaclass
);
py2_metaclass
=
NULL
;
}
owned_metaclass
=
PyObject_GetItem
(
dict
,
PYIDENT
(
"__metaclass__"
));
if
(
owned_metaclass
)
{
metaclass
=
owned_metaclass
;
}
else
if
(
likely
(
PyErr_ExceptionMatches
(
PyExc_KeyError
)))
{
PyErr_Clear
();
}
else
{
return
NULL
;
}
}
if
(
calculate_metaclass
)
{
if
(
py2_metaclass
||
PyType_Check
(
metaclass
))
{
metaclass
=
__Pyx_CalculateMetaclass
((
PyTypeObject
*
)
metaclass
,
bases
);
Py_XDECREF
(
py2_metaclass
);
if
(
unlikely
(
!
metaclass
))
return
NULL
;
}
else
{
Py_XDECREF
(
py2_metaclass
);
calculate_metaclass
=
0
;
}
if
(
calculate_metaclass
&&
(
!
metaclass
||
PyType_Check
(
metaclass
)))
{
metaclass
=
__Pyx_CalculateMetaclass
((
PyTypeObject
*
)
metaclass
,
bases
);
Py_XDECREF
(
owned_metaclass
);
if
(
unlikely
(
!
metaclass
))
return
NULL
;
owned_metaclass
=
metaclass
;
}
margs
=
PyTuple_Pack
(
3
,
name
,
bases
,
dict
);
if
(
unlikely
(
!
margs
))
{
...
...
@@ -905,9 +894,7 @@ static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObj
result
=
PyObject_Call
(
metaclass
,
margs
,
mkw
);
Py_DECREF
(
margs
);
}
if
(
calculate_metaclass
)
{
Py_DECREF
(
metaclass
);
}
Py_XDECREF
(
owned_metaclass
);
return
result
;
}
...
...
tests/run/metaclass.pyx
View file @
77eb0f6f
...
...
@@ -15,6 +15,26 @@ class Foo(object):
"""
__metaclass__
=
Base
def
non_type_metaclass
(
name
,
bases
,
namespace
):
namespace
[
'BASES'
]
=
[
b
.
__name__
for
b
in
bases
]
namespace
[
'NAME'
]
=
name
return
type
(
name
,
bases
,
namespace
)
class
FunctionAsPy2Metaclass
(
object
):
"""
>>> obj = FunctionAsPy2Metaclass()
>>> obj.NAME
'FunctionAsPy2Metaclass'
>>> obj.BASES
['object']
>>> obj.x
1
"""
__metaclass__
=
non_type_metaclass
x
=
1
class
ODict
(
dict
):
def
__init__
(
self
):
dict
.
__init__
(
self
)
...
...
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