8000 More python types · go-python/gpython@0827e7c · GitHub
[go: up one dir, main page]

Skip to content

Commit 0827e7c

Browse files
committed
More python types
1 parent f65c2cd commit 0827e7c

File tree

3 files changed

+276
-0
lines changed

3 files changed

+276
-0
lines changed

py/code.go

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// Code objects
2+
3+
package py
4+
5+
import (
6+
"fmt"
7+
"strings"
8+
)
9+
10+
// Code object
11+
type Code struct {
12+
// Object_HEAD
13+
co_argcount int // #arguments, except *args
14+
co_kwonlyargcount int // #keyword only arguments
15+
co_nlocals int // #local variables
16+
co_stacksize int // #entries needed for evaluation stack
17+
co_flags int // CO_..., see below
18+
co_code Object // instruction opcodes
19+
co_consts Object // list (constants used)
20+
co_names Object // list of strings (names used)
21+
co_varnames Object // tuple of strings (local variable names)
22+
co_freevars Object // tuple of strings (free variable names)
23+
co_cellvars Object // tuple of strings (cell variable names)
24+
// The rest doesn't count for hash or comparisons
25+
co_cell2arg *byte // Maps cell vars which are arguments.
26+
co_filename Object // unicode (where it was loaded from)
27+
co_name Object // unicode (name, for reference)
28+
co_firstlineno int // first source line number
29+
co_lnotab Object // string (encoding addr<->lineno mapping) See Objects/lnotab_notes.txt for details.
30+
31+
co_weakreflist Object // to support weakrefs to code objects
32+
}
33+
34+
const (
35+
// Masks for co_flags above
36+
CO_OPTIMIZED = 0x0001
37+
CO_NEWLOCALS = 0x0002
38+
CO_VARARGS = 0x0004
39+
CO_VARKEYWORDS = 0x0008
40+
CO_NESTED = 0x0010
41+
CO_GENERATOR = 0x0020
42+
43+
// The CO_NOFREE flag is set if there are no free or cell
44+
// variables. This information is redundant, but it allows a
45+
// single flag test to determine whether there is any extra
46+
// work to be done when the call frame it setup.
47+
48+
CO_NOFREE = 0x0040
49+
CO_GENERATOR_ALLOWED = 0x1000
50+
CO_FUTURE_DIVISION = 0x2000
51+
CO_FUTURE_ABSOLUTE_IMPORT = 0x4000 // do absolute imports by default
52+
CO_FUTURE_WITH_STATEMENT = 0x8000
53+
CO_FUTURE_PRINT_FUNCTION = 0x10000
54+
CO_FUTURE_UNICODE_LITERALS = 0x20000
55+
CO_FUTURE_BARRY_AS_BDFL = 0x40000
56+
57+
// This value is found in the co_cell2arg array when the
58+
// associated cell variable does not correspond to an
59+
// argument. The maximum number of arguments is 255 (indexed
60+
// up to 254), so 255 work as a special flag.
61+
CO_CELL_NOT_AN_ARG = 255
62+
63+
CO_MAXBLOCKS = 20 // Max static block nesting within a function
64+
)
65+
66+
func intern_strings(tuple Tuple) {
67+
for _, v_ := range tuple {
68+
v := v_.(String)
69+
fmt.Printf("Interning '%s'\n", v)
70+
// FIXME
71+
//PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
72+
}
73+
}
74+
75+
const NAME_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
76+
77+
// all_name_chars(s): true iff all chars in s are valid NAME_CHARS
78+
func all_name_chars(s String) bool {
79+
for _, c := range s {
80+
if strings.IndexRune(NAME_CHARS, c) < 0 {
81+
return false
82+
}
83+
}
84+
return true
85+
}
86+
87+
// Make a new code object
88+
func NewCode(argcount int, kwonlyargcount int,
89+
nlocals int, stacksize int, flags int,
90+
code Object, consts_ Object, names_ Object,
91+
varnames_ Object, freevars_ Object, cellvars_ Object,
92+
filename_ Object, name_ Object, firstlineno int,
93+
lnotab_ Object) (co *Code) {
94+
95+
var cell2arg *byte
96+
97+
// Type assert the objects
98+
consts := consts_.(Tuple)
99+
names := names_.(Tuple)
100+
varnames := varnames_.(Tuple)
101+
freevars := freevars_.(Tuple)
102+
cellvars := cellvars_.(Tuple)
103+
name := name_.(String)
104+
filename := filename_.(String)
105+
lnotab := lnotab_.(Bytes)
106+
107+
// Check argument types
108+
if argcount < 0 || kwonlyargcount < 0 || nlocals < 0 {
109+
panic("Bad arguments to NewCode")
110+
}
111+
112+
// Ensure that the filename is a ready Unicode string
113+
// FIXME
114+
// if PyUnicode_READY(filename) < 0 {
115+
// return nil;
116+
// }
117+
118+
n_cellvars := len(cellvars)
119+
intern_strings(names)
120+
intern_strings(varnames)
121+
intern_strings(freevars)
122+
intern_strings(cellvars)
123+
/* Intern selected string constants */
124+
for i := len(consts) - 1; i >= 0; i-- {
125+
v := consts[i].(String)
126+
if !all_name_chars(v) {
127+
continue
128+
}
129+
// FIXME PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i));
130+
}
131+
/* Create mapping between cells and arguments if needed. */
132+
if n_cellvars != 0 {
133+
total_args := argcount + kwonlyargcount
134+
if (flags & CO_VARARGS) != 0 {
135+
total_args++
136+
}
137+
if (flags & CO_VARKEYWORDS) != 0 {
138+
total_args++
139+
}
140+
used_cell2arg := false
141+
cell2arg := make([]byte, n_cellvars)
142+
for i := range cell2arg {
143+
cell2arg[i] = CO_CELL_NOT_AN_ARG
144+
}
145+
// Find cells which are also arguments.
146+
for i, cell := range cellvars {
147+
for j := 0; j < total_args; j++ {
148+
arg := varnames[j]
149+
if cell != arg {
150+
cell2arg[i] = byte(j)
151+
used_cell2arg = true
152+
break
153+
}
154+
}
155+
}
156+
if !used_cell2arg {
157+
cell2arg = nil
158+
}
159+
}
160+
161+
// FIXME co = PyObject_NEW(PyCodeObject, &PyCode_Type);
162+
163+
co.co_argcount = argcount
164+
co.co_kwonlyargcount = kwonlyargcount
165+
co.co_nlocals = nlocals
166+
co.co_stacksize = stacksize
167+
co.co_flags = flags
168+
co.co_code = code
169+
co.co_consts = consts
170+
co.co_names = names
171+
co.co_varnames = varnames
172+
co.co_freevars = freevars
173+
co.co_cellvars = cellvars
174+
co.co_cell2arg = cell2arg
175+
co.co_filename = filename
176+
co.co_name = name
177+
co.co_firstlineno = firstlineno
178+
co.co_lnotab = lnotab
179+
co.co_weakreflist = nil
180+
return co
181+
}

