|
45 | 45 | import java.util.ArrayList;
|
46 | 46 | import java.util.Arrays;
|
47 | 47 | import java.util.Collections;
|
| 48 | +import java.util.LinkedHashMap; |
48 | 49 | import java.util.List;
|
49 | 50 | import java.util.Map;
|
50 | 51 | import java.util.Map.Entry;
|
|
68 | 69 | import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAcquireLibrary;
|
69 | 70 | import com.oracle.graal.python.builtins.objects.bytes.BytesNodes;
|
70 | 71 | import com.oracle.graal.python.builtins.objects.bytes.PBytes;
|
71 |
| -import com.oracle.graal.python.builtins.objects.common.EconomicMapStorage; |
72 |
| -import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes; |
73 | 72 | import com.oracle.graal.python.builtins.objects.common.SequenceNodes.LenNode;
|
74 | 73 | import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
|
75 | 74 | import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GetItemNode;
|
|
92 | 91 | import com.oracle.graal.python.lib.PyNumberIndexNode;
|
93 | 92 | import com.oracle.graal.python.lib.PyOSFSPathNode;
|
94 | 93 | import com.oracle.graal.python.lib.PyObjectAsFileDescriptor;
|
| 94 | +import com.oracle.graal.python.lib.PyObjectGetAttr; |
| 95 | +import com.oracle.graal.python.lib.PyObjectGetItem; |
95 | 96 | import com.oracle.graal.python.lib.PyObjectSizeNode;
|
96 | 97 | import com.oracle.graal.python.lib.PyUnicodeCheckNode;
|
97 | 98 | import com.oracle.graal.python.nodes.ErrorMessages;
|
|
101 | 102 | import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
|
102 | 103 | import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
|
103 | 104 | import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
|
| 105 | +import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode; |
104 | 106 | import com.oracle.graal.python.nodes.function.builtins.PythonBinaryClinicBuiltinNode;
|
105 | 107 | import com.oracle.graal.python.nodes.function.builtins.PythonClinicBuiltinNode;
|
106 | 108 | import com.oracle.graal.python.nodes.function.builtins.PythonTernaryClinicBuiltinNode;
|
@@ -252,7 +254,21 @@ public void initialize(Python3Core core) {
|
252 | 254 | PythonLanguage language = core.getLanguage();
|
253 | 255 | addBuiltinConstant("_have_functions", PFactory.createList(language, haveFunctions.toArray()));
|
254 | 256 | addBuiltinConstant("environ", PFactory.createDict(language));
|
255 |
| - addBuiltinConstant("sysconf_names", PFactory.createDict(language)); |
| 257 | + |
| 258 | + LinkedHashMap<String, Object> sysconfigNames = new LinkedHashMap<>(); |
| 259 | + for (IntConstant name : PosixConstants.sysconfigNames) { |
| 260 | + if (name.defined) { |
| 261 | + // add the constant without the leading underscore |
| 262 | + String pythonName; |
| 263 | + if (name.name.startsWith("_")) { |
| 264 | + pythonName = name.name.substring(1); |
| 265 | + } else { |
| 266 | + pythonName = name.name; |
| 267 | + } |
| 268 | + sysconfigNames.put(pythonName, name.getValueIfDefined()); |
| 269 | + } |
| 270 | + } |
| 271 | + addBuiltinConstant("sysconf_names", PFactory.createDictFromMap(language, sysconfigNames)); |
256 | 272 |
|
257 | 273 | StructSequence.initType(core, STAT_RESULT_DESC);
|
258 | 274 | StructSequence.initType(core, STATVFS_RESULT_DESC);
|
@@ -346,9 +362,6 @@ public void postInitialize(Python3Core core) {
|
346 | 362 | Object environAttr = posix.getAttribute(tsLiteral("environ"));
|
347 | 363 | ((PDict) environAttr).setDictStorage(environ.getDictStorage());
|
348 | 364 |
|
349 |
| - PDict sysconfNamesAttr = (PDict) posix.getAttribute(tsLiteral("sysconf_names")); |
350 |
| - sysconfNamesAttr.setDictStorage(HashingStorageNodes.HashingStorageCopy.executeUncached(SysconfNode.SYSCONF_NAMES)); |
351 |
| - |
352 | 365 | if (posixLib.getBackend(posixSupport).toJavaStringUncached().equals("java")) {
|
353 | 366 | posix.setAttribute(toTruffleStringUncached("statvfs"), PNone.NO_VALUE);
|
354 | 367 | posix.setAttribute(toTruffleStringUncached("geteuid"), PNone.NO_VALUE);
|
@@ -2863,50 +2876,43 @@ static int getCpuCount() {
|
2863 | 2876 | }
|
2864 | 2877 | }
|
2865 | 2878 |
|
2866 |
| - @Builtin(name = "sysconf", minNumOfPositionalArgs = 1, parameterNames = {"name"}) |
| 2879 | + @Builtin(name = "sysconf", minNumOfPositionalArgs = 2, parameterNames = {"$self", "name"}, declaresExplicitSelf = true) |
2867 | 2880 | @GenerateNodeFactory
|
2868 |
| - abstract static class SysconfNode extends PythonUnaryBuiltinNode { |
| 2881 | + abstract static class SysconfNode extends PythonBinaryBuiltinNode { |
2869 | 2882 |
|
2870 |
| - public static final TruffleString T_SC_CLK_TCK = tsLiteral("SC_CLK_TCK"); |
2871 |
| - public static final TruffleString T_SC_NPROCESSORS_ONLN = tsLiteral("SC_NPROCESSORS_ONLN"); |
2872 |
| - public static final int SC_CLK_TCK = 2; |
2873 |
| - public static final int SC_NPROCESSORS_ONLN = 84; |
2874 |
| - public static final EconomicMapStorage SYSCONF_NAMES = EconomicMapStorage.create(); |
2875 |
| - static { |
2876 |
| - // TODO populate from constants |
2877 |
| - SYSCONF_NAMES.putUncached(T_SC_CLK_TCK, SC_CLK_TCK); |
2878 |
| - SYSCONF_NAMES.putUncached(T_SC_NPROCESSORS_ONLN, SC_NPROCESSORS_ONLN); |
2879 |
| - } |
| 2883 | + private static final TruffleString T_SYSCONF_NAMES = tsLiteral("sysconf_names"); |
2880 | 2884 |
|
2881 | 2885 | @Specialization
|
2882 |
| - static int sysconf(VirtualFrame frame, Object arg, |
| 2886 | + static long sysconf(VirtualFrame frame, PythonModule self, Object arg, |
2883 | 2887 | @Bind("this") Node inliningTarget,
|
2884 | 2888 | @Cached PyLongCheckNode longCheckNode,
|
2885 | 2889 | @Cached PyLongAsIntNode asIntNode,
|
2886 | 2890 | @Cached PyUnicodeCheckNode unicodeCheckNode,
|
2887 |
| - @Cached HashingStorageNodes.HashingStorageGetItem getItem, |
| 2891 | + @Cached PyObjectGetAttr getAttr, |
| 2892 | + @Cached PyObjectGetItem getItem, |
2888 | 2893 | @Cached PRaiseNode raiseNode,
|
| 2894 | + @Bind PythonContext context, |
| 2895 | + @CachedLibrary("context.getPosixSupport()") PosixSupportLibrary posixLib, |
2889 | 2896 | @Cached PConstructAndRaiseNode.Lazy constructAndRaiseNode) {
|
2890 | 2897 | int id;
|
2891 | 2898 | if (longCheckNode.execute(inliningTarget, arg)) {
|
2892 | 2899 | id = asIntNode.execute(frame, inliningTarget, arg);
|
2893 | 2900 | } else if (unicodeCheckNode.execute(inliningTarget, arg)) {
|
2894 |
| - Object idObj = getItem.execute(frame, inliningTarget, SYSCONF_NAMES, arg); |
2895 |
| - if (idObj instanceof Integer idInt) { |
2896 |
| - id = idInt; |
2897 |
| - } else { |
| 2901 | + try { |
| 2902 | + Object sysconfigNamesObject = getAttr.execute(frame, inliningTarget, self, T_SYSCONF_NAMES); |
| 2903 | + Object idObj = getItem.execute(frame, inliningTarget, sysconfigNamesObject, arg); |
| 2904 | + id = asIntNode.execute(frame, inliningTarget, idObj); |
| 2905 | + } catch (PException e) { |
2898 | 2906 | throw raiseNode.raise(inliningTarget, ValueError, ErrorMessages.UNRECOGNIZED_CONF_NAME);
|
2899 | 2907 | }
|
2900 | 2908 | } else {
|
2901 | 2909 | throw raiseNode.raise(inliningTarget, TypeError, ErrorMessages.CONFIGURATION_NAMES_MUST_BE_STRINGS_OR_INTEGERS);
|
2902 | 2910 | }
|
2903 |
| - if (id == SC_CLK_TCK) { |
2904 |
| - return 100; // it's 100 on most default kernel configs. TODO: use real value through |
2905 |
| - // NFI |
2906 |
| - } else if (id == SC_NPROCESSORS_ONLN) { |
2907 |
| - return CpuCountNode.getCpuCount(); |
| 2911 | + try { |
| 2912 | + return posixLib.sysconf(context.getPosixSupport(), id); |
| 2913 | + } catch (PosixException e) { |
| 2914 | + throw constructAndRaiseNode.get(inliningTarget).raiseOSErrorFromPosixException(frame, e); |
2908 | 2915 | }
|
2909 |
| - throw constructAndRaiseNode.get(inliningTarget).raiseOSError(frame, OSErrorEnum.EINVAL); |
2910 | 2916 | }
|
2911 | 2917 | }
|
2912 | 2918 |
|
|
0 commit comments