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
2ec4d662
Commit
2ec4d662
authored
Nov 08, 2010
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
clean up metaclass code
parent
dbf0e770
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
41 deletions
+41
-41
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+37
-38
Cython/Compiler/Symtab.py
Cython/Compiler/Symtab.py
+3
-3
tests/run/metaclass.pyx
tests/run/metaclass.pyx
+1
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
2ec4d662
...
@@ -1450,7 +1450,7 @@ class NameNode(AtomicExprNode):
...
@@ -1450,7 +1450,7 @@ class NameNode(AtomicExprNode):
return
# There was an error earlier
return
# There was an error earlier
if
entry
.
is_builtin
and
Options
.
cache_builtins
:
if
entry
.
is_builtin
and
Options
.
cache_builtins
:
return
# Lookup already cached
return
# Lookup already cached
elif
entry
.
is_
real_dict
:
elif
entry
.
is_
pyclass_attr
:
assert
entry
.
type
.
is_pyobject
,
"Python global or builtin not a Python object"
assert
entry
.
type
.
is_pyobject
,
"Python global or builtin not a Python object"
interned_cname
=
code
.
intern_identifier
(
self
.
entry
.
name
)
interned_cname
=
code
.
intern_identifier
(
self
.
entry
.
name
)
if
entry
.
is_builtin
:
if
entry
.
is_builtin
:
...
@@ -1459,7 +1459,7 @@ class NameNode(AtomicExprNode):
...
@@ -1459,7 +1459,7 @@ class NameNode(AtomicExprNode):
namespace
=
entry
.
scope
.
namespace_cname
namespace
=
entry
.
scope
.
namespace_cname
code
.
globalstate
.
use_utility_code
(
getitem_dict_utility_code
)
code
.
globalstate
.
use_utility_code
(
getitem_dict_utility_code
)
code
.
putln
(
code
.
putln
(
'%s =
__Pyx_PyDi
ct_GetItem(%s, %s); %s'
%
(
'%s =
PyObje
ct_GetItem(%s, %s); %s'
%
(
self
.
result
(),
self
.
result
(),
namespace
,
namespace
,
interned_cname
,
interned_cname
,
...
@@ -1521,9 +1521,9 @@ class NameNode(AtomicExprNode):
...
@@ -1521,9 +1521,9 @@ class NameNode(AtomicExprNode):
# in Py2.6+, we need to invalidate the method cache
# in Py2.6+, we need to invalidate the method cache
code
.
putln
(
"PyType_Modified(%s);"
%
code
.
putln
(
"PyType_Modified(%s);"
%
entry
.
scope
.
parent_type
.
typeptr_cname
)
entry
.
scope
.
parent_type
.
typeptr_cname
)
elif
entry
.
is_
real_dict
:
elif
entry
.
is_
pyclass_attr
:
code
.
put_error_if_neg
(
self
.
pos
,
code
.
put_error_if_neg
(
self
.
pos
,
'Py
Di
ct_SetItem(%s, %s, %s)'
%
(
'Py
Obje
ct_SetItem(%s, %s, %s)'
%
(
namespace
,
namespace
,
interned_cname
,
interned_cname
,
rhs
.
py_result
()))
rhs
.
py_result
()))
...
@@ -1608,10 +1608,10 @@ class NameNode(AtomicExprNode):
...
@@ -1608,10 +1608,10 @@ class NameNode(AtomicExprNode):
if
not
self
.
entry
.
is_pyglobal
:
if
not
self
.
entry
.
is_pyglobal
:
error
(
self
.
pos
,
"Deletion of local or C global name not supported"
)
error
(
self
.
pos
,
"Deletion of local or C global name not supported"
)
return
return
if
self
.
entry
.
is_
real_dict
:
if
self
.
entry
.
is_
pyclass_attr
:
namespace
=
self
.
entry
.
scope
.
namespace_cname
namespace
=
self
.
entry
.
scope
.
namespace_cname
code
.
put_error_if_neg
(
self
.
pos
,
code
.
put_error_if_neg
(
self
.
pos
,
'Py
Dict
_DelItemString(%s, "%s")'
%
(
'Py
Mapping
_DelItemString(%s, "%s")'
%
(
namespace
,
namespace
,
self
.
entry
.
name
))
self
.
entry
.
name
))
else
:
else
:
...
@@ -4494,7 +4494,7 @@ class ClassNode(ExprNode, ModuleNameMixin):
...
@@ -4494,7 +4494,7 @@ class ClassNode(ExprNode, ModuleNameMixin):
self
.
keyword_args
.
analyse_types
(
env
)
self
.
keyword_args
.
analyse_types
(
env
)
if
self
.
starstar_arg
:
if
self
.
starstar_arg
:
self
.
starstar_arg
.
analyse_types
(
env
)
self
.
starstar_arg
.
analyse_types
(
env
)
if
self
.
starstar_arg
:
# make sure we have a Python object as **kwargs mapping
self
.
starstar_arg
=
\
self
.
starstar_arg
=
\
self
.
starstar_arg
.
coerce_to_pyobject
(
env
)
self
.
starstar_arg
.
coerce_to_pyobject
(
env
)
self
.
type
=
py_object_type
self
.
type
=
py_object_type
...
@@ -4504,7 +4504,7 @@ class ClassNode(ExprNode, ModuleNameMixin):
...
@@ -4504,7 +4504,7 @@ class ClassNode(ExprNode, ModuleNameMixin):
self
.
set_mod_name
(
env
)
self
.
set_mod_name
(
env
)
def
may_be_none
(
self
):
def
may_be_none
(
self
):
return
Fals
e
return
Tru
e
gil_message
=
"Constructing Python class"
gil_message
=
"Constructing Python class"
...
@@ -4540,6 +4540,7 @@ class ClassNode(ExprNode, ModuleNameMixin):
...
@@ -4540,6 +4540,7 @@ class ClassNode(ExprNode, ModuleNameMixin):
code
.
error_goto_if_null
(
self
.
result
(),
self
.
pos
)))
code
.
error_goto_if_null
(
self
.
result
(),
self
.
pos
)))
code
.
put_gotref
(
self
.
py_result
())
code
.
put_gotref
(
self
.
py_result
())
class
BoundMethodNode
(
ExprNode
):
class
BoundMethodNode
(
ExprNode
):
# Helper class used in the implementation of Python
# Helper class used in the implementation of Python
# class definitions. Constructs an bound method
# class definitions. Constructs an bound method
...
@@ -7183,11 +7184,13 @@ static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) {
...
@@ -7183,11 +7184,13 @@ static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) {
create_class_utility_code
=
UtilityCode
(
create_class_utility_code
=
UtilityCode
(
proto
=
"""
proto
=
"""
static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name,
static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name,
PyObject *modname, PyObject *kwargs); /*proto*/
PyObject *modname, PyObject *kwargs); /*proto*/
static int __Pyx_PrepareClass(PyObject *metaclass, PyObject *bases, PyObject *name,
PyObject *mkw, PyObject *dict); /*proto*/
"""
,
"""
,
impl
=
"""
impl
=
"""
static int __Pyx_PrepareClass(PyObject *metaclass, PyObject *bases, PyObject *name,
PyObject *mkw, PyObject *dict)
static int __Pyx_PrepareClass(PyObject *metaclass, PyObject *bases, PyObject *name,
{
PyObject *mkw, PyObject *dict)
{
PyObject *prep;
PyObject *prep;
PyObject *pargs;
PyObject *pargs;
PyObject *ns;
PyObject *ns;
...
@@ -7219,9 +7222,8 @@ static int __Pyx_PrepareClass(PyObject *metaclass, PyObject *bases, PyObject *na
...
@@ -7219,9 +7222,8 @@ static int __Pyx_PrepareClass(PyObject *metaclass, PyObject *bases, PyObject *na
return 0;
return 0;
}
}
static PyObject *__Pyx_CreateClass(
static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name,
PyObject *bases, PyObject *dict, PyObject *name, PyObject *modname, PyObject *kwargs)
PyObject *modname, PyObject *kwargs) {
{
PyObject *result = NULL;
PyObject *result = NULL;
PyObject *metaclass = NULL;
PyObject *metaclass = NULL;
PyObject *mkw = NULL;
PyObject *mkw = NULL;
...
@@ -7232,7 +7234,7 @@ static PyObject *__Pyx_CreateClass(
...
@@ -7232,7 +7234,7 @@ static PyObject *__Pyx_CreateClass(
/* Python3 metaclasses */
/* Python3 metaclasses */
if (kwargs) {
if (kwargs) {
mkw = PyDict_Copy(kwargs); /* Don't modify kwargs passed in! */
mkw = PyDict_Copy(kwargs); /* Don't modify kwargs passed in! */
if (
mkw == NULL
)
if (
!mkw
)
return NULL;
return NULL;
metaclass = PyDict_GetItemString(mkw, "metaclass");
metaclass = PyDict_GetItemString(mkw, "metaclass");
if (metaclass) {
if (metaclass) {
...
@@ -7243,31 +7245,29 @@ static PyObject *__Pyx_CreateClass(
...
@@ -7243,31 +7245,29 @@ static PyObject *__Pyx_CreateClass(
goto bad;
goto bad;
}
}
}
}
/* Python2 __metaclass__ */
if (!metaclass) {
if (metaclass == NULL) {
/* Python2 __metaclass__ */
metaclass = PyDict_GetItemString(dict, "__metaclass__");
metaclass = PyDict_GetItemString(dict, "__metaclass__");
if (metaclass)
if (!metaclass) {
Py_INCREF(metaclass);
/* Default metaclass */
}
/* Default metaclass */
if (metaclass == NULL) {
#if PY_MAJOR_VERSION < 3
#if PY_MAJOR_VERSION < 3
if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
PyObject *base = PyTuple_GET_ITEM(bases, 0);
PyObject *base = PyTuple_GET_ITEM(bases, 0);
metaclass = PyObject_GetAttrString(base, "__class__");
metaclass = PyObject_GetAttrString(base, "__class__");
if (metaclass == NULL
) {
if (!metaclass
) {
PyErr_Clear();
PyErr_Clear();
metaclass = (PyObject *)base->ob_type;
metaclass = (PyObject *)base->ob_type;
}
}
} else
} else
metaclass = (PyObject *) &PyClass_Type;
metaclass = (PyObject *) &PyClass_Type;
#else
#else
if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
PyObject *base = PyTuple_GET_ITEM(bases, 0);
PyObject *base = PyTuple_GET_ITEM(bases, 0);
metaclass = (PyObject *)base->ob_type;
metaclass = (PyObject *)base->ob_type;
} else
} else
metaclass = (PyObject *) &PyType_Type;
metaclass = (PyObject *) &PyType_Type;
#endif
#endif
}
Py_INCREF(metaclass);
Py_INCREF(metaclass);
}
}
if (mkw && PyDict_Size(mkw) > 0) {
if (mkw && PyDict_Size(mkw) > 0) {
...
@@ -7280,8 +7280,7 @@ static PyObject *__Pyx_CreateClass(
...
@@ -7280,8 +7280,7 @@ static PyObject *__Pyx_CreateClass(
}
}
bad:
bad:
Py_DECREF(metaclass);
Py_DECREF(metaclass);
if (mkw)
Py_XDECREF(mkw);
Py_XDECREF(mkw);
return result;
return result;
}
}
"""
)
"""
)
...
...
Cython/Compiler/Symtab.py
View file @
2ec4d662
...
@@ -70,7 +70,7 @@ class Entry(object):
...
@@ -70,7 +70,7 @@ class Entry(object):
# or class attribute during
# or class attribute during
# class construction
# class construction
# is_member boolean Is an assigned class member
# is_member boolean Is an assigned class member
# is_
real_dict boolean Is a real dict, PyClass attributes dict
# is_
pyclass_attr boolean Is a name in a Python class namespace
# is_variable boolean Is a variable
# is_variable boolean Is a variable
# is_cfunction boolean Is a C function
# is_cfunction boolean Is a C function
# is_cmethod boolean Is a C method of an extension type
# is_cmethod boolean Is a C method of an extension type
...
@@ -132,7 +132,7 @@ class Entry(object):
...
@@ -132,7 +132,7 @@ class Entry(object):
is_cglobal = 0
is_cglobal = 0
is_pyglobal = 0
is_pyglobal = 0
is_member = 0
is_member = 0
is_
real_dict
= 0
is_
pyclass_attr
= 0
is_variable = 0
is_variable = 0
is_cfunction = 0
is_cfunction = 0
is_cmethod = 0
is_cmethod = 0
...
@@ -1419,7 +1419,7 @@ class PyClassScope(ClassScope):
...
@@ -1419,7 +1419,7 @@ class PyClassScope(ClassScope):
entry
=
Scope
.
declare_var
(
self
,
name
,
type
,
pos
,
entry
=
Scope
.
declare_var
(
self
,
name
,
type
,
pos
,
cname
,
visibility
,
is_cdef
)
cname
,
visibility
,
is_cdef
)
entry
.
is_pyglobal
=
1
entry
.
is_pyglobal
=
1
entry
.
is_
real_dict
=
1
entry
.
is_
pyclass_attr
=
1
return
entry
return
entry
def
add_default_value
(
self
,
type
):
def
add_default_value
(
self
,
type
):
...
...
tests/run/metaclass.pyx
View file @
2ec4d662
...
@@ -12,6 +12,7 @@ class Foo(object):
...
@@ -12,6 +12,7 @@ class Foo(object):
"""
"""
__metaclass__
=
Base
__metaclass__
=
Base
class
Py3Base
(
type
):
class
Py3Base
(
type
):
def
__new__
(
cls
,
name
,
bases
,
attrs
,
foo
=
None
):
def
__new__
(
cls
,
name
,
bases
,
attrs
,
foo
=
None
):
attrs
[
'foo'
]
=
foo
attrs
[
'foo'
]
=
foo
...
...
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