Skip to content

Commit 1a24c15

Browse files
ilyass-elmazidielkorchi
authored andcommitted
[GR-68952] Backport to 25.0: Fix wrong line endings on windows when downloading patches
PullRequest: graalpython/3989
2 parents baca0dc + 81c2967 commit 1a24c15

File tree

14 files changed

+163
-95
lines changed

14 files changed

+163
-95
lines changed

graalpython/com.oracle.graal.python.pegparser.generator/main_parser_gen.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import os
4242
import sys
4343
import tokenize
44+
import io
4445

4546
from pegen.build import generate_token_definitions
4647
from pegen.grammar_parser import GeneratedParser as GrammarParser
@@ -71,34 +72,29 @@ def main():
7172

7273
# Determine which line separator to use
7374
if os.path.exists(args.output_file):
74-
stat_result = os.stat(args.output_file)
75-
atime, mtime = stat_result.st_atime, stat_result.st_mtime
76-
with open(args.output_file, "r", encoding="utf-8", newline=os.linesep) as f:
77-
content = f.read()
78-
if os.linesep != "\n":
79-
unix_content = content.replace(os.linesep, "\n")
80-
if unix_content == content:
81-
# Windows file has Unix line endings
82-
linesep = "\n"
83-
content = unix_content
75+
with open(args.output_file, "rb") as f:
76+
raw_old_content = f.read()
77+
if b'\r\n' in raw_old_content:
78+
linesep = '\r\n'
8479
else:
85-
linesep = os.linesep
86-
else:
87-
linesep = "\n"
80+
linesep = '\n'
81+
with open(args.output_file, "r", encoding="utf-8") as f:
82+
old_content = f.read()
8883
else:
89-
content = None
84+
old_content = None
9085
linesep = os.linesep
9186

92-
with open(args.output_file, "w", encoding="utf-8", newline=linesep) as file:
93-
gen = JavaParserGenerator(grammar, all_tokens, exact_tokens, non_exact_tokens, file, debug=args.debug)
94-
gen.generate(os.path.basename(args.grammar_file))
87+
out = io.StringIO()
88+
class_name = os.path.splitext(os.path.basename(args.output_file))[0]
89+
gen = JavaParserGenerator(grammar, all_tokens, exact_tokens, non_exact_tokens, out, class_name, debug=args.debug)
90+
gen.generate(os.path.basename(args.grammar_file))
9591

96-
with open(args.output_file, "r", encoding="utf-8", newline=linesep) as file:
97-
new_content = file.read()
98-
99-
if content == new_content:
92+
if out.getvalue() != old_content:
93+
print(f"Writing new {args.output_file}")
94+
with open(args.output_file, "w", encoding="utf-8", newline=linesep) as f:
95+
f.write(out.getvalue())
96+
else:
10097
print(f"{args.output_file} not modified")
101-
os.utime(args.output_file, (atime, mtime))
10298

10399

104100
if __name__ == '__main__':

graalpython/com.oracle.graal.python.pegparser.generator/pegjava/java_generator.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -874,10 +874,12 @@ def __init__(
874874
exact_tokens: Dict[str, int],
875875
non_exact_tokens: Set[str],
876876
file: Optional[IO[Text]],
877+
class_name: str,
877878
debug: bool = True,
878879
skip_actions: bool = False,
879880
):
880881
super().__init__(grammar, set(tokens.values()), file)
882+
self.class_name = class_name
881883
self.typingvisitor = TypingVisitor(self) # Java type hack
882884
self.callmakervisitor = JavaCallMakerVisitor(self, exact_tokens, non_exact_tokens, self.print)
883885
self.lookahead_functions: Dict[str, FunctionCall] = {}
@@ -930,10 +932,9 @@ def generate(self, filename: str) -> None:
930932
self.print(f"// Generated from {filename} by pegen")
931933
self.print("package com.oracle.graal.python.pegparser;")
932934
self.print(IMPORTS)
933-
className = os.path.splitext(os.path.basename(self.file.name))[0]
934935
self.print('@SuppressWarnings({"all", "cast"})')
935936
self.print('@SuppressFBWarnings')
936-
self.print("public final class %s extends AbstractParser {" % className)
937+
self.print("public final class %s extends AbstractParser {" % self.class_name)
937938
# Java needs a few fields declarations. Also, we're now in a class
938939
self.level += 1
939940
self.print()
@@ -945,12 +946,12 @@ def generate(self, filename: str) -> None:
945946
self.print(f"private static final int {rulename.upper()}_ID = {i};{comment}")
946947
self.print()
947948
# Java needs a constructor
948-
self.print("public %s(String source, SourceRange sourceRange, ParserCallbacks parserCb, InputType startRule, EnumSet<Flags> flags, int featureVersion) {" % className)
949+
self.print("public %s(String source, SourceRange sourceRange, ParserCallbacks parserCb, InputType startRule, EnumSet<Flags> flags, int featureVersion) {" % self.class_name)
949950
with self.indent():
950951
self.print("super(source, sourceRange, parserCb, startRule, flags, featureVersion);")
951952
self.print("}")
952953
self.print()
953-
self.print("public %s(String source, ParserCallbacks parserCb, InputType startRule, EnumSet<Flags> flags, int featureVersion) {" % className)
954+
self.print("public %s(String source, ParserCallbacks parserCb, InputType startRule, EnumSet<Flags> flags, int featureVersion) {" % self.class_name)
954955
with self.indent():
955956
self.print("super(source, null, parserCb, startRule, flags, featureVersion);")
956957
self.print("}")

