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
71cd5515
Commit
71cd5515
authored
Jan 20, 2007
by
Walter Dörwald
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Port test_new.py to unittest.
parent
d414302e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
162 additions
and
190 deletions
+162
-190
Lib/test/output/test_new
Lib/test/output/test_new
+0
-7
Lib/test/test_new.py
Lib/test/test_new.py
+162
-183
No files found.
Lib/test/output/test_new
deleted
100644 → 0
View file @
d414302e
test_new
new.module()
new.classobj()
new.instance()
new.instancemethod()
new.function()
new.code()
Lib/test/test_new.py
View file @
71cd5515
from
test.test_support
import
verbose
,
verify
,
TestFailed
import
unittest
import
sys
from
test
import
test_support
import
new
import
sys
,
new
class
Eggs
:
class
NewTest
(
unittest
.
TestCase
):
def
get_yolks
(
self
):
def
test_spam
(
self
):
return
self
.
yolks
class
Eggs
:
def
get_yolks
(
self
):
print
'new.module()'
return
self
.
yolks
m
=
new
.
module
(
'Spam'
)
if
verbose
:
m
=
new
.
module
(
'Spam'
)
print
m
m
.
Eggs
=
Eggs
m
.
Eggs
=
Eggs
sys
.
modules
[
'Spam'
]
=
m
sys
.
modules
[
'Spam'
]
=
m
import
Spam
import
Spam
def
get_more_yolks
(
self
):
def
get_more_yolks
(
self
):
return
self
.
yolks
+
3
return
self
.
yolks
+
3
# new.classobj()
print
'new.classobj()'
C
=
new
.
classobj
(
'Spam'
,
(
Spam
.
Eggs
,),
{
'get_more_yolks'
:
get_more_yolks
})
C
=
new
.
classobj
(
'Spam'
,
(
Spam
.
Eggs
,),
{
'get_more_yolks'
:
get_more_yolks
})
if
verbose
:
# new.instance()
print
C
c
=
new
.
instance
(
C
,
{
'yolks'
:
3
})
print
'new.instance()'
c
=
new
.
instance
(
C
,
{
'yolks'
:
3
})
o
=
new
.
instance
(
C
)
if
verbose
:
self
.
assertEqual
(
o
.
__dict__
,
{},
"new __dict__ should be empty"
)
print
c
del
o
o
=
new
.
instance
(
C
)
o
=
new
.
instance
(
C
,
None
)
verify
(
o
.
__dict__
==
{},
self
.
assertEqual
(
o
.
__dict__
,
{},
"new __dict__ should be empty"
)
"new __dict__ should be empty"
)
del
o
del
o
o
=
new
.
instance
(
C
,
None
)
def
break_yolks
(
self
):
verify
(
o
.
__dict__
==
{},
self
.
yolks
=
self
.
yolks
-
2
"new __dict__ should be empty"
)
del
o
# new.instancemethod()
im
=
new
.
instancemethod
(
break_yolks
,
c
,
C
)
def
break_yolks
(
self
):
self
.
yolks
=
self
.
yolks
-
2
self
.
assertEqual
(
c
.
get_yolks
(),
3
,
print
'new.instancemethod()'
'Broken call of hand-crafted class instance'
)
im
=
new
.
instancemethod
(
break_yolks
,
c
,
C
)
self
.
assertEqual
(
c
.
get_more_yolks
(),
6
,
if
verbose
:
'Broken call of hand-crafted class instance'
)
print
im
im
()
verify
(
c
.
get_yolks
()
==
3
and
c
.
get_more_yolks
()
==
6
,
self
.
assertEqual
(
c
.
get_yolks
(),
1
,
'Broken call of hand-crafted class instance'
)
'Broken call of hand-crafted instance method'
)
im
()
self
.
assertEqual
(
c
.
get_more_yolks
(),
4
,
verify
(
c
.
get_yolks
()
==
1
and
c
.
get_more_yolks
()
==
4
,
'Broken call of hand-crafted instance method'
)
'Broken call of hand-crafted instance method'
)
im
=
new
.
instancemethod
(
break_yolks
,
c
)
im
=
new
.
instancemethod
(
break_yolks
,
c
)
im
()
im
()
self
.
assertEqual
(
c
.
get_yolks
(),
-
1
)
verify
(
c
.
get_yolks
()
==
-
1
)
try
:
# Verify that dangerous instance method creation is forbidden
new
.
instancemethod
(
break_yolks
,
None
)
self
.
assertRaises
(
TypeError
,
new
.
instancemethod
,
break_yolks
,
None
)
except
TypeError
:
pass
# Verify that instancemethod() doesn't allow keyword args
else
:
self
.
assertRaises
(
TypeError
,
new
.
instancemethod
,
break_yolks
,
c
,
kw
=
1
)
raise
TestFailed
,
"dangerous instance method creation allowed"
def
test_scope
(
self
):
# Verify that instancemethod() doesn't allow keyword args
# It's unclear what the semantics should be for a code object compiled
try
:
# at module scope, but bound and run in a function. In CPython, `c' is
new
.
instancemethod
(
break_yolks
,
c
,
kw
=
1
)
# global (by accident?) while in Jython, `c' is local. The intent of
except
TypeError
:
# the test clearly is to make `c' global, so let's be explicit about it.
pass
codestr
=
'''
else
:
global c
raise
TestFailed
,
"instancemethod shouldn't accept keyword args"
a = 1
b = 2
# It's unclear what the semantics should be for a code object compiled at
c = a + b
# module scope, but bound and run in a function. In CPython, `c' is global
'''
# (by accident?) while in Jython, `c' is local. The intent of the test
# clearly is to make `c' global, so let's be explicit about it.
codestr
=
"
\
n
"
.
join
(
l
.
strip
()
for
l
in
codestr
.
splitlines
())
codestr
=
'''
global c
ccode
=
compile
(
codestr
,
'<string>'
,
'exec'
)
a = 1
# Jython doesn't have a __builtins__, so use a portable alternative
b = 2
import
__builtin__
c = a + b
g
=
{
'c'
:
0
,
'__builtins__'
:
__builtin__
}
'''
# this test could be more robust
ccode
=
compile
(
codestr
,
'<string>'
,
'exec'
)
func
=
new
.
function
(
ccode
,
g
)
# Jython doesn't have a __builtins__, so use a portable alternative
func
()
import
__builtin__
self
.
assertEqual
(
g
[
'c'
],
3
,
'Could not create a proper function object'
)
g
=
{
'c'
:
0
,
'__builtins__'
:
__builtin__
}
# this test could be more robust
def
test_function
(
self
):
print
'new.function()'
# test the various extended flavors of function.new
func
=
new
.
function
(
ccode
,
g
)
def
f
(
x
):
if
verbose
:
def
g
(
y
):
print
func
return
x
+
y
func
()
return
g
verify
(
g
[
'c'
]
==
3
,
g
=
f
(
4
)
'Could not create a proper function object'
)
new
.
function
(
f
.
func_code
,
{},
"blah"
)
g2
=
new
.
function
(
g
.
func_code
,
{},
"blah"
,
(
2
,),
g
.
func_closure
)
# test the various extended flavors of function.new
self
.
assertEqual
(
g2
(),
6
)
def
f
(
x
):
g3
=
new
.
function
(
g
.
func_code
,
{},
"blah"
,
None
,
g
.
func_closure
)
def
g
(
y
):
self
.
assertEqual
(
g3
(
5
),
9
)
return
x
+
y
def
test_closure
(
func
,
closure
,
exc
):
return
g
self
.
assertRaises
(
exc
,
new
.
function
,
func
.
func_code
,
{},
""
,
None
,
closure
)
g
=
f
(
4
)
new
.
function
(
f
.
func_code
,
{},
"blah"
)
test_closure
(
g
,
None
,
TypeError
)
# invalid closure
g2
=
new
.
function
(
g
.
func_code
,
{},
"blah"
,
(
2
,),
g
.
func_closure
)
test_closure
(
g
,
(
1
,),
TypeError
)
# non-cell in closure
verify
(
g2
()
==
6
)
test_closure
(
g
,
(
1
,
1
),
ValueError
)
# closure is wrong size
g3
=
new
.
function
(
g
.
func_code
,
{},
"blah"
,
None
,
g
.
func_closure
)
test_closure
(
f
,
g
.
func_closure
,
ValueError
)
# no closure needed
verify
(
g3
(
5
)
==
9
)
def
test_closure
(
func
,
closure
,
exc
):
# Note: Jython will never have new.code()
try
:
if
hasattr
(
new
,
'code'
):
new
.
function
(
func
.
func_code
,
{},
""
,
None
,
closure
)
def
test_code
(
self
):
except
exc
:
# bogus test of new.code()
pass
def
f
(
a
):
pass
else
:
print
"corrupt closure accepted"
c
=
f
.
func_code
argcount
=
c
.
co_argcount
test_closure
(
g
,
None
,
TypeError
)
# invalid closure
nlocals
=
c
.
co_nlocals
test_closure
(
g
,
(
1
,),
TypeError
)
# non-cell in closure
stacksize
=
c
.
co_stacksize
test_closure
(
g
,
(
1
,
1
),
ValueError
)
# closure is wrong size
flags
=
c
.
co_flags
test_closure
(
f
,
g
.
func_closure
,
ValueError
)
# no closure needed
codestring
=
c
.
co_code
constants
=
c
.
co_consts
print
'new.code()'
names
=
c
.
co_names
# bogus test of new.code()
varnames
=
c
.
co_varnames
# Note: Jython will never have new.code()
filename
=
c
.
co_filename
if
hasattr
(
new
,
'code'
):
name
=
c
.
co_name
def
f
(
a
):
pass
firstlineno
=
c
.
co_firstlineno
lnotab
=
c
.
co_lnotab
c
=
f
.
func_code
freevars
=
c
.
co_freevars
argcount
=
c
.
co_argcount
cellvars
=
c
.
co_cellvars
nlocals
=
c
.
co_nlocals
stacksize
=
c
.
co_stacksize
d
=
new
.
code
(
argcount
,
nlocals
,
stacksize
,
flags
,
codestring
,
flags
=
c
.
co_flags
constants
,
names
,
varnames
,
filename
,
name
,
codestring
=
c
.
co_code
firstlineno
,
lnotab
,
freevars
,
cellvars
)
constants
=
c
.
co_consts
names
=
c
.
co_names
# test backwards-compatibility version with no freevars or cellvars
varnames
=
c
.
co_varnames
d
=
new
.
code
(
argcount
,
nlocals
,
stacksize
,
flags
,
codestring
,
filename
=
c
.
co_filename
constants
,
names
,
varnames
,
filename
,
name
,
name
=
c
.
co_name
firstlineno
,
lnotab
)
firstlineno
=
c
.
co_firstlineno
lnotab
=
c
.
co_lnotab
# negative co_argcount used to trigger a SystemError
freevars
=
c
.
co_freevars
self
.
assertRaises
(
ValueError
,
new
.
code
,
cellvars
=
c
.
co_cellvars
-
argcount
,
nlocals
,
stacksize
,
flags
,
codestring
,
constants
,
names
,
varnames
,
filename
,
name
,
firstlineno
,
lnotab
)
d
=
new
.
code
(
argcount
,
nlocals
,
stacksize
,
flags
,
codestring
,
constants
,
names
,
varnames
,
filename
,
name
,
# negative co_nlocals used to trigger a SystemError
firstlineno
,
lnotab
,
freevars
,
cellvars
)
self
.
assertRaises
(
ValueError
,
new
.
code
,
argcount
,
-
nlocals
,
stacksize
,
flags
,
codestring
,
# test backwards-compatibility version with no freevars or cellvars
constants
,
names
,
varnames
,
filename
,
name
,
firstlineno
,
lnotab
)
d
=
new
.
code
(
argcount
,
nlocals
,
stacksize
,
flags
,
codestring
,
constants
,
names
,
varnames
,
filename
,
name
,
# non-string co_name used to trigger a Py_FatalError
firstlineno
,
lnotab
)
self
.
assertRaises
(
TypeError
,
new
.
code
,
argcount
,
nlocals
,
stacksize
,
flags
,
codestring
,
try
:
# this used to trigger a SystemError
constants
,
(
5
,),
varnames
,
filename
,
name
,
firstlineno
,
lnotab
)
d
=
new
.
code
(
-
argcount
,
nlocals
,
stacksize
,
flags
,
codestring
,
constants
,
names
,
varnames
,
filename
,
name
,
# new.code used to be a way to mutate a tuple...
firstlineno
,
lnotab
)
class
S
(
str
):
except
ValueError
:
pass
pass
t
=
(
S
(
"ab"
),)
else
:
d
=
new
.
code
(
argcount
,
nlocals
,
stacksize
,
flags
,
codestring
,
raise
TestFailed
,
"negative co_argcount didn't trigger an exception"
constants
,
t
,
varnames
,
filename
,
name
,
firstlineno
,
lnotab
)
try
:
# this used to trigger a SystemError
self
.
assert_
(
type
(
t
[
0
])
is
S
,
"eek, tuple changed under us!"
)
d
=
new
.
code
(
argcount
,
-
nlocals
,
stacksize
,
flags
,
codestring
,
constants
,
names
,
varnames
,
filename
,
name
,
def
test_main
():
firstlineno
,
lnotab
)
test_support
.
run_unittest
(
NewTest
)
except
ValueError
:
pass
if
__name__
==
"__main__"
:
else
:
test_main
()
raise
TestFailed
,
"negative co_nlocals didn't trigger an exception"
try
:
# this used to trigger a Py_FatalError!
d
=
new
.
code
(
argcount
,
nlocals
,
stacksize
,
flags
,
codestring
,
constants
,
(
5
,),
varnames
,
filename
,
name
,
firstlineno
,
lnotab
)
except
TypeError
:
pass
else
:
raise
TestFailed
,
"non-string co_name didn't trigger an exception"
# new.code used to be a way to mutate a tuple...
class
S
(
str
):
pass
t
=
(
S
(
"ab"
),)
d
=
new
.
code
(
argcount
,
nlocals
,
stacksize
,
flags
,
codestring
,
constants
,
t
,
varnames
,
filename
,
name
,
firstlineno
,
lnotab
)
verify
(
type
(
t
[
0
])
is
S
,
"eek, tuple changed under us!"
)
if
verbose
:
print
d
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