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
5240d741
Commit
5240d741
authored
Mar 13, 2007
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Patch #1444529: the builtin compile() now accepts keyword arguments.
(backport)
parent
5dc4fe09
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
23 additions
and
9 deletions
+23
-9
Doc/lib/libfuncs.tex
Doc/lib/libfuncs.tex
+5
-5
Lib/test/test_builtin.py
Lib/test/test_builtin.py
+9
-0
Misc/NEWS
Misc/NEWS
+2
-0
Python/bltinmodule.c
Python/bltinmodule.c
+7
-4
No files found.
Doc/lib/libfuncs.tex
View file @
5240d741
...
...
@@ -175,15 +175,15 @@ class C:
\code
{
\var
{
x
}
>
\var
{
y
}}
.
\end{funcdesc}
\begin{funcdesc}
{
compile
}{
s
tring, filename, kind
\optional
{
,
\begin{funcdesc}
{
compile
}{
s
ource, filename, mode
\optional
{
,
flags
\optional
{
, dont
_
inherit
}}}
Compile the
\var
{
s
tring
}
into a code object. Code objects can be
Compile the
\var
{
s
ource
}
into a code object. Code objects can be
executed by an
\keyword
{
exec
}
statement or evaluated by a call to
\function
{
eval()
}
. The
\var
{
filename
}
argument should
give the file from which the code was read; pass some recognizable value
if it wasn't read from a file (
\code
{
'<string>'
}
is commonly used).
The
\var
{
kind
}
argument specifies what kind of code must be
compiled; it can be
\code
{
'exec'
}
if
\var
{
s
tring
}
consists of a
The
\var
{
mode
}
argument specifies what kind of code must be
compiled; it can be
\code
{
'exec'
}
if
\var
{
s
ource
}
consists of a
sequence of statements,
\code
{
'eval'
}
if it consists of a single
expression, or
\code
{
'single'
}
if it consists of a single
interactive statement (in the latter case, expression statements
...
...
@@ -198,7 +198,7 @@ class C:
The optional arguments
\var
{
flags
}
and
\var
{
dont
_
inherit
}
(which are new in Python 2.2) control which future statements (see
\pep
{
236
}
) affect the compilation of
\var
{
s
tring
}
. If neither is
\pep
{
236
}
) affect the compilation of
\var
{
s
ource
}
. If neither is
present (or both are zero) the code is compiled with those future
statements that are in effect in the code that is calling compile.
If the
\var
{
flags
}
argument is given and
\var
{
dont
_
inherit
}
is not
...
...
Lib/test/test_builtin.py
View file @
5240d741
...
...
@@ -107,9 +107,12 @@ class BuiltinTest(unittest.TestCase):
__import__
(
'sys'
)
__import__
(
'time'
)
__import__
(
'string'
)
__import__
(
name
=
'sys'
)
__import__
(
name
=
'time'
,
level
=
0
)
self
.
assertRaises
(
ImportError
,
__import__
,
'spamspam'
)
self
.
assertRaises
(
TypeError
,
__import__
,
1
,
2
,
3
,
4
)
self
.
assertRaises
(
ValueError
,
__import__
,
''
)
self
.
assertRaises
(
TypeError
,
__import__
,
'sys'
,
name
=
'sys'
)
def
test_abs
(
self
):
# int
...
...
@@ -243,15 +246,21 @@ class BuiltinTest(unittest.TestCase):
compile
(
'print 1
\
n
'
,
''
,
'exec'
)
bom
=
'
\
xef
\
xbb
\
xbf
'
compile
(
bom
+
'print 1
\
n
'
,
''
,
'exec'
)
compile
(
source
=
'pass'
,
filename
=
'?'
,
mode
=
'exec'
)
compile
(
dont_inherit
=
0
,
filename
=
'tmp'
,
source
=
'0'
,
mode
=
'eval'
)
compile
(
'pass'
,
'?'
,
dont_inherit
=
1
,
mode
=
'exec'
)
self
.
assertRaises
(
TypeError
,
compile
)
self
.
assertRaises
(
ValueError
,
compile
,
'print 42
\
n
'
,
'<string>'
,
'badmode'
)
self
.
assertRaises
(
ValueError
,
compile
,
'print 42
\
n
'
,
'<string>'
,
'single'
,
0xff
)
self
.
assertRaises
(
TypeError
,
compile
,
chr
(
0
),
'f'
,
'exec'
)
self
.
assertRaises
(
TypeError
,
compile
,
'pass'
,
'?'
,
'exec'
,
mode
=
'eval'
,
source
=
'0'
,
filename
=
'tmp'
)
if
have_unicode
:
compile
(
unicode
(
'print u"
\
xc3
\
xa5
"
\
n
'
,
'utf8'
),
''
,
'exec'
)
self
.
assertRaises
(
TypeError
,
compile
,
unichr
(
0
),
'f'
,
'exec'
)
self
.
assertRaises
(
ValueError
,
compile
,
unicode
(
'a = 1'
),
'f'
,
'bad'
)
def
test_delattr
(
self
):
import
sys
sys
.
spam
=
1
...
...
Misc/NEWS
View file @
5240d741
...
...
@@ -12,6 +12,8 @@ What's New in Python 2.6 alpha 1?
Core and builtins
-----------------
- Patch #1444529: the builtin compile() now accepts keyword arguments.
- Bug #1678647: write a newline after printing an exception in any
case, even when converting the value to a string failed.
...
...
Python/bltinmodule.c
View file @
5240d741
...
...
@@ -402,7 +402,7 @@ a common type, using the same rules as used by arithmetic operations.\n\
If coercion is not possible, raise TypeError."
);
static
PyObject
*
builtin_compile
(
PyObject
*
self
,
PyObject
*
args
)
builtin_compile
(
PyObject
*
self
,
PyObject
*
args
,
PyObject
*
kwds
)
{
char
*
str
;
char
*
filename
;
...
...
@@ -413,9 +413,12 @@ builtin_compile(PyObject *self, PyObject *args)
PyCompilerFlags
cf
;
PyObject
*
result
=
NULL
,
*
cmd
,
*
tmp
=
NULL
;
Py_ssize_t
length
;
static
char
*
kwlist
[]
=
{
"source"
,
"filename"
,
"mode"
,
"flags"
,
"dont_inherit"
,
NULL
};
if
(
!
PyArg_ParseTuple
(
args
,
"Oss|ii:compile"
,
&
cmd
,
&
filename
,
&
startstr
,
&
supplied_flags
,
&
dont_inherit
))
if
(
!
PyArg_ParseTupleAndKeywords
(
args
,
kwds
,
"Oss|ii:compile"
,
kwlist
,
&
cmd
,
&
filename
,
&
startstr
,
&
supplied_flags
,
&
dont_inherit
))
return
NULL
;
cf
.
cf_flags
=
supplied_flags
;
...
...
@@ -2237,7 +2240,7 @@ static PyMethodDef builtin_methods[] = {
{
"chr"
,
builtin_chr
,
METH_VARARGS
,
chr_doc
},
{
"cmp"
,
builtin_cmp
,
METH_VARARGS
,
cmp_doc
},
{
"coerce"
,
builtin_coerce
,
METH_VARARGS
,
coerce_doc
},
{
"compile"
,
builtin_compile
,
METH_VARARG
S
,
compile_doc
},
{
"compile"
,
(
PyCFunction
)
builtin_compile
,
METH_VARARGS
|
METH_KEYWORD
S
,
compile_doc
},
{
"delattr"
,
builtin_delattr
,
METH_VARARGS
,
delattr_doc
},
{
"dir"
,
builtin_dir
,
METH_VARARGS
,
dir_doc
},
{
"divmod"
,
builtin_divmod
,
METH_VARARGS
,
divmod_doc
},
...
...
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