Skip to content

Commit a309e24

Browse files
committed
[GR-23208] Get test_code to pass.
PullRequest: graalpython/1123
2 parents 4946847 + 3c90ddd commit a309e24

File tree

6 files changed

+165
-63
lines changed

6 files changed

+165
-63
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_code.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
*graalpython.lib-python.3.test.test_code.CodeTest.test_constructor
88
*graalpython.lib-python.3.test.test_code.CodeTest.test_newempty
99
*graalpython.lib-python.3.test.test_code.CodeTest.test_replace
10+
*graalpython.lib-python.3.test.test_code.CodeWeakRefTest.test_basic

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/CodeBuiltins.java

Lines changed: 71 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,25 @@
2626

2727
package com.oracle.graal.python.builtins.objects.code;
2828

29+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__HASH__;
30+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REPR__;
31+
2932
import java.util.List;
3033

3134
import com.oracle.graal.python.builtins.Builtin;
3235
import com.oracle.graal.python.builtins.CoreFunctions;
3336
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
3437
import com.oracle.graal.python.builtins.PythonBuiltins;
3538
import com.oracle.graal.python.builtins.objects.PNone;
39+
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
3640
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
37-
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
41+
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
42+
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
3843
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
3944
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
4045
import com.oracle.truffle.api.dsl.NodeFactory;
4146
import com.oracle.truffle.api.dsl.Specialization;
47+
import com.oracle.truffle.api.library.CachedLibrary;
4248

