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
e95a78ad
Commit
e95a78ad
authored
Jun 12, 2016
by
scoder
Committed by
GitHub
Jun 12, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #511 from klaussfreire/master
Add no_gc directive to fully disable GC
parents
d142afbf
264ae3f5
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
61 additions
and
2 deletions
+61
-2
Cython/Compiler/Options.py
Cython/Compiler/Options.py
+2
-0
Cython/Compiler/Symtab.py
Cython/Compiler/Symtab.py
+1
-1
Cython/Shadow.py
Cython/Shadow.py
+1
-1
docs/src/userguide/extension_types.rst
docs/src/userguide/extension_types.rst
+15
-0
tests/run/no_gc.pyx
tests/run/no_gc.pyx
+42
-0
No files found.
Cython/Compiler/Options.py
View file @
e95a78ad
...
...
@@ -116,6 +116,7 @@ directive_defaults = {
'internal'
:
False
,
'profile'
:
False
,
'no_gc_clear'
:
False
,
'no_gc'
:
False
,
'linetrace'
:
False
,
'emit_code_comments'
:
True
,
# copy original source code into C code comments
'annotation_typing'
:
False
,
# read type declarations from Python function annotations
...
...
@@ -246,6 +247,7 @@ directive_scopes = { # defaults to available everywhere
'inline'
:
(
'function'
,),
'staticmethod'
:
(
'function'
,),
# FIXME: analysis currently lacks more specific function scope
'no_gc_clear'
:
(
'cclass'
,),
'no_gc'
:
(
'cclass'
,),
'internal'
:
(
'cclass'
,),
'autotestdict'
:
(
'module'
,),
'autotestdict.all'
:
(
'module'
,),
...
...
Cython/Compiler/Symtab.py
View file @
e95a78ad
...
...
@@ -1889,7 +1889,7 @@ class CClassScope(ClassScope):
def
needs_gc
(
self
):
# If the type or any of its base types have Python-valued
# C attributes, then it needs to participate in GC.
if
self
.
has_cyclic_pyobject_attrs
:
if
self
.
has_cyclic_pyobject_attrs
and
not
self
.
directives
.
get
(
'no_gc'
,
False
)
:
return
True
base_type
=
self
.
parent_type
.
base_type
if
base_type
and
base_type
.
scope
is
not
None
:
...
...
Cython/Shadow.py
View file @
e95a78ad
...
...
@@ -118,7 +118,7 @@ optimization = _Optimization()
overflowcheck
.
fold
=
optimization
.
use_switch
=
\
optimization
.
unpack_method_calls
=
lambda
arg
:
_EmptyDecoratorAndManager
()
final
=
internal
=
type_version_tag
=
no_gc_clear
=
_empty_decorator
final
=
internal
=
type_version_tag
=
no_gc_clear
=
no_gc
=
_empty_decorator
def
inline
(
f
,
*
args
,
**
kwds
):
...
...
docs/src/userguide/extension_types.rst
View file @
e95a78ad
...
...
@@ -514,6 +514,21 @@ which makes it impossible to clean up the cursor.
Using the ``no_gc_clear`` decorator this can not happen anymore because the
references of a cursor object will not be cleared anymore.
In rare cases, extension types can be guaranteed not to participate in cycles,
but the compiler won't be able to prove this. This would be the case if
the class can never reference itself, even indirectly.
In that case, you can manually disable cycle collection by using the
``no_gc`` decorator, but beware that doing so when in fact the extension type
can participate in cycles could cause memory leaks ::
@cython.no_gc
cdef class UserInfo:
cdef str name
cdef tuple addresses
If you can be sure addresses will contain only references to strings,
the above would be safe, and it may yield a significant speedup, depending on
your usage pattern.
Public and external extension types
====================================
...
...
tests/run/no_gc.pyx
0 → 100644
View file @
e95a78ad
"""
Check that the @cython.no_gc decorator disables generation of the
tp_clear and tp_traverse slots, that is, disables cycle collection.
"""
cimport
cython
from
cpython.ref
cimport
PyObject
,
Py_TYPE
# Force non-gc'd PyTypeObject when safety is guaranteed by user but not provable
cdef
extern
from
*
:
ctypedef
struct
PyTypeObject
:
void
(
*
tp_clear
)(
object
)
void
(
*
tp_traverse
)(
object
)
def
is_tp_clear_null
(
obj
):
return
(
<
PyTypeObject
*>
Py_TYPE
(
obj
)).
tp_clear
is
NULL
def
is_tp_traverse_null
(
obj
):
return
(
<
PyTypeObject
*>
Py_TYPE
(
obj
)).
tp_traverse
is
NULL
@
cython
.
no_gc
cdef
class
DisableGC
:
"""
An extension type that has tp_clear and tp_traverse methods generated
to test that it actually clears the references to NULL.
>>> uut = DisableGC()
>>> is_tp_clear_null(uut)
True
>>> is_tp_traverse_null(uut)
True
"""
cdef
public
object
requires_cleanup
def
__cinit__
(
self
):
self
.
requires_cleanup
=
(
"Tuples to strings don't really need cleanup, cannot take part of cycles"
,)
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