Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
8d6c180c
Commit
8d6c180c
authored
Jan 18, 1995
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fully adapted to new naming scheme and added some features for AppleEvent generation
parent
8cfc4bfb
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
137 additions
and
83 deletions
+137
-83
Tools/bgen/bgen/bgenGenerator.py
Tools/bgen/bgen/bgenGenerator.py
+7
-4
Tools/bgen/bgen/bgenGeneratorGroup.py
Tools/bgen/bgen/bgenGeneratorGroup.py
+1
-1
Tools/bgen/bgen/bgenModule.py
Tools/bgen/bgen/bgenModule.py
+19
-13
Tools/bgen/bgen/bgenObjectDefinition.py
Tools/bgen/bgen/bgenObjectDefinition.py
+65
-57
Tools/bgen/bgen/bgenType.py
Tools/bgen/bgen/bgenType.py
+45
-8
No files found.
Tools/bgen/bgen/bgenGenerator.py
View file @
8d6c180c
...
...
@@ -22,7 +22,7 @@ class Generator:
def
__init__
(
self
,
*
argumentList
):
apply
(
self
.
parseArguments
,
argumentList
)
self
.
prefix
=
"XXX"
# Will be changed by setprefix() call
self
.
objecttype
=
"
o
bject"
# Type of _self argument to function
self
.
objecttype
=
"
PyO
bject"
# Type of _self argument to function
self
.
itselftype
=
None
# Type of _self->ob_itself, if defined
def
setprefix
(
self
,
prefix
):
...
...
@@ -92,12 +92,13 @@ class Generator:
def
getargs
(
self
):
fmt
=
""
lst
=
""
sep
=
",
\
n
"
+
' '
*
len
(
"if (!PyArg_ParseTuple("
)
for
arg
in
self
.
argumentList
:
if
arg
.
flags
==
SelfMode
:
continue
if
arg
.
mode
in
(
InMode
,
InOutMode
):
fmt
=
fmt
+
arg
.
getargsFormat
()
lst
=
lst
+
", "
+
arg
.
getargsArgs
()
lst
=
lst
+
sep
+
arg
.
getargsArgs
()
Output
(
"if (!PyArg_ParseTuple(_args,
\
"
%s
\
"
%s))"
,
fmt
,
lst
)
IndentLevel
()
Output
(
"return NULL;"
)
...
...
@@ -110,11 +111,12 @@ class Generator:
def
callit
(
self
):
args
=
""
sep
=
",
\
n
"
+
' '
*
len
(
"%s = %s("
%
(
self
.
rv
.
name
,
self
.
name
))
for
arg
in
self
.
argumentList
:
if
arg
is
self
.
rv
:
continue
s
=
arg
.
passArgument
()
if
args
:
s
=
", "
+
s
if
args
:
s
=
sep
+
s
args
=
args
+
s
if
self
.
rv
:
Output
(
"%s = %s(%s);"
,
...
...
@@ -129,12 +131,13 @@ class Generator:
def
returnvalue
(
self
):
fmt
=
""
lst
=
""
sep
=
",
\
n
"
+
' '
*
len
(
"return Py_BuildValue("
)
for
arg
in
self
.
argumentList
:
if
not
arg
:
continue
if
arg
.
flags
==
ErrorMode
:
continue
if
arg
.
mode
in
(
OutMode
,
InOutMode
):
fmt
=
fmt
+
arg
.
mkvalueFormat
()
lst
=
lst
+
", "
+
arg
.
mkvalueArgs
()
lst
=
lst
+
sep
+
arg
.
mkvalueArgs
()
if
fmt
==
""
:
Output
(
"Py_INCREF(Py_None);"
)
Output
(
"return Py_None;"
);
...
...
Tools/bgen/bgen/bgenGeneratorGroup.py
View file @
8d6c180c
...
...
@@ -14,7 +14,7 @@ class GeneratorGroup:
for
g
in
self
.
generators
:
g
.
generate
()
Output
()
Output
(
"static
struct methodlist
%s_methods[] = {"
,
self
.
prefix
)
Output
(
"static
PyMethodDef
%s_methods[] = {"
,
self
.
prefix
)
IndentLevel
()
for
g
in
self
.
generators
:
g
.
reference
()
...
...
Tools/bgen/bgen/bgenModule.py
View file @
8d6c180c
...
...
@@ -5,18 +5,20 @@ class Module(GeneratorGroup):
def
__init__
(
self
,
name
,
prefix
=
None
,
includestuff
=
None
,
initstuff
=
None
):
initstuff
=
None
,
preinitstuff
=
None
):
GeneratorGroup
.
__init__
(
self
,
prefix
or
name
)
self
.
name
=
name
self
.
includestuff
=
includestuff
self
.
initstuff
=
initstuff
self
.
preinitstuff
=
preinitstuff
def
addobject
(
self
,
od
):
self
.
generators
.
append
(
od
)
def
generate
(
self
):
OutHeader1
(
"Module "
+
self
.
name
)
Output
(
"#include
<Python.h>
"
)
Output
(
"#include
\
"
Python.h
\
"
"
)
Output
()
if
self
.
includestuff
:
...
...
@@ -26,36 +28,40 @@ class Module(GeneratorGroup):
self
.
declareModuleVariables
()
GeneratorGroup
.
generate
(
self
)
if
self
.
preinitstuff
:
Output
()
Output
(
"%s"
,
self
.
preinitstuff
)
Output
()
Output
(
"void init%s()"
,
self
.
name
)
OutLbrace
()
Output
(
"
o
bject *m;"
)
Output
(
"
o
bject *d;"
)
Output
(
"
PyO
bject *m;"
)
Output
(
"
PyO
bject *d;"
)
Output
()
if
self
.
initstuff
:
Output
(
"%s"
,
self
.
initstuff
)
Output
()
Output
(
"m =
initm
odule(
\
"
%s
\
"
, %s_methods);"
,
Output
(
"m =
Py_InitM
odule(
\
"
%s
\
"
, %s_methods);"
,
self
.
name
,
self
.
prefix
)
Output
(
"d =
getmoduled
ict(m);"
)
Output
(
"d =
PyModule_GetD
ict(m);"
)
self
.
createModuleVariables
()
OutRbrace
()
OutHeader1
(
"End module "
+
self
.
name
)
def
declareModuleVariables
(
self
):
self
.
errorname
=
self
.
prefix
+
"_Error"
Output
(
"static
o
bject *%s;"
,
self
.
errorname
)
Output
(
"static
PyO
bject *%s;"
,
self
.
errorname
)
def
createModuleVariables
(
self
):
Output
(
"""if ((%s =
newstringobject
("%s.Error")) == NULL ||"""
,
self
.
errorname
,
self
.
name
)
Output
(
"""
dictinsert
(d, "Error", %s) != 0)"""
,
self
.
errorname
)
Output
(
"""
\
t
fatal
("can't initialize %s.Error");"""
,
self
.
name
)
Output
(
"""if ((%s =
PyString_FromString
("%s.Error")) == NULL ||"""
,
self
.
errorname
,
self
.
name
)
Output
(
"""
PyDict_SetItemString
(d, "Error", %s) != 0)"""
,
self
.
errorname
)
Output
(
"""
Py_FatalError
("can't initialize %s.Error");"""
,
self
.
name
)
def
_test
():
...
...
Tools/bgen/bgen/bgenObjectDefinition.py
View file @
8d6c180c
...
...
@@ -9,9 +9,8 @@ class ObjectDefinition(GeneratorGroup):
self
.
name
=
name
self
.
itselftype
=
itselftype
self
.
objecttype
=
name
+
'Object'
self
.
typename
=
self
.
prefix
+
'_'
+
\
string
.
upper
(
name
[:
1
])
+
\
string
.
lower
(
name
[
1
:])
+
'_Type'
self
.
typename
=
name
+
'_Type'
self
.
argref
=
""
# set to "*" if arg to <type>_New should be pointer
def
add
(
self
,
g
):
g
.
setselftype
(
self
.
objecttype
,
self
.
itselftype
)
...
...
@@ -29,87 +28,76 @@ class ObjectDefinition(GeneratorGroup):
Output
(
"staticforward PyTypeObject %s;"
,
self
.
typename
)
Output
()
Output
(
"#define
is_%sobject(x) ((x)->ob_type ==
%s)"
,
self
.
name
,
self
.
typename
)
Output
(
"#define
%s_Check(x) ((x)->ob_type == &
%s)"
,
self
.
prefix
,
self
.
typename
)
Output
()
Output
(
"typedef struct {"
)
IndentLevel
()
Output
(
"
OB
_HEAD"
)
Output
(
"
PyObject
_HEAD"
)
Output
(
"%s ob_itself;"
,
self
.
itselftype
)
DedentLevel
()
Output
(
"} %s;"
,
self
.
objecttype
)
Output
()
Output
(
"static %s *new%s(itself)"
,
self
.
objecttype
,
self
.
objecttype
)
self
.
outputNew
()
self
.
outputConvert
()
self
.
outputDealloc
()
GeneratorGroup
.
generate
(
self
)
self
.
outputGetattr
()
self
.
outputSetattr
()
self
.
outputTypeObject
()
OutHeader2
(
"End object type "
+
self
.
name
)
def
outputNew
(
self
):
Output
(
"static %s *%s_New(itself)"
,
self
.
objecttype
,
self
.
prefix
)
IndentLevel
()
Output
(
"
%s itself;"
,
self
.
itselftype
)
Output
(
"
const %s %sitself;"
,
self
.
itselftype
,
self
.
argref
)
DedentLevel
()
OutLbrace
()
Output
(
"%s *it;"
,
self
.
objecttype
)
Output
(
"it = PyObject_NEW(%s, &%s);"
,
self
.
objecttype
,
self
.
typename
)
Output
(
"if (it == NULL) return NULL;"
)
Output
(
"it->ob_itself =
itself;"
)
Output
(
"it->ob_itself =
%sitself;"
,
self
.
argref
)
Output
(
"return it;"
)
OutRbrace
()
Output
()
def
outputConvert
(
self
):
Output
(
"""
\
static int %(prefix)s_Convert(v, p_itself)
PyObject *v;
%(itselftype)s *p_itself;
{
if (v == NULL || !%(prefix)s_Check(v)) {
PyErr_SetString(PyExc_TypeError, "%(name)s required");
return 0;
}
*p_itself = ((%(objecttype)s *)v)->ob_itself;
return 1;
}
"""
%
self
.
__dict__
)
def
outputDealloc
(
self
):
Output
(
"static void %s_dealloc(self)"
,
self
.
prefix
)
IndentLevel
()
Output
(
"%s *self;"
,
self
.
objecttype
)
DedentLevel
()
OutLbrace
()
## Output("if (self->ob_itself != NULL)")
## OutLbrace()
## self.outputFreeIt("self->ob_itself")
## OutRbrace()
Output
(
"DEL(self);"
)
self
.
outputFreeIt
(
"self->ob_itself"
)
Output
(
"PyMem_DEL(self);"
)
OutRbrace
()
Output
()
## Output("static int %s_converter(p_itself, p_it)", self.prefix)
## IndentLevel()
## Output("%s *p_itself;", self.itselftype)
## Output("%s **p_it;", self.objecttype)
## DedentLevel()
## OutLbrace()
## Output("if (**p_it == NULL)")
## OutLbrace()
## Output("*p_it = new%s(*p_itself);", self.objecttype)
## OutRbrace()
## Output("else")
## OutLbrace()
## Output("*p_itself = (*p_it)->ob_itself;")
## OutRbrace()
## Output("return 1;")
## OutRbrace()
## Output()
GeneratorGroup
.
generate
(
self
)
self
.
outputGetattr
()
self
.
outputSetattr
()
Output
(
"static PyTypeObject %s = {"
,
self
.
typename
)
IndentLevel
()
Output
(
"PyObject_HEAD_INIT(&PyType_Type)"
)
Output
(
"0, /*ob_size*/"
)
Output
(
"
\
"
%s
\
"
, /*tp_name*/"
,
self
.
name
)
Output
(
"sizeof(%s), /*tp_basicsize*/"
,
self
.
objecttype
)
Output
(
"0, /*tp_itemsize*/"
)
Output
(
"/* methods */"
)
Output
(
"(destructor) %s_dealloc, /*tp_dealloc*/"
,
self
.
prefix
)
Output
(
"0, /*tp_print*/"
)
Output
(
"(getattrfunc) %s_getattr, /*tp_getattr*/"
,
self
.
prefix
)
Output
(
"(setattrfunc) %s_setattr, /*tp_setattr*/"
,
self
.
prefix
)
DedentLevel
()
Output
(
"};"
)
OutHeader2
(
"End object type "
+
self
.
name
)
def
outputFreeIt
(
self
,
name
):
Output
(
"
DEL(%s); /* XXX
*/"
,
name
)
Output
(
"
/* Cleanup of %s goes here
*/"
,
name
)
def
outputGetattr
(
self
):
Output
(
"static PyObject *%s_getattr(self, name)"
,
self
.
prefix
)
...
...
@@ -123,8 +111,28 @@ class ObjectDefinition(GeneratorGroup):
Output
()
def
outputGetattrBody
(
self
):
Output
(
"return findmethod(self, %s_methods, name);"
,
self
.
prefix
)
self
.
outputGetattrHook
()
Output
(
"return Py_FindMethod(%s_methods, (PyObject *)self, name);"
,
self
.
prefix
)
def
outputGetattrHook
(
self
):
pass
def
outputSetattr
(
self
):
Output
(
"#define %s_setattr NULL"
,
self
.
prefix
)
Output
()
def
outputTypeObject
(
self
):
Output
(
"static PyTypeObject %s = {"
,
self
.
typename
)
IndentLevel
()
Output
(
"PyObject_HEAD_INIT(&PyType_Type)"
)
Output
(
"0, /*ob_size*/"
)
Output
(
"
\
"
%s
\
"
, /*tp_name*/"
,
self
.
name
)
Output
(
"sizeof(%s), /*tp_basicsize*/"
,
self
.
objecttype
)
Output
(
"0, /*tp_itemsize*/"
)
Output
(
"/* methods */"
)
Output
(
"(destructor) %s_dealloc, /*tp_dealloc*/"
,
self
.
prefix
)
Output
(
"0, /*tp_print*/"
)
Output
(
"(getattrfunc) %s_getattr, /*tp_getattr*/"
,
self
.
prefix
)
Output
(
"(setattrfunc) %s_setattr, /*tp_setattr*/"
,
self
.
prefix
)
DedentLevel
()
Output
(
"};"
)
Tools/bgen/bgen/bgenType.py
View file @
8d6c180c
...
...
@@ -39,6 +39,9 @@ class Type:
"""
Output
(
"%s %s;"
,
self
.
typeName
,
name
)
def
getargs
(
self
):
return
self
.
getargsFormat
(),
self
.
getargsArgs
()
def
getargsFormat
(
self
):
"""Return the format for this type for use with [new]getargs().
...
...
@@ -85,6 +88,9 @@ class Type:
"""
Output
(
"/* XXX no err check for %s %s */"
,
self
.
typeName
,
name
)
def
mkvalue
(
self
):
return
self
.
mkvalueFormat
(),
self
.
mkvalueArgs
()
def
mkvalueFormat
(
self
):
"""Return the format for this type for use with mkvalue().
...
...
@@ -113,8 +119,8 @@ char = Type("char", "c")
# Some Python related types.
objectptr
=
Type
(
"
o
bject*"
,
"O"
)
stringobjectptr
=
Type
(
"
stringo
bject*"
,
"S"
)
objectptr
=
Type
(
"
PyO
bject*"
,
"O"
)
stringobjectptr
=
Type
(
"
PyStringO
bject*"
,
"S"
)
# Etc.
...
...
@@ -125,7 +131,7 @@ class SizedInputBuffer:
"Sized input buffer -- buffer size is an input parameter"
def
__init__
(
self
,
size
):
def
__init__
(
self
,
size
=
''
):
self
.
size
=
size
def
declare
(
self
,
name
):
...
...
@@ -136,7 +142,7 @@ class SizedInputBuffer:
return
"s#"
def
getargsArgs
(
self
,
name
):
return
"
%s,
%s__len__"
%
(
name
,
name
)
return
"
&%s, &
%s__len__"
%
(
name
,
name
)
def
getargsCheck
(
self
,
name
):
pass
...
...
@@ -151,14 +157,28 @@ class FixedInputBuffer(SizedInputBuffer):
def
getargsCheck
(
self
,
name
):
Output
(
"if (%s__len__ != %s)"
,
name
,
str
(
self
.
size
))
IndentLevel
()
Output
(
'err_setstr(TypeError, "bad string length");'
)
DedentLevel
()
OutLbrace
()
Output
(
'PyErr_SetString(PyExc_TypeError, "bad string length");'
)
Output
(
'return NULL;'
)
OutRbrace
()
def
passInput
(
self
,
name
):
return
name
class
RecordBuffer
(
FixedInputBuffer
):
"Like fixed input buffer -- but declared as a record type pointer"
def
__init__
(
self
,
type
):
self
.
type
=
type
self
.
size
=
"sizeof(%s)"
%
type
def
declare
(
self
,
name
):
Output
(
"%s *%s;"
,
self
.
type
,
name
)
Output
(
"int %s__len__;"
,
name
)
class
SizedOutputBuffer
:
"Sized output buffer -- buffer size is an input-output parameter"
...
...
@@ -183,6 +203,23 @@ class SizedOutputBuffer:
return
"%s, %s__len__"
%
(
name
,
name
)
class
VarSizedOutputBuffer
(
SizedOutputBuffer
):
"Variable Sized output buffer -- buffer size is an input and an output parameter"
def
getargsFormat
(
self
):
return
"i"
def
getargsArgs
(
self
,
name
):
return
"&%s__len__"
%
name
def
getargsCheck
(
self
,
name
):
pass
def
passOutput
(
self
,
name
):
return
"%s, %s__len__, &%s__len__"
%
(
name
,
name
,
name
)
class
FixedOutputBuffer
:
"Fixed output buffer -- buffer size is a constant"
...
...
@@ -266,7 +303,7 @@ class StructureByValue:
def
getargsCheck
(
self
,
name
):
Output
(
"if (%s__len__ != sizeof %s)"
,
name
,
name
)
IndentLevel
()
Output
(
'
err_setstr(
TypeError, "bad structure length");'
)
Output
(
'
PyErr_SetString(PyExc_
TypeError, "bad structure length");'
)
DedentLevel
()
Output
(
"memcpy(&%s, %s__str__, %s__len__);"
,
name
,
name
,
name
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment