|
43 | 43 | import static com.oracle.graal.python.nodes.BuiltinNames.J_UNICODEDATA; |
44 | 44 | import static com.oracle.graal.python.nodes.BuiltinNames.T_UNICODEDATA; |
45 | 45 | import static com.oracle.graal.python.nodes.BuiltinNames.T___GRAALPYTHON__; |
| 46 | +import static com.oracle.graal.python.nodes.SpecialAttributeNames.T___MODULE__; |
| 47 | +import static com.oracle.graal.python.nodes.SpecialAttributeNames.T___QUALNAME__; |
46 | 48 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.KeyError; |
47 | 49 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError; |
48 | 50 | import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING; |
|
65 | 67 | import com.oracle.graal.python.builtins.PythonBuiltins; |
66 | 68 | import com.oracle.graal.python.builtins.objects.PNone; |
67 | 69 | import com.oracle.graal.python.builtins.objects.module.PythonModule; |
| 70 | +import com.oracle.graal.python.builtins.objects.object.PythonObject; |
| 71 | +import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass; |
| 72 | +import com.oracle.graal.python.builtins.objects.type.PythonClass; |
68 | 73 | import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs; |
69 | 74 | import com.oracle.graal.python.lib.PyObjectGetAttr; |
70 | 75 | import com.oracle.graal.python.nodes.ErrorMessages; |
|
97 | 102 | public final class UnicodeDataModuleBuiltins extends PythonBuiltins { |
98 | 103 | private static final TruffleString T__CPYTHON_UNICODEDATA = toTruffleStringUncached("_cpython_unicodedata"); |
99 | 104 | private static final TruffleString T_LOOKUP = toTruffleStringUncached("lookup"); |
| 105 | + private static final TruffleString T_UCD_3_2_0 = toTruffleStringUncached("ucd_3_2_0"); |
| 106 | + private static final TruffleString T_UNIDATA_VERSION = toTruffleStringUncached("unidata_version"); |
100 | 107 |
|
101 | 108 | @Override |
102 | 109 | protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() { |
@@ -131,16 +138,33 @@ private static String getUnicodeNameTB(int cp) { |
131 | 138 | public void postInitialize(Python3Core core) { |
132 | 139 | super.postInitialize(core); |
133 | 140 | PythonModule self = core.lookupBuiltinModule(T_UNICODEDATA); |
134 | | - self.setAttribute(toTruffleStringUncached("unidata_version"), toTruffleStringUncached(getUnicodeVersion())); |
| 141 | + self.setAttribute(T_UNIDATA_VERSION, toTruffleStringUncached(getUnicodeVersion())); |
135 | 142 | if (core.getLanguage().getEngineOption(PythonOptions.UnicodeCharacterDatabaseNativeFallback)) { |
136 | 143 | PyObjectCallMethodObjArgs.executeUncached(core.lookupBuiltinModule(T___GRAALPYTHON__), toTruffleStringUncached("import_current_as_named_module_with_delegate"), |
137 | 144 | /* module_name= */ T_UNICODEDATA, |
138 | 145 | /* delegate_name= */ T__CPYTHON_UNICODEDATA, |
139 | | - /* delegate_attributes= */ PFactory.createList(core.getLanguage(), new Object[]{toTruffleStringUncached("ucd_3_2_0")}), |
| 146 | + /* delegate_attributes= */ PFactory.createList(core.getLanguage(), new Object[]{T_UCD_3_2_0}), |
140 | 147 | /* owner_globals= */ GetOrCreateDictNode.executeUncached(self)); |
| 148 | + } else { |
| 149 | + self.setAttribute(T_UCD_3_2_0, createUCDCompatibilityObject(core, self)); |
141 | 150 | } |
142 | 151 | } |
143 | 152 |
|
| 153 | + private PythonObject createUCDCompatibilityObject(Python3Core core, PythonModule self) { |
| 154 | + TruffleString t_ucd = toTruffleStringUncached("UCD"); |
| 155 | + PythonClass clazz = PFactory.createPythonClassAndFixupSlots(null, core.getLanguage(), t_ucd, PythonBuiltinClassType.PythonObject, |
| 156 | + new PythonAbstractClass[]{core.lookupType(PythonBuiltinClassType.PythonObject)}); |
| 157 | + for (String s : new String[]{"normalize", "is_normalized", "lookup", "name", "bidirectional", "category", "combining", "east_asian_width", "decomposition", "digit", "decimal"}) { |
| 158 | + TruffleString ts = toTruffleStringUncached(s); |
| 159 | + clazz.setAttribute(ts, PFactory.createStaticmethodFromCallableObj(core.getLanguage(), self.getAttribute(ts))); |
| 160 | + } |
| 161 | + clazz.setAttribute(T___MODULE__, T_UNICODEDATA); |
| 162 | + clazz.setAttribute(T___QUALNAME__, t_ucd); |
| 163 | + PythonObject obj = PFactory.createPythonObject(clazz, clazz.getInstanceShape()); |
| 164 | + obj.setAttribute(T_UNIDATA_VERSION, toTruffleStringUncached("3.2.0")); |
| 165 | + return obj; |
| 166 | + } |
| 167 | + |
144 | 168 | static final int NORMALIZER_FORM_COUNT = 4; |
145 | 169 |
|
146 | 170 | @TruffleBoundary |
|
0 commit comments