4349
@CoreFunctions(extendClasses = PythonBuiltinClassType.PCode)
4450
public class CodeBuiltins extends PythonBuiltins {
@@ -50,33 +56,25 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
5056

5157
@Builtin(name = "co_freevars", minNumOfPositionalArgs = 1, isGetter = true)
5258
@GenerateNodeFactory
53-
public abstract static class GetFreeVarsNode extends PythonBuiltinNode {
59+
public abstract static class GetFreeVarsNode extends PythonUnaryBuiltinNode {
5460
@Specialization
5561
protected Object get(PCode self) {
56-
Object[] freeVars = self.getFreeVars();
57-
if (freeVars != null) {
58-
return factory().createTuple(freeVars);
59-
}
60-
return PNone.NONE;
62+
return self.co_freevars(factory());
6163
}
6264
}
6365

6466
@Builtin(name = "co_cellvars", minNumOfPositionalArgs = 1, isGetter = true)
6567
@GenerateNodeFactory
66-
public abstract static class GetCellVarsNode extends PythonBuiltinNode {
68+
public abstract static class GetCellVarsNode extends PythonUnaryBuiltinNode {
6769
@Specialization
6870
protected Object get(PCode self) {
69-
Object[] cellVars = self.getCellVars();
70-
if (cellVars != null) {
71-
return factory().createTuple(cellVars);
72-
}
73-
return PNone.NONE;
71+
return self.co_cellvars(factory());
7472
}
7573
}
7674

7775
@Builtin(name = "co_filename", minNumOfPositionalArgs = 1, isGetter = true)
7876
@GenerateNodeFactory
79-
public abstract static class GetFilenameNode extends PythonBuiltinNode {
77+
public abstract static class GetFilenameNode extends PythonUnaryBuiltinNode {
8078
@Specialization
8179
protected Object get(PCode self) {
8280
String filename = self.getFilename();
@@ -89,7 +87,7 @@ protected Object get(PCode self) {
8987

9088
@Builtin(name = "co_firstlineno", minNumOfPositionalArgs = 1, isGetter = true)
9189
@GenerateNodeFactory
92-
public abstract static class GetLinenoNode extends PythonBuiltinNode {
90+
public abstract static class GetLinenoNode extends PythonUnaryBuiltinNode {
9391
@Specialization
9492
protected Object get(PCode self) {
9593
return self.getFirstLineNo();
@@ -98,57 +96,53 @@ protected Object get(PCode self) {
9896

9997
@Builtin(name = "co_name", minNumOfPositionalArgs = 1, isGetter = true)
10098
@GenerateNodeFactory
101-
public abstract static class GetNameNode extends PythonBuiltinNode {
99+
public abstract static class GetNameNode extends PythonUnaryBuiltinNode {
102100
@Specialization
103101
@TruffleBoundary
104102
protected Object get(PCode self) {
105-
String name = self.getName();
106-
if (name != null) {
107-
return name;
108-
}
109-
return PNone.NONE;
103+
return self.co_name();
110104
}
111105
}
112106

113107
@Builtin(name = "co_argcount", minNumOfPositionalArgs = 1, isGetter = true)
114108
@GenerateNodeFactory
115-
public abstract static class GetArgCountNode extends PythonBuiltinNode {
109+
public abstract static class GetArgCountNode extends PythonUnaryBuiltinNode {
116110
@Specialization
117111
protected Object get(PCode self) {
118-
return self.getArgcount();
112+
return self.co_argcount();
119113
}
120114
}
121115

122116
@Builtin(name = "co_posonlyargcount", minNumOfPositionalArgs = 1, isGetter = true)
123117
@GenerateNodeFactory
124-
public abstract static class GetPosOnlyArgCountNode extends PythonBuiltinNode {
118+
public abstract static class GetPosOnlyArgCountNode extends PythonUnaryBuiltinNode {
125119
@Specialization
126120
protected Object get(PCode self) {
127-
return self.getPositionalOnlyArgCount();
121+
return self.co_posonlyargcount();
128122
}
129123
}
130124

131125
@Builtin(name = "co_kwonlyargcount", minNumOfPositionalArgs = 1, isGetter = true)
132126
@GenerateNodeFactory
133-
public abstract static class GetKnownlyArgCountNode extends PythonBuiltinNode {
127+
public abstract static class GetKnownlyArgCountNode extends PythonUnaryBuiltinNode {
134128
@Specialization
135129
protected Object get(PCode self) {
136-
return self.getKwonlyargcount();
130+
return self.co_kwonlyargcount();
137131
}
138132
}
139133

140134
@Builtin(name = "co_nlocals", minNumOfPositionalArgs = 1, isGetter = true)
141135
@GenerateNodeFactory
142-
public abstract static class GetNLocalsNode extends PythonBuiltinNode {
136+
public abstract static class GetNLocalsNode extends PythonUnaryBuiltinNode {
143137
@Specialization
144138
protected Object get(PCode self) {
145-
return self.getNlocals();
139+
return self.co_nlocals();
146140
}
147141
}
148142

149143
@Builtin(name = "co_stacksize", minNumOfPositionalArgs = 1, isGetter = true)
150144
@GenerateNodeFactory
151-
public abstract static class GetStackSizeNode extends PythonBuiltinNode {
145+
public abstract static class GetStackSizeNode extends PythonUnaryBuiltinNode {
152146
@Specialization
153147
protected Object get(PCode self) {
154148
return self.getStacksize();
@@ -157,68 +151,52 @@ protected Object get(PCode self) {
157151

158152
@Builtin(name = "co_flags", minNumOfPositionalArgs = 1, isGetter = true)
159153
@GenerateNodeFactory
160-
public abstract static class GetFlagsNode extends PythonBuiltinNode {
154+
public abstract static class GetFlagsNode extends PythonUnaryBuiltinNode {
161155
@Specialization
162156
protected Object get(PCode self) {
163-
return self.getFlags();
157+
return self.co_flags();
164158
}
165159
}
166160

167161
@Builtin(name = "co_code", minNumOfPositionalArgs = 1, isGetter = true)
168162
@GenerateNodeFactory
169-
public abstract static class GetCodeNode extends PythonBuiltinNode {
163+
public abstract static class GetCodeNode extends PythonUnaryBuiltinNode {
170164
@Specialization
171165
protected Object get(PCode self) {
172-
byte[] codestring = self.getCodestring();
173-
if (codestring == null) {
174-
codestring = new byte[0];
175-
}
176-
return factory().createBytes(codestring);
166+
return self.co_code(factory());
177167
}
178168
}
179169

180170
@Builtin(name = "co_consts", minNumOfPositionalArgs = 1, isGetter = true)
181171
@GenerateNodeFactory
182-
public abstract static class GetConstsNode extends PythonBuiltinNode {
172+
public abstract static class GetConstsNode extends PythonUnaryBuiltinNode {
183173
@Specialization
184174
protected Object get(PCode self) {
185-
Object[] constants = self.getConstants();
186-
if (constants == null) {
187-
constants = new Object[0];
188-
}
189-
return factory().createTuple(constants);
175+
return self.co_consts(factory());
190176
}
191177
}
192178

193179
@Builtin(name = "co_names", minNumOfPositionalArgs = 1, isGetter = true)
194180
@GenerateNodeFactory
195-
public abstract static class GetNamesNode extends PythonBuiltinNode {
181+
public abstract static class GetNamesNode extends PythonUnaryBuiltinNode {
196182
@Specialization
197183
protected Object get(PCode self) {
198-
Object[] names = self.getNames();
199-
if (names == null) {
200-
names = new Object[0];
201-
}
202-
return factory().createTuple(names);
184+
return self.co_names(factory());
203185
}
204186
}
205187

206188
@Builtin(name = "co_varnames", minNumOfPositionalArgs = 1, isGetter = true)
207189
@GenerateNodeFactory
208-
public abstract static class GetVarNamesNode extends PythonBuiltinNode {
190+
public abstract static class GetVarNamesNode extends PythonUnaryBuiltinNode {
209191
@Specialization
210192
protected Object get(PCode self) {
211-
Object[] varNames = self.getVarnames();
212-
if (varNames != null) {
213-
return factory().createTuple(varNames);
214-
}
215-
return PNone.NONE;
193+
return self.co_varnames(factory());
216194
}
217195
}
218196

219197
@Builtin(name = "co_lnotab", minNumOfPositionalArgs = 1, isGetter = true)
220198
@GenerateNodeFactory
221-
public abstract static class GetLNoTabNode extends PythonBuiltinNode {
199+
public abstract static class GetLNoTabNode extends PythonUnaryBuiltinNode {
222200
@Specialization
223201
protected Object get(PCode self) {
224202
byte[] lnotab = self.getLnotab();
@@ -229,4 +207,40 @@ protected Object get(PCode self) {
229207
return factory().createBytes(lnotab);
230208
}
231209
}
210+
211+
@Builtin(name = __REPR__, minNumOfPositionalArgs = 1)
212+
@GenerateNodeFactory
213+
public abstract static class CodeReprNode extends PythonUnaryBuiltinNode {
214+
@Specialization
215+
Object repr(PCode self) {
216+
return self.toString();
217+
}
218+
}
219+
220+
@Builtin(name = __HASH__, minNumOfPositionalArgs = 1)
221+
@GenerateNodeFactory
222+
public abstract static class CodeHashNode extends PythonUnaryBuiltinNode {
223+
@Specialization
224+
long hash(PCode self,
225+
@CachedLibrary(limit = "getCallSiteInlineCacheMaxDepth()") PythonObjectLibrary pol) {
226+
long h, h0, h1, h2, h3, h4, h5, h6;
227+
PythonObjectFactory factory = factory();
228+
229+
h0 = pol.hash(self.co_name());
230+
h1 = pol.hash(self.co_code(factory));
231+
h2 = pol.hash(self.co_consts(factory));
232+
h3 = pol.hash(self.co_names(factory));
233+
h4 = pol.hash(self.co_varnames(factory));
234+
h5 = pol.hash(self.co_freevars(factory));
235+
h6 = pol.hash(self.co_cellvars(factory));
236+
237+
h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
238+
self.co_argcount() ^ self.co_posonlyargcount() ^ self.co_kwonlyargcount() ^
239+
self.co_nlocals() ^ self.co_flags();
240+
if (h == -1) {
241+
h = -2;
242+
}
243+
return h;
244+
}
245+
}
232246
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/PCode.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@
4848

4949
import com.oracle.graal.python.PythonLanguage;
5050
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
51+
import com.oracle.graal.python.builtins.objects.PNone;
52+
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
53+
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
5154
import com.oracle.graal.python.builtins.objects.function.Signature;
5255
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
56+
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
5357
import com.oracle.graal.python.nodes.ModuleRootNode;
5458
import com.oracle.graal.python.nodes.PClosureFunctionRootNode;
5559
import com.oracle.graal.python.nodes.PClosureRootNode;
@@ -64,6 +68,7 @@
6468
import com.oracle.graal.python.nodes.generator.GeneratorFunctionRootNode;
6569
import com.oracle.graal.python.nodes.literal.SimpleLiteralNode;
6670
import com.oracle.graal.python.runtime.PythonCodeSerializer;
71+
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
6772
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6873
import com.oracle.truffle.api.RootCallTarget;
6974
import com.oracle.truffle.api.interop.UnsupportedMessageException;
@@ -487,4 +492,89 @@ private SourceSection readSourceLocation() {
487492
public boolean hasSourceLocation() {
488493
return readSourceLocation() != null;
489494
}
495+
496+
@Override
497+
@TruffleBoundary
498+
public String toString() {
499+
String codeName = this.getName() == null ? "None" : this.getName();
500+
String codeFilename = this.getFilename() == null ? "None" : this.getFilename();
501+
int codeFirstLineNo = this.getFirstLineNo() == 0 ? -1 : this.getFirstLineNo();
502+
return String.format("<code object %s, file \"%s\", line %d>", codeName, codeFilename, codeFirstLineNo);
503+
}
504+
505+
public Object co_name() {
506+
String codeName = this.getName();
507+
if (codeName != null) {
508+
return codeName;
509+
}
510+
return PNone.NONE;
511+
}
512+
513+
public PBytes co_code(PythonObjectFactory factory) {
514+
byte[] codeCodeString = this.getCodestring();
515+
if (codeCodeString == null) {
516+
codeCodeString = new byte[0];
517+
}
518+
return factory.createBytes(codeCodeString);
519+
}
520+
521+
public PTuple co_consts(PythonObjectFactory factory) {
522+
Object[] codeConstants = this.getConstants();
523+
if (codeConstants == null) {
524+
codeConstants = new Object[0];
525+
}
526+
return factory.createTuple(codeConstants);
527+
}
528+
529+
public PTuple co_names(PythonObjectFactory factory) {
530+
Object[] codeNames = this.getNames();
531+
if (codeNames == null) {
532+
codeNames = new Object[0];
533+
}
534+
return factory.createTuple(codeNames);
535+
}
536+
537+
public PythonAbstractObject co_varnames(PythonObjectFactory factory) {
538+
Object[] codeVarNames = this.getVarnames();
539+
if (codeVarNames != null) {
540+
return factory.createTuple(codeVarNames);
541+
}
542+
return PNone.NONE;
543+
}
544+
545+
public PythonAbstractObject co_freevars(PythonObjectFactory factory) {
546+
Object[] codeFreeVars = this.getFreeVars();
547+
if (codeFreeVars != null) {
548+
return factory.createTuple(codeFreeVars);
549+
}
550+
return PNone.NONE;
551+
}
552+
553+
public PythonAbstractObject co_cellvars(PythonObjectFactory factory) {
554+
Object[] codeCellVars = this.getCellVars();
555+
if (codeCellVars != null) {
556+
return factory.createTuple(codeCellVars);
557+
}
558+
return PNone.NONE;
559+
}
560+
561+
public int co_argcount() {
562+
return this.getArgcount();
563+
}
564+
565+
public int co_posonlyargcount() {
566+
return this.getPositionalOnlyArgCount();
567+
}
568+
569+
public int co_kwonlyargcount() {
570+
return this.getKwonlyargcount();
571+
}
572+
573+
public int co_nlocals() {
574+
return this.getNlocals();
575+
}
576+
577+
public int co_flags() {
578+
return this.getFlags();
579+
}
490580
}

graalpython/lib-graalpython/code.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ def a_function(): pass
4141
codetype = type(a_function.__code__)
4242

4343

44-
def co_repr(self):
45-
return '<code object %s, file "%s", line %d>' % (self.co_name, self.co_filename, self.co_firstlineno)
46-
47-
4844
def co_replace(self, **kwargs):
4945
import types
5046
return types.CodeType(
@@ -67,5 +63,4 @@ def co_replace(self, **kwargs):
6763
)
6864

6965

70-
codetype.__repr__ = co_repr
7166
codetype.replace = co_replace

0 commit comments

Comments
 (0)