Skip to content

Commit 6502abe

Browse files
committed
[GR-23945] unicodedata.is_normalized is missing
[GR-23947] unicodedata doesn't have version attribute but unidata_version
1 parent 7fef1c6 commit 6502abe

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public static String getUnicodeVersion() {
158158
@Override
159159
public void initialize(PythonCore core) {
160160
super.initialize(core);
161-
builtinConstants.put("version", getUnicodeVersion());
161+
builtinConstants.put("unidata_version", getUnicodeVersion());
162162
PythonBuiltinClass objectType = core.lookupType(PythonBuiltinClassType.PythonObject);
163163
PythonObject ucd_3_2_0 = core.factory().createPythonObject(objectType);
164164
ucd_3_2_0.setAttribute("unidata_version", "3.2.0");
@@ -198,4 +198,36 @@ public String normalize(String form, PString unistr,
198198
}
199199

200200
}
201+
202+
// unicodedata.is_normalized(form, unistr)
203+
@Builtin(name = "is_normalized", minNumOfPositionalArgs = 2)
204+
@GenerateNodeFactory
205+
public abstract static class IsNormalizedNode extends PythonBuiltinNode {
206+
@TruffleBoundary
207+
protected Normalizer.Form getForm(String form) {
208+
try {
209+
return Normalizer.Form.valueOf(form);
210+
} catch (IllegalArgumentException e) {
211+
return null;
212+
}
213+
}
214+
215+
@Specialization(guards = {"form.equals(cachedForm)"}, limit = "4")
216+
@TruffleBoundary
217+
public boolean isNormalized(@SuppressWarnings("unused") String form, String unistr,
218+
@SuppressWarnings("unused") @Cached("form") String cachedForm,
219+
@Cached("getForm(cachedForm)") Normalizer.Form cachedNormForm) {
220+
if (cachedNormForm == null) {
221+
throw raise(ValueError, ErrorMessages.INVALID_NORMALIZATION_FORM);
222+
}
223+
return Normalizer.isNormalized(unistr, cachedNormForm);
224+
}
225+
226+
@Specialization(guards = {"form.equals(cachedForm)"}, limit = "4")
227+
public boolean normalize(String form, PString unistr,
228+
@Cached("form") String cachedForm,
229+
@Cached("getForm(cachedForm)") Normalizer.Form cachedNormForm) {
230+
return isNormalized(form, unistr.getValue(), cachedForm, cachedNormForm);
231+
}
232+
}
201233
}

0 commit comments

Comments
 (0)