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
Boxiang Sun
cython
Commits
8b50da87
Commit
8b50da87
authored
Jan 28, 2012
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix (and test) bisecting into code object cache
parent
ad18e020
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
113 additions
and
13 deletions
+113
-13
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+2
-2
Cython/Utility/ModuleSetupCode.c
Cython/Utility/ModuleSetupCode.c
+12
-11
tests/run/code_object_cache.pyx
tests/run/code_object_cache.pyx
+99
-0
No files found.
Cython/Compiler/Nodes.py
View file @
8b50da87
...
...
@@ -8711,12 +8711,12 @@ static void __Pyx_AddTraceback(const char *funcname, int %(CLINENO)s,
PyObject *py_globals = 0;
PyFrameObject *py_frame = 0;
py_code = %(FINDCODEOBJECT)s(%(CLINENO)s);
py_code = %(FINDCODEOBJECT)s(%(CLINENO)s
? %(CLINENO)s : %(LINENO)s
);
if (!py_code) {
py_code = __Pyx_CreateCodeObjectForTraceback(
funcname, %(CLINENO)s, %(LINENO)s, %(FILENAME)s);
if (!py_code) goto bad;
%(INSERTCODEOBJECT)s(%(CLINENO)s, py_code);
%(INSERTCODEOBJECT)s(%(CLINENO)s
? %(CLINENO)s : %(LINENO)s
, py_code);
}
py_globals = PyModule_GetDict(%(GLOBALS)s);
if (!py_globals) goto bad;
...
...
Cython/Utility/ModuleSetupCode.c
View file @
8b50da87
...
...
@@ -234,6 +234,7 @@ struct __Pyx_CodeObjectCache {
static
struct
__Pyx_CodeObjectCache
__pyx_code_cache
=
{
0
,
0
,
NULL
};
static
int
__pyx_bisect_code_objects
(
__Pyx_CodeObjectCacheEntry
*
entries
,
int
count
,
int
code_line
);
static
void
__pyx_clear_code_object_cache
(
void
);
static
PyCodeObject
*
__pyx_find_code_object
(
int
code_line
);
static
void
__pyx_insert_code_object
(
int
code_line
,
PyCodeObject
*
code_object
);
...
...
@@ -258,25 +259,25 @@ static void __pyx_clear_code_object_cache(void) {
PyMem_Free
(
entries
);
}
static
int
__pyx_bisect_code_objects
(
int
code_line
)
{
int
start
,
end
,
mid
;
__Pyx_CodeObjectCacheEntry
*
entries
=
__pyx_code_cache
.
entries
;
start
=
0
;
end
=
__pyx_code_cache
.
count
-
1
;
static
int
__pyx_bisect_code_objects
(
__Pyx_CodeObjectCacheEntry
*
entries
,
int
count
,
int
code_line
)
{
int
start
=
0
,
mid
=
0
,
end
=
count
-
1
;
if
(
end
>=
0
&&
code_line
>
entries
[
end
].
code_line
)
{
return
count
;
}
while
(
start
<
end
)
{
mid
=
(
start
+
end
)
/
2
;
if
(
code_line
<
entries
[
mid
].
code_line
)
{
end
=
mid
-
1
;
end
=
mid
;
}
else
if
(
code_line
>
entries
[
mid
].
code_line
)
{
start
=
mid
+
1
;
}
else
{
return
mid
;
}
}
if
(
start
==
end
||
code_line
<=
entries
[
start
].
code_line
)
{
return
start
;
if
(
code_line
<=
entries
[
mid
].
code_line
)
{
return
mid
;
}
else
{
return
end
;
return
mid
+
1
;
}
}
...
...
@@ -286,7 +287,7 @@ static PyCodeObject *__pyx_find_code_object(int code_line) {
if
(
unlikely
(
!
code_line
)
||
unlikely
(
!
__pyx_code_cache
.
entries
))
{
return
NULL
;
}
pos
=
__pyx_bisect_code_objects
(
code_line
);
pos
=
__pyx_bisect_code_objects
(
__pyx_code_cache
.
entries
,
__pyx_code_cache
.
count
,
code_line
);
if
(
unlikely
(
pos
>=
__pyx_code_cache
.
count
)
||
unlikely
(
__pyx_code_cache
.
entries
[
pos
].
code_line
!=
code_line
))
{
return
NULL
;
}
...
...
@@ -313,7 +314,7 @@ static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object) {
}
return
;
}
pos
=
__pyx_bisect_code_objects
(
code_line
);
pos
=
__pyx_bisect_code_objects
(
__pyx_code_cache
.
entries
,
__pyx_code_cache
.
count
,
code_line
);
if
((
pos
<
__pyx_code_cache
.
count
)
&&
unlikely
(
__pyx_code_cache
.
entries
[
pos
].
code_line
==
code_line
))
{
PyCodeObject
*
tmp
=
entries
[
pos
].
code_object
;
entries
[
pos
].
code_object
=
code_object
;
...
...
tests/run/code_object_cache.pyx
0 → 100644
View file @
8b50da87
# mode: run
# tag: except
# test the code object cache that is being used in exception raising
### low level tests
cdef
extern
from
*
:
# evil hack to access the internal utility function
ctypedef
struct
PyCodeObject
ctypedef
struct
__Pyx_CodeObjectCacheEntry
:
int
code_line
PyCodeObject
*
code_object
int
__pyx_bisect_code_objects
(
__Pyx_CodeObjectCacheEntry
*
entries
,
int
count
,
int
code_line
)
def
test_lowlevel_bisect2
(
*
indices
):
"""
>>> test_lowlevel_bisect2(1, 2, 3, 4, 5, 6)
[0, 0, 1, 1, 2, 2]
"""
cdef
__Pyx_CodeObjectCacheEntry
*
cache
=
[
__Pyx_CodeObjectCacheEntry
(
2
,
NULL
),
__Pyx_CodeObjectCacheEntry
(
4
,
NULL
),
]
return
[
__pyx_bisect_code_objects
(
cache
,
2
,
i
)
for
i
in
indices
]
def
test_lowlevel_bisect5
(
*
indices
):
"""
>>> test_lowlevel_bisect5(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
[0, 1, 2, 2, 2, 3, 3, 3, 4, 5, 5]
"""
cdef
__Pyx_CodeObjectCacheEntry
*
cache
=
[
__Pyx_CodeObjectCacheEntry
(
1
,
NULL
),
__Pyx_CodeObjectCacheEntry
(
2
,
NULL
),
__Pyx_CodeObjectCacheEntry
(
5
,
NULL
),
__Pyx_CodeObjectCacheEntry
(
8
,
NULL
),
__Pyx_CodeObjectCacheEntry
(
9
,
NULL
),
]
return
[
__pyx_bisect_code_objects
(
cache
,
5
,
i
)
for
i
in
indices
]
def
test_lowlevel_bisect6
(
*
indices
):
"""
>>> test_lowlevel_bisect6(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
[0, 0, 1, 2, 2, 2, 3, 3, 4, 5, 5, 5, 6]
"""
cdef
__Pyx_CodeObjectCacheEntry
*
cache
=
[
__Pyx_CodeObjectCacheEntry
(
2
,
NULL
),
__Pyx_CodeObjectCacheEntry
(
3
,
NULL
),
__Pyx_CodeObjectCacheEntry
(
6
,
NULL
),
__Pyx_CodeObjectCacheEntry
(
8
,
NULL
),
__Pyx_CodeObjectCacheEntry
(
9
,
NULL
),
__Pyx_CodeObjectCacheEntry
(
12
,
NULL
),
]
return
[
__pyx_bisect_code_objects
(
cache
,
6
,
i
)
for
i
in
indices
]
### Python level tests
import
sys
def
tb
():
return
sys
.
exc_info
()[
-
1
]
def
raise_keyerror
():
raise
KeyError
def
check_code_object_identity_recursively
(
tb1
,
tb2
):
if
tb1
is
None
or
tb2
is
None
:
return
code1
,
code2
=
tb1
.
tb_frame
.
f_code
,
tb2
.
tb_frame
.
f_code
if
code1
is
not
code2
:
print
(
'%s != %s'
%
(
code1
,
code2
))
check_code_object_identity_recursively
(
tb1
.
tb_next
,
tb2
.
tb_next
)
def
assert_simple_code_object_reuse
():
"""
>>> try: assert_simple_code_object_reuse()
... except KeyError: t1 = tb()
>>> try: assert_simple_code_object_reuse()
... except KeyError: t2 = tb()
>>> check_code_object_identity_recursively(t1.tb_next, t2.tb_next)
"""
raise
KeyError
def
assert_multi_step_code_object_reuse
(
recursions
=
0
):
"""
>>> for depth in range(5):
... try: assert_multi_step_code_object_reuse(depth)
... except KeyError: t1 = tb()
... try: assert_multi_step_code_object_reuse(depth)
... except KeyError: t2 = tb()
... check_code_object_identity_recursively(t1.tb_next, t2.tb_next)
"""
if
recursions
:
assert_multi_step_code_object_reuse
(
recursions
-
1
)
else
:
raise_keyerror
()
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