graalpython/com.oracle.graal.python.test.integration/src/com/oracle/graal/python/test/integration/interop/JavaInteropTest.java

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848

4949
import java.io.ByteArrayOutputStream;
5050
import java.io.IOException;
51-
import java.io.UnsupportedEncodingException;
5251
import java.math.BigInteger;
5352
import java.nio.charset.StandardCharsets;
5453
import java.util.ArrayList;
@@ -107,6 +106,14 @@ public void tearDown() {
107106
context.getEngine().close();
108107
}
109108

109+
private String getOutString() {
110+
return out.toString(StandardCharsets.UTF_8).replaceAll("\r\n", "\n");
111+
}
112+
113+
private String getErrString() {
114+
return err.toString(StandardCharsets.UTF_8).replaceAll("\r\n", "\n");
115+
}
116+
110117
@Test
111118
public void evalFailsOnError() {
112119
boolean didFail = false;
@@ -217,8 +224,8 @@ public void testHostException() {
217224
Assert.assertTrue(e.getMessage(), e.getMessage().contains("divide by zero"));
218225
}
219226

220-
String outString = out.toString(StandardCharsets.UTF_8);
221-
String errString = err.toString(StandardCharsets.UTF_8);
227+
String outString = getOutString();
228+
String errString = getErrString();
222229
Assert.assertTrue(outString, outString.isEmpty());
223230
Assert.assertTrue(errString, errString.isEmpty());
224231
}
@@ -257,7 +264,7 @@ def is_none(x):
257264
}
258265

