Skip to content

Commit ddea7cb

Browse files
committed
extmod/moduos_dupterm: Make uos.dupterm() implementation reusable.
That's just function which sets/gets dup terminal object, and can be easily reused across ports.
1 parent 0992588 commit ddea7cb

File tree

4 files changed

+90
-21
lines changed

4 files changed

+90
-21
lines changed

extmod/misc.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2014-2016 Damien P. George
7+
* Copyright (c) 2016 Paul Sokolovsky
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
// This file contains cumulative declarations for extmod/ .
29+
30+
#include "py/runtime.h"
31+
32+
MP_DECLARE_CONST_FUN_OBJ(mp_uos_dupterm_obj);

extmod/moduos_dupterm.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Paul Sokolovsky
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <errno.h>
28+
#include <string.h>
29+
#include "py/mpconfig.h"
30+
31+
#include "py/nlr.h"
32+
#include "py/runtime.h"
33+
#include "py/objtuple.h"
34+
35+
#if MICROPY_PY_OS_DUPTERM
36+
37+
STATIC mp_obj_t mp_uos_dupterm(mp_uint_t n_args, const mp_obj_t *args) {
38+
if (n_args == 0) {
39+
if (MP_STATE_PORT(term_obj) == MP_OBJ_NULL) {
40+
return mp_const_none;
41+
} else {
42+
return MP_STATE_PORT(term_obj);
43+
}
44+
} else {
45+
if (args[0] == mp_const_none) {
46+
MP_STATE_PORT(term_obj) = NULL;
47+
} else {
48+
MP_STATE_PORT(term_obj) = args[0];
49+
}
50+
return mp_const_none;
51+
}
52+
}
53+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_uos_dupterm_obj, 0, 1, mp_uos_dupterm);
54+
55+
#endif

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ PY_O_BASENAME = \
169169
../extmod/machine_mem.o \
170170
../extmod/modussl.o \
171171
../extmod/fsusermount.o \
172+
../extmod/moduos_dupterm.o \
172173

173174
# prepend the build destination prefix to the py object files
174175
PY_O = $(addprefix $(PY_BUILD)/, $(PY_O_BASENAME))

unix/modos.c

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "py/nlr.h"
3838
#include "py/runtime.h"
3939
#include "py/objtuple.h"
40+
#include "extmod/misc.h"
4041

4142
#ifdef __ANDROID__
4243
#define USE_STATFS 1
@@ -215,26 +216,6 @@ STATIC mp_obj_t mod_os_errno(mp_uint_t n_args, const mp_obj_t *args) {
215216
}
216217
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_os_errno_obj, 0, 1, mod_os_errno);
217218

218-
#if MICROPY_PY_OS_DUPTERM
219-
STATIC mp_obj_t mod_os_dupterm(mp_uint_t n_args, const mp_obj_t *args) {
220-
if (n_args == 0) {
221-
if (MP_STATE_PORT(term_obj) == MP_OBJ_NULL) {
222-
return mp_const_none;
223-
} else {
224-
return MP_STATE_PORT(term_obj);
225-
}
226-
} else {
227-
if (args[0] == mp_const_none) {
228-
MP_STATE_PORT(term_obj) = NULL;
229-
} else {
230-
MP_STATE_PORT(term_obj) = args[0];
231-
}
232-
return mp_const_none;
233-
}
234-
}
235-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_os_dupterm_obj, 0, 1, mod_os_dupterm);
236-
#endif
237-
238219
STATIC const mp_rom_map_elem_t mp_module_os_globals_table[] = {
239220
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uos) },
240221
{ MP_ROM_QSTR(MP_QSTR_errno), MP_ROM_PTR(&mod_os_errno_obj) },
@@ -248,7 +229,7 @@ STATIC const mp_rom_map_elem_t mp_module_os_globals_table[] = {
248229
{ MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&mod_os_mkdir_obj) },
249230
{ MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&mod_os_ilistdir_obj) },
250231
#if MICROPY_PY_OS_DUPTERM
251-
{ MP_ROM_QSTR(MP_QSTR_dupterm), MP_ROM_PTR(&mod_os_dupterm_obj) },
232+
{ MP_ROM_QSTR(MP_QSTR_dupterm), MP_ROM_PTR(&mp_uos_dupterm_obj) },
252233
#endif
253234
};
254235

0 commit comments

Comments
 (0)