Skip to content

Commit 648a34a

Browse files
committed
re-add fake java pyexpat for windows only (just some constants)
1 parent 7330e39 commit 648a34a

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.util.ServiceLoader;
5656
import java.util.logging.Level;
5757

58+
import com.oracle.graal.python.builtins.modules.PyExpatModuleBuiltins;
5859
import org.graalvm.nativeimage.ImageInfo;
5960

6061
import com.oracle.graal.python.PythonLanguage;
@@ -630,6 +631,8 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
630631
PythonOptions.WITHOUT_DIGEST ? null : new Blake2sObjectBuiltins(),
631632
PythonOptions.WITHOUT_DIGEST ? null : new HashlibModuleBuiltins(),
632633

634+
new PyExpatModuleBuiltins(),
635+
633636
// itertools
634637
new AccumulateBuiltins(),
635638
new CombinationsBuiltins(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.builtins.modules;
42+
43+
import java.util.LinkedHashMap;
44+
import java.util.List;
45+
46+
import com.oracle.graal.python.builtins.Builtin;
47+
import com.oracle.graal.python.builtins.CoreFunctions;
48+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
49+
import com.oracle.graal.python.builtins.PythonBuiltins;
50+
import com.oracle.graal.python.builtins.PythonOS;
51+
import com.oracle.graal.python.builtins.objects.module.PythonModule;
52+
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
53+
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
54+
import com.oracle.graal.python.builtins.Python3Core;
55+
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
56+
import com.oracle.truffle.api.dsl.NodeFactory;
57+
import com.oracle.truffle.api.dsl.Specialization;
58+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
59+
60+
import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;
61+
62+
@CoreFunctions(defineModule = "pyexpat", os = PythonOS.PLATFORM_WIN32)
63+
public class PyExpatModuleBuiltins extends PythonBuiltins {
64+
@Override
65+
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
66+
return PyExpatModuleBuiltinsFactory.getFactories();
67+
}
68+
69+
private static enum ContentModelConstant {
70+
XML_CQUANT_NONE(0),
71+
XML_CQUANT_OPT(1),
72+
XML_CQUANT_PLUS(3),
73+
XML_CQUANT_REP(2),
74+
XML_CTYPE_ANY(2),
75+
XML_CTYPE_CHOICE(5),
76+
XML_CTYPE_EMPTY(1),
77+
XML_CTYPE_MIXED(3),
78+
XML_CTYPE_NAME(4),
79+
XML_CTYPE_SEQ(6);
80+
81+
private final int number;
82+
83+
private ContentModelConstant(int number) {
84+
this.number = number;
85+
}
86+
}
87+
88+
private static enum ErrorConstant {
89+
XML_ERROR_NO_MEMORY("out of memory"),
90+
XML_ERROR_SYNTAX("syntax error"),
91+
XML_ERROR_NO_ELEMENTS("no element found"),
92+
XML_ERROR_INVALID_TOKEN("not well-formed (invalid token)"),
93+
XML_ERROR_UNCLOSED_TOKEN("unclosed token"),
94+
XML_ERROR_PARTIAL_CHAR("partial character"),
95+
XML_ERROR_TAG_MISMATCH("mismatched tag"),
96+
XML_ERROR_DUPLICATE_ATTRIBUTE("duplicate attribute"),
97+
XML_ERROR_JUNK_AFTER_DOC_ELEMENT("junk after document element"),
98+
XML_ERROR_PARAM_ENTITY_REF("illegal parameter entity reference"),
99+
XML_ERROR_UNDEFINED_ENTITY("undefined entity"),
100+
XML_ERROR_RECURSIVE_ENTITY_REF("recursive entity reference"),
101+
XML_ERROR_ASYNC_ENTITY("asynchronous entity"),
102+
XML_ERROR_BAD_CHAR_REF("reference to invalid character number"),
103+
XML_ERROR_BINARY_ENTITY_REF("reference to binary entity"),
104+
XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF("reference to external entity in attribute"),
105+
XML_ERROR_MISPLACED_XML_PI("XML or text declaration not at start of entity"),
106+
XML_ERROR_UNKNOWN_ENCODING("unknown encoding"),
107+
XML_ERROR_INCORRECT_ENCODING("encoding specified in XML declaration is incorrect"),
108+
XML_ERROR_UNCLOSED_CDATA_SECTION("unclosed CDATA section"),
109+
XML_ERROR_EXTERNAL_ENTITY_HANDLING("error in processing external entity reference"),
110+
XML_ERROR_NOT_STANDALONE("document is not standalone"),
111+
XML_ERROR_UNEXPECTED_STATE("unexpected parser state - please send a bug report"),
112+
XML_ERROR_ENTITY_DECLARED_IN_PE("entity declared in parameter entity"),
113+
XML_ERROR_FEATURE_REQUIRES_XML_DTD("requested feature requires XML_DTD support in Expat"),
114+
XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING("cannot change setting once parsing has begun"),
115+
XML_ERROR_UNBOUND_PREFIX("unbound prefix"),
116+
XML_ERROR_UNDECLARING_PREFIX("must not undeclare prefix"),
117+
XML_ERROR_INCOMPLETE_PE("incomplete markup in parameter entity"),
118+
XML_ERROR_XML_DECL("XML declaration not well-formed"),
119+
XML_ERROR_TEXT_DECL("text declaration not well-formed"),
120+
XML_ERROR_PUBLICID("illegal character(s) in public id"),
121+
XML_ERROR_SUSPENDED("parser suspended"),
122+
XML_ERROR_NOT_SUSPENDED("parser not suspended"),
123+
XML_ERROR_ABORTED("parsing aborted"),
124+
XML_ERROR_FINISHED("parsing finished"),
125+
XML_ERROR_SUSPEND_PE("cannot suspend in external parameter entity");
126+
127+
private final String message;
128+
129+
private ErrorConstant(String message) {
130+
this.message = message;
131+
}
132+
}
133+
134+
@Override
135+
public void initialize(Python3Core core) {
136+
super.initialize(core);
137+
PythonModule model = core.factory().createPythonModule(toTruffleStringUncached("pyexpat.model"));
138+
for (ContentModelConstant v : ContentModelConstant.values()) {
139+
model.setAttribute(toTruffleStringUncached(v.name()), v.number);
140+
}
141+
addBuiltinConstant("model", model);
142+
143+
PythonModule errors = core.factory().createPythonModule(toTruffleStringUncached("pyexpat.errors"));
144+
LinkedHashMap<String, Object> codes = new LinkedHashMap<>(ErrorConstant.values().length);
145+
LinkedHashMap<Object, Object> messages = new LinkedHashMap<>(ErrorConstant.values().length);
146+
for (ErrorConstant c : ErrorConstant.values()) {
147+
errors.setAttribute(toTruffleStringUncached(c.name()), toTruffleStringUncached(c.message));
148+
codes.put(c.message, c.ordinal() + 1);
149+
messages.put(c.ordinal() + 1, c.message);
150+
}
151+
errors.setAttribute(toTruffleStringUncached("messages"), core.factory().createDictFromMapGeneric(messages));
152+
errors.setAttribute(toTruffleStringUncached("codes"), core.factory().createDictFromMap(codes));
153+
addBuiltinConstant("errors", errors);
154+
}
155+
156+
@Builtin(name = "ParserCreate", parameterNames = {"encoding", "namespace_separator", "intern"}, doc = "Return a new XML parser object.")
157+
@GenerateNodeFactory
158+
abstract static class ParserCreateNode extends PythonTernaryBuiltinNode {
159+
@SuppressWarnings("unused")
160+
@Specialization
161+
@TruffleBoundary
162+
Object fail(Object encoding, Object namespace_separator, Object intern) {
163+
throw raise(PythonBuiltinClassType.NotImplementedError, toTruffleStringUncached("XML pyexpat parser is not implemented"));
164+
}
165+
}
166+
}

0 commit comments

Comments
 (0)