98
98
import org .graalvm .options .OptionKey ;
99
99
100
100
import com .oracle .graal .python .PythonLanguage ;
101
+ import com .oracle .graal .python .PythonResource ;
101
102
import com .oracle .graal .python .builtins .Python3Core ;
102
103
import com .oracle .graal .python .builtins .PythonOS ;
103
104
import com .oracle .graal .python .builtins .modules .ImpModuleBuiltins ;
@@ -1678,7 +1679,7 @@ private void initializePosixSupport() {
1678
1679
}
1679
1680
}
1680
1681
1681
- private TruffleString sysPrefix , basePrefix , coreHome , capiHome , jniHome , stdLibHome ;
1682
+ private TruffleString langHome , sysPrefix , basePrefix , coreHome , capiHome , jniHome , stdLibHome ;
1682
1683
1683
1684
public void initializeHomeAndPrefixPaths (Env newEnv , String languageHome ) {
1684
1685
sysPrefix = newEnv .getOptions ().get (PythonOptions .SysPrefix );
@@ -1721,25 +1722,44 @@ public void initializeHomeAndPrefixPaths(Env newEnv, String languageHome) {
1721
1722
home = null ;
1722
1723
}
1723
1724
1724
- if (home != null ) {
1725
+ Supplier <?>[] homeCandidates = new Supplier <?>[] {
1726
+ () -> { return home ; },
1727
+ () -> {
1728
+ try {
1729
+ return newEnv .getInternalResource (PythonResource .class ).getAbsoluteFile ();
1730
+ } catch (IOException e ) {
1731
+ return null ;
1732
+ }
1733
+ }
1734
+ };
1735
+ for (Supplier <?> homeCandidateSupplier : homeCandidates ) {
1736
+ final TruffleFile homeCandidate = (TruffleFile ) homeCandidateSupplier .get ();
1737
+ if (homeCandidate == null ) {
1738
+ continue ;
1739
+ }
1740
+ boolean homeSeemsValid = !coreHome .isEmpty () && !stdLibHome .isEmpty ();
1741
+
1742
+ langHome = toTruffleStringUncached (homeCandidate .toString ());
1725
1743
if (sysPrefix .isEmpty ()) {
1726
- sysPrefix = toTruffleStringUncached (home .getAbsoluteFile ().getPath ());
1744
+ sysPrefix = toTruffleStringUncached (homeCandidate .getAbsoluteFile ().getPath ());
1727
1745
}
1728
1746
1729
1747
if (basePrefix .isEmpty ()) {
1730
- basePrefix = toTruffleStringUncached (home .getAbsoluteFile ().getPath ());
1748
+ basePrefix = toTruffleStringUncached (homeCandidate .getAbsoluteFile ().getPath ());
1731
1749
}
1732
1750
1733
1751
if (coreHome .isEmpty ()) {
1734
1752
try {
1735
- outer : for (TruffleFile f : home .list ()) {
1753
+ outer : for (TruffleFile f : homeCandidate .list ()) {
1736
1754
if (f .getName ().equals ("lib-graalpython" ) && f .isDirectory ()) {
1737
1755
coreHome = toTruffleStringUncached (f .getPath ());
1756
+ homeSeemsValid = true ;
1738
1757
break ;
1739
1758
} else if (f .getName ().equals ("lib" ) && f .isDirectory ()) {
1740
1759
for (TruffleFile f2 : f .list ()) {
1741
1760
if (f2 .getName ().equals ("graalpy" + PythonLanguage .GRAALVM_MAJOR + "." + PythonLanguage .GRAALVM_MINOR ) && f .isDirectory ()) {
1742
1761
coreHome = toTruffleStringUncached (f2 .getPath ());
1762
+ homeSeemsValid = true ;
1743
1763
break outer ;
1744
1764
}
1745
1765
}
@@ -1752,7 +1772,7 @@ public void initializeHomeAndPrefixPaths(Env newEnv, String languageHome) {
1752
1772
if (stdLibHome .isEmpty ()) {
1753
1773
// try stdlib layouts per sysconfig or our sources
1754
1774
try {
1755
- outer : for (TruffleFile f : home .list ()) {
1775
+ outer : for (TruffleFile f : homeCandidate .list ()) {
1756
1776
if (getPythonOS () == PLATFORM_WIN32 && (f .getName ().equals ("Lib" ) || f .getName ().equals ("lib" )) && f .isDirectory ()) {
1757
1777
// nt stdlib layout
1758
1778
stdLibHome = toTruffleStringUncached (f .getPath ());
@@ -1761,6 +1781,7 @@ public void initializeHomeAndPrefixPaths(Env newEnv, String languageHome) {
1761
1781
for (TruffleFile f2 : f .list ()) {
1762
1782
if (f2 .getName ().equals ("python" + PythonLanguage .MAJOR + "." + PythonLanguage .MINOR ) && f .isDirectory ()) {
1763
1783
stdLibHome = toTruffleStringUncached (f2 .getPath ());
1784
+ homeSeemsValid = true ;
1764
1785
break outer ;
1765
1786
}
1766
1787
}
@@ -1769,6 +1790,7 @@ public void initializeHomeAndPrefixPaths(Env newEnv, String languageHome) {
1769
1790
for (TruffleFile f2 : f .list ()) {
1770
1791
if (f2 .getName ().equals ("3" ) && f .isDirectory ()) {
1771
1792
stdLibHome = toTruffleStringUncached (f2 .getPath ());
1793
+ homeSeemsValid = true ;
1772
1794
break outer ;
1773
1795
}
1774
1796
}
@@ -1785,6 +1807,18 @@ public void initializeHomeAndPrefixPaths(Env newEnv, String languageHome) {
1785
1807
if (jniHome .isEmpty ()) {
1786
1808
jniHome = coreHome ;
1787
1809
}
1810
+
1811
+ if (homeSeemsValid ) {
1812
+ break ;
1813
+ } else {
1814
+ // reset values
1815
+ sysPrefix = newEnv .getOptions ().get (PythonOptions .SysPrefix );
1816
+ basePrefix = newEnv .getOptions ().get (PythonOptions .SysBasePrefix );
1817
+ coreHome = newEnv .getOptions ().get (PythonOptions .CoreHome );
1818
+ stdLibHome = newEnv .getOptions ().get (PythonOptions .StdLibHome );
1819
+ capiHome = newEnv .getOptions ().get (PythonOptions .CAPI );
1820
+ jniHome = newEnv .getOptions ().get (PythonOptions .JNIHome );
1821
+ }
1788
1822
}
1789
1823
1790
1824
if (ImageInfo .inImageBuildtimeCode ()) {
@@ -1810,10 +1844,18 @@ public void initializeHomeAndPrefixPaths(Env newEnv, String languageHome) {
1810
1844
"\n \t StdLibHome: {4}" +
1811
1845
"\n \t Executable: {5}" +
1812
1846
"\n \t CAPI: {6}" +
1813
- "\n \t JNI library: {7}" , home != null ? home . getPath () : "" , sysPrefix , basePrefix , coreHome , stdLibHome , newEnv .getOptions ().get (PythonOptions .Executable ), capiHome ,
1847
+ "\n \t JNI library: {7}" , langHome , sysPrefix , basePrefix , coreHome , stdLibHome , newEnv .getOptions ().get (PythonOptions .Executable ), capiHome ,
1814
1848
jniHome ));
1815
1849
}
1816
1850
1851
+ @ TruffleBoundary
1852
+ public TruffleString getLanguageHome () {
1853
+ if (langHome == null || langHome .isEmpty ()) {
1854
+ langHome = T_PREFIX ;
1855
+ }
1856
+ return langHome ;
1857
+ }
1858
+
1817
1859
@ TruffleBoundary
1818
1860
public TruffleString getSysPrefix () {
1819
1861
if (sysPrefix .isEmpty ()) {
@@ -1826,13 +1868,7 @@ public TruffleString getSysPrefix() {
1826
1868
@ TruffleBoundary
1827
1869
public TruffleString getSysBasePrefix () {
1828
1870
if (basePrefix .isEmpty ()) {
1829
- String homePrefix = getLanguage ().getHome ();
1830
- if (homePrefix == null || homePrefix .isEmpty ()) {
1831
- basePrefix = T_PREFIX ;
1832
- } else {
1833
- basePrefix = toTruffleStringUncached (homePrefix );
1834
- }
1835
-
1871
+ basePrefix = getLanguageHome ();
1836
1872
}
1837
1873
return basePrefix ;
1838
1874
}
@@ -2294,15 +2330,13 @@ private static boolean hasAllowedSuffix(TruffleString path, TruffleString[] allo
2294
2330
@ TruffleBoundary
2295
2331
public boolean isPyFileInLanguageHome (TruffleFile path ) {
2296
2332
assert !ImageInfo .inImageBuildtimeCode () : "language home won't be available during image build time" ;
2297
- String languageHome = getLanguage ().getHome ();
2298
-
2299
2333
// The language home may be 'null' if an embedder uses Python. In this case, IO must just be
2300
2334
// allowed.
2301
- if (languageHome != null ) {
2335
+ if (langHome != null ) {
2302
2336
// This deliberately uses 'getAbsoluteFile' and not 'getCanonicalFile' because if, e.g.,
2303
2337
// 'path' is a symlink outside of the language home, the user should not be able to read
2304
2338
// the symlink if 'allowIO' is false.
2305
- TruffleFile coreHomePath = env .getInternalTruffleFile (languageHome ).getAbsoluteFile ();
2339
+ TruffleFile coreHomePath = getEnv () .getInternalTruffleFile (langHome . toJavaStringUncached () ).getAbsoluteFile ();
2306
2340
TruffleFile absolutePath = path .getAbsoluteFile ();
2307
2341
return absolutePath .startsWith (coreHomePath );
2308
2342
}
0 commit comments