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
Kirill Smelkov
cython
Commits
8da8ec52
Commit
8da8ec52
authored
Oct 03, 2009
by
Robert Bradshaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cython.typeof special method
parent
578d94c5
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
87 additions
and
4 deletions
+87
-4
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+18
-3
Cython/Compiler/ParseTreeTransforms.py
Cython/Compiler/ParseTreeTransforms.py
+6
-1
Cython/Shadow.py
Cython/Shadow.py
+3
-0
tests/run/typeof.pyx
tests/run/typeof.pyx
+60
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
8da8ec52
...
...
@@ -4245,6 +4245,24 @@ class SizeofVarNode(SizeofNode):
def
generate_result_code
(
self
,
code
):
pass
class
TypeofNode
(
StringNode
):
# Compile-time type of an expression, as a string.
#
# operand ExprNode
subexprs
=
[
'operand'
]
def
analyse_types
(
self
,
env
):
self
.
operand
.
analyse_types
(
env
)
from
StringEncoding
import
EncodedString
self
.
value
=
EncodedString
(
str
(
self
.
operand
.
type
))
StringNode
.
analyse_types
(
self
,
env
)
def
analyse_as_type
(
self
,
env
):
return
None
def
generate_evaluation_code
(
self
,
code
):
self
.
generate_result_code
(
code
)
#-------------------------------------------------------------------
#
...
...
@@ -4850,9 +4868,6 @@ class CondExprNode(ExprNode):
def
infer_type
(
self
,
env
):
return
self
.
compute_result_type
(
self
.
true_val
.
infer_type
(
env
),
self
.
false_val
.
infer_type
(
env
))
def
infer_types
(
self
,
env
):
return
self
.
compute_result_type
(
self
.
true_val
.
infer_types
(
env
),
self
.
false_val
.
infer_types
(
env
))
def
calculate_constant_result
(
self
):
if
self
.
test
.
constant_result
:
...
...
Cython/Compiler/ParseTreeTransforms.py
View file @
8da8ec52
...
...
@@ -328,7 +328,7 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
duplication of functionality has to occur: We manually track cimports
and which names the "cython" module may have been imported to.
"""
special_methods
=
set
([
'declare'
,
'union'
,
'struct'
,
'typedef'
,
'sizeof'
,
'cast'
,
'address'
,
'pointer'
,
'compiled'
,
'NULL'
])
special_methods
=
set
([
'declare'
,
'union'
,
'struct'
,
'typedef'
,
'sizeof'
,
'
typeof'
,
'
cast'
,
'address'
,
'pointer'
,
'compiled'
,
'NULL'
])
def
__init__
(
self
,
context
,
compilation_option_overrides
):
super
(
InterpretCompilerDirectives
,
self
).
__init__
(
context
)
...
...
@@ -1009,6 +1009,11 @@ class TransformBuiltinMethods(EnvTransform):
node
=
SizeofTypeNode
(
node
.
function
.
pos
,
arg_type
=
type
)
else
:
node
=
SizeofVarNode
(
node
.
function
.
pos
,
operand
=
node
.
args
[
0
])
elif
function
==
'typeof'
:
if
len
(
node
.
args
)
!=
1
:
error
(
node
.
function
.
pos
,
u"sizeof takes exactly one argument"
%
function
)
else
:
node
=
TypeofNode
(
node
.
function
.
pos
,
operand
=
node
.
args
[
0
])
elif
function
==
'address'
:
if
len
(
node
.
args
)
!=
1
:
error
(
node
.
function
.
pos
,
u"sizeof takes exactly one argument"
%
function
)
...
...
Cython/Shadow.py
View file @
8da8ec52
...
...
@@ -31,6 +31,9 @@ def cast(type, arg):
def
sizeof
(
arg
):
return
1
def
typeof
(
arg
):
return
type
(
arg
)
def
address
(
arg
):
return
pointer
(
type
(
arg
))([
arg
])
...
...
tests/run/typeof.pyx
0 → 100644
View file @
8da8ec52
__doc__
=
u"""
>>> simple()
int
long
long long
int *
int **
A
B
X
Python object
>>> expression()
double
double complex
int
unsigned int
"""
from
cython
cimport
typeof
cdef
class
A
:
pass
cdef
class
B
(
A
):
pass
cdef
struct
X
:
double
a
double
complex
b
def
simple
():
cdef
int
i
cdef
long
l
cdef
long
long
ll
cdef
int
*
iptr
cdef
int
**
iptrptr
cdef
A
a
cdef
B
b
cdef
X
x
print
typeof
(
i
)
print
typeof
(
l
)
print
typeof
(
ll
)
print
typeof
(
iptr
)
print
typeof
(
iptrptr
)
print
typeof
(
a
)
print
typeof
(
b
)
print
typeof
(
x
)
print
typeof
(
None
)
def
expression
():
cdef
X
x
cdef
X
*
xptr
cdef
short
s
cdef
int
i
cdef
unsigned
int
ui
print
typeof
(
x
.
a
)
print
typeof
(
xptr
.
b
)
print
typeof
(
s
+
i
)
print
typeof
(
i
+
ui
)
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