py/py.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,12 @@ type Object interface{}
88
var (
99
None, False, True, StopIteration, Elipsis Object
1010
)
11+
12+
// Some python types
13+
// FIXME factor into own files probably
14+
15+
type Tuple []Object
16+
type List []Object
17+
type Set []Object
18+
type Bytes []byte
19+
type String string

py/type.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Type objects - these make objects
2+
3+
package py
4+
5+
type Type struct {
6+
/*
7+
PyObject_VAR_HEAD
8+
const char *tp_name; // For printing, in format "<module>.<name>"
9+
Py_ssize_t tp_basicsize, tp_itemsize; // For allocation
10+
11+
// Methods to implement standard operations
12+
13+
destructor tp_dealloc;
14+
printfunc tp_print;
15+
getattrfunc tp_getattr;
16+
setattrfunc tp_setattr;
17+
void *tp_reserved; // formerly known as tp_compare
18+
reprfunc tp_repr;
19+
20+
// Method suites for standard classes
21+
22+
PyNumberMethods *tp_as_number;
23+
PySequenceMethods *tp_as_sequence;
24+
PyMappingMethods *tp_as_mapping;
25+
26+
// More standard operations (here for binary compatibility)
27+
28+
hashfunc tp_hash;
29+
ternaryfunc tp_call;
30+
reprfunc tp_str;
31+
getattrofunc tp_getattro;
32+
setattrofunc tp_setattro;
33+
34+
// Functions to access object as input/output buffer
35+
PyBufferProcs *tp_as_buffer;
36+
37+
// Flags to define presence of optional/expanded features
38+
unsigned long tp_flags;
39+
40+
const char *tp_doc; // Documentation string
41+
42+
// Assigned meaning in release 2.0
43+
// call function for all accessible objects
44+
traverseproc tp_traverse;
45+
46+
// delete references to contained objects
47+
inquiry tp_clear;
48+
49+
// Assigned meaning in release 2.1
50+
// rich comparisons
51+
richcmpfunc tp_richcompare;
52+
53+
// weak reference enabler
54+
Py_ssize_t tp_weaklistoffset;
55+
56+
// Iterators
57+
getiterfunc tp_iter;
58+
iternextfunc tp_iternext;
59+
60+
// Attribute descriptor and subclassing stuff
61+
struct PyMethodDef *tp_methods;
62+
struct PyMemberDef *tp_members;
63+
struct PyGetSetDef *tp_getset;
64+
struct _typeobject *tp_base;
65+
PyObject *tp_dict;
66+
descrgetfunc tp_descr_get;
67+
descrsetfunc tp_descr_set;
68+
Py_ssize_t tp_dictoffset;
69+
initproc tp_init;
70+
allocfunc tp_alloc;
71+
newfunc tp_new;
72+
freefunc tp_free; // Low-level free-memory routine
73+
inquiry tp_is_gc; // For PyObject_IS_GC
74+
PyObject *tp_bases;
75+
PyObject *tp_mro; // method resolution order
76+
PyObject *tp_cache;
77+
PyObject *tp_subclasses;
78+
PyObject *tp_weaklist;
79+
destructor tp_del;
80+
81+
// Type attribute cache version tag. Added in version 2.6
82+
unsigned int tp_version_tag;
83+
84+
destructor tp_finalize;
85+
*/
86+
}

0 commit comments

Comments
 (0)
0