259266
@Test
260-
public void testPassingFloats() throws UnsupportedEncodingException {
267+
public void testPassingFloats() {
261268
String source = "import polyglot\n" +
262269
"@polyglot.export_value\n" +
263270
"def foo(x, y):\n" +
@@ -266,11 +273,11 @@ public void testPassingFloats() throws UnsupportedEncodingException {
266273
context.eval(script);
267274
Value main = context.getPolyglotBindings().getMember("foo");
268275
main.execute((float) 1.0, (float) 2.0);
269-
assertEquals("2.0\n", out.toString("UTF-8"));
276+
assertEquals("2.0\n", getOutString());
270277
}
271278

272279
@Test
273-
public void testPassingBigIntegers() throws UnsupportedEncodingException {
280+
public void testPassingBigIntegers() {
274281
String source = "import polyglot\n" +
275282
"@polyglot.export_value\n" +
276283
"def foo(x, y):\n" +
@@ -279,20 +286,20 @@ public void testPassingBigIntegers() throws UnsupportedEncodingException {
279286
context.eval(script);
280287
Value main = context.getPolyglotBindings().getMember("foo");
281288
main.execute(BigInteger.valueOf(Long.MAX_VALUE).multiply(BigInteger.TWO), BigInteger.valueOf(7));
282-
assertEquals("129127208515966861298\n", out.toString("UTF-8"));
289+
assertEquals("129127208515966861298\n", getOutString());
283290
out.reset();
284291
main.execute(Long.MAX_VALUE, 14);
285-
assertEquals("129127208515966861298\n", out.toString("UTF-8"));
292+
assertEquals("129127208515966861298\n", getOutString());
286293
out.reset();
287294
main.execute(6, 7);
288-
assertEquals("42\n", out.toString("UTF-8"));
295+
assertEquals("42\n", getOutString());
289296
out.reset();
290297
main.execute(true, true);
291-
assertEquals("1\n", out.toString("UTF-8"));
298+
assertEquals("1\n", getOutString());
292299
}
293300

294301
@Test
295-
public void testBigIntegersAdd() throws UnsupportedEncodingException {
302+
public void testBigIntegersAdd() {
296303
String source = "import polyglot\n" +
297304
"@polyglot.export_value\n" +
298305
"def foo(x, y):\n" +
@@ -301,20 +308,20 @@ public void testBigIntegersAdd() throws UnsupportedEncodingException {
301308
context.eval(script);
302309
Value main = context.getPolyglotBindings().getMember("foo");
303310
main.execute(BigInteger.valueOf(24).shiftLeft(101), BigInteger.valueOf(7));
304-
assertEquals("60847228810955011271841753858055\n", out.toString("UTF-8"));
311+
assertEquals("60847228810955011271841753858055\n", getOutString());
305312
out.reset();
306313
main.execute(BigInteger.valueOf(24).shiftLeft(101), BigInteger.valueOf(24).shiftLeft(101));
307-
assertEquals("121694457621910022543683507716096\n", out.toString("UTF-8"));
314+
assertEquals("121694457621910022543683507716096\n", getOutString());
308315
out.reset();
309316
main.execute(6, 7);
310-
assertEquals("13\n", out.toString("UTF-8"));
317+
assertEquals("13\n", getOutString());
311318
out.reset();
312319
main.execute(true, true);
313-
assertEquals("2\n", out.toString("UTF-8"));
320+
assertEquals("2\n", getOutString());
314321
}
315322

316323
@Test
317-
public void testBigIntegersEg() throws UnsupportedEncodingException {
324+
public void testBigIntegersEg() {
318325
String source = "import polyglot\n" +
319326
"@polyglot.export_value\n" +
320327
"def foo(x, y):\n" +
@@ -323,26 +330,26 @@ public void testBigIntegersEg() throws UnsupportedEncodingException {
323330
context.eval(script);
324331
Value main = context.getPolyglotBindings().getMember("foo");
325332
main.execute(BigInteger.valueOf(24).shiftLeft(101), BigInteger.valueOf(7));
326-
assertEquals("False\n", out.toString("UTF-8"));
333+
assertEquals("False\n", getOutString());
327334
out.reset();
328335
main.execute(BigInteger.valueOf(24).shiftLeft(101), BigInteger.valueOf(24).shiftLeft(101));
329-
assertEquals("True\n", out.toString("UTF-8"));
336+
assertEquals("True\n", getOutString());
330337
out.reset();
331338
main.execute(6, 7);
332-
assertEquals("False\n", out.toString("UTF-8"));
339+
assertEquals("False\n", getOutString());
333340
out.reset();
334341
main.execute(6, 6);
335-
assertEquals("True\n", out.toString("UTF-8"));
342+
assertEquals("True\n", getOutString());
336343
out.reset();
337344
main.execute(true, BigInteger.ONE);
338-
assertEquals("True\n", out.toString("UTF-8"));
345+
assertEquals("True\n", getOutString());
339346
out.reset();
340347
main.execute(true, BigInteger.ZERO);
341-
assertEquals("False\n", out.toString("UTF-8"));
348+
assertEquals("False\n", getOutString());
342349
}
343350

344351
@Test
345-
public void testAsFunction() throws UnsupportedEncodingException {
352+
public void testAsFunction() {
346353
String source = "import polyglot\n" +
347354
"@polyglot.export_value\n" +
348355
"def foo():\n" +
@@ -351,11 +358,11 @@ public void testAsFunction() throws UnsupportedEncodingException {
351358
context.eval(script);
352359
Value main = context.getPolyglotBindings().getMember("foo");
353360
main.execute();
354-
assertEquals("Called\n", out.toString("UTF-8"));
361+
assertEquals("Called\n", getOutString());
355362
}
356363

357364
@Test
358-
public void testAsFunctionVarArgs() throws UnsupportedEncodingException {
365+
public void testAsFunctionVarArgs() {
359366
String source = "import polyglot\n" +
360367
"@polyglot.export_value\n" +
361368
"def foo(a, b):\n" +
@@ -364,28 +371,28 @@ public void testAsFunctionVarArgs() throws UnsupportedEncodingException {
364371
context.eval(script);
365372
Value main = context.getPolyglotBindings().getMember("foo");
366373
main.execute("Hello", "World");
367-
assertEquals("Hello World\n", out.toString("UTF-8"));
374+
assertEquals("Hello World\n", getOutString());
368375
}
369376

370377
@Test
371-
public void mainFunctionsAreImplicitlyImporteable() throws UnsupportedEncodingException {
378+
public void mainFunctionsAreImplicitlyImporteable() {
372379
String source = "def foo(a, b):\n" +
373380
" print(a, b)\n\n";
374381
Source script = Source.create("python", source);
375382
context.eval(script);
376383
Value main = context.getBindings("python").getMember("foo");
377384
main.execute("Hello", "World");
378-
assertEquals("Hello World\n", out.toString("UTF-8"));
385+
assertEquals("Hello World\n", getOutString());
379386
}
380387

381388
@Test
382-
public void builtinFunctionsAreImporteable() throws UnsupportedEncodingException {
389+
public void builtinFunctionsAreImporteable() {
383390
String source = "pass";
384391
Source script = Source.create("python", source);
385392
context.eval(script);
386393
Value main = context.getBindings("python").getMember("__builtins__").getMember("print");
387394
main.execute("Hello", "World");
388-
assertEquals("Hello World\n", out.toString("UTF-8"));
395+
assertEquals("Hello World\n", getOutString());
389396
}
390397

391398
@Test
@@ -394,22 +401,22 @@ public void enumeratingMainBindingsWorks() throws Exception {
394401
}
395402

396403
@Test
397-
public void testMultipleInvocationsAreInSameScope() throws UnsupportedEncodingException {
404+
public void testMultipleInvocationsAreInSameScope() {
398405
String source = "def foo(a, b):\n" +
399406
" print(a, b)\n" +
400407
"foo";
401408
Source script = Source.create("python", source);
402409
Value foo = context.eval(script);
403410
foo.execute("Hello", "World");
404-
assertEquals("Hello World\n", out.toString("UTF-8"));
411+
assertEquals("Hello World\n", getOutString());
405412

406413
source = "def bar(a, b):\n" +
407414
" foo(a, b)\n" +
408415
"bar";
409416
script = Source.create("python", source);
410417
Value bar = context.eval(script);
411418
bar.execute("Hello", "World");
412-
assertEquals("Hello World\nHello World\n", out.toString("UTF-8"));
419+
assertEquals("Hello World\nHello World\n", getOutString());
413420

414421
source = "invalid syntax";
415422
script = Source.create("python", source);
@@ -418,9 +425,9 @@ public void testMultipleInvocationsAreInSameScope() throws UnsupportedEncodingEx
418425
} catch (Throwable t) {
419426
}
420427
bar.execute("Hello", "World");
421-
assertEquals("Hello World\nHello World\nHello World\n", out.toString("UTF-8"));
428+
assertEquals("Hello World\nHello World\nHello World\n", getOutString());
422429
foo.execute("Hello", "World");
423-
assertEquals("Hello World\nHello World\nHello World\nHello World\n", out.toString("UTF-8"));
430+
assertEquals("Hello World\nHello World\nHello World\nHello World\n", getOutString());
424431
}
425432

426433
@Test
@@ -568,7 +575,7 @@ public void accessJavaObjectFields() throws IOException {
568575
"5.0 <class 'float'>\n" +
569576
"6.0 <class 'float'>\n" +
570577
"True <class 'bool'>\n" +
571-
"c <class 'polyglot.ForeignString'>\n", out.toString("UTF-8"));
578+
"c <class 'polyglot.ForeignString'>\n", getOutString());
572579
}
573580

574581
@Test
@@ -595,7 +602,7 @@ public void accessJavaObjectGetters() throws IOException {
595602
"5.0 <class 'float'>\n" +
596603
"6.0 <class 'float'>\n" +
597604
"True <class 'bool'>\n" +
598-
"c <class 'polyglot.ForeignString'>\n", out.toString("UTF-8"));
605+
"c <class 'polyglot.ForeignString'>\n", getOutString());
599606
}
600607

601608
@Test
@@ -612,7 +619,7 @@ public void javaStringsAndPythonStrings() throws IOException {
612619
assertEquals("" +
613620
"<class 'polyglot.ForeignDict'>\n" +
614621
"True\n" +
615-
"True\n", out.toString("UTF-8"));
622+
"True\n", getOutString());
616623
}
617624

618625
@Test

graalpython/com.oracle.graal.python.test/src/runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ def parse_config(cls, config_path: Path):
899899
if config_tags_dir := settings.get('tags_dir'):
900900
tags_dir = (config_path.parent / config_tags_dir).resolve()
901901
# Temporary hack for Bytecode DSL development in master branch:
902-
if IS_GRAALPY and __graalpython__.is_bytecode_dsl_interpreter and tags_dir:
902+
if IS_GRAALPY and getattr(__graalpython__, 'is_bytecode_dsl_interpreter', False) and tags_dir:
903903
new_tags_dir = (config_path.parent / (config_tags_dir + '_bytecode_dsl')).resolve()
904904
if new_tags_dir.exists():
905905
tags_dir = new_tags_dir

graalpython/com.oracle.graal.python.test/src/tests/conftest.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ partial_splits_individual_tests = true
2222
exclude_on = ['win32']
2323
selector = [
2424
"test_multiprocessing_graalpy.py", # import _winapi
25-
"test_patched_pip.py",
2625
"test_pathlib.py",
2726
"test_posix.py", # import posix
2827
"test_pyio.py", # pyio imports msvcrt

0 commit comments

Comments
 (0)