Skip to content

Commit 9bdb82e

Browse files
stinospfalcon
authored andcommitted
mpy-cross: Use binary file translation mode for creating mpy files on windows
This is a fix for micropython#2209: by default a file created using open() uses text translation mode so writing \n to it will result in the file having \r\n. This is obviously problematic for binary .mpy files, so provide functions for setting the open mode and use binary mode in mpy-cross' main().
1 parent 4347337 commit 9bdb82e

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

mpy-cross/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ SRC_C = \
4949
main.c \
5050
gccollect.c \
5151

52+
ifeq ($(OS),Windows_NT)
53+
SRC_C += windows/fmode.c
54+
endif
55+
5256
OBJ = $(PY_O)
5357
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
5458

mpy-cross/main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
#include "py/runtime.h"
3636
#include "py/gc.h"
3737
#include "py/stackctrl.h"
38+
#ifdef _WIN32
39+
#include "windows/fmode.h"
40+
#endif
3841

3942
// Command line options, with their defaults
4043
STATIC uint emit_opt = MP_EMIT_OPT_NONE;
@@ -185,6 +188,9 @@ MP_NOINLINE int main_(int argc, char **argv) {
185188
gc_init(heap, heap + heap_size);
186189

187190
mp_init();
191+
#ifdef _WIN32
192+
set_fmode_binary();
193+
#endif
188194
mp_obj_list_init(mp_sys_path, 0);
189195
mp_obj_list_init(mp_sys_argv, 0);
190196

windows/fmode.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013-2016 Damien P. George
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 "fmode.h"
28+
#include "py/mpconfig.h"
29+
#include <fcntl.h>
30+
#include <stdlib.h>
31+
32+
// Workaround for setting file translation mode: we must distinguish toolsets
33+
// since mingw has no _set_fmode, and altering msvc's _fmode directly has no effect
34+
STATIC int set_fmode_impl(int mode) {
35+
#ifndef _MSC_VER
36+
_fmode = mode;
37+
return 0;
38+
#else
39+
return _set_fmode(mode);
40+
#endif
41+
}
42+
43+
void set_fmode_binary(void) {
44+
set_fmode_impl(O_BINARY);
45+
}
46+
47+
void set_fmode_text(void) {
48+
set_fmode_impl(O_TEXT);
49+
}

windows/fmode.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013-2016 Damien P. George
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+
#ifndef __MICROPY_INCLUDED_WINDOWS_FMODE_H__
28+
#define __MICROPY_INCLUDED_WINDOWS_FMODE_H__
29+
30+
// Treat files opened by open() as binary. No line ending translation is done.
31+
void set_fmode_binary(void);
32+
33+
// Treat files opened by open() as text.
34+
// When reading from the file \r\n will be converted to \n.
35+
// When writing to the file \n will be converted into \r\n.
36+
void set_fmode_text(void);
37+
38+
#endif

0 commit comments

Comments
 (0)