Skip to content

Commit 00d72f7

Browse files
committed
fix style of generated FrozenModules code
1 parent 8c02cff commit 00d72f7

File tree

2 files changed

+59
-77
lines changed

2 files changed

+59
-77
lines changed

graalpython/com.oracle.graal.python.frozen/freeze_modules.py

Lines changed: 51 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -441,82 +441,52 @@ def lower_camel_case(str):
441441
return str[0].lower() + str[1:] if str[1:] else ""
442442

443443

444-
# Adapted from PEP 257: strips a uniform amount of indentation from the lines of the multiline string.
445-
# This allows us to use multiline strings with pythonic identation which produce the same identation in the written file
446-
def trim(str):
447-
if not str:
448-
return ""
449-
# Convert tabs to spaces (following the normal Python rules)
450-
# and split into a list of lines:
451-
lines = str.expandtabs().splitlines()
452-
# Determine minimum indentation (first line doesn't count):
453-
indent = sys.maxsize
454-
for line in lines:
455-
456-
stripped = line.lstrip()
457-
if stripped:
458-
indent = min(indent, len(line) - len(stripped))
459-
# Remove indentation (first line is special):
460-
trimmed = []
461-
if indent < sys.maxsize:
462-
for line in lines:
463-
trimmed.append(line[indent:].rstrip())
464-
# Strip off trailing and leading blank lines:
465-
while trimmed and not trimmed[-1]:
466-
trimmed.pop()
467-
while trimmed and not trimmed[0]:
468-
trimmed.pop(0)
469-
# Return a single string:
470-
return "\n".join(trimmed)
471-
472-
473444
#############################################
474445
# write frozen files
475446

476-
FROZEN_MODULES_HEADER = """
477-
/*
478-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
479-
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
480-
*
481-
* The Universal Permissive License (UPL), Version 1.0
482-
*
483-
* Subject to the condition set forth below, permission is hereby granted to any
484-
* person obtaining a copy of this software, associated documentation and/or
485-
* data (collectively the "Software"), free of charge and under any and all
486-
* copyright rights in the Software, and any and all patent rights owned or
487-
* freely licensable by each licensor hereunder covering either (i) the
488-
* unmodified Software as contributed to or provided by such licensor, or (ii)
489-
* the Larger Works (as defined below), to deal in both
490-
*
491-
* (a) the Software, and
492-
*
493-
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
494-
* one is included with the Software each a "Larger Work" to which the Software
495-
* is contributed by such licensors),
496-
*
497-
* without restriction, including without limitation the rights to copy, create
498-
* derivative works of, display, perform, and distribute the Software and make,
499-
* use, sell, offer for sale, import, export, have made, and have sold the
500-
* Software and the Larger Work(s), and to sublicense the foregoing rights on
501-
* either these or other terms.
502-
*
503-
* This license is subject to the following condition:
504-
*
505-
* The above copyright notice and either this complete permission notice or at a
506-
* minimum a reference to the UPL must be included in all copies or substantial
507-
* portions of the Software.
508-
*
509-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
510-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
511-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
512-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
513-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
514-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
515-
* SOFTWARE.
516-
*/
517-
package com.oracle.graal.python.builtins.objects.module;
518-
519-
public final class FrozenModules {"""
447+
FROZEN_MODULES_HEADER = """/*
448+
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
449+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
450+
*
451+
* The Universal Permissive License (UPL), Version 1.0
452+
*
453+
* Subject to the condition set forth below, permission is hereby granted to any
454+
* person obtaining a copy of this software, associated documentation and/or
455+
* data (collectively the "Software"), free of charge and under any and all
456+
* copyright rights in the Software, and any and all patent rights owned or
457+
* freely licensable by each licensor hereunder covering either (i) the
458+
* unmodified Software as contributed to or provided by such licensor, or (ii)
459+
* the Larger Works (as defined below), to deal in both
460+
*
461+
* (a) the Software, and
462+
*
463+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
464+
* one is included with the Software each a "Larger Work" to which the Software
465+
* is contributed by such licensors),
466+
*
467+
* without restriction, including without limitation the rights to copy, create
468+
* derivative works of, display, perform, and distribute the Software and make,
469+
* use, sell, offer for sale, import, export, have made, and have sold the
470+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
471+
* either these or other terms.
472+
*
473+
* This license is subject to the following condition:
474+
*
475+
* The above copyright notice and either this complete permission notice or at a
476+
* minimum a reference to the UPL must be included in all copies or substantial
477+
* portions of the Software.
478+
*
479+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
480+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
481+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
482+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
483+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
484+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
485+
* SOFTWARE.
486+
*/
487+
package com.oracle.graal.python.builtins.objects.module;
488+
489+
public final class FrozenModules {"""
520490

