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
5574f2d0
Commit
5574f2d0
authored
Apr 29, 1997
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Quickly renamed the remaining files -- this directory is done.
parent
c322b4e3
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
1834 additions
and
1748 deletions
+1834
-1748
Python/bltinmodule.c
Python/bltinmodule.c
+677
-662
Python/compile.c
Python/compile.c
+331
-312
Python/getargs.c
Python/getargs.c
+96
-95
Python/import.c
Python/import.c
+282
-270
Python/importdl.c
Python/importdl.c
+84
-66
Python/importdl.h
Python/importdl.h
+5
-4
Python/marshal.c
Python/marshal.c
+161
-156
Python/modsupport.c
Python/modsupport.c
+106
-100
Python/sigcheck.c
Python/sigcheck.c
+4
-5
Python/structmember.c
Python/structmember.c
+88
-78
No files found.
Python/bltinmodule.c
View file @
5574f2d0
...
...
@@ -31,12 +31,10 @@ PERFORMANCE OF THIS SOFTWARE.
/* Built-in functions */
#include "
allobjects
.h"
#include "
Python
.h"
#include "node.h"
#include "graminit.h"
#include "bltinmodule.h"
#include "import.h"
#include "compile.h"
#include "eval.h"
...
...
@@ -49,104 +47,106 @@ PERFORMANCE OF THIS SOFTWARE.
#endif
/* Forward */
static
object
*
filterstring
PROTO
((
object
*
,
o
bject
*
));
static
object
*
filtertuple
PROTO
((
object
*
,
o
bject
*
));
static
object
*
int_from_string
PROTO
((
o
bject
*
));
static
object
*
long_from_string
PROTO
((
o
bject
*
));
static
object
*
float_from_string
PROTO
((
o
bject
*
));
static
PyObject
*
filterstring
Py_PROTO
((
PyObject
*
,
PyO
bject
*
));
static
PyObject
*
filtertuple
Py_PROTO
((
PyObject
*
,
PyO
bject
*
));
static
PyObject
*
int_from_string
Py_PROTO
((
PyO
bject
*
));
static
PyObject
*
long_from_string
Py_PROTO
((
PyO
bject
*
));
static
PyObject
*
float_from_string
Py_PROTO
((
PyO
bject
*
));
static
o
bject
*
static
PyO
bject
*
builtin___import__
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
char
*
name
;
o
bject
*
globals
=
NULL
;
o
bject
*
locals
=
NULL
;
o
bject
*
fromlist
=
NULL
;
PyO
bject
*
globals
=
NULL
;
PyO
bject
*
locals
=
NULL
;
PyO
bject
*
fromlist
=
NULL
;
if
(
!
newgetargs
(
args
,
"s|OOO:__import__"
,
if
(
!
PyArg_ParseTuple
(
args
,
"s|OOO:__import__"
,
&
name
,
&
globals
,
&
locals
,
&
fromlist
))
return
NULL
;
return
import_m
odule
(
name
);
return
PyImport_ImportM
odule
(
name
);
}
static
o
bject
*
static
PyO
bject
*
builtin_abs
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
number_m
ethods
*
nm
;
PyO
bject
*
v
;
PyNumberM
ethods
*
nm
;
if
(
!
newgetargs
(
args
,
"O:abs"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"O:abs"
,
&
v
))
return
NULL
;
if
((
nm
=
v
->
ob_type
->
tp_as_number
)
==
NULL
)
{
err_setstr
(
TypeError
,
"abs() requires numeric argument"
);
PyErr_SetString
(
PyExc_TypeError
,
"abs() requires numeric argument"
);
return
NULL
;
}
return
(
*
nm
->
nb_absolute
)(
v
);
}
static
o
bject
*
static
PyO
bject
*
builtin_apply
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
func
,
*
alist
=
NULL
,
*
kwdict
=
NULL
;
PyO
bject
*
func
,
*
alist
=
NULL
,
*
kwdict
=
NULL
;
if
(
!
newgetargs
(
args
,
"O|OO:apply"
,
&
func
,
&
alist
,
&
kwdict
))
if
(
!
PyArg_ParseTuple
(
args
,
"O|OO:apply"
,
&
func
,
&
alist
,
&
kwdict
))
return
NULL
;
if
(
alist
!=
NULL
&&
!
is_tupleobject
(
alist
))
{
err_setstr
(
TypeError
,
"apply() 2nd argument must be tuple"
);
if
(
alist
!=
NULL
&&
!
PyTuple_Check
(
alist
))
{
PyErr_SetString
(
PyExc_TypeError
,
"apply() 2nd argument must be tuple"
);
return
NULL
;
}
if
(
kwdict
!=
NULL
&&
!
is_dictobject
(
kwdict
))
{
err_setstr
(
TypeError
,
if
(
kwdict
!=
NULL
&&
!
PyDict_Check
(
kwdict
))
{
PyErr_SetString
(
PyExc_
TypeError
,
"apply() 3rd argument must be dictionary"
);
return
NULL
;
}
return
PyEval_CallObjectWithKeywords
(
func
,
alist
,
kwdict
);
}
static
o
bject
*
static
PyO
bject
*
builtin_callable
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
PyO
bject
*
v
;
if
(
!
newgetargs
(
args
,
"O:callable"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"O:callable"
,
&
v
))
return
NULL
;
return
newintobject
((
long
)
callable
(
v
));
return
PyInt_FromLong
((
long
)
PyCallable_Check
(
v
));
}
static
o
bject
*
static
PyO
bject
*
builtin_filter
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
func
,
*
seq
,
*
result
;
sequence_m
ethods
*
sqf
;
PyO
bject
*
func
,
*
seq
,
*
result
;
PySequenceM
ethods
*
sqf
;
int
len
;
register
int
i
,
j
;
if
(
!
newgetargs
(
args
,
"OO:filter"
,
&
func
,
&
seq
))
if
(
!
PyArg_ParseTuple
(
args
,
"OO:filter"
,
&
func
,
&
seq
))
return
NULL
;
if
(
is_stringobject
(
seq
))
{
o
bject
*
r
=
filterstring
(
func
,
seq
);
if
(
PyString_Check
(
seq
))
{
PyO
bject
*
r
=
filterstring
(
func
,
seq
);
return
r
;
}
if
(
is_tupleobject
(
seq
))
{
o
bject
*
r
=
filtertuple
(
func
,
seq
);
if
(
PyTuple_Check
(
seq
))
{
PyO
bject
*
r
=
filtertuple
(
func
,
seq
);
return
r
;
}
if
((
sqf
=
seq
->
ob_type
->
tp_as_sequence
)
==
NULL
)
{
err_setstr
(
TypeError
,
PyErr_SetString
(
PyExc_
TypeError
,
"argument 2 to filter() must be a sequence type"
);
goto
Fail_2
;
}
...
...
@@ -154,132 +154,133 @@ builtin_filter(self, args)
if
((
len
=
(
*
sqf
->
sq_length
)(
seq
))
<
0
)
goto
Fail_2
;
if
(
is_listobject
(
seq
)
&&
seq
->
ob_refcnt
==
1
)
{
INCREF
(
seq
);
if
(
PyList_Check
(
seq
)
&&
seq
->
ob_refcnt
==
1
)
{
Py_
INCREF
(
seq
);
result
=
seq
;
}
else
{
if
((
result
=
newlistobject
(
len
))
==
NULL
)
if
((
result
=
PyList_New
(
len
))
==
NULL
)
goto
Fail_2
;
}
for
(
i
=
j
=
0
;
;
++
i
)
{
o
bject
*
item
,
*
good
;
PyO
bject
*
item
,
*
good
;
int
ok
;
if
((
item
=
(
*
sqf
->
sq_item
)(
seq
,
i
))
==
NULL
)
{
if
(
i
<
len
)
goto
Fail_1
;
if
(
err_occurred
()
==
IndexError
)
{
err_c
lear
();
if
(
PyErr_Occurred
()
==
PyExc_
IndexError
)
{
PyErr_C
lear
();
break
;
}
goto
Fail_1
;
}
if
(
func
==
None
)
{
if
(
func
==
Py_
None
)
{
good
=
item
;
INCREF
(
good
);
Py_
INCREF
(
good
);
}
else
{
object
*
arg
=
mkv
alue
(
"(O)"
,
item
);
PyObject
*
arg
=
Py_BuildV
alue
(
"(O)"
,
item
);
if
(
arg
==
NULL
)
goto
Fail_1
;
good
=
call_o
bject
(
func
,
arg
);
DECREF
(
arg
);
good
=
PyEval_CallO
bject
(
func
,
arg
);
Py_
DECREF
(
arg
);
if
(
good
==
NULL
)
{
DECREF
(
item
);
Py_
DECREF
(
item
);
goto
Fail_1
;
}
}
ok
=
testbool
(
good
);
DECREF
(
good
);
ok
=
PyObject_IsTrue
(
good
);
Py_
DECREF
(
good
);
if
(
ok
)
{
if
(
j
<
len
)
{
if
(
setlisti
tem
(
result
,
j
++
,
item
)
<
0
)
if
(
PyList_SetI
tem
(
result
,
j
++
,
item
)
<
0
)
goto
Fail_1
;
}
else
{
j
++
;
if
(
addlistitem
(
result
,
item
)
<
0
)
if
(
PyList_Append
(
result
,
item
)
<
0
)
goto
Fail_1
;
}
}
else
{
DECREF
(
item
);
Py_
DECREF
(
item
);
}
}
if
(
j
<
len
&&
setlists
lice
(
result
,
j
,
len
,
NULL
)
<
0
)
if
(
j
<
len
&&
PyList_SetS
lice
(
result
,
j
,
len
,
NULL
)
<
0
)
goto
Fail_1
;
return
result
;
Fail_1:
DECREF
(
result
);
Py_
DECREF
(
result
);
Fail_2:
return
NULL
;
}
static
o
bject
*
static
PyO
bject
*
builtin_chr
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
long
x
;
char
s
[
1
];
if
(
!
newgetargs
(
args
,
"l:chr"
,
&
x
))
if
(
!
PyArg_ParseTuple
(
args
,
"l:chr"
,
&
x
))
return
NULL
;
if
(
x
<
0
||
x
>=
256
)
{
err_setstr
(
ValueError
,
"chr() arg not in range(256)"
);
PyErr_SetString
(
PyExc_ValueError
,
"chr() arg not in range(256)"
);
return
NULL
;
}
s
[
0
]
=
(
char
)
x
;
return
newsizedstringobject
(
s
,
1
);
return
PyString_FromStringAndSize
(
s
,
1
);
}
static
o
bject
*
static
PyO
bject
*
builtin_cmp
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
a
,
*
b
;
PyO
bject
*
a
,
*
b
;
if
(
!
newgetargs
(
args
,
"OO:cmp"
,
&
a
,
&
b
))
if
(
!
PyArg_ParseTuple
(
args
,
"OO:cmp"
,
&
a
,
&
b
))
return
NULL
;
return
newintobject
((
long
)
cmpobject
(
a
,
b
));
return
PyInt_FromLong
((
long
)
PyObject_Compare
(
a
,
b
));
}
static
o
bject
*
static
PyO
bject
*
builtin_coerce
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
,
*
w
;
o
bject
*
res
;
PyO
bject
*
v
,
*
w
;
PyO
bject
*
res
;
if
(
!
newgetargs
(
args
,
"OO:coerce"
,
&
v
,
&
w
))
if
(
!
PyArg_ParseTuple
(
args
,
"OO:coerce"
,
&
v
,
&
w
))
return
NULL
;
if
(
c
oerce
(
&
v
,
&
w
)
<
0
)
if
(
PyNumber_C
oerce
(
&
v
,
&
w
)
<
0
)
return
NULL
;
res
=
mkv
alue
(
"(OO)"
,
v
,
w
);
DECREF
(
v
);
DECREF
(
w
);
res
=
Py_BuildV
alue
(
"(OO)"
,
v
,
w
);
Py_
DECREF
(
v
);
Py_
DECREF
(
w
);
return
res
;
}
static
o
bject
*
static
PyO
bject
*
builtin_compile
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
char
*
str
;
char
*
filename
;
char
*
startstr
;
int
start
;
if
(
!
newgetargs
(
args
,
"sss:compile"
,
&
str
,
&
filename
,
&
startstr
))
if
(
!
PyArg_ParseTuple
(
args
,
"sss:compile"
,
&
str
,
&
filename
,
&
startstr
))
return
NULL
;
if
(
strcmp
(
startstr
,
"exec"
)
==
0
)
start
=
file_input
;
...
...
@@ -288,122 +289,122 @@ builtin_compile(self, args)
else
if
(
strcmp
(
startstr
,
"single"
)
==
0
)
start
=
single_input
;
else
{
err_setstr
(
ValueError
,
PyErr_SetString
(
PyExc_
ValueError
,
"compile() mode must be 'exec' or 'eval' or 'single'"
);
return
NULL
;
}
return
compile_s
tring
(
str
,
filename
,
start
);
return
Py_CompileS
tring
(
str
,
filename
,
start
);
}
#ifndef WITHOUT_COMPLEX
static
o
bject
*
static
PyO
bject
*
builtin_complex
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
r
,
*
i
,
*
tmp
;
number_m
ethods
*
nbr
,
*
nbi
=
NULL
;
PyO
bject
*
r
,
*
i
,
*
tmp
;
PyNumberM
ethods
*
nbr
,
*
nbi
=
NULL
;
Py_complex
cr
,
ci
;
int
own_r
=
0
;
i
=
NULL
;
if
(
!
newgetargs
(
args
,
"O|O:complex"
,
&
r
,
&
i
))
if
(
!
PyArg_ParseTuple
(
args
,
"O|O:complex"
,
&
r
,
&
i
))
return
NULL
;
if
((
nbr
=
r
->
ob_type
->
tp_as_number
)
==
NULL
||
nbr
->
nb_float
==
NULL
||
(
i
!=
NULL
&&
((
nbi
=
i
->
ob_type
->
tp_as_number
)
==
NULL
||
nbi
->
nb_float
==
NULL
)))
{
err_setstr
(
TypeError
,
PyErr_SetString
(
PyExc_
TypeError
,
"complex() argument can't be converted to complex"
);
return
NULL
;
}
/* XXX Hack to support classes with __complex__ method */
if
(
is_instanceobject
(
r
))
{
static
o
bject
*
complexstr
;
o
bject
*
f
;
if
(
PyInstance_Check
(
r
))
{
static
PyO
bject
*
complexstr
;
PyO
bject
*
f
;
if
(
complexstr
==
NULL
)
{
complexstr
=
PyString_InternFromString
(
"__complex__"
);
if
(
complexstr
==
NULL
)
return
NULL
;
}
f
=
getattro
(
r
,
complexstr
);
f
=
PyObject_GetAttr
(
r
,
complexstr
);
if
(
f
==
NULL
)
err_c
lear
();
PyErr_C
lear
();
else
{
object
*
args
=
mkv
alue
(
"()"
);
PyObject
*
args
=
Py_BuildV
alue
(
"()"
);
if
(
args
==
NULL
)
return
NULL
;
r
=
call_o
bject
(
f
,
args
);
DECREF
(
args
);
r
=
PyEval_CallO
bject
(
f
,
args
);
Py_
DECREF
(
args
);
if
(
r
==
NULL
)
return
NULL
;
own_r
=
1
;
}
}
if
(
is_complexobject
(
r
))
{
cr
=
((
complexo
bject
*
)
r
)
->
cval
;
if
(
PyComplex_Check
(
r
))
{
cr
=
((
PyComplexO
bject
*
)
r
)
->
cval
;
if
(
own_r
)
DECREF
(
r
);
Py_
DECREF
(
r
);
}
else
{
tmp
=
(
*
nbr
->
nb_float
)(
r
);
if
(
own_r
)
DECREF
(
r
);
Py_
DECREF
(
r
);
if
(
tmp
==
NULL
)
return
NULL
;
cr
.
real
=
getfloatvalu
e
(
tmp
);
DECREF
(
tmp
);
cr
.
real
=
PyFloat_AsDoubl
e
(
tmp
);
Py_
DECREF
(
tmp
);
cr
.
imag
=
0
.;
}
if
(
i
==
NULL
)
{
ci
.
real
=
0
.;
ci
.
imag
=
0
.;
}
else
if
(
is_complexobject
(
i
))
ci
=
((
complexo
bject
*
)
i
)
->
cval
;
else
if
(
PyComplex_Check
(
i
))
ci
=
((
PyComplexO
bject
*
)
i
)
->
cval
;
else
{
tmp
=
(
*
nbi
->
nb_float
)(
i
);
if
(
tmp
==
NULL
)
return
NULL
;
ci
.
real
=
getfloatvalu
e
(
tmp
);
DECREF
(
tmp
);
ci
.
real
=
PyFloat_AsDoubl
e
(
tmp
);
Py_
DECREF
(
tmp
);
ci
.
imag
=
0
.;
}
cr
.
real
-=
ci
.
imag
;
cr
.
imag
+=
ci
.
real
;
return
newcomplexobject
(
cr
);
return
PyComplex_FromCComplex
(
cr
);
}
#endif
static
o
bject
*
static
PyO
bject
*
builtin_dir
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
=
NULL
;
o
bject
*
d
;
PyO
bject
*
v
=
NULL
;
PyO
bject
*
d
;
if
(
!
newgetargs
(
args
,
"|O:dir"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"|O:dir"
,
&
v
))
return
NULL
;
if
(
v
==
NULL
)
{
d
=
getl
ocals
();
INCREF
(
d
);
d
=
PyEval_GetL
ocals
();
Py_
INCREF
(
d
);
}
else
{
d
=
getattr
(
v
,
"__dict__"
);
d
=
PyObject_GetAttrString
(
v
,
"__dict__"
);
if
(
d
==
NULL
)
{
err_setstr
(
TypeError
,
PyErr_SetString
(
PyExc_
TypeError
,
"dir() argument must have __dict__ attribute"
);
return
NULL
;
}
}
if
(
is_dictobject
(
d
))
{
v
=
getdictk
eys
(
d
);
if
(
sortlis
t
(
v
)
!=
0
)
{
DECREF
(
v
);
if
(
PyDict_Check
(
d
))
{
v
=
PyDict_K
eys
(
d
);
if
(
PyList_Sor
t
(
v
)
!=
0
)
{
Py_
DECREF
(
v
);
v
=
NULL
;
}
}
...
...
@@ -411,244 +412,247 @@ builtin_dir(self, args)
v
=
PyObject_CallMethod
(
d
,
"keys"
,
NULL
);
if
(
v
==
NULL
)
{
PyErr_Clear
();
v
=
newlistobject
(
0
);
v
=
PyList_New
(
0
);
}
}
DECREF
(
d
);
Py_
DECREF
(
d
);
return
v
;
}
static
o
bject
*
static
PyO
bject
*
do_divmod
(
v
,
w
)
o
bject
*
v
,
*
w
;
PyO
bject
*
v
,
*
w
;
{
o
bject
*
res
;
PyO
bject
*
res
;
if
(
is_instanceobject
(
v
)
||
is_instanceobject
(
w
))
return
instancebino
p
(
v
,
w
,
"__divmod__"
,
"__rdivmod__"
,
if
(
PyInstance_Check
(
v
)
||
PyInstance_Check
(
w
))
return
PyInstance_DoBinO
p
(
v
,
w
,
"__divmod__"
,
"__rdivmod__"
,
do_divmod
);
if
(
v
->
ob_type
->
tp_as_number
==
NULL
||
w
->
ob_type
->
tp_as_number
==
NULL
)
{
err_setstr
(
TypeError
,
PyErr_SetString
(
PyExc_
TypeError
,
"divmod() requires numeric or class instance arguments"
);
return
NULL
;
}
if
(
c
oerce
(
&
v
,
&
w
)
!=
0
)
if
(
PyNumber_C
oerce
(
&
v
,
&
w
)
!=
0
)
return
NULL
;
res
=
(
*
v
->
ob_type
->
tp_as_number
->
nb_divmod
)(
v
,
w
);
DECREF
(
v
);
DECREF
(
w
);
Py_
DECREF
(
v
);
Py_
DECREF
(
w
);
return
res
;
}
static
o
bject
*
static
PyO
bject
*
builtin_divmod
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
,
*
w
;
PyO
bject
*
v
,
*
w
;
if
(
!
newgetargs
(
args
,
"OO:divmod"
,
&
v
,
&
w
))
if
(
!
PyArg_ParseTuple
(
args
,
"OO:divmod"
,
&
v
,
&
w
))
return
NULL
;
return
do_divmod
(
v
,
w
);
}
static
o
bject
*
static
PyO
bject
*
builtin_eval
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
cmd
;
object
*
globals
=
None
,
*
locals
=
None
;
PyO
bject
*
cmd
;
PyObject
*
globals
=
Py_None
,
*
locals
=
Py_
None
;
char
*
str
;
if
(
!
newgetargs
(
args
,
"O|O!O!:eval"
,
if
(
!
PyArg_ParseTuple
(
args
,
"O|O!O!:eval"
,
&
cmd
,
&
Mappingt
ype
,
&
globals
,
&
Mappingt
ype
,
&
locals
))
&
PyDict_T
ype
,
&
globals
,
&
PyDict_T
ype
,
&
locals
))
return
NULL
;
if
(
globals
==
None
)
{
globals
=
getg
lobals
();
if
(
locals
==
None
)
locals
=
getl
ocals
();
if
(
globals
==
Py_
None
)
{
globals
=
PyEval_GetG
lobals
();
if
(
locals
==
Py_
None
)
locals
=
PyEval_GetL
ocals
();
}
else
if
(
locals
==
None
)
else
if
(
locals
==
Py_
None
)
locals
=
globals
;
if
(
dictlookup
(
globals
,
"__builtins__"
)
==
NULL
)
{
if
(
dictinsert
(
globals
,
"__builtins__"
,
getbuiltins
())
!=
0
)
if
(
PyDict_GetItemString
(
globals
,
"__builtins__"
)
==
NULL
)
{
if
(
PyDict_SetItemString
(
globals
,
"__builtins__"
,
PyEval_GetBuiltins
())
!=
0
)
return
NULL
;
}
if
(
is_codeobject
(
cmd
))
return
eval_code
((
codeo
bject
*
)
cmd
,
globals
,
locals
);
if
(
!
is_stringobject
(
cmd
))
{
err_setstr
(
TypeError
,
if
(
PyCode_Check
(
cmd
))
return
PyEval_EvalCode
((
PyCodeO
bject
*
)
cmd
,
globals
,
locals
);
if
(
!
PyString_Check
(
cmd
))
{
PyErr_SetString
(
PyExc_
TypeError
,
"eval() argument 1 must be string or code object"
);
return
NULL
;
}
str
=
getstringvalue
(
cmd
);
if
((
int
)
strlen
(
str
)
!=
getstrings
ize
(
cmd
))
{
err_setstr
(
ValueError
,
str
=
PyString_AsString
(
cmd
);
if
((
int
)
strlen
(
str
)
!=
PyString_S
ize
(
cmd
))
{
PyErr_SetString
(
PyExc_
ValueError
,
"embedded '
\\
0' in string arg"
);
return
NULL
;
}
while
(
*
str
==
' '
||
*
str
==
'\t'
)
str
++
;
return
run_s
tring
(
str
,
eval_input
,
globals
,
locals
);
return
PyRun_S
tring
(
str
,
eval_input
,
globals
,
locals
);
}
static
o
bject
*
static
PyO
bject
*
builtin_execfile
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
char
*
filename
;
object
*
globals
=
None
,
*
locals
=
None
;
o
bject
*
res
;
PyObject
*
globals
=
Py_None
,
*
locals
=
Py_
None
;
PyO
bject
*
res
;
FILE
*
fp
;
if
(
!
newgetargs
(
args
,
"s|O!O!:execfile"
,
if
(
!
PyArg_ParseTuple
(
args
,
"s|O!O!:execfile"
,
&
filename
,
&
Mappingt
ype
,
&
globals
,
&
Mappingt
ype
,
&
locals
))
&
PyDict_T
ype
,
&
globals
,
&
PyDict_T
ype
,
&
locals
))
return
NULL
;
if
(
globals
==
None
)
{
globals
=
getg
lobals
();
if
(
locals
==
None
)
locals
=
getl
ocals
();
if
(
globals
==
Py_
None
)
{
globals
=
PyEval_GetG
lobals
();
if
(
locals
==
Py_
None
)
locals
=
PyEval_GetL
ocals
();
}
else
if
(
locals
==
None
)
else
if
(
locals
==
Py_
None
)
locals
=
globals
;
if
(
dictlookup
(
globals
,
"__builtins__"
)
==
NULL
)
{
if
(
dictinsert
(
globals
,
"__builtins__"
,
getbuiltins
())
!=
0
)
if
(
PyDict_GetItemString
(
globals
,
"__builtins__"
)
==
NULL
)
{
if
(
PyDict_SetItemString
(
globals
,
"__builtins__"
,
PyEval_GetBuiltins
())
!=
0
)
return
NULL
;
}
BGN_SAVE
Py_BEGIN_ALLOW_THREADS
fp
=
fopen
(
filename
,
"r"
);
END_SAVE
Py_END_ALLOW_THREADS
if
(
fp
==
NULL
)
{
err_errno
(
IOError
);
PyErr_SetFromErrno
(
PyExc_
IOError
);
return
NULL
;
}
res
=
run_f
ile
(
fp
,
filename
,
file_input
,
globals
,
locals
);
BGN_SAVE
res
=
PyRun_F
ile
(
fp
,
filename
,
file_input
,
globals
,
locals
);
Py_BEGIN_ALLOW_THREADS
fclose
(
fp
);
END_SAVE
Py_END_ALLOW_THREADS
return
res
;
}
static
o
bject
*
static
PyO
bject
*
builtin_float
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
number_m
ethods
*
nb
;
PyO
bject
*
v
;
PyNumberM
ethods
*
nb
;
if
(
!
newgetargs
(
args
,
"O:float"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"O:float"
,
&
v
))
return
NULL
;
if
(
is_stringobject
(
v
))
if
(
PyString_Check
(
v
))
return
float_from_string
(
v
);
if
((
nb
=
v
->
ob_type
->
tp_as_number
)
==
NULL
||
nb
->
nb_float
==
NULL
)
{
err_setstr
(
TypeError
,
PyErr_SetString
(
PyExc_
TypeError
,
"float() argument can't be converted to float"
);
return
NULL
;
}
return
(
*
nb
->
nb_float
)(
v
);
}
static
o
bject
*
static
PyO
bject
*
builtin_getattr
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
o
bject
*
name
;
PyO
bject
*
v
;
PyO
bject
*
name
;
if
(
!
newgetargs
(
args
,
"OS:getattr"
,
&
v
,
&
name
))
if
(
!
PyArg_ParseTuple
(
args
,
"OS:getattr"
,
&
v
,
&
name
))
return
NULL
;
return
getattro
(
v
,
name
);
return
PyObject_GetAttr
(
v
,
name
);
}
static
o
bject
*
static
PyO
bject
*
builtin_globals
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
d
;
PyO
bject
*
d
;
if
(
!
newgetargs
(
args
,
""
))
if
(
!
PyArg_ParseTuple
(
args
,
""
))
return
NULL
;
d
=
getg
lobals
();
INCREF
(
d
);
d
=
PyEval_GetG
lobals
();
Py_
INCREF
(
d
);
return
d
;
}
static
o
bject
*
static
PyO
bject
*
builtin_hasattr
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
o
bject
*
name
;
PyO
bject
*
v
;
PyO
bject
*
name
;
if
(
!
newgetargs
(
args
,
"OS:hasattr"
,
&
v
,
&
name
))
if
(
!
PyArg_ParseTuple
(
args
,
"OS:hasattr"
,
&
v
,
&
name
))
return
NULL
;
v
=
getattro
(
v
,
name
);
v
=
PyObject_GetAttr
(
v
,
name
);
if
(
v
==
NULL
)
{
err_c
lear
();
return
newintobject
(
0L
);
PyErr_C
lear
();
return
PyInt_FromLong
(
0L
);
}
DECREF
(
v
);
return
newintobject
(
1L
);
Py_
DECREF
(
v
);
return
PyInt_FromLong
(
1L
);
}
static
o
bject
*
static
PyO
bject
*
builtin_id
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
PyO
bject
*
v
;
if
(
!
newgetargs
(
args
,
"O:id"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"O:id"
,
&
v
))
return
NULL
;
return
newintobject
((
long
)
v
);
return
PyInt_FromLong
((
long
)
v
);
}
static
o
bject
*
static
PyO
bject
*
builtin_map
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
typedef
struct
{
o
bject
*
seq
;
sequence_m
ethods
*
sqf
;
PyO
bject
*
seq
;
PySequenceM
ethods
*
sqf
;
int
len
;
}
sequence
;
o
bject
*
func
,
*
result
;
PyO
bject
*
func
,
*
result
;
sequence
*
seqs
=
NULL
,
*
sqp
;
int
n
,
len
;
register
int
i
,
j
;
n
=
gettuples
ize
(
args
);
n
=
PyTuple_S
ize
(
args
);
if
(
n
<
2
)
{
err_setstr
(
TypeError
,
"map() requires at least two args"
);
PyErr_SetString
(
PyExc_TypeError
,
"map() requires at least two args"
);
return
NULL
;
}
func
=
gettuplei
tem
(
args
,
0
);
func
=
PyTuple_GetI
tem
(
args
,
0
);
n
--
;
if
((
seqs
=
NEW
(
sequence
,
n
))
==
NULL
)
{
err_nomem
();
if
((
seqs
=
PyMem_
NEW
(
sequence
,
n
))
==
NULL
)
{
PyErr_NoMemory
();
goto
Fail_2
;
}
for
(
len
=
0
,
i
=
0
,
sqp
=
seqs
;
i
<
n
;
++
i
,
++
sqp
)
{
int
curlen
;
if
((
sqp
->
seq
=
gettuplei
tem
(
args
,
i
+
1
))
==
NULL
)
if
((
sqp
->
seq
=
PyTuple_GetI
tem
(
args
,
i
+
1
))
==
NULL
)
goto
Fail_2
;
if
(
!
(
sqp
->
sqf
=
sqp
->
seq
->
ob_type
->
tp_as_sequence
))
{
...
...
@@ -657,7 +661,7 @@ builtin_map(self, args)
char
errbuf
[
sizeof
(
errmsg
)
+
3
];
sprintf
(
errbuf
,
errmsg
,
i
+
2
);
err_setstr
(
TypeError
,
errbuf
);
PyErr_SetString
(
PyExc_
TypeError
,
errbuf
);
goto
Fail_2
;
}
...
...
@@ -668,35 +672,36 @@ builtin_map(self, args)
len
=
curlen
;
}
if
((
result
=
(
object
*
)
newlistobject
(
len
))
==
NULL
)
if
((
result
=
(
PyObject
*
)
PyList_New
(
len
))
==
NULL
)
goto
Fail_2
;
/* XXX Special case map(None, single_list) could be more efficient */
for
(
i
=
0
;
;
++
i
)
{
o
bject
*
alist
,
*
item
=
NULL
,
*
value
;
PyO
bject
*
alist
,
*
item
=
NULL
,
*
value
;
int
any
=
0
;
if
(
func
==
None
&&
n
==
1
)
if
(
func
==
Py_
None
&&
n
==
1
)
alist
=
NULL
;
else
{
if
((
alist
=
newtupleobject
(
n
))
==
NULL
)
if
((
alist
=
PyTuple_New
(
n
))
==
NULL
)
goto
Fail_1
;
}
for
(
j
=
0
,
sqp
=
seqs
;
j
<
n
;
++
j
,
++
sqp
)
{
if
(
sqp
->
len
<
0
)
{
INCREF
(
None
);
item
=
None
;
Py_INCREF
(
Py_
None
);
item
=
Py_
None
;
}
else
{
item
=
(
*
sqp
->
sqf
->
sq_item
)(
sqp
->
seq
,
i
);
if
(
item
==
NULL
)
{
if
(
i
<
sqp
->
len
)
goto
Fail_0
;
if
(
err_occurred
()
==
IndexError
)
{
err_clear
();
INCREF
(
None
);
item
=
None
;
if
(
PyErr_Occurred
()
==
PyExc_IndexError
)
{
PyErr_Clear
();
Py_INCREF
(
Py_None
);
item
=
Py_None
;
sqp
->
len
=
-
1
;
}
else
{
...
...
@@ -709,14 +714,14 @@ builtin_map(self, args)
}
if
(
!
alist
)
break
;
if
(
settuplei
tem
(
alist
,
j
,
item
)
<
0
)
{
DECREF
(
item
);
if
(
PyTuple_SetI
tem
(
alist
,
j
,
item
)
<
0
)
{
Py_
DECREF
(
item
);
goto
Fail_0
;
}
continue
;
Fail_0:
XDECREF
(
alist
);
Py_
XDECREF
(
alist
);
goto
Fail_1
;
}
...
...
@@ -724,134 +729,135 @@ builtin_map(self, args)
alist
=
item
;
if
(
!
any
)
{
DECREF
(
alist
);
Py_
DECREF
(
alist
);
break
;
}
if
(
func
==
None
)
if
(
func
==
Py_
None
)
value
=
alist
;
else
{
value
=
call_o
bject
(
func
,
alist
);
DECREF
(
alist
);
value
=
PyEval_CallO
bject
(
func
,
alist
);
Py_
DECREF
(
alist
);
if
(
value
==
NULL
)
goto
Fail_1
;
}
if
(
i
>=
len
)
{
if
(
addlistitem
(
result
,
value
)
<
0
)
if
(
PyList_Append
(
result
,
value
)
<
0
)
goto
Fail_1
;
}
else
{
if
(
setlisti
tem
(
result
,
i
,
value
)
<
0
)
if
(
PyList_SetI
tem
(
result
,
i
,
value
)
<
0
)
goto
Fail_1
;
}
}
DEL
(
seqs
);
PyMem_
DEL
(
seqs
);
return
result
;
Fail_1:
DECREF
(
result
);
Py_
DECREF
(
result
);
Fail_2:
if
(
seqs
)
DEL
(
seqs
);
if
(
seqs
)
PyMem_
DEL
(
seqs
);
return
NULL
;
}
static
o
bject
*
static
PyO
bject
*
builtin_setattr
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
o
bject
*
name
;
o
bject
*
value
;
PyO
bject
*
v
;
PyO
bject
*
name
;
PyO
bject
*
value
;
if
(
!
newgetargs
(
args
,
"OSO:setattr"
,
&
v
,
&
name
,
&
value
))
if
(
!
PyArg_ParseTuple
(
args
,
"OSO:setattr"
,
&
v
,
&
name
,
&
value
))
return
NULL
;
if
(
setattro
(
v
,
name
,
value
)
!=
0
)
if
(
PyObject_SetAttr
(
v
,
name
,
value
)
!=
0
)
return
NULL
;
INCREF
(
None
);
return
None
;
Py_INCREF
(
Py_
None
);
return
Py_
None
;
}
static
o
bject
*
static
PyO
bject
*
builtin_delattr
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
o
bject
*
name
;
PyO
bject
*
v
;
PyO
bject
*
name
;
if
(
!
newgetargs
(
args
,
"OS:delattr"
,
&
v
,
&
name
))
if
(
!
PyArg_ParseTuple
(
args
,
"OS:delattr"
,
&
v
,
&
name
))
return
NULL
;
if
(
setattro
(
v
,
name
,
(
o
bject
*
)
NULL
)
!=
0
)
if
(
PyObject_SetAttr
(
v
,
name
,
(
PyO
bject
*
)
NULL
)
!=
0
)
return
NULL
;
INCREF
(
None
);
return
None
;
Py_INCREF
(
Py_
None
);
return
Py_
None
;
}
static
o
bject
*
static
PyO
bject
*
builtin_hash
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
PyO
bject
*
v
;
long
x
;
if
(
!
newgetargs
(
args
,
"O:hash"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"O:hash"
,
&
v
))
return
NULL
;
x
=
hashobject
(
v
);
x
=
PyObject_Hash
(
v
);
if
(
x
==
-
1
)
return
NULL
;
return
newintobject
(
x
);
return
PyInt_FromLong
(
x
);
}
static
o
bject
*
static
PyO
bject
*
builtin_hex
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
number_m
ethods
*
nb
;
PyO
bject
*
v
;
PyNumberM
ethods
*
nb
;
if
(
!
newgetargs
(
args
,
"O:hex"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"O:hex"
,
&
v
))
return
NULL
;
if
((
nb
=
v
->
ob_type
->
tp_as_number
)
==
NULL
||
nb
->
nb_hex
==
NULL
)
{
err_setstr
(
TypeError
,
PyErr_SetString
(
PyExc_
TypeError
,
"hex() argument can't be converted to hex"
);
return
NULL
;
}
return
(
*
nb
->
nb_hex
)(
v
);
}
static
object
*
builtin_raw_input
PROTO
((
object
*
,
o
bject
*
));
static
PyObject
*
builtin_raw_input
Py_PROTO
((
PyObject
*
,
PyO
bject
*
));
static
o
bject
*
static
PyO
bject
*
builtin_input
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
line
;
PyO
bject
*
line
;
char
*
str
;
o
bject
*
res
;
o
bject
*
globals
,
*
locals
;
PyO
bject
*
res
;
PyO
bject
*
globals
,
*
locals
;
line
=
builtin_raw_input
(
self
,
args
);
if
(
line
==
NULL
)
return
line
;
if
(
!
getargs
(
line
,
"s;embedded '
\\
0' in input line"
,
&
str
))
if
(
!
PyArg_Parse
(
line
,
"s;embedded '
\\
0' in input line"
,
&
str
))
return
NULL
;
while
(
*
str
==
' '
||
*
str
==
'\t'
)
str
++
;
globals
=
getglobals
();
locals
=
getlocals
();
if
(
dictlookup
(
globals
,
"__builtins__"
)
==
NULL
)
{
if
(
dictinsert
(
globals
,
"__builtins__"
,
getbuiltins
())
!=
0
)
globals
=
PyEval_GetGlobals
();
locals
=
PyEval_GetLocals
();
if
(
PyDict_GetItemString
(
globals
,
"__builtins__"
)
==
NULL
)
{
if
(
PyDict_SetItemString
(
globals
,
"__builtins__"
,
PyEval_GetBuiltins
())
!=
0
)
return
NULL
;
}
res
=
run_s
tring
(
str
,
eval_input
,
globals
,
locals
);
DECREF
(
line
);
res
=
PyRun_S
tring
(
str
,
eval_input
,
globals
,
locals
);
Py_
DECREF
(
line
);
return
res
;
}
...
...
@@ -868,37 +874,37 @@ builtin_intern(self, args)
return
s
;
}
static
o
bject
*
static
PyO
bject
*
builtin_int
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
number_m
ethods
*
nb
;
PyO
bject
*
v
;
PyNumberM
ethods
*
nb
;
if
(
!
newgetargs
(
args
,
"O:int"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"O:int"
,
&
v
))
return
NULL
;
if
(
is_stringobject
(
v
))
if
(
PyString_Check
(
v
))
return
int_from_string
(
v
);
if
((
nb
=
v
->
ob_type
->
tp_as_number
)
==
NULL
||
nb
->
nb_int
==
NULL
)
{
err_setstr
(
TypeError
,
PyErr_SetString
(
PyExc_
TypeError
,
"int() argument can't be converted to int"
);
return
NULL
;
}
return
(
*
nb
->
nb_int
)(
v
);
}
static
o
bject
*
static
PyO
bject
*
builtin_len
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
PyO
bject
*
v
;
long
len
;
typeo
bject
*
tp
;
PyTypeO
bject
*
tp
;
if
(
!
newgetargs
(
args
,
"O:len"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"O:len"
,
&
v
))
return
NULL
;
tp
=
v
->
ob_type
;
if
(
tp
->
tp_as_sequence
!=
NULL
)
{
...
...
@@ -908,47 +914,47 @@ builtin_len(self, args)
len
=
(
*
tp
->
tp_as_mapping
->
mp_length
)(
v
);
}
else
{
err_setstr
(
TypeError
,
"len() of unsized object"
);
PyErr_SetString
(
PyExc_
TypeError
,
"len() of unsized object"
);
return
NULL
;
}
if
(
len
<
0
)
return
NULL
;
else
return
newintobject
(
len
);
return
PyInt_FromLong
(
len
);
}
static
o
bject
*
static
PyO
bject
*
builtin_list
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
sequence_m
ethods
*
sqf
;
PyO
bject
*
v
;
PySequenceM
ethods
*
sqf
;
if
(
!
newgetargs
(
args
,
"O:list"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"O:list"
,
&
v
))
return
NULL
;
if
((
sqf
=
v
->
ob_type
->
tp_as_sequence
)
!=
NULL
)
{
int
n
=
(
*
sqf
->
sq_length
)(
v
);
int
i
;
o
bject
*
l
;
PyO
bject
*
l
;
if
(
n
<
0
)
return
NULL
;
l
=
newlistobject
(
n
);
l
=
PyList_New
(
n
);
if
(
l
==
NULL
)
return
NULL
;
for
(
i
=
0
;
i
<
n
;
i
++
)
{
o
bject
*
item
=
(
*
sqf
->
sq_item
)(
v
,
i
);
PyO
bject
*
item
=
(
*
sqf
->
sq_item
)(
v
,
i
);
if
(
item
==
NULL
)
{
DECREF
(
l
);
Py_
DECREF
(
l
);
l
=
NULL
;
break
;
}
setlisti
tem
(
l
,
i
,
item
);
PyList_SetI
tem
(
l
,
i
,
item
);
}
/* XXX Should support indefinite-length sequences */
return
l
;
}
err_setstr
(
TypeError
,
"list() argument must be a sequence"
);
PyErr_SetString
(
PyExc_
TypeError
,
"list() argument must be a sequence"
);
return
NULL
;
}
...
...
@@ -974,247 +980,252 @@ builtin_slice(self, args)
return
PySlice_New
(
start
,
stop
,
step
);
}
static
o
bject
*
static
PyO
bject
*
builtin_locals
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
d
;
PyO
bject
*
d
;
if
(
!
newgetargs
(
args
,
""
))
if
(
!
PyArg_ParseTuple
(
args
,
""
))
return
NULL
;
d
=
getl
ocals
();
INCREF
(
d
);
d
=
PyEval_GetL
ocals
();
Py_
INCREF
(
d
);
return
d
;
}
static
o
bject
*
static
PyO
bject
*
builtin_long
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
number_m
ethods
*
nb
;
PyO
bject
*
v
;
PyNumberM
ethods
*
nb
;
if
(
!
newgetargs
(
args
,
"O:long"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"O:long"
,
&
v
))
return
NULL
;
if
(
is_stringobject
(
v
))
if
(
PyString_Check
(
v
))
return
long_from_string
(
v
);
if
((
nb
=
v
->
ob_type
->
tp_as_number
)
==
NULL
||
nb
->
nb_long
==
NULL
)
{
err_setstr
(
TypeError
,
PyErr_SetString
(
PyExc_
TypeError
,
"long() argument can't be converted to long"
);
return
NULL
;
}
return
(
*
nb
->
nb_long
)(
v
);
}
static
o
bject
*
static
PyO
bject
*
min_max
(
args
,
sign
)
o
bject
*
args
;
PyO
bject
*
args
;
int
sign
;
{
int
i
;
o
bject
*
v
,
*
w
,
*
x
;
sequence_m
ethods
*
sq
;
PyO
bject
*
v
,
*
w
,
*
x
;
PySequenceM
ethods
*
sq
;
if
(
gettuples
ize
(
args
)
>
1
)
if
(
PyTuple_S
ize
(
args
)
>
1
)
v
=
args
;
else
if
(
!
newgetargs
(
args
,
"O:min/max"
,
&
v
))
else
if
(
!
PyArg_ParseTuple
(
args
,
"O:min/max"
,
&
v
))
return
NULL
;
sq
=
v
->
ob_type
->
tp_as_sequence
;
if
(
sq
==
NULL
)
{
err_setstr
(
TypeError
,
"min() or max() of non-sequence"
);
PyErr_SetString
(
PyExc_TypeError
,
"min() or max() of non-sequence"
);
return
NULL
;
}
w
=
NULL
;
for
(
i
=
0
;
;
i
++
)
{
x
=
(
*
sq
->
sq_item
)(
v
,
i
);
/* Implies INCREF */
if
(
x
==
NULL
)
{
if
(
err_occurred
()
==
IndexError
)
{
err_c
lear
();
if
(
PyErr_Occurred
()
==
PyExc_
IndexError
)
{
PyErr_C
lear
();
break
;
}
XDECREF
(
w
);
Py_
XDECREF
(
w
);
return
NULL
;
}
if
(
w
==
NULL
)
w
=
x
;
else
{
if
(
cmpobject
(
x
,
w
)
*
sign
>
0
)
{
DECREF
(
w
);
if
(
PyObject_Compare
(
x
,
w
)
*
sign
>
0
)
{
Py_
DECREF
(
w
);
w
=
x
;
}
else
DECREF
(
x
);
Py_
DECREF
(
x
);
}
}
if
(
w
==
NULL
)
err_setstr
(
ValueError
,
"min() or max() of empty sequence"
);
PyErr_SetString
(
PyExc_ValueError
,
"min() or max() of empty sequence"
);
return
w
;
}
static
o
bject
*
static
PyO
bject
*
builtin_min
(
self
,
v
)
o
bject
*
self
;
o
bject
*
v
;
PyO
bject
*
self
;
PyO
bject
*
v
;
{
return
min_max
(
v
,
-
1
);
}
static
o
bject
*
static
PyO
bject
*
builtin_max
(
self
,
v
)
o
bject
*
self
;
o
bject
*
v
;
PyO
bject
*
self
;
PyO
bject
*
v
;
{
return
min_max
(
v
,
1
);
}
static
o
bject
*
static
PyO
bject
*
builtin_oct
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
number_m
ethods
*
nb
;
PyO
bject
*
v
;
PyNumberM
ethods
*
nb
;
if
(
!
newgetargs
(
args
,
"O:oct"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"O:oct"
,
&
v
))
return
NULL
;
if
(
v
==
NULL
||
(
nb
=
v
->
ob_type
->
tp_as_number
)
==
NULL
||
nb
->
nb_oct
==
NULL
)
{
err_setstr
(
TypeError
,
PyErr_SetString
(
PyExc_
TypeError
,
"oct() argument can't be converted to oct"
);
return
NULL
;
}
return
(
*
nb
->
nb_oct
)(
v
);
}
static
o
bject
*
static
PyO
bject
*
builtin_open
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
char
*
name
;
char
*
mode
=
"r"
;
int
bufsize
=
-
1
;
o
bject
*
f
;
PyO
bject
*
f
;
if
(
!
newgetargs
(
args
,
"s|si:open"
,
&
name
,
&
mode
,
&
bufsize
))
if
(
!
PyArg_ParseTuple
(
args
,
"s|si:open"
,
&
name
,
&
mode
,
&
bufsize
))
return
NULL
;
f
=
newfileobject
(
name
,
mode
);
f
=
PyFile_FromString
(
name
,
mode
);
if
(
f
!=
NULL
)
setfilebufs
ize
(
f
,
bufsize
);
PyFile_SetBufS
ize
(
f
,
bufsize
);
return
f
;
}
static
o
bject
*
static
PyO
bject
*
builtin_ord
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
char
c
;
if
(
!
newgetargs
(
args
,
"c:ord"
,
&
c
))
if
(
!
PyArg_ParseTuple
(
args
,
"c:ord"
,
&
c
))
return
NULL
;
return
newintobject
((
long
)(
c
&
0xff
));
return
PyInt_FromLong
((
long
)(
c
&
0xff
));
}
static
o
bject
*
static
PyO
bject
*
do_pow
(
v
,
w
)
o
bject
*
v
,
*
w
;
PyO
bject
*
v
,
*
w
;
{
o
bject
*
res
;
if
(
is_instanceobject
(
v
)
||
is_instanceobject
(
w
))
return
instancebino
p
(
v
,
w
,
"__pow__"
,
"__rpow__"
,
do_pow
);
PyO
bject
*
res
;
if
(
PyInstance_Check
(
v
)
||
PyInstance_Check
(
w
))
return
PyInstance_DoBinO
p
(
v
,
w
,
"__pow__"
,
"__rpow__"
,
do_pow
);
if
(
v
->
ob_type
->
tp_as_number
==
NULL
||
w
->
ob_type
->
tp_as_number
==
NULL
)
{
err_setstr
(
TypeError
,
"pow() requires numeric arguments"
);
PyErr_SetString
(
PyExc_TypeError
,
"pow() requires numeric arguments"
);
return
NULL
;
}
if
(
#ifndef WITHOUT_COMPLEX
!
is_complexobject
(
v
)
&&
!
PyComplex_Check
(
v
)
&&
#endif
is_floatobject
(
w
)
&&
getfloatvalue
(
v
)
<
0
.
0
)
{
if
(
!
err_occurred
())
err_setstr
(
ValueError
,
"negative number to float power"
);
PyFloat_Check
(
w
)
&&
PyFloat_AsDouble
(
v
)
<
0
.
0
)
{
if
(
!
PyErr_Occurred
())
PyErr_SetString
(
PyExc_ValueError
,
"negative number to float power"
);
return
NULL
;
}
if
(
c
oerce
(
&
v
,
&
w
)
!=
0
)
if
(
PyNumber_C
oerce
(
&
v
,
&
w
)
!=
0
)
return
NULL
;
res
=
(
*
v
->
ob_type
->
tp_as_number
->
nb_power
)(
v
,
w
,
None
);
DECREF
(
v
);
DECREF
(
w
);
res
=
(
*
v
->
ob_type
->
tp_as_number
->
nb_power
)(
v
,
w
,
Py_
None
);
Py_
DECREF
(
v
);
Py_
DECREF
(
w
);
return
res
;
}
static
o
bject
*
static
PyO
bject
*
builtin_pow
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
object
*
v
,
*
w
,
*
z
=
None
,
*
res
;
o
bject
*
v1
,
*
z1
,
*
w2
,
*
z2
;
PyObject
*
v
,
*
w
,
*
z
=
Py_
None
,
*
res
;
PyO
bject
*
v1
,
*
z1
,
*
w2
,
*
z2
;
if
(
!
newgetargs
(
args
,
"OO|O:pow"
,
&
v
,
&
w
,
&
z
))
if
(
!
PyArg_ParseTuple
(
args
,
"OO|O:pow"
,
&
v
,
&
w
,
&
z
))
return
NULL
;
if
(
z
==
None
)
if
(
z
==
Py_
None
)
return
do_pow
(
v
,
w
);
/* XXX The ternary version doesn't do class instance coercions */
if
(
is_instanceobject
(
v
))
if
(
PyInstance_Check
(
v
))
return
v
->
ob_type
->
tp_as_number
->
nb_power
(
v
,
w
,
z
);
if
(
v
->
ob_type
->
tp_as_number
==
NULL
||
z
->
ob_type
->
tp_as_number
==
NULL
||
w
->
ob_type
->
tp_as_number
==
NULL
)
{
err_setstr
(
TypeError
,
"pow() requires numeric arguments"
);
PyErr_SetString
(
PyExc_TypeError
,
"pow() requires numeric arguments"
);
return
NULL
;
}
if
(
c
oerce
(
&
v
,
&
w
)
!=
0
)
if
(
PyNumber_C
oerce
(
&
v
,
&
w
)
!=
0
)
return
NULL
;
res
=
NULL
;
v1
=
v
;
z1
=
z
;
if
(
c
oerce
(
&
v1
,
&
z1
)
!=
0
)
if
(
PyNumber_C
oerce
(
&
v1
,
&
z1
)
!=
0
)
goto
error2
;
w2
=
w
;
z2
=
z1
;
if
(
c
oerce
(
&
w2
,
&
z2
)
!=
0
)
if
(
PyNumber_C
oerce
(
&
w2
,
&
z2
)
!=
0
)
goto
error1
;
res
=
(
*
v1
->
ob_type
->
tp_as_number
->
nb_power
)(
v1
,
w2
,
z2
);
DECREF
(
w2
);
DECREF
(
z2
);
Py_
DECREF
(
w2
);
Py_
DECREF
(
z2
);
error1:
DECREF
(
v1
);
DECREF
(
z1
);
Py_
DECREF
(
v1
);
Py_
DECREF
(
z1
);
error2:
DECREF
(
v
);
DECREF
(
w
);
Py_
DECREF
(
v
);
Py_
DECREF
(
w
);
return
res
;
}
static
o
bject
*
static
PyO
bject
*
builtin_range
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
long
ilow
=
0
,
ihigh
=
0
,
istep
=
1
;
int
i
,
n
;
o
bject
*
v
;
PyO
bject
*
v
;
if
(
gettuples
ize
(
args
)
<=
1
)
{
if
(
!
newgetargs
(
args
,
if
(
PyTuple_S
ize
(
args
)
<=
1
)
{
if
(
!
PyArg_ParseTuple
(
args
,
"l;range() requires 1-3 int arguments"
,
&
ihigh
))
return
NULL
;
}
else
{
if
(
!
newgetargs
(
args
,
if
(
!
PyArg_ParseTuple
(
args
,
"ll|l;range() requires 1-3 int arguments"
,
&
ilow
,
&
ihigh
,
&
istep
))
return
NULL
;
}
if
(
istep
==
0
)
{
err_setstr
(
ValueError
,
"zero step for range()"
);
PyErr_SetString
(
PyExc_
ValueError
,
"zero step for range()"
);
return
NULL
;
}
/* XXX ought to check overflow of subtraction */
...
...
@@ -1224,43 +1235,43 @@ builtin_range(self, args)
n
=
(
ihigh
-
ilow
+
istep
+
1
)
/
istep
;
if
(
n
<
0
)
n
=
0
;
v
=
newlistobject
(
n
);
v
=
PyList_New
(
n
);
if
(
v
==
NULL
)
return
NULL
;
for
(
i
=
0
;
i
<
n
;
i
++
)
{
object
*
w
=
newintobject
(
ilow
);
PyObject
*
w
=
PyInt_FromLong
(
ilow
);
if
(
w
==
NULL
)
{
DECREF
(
v
);
Py_
DECREF
(
v
);
return
NULL
;
}
setlisti
tem
(
v
,
i
,
w
);
PyList_SetI
tem
(
v
,
i
,
w
);
ilow
+=
istep
;
}
return
v
;
}
static
o
bject
*
static
PyO
bject
*
builtin_xrange
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
long
ilow
=
0
,
ihigh
=
0
,
istep
=
1
;
long
n
;
if
(
gettuples
ize
(
args
)
<=
1
)
{
if
(
!
newgetargs
(
args
,
if
(
PyTuple_S
ize
(
args
)
<=
1
)
{
if
(
!
PyArg_ParseTuple
(
args
,
"l;xrange() requires 1-3 int arguments"
,
&
ihigh
))
return
NULL
;
}
else
{
if
(
!
newgetargs
(
args
,
if
(
!
PyArg_ParseTuple
(
args
,
"ll|l;xrange() requires 1-3 int arguments"
,
&
ilow
,
&
ihigh
,
&
istep
))
return
NULL
;
}
if
(
istep
==
0
)
{
err_setstr
(
ValueError
,
"zero step for xrange()"
);
PyErr_SetString
(
PyExc_
ValueError
,
"zero step for xrange()"
);
return
NULL
;
}
/* XXX ought to check overflow of subtraction */
...
...
@@ -1270,107 +1281,107 @@ builtin_xrange(self, args)
n
=
(
ihigh
-
ilow
+
istep
+
1
)
/
istep
;
if
(
n
<
0
)
n
=
0
;
return
newrangeobject
(
ilow
,
n
,
istep
,
1
);
return
PyRange_New
(
ilow
,
n
,
istep
,
1
);
}
extern
char
*
my_readline
PROTO
((
char
*
));
extern
char
*
PyOS_Readline
Py_
PROTO
((
char
*
));
static
o
bject
*
static
PyO
bject
*
builtin_raw_input
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
=
NULL
;
o
bject
*
f
;
PyO
bject
*
v
=
NULL
;
PyO
bject
*
f
;
if
(
!
newgetargs
(
args
,
"|O:[raw_]input"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"|O:[raw_]input"
,
&
v
))
return
NULL
;
if
(
getfilefile
(
sysge
t
(
"stdin"
))
==
stdin
&&
getfilefile
(
sysge
t
(
"stdout"
))
==
stdout
&&
if
(
PyFile_AsFile
(
PySys_GetObjec
t
(
"stdin"
))
==
stdin
&&
PyFile_AsFile
(
PySys_GetObjec
t
(
"stdout"
))
==
stdout
&&
isatty
(
fileno
(
stdin
))
&&
isatty
(
fileno
(
stdout
)))
{
o
bject
*
po
;
PyO
bject
*
po
;
char
*
prompt
;
char
*
s
;
o
bject
*
result
;
PyO
bject
*
result
;
if
(
v
!=
NULL
)
{
po
=
strobject
(
v
);
po
=
PyObject_Str
(
v
);
if
(
po
==
NULL
)
return
NULL
;
prompt
=
getstringvalue
(
po
);
prompt
=
PyString_AsString
(
po
);
}
else
{
po
=
NULL
;
prompt
=
""
;
}
s
=
my_r
eadline
(
prompt
);
XDECREF
(
po
);
s
=
PyOS_R
eadline
(
prompt
);
Py_
XDECREF
(
po
);
if
(
s
==
NULL
)
{
err_set
(
KeyboardInterrupt
);
PyErr_SetNone
(
PyExc_
KeyboardInterrupt
);
return
NULL
;
}
if
(
*
s
==
'\0'
)
{
err_set
(
EOFError
);
PyErr_SetNone
(
PyExc_
EOFError
);
result
=
NULL
;
}
else
{
/* strip trailing '\n' */
result
=
newsizedstringobject
(
s
,
strlen
(
s
)
-
1
);
result
=
PyString_FromStringAndSize
(
s
,
strlen
(
s
)
-
1
);
}
free
(
s
);
return
result
;
}
if
(
v
!=
NULL
)
{
f
=
sysge
t
(
"stdout"
);
f
=
PySys_GetObjec
t
(
"stdout"
);
if
(
f
==
NULL
)
{
err_setstr
(
RuntimeError
,
"lost sys.stdout"
);
PyErr_SetString
(
PyExc_
RuntimeError
,
"lost sys.stdout"
);
return
NULL
;
}
flushl
ine
();
if
(
writeobject
(
v
,
f
,
PRINT_RAW
)
!=
0
)
Py_FlushL
ine
();
if
(
PyFile_WriteObject
(
v
,
f
,
Py_
PRINT_RAW
)
!=
0
)
return
NULL
;
}
f
=
sysge
t
(
"stdin"
);
f
=
PySys_GetObjec
t
(
"stdin"
);
if
(
f
==
NULL
)
{
err_setstr
(
RuntimeError
,
"lost sys.stdin"
);
PyErr_SetString
(
PyExc_
RuntimeError
,
"lost sys.stdin"
);
return
NULL
;
}
return
filegetl
ine
(
f
,
-
1
);
return
PyFile_GetL
ine
(
f
,
-
1
);
}
static
o
bject
*
static
PyO
bject
*
builtin_reduce
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
seq
,
*
func
,
*
result
=
NULL
;
sequence_m
ethods
*
sqf
;
PyO
bject
*
seq
,
*
func
,
*
result
=
NULL
;
PySequenceM
ethods
*
sqf
;
register
int
i
;
if
(
!
newgetargs
(
args
,
"OO|O:reduce"
,
&
func
,
&
seq
,
&
result
))
if
(
!
PyArg_ParseTuple
(
args
,
"OO|O:reduce"
,
&
func
,
&
seq
,
&
result
))
return
NULL
;
if
(
result
!=
NULL
)
INCREF
(
result
);
Py_
INCREF
(
result
);
if
((
sqf
=
seq
->
ob_type
->
tp_as_sequence
)
==
NULL
)
{
err_setstr
(
TypeError
,
PyErr_SetString
(
PyExc_
TypeError
,
"2nd argument to reduce() must be a sequence object"
);
return
NULL
;
}
if
((
args
=
newtupleobject
(
2
))
==
NULL
)
if
((
args
=
PyTuple_New
(
2
))
==
NULL
)
goto
Fail
;
for
(
i
=
0
;
;
++
i
)
{
o
bject
*
op2
;
PyO
bject
*
op2
;
if
(
args
->
ob_refcnt
>
1
)
{
DECREF
(
args
);
if
((
args
=
newtupleobject
(
2
))
==
NULL
)
Py_
DECREF
(
args
);
if
((
args
=
PyTuple_New
(
2
))
==
NULL
)
goto
Fail
;
}
if
((
op2
=
(
*
sqf
->
sq_item
)(
seq
,
i
))
==
NULL
)
{
if
(
err_occurred
()
==
IndexError
)
{
err_c
lear
();
if
(
PyErr_Occurred
()
==
PyExc_
IndexError
)
{
PyErr_C
lear
();
break
;
}
goto
Fail
;
...
...
@@ -1379,62 +1390,62 @@ builtin_reduce(self, args)
if
(
result
==
NULL
)
result
=
op2
;
else
{
settuplei
tem
(
args
,
0
,
result
);
settuplei
tem
(
args
,
1
,
op2
);
if
((
result
=
call_o
bject
(
func
,
args
))
==
NULL
)
PyTuple_SetI
tem
(
args
,
0
,
result
);
PyTuple_SetI
tem
(
args
,
1
,
op2
);
if
((
result
=
PyEval_CallO
bject
(
func
,
args
))
==
NULL
)
goto
Fail
;
}
}
DECREF
(
args
);
Py_
DECREF
(
args
);
if
(
result
==
NULL
)
err_setstr
(
TypeError
,
PyErr_SetString
(
PyExc_
TypeError
,
"reduce of empty sequence with no initial value"
);
return
result
;
Fail:
XDECREF
(
args
);
XDECREF
(
result
);
Py_
XDECREF
(
args
);
Py_
XDECREF
(
result
);
return
NULL
;
}
static
o
bject
*
static
PyO
bject
*
builtin_reload
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
PyO
bject
*
v
;
if
(
!
newgetargs
(
args
,
"O:reload"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"O:reload"
,
&
v
))
return
NULL
;
return
reload_m
odule
(
v
);
return
PyImport_ReloadM
odule
(
v
);
}
static
o
bject
*
static
PyO
bject
*
builtin_repr
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
PyO
bject
*
v
;
if
(
!
newgetargs
(
args
,
"O:repr"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"O:repr"
,
&
v
))
return
NULL
;
return
reprobject
(
v
);
return
PyObject_Repr
(
v
);
}
static
o
bject
*
static
PyO
bject
*
builtin_round
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
double
x
;
double
f
;
int
ndigits
=
0
;
int
i
;
if
(
!
newgetargs
(
args
,
"d|i:round"
,
&
x
,
&
ndigits
))
if
(
!
PyArg_ParseTuple
(
args
,
"d|i:round"
,
&
x
,
&
ndigits
))
return
NULL
;
f
=
1
.
0
;
for
(
i
=
ndigits
;
--
i
>=
0
;
)
...
...
@@ -1442,53 +1453,54 @@ builtin_round(self, args)
for
(
i
=
ndigits
;
++
i
<=
0
;
)
f
=
f
*
0
.
1
;
if
(
x
>=
0
.
0
)
return
newfloatobject
(
floor
(
x
*
f
+
0
.
5
)
/
f
);
return
PyFloat_FromDouble
(
floor
(
x
*
f
+
0
.
5
)
/
f
);
else
return
newfloatobject
(
ceil
(
x
*
f
-
0
.
5
)
/
f
);
return
PyFloat_FromDouble
(
ceil
(
x
*
f
-
0
.
5
)
/
f
);
}
static
o
bject
*
static
PyO
bject
*
builtin_str
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
PyO
bject
*
v
;
if
(
!
newgetargs
(
args
,
"O:str"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"O:str"
,
&
v
))
return
NULL
;
return
strobject
(
v
);
return
PyObject_Str
(
v
);
}
static
o
bject
*
static
PyO
bject
*
builtin_tuple
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
sequence_m
ethods
*
sqf
;
PyO
bject
*
v
;
PySequenceM
ethods
*
sqf
;
if
(
!
newgetargs
(
args
,
"O:tuple"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"O:tuple"
,
&
v
))
return
NULL
;
if
(
is_tupleobject
(
v
))
{
INCREF
(
v
);
if
(
PyTuple_Check
(
v
))
{
Py_
INCREF
(
v
);
return
v
;
}
if
(
is_listobject
(
v
))
return
listt
uple
(
v
);
if
(
is_stringobject
(
v
))
{
int
n
=
getstrings
ize
(
v
);
object
*
t
=
newtupleobject
(
n
);
if
(
PyList_Check
(
v
))
return
PyList_AsT
uple
(
v
);
if
(
PyString_Check
(
v
))
{
int
n
=
PyString_S
ize
(
v
);
PyObject
*
t
=
PyTuple_New
(
n
);
if
(
t
!=
NULL
)
{
int
i
;
char
*
p
=
getstringvalue
(
v
);
char
*
p
=
PyString_AsString
(
v
);
for
(
i
=
0
;
i
<
n
;
i
++
)
{
object
*
item
=
newsizedstringobject
(
p
+
i
,
1
);
PyObject
*
item
=
PyString_FromStringAndSize
(
p
+
i
,
1
);
if
(
item
==
NULL
)
{
DECREF
(
t
);
Py_
DECREF
(
t
);
t
=
NULL
;
break
;
}
settuplei
tem
(
t
,
i
,
item
);
PyTuple_SetI
tem
(
t
,
i
,
item
);
}
}
return
t
;
...
...
@@ -1497,66 +1509,68 @@ builtin_tuple(self, args)
if
((
sqf
=
v
->
ob_type
->
tp_as_sequence
)
!=
NULL
)
{
int
n
=
(
*
sqf
->
sq_length
)(
v
);
int
i
;
o
bject
*
t
;
PyO
bject
*
t
;
if
(
n
<
0
)
return
NULL
;
t
=
newtupleobject
(
n
);
t
=
PyTuple_New
(
n
);
if
(
t
==
NULL
)
return
NULL
;
for
(
i
=
0
;
i
<
n
;
i
++
)
{
o
bject
*
item
=
(
*
sqf
->
sq_item
)(
v
,
i
);
PyO
bject
*
item
=
(
*
sqf
->
sq_item
)(
v
,
i
);
if
(
item
==
NULL
)
{
DECREF
(
t
);
Py_
DECREF
(
t
);
t
=
NULL
;
break
;
}
settuplei
tem
(
t
,
i
,
item
);
PyTuple_SetI
tem
(
t
,
i
,
item
);
}
/* XXX Should support indefinite-length sequences */
return
t
;
}
/* None of the above */
err_setstr
(
TypeError
,
"tuple() argument must be a sequence"
);
PyErr_SetString
(
PyExc_TypeError
,
"tuple() argument must be a sequence"
);
return
NULL
;
}
static
o
bject
*
static
PyO
bject
*
builtin_type
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
;
PyO
bject
*
v
;
if
(
!
newgetargs
(
args
,
"O:type"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"O:type"
,
&
v
))
return
NULL
;
v
=
(
o
bject
*
)
v
->
ob_type
;
INCREF
(
v
);
v
=
(
PyO
bject
*
)
v
->
ob_type
;
Py_
INCREF
(
v
);
return
v
;
}
static
o
bject
*
static
PyO
bject
*
builtin_vars
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
v
=
NULL
;
o
bject
*
d
;
PyO
bject
*
v
=
NULL
;
PyO
bject
*
d
;
if
(
!
newgetargs
(
args
,
"|O:vars"
,
&
v
))
if
(
!
PyArg_ParseTuple
(
args
,
"|O:vars"
,
&
v
))
return
NULL
;
if
(
v
==
NULL
)
{
d
=
getl
ocals
();
d
=
PyEval_GetL
ocals
();
if
(
d
==
NULL
)
{
if
(
!
err_occurred
())
err_setstr
(
SystemError
,
"no locals!?"
);
if
(
!
PyErr_Occurred
())
PyErr_SetString
(
PyExc_SystemError
,
"no locals!?"
);
}
else
INCREF
(
d
);
Py_
INCREF
(
d
);
}
else
{
d
=
getattr
(
v
,
"__dict__"
);
d
=
PyObject_GetAttrString
(
v
,
"__dict__"
);
if
(
d
==
NULL
)
{
err_setstr
(
TypeError
,
PyErr_SetString
(
PyExc_
TypeError
,
"vars() argument must have __dict__ attribute"
);
return
NULL
;
}
...
...
@@ -1564,7 +1578,7 @@ builtin_vars(self, args)
return
d
;
}
static
struct
methodlist
builtin_methods
[]
=
{
static
PyMethodDef
builtin_methods
[]
=
{
{
"__import__"
,
builtin___import__
,
1
},
{
"abs"
,
builtin_abs
,
1
},
{
"apply"
,
builtin_apply
,
1
},
...
...
@@ -1619,201 +1633,202 @@ static struct methodlist builtin_methods[] = {
{
NULL
,
NULL
},
};
static
o
bject
*
builtin_mod
;
static
o
bject
*
builtin_dict
;
static
PyO
bject
*
builtin_mod
;
static
PyO
bject
*
builtin_dict
;
o
bject
*
getbuiltinmod
()
PyO
bject
*
PyBuiltin_GetModule
()
{
return
builtin_mod
;
}
o
bject
*
getbuiltind
ict
()
PyO
bject
*
PyBuiltin_GetD
ict
()
{
return
builtin_dict
;
}
/* Predefined exceptions */
object
*
AccessError
;
o
bject
*
PyExc_AssertionError
;
object
*
AttributeError
;
object
*
EOFError
;
o
bject
*
FloatingPointError
;
object
*
IOError
;
object
*
ImportError
;
object
*
IndexError
;
object
*
KeyError
;
object
*
KeyboardInterrupt
;
object
*
MemoryError
;
object
*
NameError
;
object
*
OverflowError
;
object
*
RuntimeError
;
object
*
SyntaxError
;
object
*
SystemError
;
object
*
SystemExit
;
object
*
TypeError
;
object
*
ValueError
;
object
*
ZeroDivisionError
;
static
o
bject
*
PyObject
*
PyExc_
AccessError
;
PyO
bject
*
PyExc_AssertionError
;
PyObject
*
PyExc_
AttributeError
;
PyObject
*
PyExc_
EOFError
;
PyO
bject
*
FloatingPointError
;
PyObject
*
PyExc_
IOError
;
PyObject
*
PyExc_
ImportError
;
PyObject
*
PyExc_
IndexError
;
PyObject
*
PyExc_
KeyError
;
PyObject
*
PyExc_
KeyboardInterrupt
;
PyObject
*
PyExc_
MemoryError
;
PyObject
*
PyExc_
NameError
;
PyObject
*
PyExc_
OverflowError
;
PyObject
*
PyExc_
RuntimeError
;
PyObject
*
PyExc_
SyntaxError
;
PyObject
*
PyExc_
SystemError
;
PyObject
*
PyExc_
SystemExit
;
PyObject
*
PyExc_
TypeError
;
PyObject
*
PyExc_
ValueError
;
PyObject
*
PyExc_
ZeroDivisionError
;
static
PyO
bject
*
newstdexception
(
name
)
char
*
name
;
{
object
*
v
=
newstringobject
(
name
);
if
(
v
==
NULL
||
dictinsert
(
builtin_dict
,
name
,
v
)
!=
0
)
fatal
(
"no mem for new standard exception"
);
PyObject
*
v
=
PyString_FromString
(
name
);
if
(
v
==
NULL
||
PyDict_SetItemString
(
builtin_dict
,
name
,
v
)
!=
0
)
Py_FatalError
(
"no mem for new standard exception"
);
return
v
;
}
static
void
initerrors
()
{
AccessError
=
newstdexception
(
"AccessError"
);
PyExc_
AccessError
=
newstdexception
(
"AccessError"
);
PyExc_AssertionError
=
newstdexception
(
"AssertionError"
);
AttributeError
=
newstdexception
(
"AttributeError"
);
EOFError
=
newstdexception
(
"EOFError"
);
PyExc_
AttributeError
=
newstdexception
(
"AttributeError"
);
PyExc_
EOFError
=
newstdexception
(
"EOFError"
);
FloatingPointError
=
newstdexception
(
"FloatingPointError"
);
IOError
=
newstdexception
(
"IOError"
);
ImportError
=
newstdexception
(
"ImportError"
);
IndexError
=
newstdexception
(
"IndexError"
);
KeyError
=
newstdexception
(
"KeyError"
);
KeyboardInterrupt
=
newstdexception
(
"KeyboardInterrupt"
);
MemoryError
=
newstdexception
(
"MemoryError"
);
NameError
=
newstdexception
(
"NameError"
);
OverflowError
=
newstdexception
(
"OverflowError"
);
RuntimeError
=
newstdexception
(
"RuntimeError"
);
SyntaxError
=
newstdexception
(
"SyntaxError"
);
SystemError
=
newstdexception
(
"SystemError"
);
SystemExit
=
newstdexception
(
"SystemExit"
);
TypeError
=
newstdexception
(
"TypeError"
);
ValueError
=
newstdexception
(
"ValueError"
);
ZeroDivisionError
=
newstdexception
(
"ZeroDivisionError"
);
PyExc_
IOError
=
newstdexception
(
"IOError"
);
PyExc_
ImportError
=
newstdexception
(
"ImportError"
);
PyExc_
IndexError
=
newstdexception
(
"IndexError"
);
PyExc_
KeyError
=
newstdexception
(
"KeyError"
);
PyExc_
KeyboardInterrupt
=
newstdexception
(
"KeyboardInterrupt"
);
PyExc_
MemoryError
=
newstdexception
(
"MemoryError"
);
PyExc_
NameError
=
newstdexception
(
"NameError"
);
PyExc_
OverflowError
=
newstdexception
(
"OverflowError"
);
PyExc_
RuntimeError
=
newstdexception
(
"RuntimeError"
);
PyExc_
SyntaxError
=
newstdexception
(
"SyntaxError"
);
PyExc_
SystemError
=
newstdexception
(
"SystemError"
);
PyExc_
SystemExit
=
newstdexception
(
"SystemExit"
);
PyExc_
TypeError
=
newstdexception
(
"TypeError"
);
PyExc_
ValueError
=
newstdexception
(
"ValueError"
);
PyExc_
ZeroDivisionError
=
newstdexception
(
"ZeroDivisionError"
);
}
void
initbuiltin
()
PyBuiltin_Init
()
{
builtin_mod
=
initm
odule
(
"__builtin__"
,
builtin_methods
);
builtin_dict
=
getmoduled
ict
(
builtin_mod
);
INCREF
(
builtin_dict
);
builtin_mod
=
Py_InitM
odule
(
"__builtin__"
,
builtin_methods
);
builtin_dict
=
PyModule_GetD
ict
(
builtin_mod
);
Py_
INCREF
(
builtin_dict
);
initerrors
();
(
void
)
dictinsert
(
builtin_dict
,
"None"
,
None
);
(
void
)
dictinsert
(
builtin_dict
,
"Ellipsis"
,
Py_Ellipsis
);
(
void
)
dictinsert
(
builtin_dict
,
"__debug__"
,
newintobject
(
Py_OptimizeFlag
==
0
));
if
(
err_occurred
())
fatal
(
"error creating None/Ellipsis/__debug__ in __builtin__"
);
(
void
)
PyDict_SetItemString
(
builtin_dict
,
"None"
,
Py_None
);
(
void
)
PyDict_SetItemString
(
builtin_dict
,
"Ellipsis"
,
Py_Ellipsis
);
(
void
)
PyDict_SetItemString
(
builtin_dict
,
"__debug__"
,
PyInt_FromLong
(
Py_OptimizeFlag
==
0
));
if
(
PyErr_Occurred
())
Py_FatalError
(
"error creating None/Ellipsis/__debug__ in __builtin__"
);
}
/* Helper for filter(): filter a tuple through a function */
static
o
bject
*
static
PyO
bject
*
filtertuple
(
func
,
tuple
)
o
bject
*
func
;
o
bject
*
tuple
;
PyO
bject
*
func
;
PyO
bject
*
tuple
;
{
o
bject
*
result
;
PyO
bject
*
result
;
register
int
i
,
j
;
int
len
=
gettuples
ize
(
tuple
);
int
len
=
PyTuple_S
ize
(
tuple
);
if
(
len
==
0
)
{
INCREF
(
tuple
);
Py_
INCREF
(
tuple
);
return
tuple
;
}
if
((
result
=
newtupleobject
(
len
))
==
NULL
)
if
((
result
=
PyTuple_New
(
len
))
==
NULL
)
return
NULL
;
for
(
i
=
j
=
0
;
i
<
len
;
++
i
)
{
o
bject
*
item
,
*
good
;
PyO
bject
*
item
,
*
good
;
int
ok
;
if
((
item
=
gettuplei
tem
(
tuple
,
i
))
==
NULL
)
if
((
item
=
PyTuple_GetI
tem
(
tuple
,
i
))
==
NULL
)
goto
Fail_1
;
if
(
func
==
None
)
{
INCREF
(
item
);
if
(
func
==
Py_
None
)
{
Py_
INCREF
(
item
);
good
=
item
;
}
else
{
object
*
arg
=
mkv
alue
(
"(O)"
,
item
);
PyObject
*
arg
=
Py_BuildV
alue
(
"(O)"
,
item
);
if
(
arg
==
NULL
)
goto
Fail_1
;
good
=
call_o
bject
(
func
,
arg
);
DECREF
(
arg
);
good
=
PyEval_CallO
bject
(
func
,
arg
);
Py_
DECREF
(
arg
);
if
(
good
==
NULL
)
goto
Fail_1
;
}
ok
=
testbool
(
good
);
DECREF
(
good
);
ok
=
PyObject_IsTrue
(
good
);
Py_
DECREF
(
good
);
if
(
ok
)
{
INCREF
(
item
);
if
(
settuplei
tem
(
result
,
j
++
,
item
)
<
0
)
Py_
INCREF
(
item
);
if
(
PyTuple_SetI
tem
(
result
,
j
++
,
item
)
<
0
)
goto
Fail_1
;
}
}
if
(
resizetupl
e
(
&
result
,
j
,
0
)
<
0
)
if
(
_PyTuple_Resiz
e
(
&
result
,
j
,
0
)
<
0
)
return
NULL
;
return
result
;
Fail_1:
DECREF
(
result
);
Py_
DECREF
(
result
);
return
NULL
;
}
/* Helper for filter(): filter a string through a function */
static
o
bject
*
static
PyO
bject
*
filterstring
(
func
,
strobj
)
o
bject
*
func
;
o
bject
*
strobj
;
PyO
bject
*
func
;
PyO
bject
*
strobj
;
{
o
bject
*
result
;
PyO
bject
*
result
;
register
int
i
,
j
;
int
len
=
getstrings
ize
(
strobj
);
int
len
=
PyString_S
ize
(
strobj
);
if
(
func
==
None
)
{
if
(
func
==
Py_
None
)
{
/* No character is ever false -- share input string */
INCREF
(
strobj
);
Py_
INCREF
(
strobj
);
return
strobj
;
}
if
((
result
=
newsizedstringobject
(
NULL
,
len
))
==
NULL
)
if
((
result
=
PyString_FromStringAndSize
(
NULL
,
len
))
==
NULL
)
return
NULL
;
for
(
i
=
j
=
0
;
i
<
len
;
++
i
)
{
o
bject
*
item
,
*
arg
,
*
good
;
PyO
bject
*
item
,
*
arg
,
*
good
;
int
ok
;
item
=
(
*
strobj
->
ob_type
->
tp_as_sequence
->
sq_item
)(
strobj
,
i
);
if
(
item
==
NULL
)
goto
Fail_1
;
arg
=
mkv
alue
(
"(O)"
,
item
);
DECREF
(
item
);
arg
=
Py_BuildV
alue
(
"(O)"
,
item
);
Py_
DECREF
(
item
);
if
(
arg
==
NULL
)
goto
Fail_1
;
good
=
call_o
bject
(
func
,
arg
);
DECREF
(
arg
);
good
=
PyEval_CallO
bject
(
func
,
arg
);
Py_
DECREF
(
arg
);
if
(
good
==
NULL
)
goto
Fail_1
;
ok
=
testbool
(
good
);
DECREF
(
good
);
ok
=
PyObject_IsTrue
(
good
);
Py_
DECREF
(
good
);
if
(
ok
)
GETSTRINGVALUE
((
stringo
bject
*
)
result
)[
j
++
]
=
GETSTRINGVALUE
((
stringo
bject
*
)
item
)[
0
];
PyString_AS_STRING
((
PyStringO
bject
*
)
result
)[
j
++
]
=
PyString_AS_STRING
((
PyStringO
bject
*
)
item
)[
0
];
}
if
(
j
<
len
&&
resizestring
(
&
result
,
j
)
<
0
)
if
(
j
<
len
&&
_PyString_Resize
(
&
result
,
j
)
<
0
)
return
NULL
;
return
result
;
Fail_1:
DECREF
(
result
);
Py_
DECREF
(
result
);
return
NULL
;
}
...
...
Python/compile.c
View file @
5574f2d0
...
...
@@ -47,7 +47,7 @@ PERFORMANCE OF THIS SOFTWARE.
#define PRIVATE_NAME_MANGLING
#endif
#include "
allobjects
.h"
#include "
Python
.h"
#include "node.h"
#include "token.h"
...
...
@@ -57,7 +57,6 @@ PERFORMANCE OF THIS SOFTWARE.
#include "structmember.h"
#include <ctype.h>
#include <errno.h>
int
Py_OptimizeFlag
=
0
;
...
...
@@ -65,7 +64,7 @@ int Py_OptimizeFlag = 0;
#define OP_ASSIGN 1
#define OP_APPLY 2
#define OFF(x) offsetof(
codeo
bject, x)
#define OFF(x) offsetof(
PyCodeO
bject, x)
static
struct
memberlist
code_memberlist
[]
=
{
{
"co_argcount"
,
T_INT
,
OFF
(
co_argcount
),
READONLY
},
...
...
@@ -83,50 +82,50 @@ static struct memberlist code_memberlist[] = {
{
NULL
}
/* Sentinel */
};
static
o
bject
*
static
PyO
bject
*
code_getattr
(
co
,
name
)
codeo
bject
*
co
;
PyCodeO
bject
*
co
;
char
*
name
;
{
return
getmember
((
char
*
)
co
,
code_memberlist
,
name
);
return
PyMember_Get
((
char
*
)
co
,
code_memberlist
,
name
);
}
static
void
code_dealloc
(
co
)
codeo
bject
*
co
;
PyCodeO
bject
*
co
;
{
XDECREF
(
co
->
co_code
);
XDECREF
(
co
->
co_consts
);
XDECREF
(
co
->
co_names
);
XDECREF
(
co
->
co_filename
);
XDECREF
(
co
->
co_name
);
XDECREF
(
co
->
co_varnames
);
DEL
(
co
);
Py_
XDECREF
(
co
->
co_code
);
Py_
XDECREF
(
co
->
co_consts
);
Py_
XDECREF
(
co
->
co_names
);
Py_
XDECREF
(
co
->
co_filename
);
Py_
XDECREF
(
co
->
co_name
);
Py_
XDECREF
(
co
->
co_varnames
);
PyMem_
DEL
(
co
);
}
static
o
bject
*
static
PyO
bject
*
code_repr
(
co
)
codeo
bject
*
co
;
PyCodeO
bject
*
co
;
{
char
buf
[
500
];
int
lineno
=
-
1
;
char
*
p
=
GETSTRINGVALUE
(
co
->
co_code
);
char
*
p
=
PyString_AS_STRING
(
co
->
co_code
);
char
*
filename
=
"???"
;
char
*
name
=
"???"
;
if
(
*
p
==
SET_LINENO
)
lineno
=
(
p
[
1
]
&
0xff
)
|
((
p
[
2
]
&
0xff
)
<<
8
);
if
(
co
->
co_filename
&&
is_stringobject
(
co
->
co_filename
))
filename
=
getstringvalue
(
co
->
co_filename
);
if
(
co
->
co_name
&&
is_stringobject
(
co
->
co_name
))
name
=
getstringvalue
(
co
->
co_name
);
if
(
co
->
co_filename
&&
PyString_Check
(
co
->
co_filename
))
filename
=
PyString_AsString
(
co
->
co_filename
);
if
(
co
->
co_name
&&
PyString_Check
(
co
->
co_name
))
name
=
PyString_AsString
(
co
->
co_name
);
sprintf
(
buf
,
"<code object %.100s at %lx, file
\"
%.300s
\"
, line %d>"
,
name
,
(
long
)
co
,
filename
,
lineno
);
return
newstringobject
(
buf
);
return
PyString_FromString
(
buf
);
}
static
int
code_compare
(
co
,
cp
)
codeo
bject
*
co
,
*
cp
;
PyCodeO
bject
*
co
,
*
cp
;
{
int
cmp
;
cmp
=
cp
->
co_argcount
-
cp
->
co_argcount
;
...
...
@@ -135,28 +134,29 @@ code_compare(co, cp)
if
(
cmp
)
return
cmp
;
cmp
=
cp
->
co_flags
-
cp
->
co_flags
;
if
(
cmp
)
return
cmp
;
cmp
=
cmpobject
((
object
*
)
co
->
co_code
,
(
object
*
)
cp
->
co_code
);
cmp
=
PyObject_Compare
((
PyObject
*
)
co
->
co_code
,
(
PyObject
*
)
cp
->
co_code
);
if
(
cmp
)
return
cmp
;
cmp
=
cmpobject
(
co
->
co_consts
,
cp
->
co_consts
);
cmp
=
PyObject_Compare
(
co
->
co_consts
,
cp
->
co_consts
);
if
(
cmp
)
return
cmp
;
cmp
=
cmpobject
(
co
->
co_names
,
cp
->
co_names
);
cmp
=
PyObject_Compare
(
co
->
co_names
,
cp
->
co_names
);
if
(
cmp
)
return
cmp
;
cmp
=
cmpobject
(
co
->
co_varnames
,
cp
->
co_varnames
);
cmp
=
PyObject_Compare
(
co
->
co_varnames
,
cp
->
co_varnames
);
return
cmp
;
}
static
long
code_hash
(
co
)
codeo
bject
*
co
;
PyCodeO
bject
*
co
;
{
long
h
,
h1
,
h2
,
h3
,
h4
;
h1
=
hashobject
((
o
bject
*
)
co
->
co_code
);
h1
=
PyObject_Hash
((
PyO
bject
*
)
co
->
co_code
);
if
(
h1
==
-
1
)
return
-
1
;
h2
=
hashobject
(
co
->
co_consts
);
h2
=
PyObject_Hash
(
co
->
co_consts
);
if
(
h2
==
-
1
)
return
-
1
;
h3
=
hashobject
(
co
->
co_names
);
h3
=
PyObject_Hash
(
co
->
co_names
);
if
(
h3
==
-
1
)
return
-
1
;
h4
=
hashobject
(
co
->
co_varnames
);
h4
=
PyObject_Hash
(
co
->
co_varnames
);
if
(
h4
==
-
1
)
return
-
1
;
h
=
h1
^
h2
^
h3
^
h4
^
co
->
co_argcount
^
co
->
co_nlocals
^
co
->
co_flags
;
...
...
@@ -164,11 +164,11 @@ code_hash(co)
return
h
;
}
typeobject
Codet
ype
=
{
OB_HEAD_INIT
(
&
Typet
ype
)
PyTypeObject
PyCode_T
ype
=
{
PyObject_HEAD_INIT
(
&
PyType_T
ype
)
0
,
"code"
,
sizeof
(
codeo
bject
),
sizeof
(
PyCodeO
bject
),
0
,
(
destructor
)
code_dealloc
,
/*tp_dealloc*/
0
,
/*tp_print*/
...
...
@@ -185,86 +185,86 @@ typeobject Codetype = {
#define NAME_CHARS \
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
codeo
bject
*
newcodeobject
(
argcount
,
nlocals
,
stacksize
,
flags
,
PyCodeO
bject
*
PyCode_New
(
argcount
,
nlocals
,
stacksize
,
flags
,
code
,
consts
,
names
,
varnames
,
filename
,
name
,
firstlineno
,
lnotab
)
int
argcount
;
int
nlocals
;
int
stacksize
;
int
flags
;
o
bject
*
code
;
o
bject
*
consts
;
o
bject
*
names
;
o
bject
*
varnames
;
o
bject
*
filename
;
o
bject
*
name
;
PyO
bject
*
code
;
PyO
bject
*
consts
;
PyO
bject
*
names
;
PyO
bject
*
varnames
;
PyO
bject
*
filename
;
PyO
bject
*
name
;
int
firstlineno
;
o
bject
*
lnotab
;
PyO
bject
*
lnotab
;
{
codeo
bject
*
co
;
PyCodeO
bject
*
co
;
int
i
;
/* Check argument types */
if
(
argcount
<
0
||
nlocals
<
0
||
code
==
NULL
||
!
is_stringobject
(
code
)
||
consts
==
NULL
||
!
is_tupleobject
(
consts
)
||
names
==
NULL
||
!
is_tupleobject
(
names
)
||
varnames
==
NULL
||
!
is_tupleobject
(
varnames
)
||
name
==
NULL
||
!
is_stringobject
(
name
)
||
filename
==
NULL
||
!
is_stringobject
(
filename
)
||
lnotab
==
NULL
||
!
is_stringobject
(
lnotab
))
{
err_badc
all
();
code
==
NULL
||
!
PyString_Check
(
code
)
||
consts
==
NULL
||
!
PyTuple_Check
(
consts
)
||
names
==
NULL
||
!
PyTuple_Check
(
names
)
||
varnames
==
NULL
||
!
PyTuple_Check
(
varnames
)
||
name
==
NULL
||
!
PyString_Check
(
name
)
||
filename
==
NULL
||
!
PyString_Check
(
filename
)
||
lnotab
==
NULL
||
!
PyString_Check
(
lnotab
))
{
PyErr_BadInternalC
all
();
return
NULL
;
}
/* Make sure names and varnames are all strings, & intern them */
for
(
i
=
gettuples
ize
(
names
);
--
i
>=
0
;
)
{
object
*
v
=
gettuplei
tem
(
names
,
i
);
if
(
v
==
NULL
||
!
is_stringobject
(
v
))
{
err_badc
all
();
for
(
i
=
PyTuple_S
ize
(
names
);
--
i
>=
0
;
)
{
PyObject
*
v
=
PyTuple_GetI
tem
(
names
,
i
);
if
(
v
==
NULL
||
!
PyString_Check
(
v
))
{
PyErr_BadInternalC
all
();
return
NULL
;
}
PyString_InternInPlace
(
&
PyTuple_GET_ITEM
(
names
,
i
));
}
for
(
i
=
gettuples
ize
(
varnames
);
--
i
>=
0
;
)
{
object
*
v
=
gettuplei
tem
(
varnames
,
i
);
if
(
v
==
NULL
||
!
is_stringobject
(
v
))
{
err_badc
all
();
for
(
i
=
PyTuple_S
ize
(
varnames
);
--
i
>=
0
;
)
{
PyObject
*
v
=
PyTuple_GetI
tem
(
varnames
,
i
);
if
(
v
==
NULL
||
!
PyString_Check
(
v
))
{
PyErr_BadInternalC
all
();
return
NULL
;
}
PyString_InternInPlace
(
&
PyTuple_GET_ITEM
(
varnames
,
i
));
}
/* Intern selected string constants */
for
(
i
=
gettuples
ize
(
consts
);
--
i
>=
0
;
)
{
object
*
v
=
gettuplei
tem
(
consts
,
i
);
for
(
i
=
PyTuple_S
ize
(
consts
);
--
i
>=
0
;
)
{
PyObject
*
v
=
PyTuple_GetI
tem
(
consts
,
i
);
char
*
p
;
if
(
!
is_stringobject
(
v
))
if
(
!
PyString_Check
(
v
))
continue
;
p
=
getstringvalue
(
v
);
p
=
PyString_AsString
(
v
);
if
((
int
)
strspn
(
p
,
NAME_CHARS
)
!=
getstrings
ize
(
v
))
!=
PyString_S
ize
(
v
))
continue
;
PyString_InternInPlace
(
&
PyTuple_GET_ITEM
(
consts
,
i
));
}
co
=
NEWOBJ
(
codeobject
,
&
Codet
ype
);
co
=
PyObject_NEW
(
PyCodeObject
,
&
PyCode_T
ype
);
if
(
co
!=
NULL
)
{
co
->
co_argcount
=
argcount
;
co
->
co_nlocals
=
nlocals
;
co
->
co_stacksize
=
stacksize
;
co
->
co_flags
=
flags
;
INCREF
(
code
);
co
->
co_code
=
(
stringo
bject
*
)
code
;
INCREF
(
consts
);
Py_
INCREF
(
code
);
co
->
co_code
=
(
PyStringO
bject
*
)
code
;
Py_
INCREF
(
consts
);
co
->
co_consts
=
consts
;
INCREF
(
names
);
Py_
INCREF
(
names
);
co
->
co_names
=
names
;
INCREF
(
varnames
);
Py_
INCREF
(
varnames
);
co
->
co_varnames
=
varnames
;
INCREF
(
filename
);
Py_
INCREF
(
filename
);
co
->
co_filename
=
filename
;
INCREF
(
name
);
Py_
INCREF
(
name
);
co
->
co_name
=
name
;
co
->
co_firstlineno
=
firstlineno
;
INCREF
(
lnotab
);
Py_
INCREF
(
lnotab
);
co
->
co_lnotab
=
lnotab
;
}
return
co
;
...
...
@@ -274,12 +274,12 @@ newcodeobject(argcount, nlocals, stacksize, flags,
/* Data structure used internally */
struct
compiling
{
o
bject
*
c_code
;
/* string */
o
bject
*
c_consts
;
/* list of objects */
o
bject
*
c_names
;
/* list of strings (names) */
o
bject
*
c_globals
;
/* dictionary (value=None) */
o
bject
*
c_locals
;
/* dictionary (value=localID) */
o
bject
*
c_varnames
;
/* list (inverse of c_locals) */
PyO
bject
*
c_code
;
/* string */
PyO
bject
*
c_consts
;
/* list of objects */
PyO
bject
*
c_names
;
/* list of strings (names) */
PyO
bject
*
c_globals
;
/* dictionary (value=None) */
PyO
bject
*
c_locals
;
/* dictionary (value=localID) */
PyO
bject
*
c_varnames
;
/* list (inverse of c_locals) */
int
c_nlocals
;
/* index of next local */
int
c_argcount
;
/* number of top-level arguments */
int
c_flags
;
/* same as co_flags */
...
...
@@ -297,7 +297,7 @@ struct compiling {
int
c_stacklevel
;
/* Current stack level */
int
c_maxstacklevel
;
/* Maximum stack level */
int
c_firstlineno
;
o
bject
*
c_lnotab
;
/* Table mapping address to line number */
PyO
bject
*
c_lnotab
;
/* Table mapping address to line number */
int
c_last_addr
,
c_last_line
,
c_lnotab_next
;
#ifdef PRIVATE_NAME_MANGLING
char
*
c_private
;
/* for private name mangling */
...
...
@@ -310,28 +310,28 @@ struct compiling {
static
void
com_error
(
c
,
exc
,
msg
)
struct
compiling
*
c
;
o
bject
*
exc
;
PyO
bject
*
exc
;
char
*
msg
;
{
int
n
=
strlen
(
msg
);
o
bject
*
v
;
PyO
bject
*
v
;
char
buffer
[
30
];
char
*
s
;
c
->
c_errors
++
;
if
(
c
->
c_lineno
<=
1
)
{
/* Unknown line number or single interactive command */
err_setstr
(
exc
,
msg
);
PyErr_SetString
(
exc
,
msg
);
return
;
}
sprintf
(
buffer
,
" (line %d)"
,
c
->
c_lineno
);
v
=
newsizedstringobject
((
char
*
)
NULL
,
n
+
strlen
(
buffer
));
v
=
PyString_FromStringAndSize
((
char
*
)
NULL
,
n
+
strlen
(
buffer
));
if
(
v
==
NULL
)
return
;
/* MemoryError, too bad */
s
=
GETSTRINGVALUE
((
stringo
bject
*
)
v
);
s
=
PyString_AS_STRING
((
PyStringO
bject
*
)
v
);
strcpy
(
s
,
msg
);
strcat
(
s
,
buffer
);
err_setval
(
exc
,
v
);
DECREF
(
v
);
PyErr_SetObject
(
exc
,
v
);
Py_
DECREF
(
v
);
}
...
...
@@ -343,7 +343,8 @@ block_push(c, type)
int
type
;
{
if
(
c
->
c_nblocks
>=
CO_MAXBLOCKS
)
{
com_error
(
c
,
SystemError
,
"too many statically nested blocks"
);
com_error
(
c
,
PyExc_SystemError
,
"too many statically nested blocks"
);
}
else
{
c
->
c_block
[
c
->
c_nblocks
++
]
=
type
;
...
...
@@ -358,55 +359,58 @@ block_pop(c, type)
if
(
c
->
c_nblocks
>
0
)
c
->
c_nblocks
--
;
if
(
c
->
c_block
[
c
->
c_nblocks
]
!=
type
&&
c
->
c_errors
==
0
)
{
com_error
(
c
,
SystemError
,
"bad block pop"
);
com_error
(
c
,
PyExc_
SystemError
,
"bad block pop"
);
}
}
/* Prototype forward declarations */
static
int
com_init
PROTO
((
struct
compiling
*
,
char
*
));
static
void
com_free
PROTO
((
struct
compiling
*
));
static
void
com_push
PROTO
((
struct
compiling
*
,
int
));
static
void
com_pop
PROTO
((
struct
compiling
*
,
int
));
static
void
com_done
PROTO
((
struct
compiling
*
));
static
void
com_node
PROTO
((
struct
compiling
*
,
struct
_node
*
));
static
void
com_factor
PROTO
((
struct
compiling
*
,
struct
_node
*
));
static
void
com_addbyte
PROTO
((
struct
compiling
*
,
int
));
static
void
com_addint
PROTO
((
struct
compiling
*
,
int
));
static
void
com_addoparg
PROTO
((
struct
compiling
*
,
int
,
int
));
static
void
com_addfwref
PROTO
((
struct
compiling
*
,
int
,
int
*
));
static
void
com_backpatch
PROTO
((
struct
compiling
*
,
int
));
static
int
com_add
PROTO
((
struct
compiling
*
,
object
*
,
object
*
));
static
int
com_addconst
PROTO
((
struct
compiling
*
,
object
*
));
static
int
com_addname
PROTO
((
struct
compiling
*
,
object
*
));
static
void
com_addopname
PROTO
((
struct
compiling
*
,
int
,
node
*
));
static
void
com_list
PROTO
((
struct
compiling
*
,
node
*
,
int
));
static
int
com_argdefs
PROTO
((
struct
compiling
*
,
node
*
));
static
int
com_newlocal
PROTO
((
struct
compiling
*
,
char
*
));
static
codeobject
*
icompile
PROTO
((
struct
_node
*
,
struct
compiling
*
));
static
codeobject
*
jcompile
PROTO
((
struct
_node
*
,
char
*
,
struct
compiling
*
));
static
object
*
parsestrplus
PROTO
((
node
*
));
static
object
*
parsestr
PROTO
((
char
*
));
static
int
com_init
Py_PROTO
((
struct
compiling
*
,
char
*
));
static
void
com_free
Py_PROTO
((
struct
compiling
*
));
static
void
com_push
Py_PROTO
((
struct
compiling
*
,
int
));
static
void
com_pop
Py_PROTO
((
struct
compiling
*
,
int
));
static
void
com_done
Py_PROTO
((
struct
compiling
*
));
static
void
com_node
Py_PROTO
((
struct
compiling
*
,
struct
_node
*
));
static
void
com_factor
Py_PROTO
((
struct
compiling
*
,
struct
_node
*
));
static
void
com_addbyte
Py_PROTO
((
struct
compiling
*
,
int
));
static
void
com_addint
Py_PROTO
((
struct
compiling
*
,
int
));
static
void
com_addoparg
Py_PROTO
((
struct
compiling
*
,
int
,
int
));
static
void
com_addfwref
Py_PROTO
((
struct
compiling
*
,
int
,
int
*
));
static
void
com_backpatch
Py_PROTO
((
struct
compiling
*
,
int
));
static
int
com_add
Py_PROTO
((
struct
compiling
*
,
PyObject
*
,
PyObject
*
));
static
int
com_addconst
Py_PROTO
((
struct
compiling
*
,
PyObject
*
));
static
int
com_addname
Py_PROTO
((
struct
compiling
*
,
PyObject
*
));
static
void
com_addopname
Py_PROTO
((
struct
compiling
*
,
int
,
node
*
));
static
void
com_list
Py_PROTO
((
struct
compiling
*
,
node
*
,
int
));
static
int
com_argdefs
Py_PROTO
((
struct
compiling
*
,
node
*
));
static
int
com_newlocal
Py_PROTO
((
struct
compiling
*
,
char
*
));
static
PyCodeObject
*
icompile
Py_PROTO
((
struct
_node
*
,
struct
compiling
*
));
static
PyCodeObject
*
jcompile
Py_PROTO
((
struct
_node
*
,
char
*
,
struct
compiling
*
));
static
PyObject
*
parsestrplus
Py_PROTO
((
node
*
));
static
PyObject
*
parsestr
Py_PROTO
((
char
*
));
static
int
com_init
(
c
,
filename
)
struct
compiling
*
c
;
char
*
filename
;
{
if
((
c
->
c_code
=
newsizedstringobject
((
char
*
)
NULL
,
1000
))
==
NULL
)
if
((
c
->
c_code
=
PyString_FromStringAndSize
((
char
*
)
NULL
,
1000
))
==
NULL
)
goto
fail_3
;
if
((
c
->
c_consts
=
newlistobject
(
0
))
==
NULL
)
if
((
c
->
c_consts
=
PyList_New
(
0
))
==
NULL
)
goto
fail_2
;
if
((
c
->
c_names
=
newlistobject
(
0
))
==
NULL
)
if
((
c
->
c_names
=
PyList_New
(
0
))
==
NULL
)
goto
fail_1
;
if
((
c
->
c_globals
=
newdictobject
())
==
NULL
)
if
((
c
->
c_globals
=
PyDict_New
())
==
NULL
)
goto
fail_0
;
if
((
c
->
c_locals
=
newdictobject
())
==
NULL
)
if
((
c
->
c_locals
=
PyDict_New
())
==
NULL
)
goto
fail_00
;
if
((
c
->
c_varnames
=
newlistobject
(
0
))
==
NULL
)
if
((
c
->
c_varnames
=
PyList_New
(
0
))
==
NULL
)
goto
fail_000
;
if
((
c
->
c_lnotab
=
newsizedstringobject
((
char
*
)
NULL
,
1000
))
==
NULL
)
if
((
c
->
c_lnotab
=
PyString_FromStringAndSize
((
char
*
)
NULL
,
1000
))
==
NULL
)
goto
fail_0000
;
c
->
c_nlocals
=
0
;
c
->
c_argcount
=
0
;
...
...
@@ -430,17 +434,17 @@ com_init(c, filename)
return
1
;
fail_0000:
DECREF
(
c
->
c_lnotab
);
Py_
DECREF
(
c
->
c_lnotab
);
fail_000:
DECREF
(
c
->
c_locals
);
Py_
DECREF
(
c
->
c_locals
);
fail_00:
DECREF
(
c
->
c_globals
);
Py_
DECREF
(
c
->
c_globals
);
fail_0:
DECREF
(
c
->
c_names
);
Py_
DECREF
(
c
->
c_names
);
fail_1:
DECREF
(
c
->
c_consts
);
Py_
DECREF
(
c
->
c_consts
);
fail_2:
DECREF
(
c
->
c_code
);
Py_
DECREF
(
c
->
c_code
);
fail_3:
return
0
;
}
...
...
@@ -449,13 +453,13 @@ static void
com_free
(
c
)
struct
compiling
*
c
;
{
XDECREF
(
c
->
c_code
);
XDECREF
(
c
->
c_consts
);
XDECREF
(
c
->
c_names
);
XDECREF
(
c
->
c_globals
);
XDECREF
(
c
->
c_locals
);
XDECREF
(
c
->
c_varnames
);
XDECREF
(
c
->
c_lnotab
);
Py_
XDECREF
(
c
->
c_code
);
Py_
XDECREF
(
c
->
c_consts
);
Py_
XDECREF
(
c
->
c_names
);
Py_
XDECREF
(
c
->
c_globals
);
Py_
XDECREF
(
c
->
c_locals
);
Py_
XDECREF
(
c
->
c_varnames
);
Py_
XDECREF
(
c
->
c_lnotab
);
}
static
void
...
...
@@ -489,9 +493,9 @@ com_done(c)
struct
compiling
*
c
;
{
if
(
c
->
c_code
!=
NULL
)
resizestring
(
&
c
->
c_code
,
c
->
c_nexti
);
_PyString_Resize
(
&
c
->
c_code
,
c
->
c_nexti
);
if
(
c
->
c_lnotab
!=
NULL
)
resizestring
(
&
c
->
c_lnotab
,
c
->
c_lnotab_next
);
_PyString_Resize
(
&
c
->
c_lnotab
,
c
->
c_lnotab_next
);
}
static
void
...
...
@@ -506,18 +510,19 @@ com_addbyte(c, byte)
fprintf(stderr, "XXX compiling bad byte: %d\n", byte);
fatal("com_addbyte: byte out of range");
*/
com_error
(
c
,
SystemError
,
"com_addbyte: byte out of range"
);
com_error
(
c
,
PyExc_SystemError
,
"com_addbyte: byte out of range"
);
}
if
(
c
->
c_code
==
NULL
)
return
;
len
=
getstrings
ize
(
c
->
c_code
);
len
=
PyString_S
ize
(
c
->
c_code
);
if
(
c
->
c_nexti
>=
len
)
{
if
(
resizestring
(
&
c
->
c_code
,
len
+
1000
)
!=
0
)
{
if
(
_PyString_Resize
(
&
c
->
c_code
,
len
+
1000
)
!=
0
)
{
c
->
c_errors
++
;
return
;
}
}
getstringvalue
(
c
->
c_code
)[
c
->
c_nexti
++
]
=
byte
;
PyString_AsString
(
c
->
c_code
)[
c
->
c_nexti
++
]
=
byte
;
}
static
void
...
...
@@ -539,14 +544,14 @@ com_add_lnotab(c, addr, line)
char
*
p
;
if
(
c
->
c_lnotab
==
NULL
)
return
;
size
=
getstrings
ize
(
c
->
c_lnotab
);
size
=
PyString_S
ize
(
c
->
c_lnotab
);
if
(
c
->
c_lnotab_next
+
2
>
size
)
{
if
(
resizestring
(
&
c
->
c_lnotab
,
size
+
1000
)
<
0
)
{
if
(
_PyString_Resize
(
&
c
->
c_lnotab
,
size
+
1000
)
<
0
)
{
c
->
c_errors
++
;
return
;
}
}
p
=
getstringvalue
(
c
->
c_lnotab
)
+
c
->
c_lnotab_next
;
p
=
PyString_AsString
(
c
->
c_lnotab
)
+
c
->
c_lnotab_next
;
*
p
++
=
addr
;
*
p
++
=
line
;
c
->
c_lnotab_next
+=
2
;
...
...
@@ -616,7 +621,7 @@ com_backpatch(c, anchor)
struct
compiling
*
c
;
int
anchor
;
/* Must be nonzero */
{
unsigned
char
*
code
=
(
unsigned
char
*
)
getstringvalue
(
c
->
c_code
);
unsigned
char
*
code
=
(
unsigned
char
*
)
PyString_AsString
(
c
->
c_code
);
int
target
=
c
->
c_nexti
;
int
dist
;
int
prev
;
...
...
@@ -637,17 +642,17 @@ com_backpatch(c, anchor)
static
int
com_add
(
c
,
list
,
v
)
struct
compiling
*
c
;
o
bject
*
list
;
o
bject
*
v
;
PyO
bject
*
list
;
PyO
bject
*
v
;
{
int
n
=
getlists
ize
(
list
);
int
n
=
PyList_S
ize
(
list
);
int
i
;
for
(
i
=
n
;
--
i
>=
0
;
)
{
object
*
w
=
getlisti
tem
(
list
,
i
);
if
(
v
->
ob_type
==
w
->
ob_type
&&
cmpobject
(
v
,
w
)
==
0
)
PyObject
*
w
=
PyList_GetI
tem
(
list
,
i
);
if
(
v
->
ob_type
==
w
->
ob_type
&&
PyObject_Compare
(
v
,
w
)
==
0
)
return
i
;
}
if
(
addlistitem
(
list
,
v
)
!=
0
)
if
(
PyList_Append
(
list
,
v
)
!=
0
)
c
->
c_errors
++
;
return
n
;
}
...
...
@@ -655,7 +660,7 @@ com_add(c, list, v)
static
int
com_addconst
(
c
,
v
)
struct
compiling
*
c
;
o
bject
*
v
;
PyO
bject
*
v
;
{
return
com_add
(
c
,
c
->
c_consts
,
v
);
}
...
...
@@ -663,7 +668,7 @@ com_addconst(c, v)
static
int
com_addname
(
c
,
v
)
struct
compiling
*
c
;
o
bject
*
v
;
PyO
bject
*
v
;
{
return
com_add
(
c
,
c
->
c_names
,
v
);
}
...
...
@@ -709,7 +714,7 @@ com_addopnamestr(c, op, name)
int
op
;
char
*
name
;
{
o
bject
*
v
;
PyO
bject
*
v
;
int
i
;
#ifdef PRIVATE_NAME_MANGLING
char
buffer
[
256
];
...
...
@@ -724,14 +729,14 @@ com_addopnamestr(c, op, name)
}
else
{
i
=
com_addname
(
c
,
v
);
DECREF
(
v
);
Py_
DECREF
(
v
);
}
/* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */
switch
(
op
)
{
case
LOAD_NAME
:
case
STORE_NAME
:
case
DELETE_NAME
:
if
(
dictlookup
(
c
->
c_globals
,
name
)
!=
NULL
)
{
if
(
PyDict_GetItemString
(
c
->
c_globals
,
name
)
!=
NULL
)
{
switch
(
op
)
{
case
LOAD_NAME
:
op
=
LOAD_GLOBAL
;
break
;
case
STORE_NAME
:
op
=
STORE_GLOBAL
;
break
;
...
...
@@ -762,7 +767,7 @@ com_addopname(c, op, n)
for
(
i
=
0
;
i
<
NCH
(
n
);
i
+=
2
)
{
char
*
s
=
STR
(
CHILD
(
n
,
i
));
if
(
p
+
strlen
(
s
)
>
buffer
+
(
sizeof
buffer
)
-
2
)
{
com_error
(
c
,
MemoryError
,
com_error
(
c
,
PyExc_
MemoryError
,
"dotted_name too long"
);
name
=
NULL
;
break
;
...
...
@@ -780,14 +785,15 @@ com_addopname(c, op, n)
com_addopnamestr
(
c
,
op
,
name
);
}
static
o
bject
*
static
PyO
bject
*
parsenumber
(
co
,
s
)
struct
compiling
*
co
;
char
*
s
;
{
extern
long
mystrtol
PROTO
((
const
char
*
,
char
**
,
int
));
extern
unsigned
long
mystrtoul
PROTO
((
const
char
*
,
char
**
,
int
));
extern
double
atof
PROTO
((
const
char
*
));
extern
long
PyOS_strtol
Py_PROTO
((
const
char
*
,
char
**
,
int
));
extern
unsigned
long
PyOS_strtoul
Py_PROTO
((
const
char
*
,
char
**
,
int
));
extern
double
atof
Py_PROTO
((
const
char
*
));
char
*
end
;
long
x
;
double
dx
;
...
...
@@ -802,18 +808,18 @@ parsenumber(co, s)
imflag
=
*
end
==
'j'
||
*
end
==
'J'
;
#endif
if
(
*
end
==
'l'
||
*
end
==
'L'
)
return
long_scan
(
s
,
0
);
return
PyLong_FromString
(
s
,
(
char
**
)
0
,
0
);
if
(
s
[
0
]
==
'0'
)
x
=
(
long
)
my
strtoul
(
s
,
&
end
,
0
);
x
=
(
long
)
PyOS_
strtoul
(
s
,
&
end
,
0
);
else
x
=
my
strtol
(
s
,
&
end
,
0
);
x
=
PyOS_
strtol
(
s
,
&
end
,
0
);
if
(
*
end
==
'\0'
)
{
if
(
errno
!=
0
)
{
com_error
(
co
,
OverflowError
,
com_error
(
co
,
PyExc_
OverflowError
,
"integer literal too large"
);
return
NULL
;
}
return
newintobject
(
x
);
return
PyInt_FromLong
(
x
);
}
/* XXX Huge floats may silently fail */
#ifndef WITHOUT_COMPLEX
...
...
@@ -822,22 +828,22 @@ parsenumber(co, s)
PyFPE_START_PROTECT
(
"atof"
,
return
0
)
c
.
imag
=
atof
(
s
);
PyFPE_END_PROTECT
(
c
)
return
newcomplexobject
(
c
);
return
PyComplex_FromCComplex
(
c
);
}
else
{
#endif
PyFPE_START_PROTECT
(
"atof"
,
return
0
)
dx
=
atof
(
s
);
PyFPE_END_PROTECT
(
dx
)
return
newfloatobject
(
dx
);
return
PyFloat_FromDouble
(
dx
);
}
}
static
o
bject
*
static
PyO
bject
*
parsestr
(
s
)
char
*
s
;
{
o
bject
*
v
;
PyO
bject
*
v
;
int
len
;
char
*
buf
;
char
*
p
;
...
...
@@ -848,27 +854,27 @@ parsestr(s)
if
(
isalpha
(
quote
)
||
quote
==
'_'
)
quote
=
*++
s
;
if
(
quote
!=
'\''
&&
quote
!=
'\"'
)
{
err_badc
all
();
PyErr_BadInternalC
all
();
return
NULL
;
}
s
++
;
len
=
strlen
(
s
);
if
(
s
[
--
len
]
!=
quote
)
{
err_badc
all
();
PyErr_BadInternalC
all
();
return
NULL
;
}
if
(
len
>=
4
&&
s
[
0
]
==
quote
&&
s
[
1
]
==
quote
)
{
s
+=
2
;
len
-=
2
;
if
(
s
[
--
len
]
!=
quote
||
s
[
--
len
]
!=
quote
)
{
err_badc
all
();
PyErr_BadInternalC
all
();
return
NULL
;
}
}
if
(
first
!=
quote
||
strchr
(
s
,
'\\'
)
==
NULL
)
return
newsizedstringobject
(
s
,
len
);
v
=
newsizedstringobject
((
char
*
)
NULL
,
len
);
p
=
buf
=
getstringvalue
(
v
);
return
PyString_FromStringAndSize
(
s
,
len
);
v
=
PyString_FromStringAndSize
((
char
*
)
NULL
,
len
);
p
=
buf
=
PyString_AsString
(
v
);
end
=
s
+
len
;
while
(
s
<
end
)
{
if
(
*
s
!=
'\\'
)
{
...
...
@@ -912,21 +918,21 @@ parsestr(s)
default:
*
p
++
=
'\\'
;
*
p
++
=
s
[
-
1
];
break
;
}
}
resizestring
(
&
v
,
(
int
)(
p
-
buf
));
_PyString_Resize
(
&
v
,
(
int
)(
p
-
buf
));
return
v
;
}
static
o
bject
*
static
PyO
bject
*
parsestrplus
(
n
)
node
*
n
;
{
o
bject
*
v
;
PyO
bject
*
v
;
int
i
;
REQ
(
CHILD
(
n
,
0
),
STRING
);
if
((
v
=
parsestr
(
STR
(
CHILD
(
n
,
0
))))
!=
NULL
)
{
/* String literal concatenation */
for
(
i
=
1
;
i
<
NCH
(
n
)
&&
v
!=
NULL
;
i
++
)
{
joinstring_decref
(
&
v
,
parsestr
(
STR
(
CHILD
(
n
,
i
))));
PyString_ConcatAndDel
(
&
v
,
parsestr
(
STR
(
CHILD
(
n
,
i
))));
}
}
return
v
;
...
...
@@ -975,7 +981,7 @@ com_atom(c, n)
node
*
n
;
{
node
*
ch
;
o
bject
*
v
;
PyO
bject
*
v
;
int
i
;
REQ
(
n
,
atom
);
ch
=
CHILD
(
n
,
0
);
...
...
@@ -1012,7 +1018,7 @@ com_atom(c, n)
}
else
{
i
=
com_addconst
(
c
,
v
);
DECREF
(
v
);
Py_
DECREF
(
v
);
}
com_addoparg
(
c
,
LOAD_CONST
,
i
);
com_push
(
c
,
1
);
...
...
@@ -1025,7 +1031,7 @@ com_atom(c, n)
}
else
{
i
=
com_addconst
(
c
,
v
);
DECREF
(
v
);
Py_
DECREF
(
v
);
}
com_addoparg
(
c
,
LOAD_CONST
,
i
);
com_push
(
c
,
1
);
...
...
@@ -1036,7 +1042,8 @@ com_atom(c, n)
break
;
default:
/* XXX fprintf(stderr, "node type %d\n", TYPE(ch)); */
com_error
(
c
,
SystemError
,
"com_atom: unexpected node type"
);
com_error
(
c
,
PyExc_SystemError
,
"com_atom: unexpected node type"
);
}
}
...
...
@@ -1072,13 +1079,13 @@ static void
com_argument
(
c
,
n
,
pkeywords
)
struct
compiling
*
c
;
node
*
n
;
/* argument */
o
bject
**
pkeywords
;
PyO
bject
**
pkeywords
;
{
node
*
m
;
REQ
(
n
,
argument
);
/* [test '='] test; really [keyword '='] test */
if
(
NCH
(
n
)
==
1
)
{
if
(
*
pkeywords
!=
NULL
)
{
com_error
(
c
,
SyntaxError
,
com_error
(
c
,
PyExc_
SyntaxError
,
"non-keyword arg after keyword arg"
);
}
else
{
...
...
@@ -1091,24 +1098,25 @@ com_argument(c, n, pkeywords)
m
=
CHILD
(
m
,
0
);
}
while
(
NCH
(
m
)
==
1
);
if
(
TYPE
(
m
)
!=
NAME
)
{
com_error
(
c
,
SyntaxError
,
"keyword can't be an expression"
);
com_error
(
c
,
PyExc_SyntaxError
,
"keyword can't be an expression"
);
}
else
{
o
bject
*
v
=
PyString_InternFromString
(
STR
(
m
));
PyO
bject
*
v
=
PyString_InternFromString
(
STR
(
m
));
if
(
v
!=
NULL
&&
*
pkeywords
==
NULL
)
*
pkeywords
=
newdictobject
();
*
pkeywords
=
PyDict_New
();
if
(
v
==
NULL
||
*
pkeywords
==
NULL
)
c
->
c_errors
++
;
else
{
if
(
dict2lookup
(
*
pkeywords
,
v
)
!=
NULL
)
com_error
(
c
,
SyntaxError
,
if
(
PyDict_GetItem
(
*
pkeywords
,
v
)
!=
NULL
)
com_error
(
c
,
PyExc_
SyntaxError
,
"duplicate keyword argument"
);
else
if
(
dict2insert
(
*
pkeywords
,
v
,
v
)
!=
0
)
if
(
PyDict_SetItem
(
*
pkeywords
,
v
,
v
)
!=
0
)
c
->
c_errors
++
;
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
v
));
com_push
(
c
,
1
);
DECREF
(
v
);
Py_
DECREF
(
v
);
}
}
com_node
(
c
,
CHILD
(
n
,
2
));
...
...
@@ -1123,7 +1131,7 @@ com_call_function(c, n)
com_addoparg
(
c
,
CALL_FUNCTION
,
0
);
}
else
{
o
bject
*
keywords
=
NULL
;
PyO
bject
*
keywords
=
NULL
;
int
i
,
na
,
nk
;
REQ
(
n
,
arglist
);
na
=
0
;
...
...
@@ -1135,9 +1143,10 @@ com_call_function(c, n)
else
nk
++
;
}
XDECREF
(
keywords
);
Py_
XDECREF
(
keywords
);
if
(
na
>
255
||
nk
>
255
)
{
com_error
(
c
,
SyntaxError
,
"more than 255 arguments"
);
com_error
(
c
,
PyExc_SyntaxError
,
"more than 255 arguments"
);
}
com_addoparg
(
c
,
CALL_FUNCTION
,
na
|
(
nk
<<
8
));
com_pop
(
c
,
na
+
2
*
nk
);
...
...
@@ -1163,7 +1172,7 @@ com_sliceobj(c, n)
/* first argument */
if
(
TYPE
(
CHILD
(
n
,
i
))
==
COLON
)
{
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
None
));
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
Py_
None
));
com_push
(
c
,
1
);
i
++
;
}
...
...
@@ -1179,7 +1188,7 @@ com_sliceobj(c, n)
i
++
;
}
else
{
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
None
));
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
Py_
None
));
com_push
(
c
,
1
);
}
/* remaining arguments */
...
...
@@ -1189,7 +1198,7 @@ com_sliceobj(c, n)
REQ
(
ch
,
sliceop
);
if
(
NCH
(
ch
)
==
1
)
{
/* right argument of ':' missing */
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
None
));
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
Py_
None
));
com_push
(
c
,
1
);
}
else
...
...
@@ -1243,7 +1252,8 @@ com_subscriptlist(c, n, assigning)
if
(
assigning
==
OP_APPLY
)
op
=
SLICE
;
else
op
=
((
assigning
==
OP_ASSIGN
)
?
STORE_SLICE
:
DELETE_SLICE
);
op
=
((
assigning
==
OP_ASSIGN
)
?
STORE_SLICE
:
DELETE_SLICE
);
com_slice
(
c
,
sub
,
op
);
if
(
op
==
STORE_SLICE
)
com_pop
(
c
,
2
);
...
...
@@ -1294,7 +1304,7 @@ com_apply_trailer(c, n)
com_subscriptlist
(
c
,
CHILD
(
n
,
1
),
OP_APPLY
);
break
;
default:
com_error
(
c
,
SystemError
,
com_error
(
c
,
PyExc_
SystemError
,
"com_apply_trailer: unknown trailer type"
);
}
}
...
...
@@ -1364,7 +1374,7 @@ com_term(c, n)
op
=
BINARY_MODULO
;
break
;
default:
com_error
(
c
,
SystemError
,
com_error
(
c
,
PyExc_
SystemError
,
"com_term: operator not *, / or %"
);
op
=
255
;
}
...
...
@@ -1392,7 +1402,7 @@ com_arith_expr(c, n)
op
=
BINARY_SUBTRACT
;
break
;
default:
com_error
(
c
,
SystemError
,
com_error
(
c
,
PyExc_
SystemError
,
"com_arith_expr: operator not + or -"
);
op
=
255
;
}
...
...
@@ -1420,7 +1430,7 @@ com_shift_expr(c, n)
op
=
BINARY_RSHIFT
;
break
;
default:
com_error
(
c
,
SystemError
,
com_error
(
c
,
PyExc_
SystemError
,
"com_shift_expr: operator not << or >>"
);
op
=
255
;
}
...
...
@@ -1444,7 +1454,7 @@ com_and_expr(c, n)
op
=
BINARY_AND
;
}
else
{
com_error
(
c
,
SystemError
,
com_error
(
c
,
PyExc_
SystemError
,
"com_and_expr: operator not &"
);
op
=
255
;
}
...
...
@@ -1468,7 +1478,7 @@ com_xor_expr(c, n)
op
=
BINARY_XOR
;
}
else
{
com_error
(
c
,
SystemError
,
com_error
(
c
,
PyExc_
SystemError
,
"com_xor_expr: operator not ^"
);
op
=
255
;
}
...
...
@@ -1492,7 +1502,7 @@ com_expr(c, n)
op
=
BINARY_OR
;
}
else
{
com_error
(
c
,
SystemError
,
com_error
(
c
,
PyExc_
SystemError
,
"com_expr: expr operator not |"
);
op
=
255
;
}
...
...
@@ -1590,7 +1600,7 @@ com_comparison(c, n)
}
op
=
cmp_type
(
CHILD
(
n
,
i
-
1
));
if
(
op
==
BAD
)
{
com_error
(
c
,
SystemError
,
com_error
(
c
,
PyExc_
SystemError
,
"com_comparison: unknown comparison op"
);
}
com_addoparg
(
c
,
COMPARE_OP
,
op
);
...
...
@@ -1656,17 +1666,17 @@ com_test(c, n)
{
REQ
(
n
,
test
);
/* and_test ('or' and_test)* | lambdef */
if
(
NCH
(
n
)
==
1
&&
TYPE
(
CHILD
(
n
,
0
))
==
lambdef
)
{
o
bject
*
v
;
PyO
bject
*
v
;
int
i
;
int
ndefs
=
com_argdefs
(
c
,
CHILD
(
n
,
0
));
v
=
(
o
bject
*
)
icompile
(
CHILD
(
n
,
0
),
c
);
v
=
(
PyO
bject
*
)
icompile
(
CHILD
(
n
,
0
),
c
);
if
(
v
==
NULL
)
{
c
->
c_errors
++
;
i
=
255
;
}
else
{
i
=
com_addconst
(
c
,
v
);
DECREF
(
v
);
Py_
DECREF
(
v
);
}
com_addoparg
(
c
,
LOAD_CONST
,
i
);
com_push
(
c
,
1
);
...
...
@@ -1713,8 +1723,8 @@ com_list(c, n, toplevel)
/* Begin of assignment compilation */
static
void
com_assign_name
PROTO
((
struct
compiling
*
,
node
*
,
int
));
static
void
com_assign
PROTO
((
struct
compiling
*
,
node
*
,
int
));
static
void
com_assign_name
P
y_P
ROTO
((
struct
compiling
*
,
node
*
,
int
));
static
void
com_assign
P
y_P
ROTO
((
struct
compiling
*
,
node
*
,
int
));
static
void
com_assign_attr
(
c
,
n
,
assigning
)
...
...
@@ -1735,7 +1745,8 @@ com_assign_trailer(c, n, assigning)
REQ
(
n
,
trailer
);
switch
(
TYPE
(
CHILD
(
n
,
0
)))
{
case
LPAR
:
/* '(' [exprlist] ')' */
com_error
(
c
,
SyntaxError
,
"can't assign to function call"
);
com_error
(
c
,
PyExc_SyntaxError
,
"can't assign to function call"
);
break
;
case
DOT
:
/* '.' NAME */
com_assign_attr
(
c
,
CHILD
(
n
,
1
),
assigning
);
...
...
@@ -1744,7 +1755,7 @@ com_assign_trailer(c, n, assigning)
com_subscriptlist
(
c
,
CHILD
(
n
,
1
),
assigning
);
break
;
default:
com_error
(
c
,
SystemError
,
"unknown trailer type"
);
com_error
(
c
,
PyExc_
SystemError
,
"unknown trailer type"
);
}
}
...
...
@@ -1825,7 +1836,7 @@ com_assign(c, n, assigning)
case
term
:
case
factor
:
if
(
NCH
(
n
)
>
1
)
{
com_error
(
c
,
SyntaxError
,
com_error
(
c
,
PyExc_
SyntaxError
,
"can't assign to operator"
);
return
;
}
...
...
@@ -1835,7 +1846,7 @@ com_assign(c, n, assigning)
case
power
:
/* atom trailer* ('**' power)* */
/* ('+'|'-'|'~') factor | atom trailer* */
if
(
TYPE
(
CHILD
(
n
,
0
))
!=
atom
)
{
com_error
(
c
,
SyntaxError
,
com_error
(
c
,
PyExc_
SyntaxError
,
"can't assign to operator"
);
return
;
}
...
...
@@ -1844,7 +1855,7 @@ com_assign(c, n, assigning)
com_node
(
c
,
CHILD
(
n
,
0
));
for
(
i
=
1
;
i
+
1
<
NCH
(
n
);
i
++
)
{
if
(
TYPE
(
CHILD
(
n
,
i
))
==
DOUBLESTAR
)
{
com_error
(
c
,
SyntaxError
,
com_error
(
c
,
PyExc_
SyntaxError
,
"can't assign to operator"
);
return
;
}
...
...
@@ -1863,7 +1874,7 @@ com_assign(c, n, assigning)
n
=
CHILD
(
n
,
1
);
if
(
TYPE
(
n
)
==
RPAR
)
{
/* XXX Should allow () = () ??? */
com_error
(
c
,
SyntaxError
,
com_error
(
c
,
PyExc_
SyntaxError
,
"can't assign to ()"
);
return
;
}
...
...
@@ -1871,7 +1882,7 @@ com_assign(c, n, assigning)
case
LSQB
:
n
=
CHILD
(
n
,
1
);
if
(
TYPE
(
n
)
==
RSQB
)
{
com_error
(
c
,
SyntaxError
,
com_error
(
c
,
PyExc_
SyntaxError
,
"can't assign to []"
);
return
;
}
...
...
@@ -1881,26 +1892,28 @@ com_assign(c, n, assigning)
com_assign_name
(
c
,
CHILD
(
n
,
0
),
assigning
);
return
;
default:
com_error
(
c
,
SyntaxError
,
com_error
(
c
,
PyExc_
SyntaxError
,
"can't assign to literal"
);
return
;
}
break
;
case
lambdef
:
com_error
(
c
,
SyntaxError
,
"can't assign to lambda"
);
com_error
(
c
,
PyExc_SyntaxError
,
"can't assign to lambda"
);
return
;
default:
/* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
com_error
(
c
,
SystemError
,
"com_assign: bad node"
);
com_error
(
c
,
PyExc_SystemError
,
"com_assign: bad node"
);
return
;
}
}
}
/* Forward */
static
node
*
get_rawdocstring
PROTO
((
node
*
));
/* Forward */
static
node
*
get_rawdocstring
P
y_P
ROTO
((
node
*
));
static
void
com_expr_stmt
(
c
,
n
)
...
...
@@ -1997,10 +2010,10 @@ com_return_stmt(c, n)
{
REQ
(
n
,
return_stmt
);
/* 'return' [testlist] */
if
(
!
c
->
c_infunction
)
{
com_error
(
c
,
SyntaxError
,
"'return' outside function"
);
com_error
(
c
,
PyExc_
SyntaxError
,
"'return' outside function"
);
}
if
(
NCH
(
n
)
<
2
)
{
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
None
));
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
Py_
None
));
com_push
(
c
,
1
);
}
else
...
...
@@ -2075,10 +2088,11 @@ com_global_stmt(c, n)
com_mangle
(
c
,
s
,
buffer
,
(
int
)
sizeof
(
buffer
)))
s
=
buffer
;
#endif
if
(
dictlookup
(
c
->
c_locals
,
s
)
!=
NULL
)
{
com_error
(
c
,
SyntaxError
,
"name is local and global"
);
if
(
PyDict_GetItemString
(
c
->
c_locals
,
s
)
!=
NULL
)
{
com_error
(
c
,
PyExc_SyntaxError
,
"name is local and global"
);
}
else
if
(
dictinsert
(
c
->
c_globals
,
s
,
None
)
!=
0
)
else
if
(
PyDict_SetItemString
(
c
->
c_globals
,
s
,
Py_
None
)
!=
0
)
c
->
c_errors
++
;
}
}
...
...
@@ -2086,36 +2100,37 @@ com_global_stmt(c, n)
static
int
com_newlocal_o
(
c
,
nameval
)
struct
compiling
*
c
;
o
bject
*
nameval
;
PyO
bject
*
nameval
;
{
int
i
;
o
bject
*
ival
;
if
(
getlists
ize
(
c
->
c_varnames
)
!=
c
->
c_nlocals
)
{
PyO
bject
*
ival
;
if
(
PyList_S
ize
(
c
->
c_varnames
)
!=
c
->
c_nlocals
)
{
/* This is usually caused by an error on a previous call */
if
(
c
->
c_errors
==
0
)
{
com_error
(
c
,
SystemError
,
"mixed up var name/index"
);
com_error
(
c
,
PyExc_SystemError
,
"mixed up var name/index"
);
}
return
0
;
}
ival
=
newintobject
(
i
=
c
->
c_nlocals
++
);
ival
=
PyInt_FromLong
(
i
=
c
->
c_nlocals
++
);
if
(
ival
==
NULL
)
c
->
c_errors
++
;
else
if
(
mappinginsert
(
c
->
c_locals
,
nameval
,
ival
)
!=
0
)
else
if
(
PyDict_SetItem
(
c
->
c_locals
,
nameval
,
ival
)
!=
0
)
c
->
c_errors
++
;
else
if
(
addlistitem
(
c
->
c_varnames
,
nameval
)
!=
0
)
else
if
(
PyList_Append
(
c
->
c_varnames
,
nameval
)
!=
0
)
c
->
c_errors
++
;
XDECREF
(
ival
);
Py_
XDECREF
(
ival
);
return
i
;
}
static
int
com_addlocal_o
(
c
,
nameval
)
struct
compiling
*
c
;
o
bject
*
nameval
;
PyO
bject
*
nameval
;
{
object
*
ival
=
mappinglookup
(
c
->
c_locals
,
nameval
);
PyObject
*
ival
=
PyDict_GetItem
(
c
->
c_locals
,
nameval
);
if
(
ival
!=
NULL
)
return
getintvalue
(
ival
);
return
PyInt_AsLong
(
ival
);
return
com_newlocal_o
(
c
,
nameval
);
}
...
...
@@ -2124,14 +2139,14 @@ com_newlocal(c, name)
struct
compiling
*
c
;
char
*
name
;
{
o
bject
*
nameval
=
PyString_InternFromString
(
name
);
PyO
bject
*
nameval
=
PyString_InternFromString
(
name
);
int
i
;
if
(
nameval
==
NULL
)
{
c
->
c_errors
++
;
return
0
;
}
i
=
com_newlocal_o
(
c
,
nameval
);
DECREF
(
nameval
);
Py_
DECREF
(
nameval
);
return
i
;
}
...
...
@@ -2145,7 +2160,7 @@ com_access_stmt(c, n)
node
*
n
;
{
int
i
,
j
,
k
,
mode
,
imode
;
o
bject
*
vmode
;
PyO
bject
*
vmode
;
REQ
(
n
,
access_stmt
);
/* 'access' NAME (',' NAME)* ':' accesstype (',' accesstype)*
accesstype: NAME+ */
...
...
@@ -2187,9 +2202,9 @@ com_access_stmt(c, n)
if
(
w
==
1
)
mode
|=
AC_W_PRIVATE
;
}
}
vmode
=
newintobject
((
long
)
mode
);
vmode
=
PyInt_FromLong
((
long
)
mode
);
imode
=
com_addconst
(
c
,
vmode
);
XDECREF
(
vmode
);
Py_
XDECREF
(
vmode
);
for
(
i
=
1
;
TYPE
(
CHILD
(
n
,
i
-
1
))
!=
COLON
;
i
+=
2
)
{
com_addoparg
(
c
,
LOAD_CONST
,
imode
);
com_addopname
(
c
,
ACCESS_MODE
,
CHILD
(
n
,
i
));
...
...
@@ -2208,7 +2223,7 @@ com_exec_stmt(c, n)
if
(
NCH
(
n
)
>=
4
)
com_node
(
c
,
CHILD
(
n
,
3
));
else
{
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
None
));
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
Py_
None
));
com_push
(
c
,
1
);
}
if
(
NCH
(
n
)
>=
6
)
...
...
@@ -2226,7 +2241,7 @@ is_constant_false(c, n)
struct
compiling
*
c
;
node
*
n
;
{
o
bject
*
v
;
PyO
bject
*
v
;
int
i
;
/* Label to avoid tail recursion */
...
...
@@ -2284,21 +2299,21 @@ is_constant_false(c, n)
case
NUMBER
:
v
=
parsenumber
(
c
,
STR
(
n
));
if
(
v
==
NULL
)
{
err_c
lear
();
PyErr_C
lear
();
break
;
}
i
=
testbool
(
v
);
DECREF
(
v
);
i
=
PyObject_IsTrue
(
v
);
Py_
DECREF
(
v
);
return
i
==
0
;
case
STRING
:
v
=
parsestr
(
STR
(
n
));
if
(
v
==
NULL
)
{
err_c
lear
();
PyErr_C
lear
();
break
;
}
i
=
testbool
(
v
);
DECREF
(
v
);
i
=
PyObject_IsTrue
(
v
);
Py_
DECREF
(
v
);
return
i
==
0
;
}
...
...
@@ -2374,7 +2389,7 @@ com_for_stmt(c, n)
struct
compiling
*
c
;
node
*
n
;
{
o
bject
*
v
;
PyO
bject
*
v
;
int
break_anchor
=
0
;
int
anchor
=
0
;
int
save_begin
=
c
->
c_begin
;
...
...
@@ -2383,12 +2398,12 @@ com_for_stmt(c, n)
com_addfwref
(
c
,
SETUP_LOOP
,
&
break_anchor
);
block_push
(
c
,
SETUP_LOOP
);
com_node
(
c
,
CHILD
(
n
,
3
));
v
=
newintobject
(
0L
);
v
=
PyInt_FromLong
(
0L
);
if
(
v
==
NULL
)
c
->
c_errors
++
;
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
v
));
com_push
(
c
,
1
);
XDECREF
(
v
);
Py_
XDECREF
(
v
);
c
->
c_begin
=
c
->
c_nexti
;
com_addoparg
(
c
,
SET_LINENO
,
n
->
n_lineno
);
com_addfwref
(
c
,
FOR_LOOP
,
&
anchor
);
...
...
@@ -2497,7 +2512,7 @@ com_try_except(c, n)
i
+=
3
)
{
/* except_clause: 'except' [expr [',' var]] */
if
(
except_anchor
==
0
)
{
com_error
(
c
,
SyntaxError
,
com_error
(
c
,
PyExc_
SyntaxError
,
"default 'except:' must be last"
);
break
;
}
...
...
@@ -2559,7 +2574,7 @@ com_try_finally(c, n)
com_addbyte
(
c
,
POP_BLOCK
);
block_pop
(
c
,
SETUP_FINALLY
);
block_push
(
c
,
END_FINALLY
);
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
None
));
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
Py_
None
));
/* While the generated code pushes only one item,
the try-finally handling can enter here with
up to three items. OK, here are the details:
...
...
@@ -2649,7 +2664,7 @@ get_rawdocstring(n)
return
NULL
;
}
static
o
bject
*
static
PyO
bject
*
get_docstring
(
n
)
node
*
n
;
{
...
...
@@ -2690,7 +2705,8 @@ com_continue_stmt(c, n)
com_addoparg
(
c
,
JUMP_ABSOLUTE
,
c
->
c_begin
);
}
else
{
com_error
(
c
,
SyntaxError
,
"'continue' not properly in loop"
);
com_error
(
c
,
PyExc_SyntaxError
,
"'continue' not properly in loop"
);
}
/* XXX Could allow it inside a 'finally' clause
XXX if we could pop the exception still on the stack */
...
...
@@ -2722,7 +2738,8 @@ com_argdefs(c, n)
ndefs
=
0
;
for
(
i
=
0
;
i
<
nch
;
i
++
)
{
int
t
;
if
(
TYPE
(
CHILD
(
n
,
i
))
==
STAR
||
TYPE
(
CHILD
(
n
,
i
))
==
DOUBLESTAR
)
if
(
TYPE
(
CHILD
(
n
,
i
))
==
STAR
||
TYPE
(
CHILD
(
n
,
i
))
==
DOUBLESTAR
)
break
;
nargs
++
;
i
++
;
...
...
@@ -2743,7 +2760,7 @@ com_argdefs(c, n)
/* Treat "(a=1, b)" as "(a=1, b=None)" */
if
(
ndefs
)
{
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
None
));
com_addconst
(
c
,
Py_
None
));
com_push
(
c
,
1
);
ndefs
++
;
}
...
...
@@ -2759,9 +2776,9 @@ com_funcdef(c, n)
struct
compiling
*
c
;
node
*
n
;
{
o
bject
*
v
;
PyO
bject
*
v
;
REQ
(
n
,
funcdef
);
/* funcdef: 'def' NAME parameters ':' suite */
v
=
(
o
bject
*
)
icompile
(
n
,
c
);
v
=
(
PyO
bject
*
)
icompile
(
n
,
c
);
if
(
v
==
NULL
)
c
->
c_errors
++
;
else
{
...
...
@@ -2773,7 +2790,7 @@ com_funcdef(c, n)
com_pop
(
c
,
ndefs
);
com_addopname
(
c
,
STORE_NAME
,
CHILD
(
n
,
1
));
com_pop
(
c
,
1
);
DECREF
(
v
);
Py_
DECREF
(
v
);
}
}
...
...
@@ -2798,7 +2815,7 @@ com_classdef(c, n)
node
*
n
;
{
int
i
;
o
bject
*
v
;
PyO
bject
*
v
;
REQ
(
n
,
classdef
);
/* classdef: class NAME ['(' testlist ')'] ':' suite */
if
((
v
=
PyString_InternFromString
(
STR
(
CHILD
(
n
,
1
))))
==
NULL
)
{
...
...
@@ -2809,7 +2826,7 @@ com_classdef(c, n)
i
=
com_addconst
(
c
,
v
);
com_addoparg
(
c
,
LOAD_CONST
,
i
);
com_push
(
c
,
1
);
DECREF
(
v
);
Py_
DECREF
(
v
);
/* Push the tuple of base classes on the stack */
if
(
TYPE
(
CHILD
(
n
,
2
))
!=
LPAR
)
{
com_addoparg
(
c
,
BUILD_TUPLE
,
0
);
...
...
@@ -2817,7 +2834,7 @@ com_classdef(c, n)
}
else
com_bases
(
c
,
CHILD
(
n
,
3
));
v
=
(
o
bject
*
)
icompile
(
n
,
c
);
v
=
(
PyO
bject
*
)
icompile
(
n
,
c
);
if
(
v
==
NULL
)
c
->
c_errors
++
;
else
{
...
...
@@ -2829,7 +2846,7 @@ com_classdef(c, n)
com_addbyte
(
c
,
BUILD_CLASS
);
com_pop
(
c
,
2
);
com_addopname
(
c
,
STORE_NAME
,
CHILD
(
n
,
1
));
DECREF
(
v
);
Py_
DECREF
(
v
);
}
}
...
...
@@ -2887,7 +2904,8 @@ com_node(c, n)
break
;
case
break_stmt
:
if
(
c
->
c_loops
==
0
)
{
com_error
(
c
,
SyntaxError
,
"'break' outside loop"
);
com_error
(
c
,
PyExc_SyntaxError
,
"'break' outside loop"
);
}
com_addbyte
(
c
,
BREAK_LOOP
);
break
;
...
...
@@ -2983,11 +3001,12 @@ com_node(c, n)
default:
/* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
com_error
(
c
,
SystemError
,
"com_node: unexpected node type"
);
com_error
(
c
,
PyExc_SystemError
,
"com_node: unexpected node type"
);
}
}
static
void
com_fplist
PROTO
((
struct
compiling
*
,
node
*
));
static
void
com_fplist
P
y_P
ROTO
((
struct
compiling
*
,
node
*
));
static
void
com_fpdef
(
c
,
n
)
...
...
@@ -3121,12 +3140,12 @@ com_file_input(c, n)
node
*
n
;
{
int
i
;
o
bject
*
doc
;
PyO
bject
*
doc
;
REQ
(
n
,
file_input
);
/* (NEWLINE | stmt)* ENDMARKER */
doc
=
get_docstring
(
n
);
if
(
doc
!=
NULL
)
{
int
i
=
com_addconst
(
c
,
doc
);
DECREF
(
doc
);
Py_
DECREF
(
doc
);
com_addoparg
(
c
,
LOAD_CONST
,
i
);
com_push
(
c
,
1
);
com_addopnamestr
(
c
,
STORE_NAME
,
"__doc__"
);
...
...
@@ -3146,17 +3165,17 @@ compile_funcdef(c, n)
struct
compiling
*
c
;
node
*
n
;
{
o
bject
*
doc
;
PyO
bject
*
doc
;
node
*
ch
;
REQ
(
n
,
funcdef
);
/* funcdef: 'def' NAME parameters ':' suite */
c
->
c_name
=
STR
(
CHILD
(
n
,
1
));
doc
=
get_docstring
(
CHILD
(
n
,
4
));
if
(
doc
!=
NULL
)
{
(
void
)
com_addconst
(
c
,
doc
);
DECREF
(
doc
);
Py_
DECREF
(
doc
);
}
else
(
void
)
com_addconst
(
c
,
None
);
/* No docstring */
(
void
)
com_addconst
(
c
,
Py_
None
);
/* No docstring */
ch
=
CHILD
(
n
,
2
);
/* parameters: '(' [varargslist] ')' */
ch
=
CHILD
(
ch
,
1
);
/* ')' | varargslist */
if
(
TYPE
(
ch
)
==
varargslist
)
...
...
@@ -3164,7 +3183,7 @@ compile_funcdef(c, n)
c
->
c_infunction
=
1
;
com_node
(
c
,
CHILD
(
n
,
4
));
c
->
c_infunction
=
0
;
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
None
));
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
Py_
None
));
com_push
(
c
,
1
);
com_addbyte
(
c
,
RETURN_VALUE
);
com_pop
(
c
,
1
);
...
...
@@ -3180,7 +3199,7 @@ compile_lambdef(c, n)
c
->
c_name
=
"<lambda>"
;
ch
=
CHILD
(
n
,
1
);
(
void
)
com_addconst
(
c
,
None
);
/* No docstring */
(
void
)
com_addconst
(
c
,
Py_
None
);
/* No docstring */
if
(
TYPE
(
ch
)
==
varargslist
)
{
com_arglist
(
c
,
ch
);
ch
=
CHILD
(
n
,
3
);
...
...
@@ -3198,7 +3217,7 @@ compile_classdef(c, n)
node
*
n
;
{
node
*
ch
;
o
bject
*
doc
;
PyO
bject
*
doc
;
REQ
(
n
,
classdef
);
/* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
c
->
c_name
=
STR
(
CHILD
(
n
,
1
));
...
...
@@ -3209,14 +3228,14 @@ compile_classdef(c, n)
doc
=
get_docstring
(
ch
);
if
(
doc
!=
NULL
)
{
int
i
=
com_addconst
(
c
,
doc
);
DECREF
(
doc
);
Py_
DECREF
(
doc
);
com_addoparg
(
c
,
LOAD_CONST
,
i
);
com_push
(
c
,
1
);
com_addopnamestr
(
c
,
STORE_NAME
,
"__doc__"
);
com_pop
(
c
,
1
);
}
else
(
void
)
com_addconst
(
c
,
None
);
(
void
)
com_addconst
(
c
,
Py_
None
);
com_node
(
c
,
ch
);
com_addbyte
(
c
,
LOAD_LOCALS
);
com_push
(
c
,
1
);
...
...
@@ -3239,7 +3258,7 @@ compile_node(c, n)
n
=
CHILD
(
n
,
0
);
if
(
TYPE
(
n
)
!=
NEWLINE
)
com_node
(
c
,
n
);
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
None
));
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
Py_
None
));
com_push
(
c
,
1
);
com_addbyte
(
c
,
RETURN_VALUE
);
com_pop
(
c
,
1
);
...
...
@@ -3248,7 +3267,7 @@ compile_node(c, n)
case
file_input
:
/* A whole file, or built-in function exec() */
com_file_input
(
c
,
n
);
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
None
));
com_addoparg
(
c
,
LOAD_CONST
,
com_addconst
(
c
,
Py_
None
));
com_push
(
c
,
1
);
com_addbyte
(
c
,
RETURN_VALUE
);
com_pop
(
c
,
1
);
...
...
@@ -3274,7 +3293,7 @@ compile_node(c, n)
default:
/* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
com_error
(
c
,
SystemError
,
com_error
(
c
,
PyExc_
SystemError
,
"compile_node: unexpected node type"
);
}
}
...
...
@@ -3310,19 +3329,19 @@ optimize(c)
unsigned
char
*
next_instr
,
*
cur_instr
;
int
opcode
;
int
oparg
=
0
;
o
bject
*
name
;
o
bject
*
error_type
,
*
error_value
,
*
error_traceback
;
PyO
bject
*
name
;
PyO
bject
*
error_type
,
*
error_value
,
*
error_traceback
;
#define NEXTOP() (*next_instr++)
#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
#define GETITEM(v, i) (
getlisti
tem((v), (i)))
#define GETITEM(v, i) (
PyList_GetI
tem((v), (i)))
#define GETNAMEOBJ(i) (GETITEM(c->c_names, (i)))
err_f
etch
(
&
error_type
,
&
error_value
,
&
error_traceback
);
PyErr_F
etch
(
&
error_type
,
&
error_value
,
&
error_traceback
);
c
->
c_flags
|=
CO_OPTIMIZED
;
next_instr
=
(
unsigned
char
*
)
getstringvalue
(
c
->
c_code
);
next_instr
=
(
unsigned
char
*
)
PyString_AsString
(
c
->
c_code
);
for
(;;)
{
opcode
=
NEXTOP
();
if
(
opcode
==
STOP_CODE
)
...
...
@@ -3341,10 +3360,10 @@ optimize(c)
}
}
if
(
dictlookup
(
c
->
c_locals
,
"*"
)
!=
NULL
)
if
(
PyDict_GetItemString
(
c
->
c_locals
,
"*"
)
!=
NULL
)
c
->
c_flags
&=
~
CO_OPTIMIZED
;
next_instr
=
(
unsigned
char
*
)
getstringvalue
(
c
->
c_code
);
next_instr
=
(
unsigned
char
*
)
PyString_AsString
(
c
->
c_code
);
for
(;;)
{
cur_instr
=
next_instr
;
opcode
=
NEXTOP
();
...
...
@@ -3355,18 +3374,18 @@ optimize(c)
if
(
opcode
==
LOAD_NAME
||
opcode
==
STORE_NAME
||
opcode
==
DELETE_NAME
)
{
o
bject
*
v
;
PyO
bject
*
v
;
int
i
;
name
=
GETNAMEOBJ
(
oparg
);
v
=
dict2lookup
(
c
->
c_locals
,
name
);
v
=
PyDict_GetItem
(
c
->
c_locals
,
name
);
if
(
v
==
NULL
)
{
err_c
lear
();
PyErr_C
lear
();
if
(
opcode
==
LOAD_NAME
&&
(
c
->
c_flags
&
CO_OPTIMIZED
))
cur_instr
[
0
]
=
LOAD_GLOBAL
;
continue
;
}
i
=
getintvalue
(
v
);
i
=
PyInt_AsLong
(
v
);
switch
(
opcode
)
{
case
LOAD_NAME
:
cur_instr
[
0
]
=
LOAD_FAST
;
break
;
case
STORE_NAME
:
cur_instr
[
0
]
=
STORE_FAST
;
break
;
...
...
@@ -3378,18 +3397,18 @@ optimize(c)
}
if
(
c
->
c_errors
==
0
)
err_r
estore
(
error_type
,
error_value
,
error_traceback
);
PyErr_R
estore
(
error_type
,
error_value
,
error_traceback
);
}
codeo
bject
*
c
ompile
(
n
,
filename
)
PyCodeO
bject
*
PyNode_C
ompile
(
n
,
filename
)
node
*
n
;
char
*
filename
;
{
return
jcompile
(
n
,
filename
,
NULL
);
}
static
codeo
bject
*
static
PyCodeO
bject
*
icompile
(
n
,
base
)
node
*
n
;
struct
compiling
*
base
;
...
...
@@ -3397,14 +3416,14 @@ icompile(n, base)
return
jcompile
(
n
,
base
->
c_filename
,
base
);
}
static
codeo
bject
*
static
PyCodeO
bject
*
jcompile
(
n
,
filename
,
base
)
node
*
n
;
char
*
filename
;
struct
compiling
*
base
;
{
struct
compiling
sc
;
codeo
bject
*
co
;
PyCodeO
bject
*
co
;
if
(
!
com_init
(
&
sc
,
filename
))
return
NULL
;
#ifdef PRIVATE_NAME_MANGLING
...
...
@@ -3423,14 +3442,14 @@ jcompile(n, filename, base)
sc
.
c_flags
|=
CO_NEWLOCALS
;
co
=
NULL
;
if
(
sc
.
c_errors
==
0
)
{
o
bject
*
consts
,
*
names
,
*
varnames
,
*
filename
,
*
name
;
consts
=
listt
uple
(
sc
.
c_consts
);
names
=
listt
uple
(
sc
.
c_names
);
varnames
=
listt
uple
(
sc
.
c_varnames
);
PyO
bject
*
consts
,
*
names
,
*
varnames
,
*
filename
,
*
name
;
consts
=
PyList_AsT
uple
(
sc
.
c_consts
);
names
=
PyList_AsT
uple
(
sc
.
c_names
);
varnames
=
PyList_AsT
uple
(
sc
.
c_varnames
);
filename
=
PyString_InternFromString
(
sc
.
c_filename
);
name
=
PyString_InternFromString
(
sc
.
c_name
);
if
(
!
err_o
ccurred
())
co
=
newcodeobject
(
sc
.
c_argcount
,
if
(
!
PyErr_O
ccurred
())
co
=
PyCode_New
(
sc
.
c_argcount
,
sc
.
c_nlocals
,
sc
.
c_maxstacklevel
,
sc
.
c_flags
,
...
...
@@ -3442,11 +3461,11 @@ jcompile(n, filename, base)
name
,
sc
.
c_firstlineno
,
sc
.
c_lnotab
);
XDECREF
(
consts
);
XDECREF
(
names
);
XDECREF
(
varnames
);
XDECREF
(
filename
);
XDECREF
(
name
);
Py_
XDECREF
(
consts
);
Py_
XDECREF
(
names
);
Py_
XDECREF
(
varnames
);
Py_
XDECREF
(
filename
);
Py_
XDECREF
(
name
);
}
com_free
(
&
sc
);
return
co
;
...
...
Python/getargs.c
View file @
5574f2d0
...
...
@@ -36,37 +36,38 @@ PERFORMANCE OF THIS SOFTWARE.
XXX Python source (or in an extension) uses ridiculously long names
XXX or riduculously deep nesting in format strings. */
#include "
allobjects
.h"
#include "
Python
.h"
#include <ctype.h>
int
getargs
PROTO
((
o
bject
*
,
char
*
,
...));
int
newgetargs
PROTO
((
o
bject
*
,
char
*
,
...));
int
vgetargs
PROTO
((
o
bject
*
,
char
*
,
va_list
));
int
PyArg_Parse
Py_PROTO
((
PyO
bject
*
,
char
*
,
...));
int
PyArg_ParseTuple
Py_PROTO
((
PyO
bject
*
,
char
*
,
...));
int
PyArgs_VaParse
Py_PROTO
((
PyO
bject
*
,
char
*
,
va_list
));
int
PyArg_ParseTupleAndKeywords
P
ROTO
((
object
*
,
o
bject
*
,
int
PyArg_ParseTupleAndKeywords
P
y_PROTO
((
PyObject
*
,
PyO
bject
*
,
char
*
,
char
**
,
...));
/* Forward */
static
int
vgetargs1
PROTO
((
object
*
,
char
*
,
va_list
*
,
int
));
static
void
seterror
PROTO
((
int
,
char
*
,
int
*
,
char
*
,
char
*
));
static
char
*
convertitem
PROTO
((
object
*
,
char
**
,
va_list
*
,
int
*
,
char
*
));
static
char
*
converttuple
PROTO
((
object
*
,
char
**
,
va_list
*
,
static
int
vgetargs1
Py_PROTO
((
PyObject
*
,
char
*
,
va_list
*
,
int
));
static
void
seterror
Py_PROTO
((
int
,
char
*
,
int
*
,
char
*
,
char
*
));
static
char
*
convertitem
Py_PROTO
((
PyObject
*
,
char
**
,
va_list
*
,
int
*
,
char
*
));
static
char
*
converttuple
Py_PROTO
((
PyObject
*
,
char
**
,
va_list
*
,
int
*
,
char
*
,
int
));
static
char
*
convertsimple
P
ROTO
((
o
bject
*
,
char
**
,
va_list
*
,
char
*
));
static
char
*
convertsimple1
P
ROTO
((
o
bject
*
,
char
**
,
va_list
*
));
static
char
*
convertsimple
P
y_PROTO
((
PyO
bject
*
,
char
**
,
va_list
*
,
char
*
));
static
char
*
convertsimple1
P
y_PROTO
((
PyO
bject
*
,
char
**
,
va_list
*
));
static
int
vgetargskeywords
P
ROTO
((
object
*
,
o
bject
*
,
static
int
vgetargskeywords
P
y_PROTO
((
PyObject
*
,
PyO
bject
*
,
char
*
,
char
**
,
va_list
*
));
static
char
*
skipitem
PROTO
((
char
**
,
va_list
*
));
static
char
*
skipitem
P
y_P
ROTO
((
char
**
,
va_list
*
));
#ifdef HAVE_STDARG_PROTOTYPES
/* VARARGS2 */
int
getargs
(
o
bject
*
args
,
char
*
format
,
...)
int
PyArg_Parse
(
PyO
bject
*
args
,
char
*
format
,
...)
#else
/* VARARGS */
int
getargs
(
va_alist
)
va_dcl
int
PyArg_Parse
(
va_alist
)
va_dcl
#endif
{
int
retval
;
...
...
@@ -75,11 +76,11 @@ int getargs(va_alist) va_dcl
va_start
(
va
,
format
);
#else
o
bject
*
args
;
PyO
bject
*
args
;
char
*
format
;
va_start
(
va
);
args
=
va_arg
(
va
,
o
bject
*
);
args
=
va_arg
(
va
,
PyO
bject
*
);
format
=
va_arg
(
va
,
char
*
);
#endif
retval
=
vgetargs1
(
args
,
format
,
&
va
,
1
);
...
...
@@ -90,10 +91,10 @@ int getargs(va_alist) va_dcl
#ifdef HAVE_STDARG_PROTOTYPES
/* VARARGS2 */
int
newgetargs
(
o
bject
*
args
,
char
*
format
,
...)
int
PyArg_ParseTuple
(
PyO
bject
*
args
,
char
*
format
,
...)
#else
/* VARARGS */
int
newgetargs
(
va_alist
)
va_dcl
int
PyArg_ParseTuple
(
va_alist
)
va_dcl
#endif
{
int
retval
;
...
...
@@ -102,11 +103,11 @@ int newgetargs(va_alist) va_dcl
va_start
(
va
,
format
);
#else
o
bject
*
args
;
PyO
bject
*
args
;
char
*
format
;
va_start
(
va
);
args
=
va_arg
(
va
,
o
bject
*
);
args
=
va_arg
(
va
,
PyO
bject
*
);
format
=
va_arg
(
va
,
char
*
);
#endif
retval
=
vgetargs1
(
args
,
format
,
&
va
,
0
);
...
...
@@ -116,8 +117,8 @@ int newgetargs(va_alist) va_dcl
int
vgetargs
(
args
,
format
,
va
)
o
bject
*
args
;
PyArgs_VaParse
(
args
,
format
,
va
)
PyO
bject
*
args
;
char
*
format
;
va_list
va
;
{
...
...
@@ -135,7 +136,7 @@ vgetargs(args, format, va)
static
int
vgetargs1
(
args
,
format
,
p_va
,
compat
)
o
bject
*
args
;
PyO
bject
*
args
;
char
*
format
;
va_list
*
p_va
;
int
compat
;
...
...
@@ -160,7 +161,7 @@ vgetargs1(args, format, p_va, compat)
}
else
if
(
/* '(' */
c
==
')'
)
{
if
(
level
==
0
)
fatal
(
/* '(' */
Py_FatalError
(
/* '(' */
"excess ')' in getargs format"
);
else
level
--
;
...
...
@@ -184,7 +185,7 @@ vgetargs1(args, format, p_va, compat)
}
if
(
level
!=
0
)
fatal
(
/* '(' */
"missing ')' in getargs format"
);
Py_FatalError
(
/* '(' */
"missing ')' in getargs format"
);
if
(
min
<
0
)
min
=
max
;
...
...
@@ -197,7 +198,7 @@ vgetargs1(args, format, p_va, compat)
return
1
;
sprintf
(
msgbuf
,
"%s requires no arguments"
,
fname
==
NULL
?
"function"
:
fname
);
err_setstr
(
TypeError
,
msgbuf
);
PyErr_SetString
(
PyExc_
TypeError
,
msgbuf
);
return
0
;
}
else
if
(
min
==
1
&&
max
==
1
)
{
...
...
@@ -205,7 +206,7 @@ vgetargs1(args, format, p_va, compat)
sprintf
(
msgbuf
,
"%s requires at least one argument"
,
fname
==
NULL
?
"function"
:
fname
);
err_setstr
(
TypeError
,
msgbuf
);
PyErr_SetString
(
PyExc_
TypeError
,
msgbuf
);
return
0
;
}
msg
=
convertitem
(
args
,
&
format
,
p_va
,
levels
,
msgbuf
);
...
...
@@ -215,19 +216,19 @@ vgetargs1(args, format, p_va, compat)
return
0
;
}
else
{
err_setstr
(
SystemError
,
PyErr_SetString
(
PyExc_
SystemError
,
"old style getargs format uses new features"
);
return
0
;
}
}
if
(
!
is_tupleobject
(
args
))
{
err_setstr
(
SystemError
,
if
(
!
PyTuple_Check
(
args
))
{
PyErr_SetString
(
PyExc_
SystemError
,
"new style getargs format but argument is not a tuple"
);
return
0
;
}
len
=
gettuples
ize
(
args
);
len
=
PyTuple_S
ize
(
args
);
if
(
len
<
min
||
max
<
len
)
{
if
(
message
==
NULL
)
{
...
...
@@ -241,14 +242,14 @@ vgetargs1(args, format, p_va, compat)
len
);
message
=
msgbuf
;
}
err_setstr
(
TypeError
,
message
);
PyErr_SetString
(
PyExc_
TypeError
,
message
);
return
0
;
}
for
(
i
=
0
;
i
<
len
;
i
++
)
{
if
(
*
format
==
'|'
)
format
++
;
msg
=
convertitem
(
gettuplei
tem
(
args
,
i
),
&
format
,
p_va
,
msg
=
convertitem
(
PyTuple_GetI
tem
(
args
,
i
),
&
format
,
p_va
,
levels
,
msgbuf
);
if
(
msg
)
{
seterror
(
i
+
1
,
msg
,
levels
,
fname
,
message
);
...
...
@@ -273,7 +274,7 @@ seterror(iarg, msg, levels, fname, message)
int
i
;
char
*
p
=
buf
;
if
(
err_o
ccurred
())
if
(
PyErr_O
ccurred
())
return
;
if
(
iarg
==
0
&&
message
==
NULL
)
message
=
msg
;
...
...
@@ -293,7 +294,7 @@ seterror(iarg, msg, levels, fname, message)
sprintf
(
p
,
": expected %s found"
,
msg
);
message
=
buf
;
}
err_setstr
(
TypeError
,
message
);
PyErr_SetString
(
PyExc_
TypeError
,
message
);
}
...
...
@@ -318,7 +319,7 @@ seterror(iarg, msg, levels, fname, message)
static
char
*
converttuple
(
arg
,
p_format
,
p_va
,
levels
,
msgbuf
,
toplevel
)
o
bject
*
arg
;
PyO
bject
*
arg
;
char
**
p_format
;
va_list
*
p_va
;
int
*
levels
;
...
...
@@ -348,15 +349,15 @@ converttuple(arg, p_format, p_va, levels, msgbuf, toplevel)
n
++
;
}
if
(
!
is_tupleobject
(
arg
))
{
if
(
!
PyTuple_Check
(
arg
))
{
levels
[
0
]
=
0
;
sprintf
(
msgbuf
,
toplevel
?
"%d arguments, %s"
:
"%d-tuple, %s"
,
n
,
arg
==
None
?
"None"
:
arg
->
ob_type
->
tp_name
);
n
,
arg
==
Py_
None
?
"None"
:
arg
->
ob_type
->
tp_name
);
return
msgbuf
;
}
if
((
i
=
gettuples
ize
(
arg
))
!=
n
)
{
if
((
i
=
PyTuple_S
ize
(
arg
))
!=
n
)
{
levels
[
0
]
=
0
;
sprintf
(
msgbuf
,
toplevel
?
"%d arguments, %d"
:
"%d-tuple, %d-tuple"
,
...
...
@@ -367,7 +368,7 @@ converttuple(arg, p_format, p_va, levels, msgbuf, toplevel)
format
=
*
p_format
;
for
(
i
=
0
;
i
<
n
;
i
++
)
{
char
*
msg
;
msg
=
convertitem
(
gettuplei
tem
(
arg
,
i
),
&
format
,
p_va
,
msg
=
convertitem
(
PyTuple_GetI
tem
(
arg
,
i
),
&
format
,
p_va
,
levels
+
1
,
msgbuf
);
if
(
msg
!=
NULL
)
{
levels
[
0
]
=
i
+
1
;
...
...
@@ -384,7 +385,7 @@ converttuple(arg, p_format, p_va, levels, msgbuf, toplevel)
static
char
*
convertitem
(
arg
,
p_format
,
p_va
,
levels
,
msgbuf
)
o
bject
*
arg
;
PyO
bject
*
arg
;
char
**
p_format
;
va_list
*
p_va
;
int
*
levels
;
...
...
@@ -415,7 +416,7 @@ convertitem(arg, p_format, p_va, levels, msgbuf)
static
char
*
convertsimple
(
arg
,
p_format
,
p_va
,
msgbuf
)
o
bject
*
arg
;
PyO
bject
*
arg
;
char
**
p_format
;
va_list
*
p_va
;
char
*
msgbuf
;
...
...
@@ -423,7 +424,7 @@ convertsimple(arg, p_format, p_va, msgbuf)
char
*
msg
=
convertsimple1
(
arg
,
p_format
,
p_va
);
if
(
msg
!=
NULL
)
{
sprintf
(
msgbuf
,
"%.50s, %.50s"
,
msg
,
arg
==
None
?
"None"
:
arg
->
ob_type
->
tp_name
);
arg
==
Py_
None
?
"None"
:
arg
->
ob_type
->
tp_name
);
msg
=
msgbuf
;
}
return
msg
;
...
...
@@ -437,7 +438,7 @@ convertsimple(arg, p_format, p_va, msgbuf)
static
char
*
convertsimple1
(
arg
,
p_format
,
p_va
)
o
bject
*
arg
;
PyO
bject
*
arg
;
char
**
p_format
;
va_list
*
p_va
;
{
...
...
@@ -449,8 +450,8 @@ convertsimple1(arg, p_format, p_va)
case
'b'
:
/* byte -- very short int */
{
char
*
p
=
va_arg
(
*
p_va
,
char
*
);
long
ival
=
getintvalue
(
arg
);
if
(
ival
==
-
1
&&
err_o
ccurred
())
long
ival
=
PyInt_AsLong
(
arg
);
if
(
ival
==
-
1
&&
PyErr_O
ccurred
())
return
"integer<b>"
;
else
*
p
=
(
char
)
ival
;
...
...
@@ -460,8 +461,8 @@ convertsimple1(arg, p_format, p_va)
case
'h'
:
/* short int */
{
short
*
p
=
va_arg
(
*
p_va
,
short
*
);
long
ival
=
getintvalue
(
arg
);
if
(
ival
==
-
1
&&
err_o
ccurred
())
long
ival
=
PyInt_AsLong
(
arg
);
if
(
ival
==
-
1
&&
PyErr_O
ccurred
())
return
"integer<h>"
;
else
*
p
=
(
short
)
ival
;
...
...
@@ -471,8 +472,8 @@ convertsimple1(arg, p_format, p_va)
case
'i'
:
/* int */
{
int
*
p
=
va_arg
(
*
p_va
,
int
*
);
long
ival
=
getintvalue
(
arg
);
if
(
ival
==
-
1
&&
err_o
ccurred
())
long
ival
=
PyInt_AsLong
(
arg
);
if
(
ival
==
-
1
&&
PyErr_O
ccurred
())
return
"integer<i>"
;
else
*
p
=
ival
;
...
...
@@ -482,8 +483,8 @@ convertsimple1(arg, p_format, p_va)
case
'l'
:
/* long int */
{
long
*
p
=
va_arg
(
*
p_va
,
long
*
);
long
ival
=
getintvalue
(
arg
);
if
(
ival
==
-
1
&&
err_o
ccurred
())
long
ival
=
PyInt_AsLong
(
arg
);
if
(
ival
==
-
1
&&
PyErr_O
ccurred
())
return
"integer<l>"
;
else
*
p
=
ival
;
...
...
@@ -493,8 +494,8 @@ convertsimple1(arg, p_format, p_va)
case
'f'
:
/* float */
{
float
*
p
=
va_arg
(
*
p_va
,
float
*
);
double
dval
=
getfloatvalu
e
(
arg
);
if
(
err_o
ccurred
())
double
dval
=
PyFloat_AsDoubl
e
(
arg
);
if
(
PyErr_O
ccurred
())
return
"float<f>"
;
else
*
p
=
(
float
)
dval
;
...
...
@@ -504,8 +505,8 @@ convertsimple1(arg, p_format, p_va)
case
'd'
:
/* double */
{
double
*
p
=
va_arg
(
*
p_va
,
double
*
);
double
dval
=
getfloatvalu
e
(
arg
);
if
(
err_o
ccurred
())
double
dval
=
PyFloat_AsDoubl
e
(
arg
);
if
(
PyErr_O
ccurred
())
return
"float<d>"
;
else
*
p
=
dval
;
...
...
@@ -518,7 +519,7 @@ convertsimple1(arg, p_format, p_va)
Py_complex
*
p
=
va_arg
(
*
p_va
,
Py_complex
*
);
Py_complex
cval
;
cval
=
PyComplex_AsCComplex
(
arg
);
if
(
err_o
ccurred
())
if
(
PyErr_O
ccurred
())
return
"complex<D>"
;
else
*
p
=
cval
;
...
...
@@ -529,8 +530,8 @@ convertsimple1(arg, p_format, p_va)
case
'c'
:
/* char */
{
char
*
p
=
va_arg
(
*
p_va
,
char
*
);
if
(
is_stringobject
(
arg
)
&&
getstrings
ize
(
arg
)
==
1
)
*
p
=
getstringvalue
(
arg
)[
0
];
if
(
PyString_Check
(
arg
)
&&
PyString_S
ize
(
arg
)
==
1
)
*
p
=
PyString_AsString
(
arg
)[
0
];
else
return
"char"
;
break
;
...
...
@@ -539,16 +540,16 @@ convertsimple1(arg, p_format, p_va)
case
's'
:
/* string */
{
char
**
p
=
va_arg
(
*
p_va
,
char
**
);
if
(
is_stringobject
(
arg
))
*
p
=
getstringvalue
(
arg
);
if
(
PyString_Check
(
arg
))
*
p
=
PyString_AsString
(
arg
);
else
return
"string"
;
if
(
*
format
==
'#'
)
{
int
*
q
=
va_arg
(
*
p_va
,
int
*
);
*
q
=
getstrings
ize
(
arg
);
*
q
=
PyString_S
ize
(
arg
);
format
++
;
}
else
if
((
int
)
strlen
(
*
p
)
!=
getstrings
ize
(
arg
))
else
if
((
int
)
strlen
(
*
p
)
!=
PyString_S
ize
(
arg
))
return
"string without null bytes"
;
break
;
}
...
...
@@ -556,30 +557,30 @@ convertsimple1(arg, p_format, p_va)
case
'z'
:
/* string, may be NULL (None) */
{
char
**
p
=
va_arg
(
*
p_va
,
char
**
);
if
(
arg
==
None
)
if
(
arg
==
Py_
None
)
*
p
=
0
;
else
if
(
is_stringobject
(
arg
))
*
p
=
getstringvalue
(
arg
);
else
if
(
PyString_Check
(
arg
))
*
p
=
PyString_AsString
(
arg
);
else
return
"None or string"
;
if
(
*
format
==
'#'
)
{
int
*
q
=
va_arg
(
*
p_va
,
int
*
);
if
(
arg
==
None
)
if
(
arg
==
Py_
None
)
*
q
=
0
;
else
*
q
=
getstrings
ize
(
arg
);
*
q
=
PyString_S
ize
(
arg
);
format
++
;
}
else
if
(
*
p
!=
NULL
&&
(
int
)
strlen
(
*
p
)
!=
getstrings
ize
(
arg
))
(
int
)
strlen
(
*
p
)
!=
PyString_S
ize
(
arg
))
return
"None or string without null bytes"
;
break
;
}
case
'S'
:
/* string object */
{
object
**
p
=
va_arg
(
*
p_va
,
o
bject
**
);
if
(
is_stringobject
(
arg
))
PyObject
**
p
=
va_arg
(
*
p_va
,
PyO
bject
**
);
if
(
PyString_Check
(
arg
))
*
p
=
arg
;
else
return
"string"
;
...
...
@@ -588,15 +589,15 @@ convertsimple1(arg, p_format, p_va)
case
'O'
:
/* object */
{
typeo
bject
*
type
;
o
bject
**
p
;
PyTypeO
bject
*
type
;
PyO
bject
**
p
;
if
(
*
format
==
'!'
)
{
format
++
;
type
=
va_arg
(
*
p_va
,
typeo
bject
*
);
type
=
va_arg
(
*
p_va
,
PyTypeO
bject
*
);
if
(
arg
->
ob_type
!=
type
)
return
type
->
tp_name
;
else
{
p
=
va_arg
(
*
p_va
,
o
bject
**
);
p
=
va_arg
(
*
p_va
,
PyO
bject
**
);
*
p
=
arg
;
}
}
...
...
@@ -604,13 +605,13 @@ convertsimple1(arg, p_format, p_va)
inquiry
pred
=
va_arg
(
*
p_va
,
inquiry
);
format
++
;
if
((
*
pred
)(
arg
))
{
p
=
va_arg
(
*
p_va
,
o
bject
**
);
p
=
va_arg
(
*
p_va
,
PyO
bject
**
);
*
p
=
arg
;
}
}
else
if
(
*
format
==
'&'
)
{
typedef
int
(
*
converter
)
P
ROTO
((
o
bject
*
,
void
*
));
P
y_PROTO
((
PyO
bject
*
,
void
*
));
converter
convert
=
va_arg
(
*
p_va
,
converter
);
void
*
addr
=
va_arg
(
*
p_va
,
void
*
);
format
++
;
...
...
@@ -618,7 +619,7 @@ convertsimple1(arg, p_format, p_va)
return
"(unspecified)"
;
}
else
{
p
=
va_arg
(
*
p_va
,
o
bject
**
);
p
=
va_arg
(
*
p_va
,
PyO
bject
**
);
*
p
=
arg
;
}
break
;
...
...
@@ -639,8 +640,8 @@ convertsimple1(arg, p_format, p_va)
#ifdef HAVE_STDARG_PROTOTYPES
/* VARARGS2 */
int
PyArg_ParseTupleAndKeywords
(
o
bject
*
args
,
o
bject
*
keywords
,
int
PyArg_ParseTupleAndKeywords
(
PyO
bject
*
args
,
PyO
bject
*
keywords
,
char
*
format
,
char
**
kwlist
,
...)
#else
...
...
@@ -654,14 +655,14 @@ int PyArg_ParseTupleAndKeywords(va_alist) va_dcl
va_start
(
va
,
kwlist
);
#else
o
bject
*
args
;
o
bject
*
keywords
;
PyO
bject
*
args
;
PyO
bject
*
keywords
;
char
*
format
;
char
**
kwlist
;
va_start
(
va
);
args
=
va_arg
(
va
,
o
bject
*
);
keywords
=
va_arg
(
va
,
o
bject
*
);
args
=
va_arg
(
va
,
PyO
bject
*
);
keywords
=
va_arg
(
va
,
PyO
bject
*
);
format
=
va_arg
(
va
,
char
*
);
kwlist
=
va_arg
(
va
,
char
**
);
#endif
...
...
@@ -673,8 +674,8 @@ int PyArg_ParseTupleAndKeywords(va_alist) va_dcl
static
int
vgetargskeywords
(
args
,
keywords
,
format
,
kwlist
,
p_va
)
o
bject
*
args
;
o
bject
*
keywords
;
PyO
bject
*
args
;
PyO
bject
*
keywords
;
char
*
format
;
char
**
kwlist
;
va_list
*
p_va
;
...
...
@@ -689,7 +690,7 @@ vgetargskeywords(args, keywords, format, kwlist, p_va)
int
i
,
len
,
tplen
,
kwlen
;
char
*
msg
,
*
ks
,
**
p
;
int
nkwds
,
pos
,
match
,
converted
;
o
bject
*
key
,
*
value
;
PyO
bject
*
key
,
*
value
;
/* nested tuples cannot be parsed when using keyword arguments */
...
...
@@ -830,7 +831,7 @@ vgetargskeywords(args, keywords, format, kwlist, p_va)
converted
=
0
;
for
(
i
=
tplen
;
i
<
nkwds
;
i
++
)
{
o
bject
*
item
;
PyO
bject
*
item
;
if
(
*
format
==
'|'
)
format
++
;
item
=
PyMapping_GetItemString
(
keywords
,
kwlist
[
i
]);
...
...
@@ -961,7 +962,7 @@ skipitem(p_format, p_va)
case
'S'
:
/* string object */
{
(
void
)
va_arg
(
*
p_va
,
o
bject
**
);
(
void
)
va_arg
(
*
p_va
,
PyO
bject
**
);
break
;
}
...
...
@@ -969,8 +970,8 @@ skipitem(p_format, p_va)
{
if
(
*
format
==
'!'
)
{
format
++
;
(
void
)
va_arg
(
*
p_va
,
typeo
bject
*
);
(
void
)
va_arg
(
*
p_va
,
o
bject
**
);
(
void
)
va_arg
(
*
p_va
,
PyTypeO
bject
*
);
(
void
)
va_arg
(
*
p_va
,
PyO
bject
**
);
}
#if 0
/* I don't know what this is for */
...
...
@@ -978,19 +979,19 @@ skipitem(p_format, p_va)
inquiry pred = va_arg(*p_va, inquiry);
format++;
if ((*pred)(arg)) {
(void) va_arg(*p_va,
o
bject **);
(void) va_arg(*p_va,
PyO
bject **);
}
}
#endif
else
if
(
*
format
==
'&'
)
{
typedef
int
(
*
converter
)
P
ROTO
((
o
bject
*
,
void
*
));
P
y_PROTO
((
PyO
bject
*
,
void
*
));
(
void
)
va_arg
(
*
p_va
,
converter
);
(
void
)
va_arg
(
*
p_va
,
void
*
);
format
++
;
}
else
{
(
void
)
va_arg
(
*
p_va
,
o
bject
**
);
(
void
)
va_arg
(
*
p_va
,
PyO
bject
**
);
}
break
;
}
...
...
Python/import.c
View file @
5574f2d0
...
...
@@ -31,17 +31,12 @@ PERFORMANCE OF THIS SOFTWARE.
/* Module definition and import implementation */
#include "
allobjects
.h"
#include "
Python
.h"
/* XXX Some of the following are duplicate with allobjects.h... */
#include "node.h"
#include "token.h"
#include "graminit.h"
#include "import.h"
#include "errcode.h"
#include "sysmodule.h"
#include "bltinmodule.h"
#include "pythonrun.h"
#include "marshal.h"
#include "compile.h"
#include "eval.h"
...
...
@@ -57,7 +52,7 @@ PERFORMANCE OF THIS SOFTWARE.
#include <unistd.h>
#endif
extern
long
getmt
ime
();
/* In getmtime.c */
extern
long
PyOS_GetLastModificationT
ime
();
/* In getmtime.c */
/* Magic word to reject .pyc files generated by other Python versions */
/* Change for each incompatible change */
...
...
@@ -69,22 +64,22 @@ extern long getmtime(); /* In getmtime.c */
/* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */
#define MAGIC (20121 | ((long)'\r'<<16) | ((long)'\n'<<24))
o
bject
*
import_modules
;
/* This becomes sys.modules */
PyO
bject
*
import_modules
;
/* This becomes sys.modules */
/* Initialize things */
void
initimpor
t
()
PyImport_Ini
t
()
{
if
(
import_modules
!=
NULL
)
fatal
(
"duplicate initimport() call"
);
if
((
import_modules
=
newdictobject
())
==
NULL
)
fatal
(
"no mem for dictionary of modules"
);
Py_FatalError
(
"duplicate initimport() call"
);
if
((
import_modules
=
PyDict_New
())
==
NULL
)
Py_FatalError
(
"no mem for dictionary of modules"
);
if
(
Py_OptimizeFlag
)
{
/* Replace ".pyc" with ".pyo" in import_filetab */
struct
filedescr
*
p
;
for
(
p
=
import_f
iletab
;
p
->
suffix
!=
NULL
;
p
++
)
{
for
(
p
=
_PyImport_F
iletab
;
p
->
suffix
!=
NULL
;
p
++
)
{
if
(
strcmp
(
p
->
suffix
,
".pyc"
)
==
0
)
p
->
suffix
=
".pyo"
;
}
...
...
@@ -95,19 +90,19 @@ initimport()
/* Un-initialize things, as good as we can */
void
doneimport
()
PyImport_Cleanup
()
{
if
(
import_modules
!=
NULL
)
{
o
bject
*
tmp
=
import_modules
;
PyO
bject
*
tmp
=
import_modules
;
import_modules
=
NULL
;
/* This deletes all modules from sys.modules.
When a module is deallocated, it in turn clears its
dictionary,
thus hopefully breaking any circular references between modules
and between a module's dictionary and its functions.
Note that "import" will fail while we are cleaning up.
*/
mappingc
lear
(
tmp
);
DECREF
(
tmp
);
When a module is deallocated, it in turn clears its
dictionary, thus hopefully breaking any circular
references between modules and between a module's
dictionary and its functions. Note that "import"
will fail while we are cleaning up.
*/
PyDict_C
lear
(
tmp
);
Py_
DECREF
(
tmp
);
}
}
...
...
@@ -115,7 +110,7 @@ doneimport()
/* Helper for pythonrun.c -- return magic number */
long
get_pyc_magic
()
PyImport_GetMagicNumber
()
{
return
MAGIC
;
}
...
...
@@ -123,8 +118,8 @@ get_pyc_magic()
/* Helper for sysmodule.c -- return modules dictionary */
o
bject
*
get_modules
()
PyO
bject
*
PyImport_GetModuleDict
()
{
return
import_modules
;
}
...
...
@@ -136,27 +131,28 @@ get_modules()
Because the former action is most common, THIS DOES NOT RETURN A
'NEW' REFERENCE! */
o
bject
*
add_m
odule
(
name
)
PyO
bject
*
PyImport_AddM
odule
(
name
)
char
*
name
;
{
o
bject
*
m
;
PyO
bject
*
m
;
if
(
import_modules
==
NULL
)
{
err_setstr
(
SystemError
,
"sys.modules has been deleted"
);
PyErr_SetString
(
PyExc_SystemError
,
"sys.modules has been deleted"
);
return
NULL
;
}
if
((
m
=
dictlookup
(
import_modules
,
name
))
!=
NULL
&&
is_moduleobject
(
m
))
if
((
m
=
PyDict_GetItemString
(
import_modules
,
name
))
!=
NULL
&&
PyModule_Check
(
m
))
return
m
;
m
=
newmoduleobject
(
name
);
m
=
PyModule_New
(
name
);
if
(
m
==
NULL
)
return
NULL
;
if
(
dictinsert
(
import_modules
,
name
,
m
)
!=
0
)
{
DECREF
(
m
);
if
(
PyDict_SetItemString
(
import_modules
,
name
,
m
)
!=
0
)
{
Py_
DECREF
(
m
);
return
NULL
;
}
DECREF
(
m
);
/* Yes, it still exists, in modules! */
Py_
DECREF
(
m
);
/* Yes, it still exists, in modules! */
return
m
;
}
...
...
@@ -165,29 +161,31 @@ add_module(name)
/* Execute a code object in a module and return the module object
WITH INCREMENTED REFERENCE COUNT */
o
bject
*
exec_code_m
odule
(
name
,
co
)
PyO
bject
*
PyImport_ExecCodeM
odule
(
name
,
co
)
char
*
name
;
o
bject
*
co
;
PyO
bject
*
co
;
{
o
bject
*
m
,
*
d
,
*
v
;
PyO
bject
*
m
,
*
d
,
*
v
;
m
=
add_m
odule
(
name
);
m
=
PyImport_AddM
odule
(
name
);
if
(
m
==
NULL
)
return
NULL
;
d
=
getmoduledict
(
m
);
if
(
dictlookup
(
d
,
"__builtins__"
)
==
NULL
)
{
if
(
dictinsert
(
d
,
"__builtins__"
,
getbuiltins
())
!=
0
)
d
=
PyModule_GetDict
(
m
);
if
(
PyDict_GetItemString
(
d
,
"__builtins__"
)
==
NULL
)
{
if
(
PyDict_SetItemString
(
d
,
"__builtins__"
,
PyEval_GetBuiltins
())
!=
0
)
return
NULL
;
}
/* Remember the filename as the __file__ attribute */
if
(
dictinsert
(
d
,
"__file__"
,
((
codeobject
*
)
co
)
->
co_filename
)
!=
0
)
err_clear
();
/* Not important enough to report */
v
=
eval_code
((
codeobject
*
)
co
,
d
,
d
);
/* XXX owner? */
if
(
PyDict_SetItemString
(
d
,
"__file__"
,
((
PyCodeObject
*
)
co
)
->
co_filename
)
!=
0
)
PyErr_Clear
();
/* Not important enough to report */
v
=
PyEval_EvalCode
((
PyCodeObject
*
)
co
,
d
,
d
);
/* XXX owner? */
if
(
v
==
NULL
)
return
NULL
;
DECREF
(
v
);
INCREF
(
m
);
Py_
DECREF
(
v
);
Py_
INCREF
(
m
);
return
m
;
}
...
...
@@ -236,21 +234,21 @@ check_compiled_module(pathname, mtime, cpathname)
fp
=
fopen
(
cpathname
,
"rb"
);
if
(
fp
==
NULL
)
return
NULL
;
magic
=
rd_long
(
fp
);
magic
=
PyMarshal_ReadLongFromFile
(
fp
);
if
(
magic
!=
MAGIC
)
{
if
(
verbose
)
if
(
Py_VerboseFlag
)
fprintf
(
stderr
,
"# %s has bad magic
\n
"
,
cpathname
);
fclose
(
fp
);
return
NULL
;
}
pyc_mtime
=
rd_long
(
fp
);
pyc_mtime
=
PyMarshal_ReadLongFromFile
(
fp
);
if
(
pyc_mtime
!=
mtime
)
{
if
(
verbose
)
if
(
Py_VerboseFlag
)
fprintf
(
stderr
,
"# %s has bad mtime
\n
"
,
cpathname
);
fclose
(
fp
);
return
NULL
;
}
if
(
verbose
)
if
(
Py_VerboseFlag
)
fprintf
(
stderr
,
"# %s matches %s
\n
"
,
cpathname
,
pathname
);
return
fp
;
}
...
...
@@ -258,71 +256,72 @@ check_compiled_module(pathname, mtime, cpathname)
/* Read a code object from a file and check it for validity */
static
codeo
bject
*
static
PyCodeO
bject
*
read_compiled_module
(
fp
)
FILE
*
fp
;
{
o
bject
*
co
;
PyO
bject
*
co
;
co
=
rd_object
(
fp
);
co
=
PyMarshal_ReadObjectFromFile
(
fp
);
/* Ugly: rd_object() may return NULL with or without error */
if
(
co
==
NULL
||
!
is_codeobject
(
co
))
{
if
(
!
err_o
ccurred
())
err_setstr
(
ImportError
,
if
(
co
==
NULL
||
!
PyCode_Check
(
co
))
{
if
(
!
PyErr_O
ccurred
())
PyErr_SetString
(
PyExc_
ImportError
,
"Non-code object in .pyc file"
);
XDECREF
(
co
);
Py_
XDECREF
(
co
);
return
NULL
;
}
return
(
codeo
bject
*
)
co
;
return
(
PyCodeO
bject
*
)
co
;
}
/* Load a module from a compiled file, execute it, and return its
module object WITH INCREMENTED REFERENCE COUNT */
static
o
bject
*
static
PyO
bject
*
load_compiled_module
(
name
,
cpathname
,
fp
)
char
*
name
;
char
*
cpathname
;
FILE
*
fp
;
{
long
magic
;
codeo
bject
*
co
;
o
bject
*
m
;
PyCodeO
bject
*
co
;
PyO
bject
*
m
;
magic
=
rd_long
(
fp
);
magic
=
PyMarshal_ReadLongFromFile
(
fp
);
if
(
magic
!=
MAGIC
)
{
err_setstr
(
ImportError
,
"Bad magic number in .pyc file"
);
PyErr_SetString
(
PyExc_ImportError
,
"Bad magic number in .pyc file"
);
return
NULL
;
}
(
void
)
rd_long
(
fp
);
(
void
)
PyMarshal_ReadLongFromFile
(
fp
);
co
=
read_compiled_module
(
fp
);
if
(
co
==
NULL
)
return
NULL
;
if
(
verbose
)
if
(
Py_VerboseFlag
)
fprintf
(
stderr
,
"import %s # precompiled from %s
\n
"
,
name
,
cpathname
);
m
=
exec_code_module
(
name
,
(
o
bject
*
)
co
);
DECREF
(
co
);
m
=
PyImport_ExecCodeModule
(
name
,
(
PyO
bject
*
)
co
);
Py_
DECREF
(
co
);
return
m
;
}
/* Parse a source file and return the corresponding code object */
static
codeo
bject
*
static
PyCodeO
bject
*
parse_source_module
(
pathname
,
fp
)
char
*
pathname
;
FILE
*
fp
;
{
codeo
bject
*
co
;
PyCodeO
bject
*
co
;
node
*
n
;
n
=
parse_f
ile
(
fp
,
pathname
,
file_input
);
n
=
PyParser_SimpleParseF
ile
(
fp
,
pathname
,
file_input
);
if
(
n
==
NULL
)
return
NULL
;
co
=
c
ompile
(
n
,
pathname
);
freet
ree
(
n
);
co
=
PyNode_C
ompile
(
n
,
pathname
);
PyNode_F
ree
(
n
);
return
co
;
}
...
...
@@ -335,7 +334,7 @@ parse_source_module(pathname, fp)
static
void
write_compiled_module
(
co
,
cpathname
,
mtime
)
codeo
bject
*
co
;
PyCodeO
bject
*
co
;
char
*
cpathname
;
long
mtime
;
{
...
...
@@ -343,17 +342,17 @@ write_compiled_module(co, cpathname, mtime)
fp
=
fopen
(
cpathname
,
"wb"
);
if
(
fp
==
NULL
)
{
if
(
verbose
)
if
(
Py_VerboseFlag
)
fprintf
(
stderr
,
"# can't create %s
\n
"
,
cpathname
);
return
;
}
wr_long
(
MAGIC
,
fp
);
PyMarshal_WriteLongToFile
(
MAGIC
,
fp
);
/* First write a 0 for mtime */
wr_long
(
0L
,
fp
);
wr_object
((
o
bject
*
)
co
,
fp
);
PyMarshal_WriteLongToFile
(
0L
,
fp
);
PyMarshal_WriteObjectToFile
((
PyO
bject
*
)
co
,
fp
);
if
(
ferror
(
fp
))
{
if
(
verbose
)
if
(
Py_VerboseFlag
)
fprintf
(
stderr
,
"# can't write %s
\n
"
,
cpathname
);
/* Don't keep partial file */
fclose
(
fp
);
...
...
@@ -362,10 +361,10 @@ write_compiled_module(co, cpathname, mtime)
}
/* Now write the true mtime */
fseek
(
fp
,
4L
,
0
);
wr_long
(
mtime
,
fp
);
PyMarshal_WriteLongToFile
(
mtime
,
fp
);
fflush
(
fp
);
fclose
(
fp
);
if
(
verbose
)
if
(
Py_VerboseFlag
)
fprintf
(
stderr
,
"# wrote %s
\n
"
,
cpathname
);
#ifdef macintosh
setfiletype
(
cpathname
,
'
Pyth
'
,
'
PYC
'
);
...
...
@@ -377,7 +376,7 @@ write_compiled_module(co, cpathname, mtime)
object WITH INCREMENTED REFERENCE COUNT. If there's a matching
byte-compiled file, use that instead. */
static
o
bject
*
static
PyO
bject
*
load_source_module
(
name
,
pathname
,
fp
)
char
*
name
;
char
*
pathname
;
...
...
@@ -387,10 +386,10 @@ load_source_module(name, pathname, fp)
FILE
*
fpc
;
char
buf
[
MAXPATHLEN
+
1
];
char
*
cpathname
;
codeo
bject
*
co
;
o
bject
*
m
;
PyCodeO
bject
*
co
;
PyO
bject
*
m
;
mtime
=
getmt
ime
(
pathname
);
mtime
=
PyOS_GetLastModificationT
ime
(
pathname
);
cpathname
=
make_compiled_pathname
(
pathname
,
buf
,
MAXPATHLEN
+
1
);
if
(
cpathname
!=
NULL
&&
(
fpc
=
check_compiled_module
(
pathname
,
mtime
,
cpathname
)))
{
...
...
@@ -398,7 +397,7 @@ load_source_module(name, pathname, fp)
fclose
(
fpc
);
if
(
co
==
NULL
)
return
NULL
;
if
(
verbose
)
if
(
Py_VerboseFlag
)
fprintf
(
stderr
,
"import %s # precompiled from %s
\n
"
,
name
,
cpathname
);
}
...
...
@@ -406,13 +405,13 @@ load_source_module(name, pathname, fp)
co
=
parse_source_module
(
pathname
,
fp
);
if
(
co
==
NULL
)
return
NULL
;
if
(
verbose
)
if
(
Py_VerboseFlag
)
fprintf
(
stderr
,
"import %s # from %s
\n
"
,
name
,
pathname
);
write_compiled_module
(
co
,
cpathname
,
mtime
);
}
m
=
exec_code_module
(
name
,
(
o
bject
*
)
co
);
DECREF
(
co
);
m
=
PyImport_ExecCodeModule
(
name
,
(
PyO
bject
*
)
co
);
Py_
DECREF
(
co
);
return
m
;
}
...
...
@@ -425,7 +424,7 @@ load_source_module(name, pathname, fp)
static
struct
filedescr
*
find_module
(
name
,
path
,
buf
,
buflen
,
p_fp
)
char
*
name
;
o
bject
*
path
;
PyO
bject
*
path
;
/* Output parameters: */
char
*
buf
;
int
buflen
;
...
...
@@ -445,27 +444,28 @@ find_module(name, path, buf, buflen, p_fp)
if
(
path
==
NULL
)
path
=
sysge
t
(
"path"
);
if
(
path
==
NULL
||
!
is_listobject
(
path
))
{
err_setstr
(
ImportError
,
path
=
PySys_GetObjec
t
(
"path"
);
if
(
path
==
NULL
||
!
PyList_Check
(
path
))
{
PyErr_SetString
(
PyExc_
ImportError
,
"sys.path must be a list of directory names"
);
return
NULL
;
}
npath
=
getlists
ize
(
path
);
npath
=
PyList_S
ize
(
path
);
namelen
=
strlen
(
name
);
for
(
i
=
0
;
i
<
npath
;
i
++
)
{
object
*
v
=
getlisti
tem
(
path
,
i
);
if
(
!
is_stringobject
(
v
))
PyObject
*
v
=
PyList_GetI
tem
(
path
,
i
);
if
(
!
PyString_Check
(
v
))
continue
;
len
=
getstrings
ize
(
v
);
if
(
len
+
2
+
namelen
+
import_maxsuffixs
ize
>=
buflen
)
len
=
PyString_S
ize
(
v
);
if
(
len
+
2
+
namelen
+
_PyImport_MaxSuffixS
ize
>=
buflen
)
continue
;
/* Too long */
strcpy
(
buf
,
getstringvalue
(
v
));
strcpy
(
buf
,
PyString_AsString
(
v
));
if
((
int
)
strlen
(
buf
)
!=
len
)
continue
;
/* v contains '\0' */
#ifdef macintosh
if
(
PyMac_FindResourceModule
(
name
,
buf
)
)
{
static
struct
filedescr
resfiledescr
=
{
""
,
""
,
PY_RESOURCE
};
static
struct
filedescr
resfiledescr
=
{
""
,
""
,
PY_RESOURCE
};
return
&
resfiledescr
;
}
...
...
@@ -476,7 +476,7 @@ find_module(name, path, buf, buflen, p_fp)
/* see if we are searching in directory dos_8x3 */
if
(
len
>
7
&&
!
strncmp
(
buf
+
len
-
8
,
"dos_8x3"
,
7
)){
int
j
;
char
ch
;
/* limit name to
eight
lower-case characters */
char
ch
;
/* limit name to
8
lower-case characters */
for
(
j
=
0
;
(
ch
=
name
[
j
])
&&
j
<
8
;
j
++
)
if
(
isupper
(
ch
))
buf
[
len
++
]
=
tolower
(
ch
);
...
...
@@ -489,9 +489,9 @@ find_module(name, path, buf, buflen, p_fp)
strcpy
(
buf
+
len
,
name
);
len
+=
namelen
;
}
for
(
fdp
=
import_f
iletab
;
fdp
->
suffix
!=
NULL
;
fdp
++
)
{
for
(
fdp
=
_PyImport_F
iletab
;
fdp
->
suffix
!=
NULL
;
fdp
++
)
{
strcpy
(
buf
+
len
,
fdp
->
suffix
);
if
(
verbose
>
1
)
if
(
Py_VerboseFlag
>
1
)
fprintf
(
stderr
,
"# trying %s
\n
"
,
buf
);
fp
=
fopen
(
buf
,
fdp
->
mode
);
if
(
fp
!=
NULL
)
...
...
@@ -503,7 +503,7 @@ find_module(name, path, buf, buflen, p_fp)
if
(
fp
==
NULL
)
{
char
buf
[
256
];
sprintf
(
buf
,
"No module named %.200s"
,
name
);
err_setstr
(
ImportError
,
buf
);
PyErr_SetString
(
PyExc_
ImportError
,
buf
);
return
NULL
;
}
...
...
@@ -515,16 +515,16 @@ find_module(name, path, buf, buflen, p_fp)
/* Load an external module using the default search path and return
its module object WITH INCREMENTED REFERENCE COUNT */
static
o
bject
*
static
PyO
bject
*
load_module
(
name
)
char
*
name
;
{
char
buf
[
MAXPATHLEN
+
1
];
struct
filedescr
*
fdp
;
FILE
*
fp
=
NULL
;
o
bject
*
m
;
PyO
bject
*
m
;
fdp
=
find_module
(
name
,
(
o
bject
*
)
NULL
,
buf
,
MAXPATHLEN
+
1
,
&
fp
);
fdp
=
find_module
(
name
,
(
PyO
bject
*
)
NULL
,
buf
,
MAXPATHLEN
+
1
,
&
fp
);
if
(
fdp
==
NULL
)
return
NULL
;
...
...
@@ -539,7 +539,7 @@ load_module(name)
break
;
case
C_EXTENSION
:
m
=
load_dynamic_m
odule
(
name
,
buf
,
fp
);
m
=
_PyImport_LoadDynamicM
odule
(
name
,
buf
,
fp
);
break
;
#ifdef macintosh
...
...
@@ -549,7 +549,7 @@ load_module(name)
#endif
default:
err_setstr
(
SystemError
,
PyErr_SetString
(
PyExc_
SystemError
,
"find_module returned unexpected result"
);
m
=
NULL
;
...
...
@@ -573,15 +573,15 @@ init_builtin(name)
for
(
i
=
0
;
inittab
[
i
].
name
!=
NULL
;
i
++
)
{
if
(
strcmp
(
name
,
inittab
[
i
].
name
)
==
0
)
{
if
(
inittab
[
i
].
initfunc
==
NULL
)
{
err_setstr
(
ImportError
,
PyErr_SetString
(
PyExc_
ImportError
,
"Cannot re-init internal module"
);
return
-
1
;
}
if
(
verbose
)
if
(
Py_VerboseFlag
)
fprintf
(
stderr
,
"import %s # builtin
\n
"
,
name
);
(
*
inittab
[
i
].
initfunc
)();
if
(
err_o
ccurred
())
if
(
PyErr_O
ccurred
())
return
-
1
;
return
1
;
}
...
...
@@ -598,7 +598,7 @@ find_frozen(name)
{
struct
_frozen
*
p
;
for
(
p
=
frozen_m
odules
;
;
p
++
)
{
for
(
p
=
PyImport_FrozenM
odules
;
;
p
++
)
{
if
(
p
->
name
==
NULL
)
return
NULL
;
if
(
strcmp
(
p
->
name
,
name
)
==
0
)
...
...
@@ -607,17 +607,17 @@ find_frozen(name)
return
p
;
}
static
o
bject
*
static
PyO
bject
*
get_frozen_object
(
name
)
char
*
name
;
{
struct
_frozen
*
p
=
find_frozen
(
name
);
if
(
p
==
NULL
)
{
err_setstr
(
ImportError
,
"No such frozen object"
);
PyErr_SetString
(
PyExc_
ImportError
,
"No such frozen object"
);
return
NULL
;
}
return
rds_object
((
char
*
)
p
->
code
,
p
->
size
);
return
PyMarshal_ReadObjectFromString
((
char
*
)
p
->
code
,
p
->
size
);
}
/* Initialize a frozen module.
...
...
@@ -626,30 +626,31 @@ get_frozen_object(name)
This function is also used from frozenmain.c */
int
init_frozen
(
name
)
PyImport_ImportFrozenModule
(
name
)
char
*
name
;
{
struct
_frozen
*
p
=
find_frozen
(
name
);
o
bject
*
co
;
o
bject
*
m
;
PyO
bject
*
co
;
PyO
bject
*
m
;
if
(
p
==
NULL
)
return
0
;
if
(
verbose
)
if
(
Py_VerboseFlag
)
fprintf
(
stderr
,
"import %s # frozen
\n
"
,
name
);
co
=
rds_object
((
char
*
)
p
->
code
,
p
->
size
);
co
=
PyMarshal_ReadObjectFromString
((
char
*
)
p
->
code
,
p
->
size
);
if
(
co
==
NULL
)
return
-
1
;
if
(
!
is_codeobject
(
co
))
{
DECREF
(
co
);
err_setstr
(
TypeError
,
"frozen object is not a code object"
);
if
(
!
PyCode_Check
(
co
))
{
Py_DECREF
(
co
);
PyErr_SetString
(
PyExc_TypeError
,
"frozen object is not a code object"
);
return
-
1
;
}
m
=
exec_code_m
odule
(
name
,
co
);
DECREF
(
co
);
m
=
PyImport_ExecCodeM
odule
(
name
,
co
);
Py_
DECREF
(
co
);
if
(
m
==
NULL
)
return
-
1
;
DECREF
(
m
);
Py_
DECREF
(
m
);
return
1
;
}
...
...
@@ -657,31 +658,34 @@ init_frozen(name)
/* Import a module, either built-in, frozen, or external, and return
its module object WITH INCREMENTED REFERENCE COUNT */
o
bject
*
import_m
odule
(
name
)
PyO
bject
*
PyImport_ImportM
odule
(
name
)
char
*
name
;
{
o
bject
*
m
;
PyO
bject
*
m
;
if
(
import_modules
==
NULL
)
{
err_setstr
(
SystemError
,
"sys.modules has been deleted"
);
PyErr_SetString
(
PyExc_SystemError
,
"sys.modules has been deleted"
);
return
NULL
;
}
if
((
m
=
dictlookup
(
import_modules
,
name
))
!=
NULL
)
{
INCREF
(
m
);
if
((
m
=
PyDict_GetItemString
(
import_modules
,
name
))
!=
NULL
)
{
Py_
INCREF
(
m
);
}
else
{
int
i
;
if
((
i
=
init_builtin
(
name
))
||
(
i
=
init_frozen
(
name
)))
{
if
((
i
=
init_builtin
(
name
))
||
(
i
=
PyImport_ImportFrozenModule
(
name
)))
{
if
(
i
<
0
)
return
NULL
;
if
((
m
=
dictlookup
(
import_modules
,
name
))
==
NULL
)
{
if
(
err_occurred
()
==
NULL
)
err_setstr
(
SystemError
,
if
((
m
=
PyDict_GetItemString
(
import_modules
,
name
))
==
NULL
)
{
if
(
PyErr_Occurred
()
==
NULL
)
PyErr_SetString
(
PyExc_SystemError
,
"built-in module not initialized properly"
);
}
else
INCREF
(
m
);
Py_
INCREF
(
m
);
}
else
m
=
load_module
(
name
);
...
...
@@ -694,33 +698,37 @@ import_module(name)
/* Re-import a module of any kind and return its module object, WITH
INCREMENTED REFERENCE COUNT */
o
bject
*
reload_m
odule
(
m
)
o
bject
*
m
;
PyO
bject
*
PyImport_ReloadM
odule
(
m
)
PyO
bject
*
m
;
{
char
*
name
;
int
i
;
if
(
m
==
NULL
||
!
is_moduleobject
(
m
))
{
err_setstr
(
TypeError
,
"reload() argument must be module"
);
if
(
m
==
NULL
||
!
PyModule_Check
(
m
))
{
PyErr_SetString
(
PyExc_TypeError
,
"reload() argument must be module"
);
return
NULL
;
}
name
=
getmodulen
ame
(
m
);
name
=
PyModule_GetN
ame
(
m
);
if
(
name
==
NULL
)
return
NULL
;
if
(
import_modules
==
NULL
)
{
err_setstr
(
SystemError
,
"sys.modules has been deleted"
);
PyErr_SetString
(
PyExc_SystemError
,
"sys.modules has been deleted"
);
return
NULL
;
}
if
(
m
!=
dictlookup
(
import_modules
,
name
))
{
err_setstr
(
ImportError
,
"reload() module not in sys.modules"
);
if
(
m
!=
PyDict_GetItemString
(
import_modules
,
name
))
{
PyErr_SetString
(
PyExc_ImportError
,
"reload() module not in sys.modules"
);
return
NULL
;
}
/* Check for built-in and frozen modules */
if
((
i
=
init_builtin
(
name
))
||
(
i
=
init_frozen
(
name
)))
{
if
((
i
=
init_builtin
(
name
))
||
(
i
=
PyImport_ImportFrozenModule
(
name
)))
{
if
(
i
<
0
)
return
NULL
;
INCREF
(
m
);
Py_
INCREF
(
m
);
}
else
m
=
load_module
(
name
);
...
...
@@ -732,206 +740,208 @@ reload_module(m)
importing modules.
*/
static
o
bject
*
static
PyO
bject
*
imp_get_magic
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
char
buf
[
4
];
if
(
!
newgetargs
(
args
,
""
))
if
(
!
PyArg_ParseTuple
(
args
,
""
))
return
NULL
;
buf
[
0
]
=
(
char
)
((
MAGIC
>>
0
)
&
0xff
);
buf
[
1
]
=
(
char
)
((
MAGIC
>>
8
)
&
0xff
);
buf
[
2
]
=
(
char
)
((
MAGIC
>>
16
)
&
0xff
);
buf
[
3
]
=
(
char
)
((
MAGIC
>>
24
)
&
0xff
);
return
newsizedstringobject
(
buf
,
4
);
return
PyString_FromStringAndSize
(
buf
,
4
);
}
static
o
bject
*
static
PyO
bject
*
imp_get_suffixes
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
list
;
PyO
bject
*
list
;
struct
filedescr
*
fdp
;
if
(
!
newgetargs
(
args
,
""
))
if
(
!
PyArg_ParseTuple
(
args
,
""
))
return
NULL
;
list
=
newlistobject
(
0
);
list
=
PyList_New
(
0
);
if
(
list
==
NULL
)
return
NULL
;
for
(
fdp
=
import_f
iletab
;
fdp
->
suffix
!=
NULL
;
fdp
++
)
{
object
*
item
=
mkv
alue
(
"ssi"
,
for
(
fdp
=
_PyImport_F
iletab
;
fdp
->
suffix
!=
NULL
;
fdp
++
)
{
PyObject
*
item
=
Py_BuildV
alue
(
"ssi"
,
fdp
->
suffix
,
fdp
->
mode
,
fdp
->
type
);
if
(
item
==
NULL
)
{
DECREF
(
list
);
Py_
DECREF
(
list
);
return
NULL
;
}
if
(
addlistitem
(
list
,
item
)
<
0
)
{
DECREF
(
list
);
DECREF
(
item
);
if
(
PyList_Append
(
list
,
item
)
<
0
)
{
Py_
DECREF
(
list
);
Py_
DECREF
(
item
);
return
NULL
;
}
DECREF
(
item
);
Py_
DECREF
(
item
);
}
return
list
;
}
static
o
bject
*
static
PyO
bject
*
imp_find_module
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
extern
int
fclose
PROTO
((
FILE
*
));
extern
int
fclose
P
y_P
ROTO
((
FILE
*
));
char
*
name
;
o
bject
*
path
=
NULL
;
o
bject
*
fob
,
*
ret
;
PyO
bject
*
path
=
NULL
;
PyO
bject
*
fob
,
*
ret
;
struct
filedescr
*
fdp
;
char
pathname
[
MAXPATHLEN
+
1
];
FILE
*
fp
;
if
(
!
newgetargs
(
args
,
"s|O!"
,
&
name
,
&
Listt
ype
,
&
path
))
if
(
!
PyArg_ParseTuple
(
args
,
"s|O!"
,
&
name
,
&
PyList_T
ype
,
&
path
))
return
NULL
;
fdp
=
find_module
(
name
,
path
,
pathname
,
MAXPATHLEN
+
1
,
&
fp
);
if
(
fdp
==
NULL
)
return
NULL
;
fob
=
newopenfileobject
(
fp
,
pathname
,
fdp
->
mode
,
fclose
);
fob
=
PyFile_FromFile
(
fp
,
pathname
,
fdp
->
mode
,
fclose
);
if
(
fob
==
NULL
)
{
fclose
(
fp
);
return
NULL
;
}
ret
=
mkv
alue
(
"Os(ssi)"
,
ret
=
Py_BuildV
alue
(
"Os(ssi)"
,
fob
,
pathname
,
fdp
->
suffix
,
fdp
->
mode
,
fdp
->
type
);
DECREF
(
fob
);
Py_
DECREF
(
fob
);
return
ret
;
}
static
o
bject
*
static
PyO
bject
*
imp_init_builtin
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
char
*
name
;
int
ret
;
o
bject
*
m
;
if
(
!
newgetargs
(
args
,
"s"
,
&
name
))
PyO
bject
*
m
;
if
(
!
PyArg_ParseTuple
(
args
,
"s"
,
&
name
))
return
NULL
;
ret
=
init_builtin
(
name
);
if
(
ret
<
0
)
return
NULL
;
if
(
ret
==
0
)
{
INCREF
(
None
);
return
None
;
Py_INCREF
(
Py_
None
);
return
Py_
None
;
}
m
=
add_m
odule
(
name
);
XINCREF
(
m
);
m
=
PyImport_AddM
odule
(
name
);
Py_
XINCREF
(
m
);
return
m
;
}
static
o
bject
*
static
PyO
bject
*
imp_init_frozen
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
char
*
name
;
int
ret
;
o
bject
*
m
;
if
(
!
newgetargs
(
args
,
"s"
,
&
name
))
PyO
bject
*
m
;
if
(
!
PyArg_ParseTuple
(
args
,
"s"
,
&
name
))
return
NULL
;
ret
=
init_frozen
(
name
);
ret
=
PyImport_ImportFrozenModule
(
name
);
if
(
ret
<
0
)
return
NULL
;
if
(
ret
==
0
)
{
INCREF
(
None
);
return
None
;
Py_INCREF
(
Py_
None
);
return
Py_
None
;
}
m
=
add_m
odule
(
name
);
XINCREF
(
m
);
m
=
PyImport_AddM
odule
(
name
);
Py_
XINCREF
(
m
);
return
m
;
}
static
o
bject
*
static
PyO
bject
*
imp_get_frozen_object
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
char
*
name
;
if
(
!
newgetargs
(
args
,
"s"
,
&
name
))
if
(
!
PyArg_ParseTuple
(
args
,
"s"
,
&
name
))
return
NULL
;
return
get_frozen_object
(
name
);
}
static
o
bject
*
static
PyO
bject
*
imp_is_builtin
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
int
i
;
char
*
name
;
if
(
!
newgetargs
(
args
,
"s"
,
&
name
))
if
(
!
PyArg_ParseTuple
(
args
,
"s"
,
&
name
))
return
NULL
;
for
(
i
=
0
;
inittab
[
i
].
name
!=
NULL
;
i
++
)
{
if
(
strcmp
(
name
,
inittab
[
i
].
name
)
==
0
)
{
if
(
inittab
[
i
].
initfunc
==
NULL
)
return
newintobject
(
-
1
);
return
PyInt_FromLong
(
-
1
);
else
return
newintobject
(
1
);
return
PyInt_FromLong
(
1
);
}
}
return
newintobject
(
0
);
return
PyInt_FromLong
(
0
);
}
static
o
bject
*
static
PyO
bject
*
imp_is_frozen
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
struct
_frozen
*
p
;
char
*
name
;
if
(
!
newgetargs
(
args
,
"s"
,
&
name
))
if
(
!
PyArg_ParseTuple
(
args
,
"s"
,
&
name
))
return
NULL
;
for
(
p
=
frozen_m
odules
;
;
p
++
)
{
for
(
p
=
PyImport_FrozenM
odules
;
;
p
++
)
{
if
(
p
->
name
==
NULL
)
break
;
if
(
strcmp
(
p
->
name
,
name
)
==
0
)
return
newintobject
(
1
);
return
PyInt_FromLong
(
1
);
}
return
newintobject
(
0
);
return
PyInt_FromLong
(
0
);
}
static
FILE
*
get_file
(
pathname
,
fob
,
mode
)
char
*
pathname
;
o
bject
*
fob
;
PyO
bject
*
fob
;
char
*
mode
;
{
FILE
*
fp
;
if
(
fob
==
NULL
)
{
fp
=
fopen
(
pathname
,
mode
);
if
(
fp
==
NULL
)
err_errno
(
IOError
);
PyErr_SetFromErrno
(
PyExc_
IOError
);
}
else
{
fp
=
getfilef
ile
(
fob
);
fp
=
PyFile_AsF
ile
(
fob
);
if
(
fp
==
NULL
)
err_setstr
(
ValueError
,
"bad/closed file object"
);
PyErr_SetString
(
PyExc_ValueError
,
"bad/closed file object"
);
}
return
fp
;
}
static
o
bject
*
static
PyO
bject
*
imp_load_compiled
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
char
*
name
;
char
*
pathname
;
o
bject
*
fob
=
NULL
;
o
bject
*
m
;
PyO
bject
*
fob
=
NULL
;
PyO
bject
*
m
;
FILE
*
fp
;
if
(
!
newgetargs
(
args
,
"ssO!"
,
&
name
,
&
pathname
,
&
Filetype
,
&
fob
))
if
(
!
PyArg_ParseTuple
(
args
,
"ssO!"
,
&
name
,
&
pathname
,
&
PyFile_Type
,
&
fob
))
return
NULL
;
fp
=
get_file
(
pathname
,
fob
,
"rb"
);
if
(
fp
==
NULL
)
...
...
@@ -940,35 +950,37 @@ imp_load_compiled(self, args)
return
m
;
}
static
o
bject
*
static
PyO
bject
*
imp_load_dynamic
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
char
*
name
;
char
*
pathname
;
o
bject
*
fob
=
NULL
;
o
bject
*
m
;
PyO
bject
*
fob
=
NULL
;
PyO
bject
*
m
;
FILE
*
fp
=
NULL
;
if
(
!
newgetargs
(
args
,
"ss|O!"
,
&
name
,
&
pathname
,
&
Filetype
,
&
fob
))
if
(
!
PyArg_ParseTuple
(
args
,
"ss|O!"
,
&
name
,
&
pathname
,
&
PyFile_Type
,
&
fob
))
return
NULL
;
if
(
fob
)
fp
=
get_file
(
pathname
,
fob
,
"r"
);
m
=
load_dynamic_m
odule
(
name
,
pathname
,
fp
);
m
=
_PyImport_LoadDynamicM
odule
(
name
,
pathname
,
fp
);
return
m
;
}
static
o
bject
*
static
PyO
bject
*
imp_load_source
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
char
*
name
;
char
*
pathname
;
o
bject
*
fob
=
NULL
;
o
bject
*
m
;
PyO
bject
*
fob
=
NULL
;
PyO
bject
*
m
;
FILE
*
fp
;
if
(
!
newgetargs
(
args
,
"ssO!"
,
&
name
,
&
pathname
,
&
Filetype
,
&
fob
))
if
(
!
PyArg_ParseTuple
(
args
,
"ssO!"
,
&
name
,
&
pathname
,
&
PyFile_Type
,
&
fob
))
return
NULL
;
fp
=
get_file
(
pathname
,
fob
,
"r"
);
if
(
fp
==
NULL
)
...
...
@@ -978,34 +990,34 @@ imp_load_source(self, args)
}
#ifdef macintosh
static
o
bject
*
static
PyO
bject
*
imp_load_resource
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
char
*
name
;
char
*
pathname
;
o
bject
*
m
;
PyO
bject
*
m
;
if
(
!
newgetargs
(
args
,
"ss"
,
&
name
,
&
pathname
))
if
(
!
PyArg_ParseTuple
(
args
,
"ss"
,
&
name
,
&
pathname
))
return
NULL
;
m
=
PyMac_LoadResourceModule
(
name
,
pathname
);
return
m
;
}
#endif
/* macintosh */
static
o
bject
*
static
PyO
bject
*
imp_new_module
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
char
*
name
;
if
(
!
newgetargs
(
args
,
"s"
,
&
name
))
if
(
!
PyArg_ParseTuple
(
args
,
"s"
,
&
name
))
return
NULL
;
return
newmoduleobject
(
name
);
return
PyModule_New
(
name
);
}
static
struct
methodlist
imp_methods
[]
=
{
static
PyMethodDef
imp_methods
[]
=
{
{
"get_frozen_object"
,
imp_get_frozen_object
,
1
},
{
"get_magic"
,
imp_get_magic
,
1
},
{
"get_suffixes"
,
imp_get_suffixes
,
1
},
...
...
@@ -1027,34 +1039,34 @@ static struct methodlist imp_methods[] = {
void
initimp
()
{
o
bject
*
m
,
*
d
,
*
v
;
PyO
bject
*
m
,
*
d
,
*
v
;
m
=
initm
odule
(
"imp"
,
imp_methods
);
d
=
getmoduled
ict
(
m
);
m
=
Py_InitM
odule
(
"imp"
,
imp_methods
);
d
=
PyModule_GetD
ict
(
m
);
v
=
newintobject
(
SEARCH_ERROR
);
dictinsert
(
d
,
"SEARCH_ERROR"
,
v
);
XDECREF
(
v
);
v
=
PyInt_FromLong
(
SEARCH_ERROR
);
PyDict_SetItemString
(
d
,
"SEARCH_ERROR"
,
v
);
Py_
XDECREF
(
v
);
v
=
newintobject
(
PY_SOURCE
);
dictinsert
(
d
,
"PY_SOURCE"
,
v
);
XDECREF
(
v
);
v
=
PyInt_FromLong
(
PY_SOURCE
);
PyDict_SetItemString
(
d
,
"PY_SOURCE"
,
v
);
Py_
XDECREF
(
v
);
v
=
newintobject
(
PY_COMPILED
);
dictinsert
(
d
,
"PY_COMPILED"
,
v
);
XDECREF
(
v
);
v
=
PyInt_FromLong
(
PY_COMPILED
);
PyDict_SetItemString
(
d
,
"PY_COMPILED"
,
v
);
Py_
XDECREF
(
v
);
v
=
newintobject
(
C_EXTENSION
);
dictinsert
(
d
,
"C_EXTENSION"
,
v
);
XDECREF
(
v
);
v
=
PyInt_FromLong
(
C_EXTENSION
);
PyDict_SetItemString
(
d
,
"C_EXTENSION"
,
v
);
Py_
XDECREF
(
v
);
#ifdef macintosh
v
=
newintobject
(
PY_RESOURCE
);
dictinsert
(
d
,
"PY_RESOURCE"
,
v
);
XDECREF
(
v
);
v
=
PyInt_FromLong
(
PY_RESOURCE
);
PyDict_SetItemString
(
d
,
"PY_RESOURCE"
,
v
);
Py_
XDECREF
(
v
);
#endif
if
(
err_o
ccurred
())
fatal
(
"imp module initialization failed"
);
if
(
PyErr_O
ccurred
())
Py_FatalError
(
"imp module initialization failed"
);
}
Python/importdl.c
View file @
5574f2d0
...
...
@@ -32,7 +32,7 @@ PERFORMANCE OF THIS SOFTWARE.
/* Support for dynamic loading of extension modules */
/* If no dynamic linking is supported, this file still generates some code! */
#include "
allobjects
.h"
#include "
Python
.h"
#include "osdefs.h"
#include "importdl.h"
...
...
@@ -167,7 +167,7 @@ typedef void (*dl_funcptr)();
#ifdef USE_MAC_DYNAMIC_LOADING
#include <Aliases.h>
#include <CodeFragments.h>
#ifdef SYMANTEC__CFM68K__
/* Really
just
an older version of Universal Headers */
#ifdef SYMANTEC__CFM68K__
/* Really an older version of Universal Headers */
#define CFragConnectionID ConnectionID
#define kLoadCFrag 0x01
#endif
...
...
@@ -184,7 +184,7 @@ typedef void (*dl_funcptr)();
#endif
#endif
/* USE_RLD */
extern
char
*
getprogramn
ame
();
extern
char
*
Py_GetProgramN
ame
();
#ifndef FUNCNAME_PATTERN
#if defined(__hp9000s300) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__BORLANDC__)
...
...
@@ -207,9 +207,9 @@ extern char *getprogramname();
#endif
/* Pass it on to import.c */
int
import_maxsuffixs
ize
=
MAXSUFFIXSIZE
;
int
_PyImport_MaxSuffixS
ize
=
MAXSUFFIXSIZE
;
struct
filedescr
import_f
iletab
[]
=
{
struct
filedescr
_PyImport_F
iletab
[]
=
{
#ifdef SHORT_EXT
{
SHORT_EXT
,
"rb"
,
C_EXTENSION
},
#endif
/* !SHORT_EXT */
...
...
@@ -225,17 +225,18 @@ struct filedescr import_filetab[] = {
#undef DYNAMIC_LINK
#endif
o
bject
*
load_dynamic_m
odule
(
name
,
pathname
,
fp
)
PyO
bject
*
_PyImport_LoadDynamicM
odule
(
name
,
pathname
,
fp
)
char
*
name
;
char
*
pathname
;
FILE
*
fp
;
{
#ifndef DYNAMIC_LINK
err_setstr
(
ImportError
,
"dynamically linked modules not supported"
);
PyErr_SetString
(
PyExc_ImportError
,
"dynamically linked modules not supported"
);
return
NULL
;
#else
o
bject
*
m
,
*
d
,
*
s
;
PyO
bject
*
m
,
*
d
,
*
s
;
char
funcname
[
258
];
dl_funcptr
p
=
NULL
;
#ifdef USE_SHLIB
...
...
@@ -274,11 +275,12 @@ load_dynamic_module(name, pathname, fp)
#endif
/* USE_SHLIB */
#ifdef USE_MAC_DYNAMIC_LOADING
/*
** Dynamic loading of CFM shared libraries on the Mac.
** The code has become more convoluted than it was, because we want to be able
** to put multiple modules in a single file. For this reason, we have to determine
** the fragment name, and we cannot use the library entry point but we have to locate
** the correct init routine "by hand".
** Dynamic loading of CFM shared libraries on the Mac. The
** code has become more convoluted than it was, because we
** want to be able to put multiple modules in a single
** file. For this reason, we have to determine the fragment
** name, and we cannot use the library entry point but we have
** to locate the correct init routine "by hand".
*/
{
FSSpec
libspec
;
...
...
@@ -297,30 +299,35 @@ load_dynamic_module(name, pathname, fp)
err
=
ResolveAliasFile
(
&
libspec
,
1
,
&
isfolder
,
&
didsomething
);
if
(
err
)
{
sprintf
(
buf
,
"%s: %s"
,
pathname
,
PyMac_StrError
(
err
));
err_setstr
(
ImportError
,
buf
);
PyErr_SetString
(
PyExc_
ImportError
,
buf
);
return
NULL
;
}
/* Next, determine the fragment name, by stripping '.slb' and 'module' */
/* Next, determine the fragment name,
by stripping '.slb' and 'module' */
memcpy
(
fragname
+
1
,
libspec
.
name
+
1
,
libspec
.
name
[
0
]);
fragname
[
0
]
=
libspec
.
name
[
0
];
if
(
strncmp
((
char
*
)(
fragname
+
1
+
fragname
[
0
]
-
4
),
".slb"
,
4
)
==
0
)
if
(
strncmp
((
char
*
)(
fragname
+
1
+
fragname
[
0
]
-
4
),
".slb"
,
4
)
==
0
)
fragname
[
0
]
-=
4
;
if
(
strncmp
((
char
*
)(
fragname
+
1
+
fragname
[
0
]
-
6
),
"module"
,
6
)
==
0
)
if
(
strncmp
((
char
*
)(
fragname
+
1
+
fragname
[
0
]
-
6
),
"module"
,
6
)
==
0
)
fragname
[
0
]
-=
6
;
/* Load the fragment (or return the connID if it is already loaded */
/* Load the fragment
(or return the connID if it is already loaded */
err
=
GetDiskFragment
(
&
libspec
,
0
,
0
,
fragname
,
kLoadCFrag
,
&
connID
,
&
mainAddr
,
errMessage
);
if
(
err
)
{
sprintf
(
buf
,
"%.*s: %s"
,
errMessage
[
0
],
errMessage
+
1
,
PyMac_StrError
(
err
));
err_setstr
(
ImportError
,
buf
);
sprintf
(
buf
,
"%.*s: %s"
,
errMessage
[
0
],
errMessage
+
1
,
PyMac_StrError
(
err
));
PyErr_SetString
(
PyExc_ImportError
,
buf
);
return
NULL
;
}
/* Locate the address of the correct init function */
err
=
FindSymbol
(
connID
,
Pstring
(
funcname
),
&
symAddr
,
&
class
);
if
(
err
)
{
sprintf
(
buf
,
"%s: %s"
,
funcname
,
PyMac_StrError
(
err
));
err_setstr
(
ImportError
,
buf
);
PyErr_SetString
(
PyExc_
ImportError
,
buf
);
return
NULL
;
}
p
=
(
dl_funcptr
)
symAddr
;
...
...
@@ -334,12 +341,12 @@ load_dynamic_module(name, pathname, fp)
void
*
handle
=
dlopen
(
pathname
,
RTLD_NOW
);
#else
void
*
handle
;
if
(
verbose
)
if
(
Py_VerboseFlag
)
printf
(
"dlopen(
\"
%s
\"
, %d);
\n
"
,
pathname
,
RTLD_LAZY
);
handle
=
dlopen
(
pathname
,
RTLD_LAZY
);
#endif
/* RTLD_NOW */
if
(
handle
==
NULL
)
{
err_setstr
(
ImportError
,
dlerror
());
PyErr_SetString
(
PyExc_
ImportError
,
dlerror
());
return
NULL
;
}
if
(
fp
!=
NULL
&&
nhandles
<
128
)
...
...
@@ -382,35 +389,44 @@ load_dynamic_module(name, pathname, fp)
unsigned
int
errorCode
;
/* Get an error string from Win32 error code */
char
theInfo
[
256
];
/* Pointer to error text from system */
int
theLength
;
/* Length of error text */
char
theInfo
[
256
];
/* Pointer to error text
from system */
int
theLength
;
/* Length of error text */
errorCode
=
GetLastError
();
theLength
=
FormatMessage
(
FORMAT_MESSAGE_FROM_SYSTEM
,
/* flags */
NULL
,
/* message source */
errorCode
,
/* the message (error) ID */
0
,
/* default language environment */
(
LPTSTR
)
theInfo
,
/* the buffer */
sizeof
(
theInfo
),
/* the buffer size */
NULL
);
/* no additional format args. */
/* Problem: could not get the error message. This should not happen if called correctly. */
theLength
=
FormatMessage
(
FORMAT_MESSAGE_FROM_SYSTEM
,
/* flags */
NULL
,
/* message source */
errorCode
,
/* the message (error) ID */
0
,
/* default language environment */
(
LPTSTR
)
theInfo
,
/* the buffer */
sizeof
(
theInfo
),
/* the buffer size */
NULL
);
/* no additional format args. */
/* Problem: could not get the error message.
This should not happen if called correctly. */
if
(
theLength
==
0
)
{
sprintf
(
errBuf
,
"DLL load failed with error code %d"
,
errorCode
);
sprintf
(
errBuf
,
"DLL load failed with error code %d"
,
errorCode
);
}
else
{
int
len
;
/* For some reason a \r\n is appended to the text */
if
(
theLength
>=
2
&&
theInfo
[
theLength
-
2
]
==
'\r'
&&
theInfo
[
theLength
-
1
]
==
'\n'
)
{
/* For some reason a \r\n
is appended to the text */
if
(
theLength
>=
2
&&
theInfo
[
theLength
-
2
]
==
'\r'
&&
theInfo
[
theLength
-
1
]
==
'\n'
)
{
theLength
-=
2
;
theInfo
[
theLength
]
=
'\0'
;
}
strcpy
(
errBuf
,
"DLL load failed: "
);
len
=
strlen
(
errBuf
);
strncpy
(
errBuf
+
len
,
theInfo
,
sizeof
(
errBuf
)
-
len
);
strncpy
(
errBuf
+
len
,
theInfo
,
sizeof
(
errBuf
)
-
len
);
errBuf
[
sizeof
(
errBuf
)
-
1
]
=
'\0'
;
}
err_setstr
(
ImportError
,
errBuf
);
PyErr_SetString
(
PyExc_
ImportError
,
errBuf
);
return
NULL
;
}
p
=
GetProcAddress
(
hDLL
,
funcname
);
...
...
@@ -422,15 +438,16 @@ load_dynamic_module(name, pathname, fp)
hDLL
=
LoadLibrary
(
pathname
);
if
(
hDLL
<
HINSTANCE_ERROR
){
char
errBuf
[
256
];
sprintf
(
errBuf
,
"DLL load failed with error code %d"
,
hDLL
);
err_setstr
(
ImportError
,
errBuf
);
sprintf
(
errBuf
,
"DLL load failed with error code %d"
,
hDLL
);
PyErr_SetString
(
PyExc_ImportError
,
errBuf
);
return
NULL
;
}
p
=
GetProcAddress
(
hDLL
,
funcname
);
}
#endif
/* MS_WIN16 */
#ifdef USE_DL
p
=
dl_loadmod
(
getprogramn
ame
(),
pathname
,
funcname
);
p
=
dl_loadmod
(
Py_GetProgramN
ame
(),
pathname
,
funcname
);
#endif
/* USE_DL */
#ifdef USE_RLD
{
...
...
@@ -455,7 +472,7 @@ load_dynamic_module(name, pathname, fp)
NXGetMemoryBuffer
(
errorStream
,
&
streamBuf
,
&
len
,
&
maxLen
);
err_setstr
(
ImportError
,
streamBuf
);
PyErr_SetString
(
PyExc_
ImportError
,
streamBuf
);
}
if
(
ret
&&
rld_lookup
(
errorStream
,
funcname
,
&
ptr
))
...
...
@@ -473,25 +490,26 @@ load_dynamic_module(name, pathname, fp)
int
flags
;
flags
=
BIND_FIRST
|
BIND_DEFERRED
;
if
(
verbose
)
if
(
Py_VerboseFlag
)
{
flags
=
DYNAMIC_PATH
|
BIND_FIRST
|
BIND_IMMEDIATE
|
BIND_NONFATAL
|
BIND_VERBOSE
;
flags
=
DYNAMIC_PATH
|
BIND_FIRST
|
BIND_IMMEDIATE
|
BIND_NONFATAL
|
BIND_VERBOSE
;
printf
(
"shl_load %s
\n
"
,
pathname
);
}
lib
=
shl_load
(
pathname
,
flags
,
0
);
if
(
lib
==
NULL
)
{
char
buf
[
256
];
if
(
verbose
)
if
(
Py_VerboseFlag
)
perror
(
pathname
);
sprintf
(
buf
,
"Failed to load %.200s"
,
pathname
);
err_setstr
(
ImportError
,
buf
);
PyErr_SetString
(
PyExc_
ImportError
,
buf
);
return
NULL
;
}
if
(
verbose
)
if
(
Py_VerboseFlag
)
printf
(
"shl_findsym %s
\n
"
,
funcname
);
shl_findsym
(
&
lib
,
funcname
,
TYPE_UNDEFINED
,
(
void
*
)
&
p
);
if
(
p
==
NULL
&&
verbose
)
if
(
p
==
NULL
&&
Py_VerboseFlag
)
perror
(
funcname
);
}
#endif
/* hpux */
...
...
@@ -499,31 +517,31 @@ load_dynamic_module(name, pathname, fp)
got_it:
#endif
if
(
p
==
NULL
)
{
err_setstr
(
ImportError
,
PyErr_SetString
(
PyExc_
ImportError
,
"dynamic module does not define init function"
);
return
NULL
;
}
(
*
p
)();
/* XXX Need check for err_occurred() here */
m
=
dictlookup
(
import_modules
,
name
);
m
=
PyDict_GetItemString
(
import_modules
,
name
);
if
(
m
==
NULL
)
{
if
(
err_o
ccurred
()
==
NULL
)
err_setstr
(
SystemError
,
if
(
PyErr_O
ccurred
()
==
NULL
)
PyErr_SetString
(
PyExc_
SystemError
,
"dynamic module not initialized properly"
);
return
NULL
;
}
/* Remember the filename as the __file__ attribute */
d
=
getmoduled
ict
(
m
);
s
=
newstringobject
(
pathname
);
if
(
s
==
NULL
||
dictinsert
(
d
,
"__file__"
,
s
)
!=
0
)
err_c
lear
();
/* Not important enough to report */
XDECREF
(
s
);
if
(
verbose
)
d
=
PyModule_GetD
ict
(
m
);
s
=
PyString_FromString
(
pathname
);
if
(
s
==
NULL
||
PyDict_SetItemString
(
d
,
"__file__"
,
s
)
!=
0
)
PyErr_C
lear
();
/* Not important enough to report */
Py_
XDECREF
(
s
);
if
(
Py_VerboseFlag
)
fprintf
(
stderr
,
"import %s # dynamically loaded from %s
\n
"
,
name
,
pathname
);
INCREF
(
m
);
Py_
INCREF
(
m
);
return
m
;
#endif
/* DYNAMIC_LINK */
}
...
...
@@ -555,7 +573,7 @@ aix_getoldmodules(modlistptr)
-- Get the list of loaded modules into ld_info structures.
*/
if
((
ldibuf
=
malloc
(
bufsize
))
==
NULL
)
{
err_setstr
(
ImportError
,
strerror
(
errno
));
PyErr_SetString
(
PyExc_
ImportError
,
strerror
(
errno
));
return
-
1
;
}
while
((
errflag
=
loadquery
(
L_GETINFO
,
ldibuf
,
bufsize
))
==
-
1
...
...
@@ -563,12 +581,12 @@ aix_getoldmodules(modlistptr)
free
(
ldibuf
);
bufsize
+=
1024
;
if
((
ldibuf
=
malloc
(
bufsize
))
==
NULL
)
{
err_setstr
(
ImportError
,
strerror
(
errno
));
PyErr_SetString
(
PyExc_
ImportError
,
strerror
(
errno
));
return
-
1
;
}
}
if
(
errflag
==
-
1
)
{
err_setstr
(
ImportError
,
strerror
(
errno
));
PyErr_SetString
(
PyExc_
ImportError
,
strerror
(
errno
));
return
-
1
;
}
/*
...
...
@@ -578,7 +596,7 @@ aix_getoldmodules(modlistptr)
prevmodptr
=
NULL
;
do
{
if
((
modptr
=
(
ModulePtr
)
malloc
(
sizeof
(
Module
)))
==
NULL
)
{
err_setstr
(
ImportError
,
strerror
(
errno
));
PyErr_SetString
(
PyExc_
ImportError
,
strerror
(
errno
));
while
(
*
modlistptr
)
{
modptr
=
(
ModulePtr
)
*
modlistptr
;
*
modlistptr
=
(
void
*
)
modptr
->
next
;
...
...
@@ -662,7 +680,7 @@ aix_loaderror(pathname)
ERRBUF_APPEND
(
"
\n
"
);
}
errbuf
[
strlen
(
errbuf
)
-
1
]
=
'\0'
;
/* trim off last newline */
err_setstr
(
ImportError
,
errbuf
);
PyErr_SetString
(
PyExc_
ImportError
,
errbuf
);
return
;
}
...
...
Python/importdl.h
View file @
5574f2d0
...
...
@@ -40,10 +40,11 @@ extern struct filedescr {
char
*
suffix
;
char
*
mode
;
enum
filetype
type
;
}
import_f
iletab
[];
}
_PyImport_F
iletab
[];
extern
o
bject
*
import_modules
;
extern
PyO
bject
*
import_modules
;
extern
object
*
load_dynamic_module
PROTO
((
char
*
name
,
char
*
pathname
,
FILE
*
));
extern
PyObject
*
_PyImport_LoadDynamicModule
Py_PROTO
((
char
*
name
,
char
*
pathname
,
FILE
*
));
extern
int
import_maxsuffixs
ize
;
extern
int
_PyImport_MaxSuffixS
ize
;
Python/marshal.c
View file @
5574f2d0
...
...
@@ -34,14 +34,11 @@ PERFORMANCE OF THIS SOFTWARE.
a true persistent storage facility would be much harder, since
it would have to take circular links and sharing into account. */
#include "allobjects.h"
#include "modsupport.h"
#include "Python.h"
#include "longintrepr.h"
#include "compile.h"
#include "marshal.h"
#include <errno.h>
#define TYPE_NULL '0'
#define TYPE_NONE 'N'
#define TYPE_ELLIPSIS '.'
...
...
@@ -61,7 +58,7 @@ typedef struct {
FILE
*
fp
;
int
error
;
/* If fp == NULL, the following are valid: */
o
bject
*
str
;
PyO
bject
*
str
;
char
*
ptr
;
char
*
end
;
}
WFILE
;
...
...
@@ -78,14 +75,15 @@ w_more(c, p)
int
size
,
newsize
;
if
(
p
->
str
==
NULL
)
return
;
/* An error already occurred */
size
=
getstrings
ize
(
p
->
str
);
size
=
PyString_S
ize
(
p
->
str
);
newsize
=
size
+
1024
;
if
(
resizestring
(
&
p
->
str
,
newsize
)
!=
0
)
{
if
(
_PyString_Resize
(
&
p
->
str
,
newsize
)
!=
0
)
{
p
->
ptr
=
p
->
end
=
NULL
;
}
else
{
p
->
ptr
=
GETSTRINGVALUE
((
stringobject
*
)
p
->
str
)
+
size
;
p
->
end
=
GETSTRINGVALUE
((
stringobject
*
)
p
->
str
)
+
newsize
;
p
->
ptr
=
PyString_AS_STRING
((
PyStringObject
*
)
p
->
str
)
+
size
;
p
->
end
=
PyString_AS_STRING
((
PyStringObject
*
)
p
->
str
)
+
newsize
;
*
p
->
ptr
++
=
c
;
}
}
...
...
@@ -140,19 +138,19 @@ w_long64(x, p)
static
void
w_object
(
v
,
p
)
o
bject
*
v
;
PyO
bject
*
v
;
WFILE
*
p
;
{
int
i
,
n
;
if
(
v
==
NULL
)
w_byte
(
TYPE_NULL
,
p
);
else
if
(
v
==
None
)
else
if
(
v
==
Py_
None
)
w_byte
(
TYPE_NONE
,
p
);
else
if
(
v
==
Py_Ellipsis
)
w_byte
(
TYPE_ELLIPSIS
,
p
);
else
if
(
is_intobject
(
v
))
{
long
x
=
GETINTVALUE
((
into
bject
*
)
v
);
else
if
(
PyInt_Check
(
v
))
{
long
x
=
PyInt_AS_LONG
((
PyIntO
bject
*
)
v
);
#if SIZEOF_LONG > 4
long
y
=
x
>>
31
;
if
(
y
&&
y
!=
-
1
)
{
...
...
@@ -166,8 +164,8 @@ w_object(v, p)
w_long
(
x
,
p
);
}
}
else
if
(
is_longobject
(
v
))
{
longobject
*
ob
=
(
longo
bject
*
)
v
;
else
if
(
PyLong_Check
(
v
))
{
PyLongObject
*
ob
=
(
PyLongO
bject
*
)
v
;
w_byte
(
TYPE_LONG
,
p
);
n
=
ob
->
ob_size
;
w_long
((
long
)
n
,
p
);
...
...
@@ -176,77 +174,81 @@ w_object(v, p)
for
(
i
=
0
;
i
<
n
;
i
++
)
w_short
(
ob
->
ob_digit
[
i
],
p
);
}
else
if
(
is_floatobject
(
v
))
{
extern
void
float_buf_repr
PROTO
((
char
*
,
floatobject
*
));
else
if
(
PyFloat_Check
(
v
))
{
extern
void
PyFloat_AsString
Py_PROTO
((
char
*
,
PyFloatObject
*
));
char
buf
[
256
];
/* Plenty to format any double */
float_buf_repr
(
buf
,
(
floato
bject
*
)
v
);
PyFloat_AsString
(
buf
,
(
PyFloatO
bject
*
)
v
);
n
=
strlen
(
buf
);
w_byte
(
TYPE_FLOAT
,
p
);
w_byte
(
n
,
p
);
w_string
(
buf
,
n
,
p
);
}
#ifndef WITHOUT_COMPLEX
else
if
(
is_complexobject
(
v
))
{
extern
void
float_buf_repr
PROTO
((
char
*
,
floatobject
*
));
else
if
(
PyComplex_Check
(
v
))
{
extern
void
PyFloat_AsString
Py_PROTO
((
char
*
,
PyFloatObject
*
));
char
buf
[
256
];
/* Plenty to format any double */
floato
bject
*
temp
;
PyFloatO
bject
*
temp
;
w_byte
(
TYPE_COMPLEX
,
p
);
temp
=
(
floatobject
*
)
newfloatobject
(
PyComplex_RealAsDouble
(
v
));
float_buf_repr
(
buf
,
temp
);
DECREF
(
temp
);
temp
=
(
PyFloatObject
*
)
PyFloat_FromDouble
(
PyComplex_RealAsDouble
(
v
));
PyFloat_AsString
(
buf
,
temp
);
Py_DECREF
(
temp
);
n
=
strlen
(
buf
);
w_byte
(
n
,
p
);
w_string
(
buf
,
n
,
p
);
temp
=
(
floatobject
*
)
newfloatobject
(
PyComplex_ImagAsDouble
(
v
));
float_buf_repr
(
buf
,
temp
);
DECREF
(
temp
);
temp
=
(
PyFloatObject
*
)
PyFloat_FromDouble
(
PyComplex_ImagAsDouble
(
v
));
PyFloat_AsString
(
buf
,
temp
);
Py_DECREF
(
temp
);
n
=
strlen
(
buf
);
w_byte
(
n
,
p
);
w_string
(
buf
,
n
,
p
);
}
#endif
else
if
(
is_stringobject
(
v
))
{
else
if
(
PyString_Check
(
v
))
{
w_byte
(
TYPE_STRING
,
p
);
n
=
getstrings
ize
(
v
);
n
=
PyString_S
ize
(
v
);
w_long
((
long
)
n
,
p
);
w_string
(
getstringvalue
(
v
),
n
,
p
);
w_string
(
PyString_AsString
(
v
),
n
,
p
);
}
else
if
(
is_tupleobject
(
v
))
{
else
if
(
PyTuple_Check
(
v
))
{
w_byte
(
TYPE_TUPLE
,
p
);
n
=
gettuples
ize
(
v
);
n
=
PyTuple_S
ize
(
v
);
w_long
((
long
)
n
,
p
);
for
(
i
=
0
;
i
<
n
;
i
++
)
{
w_object
(
GETTUPLE
ITEM
(
v
,
i
),
p
);
w_object
(
PyTuple_GET_
ITEM
(
v
,
i
),
p
);
}
}
else
if
(
is_listobject
(
v
))
{
else
if
(
PyList_Check
(
v
))
{
w_byte
(
TYPE_LIST
,
p
);
n
=
getlists
ize
(
v
);
n
=
PyList_S
ize
(
v
);
w_long
((
long
)
n
,
p
);
for
(
i
=
0
;
i
<
n
;
i
++
)
{
w_object
(
getlisti
tem
(
v
,
i
),
p
);
w_object
(
PyList_GetI
tem
(
v
,
i
),
p
);
}
}
else
if
(
is_dictobject
(
v
))
{
else
if
(
PyDict_Check
(
v
))
{
int
pos
;
o
bject
*
key
,
*
value
;
PyO
bject
*
key
,
*
value
;
w_byte
(
TYPE_DICT
,
p
);
/* This one is NULL object terminated! */
pos
=
0
;
while
(
mappinggetn
ext
(
v
,
&
pos
,
&
key
,
&
value
))
{
while
(
PyDict_N
ext
(
v
,
&
pos
,
&
key
,
&
value
))
{
w_object
(
key
,
p
);
w_object
(
value
,
p
);
}
w_object
((
o
bject
*
)
NULL
,
p
);
w_object
((
PyO
bject
*
)
NULL
,
p
);
}
else
if
(
is_codeobject
(
v
))
{
codeobject
*
co
=
(
codeo
bject
*
)
v
;
else
if
(
PyCode_Check
(
v
))
{
PyCodeObject
*
co
=
(
PyCodeO
bject
*
)
v
;
w_byte
(
TYPE_CODE
,
p
);
w_short
(
co
->
co_argcount
,
p
);
w_short
(
co
->
co_nlocals
,
p
);
w_short
(
co
->
co_stacksize
,
p
);
w_short
(
co
->
co_flags
,
p
);
w_object
((
o
bject
*
)
co
->
co_code
,
p
);
w_object
((
PyO
bject
*
)
co
->
co_code
,
p
);
w_object
(
co
->
co_consts
,
p
);
w_object
(
co
->
co_names
,
p
);
w_object
(
co
->
co_varnames
,
p
);
...
...
@@ -262,7 +264,7 @@ w_object(v, p)
}
void
wr_long
(
x
,
fp
)
PyMarshal_WriteLongToFile
(
x
,
fp
)
long
x
;
FILE
*
fp
;
{
...
...
@@ -273,8 +275,8 @@ wr_long(x, fp)
}
void
wr_object
(
x
,
fp
)
o
bject
*
x
;
PyMarshal_WriteObjectToFile
(
x
,
fp
)
PyO
bject
*
x
;
FILE
*
fp
;
{
WFILE
wf
;
...
...
@@ -351,10 +353,10 @@ r_long64(p)
x
=
(
x
&
0xFFFFFFFF
)
|
(
r_long
(
p
)
<<
32
);
#else
if
(
r_long
(
p
)
!=
0
)
{
object
*
f
=
sysge
t
(
"stderr"
);
err_c
lear
();
PyObject
*
f
=
PySys_GetObjec
t
(
"stderr"
);
PyErr_C
lear
();
if
(
f
!=
NULL
)
writes
tring
(
PyFile_WriteS
tring
(
"Warning: un-marshal 64-bit int in 32-bit mode
\n
"
,
f
);
}
...
...
@@ -362,60 +364,61 @@ r_long64(p)
return
x
;
}
static
o
bject
*
static
PyO
bject
*
r_object
(
p
)
RFILE
*
p
;
{
o
bject
*
v
,
*
v2
;
PyO
bject
*
v
,
*
v2
;
long
i
,
n
;
int
type
=
r_byte
(
p
);
switch
(
type
)
{
case
EOF
:
err_setstr
(
EOFError
,
"EOF read where object expected"
);
PyErr_SetString
(
PyExc_EOFError
,
"EOF read where object expected"
);
return
NULL
;
case
TYPE_NULL
:
return
NULL
;
case
TYPE_NONE
:
INCREF
(
None
);
return
None
;
Py_INCREF
(
Py_
None
);
return
Py_
None
;
case
TYPE_ELLIPSIS
:
INCREF
(
Py_Ellipsis
);
Py_
INCREF
(
Py_Ellipsis
);
return
Py_Ellipsis
;
case
TYPE_INT
:
return
newintobject
(
r_long
(
p
));
return
PyInt_FromLong
(
r_long
(
p
));
case
TYPE_INT64
:
return
newintobject
(
r_long64
(
p
));
return
PyInt_FromLong
(
r_long64
(
p
));
case
TYPE_LONG
:
{
int
size
;
longo
bject
*
ob
;
PyLongO
bject
*
ob
;
n
=
r_long
(
p
);
size
=
n
<
0
?
-
n
:
n
;
ob
=
alloclongobject
(
size
);
ob
=
_PyLong_New
(
size
);
if
(
ob
==
NULL
)
return
NULL
;
ob
->
ob_size
=
n
;
for
(
i
=
0
;
i
<
size
;
i
++
)
ob
->
ob_digit
[
i
]
=
r_short
(
p
);
return
(
o
bject
*
)
ob
;
return
(
PyO
bject
*
)
ob
;
}
case
TYPE_FLOAT
:
{
extern
double
atof
PROTO
((
const
char
*
));
extern
double
atof
P
y_P
ROTO
((
const
char
*
));
char
buf
[
256
];
double
dx
;
n
=
r_byte
(
p
);
if
(
r_string
(
buf
,
(
int
)
n
,
p
)
!=
n
)
{
err_setstr
(
EOFError
,
PyErr_SetString
(
PyExc_
EOFError
,
"EOF read where object expected"
);
return
NULL
;
}
...
...
@@ -423,18 +426,18 @@ r_object(p)
PyFPE_START_PROTECT
(
"atof"
,
return
0
)
dx
=
atof
(
buf
);
PyFPE_END_PROTECT
(
dx
)
return
newfloatobject
(
dx
);
return
PyFloat_FromDouble
(
dx
);
}
#ifndef WITHOUT_COMPLEX
case
TYPE_COMPLEX
:
{
extern
double
atof
PROTO
((
const
char
*
));
extern
double
atof
P
y_P
ROTO
((
const
char
*
));
char
buf
[
256
];
Py_complex
c
;
n
=
r_byte
(
p
);
if
(
r_string
(
buf
,
(
int
)
n
,
p
)
!=
n
)
{
err_setstr
(
EOFError
,
PyErr_SetString
(
PyExc_
EOFError
,
"EOF read where object expected"
);
return
NULL
;
}
...
...
@@ -444,7 +447,7 @@ r_object(p)
PyFPE_END_PROTECT
(
c
)
n
=
r_byte
(
p
);
if
(
r_string
(
buf
,
(
int
)
n
,
p
)
!=
n
)
{
err_setstr
(
EOFError
,
PyErr_SetString
(
PyExc_
EOFError
,
"EOF read where object expected"
);
return
NULL
;
}
...
...
@@ -452,18 +455,18 @@ r_object(p)
PyFPE_START_PROTECT
(
"atof"
,
return
0
)
c
.
imag
=
atof
(
buf
);
PyFPE_END_PROTECT
(
c
)
return
newcomplexobject
(
c
);
return
PyComplex_FromCComplex
(
c
);
}
#endif
case
TYPE_STRING
:
n
=
r_long
(
p
);
v
=
newsizedstringobject
((
char
*
)
NULL
,
n
);
v
=
PyString_FromStringAndSize
((
char
*
)
NULL
,
n
);
if
(
v
!=
NULL
)
{
if
(
r_string
(
getstringvalue
(
v
),
(
int
)
n
,
p
)
!=
n
)
{
DECREF
(
v
);
if
(
r_string
(
PyString_AsString
(
v
),
(
int
)
n
,
p
)
!=
n
)
{
Py_
DECREF
(
v
);
v
=
NULL
;
err_setstr
(
EOFError
,
PyErr_SetString
(
PyExc_
EOFError
,
"EOF read where object expected"
);
}
}
...
...
@@ -471,50 +474,50 @@ r_object(p)
case
TYPE_TUPLE
:
n
=
r_long
(
p
);
v
=
newtupleobject
((
int
)
n
);
v
=
PyTuple_New
((
int
)
n
);
if
(
v
==
NULL
)
return
v
;
for
(
i
=
0
;
i
<
n
;
i
++
)
{
v2
=
r_object
(
p
);
if
(
v2
==
NULL
)
{
DECREF
(
v
);
Py_
DECREF
(
v
);
v
=
NULL
;
break
;
}
SETTUPLE
ITEM
(
v
,
(
int
)
i
,
v2
);
PyTuple_SET_
ITEM
(
v
,
(
int
)
i
,
v2
);
}
return
v
;
case
TYPE_LIST
:
n
=
r_long
(
p
);
v
=
newlistobject
((
int
)
n
);
v
=
PyList_New
((
int
)
n
);
if
(
v
==
NULL
)
return
v
;
for
(
i
=
0
;
i
<
n
;
i
++
)
{
v2
=
r_object
(
p
);
if
(
v2
==
NULL
)
{
DECREF
(
v
);
Py_
DECREF
(
v
);
v
=
NULL
;
break
;
}
setlisti
tem
(
v
,
(
int
)
i
,
v2
);
PyList_SetI
tem
(
v
,
(
int
)
i
,
v2
);
}
return
v
;
case
TYPE_DICT
:
v
=
newdictobject
();
v
=
PyDict_New
();
if
(
v
==
NULL
)
return
NULL
;
for
(;;)
{
o
bject
*
key
,
*
val
;
PyO
bject
*
key
,
*
val
;
key
=
r_object
(
p
);
if
(
key
==
NULL
)
break
;
/* XXX Assume TYPE_NULL, not an error */
val
=
r_object
(
p
);
if
(
val
!=
NULL
)
dict2insert
(
v
,
key
,
val
);
DECREF
(
key
);
XDECREF
(
val
);
PyDict_SetItem
(
v
,
key
,
val
);
Py_
DECREF
(
key
);
Py_
XDECREF
(
val
);
}
return
v
;
...
...
@@ -524,14 +527,14 @@ r_object(p)
int
nlocals
=
r_short
(
p
);
int
stacksize
=
r_short
(
p
);
int
flags
=
r_short
(
p
);
o
bject
*
code
=
NULL
;
o
bject
*
consts
=
NULL
;
o
bject
*
names
=
NULL
;
o
bject
*
varnames
=
NULL
;
o
bject
*
filename
=
NULL
;
o
bject
*
name
=
NULL
;
PyO
bject
*
code
=
NULL
;
PyO
bject
*
consts
=
NULL
;
PyO
bject
*
names
=
NULL
;
PyO
bject
*
varnames
=
NULL
;
PyO
bject
*
filename
=
NULL
;
PyO
bject
*
name
=
NULL
;
int
firstlineno
=
0
;
o
bject
*
lnotab
=
NULL
;
PyO
bject
*
lnotab
=
NULL
;
code
=
r_object
(
p
);
if
(
code
)
consts
=
r_object
(
p
);
...
...
@@ -544,20 +547,20 @@ r_object(p)
lnotab
=
r_object
(
p
);
}
if
(
!
err_o
ccurred
())
{
v
=
(
object
*
)
newcodeobject
(
if
(
!
PyErr_O
ccurred
())
{
v
=
(
PyObject
*
)
PyCode_New
(
argcount
,
nlocals
,
stacksize
,
flags
,
code
,
consts
,
names
,
varnames
,
filename
,
name
,
firstlineno
,
lnotab
);
}
else
v
=
NULL
;
XDECREF
(
code
);
XDECREF
(
consts
);
XDECREF
(
names
);
XDECREF
(
varnames
);
XDECREF
(
filename
);
XDECREF
(
name
);
Py_
XDECREF
(
code
);
Py_
XDECREF
(
consts
);
Py_
XDECREF
(
names
);
Py_
XDECREF
(
varnames
);
Py_
XDECREF
(
filename
);
Py_
XDECREF
(
name
);
}
return
v
;
...
...
@@ -565,14 +568,14 @@ r_object(p)
default:
/* Bogus data got written, which isn't ideal.
This will let you keep working and recover. */
INCREF
(
None
);
return
None
;
Py_INCREF
(
Py_
None
);
return
Py_
None
;
}
}
long
rd_long
(
fp
)
PyMarshal_ReadLongFromFile
(
fp
)
FILE
*
fp
;
{
RFILE
rf
;
...
...
@@ -580,13 +583,12 @@ rd_long(fp)
return
r_long
(
&
rf
);
}
o
bject
*
rd_object
(
fp
)
PyO
bject
*
PyMarshal_ReadObjectFromFile
(
fp
)
FILE
*
fp
;
{
RFILE
rf
;
if
(
err_occurred
())
{
fatal
(
"XXX rd_object called with exception set"
);
/* tmp */
if
(
PyErr_Occurred
())
{
fprintf
(
stderr
,
"XXX rd_object called with exception set
\n
"
);
return
NULL
;
}
...
...
@@ -594,13 +596,13 @@ rd_object(fp)
return
r_object
(
&
rf
);
}
o
bject
*
rds_object
(
str
,
len
)
PyO
bject
*
PyMarshal_ReadObjectFromString
(
str
,
len
)
char
*
str
;
int
len
;
{
RFILE
rf
;
if
(
err_o
ccurred
())
{
if
(
PyErr_O
ccurred
())
{
fprintf
(
stderr
,
"XXX rds_object called with exception set
\n
"
);
return
NULL
;
}
...
...
@@ -611,25 +613,26 @@ rds_object(str, len)
return
r_object
(
&
rf
);
}
o
bject
*
PyO
bject
*
PyMarshal_WriteObjectToString
(
x
)
/* wrs_object() */
o
bject
*
x
;
PyO
bject
*
x
;
{
WFILE
wf
;
wf
.
fp
=
NULL
;
wf
.
str
=
newsizedstringobject
((
char
*
)
NULL
,
50
);
wf
.
str
=
PyString_FromStringAndSize
((
char
*
)
NULL
,
50
);
if
(
wf
.
str
==
NULL
)
return
NULL
;
wf
.
ptr
=
GETSTRINGVALUE
((
stringo
bject
*
)
wf
.
str
);
wf
.
end
=
wf
.
ptr
+
getstrings
ize
(
wf
.
str
);
wf
.
ptr
=
PyString_AS_STRING
((
PyStringO
bject
*
)
wf
.
str
);
wf
.
end
=
wf
.
ptr
+
PyString_S
ize
(
wf
.
str
);
wf
.
error
=
0
;
w_object
(
x
,
&
wf
);
if
(
wf
.
str
!=
NULL
)
resizestring
(
&
wf
.
str
,
(
int
)
(
wf
.
ptr
-
GETSTRINGVALUE
((
stringobject
*
)
wf
.
str
)));
_PyString_Resize
(
&
wf
.
str
,
(
int
)
(
wf
.
ptr
-
PyString_AS_STRING
((
PyStringObject
*
)
wf
.
str
)));
if
(
wf
.
error
)
{
XDECREF
(
wf
.
str
);
err_setstr
(
ValueError
,
"unmarshallable object"
);
Py_
XDECREF
(
wf
.
str
);
PyErr_SetString
(
PyExc_
ValueError
,
"unmarshallable object"
);
return
NULL
;
}
return
wf
.
str
;
...
...
@@ -637,95 +640,97 @@ PyMarshal_WriteObjectToString(x) /* wrs_object() */
/* And an interface for Python programs... */
static
o
bject
*
static
PyO
bject
*
marshal_dump
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
WFILE
wf
;
o
bject
*
x
;
o
bject
*
f
;
if
(
!
getargs
(
args
,
"(OO)"
,
&
x
,
&
f
))
PyO
bject
*
x
;
PyO
bject
*
f
;
if
(
!
PyArg_Parse
(
args
,
"(OO)"
,
&
x
,
&
f
))
return
NULL
;
if
(
!
is_fileobject
(
f
))
{
err_setstr
(
TypeError
,
"marshal.dump() 2nd arg must be file"
);
if
(
!
PyFile_Check
(
f
))
{
PyErr_SetString
(
PyExc_TypeError
,
"marshal.dump() 2nd arg must be file"
);
return
NULL
;
}
wf
.
fp
=
getfilef
ile
(
f
);
wf
.
fp
=
PyFile_AsF
ile
(
f
);
wf
.
str
=
NULL
;
wf
.
ptr
=
wf
.
end
=
NULL
;
wf
.
error
=
0
;
w_object
(
x
,
&
wf
);
if
(
wf
.
error
)
{
err_setstr
(
ValueError
,
"unmarshallable object"
);
PyErr_SetString
(
PyExc_
ValueError
,
"unmarshallable object"
);
return
NULL
;
}
INCREF
(
None
);
return
None
;
Py_INCREF
(
Py_
None
);
return
Py_
None
;
}
static
o
bject
*
static
PyO
bject
*
marshal_load
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
RFILE
rf
;
o
bject
*
f
;
o
bject
*
v
;
if
(
!
getargs
(
args
,
"O"
,
&
f
))
PyO
bject
*
f
;
PyO
bject
*
v
;
if
(
!
PyArg_Parse
(
args
,
"O"
,
&
f
))
return
NULL
;
if
(
!
is_fileobject
(
f
))
{
err_setstr
(
TypeError
,
"marshal.load() arg must be file"
);
if
(
!
PyFile_Check
(
f
))
{
PyErr_SetString
(
PyExc_TypeError
,
"marshal.load() arg must be file"
);
return
NULL
;
}
rf
.
fp
=
getfilef
ile
(
f
);
rf
.
fp
=
PyFile_AsF
ile
(
f
);
rf
.
str
=
NULL
;
rf
.
ptr
=
rf
.
end
=
NULL
;
err_c
lear
();
PyErr_C
lear
();
v
=
r_object
(
&
rf
);
if
(
err_o
ccurred
())
{
XDECREF
(
v
);
if
(
PyErr_O
ccurred
())
{
Py_
XDECREF
(
v
);
v
=
NULL
;
}
return
v
;
}
static
o
bject
*
static
PyO
bject
*
marshal_dumps
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
o
bject
*
x
;
if
(
!
getargs
(
args
,
"O"
,
&
x
))
PyO
bject
*
x
;
if
(
!
PyArg_Parse
(
args
,
"O"
,
&
x
))
return
NULL
;
return
PyMarshal_WriteObjectToString
(
x
);
}
static
o
bject
*
static
PyO
bject
*
marshal_loads
(
self
,
args
)
o
bject
*
self
;
o
bject
*
args
;
PyO
bject
*
self
;
PyO
bject
*
args
;
{
RFILE
rf
;
o
bject
*
v
;
PyO
bject
*
v
;
char
*
s
;
int
n
;
if
(
!
getargs
(
args
,
"s#"
,
&
s
,
&
n
))
if
(
!
PyArg_Parse
(
args
,
"s#"
,
&
s
,
&
n
))
return
NULL
;
rf
.
fp
=
NULL
;
rf
.
str
=
args
;
rf
.
ptr
=
s
;
rf
.
end
=
s
+
n
;
err_c
lear
();
PyErr_C
lear
();
v
=
r_object
(
&
rf
);
if
(
err_o
ccurred
())
{
XDECREF
(
v
);
if
(
PyErr_O
ccurred
())
{
Py_
XDECREF
(
v
);
v
=
NULL
;
}
return
v
;
}
static
struct
methodlist
marshal_methods
[]
=
{
static
PyMethodDef
marshal_methods
[]
=
{
{
"dump"
,
marshal_dump
},
{
"load"
,
marshal_load
},
{
"dumps"
,
marshal_dumps
},
...
...
@@ -734,7 +739,7 @@ static struct methodlist marshal_methods[] = {
};
void
initmarshal
()
PyMarshal_Init
()
{
(
void
)
initm
odule
(
"marshal"
,
marshal_methods
);
(
void
)
Py_InitM
odule
(
"marshal"
,
marshal_methods
);
}
Python/modsupport.c
View file @
5574f2d0
...
...
@@ -31,8 +31,7 @@ PERFORMANCE OF THIS SOFTWARE.
/* Module support implementation */
#include "allobjects.h"
#include "import.h"
#include "Python.h"
#ifdef MPW
/* MPW pushes 'extended' for float and double types with varargs */
typedef
extended
va_double
;
...
...
@@ -53,37 +52,39 @@ static char api_version_warning[] =
"WARNING: Python C API version mismatch for module %s:
\n
\
This Python has API version %d, module %s has version %d.
\n
"
;
o
bject
*
initm
odule4
(
name
,
methods
,
doc
,
passthrough
,
module_api_version
)
PyO
bject
*
Py_InitM
odule4
(
name
,
methods
,
doc
,
passthrough
,
module_api_version
)
char
*
name
;
struct
methodlist
*
methods
;
PyMethodDef
*
methods
;
char
*
doc
;
o
bject
*
passthrough
;
PyO
bject
*
passthrough
;
int
module_api_version
;
{
o
bject
*
m
,
*
d
,
*
v
;
struct
methodlist
*
ml
;
PyO
bject
*
m
,
*
d
,
*
v
;
PyMethodDef
*
ml
;
if
(
module_api_version
!=
PYTHON_API_VERSION
)
fprintf
(
stderr
,
api_version_warning
,
name
,
PYTHON_API_VERSION
,
name
,
module_api_version
);
if
((
m
=
add_m
odule
(
name
))
==
NULL
)
{
if
((
m
=
PyImport_AddM
odule
(
name
))
==
NULL
)
{
fprintf
(
stderr
,
"initializing module: %s
\n
"
,
name
);
fatal
(
"can't create a module"
);
Py_FatalError
(
"can't create a module"
);
}
d
=
getmoduled
ict
(
m
);
d
=
PyModule_GetD
ict
(
m
);
for
(
ml
=
methods
;
ml
->
ml_name
!=
NULL
;
ml
++
)
{
v
=
newmethodobject
(
ml
,
passthrough
);
if
(
v
==
NULL
||
dictinsert
(
d
,
ml
->
ml_name
,
v
)
!=
0
)
{
v
=
PyCFunction_New
(
ml
,
passthrough
);
if
(
v
==
NULL
||
PyDict_SetItemString
(
d
,
ml
->
ml_name
,
v
)
!=
0
)
{
fprintf
(
stderr
,
"initializing module: %s
\n
"
,
name
);
fatal
(
"can't initialize module"
);
Py_FatalError
(
"can't initialize module"
);
}
DECREF
(
v
);
Py_
DECREF
(
v
);
}
if
(
doc
!=
NULL
)
{
v
=
newstringobject
(
doc
);
if
(
v
==
NULL
||
dictinsert
(
d
,
"__doc__"
,
v
)
!=
0
)
fatal
(
"can't add doc string"
);
DECREF
(
v
);
v
=
PyString_FromString
(
doc
);
if
(
v
==
NULL
||
PyDict_SetItemString
(
d
,
"__doc__"
,
v
)
!=
0
)
Py_FatalError
(
"can't add doc string"
);
Py_
DECREF
(
v
);
}
return
m
;
}
...
...
@@ -91,7 +92,7 @@ initmodule4(name, methods, doc, passthrough, module_api_version)
/* Helper for mkvalue() to scan the length of a format */
static
int
countformat
PROTO
((
char
*
format
,
int
endchar
));
static
int
countformat
P
y_P
ROTO
((
char
*
format
,
int
endchar
));
static
int
countformat
(
format
,
endchar
)
char
*
format
;
int
endchar
;
...
...
@@ -102,7 +103,8 @@ static int countformat(format, endchar)
switch
(
*
format
)
{
case
'\0'
:
/* Premature end */
err_setstr
(
SystemError
,
"unmatched paren in format"
);
PyErr_SetString
(
PyExc_SystemError
,
"unmatched paren in format"
);
return
-
1
;
case
'('
:
case
'['
:
...
...
@@ -136,118 +138,121 @@ static int countformat(format, endchar)
/* Generic function to create a value -- the inverse of getargs() */
/* After an original idea and first implementation by Steven Miale */
static
object
*
do_mktuple
PROTO
((
char
**
,
va_list
*
,
int
,
int
));
static
object
*
do_mklist
PROTO
((
char
**
,
va_list
*
,
int
,
int
));
static
object
*
do_mkdict
PROTO
((
char
**
,
va_list
*
,
int
,
int
));
static
object
*
do_mkvalue
PROTO
((
char
**
,
va_list
*
));
static
PyObject
*
do_mktuple
Py_
PROTO
((
char
**
,
va_list
*
,
int
,
int
));
static
PyObject
*
do_mklist
Py_
PROTO
((
char
**
,
va_list
*
,
int
,
int
));
static
PyObject
*
do_mkdict
Py_
PROTO
((
char
**
,
va_list
*
,
int
,
int
));
static
PyObject
*
do_mkvalue
Py_
PROTO
((
char
**
,
va_list
*
));
static
o
bject
*
static
PyO
bject
*
do_mkdict
(
p_format
,
p_va
,
endchar
,
n
)
char
**
p_format
;
va_list
*
p_va
;
int
endchar
;
int
n
;
{
o
bject
*
d
;
PyO
bject
*
d
;
int
i
;
if
(
n
<
0
)
return
NULL
;
if
((
d
=
newdictobject
())
==
NULL
)
if
((
d
=
PyDict_New
())
==
NULL
)
return
NULL
;
for
(
i
=
0
;
i
<
n
;
i
+=
2
)
{
o
bject
*
k
,
*
v
;
PyO
bject
*
k
,
*
v
;
k
=
do_mkvalue
(
p_format
,
p_va
);
if
(
k
==
NULL
)
{
DECREF
(
d
);
Py_
DECREF
(
d
);
return
NULL
;
}
v
=
do_mkvalue
(
p_format
,
p_va
);
if
(
v
==
NULL
)
{
DECREF
(
k
);
DECREF
(
d
);
Py_
DECREF
(
k
);
Py_
DECREF
(
d
);
return
NULL
;
}
if
(
dict2insert
(
d
,
k
,
v
)
<
0
)
{
DECREF
(
k
);
DECREF
(
v
);
DECREF
(
d
);
if
(
PyDict_SetItem
(
d
,
k
,
v
)
<
0
)
{
Py_
DECREF
(
k
);
Py_
DECREF
(
v
);
Py_
DECREF
(
d
);
return
NULL
;
}
}
if
(
d
!=
NULL
&&
**
p_format
!=
endchar
)
{
DECREF
(
d
);
Py_
DECREF
(
d
);
d
=
NULL
;
err_setstr
(
SystemError
,
"Unmatched paren in format"
);
PyErr_SetString
(
PyExc_SystemError
,
"Unmatched paren in format"
);
}
else
if
(
endchar
)
++*
p_format
;
return
d
;
}
static
o
bject
*
static
PyO
bject
*
do_mklist
(
p_format
,
p_va
,
endchar
,
n
)
char
**
p_format
;
va_list
*
p_va
;
int
endchar
;
int
n
;
{
o
bject
*
v
;
PyO
bject
*
v
;
int
i
;
if
(
n
<
0
)
return
NULL
;
if
((
v
=
newlistobject
(
n
))
==
NULL
)
if
((
v
=
PyList_New
(
n
))
==
NULL
)
return
NULL
;
for
(
i
=
0
;
i
<
n
;
i
++
)
{
o
bject
*
w
=
do_mkvalue
(
p_format
,
p_va
);
PyO
bject
*
w
=
do_mkvalue
(
p_format
,
p_va
);
if
(
w
==
NULL
)
{
DECREF
(
v
);
Py_
DECREF
(
v
);
return
NULL
;
}
setlisti
tem
(
v
,
i
,
w
);
PyList_SetI
tem
(
v
,
i
,
w
);
}
if
(
v
!=
NULL
&&
**
p_format
!=
endchar
)
{
DECREF
(
v
);
Py_
DECREF
(
v
);
v
=
NULL
;
err_setstr
(
SystemError
,
"Unmatched paren in format"
);
PyErr_SetString
(
PyExc_SystemError
,
"Unmatched paren in format"
);
}
else
if
(
endchar
)
++*
p_format
;
return
v
;
}
static
o
bject
*
static
PyO
bject
*
do_mktuple
(
p_format
,
p_va
,
endchar
,
n
)
char
**
p_format
;
va_list
*
p_va
;
int
endchar
;
int
n
;
{
o
bject
*
v
;
PyO
bject
*
v
;
int
i
;
if
(
n
<
0
)
return
NULL
;
if
((
v
=
newtupleobject
(
n
))
==
NULL
)
if
((
v
=
PyTuple_New
(
n
))
==
NULL
)
return
NULL
;
for
(
i
=
0
;
i
<
n
;
i
++
)
{
o
bject
*
w
=
do_mkvalue
(
p_format
,
p_va
);
PyO
bject
*
w
=
do_mkvalue
(
p_format
,
p_va
);
if
(
w
==
NULL
)
{
DECREF
(
v
);
Py_
DECREF
(
v
);
return
NULL
;
}
settuplei
tem
(
v
,
i
,
w
);
PyTuple_SetI
tem
(
v
,
i
,
w
);
}
if
(
v
!=
NULL
&&
**
p_format
!=
endchar
)
{
DECREF
(
v
);
Py_
DECREF
(
v
);
v
=
NULL
;
err_setstr
(
SystemError
,
"Unmatched paren in format"
);
PyErr_SetString
(
PyExc_SystemError
,
"Unmatched paren in format"
);
}
else
if
(
endchar
)
++*
p_format
;
return
v
;
}
static
o
bject
*
static
PyO
bject
*
do_mkvalue
(
p_format
,
p_va
)
char
**
p_format
;
va_list
*
p_va
;
...
...
@@ -269,26 +274,27 @@ do_mkvalue(p_format, p_va)
case
'b'
:
case
'h'
:
case
'i'
:
return
newintobject
((
long
)
va_arg
(
*
p_va
,
int
));
return
PyInt_FromLong
((
long
)
va_arg
(
*
p_va
,
int
));
case
'l'
:
return
newintobject
((
long
)
va_arg
(
*
p_va
,
long
));
return
PyInt_FromLong
((
long
)
va_arg
(
*
p_va
,
long
));
case
'f'
:
case
'd'
:
return
newfloatobject
((
double
)
va_arg
(
*
p_va
,
va_double
));
return
PyFloat_FromDouble
(
(
double
)
va_arg
(
*
p_va
,
va_double
));
case
'c'
:
{
char
p
[
1
];
p
[
0
]
=
va_arg
(
*
p_va
,
int
);
return
newsizedstringobject
(
p
,
1
);
return
PyString_FromStringAndSize
(
p
,
1
);
}
case
's'
:
case
'z'
:
{
o
bject
*
v
;
PyO
bject
*
v
;
char
*
str
=
va_arg
(
*
p_va
,
char
*
);
int
n
;
if
(
**
p_format
==
'#'
)
{
...
...
@@ -298,13 +304,13 @@ do_mkvalue(p_format, p_va)
else
n
=
-
1
;
if
(
str
==
NULL
)
{
v
=
None
;
INCREF
(
v
);
v
=
Py_
None
;
Py_
INCREF
(
v
);
}
else
{
if
(
n
<
0
)
n
=
strlen
(
str
);
v
=
newsizedstringobject
(
str
,
n
);
v
=
PyString_FromStringAndSize
(
str
,
n
);
}
return
v
;
}
...
...
@@ -312,18 +318,18 @@ do_mkvalue(p_format, p_va)
case
'S'
:
case
'O'
:
if
(
**
p_format
==
'&'
)
{
typedef
object
*
(
*
converter
)
PROTO
((
void
*
));
typedef
PyObject
*
(
*
converter
)
Py_
PROTO
((
void
*
));
converter
func
=
va_arg
(
*
p_va
,
converter
);
void
*
arg
=
va_arg
(
*
p_va
,
void
*
);
++*
p_format
;
return
(
*
func
)(
arg
);
}
else
{
o
bject
*
v
;
v
=
va_arg
(
*
p_va
,
o
bject
*
);
PyO
bject
*
v
;
v
=
va_arg
(
*
p_va
,
PyO
bject
*
);
if
(
v
!=
NULL
)
INCREF
(
v
);
else
if
(
!
err_o
ccurred
())
Py_
INCREF
(
v
);
else
if
(
!
PyErr_O
ccurred
())
/* If a NULL was passed
* because a call that should
* have constructed a value
...
...
@@ -332,7 +338,7 @@ do_mkvalue(p_format, p_va)
* no error occurred it's not
* clear that the caller knew
* what she was doing. */
err_setstr
(
SystemError
,
PyErr_SetString
(
PyExc_
SystemError
,
"NULL object passed to mkvalue"
);
return
v
;
}
...
...
@@ -344,7 +350,7 @@ do_mkvalue(p_format, p_va)
break
;
default:
err_setstr
(
SystemError
,
PyErr_SetString
(
PyExc_
SystemError
,
"bad format char passed to mkvalue"
);
return
NULL
;
...
...
@@ -355,14 +361,14 @@ do_mkvalue(p_format, p_va)
#ifdef HAVE_STDARG_PROTOTYPES
/* VARARGS 2 */
object
*
mkv
alue
(
char
*
format
,
...)
PyObject
*
Py_BuildV
alue
(
char
*
format
,
...)
#else
/* VARARGS */
object
*
mkv
alue
(
va_alist
)
va_dcl
PyObject
*
Py_BuildV
alue
(
va_alist
)
va_dcl
#endif
{
va_list
va
;
o
bject
*
retval
;
PyO
bject
*
retval
;
#ifdef HAVE_STDARG_PROTOTYPES
va_start
(
va
,
format
);
#else
...
...
@@ -370,13 +376,13 @@ object *mkvalue(va_alist) va_dcl
va_start
(
va
);
format
=
va_arg
(
va
,
char
*
);
#endif
retval
=
vmkv
alue
(
format
,
va
);
retval
=
Py_VaBuildV
alue
(
format
,
va
);
va_end
(
va
);
return
retval
;
}
o
bject
*
vmkv
alue
(
format
,
va
)
PyO
bject
*
Py_VaBuildV
alue
(
format
,
va
)
char
*
format
;
va_list
va
;
{
...
...
@@ -393,8 +399,8 @@ vmkvalue(format, va)
if
(
n
<
0
)
return
NULL
;
if
(
n
==
0
)
{
INCREF
(
None
);
return
None
;
Py_INCREF
(
Py_
None
);
return
Py_
None
;
}
if
(
n
==
1
)
return
do_mkvalue
(
&
f
,
&
lva
);
...
...
@@ -403,19 +409,19 @@ vmkvalue(format, va)
#ifdef HAVE_STDARG_PROTOTYPES
o
bject
*
PyEval_CallFunction
(
o
bject
*
obj
,
char
*
format
,
...)
PyO
bject
*
PyEval_CallFunction
(
PyO
bject
*
obj
,
char
*
format
,
...)
#else
o
bject
*
PyO
bject
*
PyEval_CallFunction
(
obj
,
format
,
va_alist
)
o
bject
*
obj
;
PyO
bject
*
obj
;
char
*
format
;
va_dcl
#endif
{
va_list
vargs
;
o
bject
*
args
;
o
bject
*
res
;
PyO
bject
*
args
;
PyO
bject
*
res
;
#ifdef HAVE_STDARG_PROTOTYPES
va_start
(
vargs
,
format
);
...
...
@@ -423,37 +429,37 @@ PyEval_CallFunction(obj, format, va_alist)
va_start
(
vargs
);
#endif
args
=
vmkv
alue
(
format
,
vargs
);
args
=
Py_VaBuildV
alue
(
format
,
vargs
);
va_end
(
vargs
);
if
(
args
==
NULL
)
return
NULL
;
res
=
call_o
bject
(
obj
,
args
);
DECREF
(
args
);
res
=
PyEval_CallO
bject
(
obj
,
args
);
Py_
DECREF
(
args
);
return
res
;
}
#ifdef HAVE_STDARG_PROTOTYPES
o
bject
*
PyEval_CallMethod
(
o
bject
*
obj
,
char
*
methonname
,
char
*
format
,
...)
PyO
bject
*
PyEval_CallMethod
(
PyO
bject
*
obj
,
char
*
methonname
,
char
*
format
,
...)
#else
o
bject
*
PyO
bject
*
PyEval_CallMethod
(
obj
,
methonname
,
format
,
va_alist
)
o
bject
*
obj
;
PyO
bject
*
obj
;
char
*
methonname
;
char
*
format
;
va_dcl
#endif
{
va_list
vargs
;
o
bject
*
meth
;
o
bject
*
args
;
o
bject
*
res
;
PyO
bject
*
meth
;
PyO
bject
*
args
;
PyO
bject
*
res
;
meth
=
getattr
(
obj
,
methonname
);
meth
=
PyObject_GetAttrString
(
obj
,
methonname
);
if
(
meth
==
NULL
)
return
NULL
;
...
...
@@ -463,17 +469,17 @@ PyEval_CallMethod(obj, methonname, format, va_alist)
va_start
(
vargs
);
#endif
args
=
vmkv
alue
(
format
,
vargs
);
args
=
Py_VaBuildV
alue
(
format
,
vargs
);
va_end
(
vargs
);
if
(
args
==
NULL
)
{
DECREF
(
meth
);
Py_
DECREF
(
meth
);
return
NULL
;
}
res
=
call_o
bject
(
meth
,
args
);
DECREF
(
meth
);
DECREF
(
args
);
res
=
PyEval_CallO
bject
(
meth
,
args
);
Py_
DECREF
(
meth
);
Py_
DECREF
(
args
);
return
res
;
}
Python/sigcheck.c
View file @
5574f2d0
...
...
@@ -36,15 +36,14 @@ PERFORMANCE OF THIS SOFTWARE.
overridden (at link time) by a more powerful version implemented in
signalmodule.c. */
#include "allobjects.h"
#include "intrcheck.h"
#include "Python.h"
/* ARGSUSED */
int
sigcheck
()
PyErr_CheckSignals
()
{
if
(
!
intrcheck
())
if
(
!
PyOS_InterruptOccurred
())
return
0
;
err_set
(
KeyboardInterrupt
);
PyErr_SetNone
(
PyExc_
KeyboardInterrupt
);
return
-
1
;
}
Python/structmember.c
View file @
5574f2d0
...
...
@@ -31,35 +31,36 @@ PERFORMANCE OF THIS SOFTWARE.
/* Map C struct members to Python object attributes */
#include "
allobjects
.h"
#include "
Python
.h"
#include "structmember.h"
static
o
bject
*
static
PyO
bject
*
listmembers
(
mlist
)
struct
memberlist
*
mlist
;
{
int
i
,
n
;
o
bject
*
v
;
PyO
bject
*
v
;
for
(
n
=
0
;
mlist
[
n
].
name
!=
NULL
;
n
++
)
;
v
=
newlistobject
(
n
);
v
=
PyList_New
(
n
);
if
(
v
!=
NULL
)
{
for
(
i
=
0
;
i
<
n
;
i
++
)
setlistitem
(
v
,
i
,
newstringobject
(
mlist
[
i
].
name
));
if
(
err_occurred
())
{
DECREF
(
v
);
PyList_SetItem
(
v
,
i
,
PyString_FromString
(
mlist
[
i
].
name
));
if
(
PyErr_Occurred
())
{
Py_DECREF
(
v
);
v
=
NULL
;
}
else
{
sortlis
t
(
v
);
PyList_Sor
t
(
v
);
}
}
return
v
;
}
o
bject
*
getmember
(
addr
,
mlist
,
name
)
PyO
bject
*
PyMember_Get
(
addr
,
mlist
,
name
)
char
*
addr
;
struct
memberlist
*
mlist
;
char
*
name
;
...
...
@@ -70,111 +71,118 @@ getmember(addr, mlist, name)
return
listmembers
(
mlist
);
for
(
l
=
mlist
;
l
->
name
!=
NULL
;
l
++
)
{
if
(
strcmp
(
l
->
name
,
name
)
==
0
)
{
o
bject
*
v
;
PyO
bject
*
v
;
addr
+=
l
->
offset
;
switch
(
l
->
type
)
{
case
T_BYTE
:
v
=
newintobject
((
long
)
v
=
PyInt_FromLong
((
long
)
(((
*
(
char
*
)
addr
&
0xff
)
^
0x80
)
-
0x80
));
break
;
case
T_UBYTE
:
v
=
newintobject
((
long
)
*
(
char
*
)
addr
&
0xff
);
v
=
PyInt_FromLong
((
long
)
*
(
char
*
)
addr
&
0xff
);
break
;
case
T_SHORT
:
v
=
newintobject
((
long
)
*
(
short
*
)
addr
);
v
=
PyInt_FromLong
((
long
)
*
(
short
*
)
addr
);
break
;
case
T_USHORT
:
v
=
newintobject
((
long
)
v
=
PyInt_FromLong
((
long
)
*
(
unsigned
short
*
)
addr
);
break
;
case
T_INT
:
v
=
newintobject
((
long
)
*
(
int
*
)
addr
);
v
=
PyInt_FromLong
((
long
)
*
(
int
*
)
addr
);
break
;
case
T_UINT
:
v
=
newintobject
((
long
)
*
(
unsigned
int
*
)
addr
);
v
=
PyInt_FromLong
((
long
)
*
(
unsigned
int
*
)
addr
);
break
;
case
T_LONG
:
v
=
newintobject
(
*
(
long
*
)
addr
);
v
=
PyInt_FromLong
(
*
(
long
*
)
addr
);
break
;
case
T_ULONG
:
v
=
dnewlongobject
((
double
)
v
=
PyLong_FromDouble
((
double
)
*
(
unsigned
long
*
)
addr
);
break
;
case
T_FLOAT
:
v
=
newfloatobject
((
double
)
*
(
float
*
)
addr
);
v
=
PyFloat_FromDouble
((
double
)
*
(
float
*
)
addr
);
break
;
case
T_DOUBLE
:
v
=
newfloatobject
(
*
(
double
*
)
addr
);
v
=
PyFloat_FromDouble
(
*
(
double
*
)
addr
);
break
;
case
T_STRING
:
if
(
*
(
char
**
)
addr
==
NULL
)
{
INCREF
(
None
);
v
=
None
;
Py_INCREF
(
Py_
None
);
v
=
Py_
None
;
}
else
v
=
newstringobject
(
*
(
char
**
)
addr
);
v
=
PyString_FromString
(
*
(
char
**
)
addr
);
break
;
case
T_STRING_INPLACE
:
v
=
newstringobject
((
char
*
)
addr
);
v
=
PyString_FromString
((
char
*
)
addr
);
break
;
#ifdef macintosh
case
T_PSTRING
:
if
(
*
(
char
**
)
addr
==
NULL
)
{
INCREF
(
None
);
v
=
None
;
Py_INCREF
(
Py_
None
);
v
=
Py_
None
;
}
else
v
=
newsizedstringobject
((
*
(
char
**
)
addr
)
+
1
,
**
(
unsigned
char
**
)
addr
);
v
=
PyString_FromStringAndSize
(
(
*
(
char
**
)
addr
)
+
1
,
**
(
unsigned
char
**
)
addr
);
break
;
case
T_PSTRING_INPLACE
:
v
=
newsizedstringobject
(((
char
*
)
addr
)
+
1
,
*
(
unsigned
char
*
)
addr
);
v
=
PyString_FromStringAndSize
(
((
char
*
)
addr
)
+
1
,
*
(
unsigned
char
*
)
addr
);
break
;
#endif
/* macintosh */
case
T_CHAR
:
v
=
newsizedstringobject
((
char
*
)
addr
,
1
);
v
=
PyString_FromStringAndSize
((
char
*
)
addr
,
1
);
break
;
case
T_OBJECT
:
v
=
*
(
o
bject
**
)
addr
;
v
=
*
(
PyO
bject
**
)
addr
;
if
(
v
==
NULL
)
v
=
None
;
INCREF
(
v
);
v
=
Py_
None
;
Py_
INCREF
(
v
);
break
;
default:
err_setstr
(
SystemError
,
"bad memberlist type"
);
PyErr_SetString
(
PyExc_SystemError
,
"bad memberlist type"
);
v
=
NULL
;
}
return
v
;
}
}
err_setstr
(
AttributeError
,
name
);
PyErr_SetString
(
PyExc_
AttributeError
,
name
);
return
NULL
;
}
int
setmember
(
addr
,
mlist
,
name
,
v
)
PyMember_Set
(
addr
,
mlist
,
name
,
v
)
char
*
addr
;
struct
memberlist
*
mlist
;
char
*
name
;
o
bject
*
v
;
PyO
bject
*
v
;
{
struct
memberlist
*
l
;
for
(
l
=
mlist
;
l
->
name
!=
NULL
;
l
++
)
{
if
(
strcmp
(
l
->
name
,
name
)
==
0
)
{
#ifdef macintosh
if
(
l
->
readonly
||
l
->
type
==
T_STRING
||
l
->
type
==
T_PSTRING
)
{
if
(
l
->
readonly
||
l
->
type
==
T_STRING
||
l
->
type
==
T_PSTRING
)
{
#else
if
(
l
->
readonly
||
l
->
type
==
T_STRING
)
{
#endif
/* macintosh */
err_setstr
(
TypeError
,
"readonly attribute"
);
PyErr_SetString
(
PyExc_TypeError
,
"readonly attribute"
);
return
-
1
;
}
if
(
v
==
NULL
&&
l
->
type
!=
T_OBJECT
)
{
err_setstr
(
TypeError
,
PyErr_SetString
(
PyExc_
TypeError
,
"can't delete numeric/char attribute"
);
return
-
1
;
}
...
...
@@ -182,90 +190,92 @@ setmember(addr, mlist, name, v)
switch
(
l
->
type
)
{
case
T_BYTE
:
case
T_UBYTE
:
if
(
!
is_intobject
(
v
))
{
err_badarg
();
if
(
!
PyInt_Check
(
v
))
{
PyErr_BadArgument
();
return
-
1
;
}
*
(
char
*
)
addr
=
(
char
)
getintvalue
(
v
);
*
(
char
*
)
addr
=
(
char
)
PyInt_AsLong
(
v
);
break
;
case
T_SHORT
:
case
T_USHORT
:
if
(
!
is_intobject
(
v
))
{
err_badarg
();
if
(
!
PyInt_Check
(
v
))
{
PyErr_BadArgument
();
return
-
1
;
}
*
(
short
*
)
addr
=
(
short
)
getintvalue
(
v
);
*
(
short
*
)
addr
=
(
short
)
PyInt_AsLong
(
v
);
break
;
case
T_UINT
:
case
T_INT
:
if
(
!
is_intobject
(
v
))
{
err_badarg
();
if
(
!
PyInt_Check
(
v
))
{
PyErr_BadArgument
();
return
-
1
;
}
*
(
int
*
)
addr
=
(
int
)
getintvalue
(
v
);
*
(
int
*
)
addr
=
(
int
)
PyInt_AsLong
(
v
);
break
;
case
T_LONG
:
if
(
!
is_intobject
(
v
))
{
err_badarg
();
if
(
!
PyInt_Check
(
v
))
{
PyErr_BadArgument
();
return
-
1
;
}
*
(
long
*
)
addr
=
getintvalue
(
v
);
*
(
long
*
)
addr
=
PyInt_AsLong
(
v
);
break
;
case
T_ULONG
:
if
(
is_intobject
(
v
))
*
(
long
*
)
addr
=
getintvalue
(
v
);
else
if
(
is_longobject
(
v
))
*
(
long
*
)
addr
=
getlongvalue
(
v
);
if
(
PyInt_Check
(
v
))
*
(
long
*
)
addr
=
PyInt_AsLong
(
v
);
else
if
(
PyLong_Check
(
v
))
*
(
long
*
)
addr
=
PyLong_AsLong
(
v
);
else
{
err_badarg
();
PyErr_BadArgument
();
return
-
1
;
}
break
;
case
T_FLOAT
:
if
(
is_intobject
(
v
))
*
(
float
*
)
addr
=
(
float
)
getintvalue
(
v
);
else
if
(
is_floatobject
(
v
))
if
(
PyInt_Check
(
v
))
*
(
float
*
)
addr
=
(
float
)
getfloatvalue
(
v
);
(
float
)
PyInt_AsLong
(
v
);
else
if
(
PyFloat_Check
(
v
))
*
(
float
*
)
addr
=
(
float
)
PyFloat_AsDouble
(
v
);
else
{
err_badarg
();
PyErr_BadArgument
();
return
-
1
;
}
break
;
case
T_DOUBLE
:
if
(
is_intobject
(
v
))
if
(
PyInt_Check
(
v
))
*
(
double
*
)
addr
=
(
double
)
getintvalue
(
v
);
else
if
(
is_floatobject
(
v
))
*
(
double
*
)
addr
=
getfloatvalu
e
(
v
);
(
double
)
PyInt_AsLong
(
v
);
else
if
(
PyFloat_Check
(
v
))
*
(
double
*
)
addr
=
PyFloat_AsDoubl
e
(
v
);
else
{
err_badarg
();
PyErr_BadArgument
();
return
-
1
;
}
break
;
case
T_OBJECT
:
XDECREF
(
*
(
o
bject
**
)
addr
);
XINCREF
(
v
);
*
(
o
bject
**
)
addr
=
v
;
Py_XDECREF
(
*
(
PyO
bject
**
)
addr
);
Py_
XINCREF
(
v
);
*
(
PyO
bject
**
)
addr
=
v
;
break
;
case
T_CHAR
:
if
(
is_stringobject
(
v
)
&&
getstrings
ize
(
v
)
==
1
)
{
if
(
PyString_Check
(
v
)
&&
PyString_S
ize
(
v
)
==
1
)
{
*
(
char
*
)
addr
=
getstringvalue
(
v
)[
0
];
PyString_AsString
(
v
)[
0
];
}
else
{
err_badarg
();
PyErr_BadArgument
();
return
-
1
;
}
default:
err_setstr
(
SystemError
,
"bad memberlist type"
);
PyErr_SetString
(
PyExc_SystemError
,
"bad memberlist type"
);
return
-
1
;
}
return
0
;
}
}
err_setstr
(
AttributeError
,
name
);
PyErr_SetString
(
PyExc_
AttributeError
,
name
);
return
-
1
;
}
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