Skip to content

Commit b4e76ef

Browse files
committed
Provide a shim ucd_3_2_0 object when we do not allow the fallback
1 parent f32cbbd commit b4e76ef

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_unicodedata.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ def test_lookup(self):
7676
unicodedata.lookup("a" * 257)
7777

7878
def test_lookup_named_sequence(self):
79+
if unicodedata.ucd_3_2_0.bidirectional == unicodedata.bidirectional:
80+
raise unittest.SkipTest("Only supported with CPython's unicodedata.ucd_3_2_0")
81+
7982
unicode_name = "LATIN SMALL LETTER R WITH TILDE"
8083
self.assertEqual(unicodedata.lookup(unicode_name), "\u0072\u0303")
8184

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/UnicodeDataModuleBuiltins.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import static com.oracle.graal.python.nodes.BuiltinNames.J_UNICODEDATA;
4444
import static com.oracle.graal.python.nodes.BuiltinNames.T_UNICODEDATA;
4545
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__;
4648
import static com.oracle.graal.python.runtime.exception.PythonErrorType.KeyError;
4749
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
4850
import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;
@@ -65,6 +67,9 @@
6567
import com.oracle.graal.python.builtins.PythonBuiltins;
6668
import com.oracle.graal.python.builtins.objects.PNone;
6769
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;
6873
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
6974
import com.oracle.graal.python.lib.PyObjectGetAttr;
7075
import com.oracle.graal.python.nodes.ErrorMessages;
@@ -97,6 +102,8 @@
97102
public final class UnicodeDataModuleBuiltins extends PythonBuiltins {
98103
private static final TruffleString T__CPYTHON_UNICODEDATA = toTruffleStringUncached("_cpython_unicodedata");
99104
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");
100107

101108
@Override
102109
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
@@ -131,16 +138,33 @@ private static String getUnicodeNameTB(int cp) {
131138
public void postInitialize(Python3Core core) {
132139
super.postInitialize(core);
133140
PythonModule self = core.lookupBuiltinModule(T_UNICODEDATA);
134-
self.setAttribute(toTruffleStringUncached("unidata_version"), toTruffleStringUncached(getUnicodeVersion()));
141+
self.setAttribute(T_UNIDATA_VERSION, toTruffleStringUncached(getUnicodeVersion()));
135142
if (core.getLanguage().getEngineOption(PythonOptions.UnicodeCharacterDatabaseNativeFallback)) {
136143
PyObjectCallMethodObjArgs.executeUncached(core.lookupBuiltinModule(T___GRAALPYTHON__), toTruffleStringUncached("import_current_as_named_module_with_delegate"),
137144
/* module_name= */ T_UNICODEDATA,
138145
/* 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}),
140147
/* owner_globals= */ GetOrCreateDictNode.executeUncached(self));
148+
} else {
149+
self.setAttribute(T_UCD_3_2_0, createUCDCompatibilityObject(core, self));
141150
}
142151
}
143152

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+
144168
static final int NORMALIZER_FORM_COUNT = 4;
145169

146170
@TruffleBoundary

mx.graalpython/mx_graalpython.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,6 +2254,7 @@ def bytecode_dsl_build_args(prefix=''):
22542254
'-Dpolyglot.python.PosixModuleBackend=native',
22552255
'-Dpolyglot.python.Sha3ModuleBackend=native',
22562256
'-Dpolyglot.python.CompressionModulesBackend=native',
2257+
'-Dpolyglot.python.UnicodeCharacterDatabaseNativeFallback=true',
22572258
] + bytecode_dsl_build_args(),
22582259
language='python',
22592260
default_vm_args=[

mx.graalpython/suite.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,11 +889,12 @@
889889
# GraalPy standalone specific flags
890890
# uncomment to disable JLine FFM provider at native image build time
891891
#'-Dorg.graalvm.shadowed.org.jline.terminal.ffm.disable=true',
892-
'--enable-native-access=org.graalvm.shadowed.jline',
892+
'--enable-native-access=org.graalvm.shadowed.jline',
893893
"-Dpolyglot.python.PosixModuleBackend=native",
894894
"-Dpolyglot.python.Sha3ModuleBackend=native",
895895
"-Dpolyglot.python.CompressionModulesBackend=native",
896896
"-Dpolyglot.python.PyExpatModuleBackend=native",
897+
"-Dpolyglot.python.UnicodeCharacterDatabaseNativeFallback=true",
897898
],
898899
"dynamicBuildArgs": "libpythonvm_build_args",
899900
},

0 commit comments

Comments
 (0)