Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.

This commit is contained in:
Christian Heimes 2007-11-27 21:34:01 +00:00
parent 8b01140d2c
commit c756d00cf2
10 changed files with 52 additions and 81 deletions

View file

@ -1,6 +1,6 @@
"""Support Eiffel-style preconditions and postconditions.""" """Support Eiffel-style preconditions and postconditions."""
from new import function from types import FunctionType as function
class EiffelBaseMetaClass(type): class EiffelBaseMetaClass(type):

View file

@ -1,7 +1,7 @@
"""A flow graph representation for Python bytecode""" """A flow graph representation for Python bytecode"""
import dis import dis
import new import types
import sys import sys
from compiler import misc from compiler import misc
@ -595,7 +595,7 @@ class PyFlowGraph(FlowGraph):
argcount = self.argcount argcount = self.argcount
if self.flags & CO_VARKEYWORDS: if self.flags & CO_VARKEYWORDS:
argcount = argcount - 1 argcount = argcount - 1
return new.code(argcount, nlocals, self.stacksize, self.flags, return types.CodeType(argcount, nlocals, self.stacksize, self.flags,
self.lnotab.getCode(), self.getConsts(), self.lnotab.getCode(), self.getConsts(),
tuple(self.names), tuple(self.varnames), tuple(self.names), tuple(self.varnames),
self.filename, self.name, self.lnotab.firstline, self.filename, self.name, self.lnotab.firstline,

View file

@ -2016,16 +2016,16 @@ class Tester:
return (f,t) return (f,t)
def rundict(self, d, name, module=None): def rundict(self, d, name, module=None):
import new import types
m = new.module(name) m = types.ModuleType(name)
m.__dict__.update(d) m.__dict__.update(d)
if module is None: if module is None:
module = False module = False
return self.rundoc(m, name, module) return self.rundoc(m, name, module)
def run__test__(self, d, name): def run__test__(self, d, name):
import new import types
m = new.module(name) m = types.ModuleType(name)
m.__test__ = d m.__test__ = d
return self.rundoc(m, name) return self.rundoc(m, name)

View file

@ -7,7 +7,7 @@ import imp
import marshal import marshal
import os import os
import sys import sys
import new import types
import struct import struct
if hasattr(sys.__stdout__, "newlines"): if hasattr(sys.__stdout__, "newlines"):
@ -594,7 +594,7 @@ class ModuleFinder:
if isinstance(consts[i], type(co)): if isinstance(consts[i], type(co)):
consts[i] = self.replace_paths_in_code(consts[i]) consts[i] = self.replace_paths_in_code(consts[i])
return new.code(co.co_argcount, co.co_nlocals, co.co_stacksize, return types.CodeType(co.co_argcount, co.co_nlocals, co.co_stacksize,
co.co_flags, co.co_code, tuple(consts), co.co_names, co.co_flags, co.co_code, tuple(consts), co.co_names,
co.co_varnames, new_filename, co.co_name, co.co_varnames, new_filename, co.co_name,
co.co_firstlineno, co.co_lnotab, co.co_firstlineno, co.co_lnotab,

View file

@ -4,7 +4,6 @@ from test.test_support import verify, vereq, verbose, TestFailed, TESTFN, get_or
from copy import deepcopy from copy import deepcopy
import warnings import warnings
import types import types
import new
warnings.filterwarnings("ignore", warnings.filterwarnings("ignore",
r'complex divmod\(\), // and % are deprecated$', r'complex divmod\(\), // and % are deprecated$',

View file

@ -448,8 +448,8 @@ docstring, and will recursively explore its contents, including
functions, classes, and the `__test__` dictionary, if it exists: functions, classes, and the `__test__` dictionary, if it exists:
>>> # A module >>> # A module
>>> import new >>> import types
>>> m = new.module('some_module') >>> m = types.ModuleType('some_module')
>>> def triple(val): >>> def triple(val):
... ''' ... '''
... >>> print triple(11) ... >>> print triple(11)
@ -1937,11 +1937,11 @@ def test_DocFileSuite():
If DocFileSuite is used from an interactive session, then files If DocFileSuite is used from an interactive session, then files
are resolved relative to the directory of sys.argv[0]: are resolved relative to the directory of sys.argv[0]:
>>> import new, os.path, test.test_doctest >>> import types, os.path, test.test_doctest
>>> save_argv = sys.argv >>> save_argv = sys.argv
>>> sys.argv = [test.test_doctest.__file__] >>> sys.argv = [test.test_doctest.__file__]
>>> suite = doctest.DocFileSuite('test_doctest.txt', >>> suite = doctest.DocFileSuite('test_doctest.txt',
... package=new.module('__main__')) ... package=types.ModuleType('__main__'))
>>> sys.argv = save_argv >>> sys.argv = save_argv
By setting `module_relative=False`, os-specific paths may be By setting `module_relative=False`, os-specific paths may be
@ -2366,9 +2366,9 @@ def old_test3(): r"""
""" """
def old_test4(): """ def old_test4(): """
>>> import new >>> import types
>>> m1 = new.module('_m1') >>> m1 = types.ModuleType('_m1')
>>> m2 = new.module('_m2') >>> m2 = types.ModuleType('_m2')
>>> test_data = \""" >>> test_data = \"""
... def _f(): ... def _f():
... '''>>> assert 1 == 1 ... '''>>> assert 1 == 1

View file

@ -132,8 +132,8 @@ if f2.a.one <> f1.a.one <> F.a.one <> 11:
raise TestFailed raise TestFailed
# im_func may not be a Python method! # im_func may not be a Python method!
import new import types
F.id = new.instancemethod(id, None, F) F.id = types.MethodType(id, None, F)
eff = F() eff = F()
if eff.id() <> id(eff): if eff.id() <> id(eff):

View file

@ -167,8 +167,8 @@ class GetoptTests(unittest.TestCase):
['a1', 'a2'] ['a1', 'a2']
""" """
import new import types
m = new.module("libreftest", s) m = types.ModuleType("libreftest", s)
run_doctest(m, verbose) run_doctest(m, verbose)

View file

@ -203,9 +203,9 @@ class TestRetrievingSourceCode(GetSourceBase):
self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__) self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__)
def test_getmodule_recursion(self): def test_getmodule_recursion(self):
from new import module from types import ModuleType
name = '__inspect_dummy' name = '__inspect_dummy'
m = sys.modules[name] = module(name) m = sys.modules[name] = ModuleType(name)
m.__file__ = "<string>" # hopefully not a real filename... m.__file__ = "<string>" # hopefully not a real filename...
m.__loader__ = "dummy" # pretend the filename is understood by a loader m.__loader__ = "dummy" # pretend the filename is understood by a loader
exec "def x(): pass" in m.__dict__ exec "def x(): pass" in m.__dict__

View file

@ -9,6 +9,7 @@ Still need testing:
from test import test_support from test import test_support
import unittest import unittest
from unittest import TestCase from unittest import TestCase
import types
### Support code ### Support code
################################################################ ################################################################
@ -153,8 +154,7 @@ class Test_TestLoader(TestCase):
# "This method searches `module` for classes derived from TestCase" # "This method searches `module` for classes derived from TestCase"
def test_loadTestsFromModule__TestCase_subclass(self): def test_loadTestsFromModule__TestCase_subclass(self):
import new m = types.ModuleType('m')
m = new.module('m')
class MyTestCase(unittest.TestCase): class MyTestCase(unittest.TestCase):
def test(self): def test(self):
pass pass
@ -171,8 +171,7 @@ class Test_TestLoader(TestCase):
# #
# What happens if no tests are found (no TestCase instances)? # What happens if no tests are found (no TestCase instances)?
def test_loadTestsFromModule__no_TestCase_instances(self): def test_loadTestsFromModule__no_TestCase_instances(self):
import new m = types.ModuleType('m')
m = new.module('m')
loader = unittest.TestLoader() loader = unittest.TestLoader()
suite = loader.loadTestsFromModule(m) suite = loader.loadTestsFromModule(m)
@ -183,8 +182,7 @@ class Test_TestLoader(TestCase):
# #
# What happens if no tests are found (TestCases instances, but no tests)? # What happens if no tests are found (TestCases instances, but no tests)?
def test_loadTestsFromModule__no_TestCase_tests(self): def test_loadTestsFromModule__no_TestCase_tests(self):
import new m = types.ModuleType('m')
m = new.module('m')
class MyTestCase(unittest.TestCase): class MyTestCase(unittest.TestCase):
pass pass
m.testcase_1 = MyTestCase m.testcase_1 = MyTestCase
@ -381,8 +379,7 @@ class Test_TestLoader(TestCase):
# Does it raise an exception if the name resolves to an invalid # Does it raise an exception if the name resolves to an invalid
# object? # object?
def test_loadTestsFromName__relative_bad_object(self): def test_loadTestsFromName__relative_bad_object(self):
import new m = types.ModuleType('m')
m = new.module('m')
m.testcase_1 = object() m.testcase_1 = object()
loader = unittest.TestLoader() loader = unittest.TestLoader()
@ -396,8 +393,7 @@ class Test_TestLoader(TestCase):
# "The specifier name is a ``dotted name'' that may # "The specifier name is a ``dotted name'' that may
# resolve either to ... a test case class" # resolve either to ... a test case class"
def test_loadTestsFromName__relative_TestCase_subclass(self): def test_loadTestsFromName__relative_TestCase_subclass(self):
import new m = types.ModuleType('m')
m = new.module('m')
class MyTestCase(unittest.TestCase): class MyTestCase(unittest.TestCase):
def test(self): def test(self):
pass pass
@ -413,8 +409,7 @@ class Test_TestLoader(TestCase):
# within a test case class, or a callable object which returns a # within a test case class, or a callable object which returns a
# TestCase or TestSuite instance." # TestCase or TestSuite instance."
def test_loadTestsFromName__relative_TestSuite(self): def test_loadTestsFromName__relative_TestSuite(self):
import new m = types.ModuleType('m')
m = new.module('m')
class MyTestCase(unittest.TestCase): class MyTestCase(unittest.TestCase):
def test(self): def test(self):
pass pass
@ -429,8 +424,7 @@ class Test_TestLoader(TestCase):
# "The specifier name is a ``dotted name'' that may resolve ... to # "The specifier name is a ``dotted name'' that may resolve ... to
# ... a test method within a test case class" # ... a test method within a test case class"
def test_loadTestsFromName__relative_testmethod(self): def test_loadTestsFromName__relative_testmethod(self):
import new m = types.ModuleType('m')
m = new.module('m')
class MyTestCase(unittest.TestCase): class MyTestCase(unittest.TestCase):
def test(self): def test(self):
pass pass
@ -451,8 +445,7 @@ class Test_TestLoader(TestCase):
# resolve "a test method within a test case class" that doesn't exist # resolve "a test method within a test case class" that doesn't exist
# for the given name (relative to a provided module)? # for the given name (relative to a provided module)?
def test_loadTestsFromName__relative_invalid_testmethod(self): def test_loadTestsFromName__relative_invalid_testmethod(self):
import new m = types.ModuleType('m')
m = new.module('m')
class MyTestCase(unittest.TestCase): class MyTestCase(unittest.TestCase):
def test(self): def test(self):
pass pass
@ -469,8 +462,7 @@ class Test_TestLoader(TestCase):
# "The specifier name is a ``dotted name'' that may resolve ... to # "The specifier name is a ``dotted name'' that may resolve ... to
# ... a callable object which returns a ... TestSuite instance" # ... a callable object which returns a ... TestSuite instance"
def test_loadTestsFromName__callable__TestSuite(self): def test_loadTestsFromName__callable__TestSuite(self):
import new m = types.ModuleType('m')
m = new.module('m')
testcase_1 = unittest.FunctionTestCase(lambda: None) testcase_1 = unittest.FunctionTestCase(lambda: None)
testcase_2 = unittest.FunctionTestCase(lambda: None) testcase_2 = unittest.FunctionTestCase(lambda: None)
def return_TestSuite(): def return_TestSuite():
@ -485,8 +477,7 @@ class Test_TestLoader(TestCase):
# "The specifier name is a ``dotted name'' that may resolve ... to # "The specifier name is a ``dotted name'' that may resolve ... to
# ... a callable object which returns a TestCase ... instance" # ... a callable object which returns a TestCase ... instance"
def test_loadTestsFromName__callable__TestCase_instance(self): def test_loadTestsFromName__callable__TestCase_instance(self):
import new m = types.ModuleType('m')
m = new.module('m')
testcase_1 = unittest.FunctionTestCase(lambda: None) testcase_1 = unittest.FunctionTestCase(lambda: None)
def return_TestCase(): def return_TestCase():
return testcase_1 return testcase_1
@ -502,8 +493,7 @@ class Test_TestLoader(TestCase):
# #
# What happens if the callable returns something else? # What happens if the callable returns something else?
def test_loadTestsFromName__callable__wrong_type(self): def test_loadTestsFromName__callable__wrong_type(self):
import new m = types.ModuleType('m')
m = new.module('m')
def return_wrong(): def return_wrong():
return 6 return 6
m.return_wrong = return_wrong m.return_wrong = return_wrong
@ -751,8 +741,7 @@ class Test_TestLoader(TestCase):
# Does it raise an exception if the name resolves to an invalid # Does it raise an exception if the name resolves to an invalid
# object? # object?
def test_loadTestsFromNames__relative_bad_object(self): def test_loadTestsFromNames__relative_bad_object(self):
import new m = types.ModuleType('m')
m = new.module('m')
m.testcase_1 = object() m.testcase_1 = object()
loader = unittest.TestLoader() loader = unittest.TestLoader()
@ -766,8 +755,7 @@ class Test_TestLoader(TestCase):
# "The specifier name is a ``dotted name'' that may resolve ... to # "The specifier name is a ``dotted name'' that may resolve ... to
# ... a test case class" # ... a test case class"
def test_loadTestsFromNames__relative_TestCase_subclass(self): def test_loadTestsFromNames__relative_TestCase_subclass(self):
import new m = types.ModuleType('m')
m = new.module('m')
class MyTestCase(unittest.TestCase): class MyTestCase(unittest.TestCase):
def test(self): def test(self):
pass pass
@ -783,8 +771,7 @@ class Test_TestLoader(TestCase):
# "The specifier name is a ``dotted name'' that may resolve ... to # "The specifier name is a ``dotted name'' that may resolve ... to
# ... a TestSuite instance" # ... a TestSuite instance"
def test_loadTestsFromNames__relative_TestSuite(self): def test_loadTestsFromNames__relative_TestSuite(self):
import new m = types.ModuleType('m')
m = new.module('m')
class MyTestCase(unittest.TestCase): class MyTestCase(unittest.TestCase):
def test(self): def test(self):
pass pass
@ -799,8 +786,7 @@ class Test_TestLoader(TestCase):
# "The specifier name is a ``dotted name'' that may resolve ... to ... a # "The specifier name is a ``dotted name'' that may resolve ... to ... a
# test method within a test case class" # test method within a test case class"
def test_loadTestsFromNames__relative_testmethod(self): def test_loadTestsFromNames__relative_testmethod(self):
import new m = types.ModuleType('m')
m = new.module('m')
class MyTestCase(unittest.TestCase): class MyTestCase(unittest.TestCase):
def test(self): def test(self):
pass pass
@ -819,8 +805,7 @@ class Test_TestLoader(TestCase):
# Does the method gracefully handle names that initially look like they # Does the method gracefully handle names that initially look like they
# resolve to "a test method within a test case class" but don't? # resolve to "a test method within a test case class" but don't?
def test_loadTestsFromNames__relative_invalid_testmethod(self): def test_loadTestsFromNames__relative_invalid_testmethod(self):
import new m = types.ModuleType('m')
m = new.module('m')
class MyTestCase(unittest.TestCase): class MyTestCase(unittest.TestCase):
def test(self): def test(self):
pass pass
@ -837,8 +822,7 @@ class Test_TestLoader(TestCase):
# "The specifier name is a ``dotted name'' that may resolve ... to # "The specifier name is a ``dotted name'' that may resolve ... to
# ... a callable object which returns a ... TestSuite instance" # ... a callable object which returns a ... TestSuite instance"
def test_loadTestsFromNames__callable__TestSuite(self): def test_loadTestsFromNames__callable__TestSuite(self):
import new m = types.ModuleType('m')
m = new.module('m')
testcase_1 = unittest.FunctionTestCase(lambda: None) testcase_1 = unittest.FunctionTestCase(lambda: None)
testcase_2 = unittest.FunctionTestCase(lambda: None) testcase_2 = unittest.FunctionTestCase(lambda: None)
def return_TestSuite(): def return_TestSuite():
@ -855,8 +839,7 @@ class Test_TestLoader(TestCase):
# "The specifier name is a ``dotted name'' that may resolve ... to # "The specifier name is a ``dotted name'' that may resolve ... to
# ... a callable object which returns a TestCase ... instance" # ... a callable object which returns a TestCase ... instance"
def test_loadTestsFromNames__callable__TestCase_instance(self): def test_loadTestsFromNames__callable__TestCase_instance(self):
import new m = types.ModuleType('m')
m = new.module('m')
testcase_1 = unittest.FunctionTestCase(lambda: None) testcase_1 = unittest.FunctionTestCase(lambda: None)
def return_TestCase(): def return_TestCase():
return testcase_1 return testcase_1
@ -874,8 +857,7 @@ class Test_TestLoader(TestCase):
# #
# Are staticmethods handled correctly? # Are staticmethods handled correctly?
def test_loadTestsFromNames__callable__call_staticmethod(self): def test_loadTestsFromNames__callable__call_staticmethod(self):
import new m = types.ModuleType('m')
m = new.module('m')
class Test1(unittest.TestCase): class Test1(unittest.TestCase):
def test(self): def test(self):
pass pass
@ -899,8 +881,7 @@ class Test_TestLoader(TestCase):
# #
# What happens when the callable returns something else? # What happens when the callable returns something else?
def test_loadTestsFromNames__callable__wrong_type(self): def test_loadTestsFromNames__callable__wrong_type(self):
import new m = types.ModuleType('m')
m = new.module('m')
def return_wrong(): def return_wrong():
return 6 return 6
m.return_wrong = return_wrong m.return_wrong = return_wrong
@ -1043,8 +1024,7 @@ class Test_TestLoader(TestCase):
# Implicit in the documentation is that testMethodPrefix is respected by # Implicit in the documentation is that testMethodPrefix is respected by
# all loadTestsFrom* methods. # all loadTestsFrom* methods.
def test_testMethodPrefix__loadTestsFromModule(self): def test_testMethodPrefix__loadTestsFromModule(self):
import new m = types.ModuleType('m')
m = new.module('m')
class Foo(unittest.TestCase): class Foo(unittest.TestCase):
def test_1(self): pass def test_1(self): pass
def test_2(self): pass def test_2(self): pass
@ -1067,8 +1047,7 @@ class Test_TestLoader(TestCase):
# Implicit in the documentation is that testMethodPrefix is respected by # Implicit in the documentation is that testMethodPrefix is respected by
# all loadTestsFrom* methods. # all loadTestsFrom* methods.
def test_testMethodPrefix__loadTestsFromName(self): def test_testMethodPrefix__loadTestsFromName(self):
import new m = types.ModuleType('m')
m = new.module('m')
class Foo(unittest.TestCase): class Foo(unittest.TestCase):
def test_1(self): pass def test_1(self): pass
def test_2(self): pass def test_2(self): pass
@ -1091,8 +1070,7 @@ class Test_TestLoader(TestCase):
# Implicit in the documentation is that testMethodPrefix is respected by # Implicit in the documentation is that testMethodPrefix is respected by
# all loadTestsFrom* methods. # all loadTestsFrom* methods.
def test_testMethodPrefix__loadTestsFromNames(self): def test_testMethodPrefix__loadTestsFromNames(self):
import new m = types.ModuleType('m')
m = new.module('m')
class Foo(unittest.TestCase): class Foo(unittest.TestCase):
def test_1(self): pass def test_1(self): pass
def test_2(self): pass def test_2(self): pass
@ -1143,8 +1121,7 @@ class Test_TestLoader(TestCase):
def reversed_cmp(x, y): def reversed_cmp(x, y):
return -cmp(x, y) return -cmp(x, y)
import new m = types.ModuleType('m')
m = new.module('m')
class Foo(unittest.TestCase): class Foo(unittest.TestCase):
def test_1(self): pass def test_1(self): pass
def test_2(self): pass def test_2(self): pass
@ -1162,8 +1139,7 @@ class Test_TestLoader(TestCase):
def reversed_cmp(x, y): def reversed_cmp(x, y):
return -cmp(x, y) return -cmp(x, y)
import new m = types.ModuleType('m')
m = new.module('m')
class Foo(unittest.TestCase): class Foo(unittest.TestCase):
def test_1(self): pass def test_1(self): pass
def test_2(self): pass def test_2(self): pass
@ -1181,8 +1157,7 @@ class Test_TestLoader(TestCase):
def reversed_cmp(x, y): def reversed_cmp(x, y):
return -cmp(x, y) return -cmp(x, y)
import new m = types.ModuleType('m')
m = new.module('m')
class Foo(unittest.TestCase): class Foo(unittest.TestCase):
def test_1(self): pass def test_1(self): pass
def test_2(self): pass def test_2(self): pass
@ -1254,8 +1229,7 @@ class Test_TestLoader(TestCase):
# It is implicit in the documentation for TestLoader.suiteClass that # It is implicit in the documentation for TestLoader.suiteClass that
# all TestLoader.loadTestsFrom* methods respect it. Let's make sure # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
def test_suiteClass__loadTestsFromModule(self): def test_suiteClass__loadTestsFromModule(self):
import new m = types.ModuleType('m')
m = new.module('m')
class Foo(unittest.TestCase): class Foo(unittest.TestCase):
def test_1(self): pass def test_1(self): pass
def test_2(self): pass def test_2(self): pass
@ -1271,8 +1245,7 @@ class Test_TestLoader(TestCase):
# It is implicit in the documentation for TestLoader.suiteClass that # It is implicit in the documentation for TestLoader.suiteClass that
# all TestLoader.loadTestsFrom* methods respect it. Let's make sure # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
def test_suiteClass__loadTestsFromName(self): def test_suiteClass__loadTestsFromName(self):
import new m = types.ModuleType('m')
m = new.module('m')
class Foo(unittest.TestCase): class Foo(unittest.TestCase):
def test_1(self): pass def test_1(self): pass
def test_2(self): pass def test_2(self): pass
@ -1288,8 +1261,7 @@ class Test_TestLoader(TestCase):
# It is implicit in the documentation for TestLoader.suiteClass that # It is implicit in the documentation for TestLoader.suiteClass that
# all TestLoader.loadTestsFrom* methods respect it. Let's make sure # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
def test_suiteClass__loadTestsFromNames(self): def test_suiteClass__loadTestsFromNames(self):
import new m = types.ModuleType('m')
m = new.module('m')
class Foo(unittest.TestCase): class Foo(unittest.TestCase):
def test_1(self): pass def test_1(self): pass
def test_2(self): pass def test_2(self): pass