Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
18a48d66
Commit
18a48d66
authored
Apr 03, 2015
by
Chris Toshok
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
call _PyFile_SanitizeMode to strip out U (and validate) the mode before passing it along to fopen.
parent
2748c606
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
1 deletion
+38
-1
src/runtime/file.cpp
src/runtime/file.cpp
+8
-1
test/tests/file.py
test/tests/file.py
+30
-0
No files found.
src/runtime/file.cpp
View file @
18a48d66
...
...
@@ -826,7 +826,14 @@ Box* fileNew(BoxedClass* cls, Box* s, Box* m) {
auto
fn
=
static_cast
<
BoxedString
*>
(
s
);
auto
mode
=
static_cast
<
BoxedString
*>
(
m
);
FILE
*
f
=
fopen
(
fn
->
data
(),
mode
->
data
());
// all characters in python mode specifiers are valid in fopen calls except 'U'. we strip it out
// of the string we pass to fopen, but pass it along to the BoxedFile ctor.
auto
file_mode
=
std
::
unique_ptr
<
char
[]
>
(
new
char
[
mode
->
size
()
+
1
]);
memmove
(
&
file_mode
[
0
],
mode
->
data
(),
mode
->
size
()
+
1
);
_PyFile_SanitizeMode
(
&
file_mode
[
0
]);
checkAndThrowCAPIException
();
FILE
*
f
=
fopen
(
fn
->
data
(),
&
file_mode
[
0
]);
if
(
!
f
)
{
PyErr_SetFromErrnoWithFilename
(
IOError
,
fn
->
data
());
throwCAPIException
();
...
...
test/tests/file.py
View file @
18a48d66
import
sys
import
tempfile
f
=
open
(
"/dev/null"
)
print
repr
(
f
.
read
())
...
...
@@ -58,3 +59,32 @@ print f.write("H")
print
f
.
tell
()
print
f
.
flush
()
print
f
.
close
()
# tests for universal newlines
fd
,
fn
=
tempfile
.
mkstemp
()
with
open
(
fn
,
"wb"
)
as
f
:
f
.
write
(
"hello world!
\
r
"
)
f
.
write
(
"hello world!
\
r
\
n
"
)
f
.
write
(
"hello world!
\
n
"
)
f
.
write
(
"hello world!
\
r
"
)
with
open
(
fn
)
as
f
:
print
len
(
f
.
readlines
())
with
open
(
fn
,
"rU"
)
as
f
:
print
len
(
f
.
readlines
())
fd
,
fn
=
tempfile
.
mkstemp
()
try
:
with
open
(
fn
,
"wU"
)
as
f
:
print
"succeeded"
except
Exception
as
e
:
print
e
try
:
with
open
(
fn
,
"aU"
)
as
f
:
print
"succeeded"
except
Exception
as
e
:
print
e
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