Python源码示例:foo.Foo()
示例1
def test_special_cases():
'''
Extraordinary cases that should still be supported
by IP.
'''
#ensure that assemblies reopening the same module and overriding
#a class work. by "work", this means that the last (alphabetically) DLL
#should be the one that's imported
import foo
AreEqual(foo.Foo().BAR, 4)
#test some unusual DLL filenames
for partial_ns in ["ZERO", "ONE", "a", "UNDERSCORE", "WHITESPACE", "BIGFILENAME"]:
mod_name = "foo" + partial_ns
exec "import " + mod_name
exec "AreEqual(" + mod_name + ".Foo().BAR, 1)"
示例2
def test_special_cases():
'''
Extraordinary cases that should still be supported
by IP.
'''
#ensure that assemblies reopening the same module and overriding
#a class work. by "work", this means that the last (alphabetically) DLL
#should be the one that's imported
import foo
AreEqual(foo.Foo().BAR, 4)
#test some unusual DLL filenames
for partial_ns in ["ZERO", "ONE", "a", "UNDERSCORE", "WHITESPACE", "BIGFILENAME"]:
mod_name = "foo" + partial_ns
exec("import " + mod_name)
exec("AreEqual(" + mod_name + ".Foo().BAR, 1)")
示例3
def test_attribute_access(v):
v.scan(
"""\
# foo.py
class Foo:
pass
# bar.py
from foo import Foo
# main.py
import bar
bar.Foo
"""
)
check(v.defined_imports, ["Foo", "bar"])
check(v.unused_imports, [])
示例4
def test_sanity():
'''
Sanity checks. All of these are fairly normal imports
and should work.
'''
#first check that our native modules are still present and accounted for...
import binascii
import _collections
import copy_reg
import cPickle
import cStringIO
import datetime
import errno
import exceptions
import gc
import imp
import itertools
import marshal
import math
import nt
import operator
import re
import socket
import _struct
import thread
import time
#next run through our first set of "OK" modules
for i in xrange(50):
mod_name = "foo" + str(i)
exec "import " + mod_name
exec "AreEqual(" + mod_name + ".Foo().BAR," + str(i) + ")"
示例5
def test_foo(self):
f = foo.Foo("hello")
self.assertEqual(f.get_datum(), "hello")
示例6
def test_pass_foo_by_value(self):
obj = foo.SomeObject("")
f = foo.Foo("hello")
obj.set_foo_value(f)
f2 = obj.get_foo_value()
self.assertEqual(f2.get_datum(), "hello")
示例7
def test_pass_foo_by_transfer_ptr(self):
obj = foo.SomeObject("")
f = foo.Foo("hello")
obj.set_foo_ptr(f)
del f
f2 = obj.get_foo_ptr()
self.assertEqual(f2.get_datum(), "hello")
示例8
def test_pass_foo_shared_ptr(self):
obj = foo.SomeObject("")
f = foo.Foo("hello")
obj.set_foo_shared_ptr(f)
self.assertEqual(f.get_datum(), "hello")
f2 = obj.get_foo_shared_ptr()
self.assertEqual(f2.get_datum(), "hello")
示例9
def test_pass_by_reference(self):
obj = foo.SomeObject("")
f = foo.Foo("hello")
obj.set_foo_by_ref(f)
self.assertEqual(f.get_datum(), "hello")
f2 = obj.get_foo_by_ref()
self.assertEqual(f2.get_datum(), "hello")
示例10
def test_custom_instance_attribute(self):
obj = foo.Foo()
if foo.Foo.instance_count == 1:
self.assertTrue(obj.is_unique)
else:
self.assertFalse(obj.is_unique)
示例11
def test_return_type_narrowing(self):
obj = foo.SomeObject("zbr")
obj.set_foo_ptr(foo.Foo())
foo1 = obj.get_foo_ptr()
self.assertEqual(type(foo1), foo.Foo)
bar2 = foo.Bar()
self.assertEqual(type(bar2), foo.Bar)
obj.set_foo_ptr(bar2)
foo2 = obj.get_foo_ptr()
self.assertEqual(type(foo2), foo.Bar)
示例12
def test_subclass_with_virtual_with_foo_parameter_value(self):
class Test(foo.SomeObject):
def __init__(self, prefix, extra_prefix):
super(Test, self).__init__(prefix)
self.extra_prefix = extra_prefix
def get_prefix_with_foo_value(self, fooval):
prefix = super(Test, self).get_prefix_with_foo_value(fooval)
return prefix + self.extra_prefix + fooval.get_datum()
t = Test("123", "456")
foo1 = foo.Foo("zbr")
prefix = t.get_prefix_with_foo_value(foo1)
self.assertEqual(prefix, "123zbr456zbr")
示例13
def test_subclass_with_virtual_with_foo_parameter_ref(self):
class Test(foo.SomeObject):
def __init__(self, prefix, extra_prefix):
super(Test, self).__init__(prefix)
self.extra_prefix = extra_prefix
def get_prefix_with_foo_ref(self, fooval):
prefix = super(Test, self).get_prefix_with_foo_ref(fooval)
return prefix + self.extra_prefix + fooval.get_datum()
t = Test("123", "456")
foo1 = foo.Foo("zbr")
prefix = t.get_prefix_with_foo_ref(foo1)
self.assertEqual(prefix, "123zbr456zbr")
示例14
def test_subclass_with_virtual_with_foo_parameter_ptr(self):
class Test(foo.SomeObject):
def __init__(self, prefix, extra_prefix):
super(Test, self).__init__(prefix)
self.extra_prefix = extra_prefix
def get_prefix_with_foo_ptr(self, fooval):
prefix = super(Test, self).get_prefix_with_foo_ptr(fooval)
return prefix + self.extra_prefix + fooval.get_datum()
t = Test("123", "456")
foo1 = foo.Foo("zbr")
prefix = t.get_prefix_with_foo_ptr(foo1)
self.assertEqual(prefix, "123zbr456zbr")
示例15
def test_instance_creation_function(self):
f = foo.Foo()
self.assertTrue(f.is_initialized())
b = foo.Bar()
self.assertTrue(b.is_initialized())
示例16
def test_output_stream(self):
f = foo.Foo("hello")
self.assertEqual(str(f), "hello")
示例17
def test_bug436154(self):
r = foo.Foo.add_sub(1, 2)
self.assertEqual(r, 3)
示例18
def test_foo(self):
foo_instance = Foo()
self.assertEqual(
foo_instance.foo(x=32),
64
)
示例19
def test_foo_get(self):
foo_instance = Foo()
self.assertIsInstance(
foo_instance.foo_get(),
foo.Bar
)
示例20
def test_sanity():
'''
Sanity checks. All of these are fairly normal imports
and should work.
'''
#first check that our native modules are still present and accounted for...
import binascii
import _collections
import copyreg
import pickle
import io
import datetime
import errno
import exceptions
import gc
import imp
import itertools
import marshal
import math
import nt
import operator
import re
import socket
import _struct
import _thread
import time
#next run through our first set of "OK" modules
for i in range(50):
mod_name = "foo" + str(i)
exec("import " + mod_name)
exec("AreEqual(" + mod_name + ".Foo().BAR," + str(i) + ")")
示例21
def test_bad_stuff():
'''
Cases where IP should not load an assembly for one
reason or another.
'''
#ensure that users cannot override IP native modules
import sys
Assert(sys.winver != "HIJACKED")
import re
Assert(re.compile != "HIJACKED")
#ensure corrupted DLLs cannot be loaded
try:
import fooCORRUPT
raise Exception("Corrupted DLL was loaded")
except ImportError as e:
pass
#nothing to do for unmanaged DLLs...if the interpreter has made it
#this far, all is well:)
#ensure *.exe's cannot take precedence over *.dlls
import fooDLLEXE
AreEqual(fooDLLEXE.Foo().BAR, 1)
#ensure *.exe's are not autoloaded at all!
try:
import fooEXEONLY
raise Exception("*.exe's should not be autoloaded!")
except ImportError as e:
pass
except SystemError as e:
print("Work Item #189503")
#ensure *.txt's are not autoloaded at all
try:
import fooTXTDLL
raise Exception("*.txt's should not be autoloaded!")
except ImportError as e:
pass