@@ -43,33 +43,12 @@ def reformat(source):
43
43
class Tests (unittest .TestCase ):
44
44
maxDiff = 80 * 30
45
45
46
- def _test_patch_file (self , tmp_dir ):
47
- # test Patcher.patcher()
48
- source = """
49
- PyTypeObject*
50
- test_type(PyObject *obj, PyTypeObject *type)
51
- {
52
- Py_TYPE(obj) = type;
53
- return Py_TYPE(obj);
54
- }
55
- """
56
- expected = """
57
- #include "pythoncapi_compat.h"
58
-
59
- PyTypeObject*
60
- test_type(PyObject *obj, PyTypeObject *type)
61
- {
62
- Py_SET_TYPE(obj, type);
63
- return Py_TYPE(obj);
64
- }
65
- """
66
- source = reformat (source )
67
- expected = reformat (expected )
68
-
46
+ def _patch_file (self , source , tmp_dir = None ):
47
+ # test Patcher.patcher()
69
48
filename = tempfile .mktemp (suffix = '.c' , dir = tmp_dir )
70
49
old_filename = filename + ".old"
71
50
try :
72
- with open (filename , "w" , encoding = "utf-8" ) as fp :
51
+ with open (filename , "w" , encoding = "utf-8" , newline = "" ) as fp :
73
52
fp .write (source )
74
53
75
54
old_stderr = sys .stderr
@@ -93,10 +72,10 @@ def _test_patch_file(self, tmp_dir):
93
72
sys .stderr = old_stderr
94
73
sys .argv = old_argv
95
74
96
- with open (filename , encoding = "utf-8" ) as fp :
75
+ with open (filename , encoding = "utf-8" , newline = "" ) as fp :
97
76
new_contents = fp .read ()
98
77
99
- with open (old_filename , encoding = "utf-8" ) as fp :
78
+ with open (old_filename , encoding = "utf-8" , newline = "" ) as fp :
100
79
old_contents = fp .read ()
101
80
finally :
102
81
try :
@@ -108,15 +87,53 @@ def _test_patch_file(self, tmp_dir):
108
87
except FileNotFoundError :
109
88
pass
110
89
111
- self .assertEqual (new_contents , expected )
112
90
self .assertEqual (old_contents , source )
91
+ return new_contents
113
92
114
93
def test_patch_file (self ):
115
- self ._test_patch_file (None )
94
+ source = """
95
+ PyTypeObject*
96
+ test_type(PyObject *obj, PyTypeObject *type)
97
+ {
98
+ Py_TYPE(obj) = type;
99
+ return Py_TYPE(obj);
100
+ }
101
+ """
102
+ expected = """
103
+ #include "pythoncapi_compat.h"
104
+
105
+ PyTypeObject*
106
+ test_type(PyObject *obj, PyTypeObject *type)
107
+ {
108
+ Py_SET_TYPE(obj, type);
109
+ return Py_TYPE(obj);
110
+ }
111
+ """
112
+ source = reformat (source )
113
+ expected = reformat (expected )
114
+
115
+ new_contents = self ._patch_file (source )
116
+ self .assertEqual (new_contents , expected )
116
117
117
- def test_patch_directory (self ):
118
118
with tempfile .TemporaryDirectory () as tmp_dir :
119
- self ._test_patch_file (tmp_dir )
119
+ new_contents = self ._patch_file (source , tmp_dir )
120
+ self .assertEqual (new_contents , expected )
121
+
122
+ def test_patch_file_preserve_newlines (self ):
123
+ source = """
124
+ Py_ssize_t get_size(PyVarObject *obj)\r \n \
125
+ \n \
126
+ { return obj->ob_size; }\r \
127
+ """
128
+ expected = """
129
+ Py_ssize_t get_size(PyVarObject *obj)\r \n \
130
+ \n \
131
+ { return Py_SIZE(obj); }\r \
132
+ """
133
+ source = reformat (source )
134
+ expected = reformat (expected )
135
+ new_contents = self ._patch_file (source )
136
+ self .assertEqual (new_contents , expected )
120
137
121
138
def check_replace (self , source , expected , ** kwargs ):
122
139
source = reformat (source )
0 commit comments