Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.

Commit b0e6874

Browse files
Christopher-Chianellitriceo
authored andcommitted
chore: Rename internal methods of PythonLikeObject to use '$' instead of '__'
- This prevents naming conflicts in Python since '$' is invalid in Python identifier - Additionally, it make it consistent with the names of other generated methods/fields in the interpreter
1 parent 5f902c4 commit b0e6874

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+276
-276
lines changed

jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/CPythonBackedPythonInterpreter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public static void updateJavaObjectFromPythonObject(PythonLikeObject javaObject,
113113
OpaquePythonReference pythonObject,
114114
Map<Number, PythonLikeObject> instanceMap) {
115115
Map<String, PythonLikeObject> dict = getPythonReferenceDict(pythonObject, instanceMap);
116-
dict.forEach(javaObject::__setAttribute);
116+
dict.forEach(javaObject::$setAttribute);
117117
}
118118

119119
public static PythonLikeObject callPythonReference(OpaquePythonReference object, List<PythonLikeObject> positionalArguments,

jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/PythonClassTranslator.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public static PythonLikeType translatePythonClass(PythonCompiledClass pythonComp
144144
classWriter.visit(Opcodes.V11, Modifier.PUBLIC, internalClassName, null,
145145
superClassType.getJavaTypeInternalName(), interfaces);
146146

147-
pythonCompiledClass.staticAttributeNameToObject.forEach(pythonLikeType::__setAttribute);
147+
pythonCompiledClass.staticAttributeNameToObject.forEach(pythonLikeType::$setAttribute);
148148

149149
classWriter.visitField(Modifier.PUBLIC | Modifier.STATIC, TYPE_FIELD_NAME, Type.getDescriptor(PythonLikeType.class),
150150
null, null);
@@ -154,7 +154,7 @@ public static PythonLikeType translatePythonClass(PythonCompiledClass pythonComp
154154

155155
for (Map.Entry<String, PythonLikeObject> staticAttributeEntry : pythonCompiledClass.staticAttributeNameToObject
156156
.entrySet()) {
157-
pythonLikeType.__setAttribute(staticAttributeEntry.getKey(), staticAttributeEntry.getValue());
157+
pythonLikeType.$setAttribute(staticAttributeEntry.getKey(), staticAttributeEntry.getValue());
158158
}
159159

160160
Map<String, PythonLikeType> attributeNameToTypeMap = new HashMap<>();
@@ -259,17 +259,17 @@ public static PythonLikeType translatePythonClass(PythonCompiledClass pythonComp
259259
PythonBytecodeToJavaBytecodeTranslator.writeClassOutput(BuiltinTypes.classNameToBytecode, className,
260260
classWriter.toByteArray());
261261

262-
pythonLikeType.__setAttribute("__name__", PythonString.valueOf(pythonCompiledClass.className));
263-
pythonLikeType.__setAttribute("__qualname__", PythonString.valueOf(pythonCompiledClass.qualifiedName));
264-
pythonLikeType.__setAttribute("__module__", PythonString.valueOf(pythonCompiledClass.module));
262+
pythonLikeType.$setAttribute("__name__", PythonString.valueOf(pythonCompiledClass.className));
263+
pythonLikeType.$setAttribute("__qualname__", PythonString.valueOf(pythonCompiledClass.qualifiedName));
264+
pythonLikeType.$setAttribute("__module__", PythonString.valueOf(pythonCompiledClass.module));
265265

266266
PythonLikeDict annotations = new PythonLikeDict();
267267
pythonCompiledClass.typeAnnotations.forEach((name, type) -> annotations.put(PythonString.valueOf(name), type));
268-
pythonLikeType.__setAttribute("__annotations__", annotations);
268+
pythonLikeType.$setAttribute("__annotations__", annotations);
269269

270270
PythonLikeTuple mro = new PythonLikeTuple();
271271
mro.addAll(superTypeList);
272-
pythonLikeType.__setAttribute("__mro__", mro);
272+
pythonLikeType.$setAttribute("__mro__", mro);
273273

274274
Class<? extends PythonLikeObject> generatedClass;
275275
try {
@@ -339,7 +339,7 @@ public static void setSelfStaticInstances(PythonCompiledClass pythonCompiledClas
339339
objectInstance.$setCPythonId(PythonInteger.valueOf(pythonReferenceId.longValue()));
340340
objectInstance.$setInstanceMap(instanceMap);
341341
objectInstance.$readFieldsFromCPythonReference();
342-
pythonLikeType.__setAttribute(attributeName, objectInstance);
342+
pythonLikeType.$setAttribute(attributeName, objectInstance);
343343
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
344344
throw new RuntimeException("Unable to construct instance of class (" + generatedClass + ")", e);
345345
}
@@ -442,7 +442,7 @@ private static Class<?> createBytecodeForMethodAndSetOnClass(String className, P
442442

443443
generatedClass.getField(getJavaMethodName(methodEntry.getKey()))
444444
.set(null, functionInstance);
445-
pythonLikeType.__setAttribute(methodEntry.getKey(), translatedPythonMethodWrapper);
445+
pythonLikeType.$setAttribute(methodEntry.getKey(), translatedPythonMethodWrapper);
446446
return functionClass;
447447
} catch (IllegalAccessException | NoSuchFieldException e) {
448448
throw new IllegalStateException("Impossible State: could not access method (" + methodEntry.getKey()
@@ -508,7 +508,7 @@ private static Class<?> createPythonWrapperMethod(String methodName, PythonCompi
508508
Type.getDescriptor(PythonLikeType.class));
509509
methodVisitor.visitLdcInsn(methodName);
510510
methodVisitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(PythonLikeObject.class),
511-
"__getAttributeOrError",
511+
"$getAttributeOrError",
512512
Type.getMethodDescriptor(Type.getType(PythonLikeObject.class), Type.getType(String.class)),
513513
true);
514514
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(PythonLikeFunction.class));
@@ -805,7 +805,7 @@ public static Type getVirtualFunctionReturnType(PythonCompiledFunction function)
805805
public static void createGetAttribute(ClassWriter classWriter, String classInternalName, String superInternalName,
806806
Collection<String> instanceAttributes,
807807
Map<String, PythonLikeType> fieldToType) {
808-
MethodVisitor methodVisitor = classWriter.visitMethod(Modifier.PUBLIC, "__getAttributeOrNull",
808+
MethodVisitor methodVisitor = classWriter.visitMethod(Modifier.PUBLIC, "$getAttributeOrNull",
809809
Type.getMethodDescriptor(Type.getType(PythonLikeObject.class),
810810
Type.getType(String.class)),
811811
null, null);
@@ -823,7 +823,7 @@ public static void createGetAttribute(ClassWriter classWriter, String classInter
823823
}, () -> {
824824
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
825825
methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
826-
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "__getAttributeOrNull",
826+
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "$getAttributeOrNull",
827827
Type.getMethodDescriptor(Type.getType(PythonLikeObject.class),
828828
Type.getType(String.class)),
829829
false);
@@ -837,7 +837,7 @@ public static void createGetAttribute(ClassWriter classWriter, String classInter
837837
public static void createSetAttribute(ClassWriter classWriter, String classInternalName, String superInternalName,
838838
Collection<String> instanceAttributes,
839839
Map<String, PythonLikeType> fieldToType) {
840-
MethodVisitor methodVisitor = classWriter.visitMethod(Modifier.PUBLIC, "__setAttribute",
840+
MethodVisitor methodVisitor = classWriter.visitMethod(Modifier.PUBLIC, "$setAttribute",
841841
Type.getMethodDescriptor(Type.VOID_TYPE,
842842
Type.getType(String.class),
843843
Type.getType(PythonLikeObject.class)),
@@ -860,7 +860,7 @@ public static void createSetAttribute(ClassWriter classWriter, String classInter
860860
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
861861
methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
862862
methodVisitor.visitVarInsn(Opcodes.ALOAD, 2);
863-
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "__setAttribute",
863+
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "$setAttribute",
864864
Type.getMethodDescriptor(Type.VOID_TYPE,
865865
Type.getType(String.class),
866866
Type.getType(PythonLikeObject.class)),
@@ -875,7 +875,7 @@ public static void createSetAttribute(ClassWriter classWriter, String classInter
875875
public static void createDeleteAttribute(ClassWriter classWriter, String classInternalName, String superInternalName,
876876
Collection<String> instanceAttributes,
877877
Map<String, PythonLikeType> fieldToType) {
878-
MethodVisitor methodVisitor = classWriter.visitMethod(Modifier.PUBLIC, "__deleteAttribute",
878+
MethodVisitor methodVisitor = classWriter.visitMethod(Modifier.PUBLIC, "$deleteAttribute",
879879
Type.getMethodDescriptor(Type.VOID_TYPE,
880880
Type.getType(String.class)),
881881
null, null);
@@ -894,7 +894,7 @@ public static void createDeleteAttribute(ClassWriter classWriter, String classIn
894894
}, () -> {
895895
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
896896
methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
897-
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "__deleteAttribute",
897+
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "$deleteAttribute",
898898
Type.getMethodDescriptor(Type.VOID_TYPE,
899899
Type.getType(String.class)),
900900
false);

jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/PythonGeneratorTranslator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,12 +656,12 @@ private static void generateAdvanceGeneratorMethodForYieldFrom(ClassWriter class
656656
methodVisitor.visitInsn(Opcodes.DUP);
657657
methodVisitor.visitMethodInsn(Opcodes.INVOKEINTERFACE,
658658
Type.getInternalName(PythonLikeObject.class),
659-
"__getType", Type.getMethodDescriptor(Type.getType(PythonLikeType.class)),
659+
"$getType", Type.getMethodDescriptor(Type.getType(PythonLikeType.class)),
660660
true);
661661
methodVisitor.visitLdcInsn("throw");
662662
methodVisitor.visitMethodInsn(Opcodes.INVOKEINTERFACE,
663663
Type.getInternalName(PythonLikeObject.class),
664-
"__getAttributeOrNull",
664+
"$getAttributeOrNull",
665665
Type.getMethodDescriptor(Type.getType(PythonLikeObject.class),
666666
Type.getType(String.class)),
667667
true);

jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/PythonLikeObject.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public interface PythonLikeObject {
2727
* @return The attribute of the object that corresponds with attributeName
2828
* @throws AttributeError if the attribute does not exist
2929
*/
30-
PythonLikeObject __getAttributeOrNull(String attributeName);
30+
PythonLikeObject $getAttributeOrNull(String attributeName);
3131

3232
/**
3333
* Gets an attribute by name.
@@ -36,8 +36,8 @@ public interface PythonLikeObject {
3636
* @return The attribute of the object that corresponds with attributeName
3737
* @throws AttributeError if the attribute does not exist
3838
*/
39-
default PythonLikeObject __getAttributeOrError(String attributeName) {
40-
PythonLikeObject out = this.__getAttributeOrNull(attributeName);
39+
default PythonLikeObject $getAttributeOrError(String attributeName) {
40+
PythonLikeObject out = this.$getAttributeOrNull(attributeName);
4141
if (out == null) {
4242
throw new AttributeError("object '" + this + "' does not have attribute '" + attributeName + "'");
4343
}
@@ -50,52 +50,52 @@ default PythonLikeObject __getAttributeOrError(String attributeName) {
5050
* @param attributeName Name of the attribute to set
5151
* @param value Value to set the attribute to
5252
*/
53-
void __setAttribute(String attributeName, PythonLikeObject value);
53+
void $setAttribute(String attributeName, PythonLikeObject value);
5454

5555
/**
5656
* Delete an attribute by name.
5757
*
5858
* @param attributeName Name of the attribute to delete
5959
*/
60-
void __deleteAttribute(String attributeName);
60+
void $deleteAttribute(String attributeName);
6161

6262
/**
6363
* Returns the type describing the object
6464
*
6565
* @return the type describing the object
6666
*/
67-
PythonLikeType __getType();
67+
PythonLikeType $getType();
6868

6969
/**
70-
* Return a generic version of {@link PythonLikeObject#__getType()}. This is used in bytecode
70+
* Return a generic version of {@link PythonLikeObject#$getType()}. This is used in bytecode
7171
* generation and not at runtime. For example, for a list of integers, this return
7272
* list[int], while getType returns list. Both methods are needed so type([1,2,3]) is type(['a', 'b', 'c'])
7373
* return True.
7474
*
7575
* @return the generic version of this object's type. Must not be used in identity checks.
7676
*/
77-
default PythonLikeType __getGenericType() {
78-
return __getType();
77+
default PythonLikeType $getGenericType() {
78+
return $getType();
7979
}
8080

8181
default PythonLikeObject $method$__getattribute__(PythonString pythonName) {
8282
String name = pythonName.value;
83-
PythonLikeObject objectResult = __getAttributeOrNull(name);
83+
PythonLikeObject objectResult = $getAttributeOrNull(name);
8484
if (objectResult != null) {
8585
return objectResult;
8686
}
8787

88-
PythonLikeType type = __getType();
89-
PythonLikeObject typeResult = type.__getAttributeOrNull(name);
88+
PythonLikeType type = $getType();
89+
PythonLikeObject typeResult = type.$getAttributeOrNull(name);
9090
if (typeResult != null) {
91-
PythonLikeObject maybeDescriptor = typeResult.__getAttributeOrNull(PythonTernaryOperator.GET.dunderMethod);
91+
PythonLikeObject maybeDescriptor = typeResult.$getAttributeOrNull(PythonTernaryOperator.GET.dunderMethod);
9292
if (maybeDescriptor == null) {
93-
maybeDescriptor = typeResult.__getType().__getAttributeOrNull(PythonTernaryOperator.GET.dunderMethod);
93+
maybeDescriptor = typeResult.$getType().$getAttributeOrNull(PythonTernaryOperator.GET.dunderMethod);
9494
}
9595

9696
if (maybeDescriptor != null) {
9797
if (!(maybeDescriptor instanceof PythonLikeFunction)) {
98-
throw new UnsupportedOperationException("'" + maybeDescriptor.__getType() + "' is not callable");
98+
throw new UnsupportedOperationException("'" + maybeDescriptor.$getType() + "' is not callable");
9999
}
100100
return TernaryDunderBuiltin.GET_DESCRIPTOR.invoke(typeResult, this, type);
101101
}
@@ -107,13 +107,13 @@ default PythonLikeType __getGenericType() {
107107

108108
default PythonLikeObject $method$__setattr__(PythonString pythonName, PythonLikeObject value) {
109109
String name = pythonName.value;
110-
__setAttribute(name, value);
110+
$setAttribute(name, value);
111111
return PythonNone.INSTANCE;
112112
}
113113

114114
default PythonLikeObject $method$__delattr__(PythonString pythonName) {
115115
String name = pythonName.value;
116-
__deleteAttribute(name);
116+
$deleteAttribute(name);
117117
return PythonNone.INSTANCE;
118118
}
119119

@@ -141,7 +141,7 @@ default PythonLikeType __getGenericType() {
141141
} else {
142142
position = String.valueOf(System.identityHashCode(this));
143143
}
144-
return PythonString.valueOf("<" + __getType().getTypeName() + " object at " + position + ">");
144+
return PythonString.valueOf("<" + $getType().getTypeName() + " object at " + position + ">");
145145
}
146146

147147
default PythonLikeObject $method$__format__() {
@@ -153,6 +153,6 @@ default PythonLikeType __getGenericType() {
153153
}
154154

155155
default PythonLikeObject $method$__hash__() {
156-
throw new TypeError("unhashable type: '" + __getType().getTypeName() + "'");
156+
throw new TypeError("unhashable type: '" + $getType().getTypeName() + "'");
157157
}
158158
}

jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/PythonOverloadImplementor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ public static void createDispatchesFor(PythonLikeType pythonLikeType) {
7474
createDispatchForMethod(pythonLikeType, methodName, pythonLikeType.getMethodType(methodName).orElseThrow(),
7575
pythonLikeType.getMethodKind(methodName)
7676
.orElse(PythonClassTranslator.PythonMethodKind.VIRTUAL_METHOD));
77-
pythonLikeType.__setAttribute(methodName, overloadDispatch);
77+
pythonLikeType.$setAttribute(methodName, overloadDispatch);
7878
}
7979

8080
if (pythonLikeType.getConstructorType().isPresent()) {
8181
PythonLikeFunction overloadDispatch =
8282
createDispatchForMethod(pythonLikeType, "__init__", pythonLikeType.getConstructorType().orElseThrow(),
8383
PythonClassTranslator.PythonMethodKind.VIRTUAL_METHOD);
8484
pythonLikeType.setConstructor(overloadDispatch);
85-
pythonLikeType.__setAttribute("__init__", overloadDispatch);
85+
pythonLikeType.$setAttribute("__init__", overloadDispatch);
8686
}
8787
}
8888

@@ -142,7 +142,7 @@ private static PythonLikeFunction createDispatchForMethod(PythonLikeType pythonL
142142
}
143143

144144
private static void createGetTypeFunction(PythonClassTranslator.PythonMethodKind kind, ClassWriter classWriter) {
145-
MethodVisitor methodVisitor = classWriter.visitMethod(Modifier.PUBLIC, "__getType",
145+
MethodVisitor methodVisitor = classWriter.visitMethod(Modifier.PUBLIC, "$getType",
146146
Type.getMethodDescriptor(Type.getType(PythonLikeType.class)),
147147
null,
148148
null);
@@ -484,7 +484,7 @@ private static void createGenericDispatch(MethodVisitor methodVisitor,
484484
public static String getCallErrorInfo(List<PythonLikeObject> positionalArgs,
485485
Map<PythonString, PythonLikeObject> namedArgs) {
486486
return "Could not find an overload that accept " + positionalArgs.stream()
487-
.map(arg -> arg.__getType().getTypeName()).collect(Collectors.joining(", ", "(", ") argument types. "));
487+
.map(arg -> arg.$getType().getTypeName()).collect(Collectors.joining(", ", "(", ") argument types. "));
488488
}
489489

490490
private static SortedMap<PythonLikeType, List<PythonFunctionSignature>>

jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/builtins/BinaryDunderBuiltin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ public BinaryDunderBuiltin(PythonBinaryOperator operator) {
3939

4040
PythonLikeObject object = positionalArguments.get(0);
4141
PythonLikeObject arg = positionalArguments.get(1);
42-
PythonLikeFunction dunderMethod = (PythonLikeFunction) object.__getType().__getAttributeOrError(DUNDER_METHOD_NAME);
42+
PythonLikeFunction dunderMethod = (PythonLikeFunction) object.$getType().$getAttributeOrError(DUNDER_METHOD_NAME);
4343
return dunderMethod.$call(List.of(object, arg), Map.of(), null);
4444
}
4545

4646
public PythonLikeObject invoke(PythonLikeObject object, PythonLikeObject arg) {
47-
PythonLikeFunction dunderMethod = (PythonLikeFunction) object.__getType().__getAttributeOrError(DUNDER_METHOD_NAME);
47+
PythonLikeFunction dunderMethod = (PythonLikeFunction) object.$getType().$getAttributeOrError(DUNDER_METHOD_NAME);
4848
return dunderMethod.$call(List.of(object, arg), Map.of(), null);
4949
}
5050
}

0 commit comments

Comments
 (0)