521491
def freeze_module(src):
522492
with open(src.pyfile, "r") as src_file, open(src.binaryfile, "wb") as binary_file:
@@ -543,10 +513,13 @@ def write_frozen_lookup(out_file, modules):
543513
out_file.write(" switch (name) {\n")
544514
for module in modules:
545515
if module.source and (module.source.ispkg != module.ispkg):
546-
out_file.write(f' case "{module.name}": return Map.{module.symbol}.asPackage({"true" if module.ispkg else "false"});\n')
516+
out_file.write(f' case "{module.name}":\n')
517+
out_file.write(f' return Map.{module.symbol}.asPackage({"true" if module.ispkg else "false"});\n')
547518
else:
548-
out_file.write(f' case "{module.name}": return Map.{module.symbol};\n')
549-
out_file.write(" default: return null;\n")
519+
out_file.write(f' case "{module.name}":\n')
520+
out_file.write(f' return Map.{module.symbol};\n')
521+
out_file.write(" default:\n")
522+
out_file.write(" return null;\n")
550523
out_file.write(" }\n")
551524
out_file.write(" }\n")
552525

@@ -561,7 +534,7 @@ def write_frozen_module_file(file, modules):
561534
content = None
562535
os.makedirs(os.path.dirname(file), exist_ok=True)
563536
with open(file, "w") as out_file:
564-
out_file.write(trim(FROZEN_MODULES_HEADER))
537+
out_file.write(FROZEN_MODULES_HEADER)
565538
out_file.write("\n\n")
566539
write_frozen_modules_map(out_file, modules)
567540
out_file.write("\n")
@@ -575,6 +548,7 @@ def write_frozen_module_file(file, modules):
575548
os.utime(file, (atime, mtime))
576549
else:
577550
print(f"{file} modified, rebuild needed!")
551+
sys.exit(1)
578552

579553
def add_tabs(str, number):
580554
lines = str.splitlines()

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/module/FrozenModules.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
public final class FrozenModules {
4444

4545
private static final class Map {
46+
private static final PythonFrozenModule IMPORTLIB__BOOTSTRAP = new PythonFrozenModule("IMPORTLIB__BOOTSTRAP", "importlib._bootstrap", false);
47+
private static final PythonFrozenModule IMPORTLIB__BOOTSTRAP_EXTERNAL = new PythonFrozenModule("IMPORTLIB__BOOTSTRAP_EXTERNAL", "importlib._bootstrap_external", false);
4648
private static final PythonFrozenModule ABC = new PythonFrozenModule("ABC", "abc", false);
4749
private static final PythonFrozenModule CODECS = new PythonFrozenModule("CODECS", "codecs", false);
4850
private static final PythonFrozenModule ENCODINGS = new PythonFrozenModule("ENCODINGS", "encodings", true);
@@ -201,6 +203,10 @@ private static final class Map {
201203

202204
public static final PythonFrozenModule lookup(String name) {
203205
switch (name) {
206+
case "_frozen_importlib":
207+
return Map.IMPORTLIB__BOOTSTRAP;
208+
case "_frozen_importlib_external":
209+
return Map.IMPORTLIB__BOOTSTRAP_EXTERNAL;
204210
case "abc":
205211
return Map.ABC;
206212
case "codecs":
@@ -503,6 +509,8 @@ public static final PythonFrozenModule lookup(String name) {
503509
return Map.NTPATH;
504510
case "posixpath":
505511
return Map.POSIXPATH;
512+
case "os.path":
513+
return Map.POSIXPATH;
506514
case "os":
507515
return Map.OS;
508516
case "site":

0 commit comments

Comments
 (0)