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
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
e1a600b5
Commit
e1a600b5
authored
May 23, 2008
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
reverted last change, based on extended test case
parent
1e6272ac
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
0 deletions
+58
-0
Cython/Compiler/Symtab.py
Cython/Compiler/Symtab.py
+43
-0
tests/run/classmethod.pyx
tests/run/classmethod.pyx
+15
-0
No files found.
Cython/Compiler/Symtab.py
View file @
e1a600b5
...
...
@@ -1121,6 +1121,24 @@ class ClassScope(Scope):
def add_string_const(self, value):
return self.outer_scope.add_string_const(value)
def lookup(self, name):
if name == "classmethod":
# We don'
t
want
to
use
the
builtin
classmethod
here
'cause it won'
t
do
the
# right thing in this scope (as the class memebers aren't still functions).
# Don't want to add a cfunction to this scope 'cause that would mess with
# the type definition, so we just return the right entry.
self
.
use_utility_code
(
classmethod_utility_code
)
entry
=
Entry
(
"classmethod"
,
"__Pyx_Method_ClassMethod"
,
PyrexTypes
.
CFuncType
(
py_object_type
,
[
PyrexTypes
.
CFuncTypeArg
(
""
,
py_object_type
,
None
)],
0
,
0
))
entry
.
is_cfunction
=
1
return
entry
else
:
return
Scope
.
lookup
(
self
,
name
)
class
PyClassScope
(
ClassScope
):
# Namespace of a Python class.
...
...
@@ -1371,3 +1389,28 @@ class PropertyScope(Scope):
error
(
pos
,
"Only __get__, __set__ and __del__ methods allowed "
"in a property declaration"
)
return
None
# Should this go elsewhere (and then get imported)?
#------------------------------------------------------------------------------------
classmethod_utility_code
=
[
"""
#include "descrobject.h"
static PyObject* __Pyx_Method_ClassMethod(PyObject *method); /*proto*/
"""
,
"""
static PyObject* __Pyx_Method_ClassMethod(PyObject *method) {
/* It appears that PyMethodDescr_Type is not anywhere exposed in the Python/C API */
/* if (!PyObject_TypeCheck(method, &PyMethodDescr_Type)) { */
if (strcmp(Py_TYPE(method)->tp_name, "method_descriptor") == 0) { /* cdef classes */
PyMethodDescrObject *descr = (PyMethodDescrObject *)method;
return PyDescr_NewClassMethod(descr->d_type, descr->d_method);
}
else if (PyMethod_Check(method)) { /* python classes */
return PyClassMethod_New(PyMethod_GET_FUNCTION(method));
}
PyErr_Format(PyExc_TypeError, "Class-level classmethod() can only be called on a method_descriptor or instance method.");
return NULL;
}
"""
]
tests/run/classmethod.pyx
View file @
e1a600b5
__doc__
=
u"""
>>> class1.view()
class1
>>> class1.plus(1)
6
>>> class2.view()
class2
>>> class2.plus(1)
7
>>> class3.view()
class3
>>> class3.plus(1)
8
"""
...
...
@@ -13,11 +19,20 @@ def f_plus(cls, a):
class
class1
:
a
=
5
plus
=
classmethod
(
f_plus
)
def
view
(
cls
):
print
cls
.
__name__
view
=
classmethod
(
view
)
class
class2
(
object
):
a
=
6
plus
=
classmethod
(
f_plus
)
def
view
(
cls
):
print
cls
.
__name__
view
=
classmethod
(
view
)
cdef
class
class3
:
a
=
7
plus
=
classmethod
(
f_plus
)
def
view
(
cls
):
print
cls
.
__name__
view
=
classmethod
(
view
)
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