Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
51ee44a7
Commit
51ee44a7
authored
Jan 20, 2014
by
Paul Sokolovsky
Browse files
unix file: Refactor and add sys.stdout/stdin/stderr.
parent
8965a5eb
Changes
2
Hide whitespace changes
Inline
Side-by-side
unix/file.c
View file @
51ee44a7
...
...
@@ -8,6 +8,7 @@
#include
"mpconfig.h"
#include
"mpqstr.h"
#include
"obj.h"
#include
"runtime.h"
#include
"stream.h"
typedef
struct
_mp_obj_fdfile_t
{
...
...
@@ -15,6 +16,8 @@ typedef struct _mp_obj_fdfile_t {
int
fd
;
}
mp_obj_fdfile_t
;
static
const
mp_obj_type_t
rawfile_type
;
static
void
fdfile_print
(
void
(
*
print
)(
void
*
env
,
const
char
*
fmt
,
...),
void
*
env
,
mp_obj_t
self_in
,
mp_print_kind_t
kind
)
{
mp_obj_fdfile_t
*
self
=
self_in
;
print
(
env
,
"<io.FileIO %d>"
,
self
->
fd
);
...
...
@@ -45,6 +48,13 @@ static mp_obj_t fdfile_close(mp_obj_t self_in) {
}
static
MP_DEFINE_CONST_FUN_OBJ_1
(
fdfile_close_obj
,
fdfile_close
);
static
mp_obj_fdfile_t
*
fdfile_new
(
int
fd
)
{
mp_obj_fdfile_t
*
o
=
m_new_obj
(
mp_obj_fdfile_t
);
o
->
base
.
type
=
&
rawfile_type
;
o
->
fd
=
fd
;
return
o
;
}
static
mp_obj_t
fdfile_make_new
(
mp_obj_t
type_in
,
uint
n_args
,
uint
n_kw
,
const
mp_obj_t
*
args
)
{
mp_obj_fdfile_t
*
o
=
m_new_obj
(
mp_obj_fdfile_t
);
o
->
base
.
type
=
type_in
;
...
...
@@ -81,11 +91,11 @@ static mp_obj_t fdfile_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
}
}
o
->
fd
=
open
(
fname
,
mode
,
0644
);
if
(
o
->
fd
==
-
1
)
{
nlr_jump
(
mp_obj_new_exception_msg_
1_
arg
(
MP_QSTR_OSError
,
"[Errno %d]"
,
(
const
char
*
)(
machine_int_t
)
errno
));
int
fd
=
open
(
fname
,
mode
,
0644
);
if
(
fd
==
-
1
)
{
nlr_jump
(
mp_obj_new_exception_msg_
v
arg
(
MP_QSTR_OSError
,
"[Errno %d]"
,
errno
));
}
return
o
;
return
fdfile_new
(
fd
)
;
}
static
const
mp_method_t
rawfile_type_methods
[]
=
{
...
...
@@ -117,3 +127,12 @@ mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args) {
return
fdfile_make_new
((
mp_obj_t
)
&
rawfile_type
,
n_args
,
0
,
args
);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
mp_builtin_open_obj
,
1
,
2
,
mp_builtin_open
);
void
file_init
()
{
rt_store_name
(
qstr_from_str_static
(
"open"
),
(
mp_obj_t
)
&
mp_builtin_open_obj
);
mp_obj_t
m_sys
=
mp_obj_new_module
(
qstr_from_str_static
(
"sys"
));
rt_store_attr
(
m_sys
,
qstr_from_str_static
(
"stdin"
),
fdfile_new
(
STDIN_FILENO
));
rt_store_attr
(
m_sys
,
qstr_from_str_static
(
"stdout"
),
fdfile_new
(
STDOUT_FILENO
));
rt_store_attr
(
m_sys
,
qstr_from_str_static
(
"stderr"
),
fdfile_new
(
STDERR_FILENO
));
}
unix/main.c
View file @
51ee44a7
...
...
@@ -21,6 +21,7 @@
#endif
extern
const
mp_obj_fun_native_t
mp_builtin_open_obj
;
void
file_init
();
void
rawsocket_init
();
static
void
execute_from_lexer
(
mp_lexer_t
*
lex
,
mp_parse_input_kind_t
input_kind
,
bool
is_repl
)
{
...
...
@@ -221,7 +222,7 @@ int main(int argc, char **argv) {
rt_store_attr
(
m_sys
,
qstr_from_str_static
(
"argv"
),
py_argv
);
rt_store_name
(
qstr_from_str_static
(
"test"
),
test_obj_new
(
42
));
rt_store_name
(
qstr_from_str_static
(
"open"
),
(
mp_obj_t
)
&
mp_builtin_open_obj
);
file_init
(
);
rawsocket_init
();
// Here is some example code to create a class and instance of that class.
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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