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
909f362c
Commit
909f362c
authored
May 09, 2012
by
Vitja Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
print: Do Py_INCREF() before using stdout
parent
e32639a3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
6 deletions
+56
-6
Cython/Utility/Printing.c
Cython/Utility/Printing.c
+16
-6
tests/run/print_refcount.pyx
tests/run/print_refcount.pyx
+40
-0
No files found.
Cython/Utility/Printing.c
View file @
909f362c
...
...
@@ -30,14 +30,15 @@ static int __Pyx_Print(PyObject* f, PyObject *arg_tuple, int newline) {
if
(
!
(
f
=
__Pyx_GetStdout
()))
return
-
1
;
}
Py_INCREF
(
f
);
for
(
i
=
0
;
i
<
PyTuple_GET_SIZE
(
arg_tuple
);
i
++
)
{
if
(
PyFile_SoftSpace
(
f
,
1
))
{
if
(
PyFile_WriteString
(
" "
,
f
)
<
0
)
return
-
1
;
goto
error
;
}
v
=
PyTuple_GET_ITEM
(
arg_tuple
,
i
);
if
(
PyFile_WriteObject
(
v
,
f
,
Py_PRINT_RAW
)
<
0
)
return
-
1
;
goto
error
;
if
(
PyString_Check
(
v
))
{
char
*
s
=
PyString_AsString
(
v
);
Py_ssize_t
len
=
PyString_Size
(
v
);
...
...
@@ -49,10 +50,14 @@ static int __Pyx_Print(PyObject* f, PyObject *arg_tuple, int newline) {
}
if
(
newline
)
{
if
(
PyFile_WriteString
(
"
\n
"
,
f
)
<
0
)
return
-
1
;
goto
error
;
PyFile_SoftSpace
(
f
,
0
);
}
Py_DECREF
(
f
);
return
0
;
error:
Py_DECREF
(
f
);
return
-
1
;
}
#else
/* Python 3 has a print function */
...
...
@@ -125,15 +130,20 @@ static int __Pyx_PrintOne(PyObject* f, PyObject *o) {
if
(
!
(
f
=
__Pyx_GetStdout
()))
return
-
1
;
}
Py_INCREF
(
f
);
if
(
PyFile_SoftSpace
(
f
,
0
))
{
if
(
PyFile_WriteString
(
" "
,
f
)
<
0
)
return
-
1
;
goto
error
;
}
if
(
PyFile_WriteObject
(
o
,
f
,
Py_PRINT_RAW
)
<
0
)
return
-
1
;
goto
error
;
if
(
PyFile_WriteString
(
"
\n
"
,
f
)
<
0
)
return
-
1
;
goto
error
;
Py_DECREF
(
f
);
return
0
;
error:
Py_DECREF
(
f
);
return
-
1
;
/* the line below is just to avoid compiler
* compiler warnings about unused functions */
return
__Pyx_Print
(
f
,
NULL
,
0
);
...
...
tests/run/print_refcount.pyx
0 → 100644
View file @
909f362c
# mode: run
import
sys
def
test_print_refcount
():
"""
>>> test_print_refcount()
"""
old_stdout
=
sys
.
stdout
class
StdoutGuard
:
def
__getattr__
(
self
,
attr
):
sys
.
stdout
=
old_stdout
raise
RuntimeError
sys
.
stdout
=
StdoutGuard
()
try
:
print
"Hello"
,
"world!"
except
RuntimeError
:
pass
finally
:
sys
.
stdout
=
old_stdout
class
TriggerSIGSEGV
(
object
):
pass
def
test_printone_refcount
():
"""
>>> test_printone_refcount()
"""
old_stdout
=
sys
.
stdout
class
StdoutGuard
:
def
__getattr__
(
self
,
attr
):
sys
.
stdout
=
old_stdout
raise
RuntimeError
sys
.
stdout
=
StdoutGuard
()
try
:
print
"Oops!"
except
RuntimeError
:
pass
finally
:
sys
.
stdout
=
old_stdout
class
TriggerSIGSEGV
(
object
):
pass
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