path
stringlengths
14
112
content
stringlengths
0
6.32M
size
int64
0
6.32M
max_lines
int64
1
100k
repo_name
stringclasses
2 values
autogenerated
bool
1 class
cosmopolitan/third_party/python/Lib/distutils/tests/test_bdist_msi.py
"""Tests for distutils.command.bdist_msi.""" import sys import unittest from test.support import run_unittest from distutils.tests import support @unittest.skipUnless(sys.platform == 'win32', 'these tests require Windows') class BDistMSITestCase(support.TempdirManager, support.LoggingSilencer, unittest.TestCase): def test_minimal(self): # minimal test XXX need more tests from distutils.command.bdist_msi import bdist_msi project_dir, dist = self.create_dist() cmd = bdist_msi(dist) cmd.ensure_finalized() def test_suite(): return unittest.makeSuite(BDistMSITestCase) if __name__ == '__main__': run_unittest(test_suite())
728
26
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_sysconfig.py
"""Tests for distutils.sysconfig.""" import os import shutil import subprocess import sys import textwrap import unittest from distutils import sysconfig from distutils.ccompiler import get_default_compiler from distutils.tests import support from test.support import TESTFN, run_unittest, check_warnings class SysconfigTestCase(support.EnvironGuard, unittest.TestCase): def setUp(self): super(SysconfigTestCase, self).setUp() self.makefile = None def tearDown(self): if self.makefile is not None: os.unlink(self.makefile) self.cleanup_testfn() super(SysconfigTestCase, self).tearDown() def cleanup_testfn(self): if os.path.isfile(TESTFN): os.remove(TESTFN) elif os.path.isdir(TESTFN): shutil.rmtree(TESTFN) def test_get_config_h_filename(self): config_h = sysconfig.get_config_h_filename() self.assertTrue(os.path.isfile(config_h), config_h) def test_get_python_lib(self): # XXX doesn't work on Linux when Python was never installed before #self.assertTrue(os.path.isdir(lib_dir), lib_dir) # test for pythonxx.lib? self.assertNotEqual(sysconfig.get_python_lib(), sysconfig.get_python_lib(prefix=TESTFN)) def test_get_config_vars(self): cvars = sysconfig.get_config_vars() self.assertIsInstance(cvars, dict) self.assertTrue(cvars) def test_srcdir(self): # See Issues #15322, #15364. srcdir = sysconfig.get_config_var('srcdir') self.assertTrue(os.path.isabs(srcdir), srcdir) self.assertTrue(os.path.isdir(srcdir), srcdir) if sysconfig.python_build: # The python executable has not been installed so srcdir # should be a full source checkout. Python_h = os.path.join(srcdir, 'Include', 'Python.h') self.assertTrue(os.path.exists(Python_h), Python_h) self.assertTrue(sysconfig._is_python_source_dir(srcdir)) elif os.name == 'posix': self.assertEqual( os.path.dirname(sysconfig.get_makefile_filename()), srcdir) def test_srcdir_independent_of_cwd(self): # srcdir should be independent of the current working directory # See Issues #15322, #15364. srcdir = sysconfig.get_config_var('srcdir') cwd = os.getcwd() try: os.chdir('..') srcdir2 = sysconfig.get_config_var('srcdir') finally: os.chdir(cwd) self.assertEqual(srcdir, srcdir2) @unittest.skipUnless(get_default_compiler() == 'unix', 'not testing if default compiler is not unix') def test_customize_compiler(self): os.environ['AR'] = 'my_ar' os.environ['ARFLAGS'] = '-arflags' # make sure AR gets caught class compiler: compiler_type = 'unix' def set_executables(self, **kw): self.exes = kw comp = compiler() sysconfig.customize_compiler(comp) self.assertEqual(comp.exes['archiver'], 'my_ar -arflags') def test_parse_makefile_base(self): self.makefile = TESTFN fd = open(self.makefile, 'w') try: fd.write(r"CONFIG_ARGS= '--arg1=optarg1' 'ENV=LIB'" '\n') fd.write('VAR=$OTHER\nOTHER=foo') finally: fd.close() d = sysconfig.parse_makefile(self.makefile) self.assertEqual(d, {'CONFIG_ARGS': "'--arg1=optarg1' 'ENV=LIB'", 'OTHER': 'foo'}) def test_parse_makefile_literal_dollar(self): self.makefile = TESTFN fd = open(self.makefile, 'w') try: fd.write(r"CONFIG_ARGS= '--arg1=optarg1' 'ENV=\$$LIB'" '\n') fd.write('VAR=$OTHER\nOTHER=foo') finally: fd.close() d = sysconfig.parse_makefile(self.makefile) self.assertEqual(d, {'CONFIG_ARGS': r"'--arg1=optarg1' 'ENV=\$LIB'", 'OTHER': 'foo'}) def test_sysconfig_module(self): import sysconfig as global_sysconfig self.assertEqual(global_sysconfig.get_config_var('CFLAGS'), sysconfig.get_config_var('CFLAGS')) self.assertEqual(global_sysconfig.get_config_var('LDFLAGS'), sysconfig.get_config_var('LDFLAGS')) @unittest.skipIf(sysconfig.get_config_var('CUSTOMIZED_OSX_COMPILER'), 'compiler flags customized') def test_sysconfig_compiler_vars(self): # On OS X, binary installers support extension module building on # various levels of the operating system with differing Xcode # configurations. This requires customization of some of the # compiler configuration directives to suit the environment on # the installed machine. Some of these customizations may require # running external programs and, so, are deferred until needed by # the first extension module build. With Python 3.3, only # the Distutils version of sysconfig is used for extension module # builds, which happens earlier in the Distutils tests. This may # cause the following tests to fail since no tests have caused # the global version of sysconfig to call the customization yet. # The solution for now is to simply skip this test in this case. # The longer-term solution is to only have one version of sysconfig. import sysconfig as global_sysconfig if sysconfig.get_config_var('CUSTOMIZED_OSX_COMPILER'): self.skipTest('compiler flags customized') self.assertEqual(global_sysconfig.get_config_var('LDSHARED'), sysconfig.get_config_var('LDSHARED')) self.assertEqual(global_sysconfig.get_config_var('CC'), sysconfig.get_config_var('CC')) @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None, 'EXT_SUFFIX required for this test') def test_SO_deprecation(self): self.assertWarns(DeprecationWarning, sysconfig.get_config_var, 'SO') @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None, 'EXT_SUFFIX required for this test') def test_SO_value(self): with check_warnings(('', DeprecationWarning)): self.assertEqual(sysconfig.get_config_var('SO'), sysconfig.get_config_var('EXT_SUFFIX')) @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None, 'EXT_SUFFIX required for this test') def test_SO_in_vars(self): vars = sysconfig.get_config_vars() self.assertIsNotNone(vars['SO']) self.assertEqual(vars['SO'], vars['EXT_SUFFIX']) def test_customize_compiler_before_get_config_vars(self): # Issue #21923: test that a Distribution compiler # instance can be called without an explicit call to # get_config_vars(). with open(TESTFN, 'w') as f: f.writelines(textwrap.dedent('''\ from distutils.core import Distribution config = Distribution().get_command_obj('config') # try_compile may pass or it may fail if no compiler # is found but it should not raise an exception. rc = config.try_compile('int x;') ''')) p = subprocess.Popen([str(sys.executable), TESTFN], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) outs, errs = p.communicate() self.assertEqual(0, p.returncode, "Subprocess failed: " + outs) def test_suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(SysconfigTestCase)) return suite if __name__ == '__main__': run_unittest(test_suite())
7,966
199
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/Setup.sample
# Setup file from the pygame project #--StartConfig SDL = -I/usr/include/SDL -D_REENTRANT -lSDL FONT = -lSDL_ttf IMAGE = -lSDL_image MIXER = -lSDL_mixer SMPEG = -lsmpeg PNG = -lpng JPEG = -ljpeg SCRAP = -lX11 PORTMIDI = -lportmidi PORTTIME = -lporttime #--EndConfig #DEBUG = -C-W -C-Wall DEBUG = #the following modules are optional. you will want to compile #everything you can, but you can ignore ones you don't have #dependencies for, just comment them out imageext src/imageext.c $(SDL) $(IMAGE) $(PNG) $(JPEG) $(DEBUG) font src/font.c $(SDL) $(FONT) $(DEBUG) mixer src/mixer.c $(SDL) $(MIXER) $(DEBUG) mixer_music src/music.c $(SDL) $(MIXER) $(DEBUG) _numericsurfarray src/_numericsurfarray.c $(SDL) $(DEBUG) _numericsndarray src/_numericsndarray.c $(SDL) $(MIXER) $(DEBUG) movie src/movie.c $(SDL) $(SMPEG) $(DEBUG) scrap src/scrap.c $(SDL) $(SCRAP) $(DEBUG) _camera src/_camera.c src/camera_v4l2.c src/camera_v4l.c $(SDL) $(DEBUG) pypm src/pypm.c $(SDL) $(PORTMIDI) $(PORTTIME) $(DEBUG) GFX = src/SDL_gfx/SDL_gfxPrimitives.c #GFX = src/SDL_gfx/SDL_gfxBlitFunc.c src/SDL_gfx/SDL_gfxPrimitives.c gfxdraw src/gfxdraw.c $(SDL) $(GFX) $(DEBUG) #these modules are required for pygame to run. they only require #SDL as a dependency. these should not be altered base src/base.c $(SDL) $(DEBUG) cdrom src/cdrom.c $(SDL) $(DEBUG) color src/color.c $(SDL) $(DEBUG) constants src/constants.c $(SDL) $(DEBUG) display src/display.c $(SDL) $(DEBUG) event src/event.c $(SDL) $(DEBUG) fastevent src/fastevent.c src/fastevents.c $(SDL) $(DEBUG) key src/key.c $(SDL) $(DEBUG) mouse src/mouse.c $(SDL) $(DEBUG) rect src/rect.c $(SDL) $(DEBUG) rwobject src/rwobject.c $(SDL) $(DEBUG) surface src/surface.c src/alphablit.c src/surface_fill.c $(SDL) $(DEBUG) surflock src/surflock.c $(SDL) $(DEBUG) time src/time.c $(SDL) $(DEBUG) joystick src/joystick.c $(SDL) $(DEBUG) draw src/draw.c $(SDL) $(DEBUG) image src/image.c $(SDL) $(DEBUG) overlay src/overlay.c $(SDL) $(DEBUG) transform src/transform.c src/rotozoom.c src/scale2x.c src/scale_mmx.c $(SDL) $(DEBUG) mask src/mask.c src/bitmask.c $(SDL) $(DEBUG) bufferproxy src/bufferproxy.c $(SDL) $(DEBUG) pixelarray src/pixelarray.c $(SDL) $(DEBUG) _arraysurfarray src/_arraysurfarray.c $(SDL) $(DEBUG)
2,249
68
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_register.py
"""Tests for distutils.command.register.""" import os import unittest import getpass import urllib import warnings from test.support import check_warnings, run_unittest from distutils.command import register as register_module from distutils.command.register import register from distutils.errors import DistutilsSetupError from distutils.log import INFO from distutils.tests.test_config import BasePyPIRCCommandTestCase try: import docutils except ImportError: docutils = None PYPIRC_NOPASSWORD = """\ [distutils] index-servers = server1 [server1] username:me """ WANTED_PYPIRC = """\ [distutils] index-servers = pypi [pypi] username:tarek password:password """ class Inputs(object): """Fakes user inputs.""" def __init__(self, *answers): self.answers = answers self.index = 0 def __call__(self, prompt=''): try: return self.answers[self.index] finally: self.index += 1 class FakeOpener(object): """Fakes a PyPI server""" def __init__(self): self.reqs = [] def __call__(self, *args): return self def open(self, req, data=None, timeout=None): self.reqs.append(req) return self def read(self): return b'xxx' def getheader(self, name, default=None): return { 'content-type': 'text/plain; charset=utf-8', }.get(name.lower(), default) class RegisterTestCase(BasePyPIRCCommandTestCase): def setUp(self): super(RegisterTestCase, self).setUp() # patching the password prompt self._old_getpass = getpass.getpass def _getpass(prompt): return 'password' getpass.getpass = _getpass urllib.request._opener = None self.old_opener = urllib.request.build_opener self.conn = urllib.request.build_opener = FakeOpener() def tearDown(self): getpass.getpass = self._old_getpass urllib.request._opener = None urllib.request.build_opener = self.old_opener super(RegisterTestCase, self).tearDown() def _get_cmd(self, metadata=None): if metadata is None: metadata = {'url': 'xxx', 'author': 'xxx', 'author_email': 'xxx', 'name': 'xxx', 'version': 'xxx'} pkg_info, dist = self.create_dist(**metadata) return register(dist) def test_create_pypirc(self): # this test makes sure a .pypirc file # is created when requested. # let's create a register instance cmd = self._get_cmd() # we shouldn't have a .pypirc file yet self.assertFalse(os.path.exists(self.rc)) # patching input and getpass.getpass # so register gets happy # # Here's what we are faking : # use your existing login (choice 1.) # Username : 'tarek' # Password : 'password' # Save your login (y/N)? : 'y' inputs = Inputs('1', 'tarek', 'y') register_module.input = inputs.__call__ # let's run the command try: cmd.run() finally: del register_module.input # we should have a brand new .pypirc file self.assertTrue(os.path.exists(self.rc)) # with the content similar to WANTED_PYPIRC f = open(self.rc) try: content = f.read() self.assertEqual(content, WANTED_PYPIRC) finally: f.close() # now let's make sure the .pypirc file generated # really works : we shouldn't be asked anything # if we run the command again def _no_way(prompt=''): raise AssertionError(prompt) register_module.input = _no_way cmd.show_response = 1 cmd.run() # let's see what the server received : we should # have 2 similar requests self.assertEqual(len(self.conn.reqs), 2) req1 = dict(self.conn.reqs[0].headers) req2 = dict(self.conn.reqs[1].headers) self.assertEqual(req1['Content-length'], '1374') self.assertEqual(req2['Content-length'], '1374') self.assertIn(b'xxx', self.conn.reqs[1].data) def test_password_not_in_file(self): self.write_file(self.rc, PYPIRC_NOPASSWORD) cmd = self._get_cmd() cmd._set_config() cmd.finalize_options() cmd.send_metadata() # dist.password should be set # therefore used afterwards by other commands self.assertEqual(cmd.distribution.password, 'password') def test_registering(self): # this test runs choice 2 cmd = self._get_cmd() inputs = Inputs('2', 'tarek', '[email protected]') register_module.input = inputs.__call__ try: # let's run the command cmd.run() finally: del register_module.input # we should have send a request self.assertEqual(len(self.conn.reqs), 1) req = self.conn.reqs[0] headers = dict(req.headers) self.assertEqual(headers['Content-length'], '608') self.assertIn(b'tarek', req.data) def test_password_reset(self): # this test runs choice 3 cmd = self._get_cmd() inputs = Inputs('3', '[email protected]') register_module.input = inputs.__call__ try: # let's run the command cmd.run() finally: del register_module.input # we should have send a request self.assertEqual(len(self.conn.reqs), 1) req = self.conn.reqs[0] headers = dict(req.headers) self.assertEqual(headers['Content-length'], '290') self.assertIn(b'tarek', req.data) @unittest.skipUnless(docutils is not None, 'needs docutils') def test_strict(self): # testing the script option # when on, the register command stops if # the metadata is incomplete or if # long_description is not reSt compliant # empty metadata cmd = self._get_cmd({}) cmd.ensure_finalized() cmd.strict = 1 self.assertRaises(DistutilsSetupError, cmd.run) # metadata are OK but long_description is broken metadata = {'url': 'xxx', 'author': 'xxx', 'author_email': 'éxéxé', 'name': 'xxx', 'version': 'xxx', 'long_description': 'title\n==\n\ntext'} cmd = self._get_cmd(metadata) cmd.ensure_finalized() cmd.strict = 1 self.assertRaises(DistutilsSetupError, cmd.run) # now something that works metadata['long_description'] = 'title\n=====\n\ntext' cmd = self._get_cmd(metadata) cmd.ensure_finalized() cmd.strict = 1 inputs = Inputs('1', 'tarek', 'y') register_module.input = inputs.__call__ # let's run the command try: cmd.run() finally: del register_module.input # strict is not by default cmd = self._get_cmd() cmd.ensure_finalized() inputs = Inputs('1', 'tarek', 'y') register_module.input = inputs.__call__ # let's run the command try: cmd.run() finally: del register_module.input # and finally a Unicode test (bug #12114) metadata = {'url': 'xxx', 'author': '\u00c9ric', 'author_email': 'xxx', 'name': 'xxx', 'version': 'xxx', 'description': 'Something about esszet \u00df', 'long_description': 'More things about esszet \u00df'} cmd = self._get_cmd(metadata) cmd.ensure_finalized() cmd.strict = 1 inputs = Inputs('1', 'tarek', 'y') register_module.input = inputs.__call__ # let's run the command try: cmd.run() finally: del register_module.input @unittest.skipUnless(docutils is not None, 'needs docutils') def test_register_invalid_long_description(self): description = ':funkie:`str`' # mimic Sphinx-specific markup metadata = {'url': 'xxx', 'author': 'xxx', 'author_email': 'xxx', 'name': 'xxx', 'version': 'xxx', 'long_description': description} cmd = self._get_cmd(metadata) cmd.ensure_finalized() cmd.strict = True inputs = Inputs('2', 'tarek', '[email protected]') register_module.input = inputs self.addCleanup(delattr, register_module, 'input') self.assertRaises(DistutilsSetupError, cmd.run) def test_check_metadata_deprecated(self): # makes sure make_metadata is deprecated cmd = self._get_cmd() with check_warnings() as w: warnings.simplefilter("always") cmd.check_metadata() self.assertEqual(len(w.warnings), 1) def test_list_classifiers(self): cmd = self._get_cmd() cmd.list_classifiers = 1 cmd.run() results = self.get_logs(INFO) self.assertEqual(results, ['running check', 'xxx']) def test_show_response(self): # test that the --show-response option return a well formatted response cmd = self._get_cmd() inputs = Inputs('1', 'tarek', 'y') register_module.input = inputs.__call__ cmd.show_response = 1 try: cmd.run() finally: del register_module.input results = self.get_logs(INFO) self.assertEqual(results[3], 75 * '-' + '\nxxx\n' + 75 * '-') def test_suite(): return unittest.makeSuite(RegisterTestCase) if __name__ == "__main__": run_unittest(test_suite())
9,765
324
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_text_file.py
"""Tests for distutils.text_file.""" import os import unittest from distutils.text_file import TextFile from distutils.tests import support from test.support import run_unittest TEST_DATA = """# test file line 3 \\ # intervening comment continues on next line """ class TextFileTestCase(support.TempdirManager, unittest.TestCase): def test_class(self): # old tests moved from text_file.__main__ # so they are really called by the buildbots # result 1: no fancy options result1 = ['# test file\n', '\n', 'line 3 \\\n', '# intervening comment\n', ' continues on next line\n'] # result 2: just strip comments result2 = ["\n", "line 3 \\\n", " continues on next line\n"] # result 3: just strip blank lines result3 = ["# test file\n", "line 3 \\\n", "# intervening comment\n", " continues on next line\n"] # result 4: default, strip comments, blank lines, # and trailing whitespace result4 = ["line 3 \\", " continues on next line"] # result 5: strip comments and blanks, plus join lines (but don't # "collapse" joined lines result5 = ["line 3 continues on next line"] # result 6: strip comments and blanks, plus join lines (and # "collapse" joined lines result6 = ["line 3 continues on next line"] def test_input(count, description, file, expected_result): result = file.readlines() self.assertEqual(result, expected_result) tmpdir = self.mkdtemp() filename = os.path.join(tmpdir, "test.txt") out_file = open(filename, "w") try: out_file.write(TEST_DATA) finally: out_file.close() in_file = TextFile(filename, strip_comments=0, skip_blanks=0, lstrip_ws=0, rstrip_ws=0) try: test_input(1, "no processing", in_file, result1) finally: in_file.close() in_file = TextFile(filename, strip_comments=1, skip_blanks=0, lstrip_ws=0, rstrip_ws=0) try: test_input(2, "strip comments", in_file, result2) finally: in_file.close() in_file = TextFile(filename, strip_comments=0, skip_blanks=1, lstrip_ws=0, rstrip_ws=0) try: test_input(3, "strip blanks", in_file, result3) finally: in_file.close() in_file = TextFile(filename) try: test_input(4, "default processing", in_file, result4) finally: in_file.close() in_file = TextFile(filename, strip_comments=1, skip_blanks=1, join_lines=1, rstrip_ws=1) try: test_input(5, "join lines without collapsing", in_file, result5) finally: in_file.close() in_file = TextFile(filename, strip_comments=1, skip_blanks=1, join_lines=1, rstrip_ws=1, collapse_join=1) try: test_input(6, "join lines with collapsing", in_file, result6) finally: in_file.close() def test_suite(): return unittest.makeSuite(TextFileTestCase) if __name__ == "__main__": run_unittest(test_suite())
3,436
108
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_bdist_rpm.py
"""Tests for distutils.command.bdist_rpm.""" import unittest import sys import os from test.support import run_unittest, requires_zlib from distutils.core import Distribution from distutils.command.bdist_rpm import bdist_rpm from distutils.tests import support from distutils.spawn import find_executable SETUP_PY = """\ from distutils.core import setup import foo setup(name='foo', version='0.1', py_modules=['foo'], url='xxx', author='xxx', author_email='xxx') """ class BuildRpmTestCase(support.TempdirManager, support.EnvironGuard, support.LoggingSilencer, unittest.TestCase): def setUp(self): try: sys.executable.encode("UTF-8") except UnicodeEncodeError: raise unittest.SkipTest("sys.executable is not encodable to UTF-8") super(BuildRpmTestCase, self).setUp() self.old_location = os.getcwd() self.old_sys_argv = sys.argv, sys.argv[:] def tearDown(self): os.chdir(self.old_location) sys.argv = self.old_sys_argv[0] sys.argv[:] = self.old_sys_argv[1] super(BuildRpmTestCase, self).tearDown() # XXX I am unable yet to make this test work without # spurious sdtout/stderr output under Mac OS X @unittest.skipUnless(sys.platform.startswith('linux'), 'spurious sdtout/stderr output under Mac OS X') @requires_zlib @unittest.skipIf(find_executable('rpm') is None, 'the rpm command is not found') @unittest.skipIf(find_executable('rpmbuild') is None, 'the rpmbuild command is not found') def test_quiet(self): # let's create a package tmp_dir = self.mkdtemp() os.environ['HOME'] = tmp_dir # to confine dir '.rpmdb' creation pkg_dir = os.path.join(tmp_dir, 'foo') os.mkdir(pkg_dir) self.write_file((pkg_dir, 'setup.py'), SETUP_PY) self.write_file((pkg_dir, 'foo.py'), '#') self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py') self.write_file((pkg_dir, 'README'), '') dist = Distribution({'name': 'foo', 'version': '0.1', 'py_modules': ['foo'], 'url': 'xxx', 'author': 'xxx', 'author_email': 'xxx'}) dist.script_name = 'setup.py' os.chdir(pkg_dir) sys.argv = ['setup.py'] cmd = bdist_rpm(dist) cmd.fix_python = True # running in quiet mode cmd.quiet = 1 cmd.ensure_finalized() cmd.run() dist_created = os.listdir(os.path.join(pkg_dir, 'dist')) self.assertIn('foo-0.1-1.noarch.rpm', dist_created) # bug #2945: upload ignores bdist_rpm files self.assertIn(('bdist_rpm', 'any', 'dist/foo-0.1-1.src.rpm'), dist.dist_files) self.assertIn(('bdist_rpm', 'any', 'dist/foo-0.1-1.noarch.rpm'), dist.dist_files) # XXX I am unable yet to make this test work without # spurious sdtout/stderr output under Mac OS X @unittest.skipUnless(sys.platform.startswith('linux'), 'spurious sdtout/stderr output under Mac OS X') @requires_zlib # http://bugs.python.org/issue1533164 @unittest.skipIf(find_executable('rpm') is None, 'the rpm command is not found') @unittest.skipIf(find_executable('rpmbuild') is None, 'the rpmbuild command is not found') def test_no_optimize_flag(self): # let's create a package that breaks bdist_rpm tmp_dir = self.mkdtemp() os.environ['HOME'] = tmp_dir # to confine dir '.rpmdb' creation pkg_dir = os.path.join(tmp_dir, 'foo') os.mkdir(pkg_dir) self.write_file((pkg_dir, 'setup.py'), SETUP_PY) self.write_file((pkg_dir, 'foo.py'), '#') self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py') self.write_file((pkg_dir, 'README'), '') dist = Distribution({'name': 'foo', 'version': '0.1', 'py_modules': ['foo'], 'url': 'xxx', 'author': 'xxx', 'author_email': 'xxx'}) dist.script_name = 'setup.py' os.chdir(pkg_dir) sys.argv = ['setup.py'] cmd = bdist_rpm(dist) cmd.fix_python = True cmd.quiet = 1 cmd.ensure_finalized() cmd.run() dist_created = os.listdir(os.path.join(pkg_dir, 'dist')) self.assertIn('foo-0.1-1.noarch.rpm', dist_created) # bug #2945: upload ignores bdist_rpm files self.assertIn(('bdist_rpm', 'any', 'dist/foo-0.1-1.src.rpm'), dist.dist_files) self.assertIn(('bdist_rpm', 'any', 'dist/foo-0.1-1.noarch.rpm'), dist.dist_files) os.remove(os.path.join(pkg_dir, 'dist', 'foo-0.1-1.noarch.rpm')) def test_suite(): return unittest.makeSuite(BuildRpmTestCase) if __name__ == '__main__': run_unittest(test_suite())
5,008
136
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_file_util.py
"""Tests for distutils.file_util.""" import unittest import os import errno from unittest.mock import patch from distutils.file_util import move_file, copy_file from distutils import log from distutils.tests import support from distutils.errors import DistutilsFileError from test.support import run_unittest class FileUtilTestCase(support.TempdirManager, unittest.TestCase): def _log(self, msg, *args): if len(args) > 0: self._logs.append(msg % args) else: self._logs.append(msg) def setUp(self): super(FileUtilTestCase, self).setUp() self._logs = [] self.old_log = log.info log.info = self._log tmp_dir = self.mkdtemp() self.source = os.path.join(tmp_dir, 'f1') self.target = os.path.join(tmp_dir, 'f2') self.target_dir = os.path.join(tmp_dir, 'd1') def tearDown(self): log.info = self.old_log super(FileUtilTestCase, self).tearDown() def test_move_file_verbosity(self): f = open(self.source, 'w') try: f.write('some content') finally: f.close() move_file(self.source, self.target, verbose=0) wanted = [] self.assertEqual(self._logs, wanted) # back to original state move_file(self.target, self.source, verbose=0) move_file(self.source, self.target, verbose=1) wanted = ['moving %s -> %s' % (self.source, self.target)] self.assertEqual(self._logs, wanted) # back to original state move_file(self.target, self.source, verbose=0) self._logs = [] # now the target is a dir os.mkdir(self.target_dir) move_file(self.source, self.target_dir, verbose=1) wanted = ['moving %s -> %s' % (self.source, self.target_dir)] self.assertEqual(self._logs, wanted) def test_move_file_exception_unpacking_rename(self): # see issue 22182 with patch("os.rename", side_effect=OSError("wrong", 1)), \ self.assertRaises(DistutilsFileError): with open(self.source, 'w') as fobj: fobj.write('spam eggs') move_file(self.source, self.target, verbose=0) def test_move_file_exception_unpacking_unlink(self): # see issue 22182 with patch("os.rename", side_effect=OSError(errno.EXDEV, "wrong")), \ patch("os.unlink", side_effect=OSError("wrong", 1)), \ self.assertRaises(DistutilsFileError): with open(self.source, 'w') as fobj: fobj.write('spam eggs') move_file(self.source, self.target, verbose=0) def test_copy_file_hard_link(self): with open(self.source, 'w') as f: f.write('some content') st = os.stat(self.source) copy_file(self.source, self.target, link='hard') st2 = os.stat(self.source) st3 = os.stat(self.target) self.assertTrue(os.path.samestat(st, st2), (st, st2)) self.assertTrue(os.path.samestat(st2, st3), (st2, st3)) with open(self.source, 'r') as f: self.assertEqual(f.read(), 'some content') def test_copy_file_hard_link_failure(self): # If hard linking fails, copy_file() falls back on copying file # (some special filesystems don't support hard linking even under # Unix, see issue #8876). with open(self.source, 'w') as f: f.write('some content') st = os.stat(self.source) with patch("os.link", side_effect=OSError(0, "linking unsupported")): copy_file(self.source, self.target, link='hard') st2 = os.stat(self.source) st3 = os.stat(self.target) self.assertTrue(os.path.samestat(st, st2), (st, st2)) self.assertFalse(os.path.samestat(st2, st3), (st2, st3)) for fn in (self.source, self.target): with open(fn, 'r') as f: self.assertEqual(f.read(), 'some content') def test_suite(): return unittest.makeSuite(FileUtilTestCase) if __name__ == "__main__": run_unittest(test_suite())
4,103
115
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_msvc9compiler.py
"""Tests for distutils.msvc9compiler.""" import sys import unittest import os from distutils.errors import DistutilsPlatformError from distutils.tests import support from test.support import run_unittest # A manifest with the only assembly reference being the msvcrt assembly, so # should have the assembly completely stripped. Note that although the # assembly has a <security> reference the assembly is removed - that is # currently a "feature", not a bug :) _MANIFEST_WITH_ONLY_MSVC_REFERENCE = """\ <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> <security> <requestedPrivileges> <requestedExecutionLevel level="asInvoker" uiAccess="false"> </requestedExecutionLevel> </requestedPrivileges> </security> </trustInfo> <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken="XXXX"> </assemblyIdentity> </dependentAssembly> </dependency> </assembly> """ # A manifest with references to assemblies other than msvcrt. When processed, # this assembly should be returned with just the msvcrt part removed. _MANIFEST_WITH_MULTIPLE_REFERENCES = """\ <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> <security> <requestedPrivileges> <requestedExecutionLevel level="asInvoker" uiAccess="false"> </requestedExecutionLevel> </requestedPrivileges> </security> </trustInfo> <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken="XXXX"> </assemblyIdentity> </dependentAssembly> </dependency> <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.VC90.MFC" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken="XXXX"></assemblyIdentity> </dependentAssembly> </dependency> </assembly> """ _CLEANED_MANIFEST = """\ <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> <security> <requestedPrivileges> <requestedExecutionLevel level="asInvoker" uiAccess="false"> </requestedExecutionLevel> </requestedPrivileges> </security> </trustInfo> <dependency> </dependency> <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.VC90.MFC" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken="XXXX"></assemblyIdentity> </dependentAssembly> </dependency> </assembly>""" if sys.platform=="win32": from distutils.msvccompiler import get_build_version if get_build_version()>=8.0: SKIP_MESSAGE = None else: SKIP_MESSAGE = "These tests are only for MSVC8.0 or above" else: SKIP_MESSAGE = "These tests are only for win32" @unittest.skipUnless(SKIP_MESSAGE is None, SKIP_MESSAGE) class msvc9compilerTestCase(support.TempdirManager, unittest.TestCase): def test_no_compiler(self): # makes sure query_vcvarsall raises # a DistutilsPlatformError if the compiler # is not found from distutils.msvc9compiler import query_vcvarsall def _find_vcvarsall(version): return None from distutils import msvc9compiler old_find_vcvarsall = msvc9compiler.find_vcvarsall msvc9compiler.find_vcvarsall = _find_vcvarsall try: self.assertRaises(DistutilsPlatformError, query_vcvarsall, 'wont find this version') finally: msvc9compiler.find_vcvarsall = old_find_vcvarsall def test_reg_class(self): from distutils.msvc9compiler import Reg self.assertRaises(KeyError, Reg.get_value, 'xxx', 'xxx') # looking for values that should exist on all # windows registry versions. path = r'Control Panel\Desktop' v = Reg.get_value(path, 'dragfullwindows') self.assertIn(v, ('0', '1', '2')) import winreg HKCU = winreg.HKEY_CURRENT_USER keys = Reg.read_keys(HKCU, 'xxxx') self.assertEqual(keys, None) keys = Reg.read_keys(HKCU, r'Control Panel') self.assertIn('Desktop', keys) def test_remove_visual_c_ref(self): from distutils.msvc9compiler import MSVCCompiler tempdir = self.mkdtemp() manifest = os.path.join(tempdir, 'manifest') f = open(manifest, 'w') try: f.write(_MANIFEST_WITH_MULTIPLE_REFERENCES) finally: f.close() compiler = MSVCCompiler() compiler._remove_visual_c_ref(manifest) # see what we got f = open(manifest) try: # removing trailing spaces content = '\n'.join([line.rstrip() for line in f.readlines()]) finally: f.close() # makes sure the manifest was properly cleaned self.assertEqual(content, _CLEANED_MANIFEST) def test_remove_entire_manifest(self): from distutils.msvc9compiler import MSVCCompiler tempdir = self.mkdtemp() manifest = os.path.join(tempdir, 'manifest') f = open(manifest, 'w') try: f.write(_MANIFEST_WITH_ONLY_MSVC_REFERENCE) finally: f.close() compiler = MSVCCompiler() got = compiler._remove_visual_c_ref(manifest) self.assertIsNone(got) def test_suite(): return unittest.makeSuite(msvc9compilerTestCase) if __name__ == "__main__": run_unittest(test_suite())
6,038
185
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_util.py
"""Tests for distutils.util.""" import os import sys import unittest from copy import copy from test.support import run_unittest from distutils.errors import DistutilsPlatformError, DistutilsByteCompileError from distutils.util import (get_platform, convert_path, change_root, check_environ, split_quoted, strtobool, rfc822_escape, byte_compile, grok_environment_error) from distutils import util # used to patch _environ_checked from distutils.sysconfig import get_config_vars from distutils import sysconfig from distutils.tests import support import _osx_support class UtilTestCase(support.EnvironGuard, unittest.TestCase): def setUp(self): super(UtilTestCase, self).setUp() # saving the environment self.name = os.name self.platform = sys.platform self.version = sys.version self.sep = os.sep self.join = os.path.join self.isabs = os.path.isabs self.splitdrive = os.path.splitdrive self._config_vars = copy(sysconfig._config_vars) # patching os.uname if hasattr(os, 'uname'): self.uname = os.uname self._uname = os.uname() else: self.uname = None self._uname = None os.uname = self._get_uname def tearDown(self): # getting back the environment os.name = self.name sys.platform = self.platform sys.version = self.version os.sep = self.sep os.path.join = self.join os.path.isabs = self.isabs os.path.splitdrive = self.splitdrive if self.uname is not None: os.uname = self.uname else: del os.uname sysconfig._config_vars = copy(self._config_vars) super(UtilTestCase, self).tearDown() def _set_uname(self, uname): self._uname = uname def _get_uname(self): return self._uname def test_get_platform(self): # windows XP, 32bits os.name = 'nt' sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' '[MSC v.1310 32 bit (Intel)]') sys.platform = 'win32' self.assertEqual(get_platform(), 'win32') # windows XP, amd64 os.name = 'nt' sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' '[MSC v.1310 32 bit (Amd64)]') sys.platform = 'win32' self.assertEqual(get_platform(), 'win-amd64') # windows XP, itanium os.name = 'nt' sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' '[MSC v.1310 32 bit (Itanium)]') sys.platform = 'win32' self.assertEqual(get_platform(), 'win-ia64') # macbook os.name = 'posix' sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) ' '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]') sys.platform = 'darwin' self._set_uname(('Darwin', 'macziade', '8.11.1', ('Darwin Kernel Version 8.11.1: ' 'Wed Oct 10 18:23:28 PDT 2007; ' 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386')) _osx_support._remove_original_values(get_config_vars()) get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3' get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g ' '-fwrapv -O3 -Wall -Wstrict-prototypes') cursize = sys.maxsize sys.maxsize = (2 ** 31)-1 try: self.assertEqual(get_platform(), 'macosx-10.3-i386') finally: sys.maxsize = cursize # macbook with fat binaries (fat, universal or fat64) _osx_support._remove_original_values(get_config_vars()) get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4' get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot ' '/Developer/SDKs/MacOSX10.4u.sdk ' '-fno-strict-aliasing -fno-common ' '-dynamic -DNDEBUG -g -O3') self.assertEqual(get_platform(), 'macosx-10.4-fat') _osx_support._remove_original_values(get_config_vars()) os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.1' self.assertEqual(get_platform(), 'macosx-10.4-fat') _osx_support._remove_original_values(get_config_vars()) get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot ' '/Developer/SDKs/MacOSX10.4u.sdk ' '-fno-strict-aliasing -fno-common ' '-dynamic -DNDEBUG -g -O3') self.assertEqual(get_platform(), 'macosx-10.4-intel') _osx_support._remove_original_values(get_config_vars()) get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot ' '/Developer/SDKs/MacOSX10.4u.sdk ' '-fno-strict-aliasing -fno-common ' '-dynamic -DNDEBUG -g -O3') self.assertEqual(get_platform(), 'macosx-10.4-fat3') _osx_support._remove_original_values(get_config_vars()) get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot ' '/Developer/SDKs/MacOSX10.4u.sdk ' '-fno-strict-aliasing -fno-common ' '-dynamic -DNDEBUG -g -O3') self.assertEqual(get_platform(), 'macosx-10.4-universal') _osx_support._remove_original_values(get_config_vars()) get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot ' '/Developer/SDKs/MacOSX10.4u.sdk ' '-fno-strict-aliasing -fno-common ' '-dynamic -DNDEBUG -g -O3') self.assertEqual(get_platform(), 'macosx-10.4-fat64') for arch in ('ppc', 'i386', 'x86_64', 'ppc64'): _osx_support._remove_original_values(get_config_vars()) get_config_vars()['CFLAGS'] = ('-arch %s -isysroot ' '/Developer/SDKs/MacOSX10.4u.sdk ' '-fno-strict-aliasing -fno-common ' '-dynamic -DNDEBUG -g -O3'%(arch,)) self.assertEqual(get_platform(), 'macosx-10.4-%s'%(arch,)) # linux debian sarge os.name = 'posix' sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) ' '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]') sys.platform = 'linux2' self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7', '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686')) self.assertEqual(get_platform(), 'linux-i686') # XXX more platforms to tests here def test_convert_path(self): # linux/mac os.sep = '/' def _join(path): return '/'.join(path) os.path.join = _join self.assertEqual(convert_path('/home/to/my/stuff'), '/home/to/my/stuff') # win os.sep = '\\' def _join(*path): return '\\'.join(path) os.path.join = _join self.assertRaises(ValueError, convert_path, '/home/to/my/stuff') self.assertRaises(ValueError, convert_path, 'home/to/my/stuff/') self.assertEqual(convert_path('home/to/my/stuff'), 'home\\to\\my\\stuff') self.assertEqual(convert_path('.'), os.curdir) def test_change_root(self): # linux/mac os.name = 'posix' def _isabs(path): return path[0] == '/' os.path.isabs = _isabs def _join(*path): return '/'.join(path) os.path.join = _join self.assertEqual(change_root('/root', '/old/its/here'), '/root/old/its/here') self.assertEqual(change_root('/root', 'its/here'), '/root/its/here') # windows os.name = 'nt' def _isabs(path): return path.startswith('c:\\') os.path.isabs = _isabs def _splitdrive(path): if path.startswith('c:'): return ('', path.replace('c:', '')) return ('', path) os.path.splitdrive = _splitdrive def _join(*path): return '\\'.join(path) os.path.join = _join self.assertEqual(change_root('c:\\root', 'c:\\old\\its\\here'), 'c:\\root\\old\\its\\here') self.assertEqual(change_root('c:\\root', 'its\\here'), 'c:\\root\\its\\here') # BugsBunny os (it's a great os) os.name = 'BugsBunny' self.assertRaises(DistutilsPlatformError, change_root, 'c:\\root', 'its\\here') # XXX platforms to be covered: mac def test_check_environ(self): util._environ_checked = 0 if 'HOME' in os.environ: del os.environ['HOME'] # posix without HOME if os.name == 'posix': # this test won't run on windows check_environ() import pwd self.assertEqual(os.environ['HOME'], pwd.getpwuid(os.getuid())[5]) else: check_environ() self.assertEqual(os.environ['PLAT'], get_platform()) self.assertEqual(util._environ_checked, 1) def test_split_quoted(self): self.assertEqual(split_quoted('""one"" "two" \'three\' \\four'), ['one', 'two', 'three', 'four']) def test_strtobool(self): yes = ('y', 'Y', 'yes', 'True', 't', 'true', 'True', 'On', 'on', '1') no = ('n', 'no', 'f', 'false', 'off', '0', 'Off', 'No', 'N') for y in yes: self.assertTrue(strtobool(y)) for n in no: self.assertFalse(strtobool(n)) def test_rfc822_escape(self): header = 'I am a\npoor\nlonesome\nheader\n' res = rfc822_escape(header) wanted = ('I am a%(8s)spoor%(8s)slonesome%(8s)s' 'header%(8s)s') % {'8s': '\n'+8*' '} self.assertEqual(res, wanted) def test_dont_write_bytecode(self): # makes sure byte_compile raise a DistutilsError # if sys.dont_write_bytecode is True old_dont_write_bytecode = sys.dont_write_bytecode sys.dont_write_bytecode = True try: self.assertRaises(DistutilsByteCompileError, byte_compile, []) finally: sys.dont_write_bytecode = old_dont_write_bytecode def test_grok_environment_error(self): # test obsolete function to ensure backward compat (#4931) exc = IOError("Unable to find batch file") msg = grok_environment_error(exc) self.assertEqual(msg, "error: Unable to find batch file") def test_suite(): return unittest.makeSuite(UtilTestCase) if __name__ == "__main__": run_unittest(test_suite())
11,250
301
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_cygwinccompiler.py
"""Tests for distutils.cygwinccompiler.""" import unittest import sys import os from io import BytesIO from test.support import run_unittest from distutils import cygwinccompiler from distutils.cygwinccompiler import (check_config_h, CONFIG_H_OK, CONFIG_H_NOTOK, CONFIG_H_UNCERTAIN, get_versions, get_msvcr) from distutils.tests import support class FakePopen(object): test_class = None def __init__(self, cmd, shell, stdout): self.cmd = cmd.split()[0] exes = self.test_class._exes if self.cmd in exes: # issue #6438 in Python 3.x, Popen returns bytes self.stdout = BytesIO(exes[self.cmd]) else: self.stdout = os.popen(cmd, 'r') class CygwinCCompilerTestCase(support.TempdirManager, unittest.TestCase): def setUp(self): super(CygwinCCompilerTestCase, self).setUp() self.version = sys.version self.python_h = os.path.join(self.mkdtemp(), 'python.h') from distutils import sysconfig self.old_get_config_h_filename = sysconfig.get_config_h_filename sysconfig.get_config_h_filename = self._get_config_h_filename self.old_find_executable = cygwinccompiler.find_executable cygwinccompiler.find_executable = self._find_executable self._exes = {} self.old_popen = cygwinccompiler.Popen FakePopen.test_class = self cygwinccompiler.Popen = FakePopen def tearDown(self): sys.version = self.version from distutils import sysconfig sysconfig.get_config_h_filename = self.old_get_config_h_filename cygwinccompiler.find_executable = self.old_find_executable cygwinccompiler.Popen = self.old_popen super(CygwinCCompilerTestCase, self).tearDown() def _get_config_h_filename(self): return self.python_h def _find_executable(self, name): if name in self._exes: return name return None def test_check_config_h(self): # check_config_h looks for "GCC" in sys.version first # returns CONFIG_H_OK if found sys.version = ('2.6.1 (r261:67515, Dec 6 2008, 16:42:21) \n[GCC ' '4.0.1 (Apple Computer, Inc. build 5370)]') self.assertEqual(check_config_h()[0], CONFIG_H_OK) # then it tries to see if it can find "__GNUC__" in pyconfig.h sys.version = 'something without the *CC word' # if the file doesn't exist it returns CONFIG_H_UNCERTAIN self.assertEqual(check_config_h()[0], CONFIG_H_UNCERTAIN) # if it exists but does not contain __GNUC__, it returns CONFIG_H_NOTOK self.write_file(self.python_h, 'xxx') self.assertEqual(check_config_h()[0], CONFIG_H_NOTOK) # and CONFIG_H_OK if __GNUC__ is found self.write_file(self.python_h, 'xxx __GNUC__ xxx') self.assertEqual(check_config_h()[0], CONFIG_H_OK) def test_get_versions(self): # get_versions calls distutils.spawn.find_executable on # 'gcc', 'ld' and 'dllwrap' self.assertEqual(get_versions(), (None, None, None)) # Let's fake we have 'gcc' and it returns '3.4.5' self._exes['gcc'] = b'gcc (GCC) 3.4.5 (mingw special)\nFSF' res = get_versions() self.assertEqual(str(res[0]), '3.4.5') # and let's see what happens when the version # doesn't match the regular expression # (\d+\.\d+(\.\d+)*) self._exes['gcc'] = b'very strange output' res = get_versions() self.assertEqual(res[0], None) # same thing for ld self._exes['ld'] = b'GNU ld version 2.17.50 20060824' res = get_versions() self.assertEqual(str(res[1]), '2.17.50') self._exes['ld'] = b'@(#)PROGRAM:ld PROJECT:ld64-77' res = get_versions() self.assertEqual(res[1], None) # and dllwrap self._exes['dllwrap'] = b'GNU dllwrap 2.17.50 20060824\nFSF' res = get_versions() self.assertEqual(str(res[2]), '2.17.50') self._exes['dllwrap'] = b'Cheese Wrap' res = get_versions() self.assertEqual(res[2], None) def test_get_msvcr(self): # none sys.version = ('2.6.1 (r261:67515, Dec 6 2008, 16:42:21) ' '\n[GCC 4.0.1 (Apple Computer, Inc. build 5370)]') self.assertEqual(get_msvcr(), None) # MSVC 7.0 sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) ' '[MSC v.1300 32 bits (Intel)]') self.assertEqual(get_msvcr(), ['msvcr70']) # MSVC 7.1 sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) ' '[MSC v.1310 32 bits (Intel)]') self.assertEqual(get_msvcr(), ['msvcr71']) # VS2005 / MSVC 8.0 sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) ' '[MSC v.1400 32 bits (Intel)]') self.assertEqual(get_msvcr(), ['msvcr80']) # VS2008 / MSVC 9.0 sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) ' '[MSC v.1500 32 bits (Intel)]') self.assertEqual(get_msvcr(), ['msvcr90']) # unknown sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) ' '[MSC v.1999 32 bits (Intel)]') self.assertRaises(ValueError, get_msvcr) def test_suite(): return unittest.makeSuite(CygwinCCompilerTestCase) if __name__ == '__main__': run_unittest(test_suite())
5,636
155
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_upload.py
"""Tests for distutils.command.upload.""" import os import unittest import unittest.mock as mock from urllib.request import HTTPError from test.support import run_unittest from distutils.command import upload as upload_mod from distutils.command.upload import upload from distutils.core import Distribution from distutils.errors import DistutilsError from distutils.log import ERROR, INFO from distutils.tests.test_config import PYPIRC, BasePyPIRCCommandTestCase PYPIRC_LONG_PASSWORD = """\ [distutils] index-servers = server1 server2 [server1] username:me password:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa [server2] username:meagain password: secret realm:acme repository:http://another.pypi/ """ PYPIRC_NOPASSWORD = """\ [distutils] index-servers = server1 [server1] username:me """ class FakeOpen(object): def __init__(self, url, msg=None, code=None): self.url = url if not isinstance(url, str): self.req = url else: self.req = None self.msg = msg or 'OK' self.code = code or 200 def getheader(self, name, default=None): return { 'content-type': 'text/plain; charset=utf-8', }.get(name.lower(), default) def read(self): return b'xyzzy' def getcode(self): return self.code class uploadTestCase(BasePyPIRCCommandTestCase): def setUp(self): super(uploadTestCase, self).setUp() self.old_open = upload_mod.urlopen upload_mod.urlopen = self._urlopen self.last_open = None self.next_msg = None self.next_code = None def tearDown(self): upload_mod.urlopen = self.old_open super(uploadTestCase, self).tearDown() def _urlopen(self, url): self.last_open = FakeOpen(url, msg=self.next_msg, code=self.next_code) return self.last_open def test_finalize_options(self): # new format self.write_file(self.rc, PYPIRC) dist = Distribution() cmd = upload(dist) cmd.finalize_options() for attr, waited in (('username', 'me'), ('password', 'secret'), ('realm', 'pypi'), ('repository', 'https://upload.pypi.org/legacy/')): self.assertEqual(getattr(cmd, attr), waited) def test_saved_password(self): # file with no password self.write_file(self.rc, PYPIRC_NOPASSWORD) # make sure it passes dist = Distribution() cmd = upload(dist) cmd.finalize_options() self.assertEqual(cmd.password, None) # make sure we get it as well, if another command # initialized it at the dist level dist.password = 'xxx' cmd = upload(dist) cmd.finalize_options() self.assertEqual(cmd.password, 'xxx') def test_upload(self): tmp = self.mkdtemp() path = os.path.join(tmp, 'xxx') self.write_file(path) command, pyversion, filename = 'xxx', '2.6', path dist_files = [(command, pyversion, filename)] self.write_file(self.rc, PYPIRC_LONG_PASSWORD) # lets run it pkg_dir, dist = self.create_dist(dist_files=dist_files) cmd = upload(dist) cmd.show_response = 1 cmd.ensure_finalized() cmd.run() # what did we send ? headers = dict(self.last_open.req.headers) self.assertEqual(headers['Content-length'], '2162') content_type = headers['Content-type'] self.assertTrue(content_type.startswith('multipart/form-data')) self.assertEqual(self.last_open.req.get_method(), 'POST') expected_url = 'https://upload.pypi.org/legacy/' self.assertEqual(self.last_open.req.get_full_url(), expected_url) self.assertTrue(b'xxx' in self.last_open.req.data) self.assertIn(b'protocol_version', self.last_open.req.data) # The PyPI response body was echoed results = self.get_logs(INFO) self.assertEqual(results[-1], 75 * '-' + '\nxyzzy\n' + 75 * '-') # bpo-32304: archives whose last byte was b'\r' were corrupted due to # normalization intended for Mac OS 9. def test_upload_correct_cr(self): # content that ends with \r should not be modified. tmp = self.mkdtemp() path = os.path.join(tmp, 'xxx') self.write_file(path, content='yy\r') command, pyversion, filename = 'xxx', '2.6', path dist_files = [(command, pyversion, filename)] self.write_file(self.rc, PYPIRC_LONG_PASSWORD) # other fields that ended with \r used to be modified, now are # preserved. pkg_dir, dist = self.create_dist( dist_files=dist_files, description='long description\r' ) cmd = upload(dist) cmd.show_response = 1 cmd.ensure_finalized() cmd.run() headers = dict(self.last_open.req.headers) self.assertEqual(headers['Content-length'], '2172') self.assertIn(b'long description\r', self.last_open.req.data) def test_upload_fails(self): self.next_msg = "Not Found" self.next_code = 404 self.assertRaises(DistutilsError, self.test_upload) def test_wrong_exception_order(self): tmp = self.mkdtemp() path = os.path.join(tmp, 'xxx') self.write_file(path) dist_files = [('xxx', '2.6', path)] # command, pyversion, filename self.write_file(self.rc, PYPIRC_LONG_PASSWORD) pkg_dir, dist = self.create_dist(dist_files=dist_files) tests = [ (OSError('oserror'), 'oserror', OSError), (HTTPError('url', 400, 'httperror', {}, None), 'Upload failed (400): httperror', DistutilsError), ] for exception, expected, raised_exception in tests: with self.subTest(exception=type(exception).__name__): with mock.patch('distutils.command.upload.urlopen', new=mock.Mock(side_effect=exception)): with self.assertRaises(raised_exception): cmd = upload(dist) cmd.ensure_finalized() cmd.run() results = self.get_logs(ERROR) self.assertIn(expected, results[-1]) self.clear_logs() def test_suite(): return unittest.makeSuite(uploadTestCase) if __name__ == "__main__": run_unittest(test_suite())
6,535
208
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_filelist.py
"""Tests for distutils.filelist.""" import os import re import unittest from distutils import debug from distutils.log import WARN from distutils.errors import DistutilsTemplateError from distutils.filelist import glob_to_re, translate_pattern, FileList from distutils import filelist import test.support from test.support import captured_stdout, run_unittest from distutils.tests import support MANIFEST_IN = """\ include ok include xo exclude xo include foo.tmp include buildout.cfg global-include *.x global-include *.txt global-exclude *.tmp recursive-include f *.oo recursive-exclude global *.x graft dir prune dir3 """ def make_local_path(s): """Converts '/' in a string to os.sep""" return s.replace('/', os.sep) class FileListTestCase(support.LoggingSilencer, unittest.TestCase): def assertNoWarnings(self): self.assertEqual(self.get_logs(WARN), []) self.clear_logs() def assertWarnings(self): self.assertGreater(len(self.get_logs(WARN)), 0) self.clear_logs() def test_glob_to_re(self): sep = os.sep if os.sep == '\\': sep = re.escape(os.sep) for glob, regex in ( # simple cases ('foo*', r'(?s:foo[^%(sep)s]*)\Z'), ('foo?', r'(?s:foo[^%(sep)s])\Z'), ('foo??', r'(?s:foo[^%(sep)s][^%(sep)s])\Z'), # special cases (r'foo\\*', r'(?s:foo\\\\[^%(sep)s]*)\Z'), (r'foo\\\*', r'(?s:foo\\\\\\[^%(sep)s]*)\Z'), ('foo????', r'(?s:foo[^%(sep)s][^%(sep)s][^%(sep)s][^%(sep)s])\Z'), (r'foo\\??', r'(?s:foo\\\\[^%(sep)s][^%(sep)s])\Z')): regex = regex % {'sep': sep} self.assertEqual(glob_to_re(glob), regex) def test_process_template_line(self): # testing all MANIFEST.in template patterns file_list = FileList() l = make_local_path # simulated file list file_list.allfiles = ['foo.tmp', 'ok', 'xo', 'four.txt', 'buildout.cfg', # filelist does not filter out VCS directories, # it's sdist that does l('.hg/last-message.txt'), l('global/one.txt'), l('global/two.txt'), l('global/files.x'), l('global/here.tmp'), l('f/o/f.oo'), l('dir/graft-one'), l('dir/dir2/graft2'), l('dir3/ok'), l('dir3/sub/ok.txt'), ] for line in MANIFEST_IN.split('\n'): if line.strip() == '': continue file_list.process_template_line(line) wanted = ['ok', 'buildout.cfg', 'four.txt', l('.hg/last-message.txt'), l('global/one.txt'), l('global/two.txt'), l('f/o/f.oo'), l('dir/graft-one'), l('dir/dir2/graft2'), ] self.assertEqual(file_list.files, wanted) def test_debug_print(self): file_list = FileList() with captured_stdout() as stdout: file_list.debug_print('xxx') self.assertEqual(stdout.getvalue(), '') debug.DEBUG = True try: with captured_stdout() as stdout: file_list.debug_print('xxx') self.assertEqual(stdout.getvalue(), 'xxx\n') finally: debug.DEBUG = False def test_set_allfiles(self): file_list = FileList() files = ['a', 'b', 'c'] file_list.set_allfiles(files) self.assertEqual(file_list.allfiles, files) def test_remove_duplicates(self): file_list = FileList() file_list.files = ['a', 'b', 'a', 'g', 'c', 'g'] # files must be sorted beforehand (sdist does it) file_list.sort() file_list.remove_duplicates() self.assertEqual(file_list.files, ['a', 'b', 'c', 'g']) def test_translate_pattern(self): # not regex self.assertTrue(hasattr( translate_pattern('a', anchor=True, is_regex=False), 'search')) # is a regex regex = re.compile('a') self.assertEqual( translate_pattern(regex, anchor=True, is_regex=True), regex) # plain string flagged as regex self.assertTrue(hasattr( translate_pattern('a', anchor=True, is_regex=True), 'search')) # glob support self.assertTrue(translate_pattern( '*.py', anchor=True, is_regex=False).search('filelist.py')) def test_exclude_pattern(self): # return False if no match file_list = FileList() self.assertFalse(file_list.exclude_pattern('*.py')) # return True if files match file_list = FileList() file_list.files = ['a.py', 'b.py'] self.assertTrue(file_list.exclude_pattern('*.py')) # test excludes file_list = FileList() file_list.files = ['a.py', 'a.txt'] file_list.exclude_pattern('*.py') self.assertEqual(file_list.files, ['a.txt']) def test_include_pattern(self): # return False if no match file_list = FileList() file_list.set_allfiles([]) self.assertFalse(file_list.include_pattern('*.py')) # return True if files match file_list = FileList() file_list.set_allfiles(['a.py', 'b.txt']) self.assertTrue(file_list.include_pattern('*.py')) # test * matches all files file_list = FileList() self.assertIsNone(file_list.allfiles) file_list.set_allfiles(['a.py', 'b.txt']) file_list.include_pattern('*') self.assertEqual(file_list.allfiles, ['a.py', 'b.txt']) def test_process_template(self): l = make_local_path # invalid lines file_list = FileList() for action in ('include', 'exclude', 'global-include', 'global-exclude', 'recursive-include', 'recursive-exclude', 'graft', 'prune', 'blarg'): self.assertRaises(DistutilsTemplateError, file_list.process_template_line, action) # include file_list = FileList() file_list.set_allfiles(['a.py', 'b.txt', l('d/c.py')]) file_list.process_template_line('include *.py') self.assertEqual(file_list.files, ['a.py']) self.assertNoWarnings() file_list.process_template_line('include *.rb') self.assertEqual(file_list.files, ['a.py']) self.assertWarnings() # exclude file_list = FileList() file_list.files = ['a.py', 'b.txt', l('d/c.py')] file_list.process_template_line('exclude *.py') self.assertEqual(file_list.files, ['b.txt', l('d/c.py')]) self.assertNoWarnings() file_list.process_template_line('exclude *.rb') self.assertEqual(file_list.files, ['b.txt', l('d/c.py')]) self.assertWarnings() # global-include file_list = FileList() file_list.set_allfiles(['a.py', 'b.txt', l('d/c.py')]) file_list.process_template_line('global-include *.py') self.assertEqual(file_list.files, ['a.py', l('d/c.py')]) self.assertNoWarnings() file_list.process_template_line('global-include *.rb') self.assertEqual(file_list.files, ['a.py', l('d/c.py')]) self.assertWarnings() # global-exclude file_list = FileList() file_list.files = ['a.py', 'b.txt', l('d/c.py')] file_list.process_template_line('global-exclude *.py') self.assertEqual(file_list.files, ['b.txt']) self.assertNoWarnings() file_list.process_template_line('global-exclude *.rb') self.assertEqual(file_list.files, ['b.txt']) self.assertWarnings() # recursive-include file_list = FileList() file_list.set_allfiles(['a.py', l('d/b.py'), l('d/c.txt'), l('d/d/e.py')]) file_list.process_template_line('recursive-include d *.py') self.assertEqual(file_list.files, [l('d/b.py'), l('d/d/e.py')]) self.assertNoWarnings() file_list.process_template_line('recursive-include e *.py') self.assertEqual(file_list.files, [l('d/b.py'), l('d/d/e.py')]) self.assertWarnings() # recursive-exclude file_list = FileList() file_list.files = ['a.py', l('d/b.py'), l('d/c.txt'), l('d/d/e.py')] file_list.process_template_line('recursive-exclude d *.py') self.assertEqual(file_list.files, ['a.py', l('d/c.txt')]) self.assertNoWarnings() file_list.process_template_line('recursive-exclude e *.py') self.assertEqual(file_list.files, ['a.py', l('d/c.txt')]) self.assertWarnings() # graft file_list = FileList() file_list.set_allfiles(['a.py', l('d/b.py'), l('d/d/e.py'), l('f/f.py')]) file_list.process_template_line('graft d') self.assertEqual(file_list.files, [l('d/b.py'), l('d/d/e.py')]) self.assertNoWarnings() file_list.process_template_line('graft e') self.assertEqual(file_list.files, [l('d/b.py'), l('d/d/e.py')]) self.assertWarnings() # prune file_list = FileList() file_list.files = ['a.py', l('d/b.py'), l('d/d/e.py'), l('f/f.py')] file_list.process_template_line('prune d') self.assertEqual(file_list.files, ['a.py', l('f/f.py')]) self.assertNoWarnings() file_list.process_template_line('prune e') self.assertEqual(file_list.files, ['a.py', l('f/f.py')]) self.assertWarnings() class FindAllTestCase(unittest.TestCase): @test.support.skip_unless_symlink def test_missing_symlink(self): with test.support.temp_cwd(): os.symlink('foo', 'bar') self.assertEqual(filelist.findall(), []) def test_basic_discovery(self): """ When findall is called with no parameters or with '.' as the parameter, the dot should be omitted from the results. """ with test.support.temp_cwd(): os.mkdir('foo') file1 = os.path.join('foo', 'file1.txt') test.support.create_empty_file(file1) os.mkdir('bar') file2 = os.path.join('bar', 'file2.txt') test.support.create_empty_file(file2) expected = [file2, file1] self.assertEqual(sorted(filelist.findall()), expected) def test_non_local_discovery(self): """ When findall is called with another path, the full path name should be returned. """ with test.support.temp_dir() as temp_dir: file1 = os.path.join(temp_dir, 'file1.txt') test.support.create_empty_file(file1) expected = [file1] self.assertEqual(filelist.findall(temp_dir), expected) def test_suite(): return unittest.TestSuite([ unittest.makeSuite(FileListTestCase), unittest.makeSuite(FindAllTestCase), ]) if __name__ == "__main__": run_unittest(test_suite())
11,475
341
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/support.py
"""Support code for distutils test cases.""" import os import sys import shutil import tempfile import unittest import sysconfig from copy import deepcopy from distutils import log from distutils.log import DEBUG, INFO, WARN, ERROR, FATAL from distutils.core import Distribution class LoggingSilencer(object): def setUp(self): super().setUp() self.threshold = log.set_threshold(log.FATAL) # catching warnings # when log will be replaced by logging # we won't need such monkey-patch anymore self._old_log = log.Log._log log.Log._log = self._log self.logs = [] def tearDown(self): log.set_threshold(self.threshold) log.Log._log = self._old_log super().tearDown() def _log(self, level, msg, args): if level not in (DEBUG, INFO, WARN, ERROR, FATAL): raise ValueError('%s wrong log level' % str(level)) if not isinstance(msg, str): raise TypeError("msg should be str, not '%.200s'" % (type(msg).__name__)) self.logs.append((level, msg, args)) def get_logs(self, *levels): def _format(msg, args): return msg % args return [msg % args for level, msg, args in self.logs if level in levels] def clear_logs(self): self.logs = [] class TempdirManager(object): """Mix-in class that handles temporary directories for test cases. This is intended to be used with unittest.TestCase. """ def setUp(self): super().setUp() self.old_cwd = os.getcwd() self.tempdirs = [] def tearDown(self): # Restore working dir, for Solaris and derivatives, where rmdir() # on the current directory fails. os.chdir(self.old_cwd) super().tearDown() while self.tempdirs: d = self.tempdirs.pop() shutil.rmtree(d, os.name in ('nt', 'cygwin')) def mkdtemp(self): """Create a temporary directory that will be cleaned up. Returns the path of the directory. """ d = tempfile.mkdtemp() self.tempdirs.append(d) return d def write_file(self, path, content='xxx'): """Writes a file in the given path. path can be a string or a sequence. """ if isinstance(path, (list, tuple)): path = os.path.join(*path) f = open(path, 'w') try: f.write(content) finally: f.close() def create_dist(self, pkg_name='foo', **kw): """Will generate a test environment. This function creates: - a Distribution instance using keywords - a temporary directory with a package structure It returns the package directory and the distribution instance. """ tmp_dir = self.mkdtemp() pkg_dir = os.path.join(tmp_dir, pkg_name) os.mkdir(pkg_dir) dist = Distribution(attrs=kw) return pkg_dir, dist class DummyCommand: """Class to store options for retrieval via set_undefined_options().""" def __init__(self, **kwargs): for kw, val in kwargs.items(): setattr(self, kw, val) def ensure_finalized(self): pass class EnvironGuard(object): def setUp(self): super(EnvironGuard, self).setUp() self.old_environ = deepcopy(os.environ) def tearDown(self): for key, value in self.old_environ.items(): if os.environ.get(key) != value: os.environ[key] = value for key in tuple(os.environ.keys()): if key not in self.old_environ: del os.environ[key] super(EnvironGuard, self).tearDown() def copy_xxmodule_c(directory): """Helper for tests that need the xxmodule.c source file. Example use: def test_compile(self): copy_xxmodule_c(self.tmpdir) self.assertIn('xxmodule.c', os.listdir(self.tmpdir)) If the source file can be found, it will be copied to *directory*. If not, the test will be skipped. Errors during copy are not caught. """ filename = _get_xxmodule_path() if filename is None: raise unittest.SkipTest('cannot find xxmodule.c (test must run in ' 'the python build dir)') shutil.copy(filename, directory) def _get_xxmodule_path(): srcdir = sysconfig.get_config_var('srcdir') candidates = [ # use installed copy if available os.path.join(os.path.dirname(__file__), 'xxmodule.c'), # otherwise try using copy from build directory os.path.join(srcdir, 'Modules', 'xxmodule.c'), # srcdir mysteriously can be $srcdir/Lib/distutils/tests when # this file is run from its parent directory, so walk up the # tree to find the real srcdir os.path.join(srcdir, '..', '..', '..', 'Modules', 'xxmodule.c'), ] for path in candidates: if os.path.exists(path): return path def fixup_build_ext(cmd): """Function needed to make build_ext tests pass. When Python was built with --enable-shared on Unix, -L. is not enough to find libpython<blah>.so, because regrtest runs in a tempdir, not in the source directory where the .so lives. When Python was built with in debug mode on Windows, build_ext commands need their debug attribute set, and it is not done automatically for some reason. This function handles both of these things. Example use: cmd = build_ext(dist) support.fixup_build_ext(cmd) cmd.ensure_finalized() Unlike most other Unix platforms, Mac OS X embeds absolute paths to shared libraries into executables, so the fixup is not needed there. """ if os.name == 'nt': cmd.debug = sys.executable.endswith('_d.exe') elif sysconfig.get_config_var('Py_ENABLE_SHARED'): # To further add to the shared builds fun on Unix, we can't just add # library_dirs to the Extension() instance because that doesn't get # plumbed through to the final compiler command. runshared = sysconfig.get_config_var('RUNSHARED') if runshared is None: cmd.library_dirs = ['.'] else: if sys.platform == 'darwin': cmd.library_dirs = [] else: name, equals, value = runshared.partition('=') cmd.library_dirs = [d for d in value.split(os.pathsep) if d]
6,533
211
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_archive_util.py
# -*- coding: utf-8 -*- """Tests for distutils.archive_util.""" import unittest import os import sys import tarfile from os.path import splitdrive import warnings from distutils import archive_util from distutils.archive_util import (check_archive_formats, make_tarball, make_zipfile, make_archive, ARCHIVE_FORMATS) from distutils.spawn import find_executable, spawn from distutils.tests import support from test.support import check_warnings, run_unittest, patch, change_cwd try: import grp import pwd UID_GID_SUPPORT = True except ImportError: UID_GID_SUPPORT = False try: import zipfile ZIP_SUPPORT = True except ImportError: ZIP_SUPPORT = find_executable('zip') try: import zlib ZLIB_SUPPORT = True except ImportError: ZLIB_SUPPORT = False try: import bz2 except ImportError: bz2 = None try: import lzma except ImportError: lzma = None def can_fs_encode(filename): """ Return True if the filename can be saved in the file system. """ if os.path.supports_unicode_filenames: return True try: filename.encode(sys.getfilesystemencoding()) except UnicodeEncodeError: return False return True class ArchiveUtilTestCase(support.TempdirManager, support.LoggingSilencer, unittest.TestCase): @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') def test_make_tarball(self, name='archive'): # creating something to tar tmpdir = self._create_files() self._make_tarball(tmpdir, name, '.tar.gz') # trying an uncompressed one self._make_tarball(tmpdir, name, '.tar', compress=None) @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') def test_make_tarball_gzip(self): tmpdir = self._create_files() self._make_tarball(tmpdir, 'archive', '.tar.gz', compress='gzip') @unittest.skipUnless(bz2, 'Need bz2 support to run') def test_make_tarball_bzip2(self): tmpdir = self._create_files() self._make_tarball(tmpdir, 'archive', '.tar.bz2', compress='bzip2') @unittest.skipUnless(lzma, 'Need lzma support to run') def test_make_tarball_xz(self): tmpdir = self._create_files() self._make_tarball(tmpdir, 'archive', '.tar.xz', compress='xz') @unittest.skipUnless(can_fs_encode('årchiv'), 'File system cannot handle this filename') def test_make_tarball_latin1(self): """ Mirror test_make_tarball, except filename contains latin characters. """ self.test_make_tarball('årchiv') # note this isn't a real word @unittest.skipUnless(can_fs_encode('のアーカイブ'), 'File system cannot handle this filename') def test_make_tarball_extended(self): """ Mirror test_make_tarball, except filename contains extended characters outside the latin charset. """ self.test_make_tarball('のアーカイブ') # japanese for archive def _make_tarball(self, tmpdir, target_name, suffix, **kwargs): tmpdir2 = self.mkdtemp() unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0], "source and target should be on same drive") base_name = os.path.join(tmpdir2, target_name) # working with relative paths to avoid tar warnings with change_cwd(tmpdir): make_tarball(splitdrive(base_name)[1], 'dist', **kwargs) # check if the compressed tarball was created tarball = base_name + suffix self.assertTrue(os.path.exists(tarball)) self.assertEqual(self._tarinfo(tarball), self._created_files) def _tarinfo(self, path): tar = tarfile.open(path) try: names = tar.getnames() names.sort() return names finally: tar.close() _zip_created_files = ['dist/', 'dist/file1', 'dist/file2', 'dist/sub/', 'dist/sub/file3', 'dist/sub2/'] _created_files = [p.rstrip('/') for p in _zip_created_files] def _create_files(self): # creating something to tar tmpdir = self.mkdtemp() dist = os.path.join(tmpdir, 'dist') os.mkdir(dist) self.write_file([dist, 'file1'], 'xxx') self.write_file([dist, 'file2'], 'xxx') os.mkdir(os.path.join(dist, 'sub')) self.write_file([dist, 'sub', 'file3'], 'xxx') os.mkdir(os.path.join(dist, 'sub2')) return tmpdir @unittest.skipUnless(find_executable('tar') and find_executable('gzip') and ZLIB_SUPPORT, 'Need the tar, gzip and zlib command to run') def test_tarfile_vs_tar(self): tmpdir = self._create_files() tmpdir2 = self.mkdtemp() base_name = os.path.join(tmpdir2, 'archive') old_dir = os.getcwd() os.chdir(tmpdir) try: make_tarball(base_name, 'dist') finally: os.chdir(old_dir) # check if the compressed tarball was created tarball = base_name + '.tar.gz' self.assertTrue(os.path.exists(tarball)) # now create another tarball using `tar` tarball2 = os.path.join(tmpdir, 'archive2.tar.gz') tar_cmd = ['tar', '-cf', 'archive2.tar', 'dist'] gzip_cmd = ['gzip', '-f9', 'archive2.tar'] old_dir = os.getcwd() os.chdir(tmpdir) try: spawn(tar_cmd) spawn(gzip_cmd) finally: os.chdir(old_dir) self.assertTrue(os.path.exists(tarball2)) # let's compare both tarballs self.assertEqual(self._tarinfo(tarball), self._created_files) self.assertEqual(self._tarinfo(tarball2), self._created_files) # trying an uncompressed one base_name = os.path.join(tmpdir2, 'archive') old_dir = os.getcwd() os.chdir(tmpdir) try: make_tarball(base_name, 'dist', compress=None) finally: os.chdir(old_dir) tarball = base_name + '.tar' self.assertTrue(os.path.exists(tarball)) # now for a dry_run base_name = os.path.join(tmpdir2, 'archive') old_dir = os.getcwd() os.chdir(tmpdir) try: make_tarball(base_name, 'dist', compress=None, dry_run=True) finally: os.chdir(old_dir) tarball = base_name + '.tar' self.assertTrue(os.path.exists(tarball)) @unittest.skipUnless(find_executable('compress'), 'The compress program is required') def test_compress_deprecated(self): tmpdir = self._create_files() base_name = os.path.join(self.mkdtemp(), 'archive') # using compress and testing the PendingDeprecationWarning old_dir = os.getcwd() os.chdir(tmpdir) try: with check_warnings() as w: warnings.simplefilter("always") make_tarball(base_name, 'dist', compress='compress') finally: os.chdir(old_dir) tarball = base_name + '.tar.Z' self.assertTrue(os.path.exists(tarball)) self.assertEqual(len(w.warnings), 1) # same test with dry_run os.remove(tarball) old_dir = os.getcwd() os.chdir(tmpdir) try: with check_warnings() as w: warnings.simplefilter("always") make_tarball(base_name, 'dist', compress='compress', dry_run=True) finally: os.chdir(old_dir) self.assertFalse(os.path.exists(tarball)) self.assertEqual(len(w.warnings), 1) @unittest.skipUnless(ZIP_SUPPORT and ZLIB_SUPPORT, 'Need zip and zlib support to run') def test_make_zipfile(self): # creating something to tar tmpdir = self._create_files() base_name = os.path.join(self.mkdtemp(), 'archive') with change_cwd(tmpdir): make_zipfile(base_name, 'dist') # check if the compressed tarball was created tarball = base_name + '.zip' self.assertTrue(os.path.exists(tarball)) with zipfile.ZipFile(tarball) as zf: self.assertEqual(sorted(zf.namelist()), self._zip_created_files) @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run') def test_make_zipfile_no_zlib(self): patch(self, archive_util.zipfile, 'zlib', None) # force zlib ImportError called = [] zipfile_class = zipfile.ZipFile def fake_zipfile(*a, **kw): if kw.get('compression', None) == zipfile.ZIP_STORED: called.append((a, kw)) return zipfile_class(*a, **kw) patch(self, archive_util.zipfile, 'ZipFile', fake_zipfile) # create something to tar and compress tmpdir = self._create_files() base_name = os.path.join(self.mkdtemp(), 'archive') with change_cwd(tmpdir): make_zipfile(base_name, 'dist') tarball = base_name + '.zip' self.assertEqual(called, [((tarball, "w"), {'compression': zipfile.ZIP_STORED})]) self.assertTrue(os.path.exists(tarball)) with zipfile.ZipFile(tarball) as zf: self.assertEqual(sorted(zf.namelist()), self._zip_created_files) def test_check_archive_formats(self): self.assertEqual(check_archive_formats(['gztar', 'xxx', 'zip']), 'xxx') self.assertIsNone(check_archive_formats(['gztar', 'bztar', 'xztar', 'ztar', 'tar', 'zip'])) def test_make_archive(self): tmpdir = self.mkdtemp() base_name = os.path.join(tmpdir, 'archive') self.assertRaises(ValueError, make_archive, base_name, 'xxx') def test_make_archive_cwd(self): current_dir = os.getcwd() def _breaks(*args, **kw): raise RuntimeError() ARCHIVE_FORMATS['xxx'] = (_breaks, [], 'xxx file') try: try: make_archive('xxx', 'xxx', root_dir=self.mkdtemp()) except: pass self.assertEqual(os.getcwd(), current_dir) finally: del ARCHIVE_FORMATS['xxx'] def test_make_archive_tar(self): base_dir = self._create_files() base_name = os.path.join(self.mkdtemp() , 'archive') res = make_archive(base_name, 'tar', base_dir, 'dist') self.assertTrue(os.path.exists(res)) self.assertEqual(os.path.basename(res), 'archive.tar') self.assertEqual(self._tarinfo(res), self._created_files) @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') def test_make_archive_gztar(self): base_dir = self._create_files() base_name = os.path.join(self.mkdtemp() , 'archive') res = make_archive(base_name, 'gztar', base_dir, 'dist') self.assertTrue(os.path.exists(res)) self.assertEqual(os.path.basename(res), 'archive.tar.gz') self.assertEqual(self._tarinfo(res), self._created_files) @unittest.skipUnless(bz2, 'Need bz2 support to run') def test_make_archive_bztar(self): base_dir = self._create_files() base_name = os.path.join(self.mkdtemp() , 'archive') res = make_archive(base_name, 'bztar', base_dir, 'dist') self.assertTrue(os.path.exists(res)) self.assertEqual(os.path.basename(res), 'archive.tar.bz2') self.assertEqual(self._tarinfo(res), self._created_files) @unittest.skipUnless(lzma, 'Need xz support to run') def test_make_archive_xztar(self): base_dir = self._create_files() base_name = os.path.join(self.mkdtemp() , 'archive') res = make_archive(base_name, 'xztar', base_dir, 'dist') self.assertTrue(os.path.exists(res)) self.assertEqual(os.path.basename(res), 'archive.tar.xz') self.assertEqual(self._tarinfo(res), self._created_files) def test_make_archive_owner_group(self): # testing make_archive with owner and group, with various combinations # this works even if there's not gid/uid support if UID_GID_SUPPORT: group = grp.getgrgid(0)[0] owner = pwd.getpwuid(0)[0] else: group = owner = 'root' base_dir = self._create_files() root_dir = self.mkdtemp() base_name = os.path.join(self.mkdtemp() , 'archive') res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner, group=group) self.assertTrue(os.path.exists(res)) res = make_archive(base_name, 'zip', root_dir, base_dir) self.assertTrue(os.path.exists(res)) res = make_archive(base_name, 'tar', root_dir, base_dir, owner=owner, group=group) self.assertTrue(os.path.exists(res)) res = make_archive(base_name, 'tar', root_dir, base_dir, owner='kjhkjhkjg', group='oihohoh') self.assertTrue(os.path.exists(res)) @unittest.skipUnless(ZLIB_SUPPORT, "Requires zlib") @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support") def test_tarfile_root_owner(self): tmpdir = self._create_files() base_name = os.path.join(self.mkdtemp(), 'archive') old_dir = os.getcwd() os.chdir(tmpdir) group = grp.getgrgid(0)[0] owner = pwd.getpwuid(0)[0] try: archive_name = make_tarball(base_name, 'dist', compress=None, owner=owner, group=group) finally: os.chdir(old_dir) # check if the compressed tarball was created self.assertTrue(os.path.exists(archive_name)) # now checks the rights archive = tarfile.open(archive_name) try: for member in archive.getmembers(): self.assertEqual(member.uid, 0) self.assertEqual(member.gid, 0) finally: archive.close() def test_suite(): return unittest.makeSuite(ArchiveUtilTestCase) if __name__ == "__main__": run_unittest(test_suite())
14,296
395
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_config_cmd.py
"""Tests for distutils.command.config.""" import unittest import os import sys from test.support import run_unittest, missing_compiler_executable from distutils.command.config import dump_file, config from distutils.tests import support from distutils import log class ConfigTestCase(support.LoggingSilencer, support.TempdirManager, unittest.TestCase): def _info(self, msg, *args): for line in msg.splitlines(): self._logs.append(line) def setUp(self): super(ConfigTestCase, self).setUp() self._logs = [] self.old_log = log.info log.info = self._info def tearDown(self): log.info = self.old_log super(ConfigTestCase, self).tearDown() def test_dump_file(self): this_file = os.path.splitext(__file__)[0] + '.py' f = open(this_file) try: numlines = len(f.readlines()) finally: f.close() dump_file(this_file, 'I am the header') self.assertEqual(len(self._logs), numlines+1) @unittest.skipIf(sys.platform == 'win32', "can't test on Windows") def test_search_cpp(self): cmd = missing_compiler_executable(['preprocessor']) if cmd is not None: self.skipTest('The %r command is not found' % cmd) pkg_dir, dist = self.create_dist() cmd = config(dist) # simple pattern searches match = cmd.search_cpp(pattern='xxx', body='/* xxx */') self.assertEqual(match, 0) match = cmd.search_cpp(pattern='_configtest', body='/* xxx */') self.assertEqual(match, 1) def test_finalize_options(self): # finalize_options does a bit of transformation # on options pkg_dir, dist = self.create_dist() cmd = config(dist) cmd.include_dirs = 'one%stwo' % os.pathsep cmd.libraries = 'one' cmd.library_dirs = 'three%sfour' % os.pathsep cmd.ensure_finalized() self.assertEqual(cmd.include_dirs, ['one', 'two']) self.assertEqual(cmd.libraries, ['one']) self.assertEqual(cmd.library_dirs, ['three', 'four']) def test_clean(self): # _clean removes files tmp_dir = self.mkdtemp() f1 = os.path.join(tmp_dir, 'one') f2 = os.path.join(tmp_dir, 'two') self.write_file(f1, 'xxx') self.write_file(f2, 'xxx') for f in (f1, f2): self.assertTrue(os.path.exists(f)) pkg_dir, dist = self.create_dist() cmd = config(dist) cmd._clean(f1, f2) for f in (f1, f2): self.assertFalse(os.path.exists(f)) def test_suite(): return unittest.makeSuite(ConfigTestCase) if __name__ == "__main__": run_unittest(test_suite())
2,782
93
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_versionpredicate.py
"""Tests harness for distutils.versionpredicate. """ import distutils.versionpredicate import doctest from test.support import run_unittest def test_suite(): return doctest.DocTestSuite(distutils.versionpredicate) if __name__ == '__main__': run_unittest(test_suite())
280
14
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_dep_util.py
"""Tests for distutils.dep_util.""" import unittest import os from distutils.dep_util import newer, newer_pairwise, newer_group from distutils.errors import DistutilsFileError from distutils.tests import support from test.support import run_unittest class DepUtilTestCase(support.TempdirManager, unittest.TestCase): def test_newer(self): tmpdir = self.mkdtemp() new_file = os.path.join(tmpdir, 'new') old_file = os.path.abspath(__file__) # Raise DistutilsFileError if 'new_file' does not exist. self.assertRaises(DistutilsFileError, newer, new_file, old_file) # Return true if 'new_file' exists and is more recently modified than # 'old_file', or if 'new_file' exists and 'old_file' doesn't. self.write_file(new_file) self.assertTrue(newer(new_file, 'I_dont_exist')) self.assertTrue(newer(new_file, old_file)) # Return false if both exist and 'old_file' is the same age or younger # than 'new_file'. self.assertFalse(newer(old_file, new_file)) def test_newer_pairwise(self): tmpdir = self.mkdtemp() sources = os.path.join(tmpdir, 'sources') targets = os.path.join(tmpdir, 'targets') os.mkdir(sources) os.mkdir(targets) one = os.path.join(sources, 'one') two = os.path.join(sources, 'two') three = os.path.abspath(__file__) # I am the old file four = os.path.join(targets, 'four') self.write_file(one) self.write_file(two) self.write_file(four) self.assertEqual(newer_pairwise([one, two], [three, four]), ([one],[three])) def test_newer_group(self): tmpdir = self.mkdtemp() sources = os.path.join(tmpdir, 'sources') os.mkdir(sources) one = os.path.join(sources, 'one') two = os.path.join(sources, 'two') three = os.path.join(sources, 'three') old_file = os.path.abspath(__file__) # return true if 'old_file' is out-of-date with respect to any file # listed in 'sources'. self.write_file(one) self.write_file(two) self.write_file(three) self.assertTrue(newer_group([one, two, three], old_file)) self.assertFalse(newer_group([one, two, old_file], three)) # missing handling os.remove(one) self.assertRaises(OSError, newer_group, [one, two, old_file], three) self.assertFalse(newer_group([one, two, old_file], three, missing='ignore')) self.assertTrue(newer_group([one, two, old_file], three, missing='newer')) def test_suite(): return unittest.makeSuite(DepUtilTestCase) if __name__ == "__main__": run_unittest(test_suite())
2,820
81
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_install_lib.py
"""Tests for distutils.command.install_data.""" import sys import os import importlib.util import unittest from distutils.command.install_lib import install_lib from distutils.extension import Extension from distutils.tests import support from distutils.errors import DistutilsOptionError from test.support import run_unittest class InstallLibTestCase(support.TempdirManager, support.LoggingSilencer, support.EnvironGuard, unittest.TestCase): def test_finalize_options(self): dist = self.create_dist()[1] cmd = install_lib(dist) cmd.finalize_options() self.assertEqual(cmd.compile, 1) self.assertEqual(cmd.optimize, 0) # optimize must be 0, 1, or 2 cmd.optimize = 'foo' self.assertRaises(DistutilsOptionError, cmd.finalize_options) cmd.optimize = '4' self.assertRaises(DistutilsOptionError, cmd.finalize_options) cmd.optimize = '2' cmd.finalize_options() self.assertEqual(cmd.optimize, 2) @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled') def test_byte_compile(self): project_dir, dist = self.create_dist() os.chdir(project_dir) cmd = install_lib(dist) cmd.compile = cmd.optimize = 1 f = os.path.join(project_dir, 'foo.py') self.write_file(f, '# python file') cmd.byte_compile([f]) pyc_file = importlib.util.cache_from_source('foo.py', optimization='') pyc_opt_file = importlib.util.cache_from_source('foo.py', optimization=cmd.optimize) self.assertTrue(os.path.exists(pyc_file)) self.assertTrue(os.path.exists(pyc_opt_file)) def test_get_outputs(self): project_dir, dist = self.create_dist() os.chdir(project_dir) os.mkdir('spam') cmd = install_lib(dist) # setting up a dist environment cmd.compile = cmd.optimize = 1 cmd.install_dir = self.mkdtemp() f = os.path.join(project_dir, 'spam', '__init__.py') self.write_file(f, '# python package') cmd.distribution.ext_modules = [Extension('foo', ['xxx'])] cmd.distribution.packages = ['spam'] cmd.distribution.script_name = 'setup.py' # get_outputs should return 4 elements: spam/__init__.py and .pyc, # foo.import-tag-abiflags.so / foo.pyd outputs = cmd.get_outputs() self.assertEqual(len(outputs), 4, outputs) def test_get_inputs(self): project_dir, dist = self.create_dist() os.chdir(project_dir) os.mkdir('spam') cmd = install_lib(dist) # setting up a dist environment cmd.compile = cmd.optimize = 1 cmd.install_dir = self.mkdtemp() f = os.path.join(project_dir, 'spam', '__init__.py') self.write_file(f, '# python package') cmd.distribution.ext_modules = [Extension('foo', ['xxx'])] cmd.distribution.packages = ['spam'] cmd.distribution.script_name = 'setup.py' # get_inputs should return 2 elements: spam/__init__.py and # foo.import-tag-abiflags.so / foo.pyd inputs = cmd.get_inputs() self.assertEqual(len(inputs), 2, inputs) def test_dont_write_bytecode(self): # makes sure byte_compile is not used dist = self.create_dist()[1] cmd = install_lib(dist) cmd.compile = 1 cmd.optimize = 1 old_dont_write_bytecode = sys.dont_write_bytecode sys.dont_write_bytecode = True try: cmd.byte_compile([]) finally: sys.dont_write_bytecode = old_dont_write_bytecode self.assertIn('byte-compiling is disabled', self.logs[0][1] % self.logs[0][2]) def test_suite(): return unittest.makeSuite(InstallLibTestCase) if __name__ == "__main__": run_unittest(test_suite())
3,974
116
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_log.py
"""Tests for distutils.log""" import io import sys import unittest from test.support import swap_attr, run_unittest from distutils import log class TestLog(unittest.TestCase): def test_non_ascii(self): # Issues #8663, #34421: test that non-encodable text is escaped with # backslashreplace error handler and encodable non-ASCII text is # output as is. for errors in ('strict', 'backslashreplace', 'surrogateescape', 'replace', 'ignore'): with self.subTest(errors=errors): stdout = io.TextIOWrapper(io.BytesIO(), encoding='cp437', errors=errors) stderr = io.TextIOWrapper(io.BytesIO(), encoding='cp437', errors=errors) old_threshold = log.set_threshold(log.DEBUG) try: with swap_attr(sys, 'stdout', stdout), \ swap_attr(sys, 'stderr', stderr): log.debug('Dεbug\tMėssãge') log.fatal('Fαtal\tÈrrōr') finally: log.set_threshold(old_threshold) stdout.seek(0) self.assertEqual(stdout.read().rstrip(), 'Dεbug\tM?ss?ge' if errors == 'replace' else 'Dεbug\tMssge' if errors == 'ignore' else 'Dεbug\tM\\u0117ss\\xe3ge') stderr.seek(0) self.assertEqual(stderr.read().rstrip(), 'Fαtal\t?rr?r' if errors == 'replace' else 'Fαtal\trrr' if errors == 'ignore' else 'Fαtal\t\\xc8rr\\u014dr') def test_suite(): return unittest.makeSuite(TestLog) if __name__ == "__main__": run_unittest(test_suite())
1,864
47
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_check.py
"""Tests for distutils.command.check.""" import textwrap import unittest from test.support import run_unittest from distutils.command.check import check, HAS_DOCUTILS from distutils.tests import support from distutils.errors import DistutilsSetupError try: import pygments except ImportError: pygments = None class CheckTestCase(support.LoggingSilencer, support.TempdirManager, unittest.TestCase): def _run(self, metadata=None, **options): if metadata is None: metadata = {} pkg_info, dist = self.create_dist(**metadata) cmd = check(dist) cmd.initialize_options() for name, value in options.items(): setattr(cmd, name, value) cmd.ensure_finalized() cmd.run() return cmd def test_check_metadata(self): # let's run the command with no metadata at all # by default, check is checking the metadata # should have some warnings cmd = self._run() self.assertEqual(cmd._warnings, 2) # now let's add the required fields # and run it again, to make sure we don't get # any warning anymore metadata = {'url': 'xxx', 'author': 'xxx', 'author_email': 'xxx', 'name': 'xxx', 'version': 'xxx'} cmd = self._run(metadata) self.assertEqual(cmd._warnings, 0) # now with the strict mode, we should # get an error if there are missing metadata self.assertRaises(DistutilsSetupError, self._run, {}, **{'strict': 1}) # and of course, no error when all metadata are present cmd = self._run(metadata, strict=1) self.assertEqual(cmd._warnings, 0) # now a test with non-ASCII characters metadata = {'url': 'xxx', 'author': '\u00c9ric', 'author_email': 'xxx', 'name': 'xxx', 'version': 'xxx', 'description': 'Something about esszet \u00df', 'long_description': 'More things about esszet \u00df'} cmd = self._run(metadata) self.assertEqual(cmd._warnings, 0) @unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils") def test_check_document(self): pkg_info, dist = self.create_dist() cmd = check(dist) # let's see if it detects broken rest broken_rest = 'title\n===\n\ntest' msgs = cmd._check_rst_data(broken_rest) self.assertEqual(len(msgs), 1) # and non-broken rest rest = 'title\n=====\n\ntest' msgs = cmd._check_rst_data(rest) self.assertEqual(len(msgs), 0) @unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils") def test_check_restructuredtext(self): # let's see if it detects broken rest in long_description broken_rest = 'title\n===\n\ntest' pkg_info, dist = self.create_dist(long_description=broken_rest) cmd = check(dist) cmd.check_restructuredtext() self.assertEqual(cmd._warnings, 1) # let's see if we have an error with strict=1 metadata = {'url': 'xxx', 'author': 'xxx', 'author_email': 'xxx', 'name': 'xxx', 'version': 'xxx', 'long_description': broken_rest} self.assertRaises(DistutilsSetupError, self._run, metadata, **{'strict': 1, 'restructuredtext': 1}) # and non-broken rest, including a non-ASCII character to test #12114 metadata['long_description'] = 'title\n=====\n\ntest \u00df' cmd = self._run(metadata, strict=1, restructuredtext=1) self.assertEqual(cmd._warnings, 0) @unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils") def test_check_restructuredtext_with_syntax_highlight(self): # Don't fail if there is a `code` or `code-block` directive example_rst_docs = [] example_rst_docs.append(textwrap.dedent("""\ Here's some code: .. code:: python def foo(): pass """)) example_rst_docs.append(textwrap.dedent("""\ Here's some code: .. code-block:: python def foo(): pass """)) for rest_with_code in example_rst_docs: pkg_info, dist = self.create_dist(long_description=rest_with_code) cmd = check(dist) cmd.check_restructuredtext() msgs = cmd._check_rst_data(rest_with_code) if pygments is not None: self.assertEqual(len(msgs), 0) else: self.assertEqual(len(msgs), 1) self.assertEqual( str(msgs[0][1]), 'Cannot analyze code. Pygments package not found.' ) def test_check_all(self): metadata = {'url': 'xxx', 'author': 'xxx'} self.assertRaises(DistutilsSetupError, self._run, {}, **{'strict': 1, 'restructuredtext': 1}) def test_suite(): return unittest.makeSuite(CheckTestCase) if __name__ == "__main__": run_unittest(test_suite())
5,256
150
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_core.py
"""Tests for distutils.core.""" import io import distutils.core import os import shutil import sys import test.support from test.support import captured_stdout, run_unittest import unittest from distutils.tests import support from distutils import log # setup script that uses __file__ setup_using___file__ = """\ __file__ from distutils.core import setup setup() """ setup_prints_cwd = """\ import os print(os.getcwd()) from distutils.core import setup setup() """ setup_does_nothing = """\ from distutils.core import setup setup() """ setup_defines_subclass = """\ from distutils.core import setup from distutils.command.install import install as _install class install(_install): sub_commands = _install.sub_commands + ['cmd'] setup(cmdclass={'install': install}) """ class CoreTestCase(support.EnvironGuard, unittest.TestCase): def setUp(self): super(CoreTestCase, self).setUp() self.old_stdout = sys.stdout self.cleanup_testfn() self.old_argv = sys.argv, sys.argv[:] self.addCleanup(log.set_threshold, log._global_log.threshold) def tearDown(self): sys.stdout = self.old_stdout self.cleanup_testfn() sys.argv = self.old_argv[0] sys.argv[:] = self.old_argv[1] super(CoreTestCase, self).tearDown() def cleanup_testfn(self): path = test.support.TESTFN if os.path.isfile(path): os.remove(path) elif os.path.isdir(path): shutil.rmtree(path) def write_setup(self, text, path=test.support.TESTFN): f = open(path, "w") try: f.write(text) finally: f.close() return path def test_run_setup_provides_file(self): # Make sure the script can use __file__; if that's missing, the test # setup.py script will raise NameError. distutils.core.run_setup( self.write_setup(setup_using___file__)) def test_run_setup_preserves_sys_argv(self): # Make sure run_setup does not clobber sys.argv argv_copy = sys.argv.copy() distutils.core.run_setup( self.write_setup(setup_does_nothing)) self.assertEqual(sys.argv, argv_copy) def test_run_setup_defines_subclass(self): # Make sure the script can use __file__; if that's missing, the test # setup.py script will raise NameError. dist = distutils.core.run_setup( self.write_setup(setup_defines_subclass)) install = dist.get_command_obj('install') self.assertIn('cmd', install.sub_commands) def test_run_setup_uses_current_dir(self): # This tests that the setup script is run with the current directory # as its own current directory; this was temporarily broken by a # previous patch when TESTFN did not use the current directory. sys.stdout = io.StringIO() cwd = os.getcwd() # Create a directory and write the setup.py file there: os.mkdir(test.support.TESTFN) setup_py = os.path.join(test.support.TESTFN, "setup.py") distutils.core.run_setup( self.write_setup(setup_prints_cwd, path=setup_py)) output = sys.stdout.getvalue() if output.endswith("\n"): output = output[:-1] self.assertEqual(cwd, output) def test_debug_mode(self): # this covers the code called when DEBUG is set sys.argv = ['setup.py', '--name'] with captured_stdout() as stdout: distutils.core.setup(name='bar') stdout.seek(0) self.assertEqual(stdout.read(), 'bar\n') distutils.core.DEBUG = True try: with captured_stdout() as stdout: distutils.core.setup(name='bar') finally: distutils.core.DEBUG = False stdout.seek(0) wanted = "options (after parsing config files):\n" self.assertEqual(stdout.readlines()[0], wanted) def test_suite(): return unittest.makeSuite(CoreTestCase) if __name__ == "__main__": run_unittest(test_suite())
4,077
141
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_config.py
"""Tests for distutils.pypirc.pypirc.""" import os import unittest from distutils.core import PyPIRCCommand from distutils.core import Distribution from distutils.log import set_threshold from distutils.log import WARN from distutils.tests import support from test.support import run_unittest PYPIRC = """\ [distutils] index-servers = server1 server2 server3 [server1] username:me password:secret [server2] username:meagain password: secret realm:acme repository:http://another.pypi/ [server3] username:cbiggles password:yh^%#rest-of-my-password """ PYPIRC_OLD = """\ [server-login] username:tarek password:secret """ WANTED = """\ [distutils] index-servers = pypi [pypi] username:tarek password:xxx """ class BasePyPIRCCommandTestCase(support.TempdirManager, support.LoggingSilencer, support.EnvironGuard, unittest.TestCase): def setUp(self): """Patches the environment.""" super(BasePyPIRCCommandTestCase, self).setUp() self.tmp_dir = self.mkdtemp() os.environ['HOME'] = self.tmp_dir self.rc = os.path.join(self.tmp_dir, '.pypirc') self.dist = Distribution() class command(PyPIRCCommand): def __init__(self, dist): PyPIRCCommand.__init__(self, dist) def initialize_options(self): pass finalize_options = initialize_options self._cmd = command self.old_threshold = set_threshold(WARN) def tearDown(self): """Removes the patch.""" set_threshold(self.old_threshold) super(BasePyPIRCCommandTestCase, self).tearDown() class PyPIRCCommandTestCase(BasePyPIRCCommandTestCase): def test_server_registration(self): # This test makes sure PyPIRCCommand knows how to: # 1. handle several sections in .pypirc # 2. handle the old format # new format self.write_file(self.rc, PYPIRC) cmd = self._cmd(self.dist) config = cmd._read_pypirc() config = list(sorted(config.items())) waited = [('password', 'secret'), ('realm', 'pypi'), ('repository', 'https://upload.pypi.org/legacy/'), ('server', 'server1'), ('username', 'me')] self.assertEqual(config, waited) # old format self.write_file(self.rc, PYPIRC_OLD) config = cmd._read_pypirc() config = list(sorted(config.items())) waited = [('password', 'secret'), ('realm', 'pypi'), ('repository', 'https://upload.pypi.org/legacy/'), ('server', 'server-login'), ('username', 'tarek')] self.assertEqual(config, waited) def test_server_empty_registration(self): cmd = self._cmd(self.dist) rc = cmd._get_rc_file() self.assertFalse(os.path.exists(rc)) cmd._store_pypirc('tarek', 'xxx') self.assertTrue(os.path.exists(rc)) f = open(rc) try: content = f.read() self.assertEqual(content, WANTED) finally: f.close() def test_config_interpolation(self): # using the % character in .pypirc should not raise an error (#20120) self.write_file(self.rc, PYPIRC) cmd = self._cmd(self.dist) cmd.repository = 'server3' config = cmd._read_pypirc() config = list(sorted(config.items())) waited = [('password', 'yh^%#rest-of-my-password'), ('realm', 'pypi'), ('repository', 'https://upload.pypi.org/legacy/'), ('server', 'server3'), ('username', 'cbiggles')] self.assertEqual(config, waited) def test_suite(): return unittest.makeSuite(PyPIRCCommandTestCase) if __name__ == "__main__": run_unittest(test_suite())
3,843
141
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_bdist.py
"""Tests for distutils.command.bdist.""" import os import unittest from test.support import run_unittest from distutils.command.bdist import bdist from distutils.tests import support class BuildTestCase(support.TempdirManager, unittest.TestCase): def test_formats(self): # let's create a command and make sure # we can set the format dist = self.create_dist()[1] cmd = bdist(dist) cmd.formats = ['msi'] cmd.ensure_finalized() self.assertEqual(cmd.formats, ['msi']) # what formats does bdist offer? formats = ['bztar', 'gztar', 'msi', 'rpm', 'tar', 'wininst', 'xztar', 'zip', 'ztar'] found = sorted(cmd.format_command) self.assertEqual(found, formats) def test_skip_build(self): # bug #10946: bdist --skip-build should trickle down to subcommands dist = self.create_dist()[1] cmd = bdist(dist) cmd.skip_build = 1 cmd.ensure_finalized() dist.command_obj['bdist'] = cmd names = ['bdist_dumb', 'bdist_wininst'] # bdist_rpm does not support --skip-build if os.name == 'nt': names.append('bdist_msi') for name in names: subcmd = cmd.get_finalized_command(name) if getattr(subcmd, '_unsupported', False): # command is not supported on this build continue self.assertTrue(subcmd.skip_build, '%s should take --skip-build from bdist' % name) def test_suite(): return unittest.makeSuite(BuildTestCase) if __name__ == '__main__': run_unittest(test_suite())
1,680
54
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_cmd.py
"""Tests for distutils.cmd.""" import unittest import os from test.support import captured_stdout, run_unittest from distutils.cmd import Command from distutils.dist import Distribution from distutils.errors import DistutilsOptionError from distutils import debug class MyCmd(Command): def initialize_options(self): pass class CommandTestCase(unittest.TestCase): def setUp(self): dist = Distribution() self.cmd = MyCmd(dist) def test_ensure_string_list(self): cmd = self.cmd cmd.not_string_list = ['one', 2, 'three'] cmd.yes_string_list = ['one', 'two', 'three'] cmd.not_string_list2 = object() cmd.yes_string_list2 = 'ok' cmd.ensure_string_list('yes_string_list') cmd.ensure_string_list('yes_string_list2') self.assertRaises(DistutilsOptionError, cmd.ensure_string_list, 'not_string_list') self.assertRaises(DistutilsOptionError, cmd.ensure_string_list, 'not_string_list2') cmd.option1 = 'ok,dok' cmd.ensure_string_list('option1') self.assertEqual(cmd.option1, ['ok', 'dok']) cmd.option2 = ['xxx', 'www'] cmd.ensure_string_list('option2') cmd.option3 = ['ok', 2] self.assertRaises(DistutilsOptionError, cmd.ensure_string_list, 'option3') def test_make_file(self): cmd = self.cmd # making sure it raises when infiles is not a string or a list/tuple self.assertRaises(TypeError, cmd.make_file, infiles=1, outfile='', func='func', args=()) # making sure execute gets called properly def _execute(func, args, exec_msg, level): self.assertEqual(exec_msg, 'generating out from in') cmd.force = True cmd.execute = _execute cmd.make_file(infiles='in', outfile='out', func='func', args=()) def test_dump_options(self): msgs = [] def _announce(msg, level): msgs.append(msg) cmd = self.cmd cmd.announce = _announce cmd.option1 = 1 cmd.option2 = 1 cmd.user_options = [('option1', '', ''), ('option2', '', '')] cmd.dump_options() wanted = ["command options for 'MyCmd':", ' option1 = 1', ' option2 = 1'] self.assertEqual(msgs, wanted) def test_ensure_string(self): cmd = self.cmd cmd.option1 = 'ok' cmd.ensure_string('option1') cmd.option2 = None cmd.ensure_string('option2', 'xxx') self.assertTrue(hasattr(cmd, 'option2')) cmd.option3 = 1 self.assertRaises(DistutilsOptionError, cmd.ensure_string, 'option3') def test_ensure_filename(self): cmd = self.cmd cmd.option1 = __file__ cmd.ensure_filename('option1') cmd.option2 = 'xxx' self.assertRaises(DistutilsOptionError, cmd.ensure_filename, 'option2') def test_ensure_dirname(self): cmd = self.cmd cmd.option1 = os.path.dirname(__file__) or os.curdir cmd.ensure_dirname('option1') cmd.option2 = 'xxx' self.assertRaises(DistutilsOptionError, cmd.ensure_dirname, 'option2') def test_debug_print(self): cmd = self.cmd with captured_stdout() as stdout: cmd.debug_print('xxx') stdout.seek(0) self.assertEqual(stdout.read(), '') debug.DEBUG = True try: with captured_stdout() as stdout: cmd.debug_print('xxx') stdout.seek(0) self.assertEqual(stdout.read(), 'xxx\n') finally: debug.DEBUG = False def test_suite(): return unittest.makeSuite(CommandTestCase) if __name__ == '__main__': run_unittest(test_suite())
3,835
127
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_extension.py
"""Tests for distutils.extension.""" import unittest import os import warnings from test.support import check_warnings, run_unittest from distutils.extension import read_setup_file, Extension class ExtensionTestCase(unittest.TestCase): def test_read_setup_file(self): # trying to read a Setup file # (sample extracted from the PyGame project) setup = os.path.join(os.path.dirname(__file__), 'Setup.sample') exts = read_setup_file(setup) names = [ext.name for ext in exts] names.sort() # here are the extensions read_setup_file should have created # out of the file wanted = ['_arraysurfarray', '_camera', '_numericsndarray', '_numericsurfarray', 'base', 'bufferproxy', 'cdrom', 'color', 'constants', 'display', 'draw', 'event', 'fastevent', 'font', 'gfxdraw', 'image', 'imageext', 'joystick', 'key', 'mask', 'mixer', 'mixer_music', 'mouse', 'movie', 'overlay', 'pixelarray', 'pypm', 'rect', 'rwobject', 'scrap', 'surface', 'surflock', 'time', 'transform'] self.assertEqual(names, wanted) def test_extension_init(self): # the first argument, which is the name, must be a string self.assertRaises(AssertionError, Extension, 1, []) ext = Extension('name', []) self.assertEqual(ext.name, 'name') # the second argument, which is the list of files, must # be a list of strings self.assertRaises(AssertionError, Extension, 'name', 'file') self.assertRaises(AssertionError, Extension, 'name', ['file', 1]) ext = Extension('name', ['file1', 'file2']) self.assertEqual(ext.sources, ['file1', 'file2']) # others arguments have defaults for attr in ('include_dirs', 'define_macros', 'undef_macros', 'library_dirs', 'libraries', 'runtime_library_dirs', 'extra_objects', 'extra_compile_args', 'extra_link_args', 'export_symbols', 'swig_opts', 'depends'): self.assertEqual(getattr(ext, attr), []) self.assertEqual(ext.language, None) self.assertEqual(ext.optional, None) # if there are unknown keyword options, warn about them with check_warnings() as w: warnings.simplefilter('always') ext = Extension('name', ['file1', 'file2'], chic=True) self.assertEqual(len(w.warnings), 1) self.assertEqual(str(w.warnings[0].message), "Unknown Extension options: 'chic'") def test_suite(): return unittest.makeSuite(ExtensionTestCase) if __name__ == "__main__": run_unittest(test_suite())
2,768
70
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_clean.py
"""Tests for distutils.command.clean.""" import os import unittest from distutils.command.clean import clean from distutils.tests import support from test.support import run_unittest class cleanTestCase(support.TempdirManager, support.LoggingSilencer, unittest.TestCase): def test_simple_run(self): pkg_dir, dist = self.create_dist() cmd = clean(dist) # let's add some elements clean should remove dirs = [(d, os.path.join(pkg_dir, d)) for d in ('build_temp', 'build_lib', 'bdist_base', 'build_scripts', 'build_base')] for name, path in dirs: os.mkdir(path) setattr(cmd, name, path) if name == 'build_base': continue for f in ('one', 'two', 'three'): self.write_file(os.path.join(path, f)) # let's run the command cmd.all = 1 cmd.ensure_finalized() cmd.run() # make sure the files where removed for name, path in dirs: self.assertFalse(os.path.exists(path), '%s was not removed' % path) # let's run the command again (should spit warnings but succeed) cmd.all = 1 cmd.ensure_finalized() cmd.run() def test_suite(): return unittest.makeSuite(cleanTestCase) if __name__ == "__main__": run_unittest(test_suite())
1,441
50
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_unixccompiler.py
"""Tests for distutils.unixccompiler.""" import sys import unittest from test.support import EnvironmentVarGuard, run_unittest from distutils import sysconfig from distutils.unixccompiler import UnixCCompiler class UnixCCompilerTestCase(unittest.TestCase): def setUp(self): self._backup_platform = sys.platform self._backup_get_config_var = sysconfig.get_config_var class CompilerWrapper(UnixCCompiler): def rpath_foo(self): return self.runtime_library_dir_option('/foo') self.cc = CompilerWrapper() def tearDown(self): sys.platform = self._backup_platform sysconfig.get_config_var = self._backup_get_config_var @unittest.skipIf(sys.platform == 'win32', "can't test on Windows") def test_runtime_libdir_option(self): # Issue#5900 # # Ensure RUNPATH is added to extension modules with RPATH if # GNU ld is used # darwin sys.platform = 'darwin' self.assertEqual(self.cc.rpath_foo(), '-L/foo') # hp-ux sys.platform = 'hp-ux' old_gcv = sysconfig.get_config_var def gcv(v): return 'xxx' sysconfig.get_config_var = gcv self.assertEqual(self.cc.rpath_foo(), ['+s', '-L/foo']) def gcv(v): return 'gcc' sysconfig.get_config_var = gcv self.assertEqual(self.cc.rpath_foo(), ['-Wl,+s', '-L/foo']) def gcv(v): return 'g++' sysconfig.get_config_var = gcv self.assertEqual(self.cc.rpath_foo(), ['-Wl,+s', '-L/foo']) sysconfig.get_config_var = old_gcv # irix646 sys.platform = 'irix646' self.assertEqual(self.cc.rpath_foo(), ['-rpath', '/foo']) # osf1V5 sys.platform = 'osf1V5' self.assertEqual(self.cc.rpath_foo(), ['-rpath', '/foo']) # GCC GNULD sys.platform = 'bar' def gcv(v): if v == 'CC': return 'gcc' elif v == 'GNULD': return 'yes' sysconfig.get_config_var = gcv self.assertEqual(self.cc.rpath_foo(), '-Wl,--enable-new-dtags,-R/foo') # GCC non-GNULD sys.platform = 'bar' def gcv(v): if v == 'CC': return 'gcc' elif v == 'GNULD': return 'no' sysconfig.get_config_var = gcv self.assertEqual(self.cc.rpath_foo(), '-Wl,-R/foo') # GCC GNULD with fully qualified configuration prefix # see #7617 sys.platform = 'bar' def gcv(v): if v == 'CC': return 'x86_64-pc-linux-gnu-gcc-4.4.2' elif v == 'GNULD': return 'yes' sysconfig.get_config_var = gcv self.assertEqual(self.cc.rpath_foo(), '-Wl,--enable-new-dtags,-R/foo') # non-GCC GNULD sys.platform = 'bar' def gcv(v): if v == 'CC': return 'cc' elif v == 'GNULD': return 'yes' sysconfig.get_config_var = gcv self.assertEqual(self.cc.rpath_foo(), '-R/foo') # non-GCC non-GNULD sys.platform = 'bar' def gcv(v): if v == 'CC': return 'cc' elif v == 'GNULD': return 'no' sysconfig.get_config_var = gcv self.assertEqual(self.cc.rpath_foo(), '-R/foo') @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for OS X') def test_osx_cc_overrides_ldshared(self): # Issue #18080: # ensure that setting CC env variable also changes default linker def gcv(v): if v == 'LDSHARED': return 'gcc-4.2 -bundle -undefined dynamic_lookup ' return 'gcc-4.2' sysconfig.get_config_var = gcv with EnvironmentVarGuard() as env: env['CC'] = 'my_cc' del env['LDSHARED'] sysconfig.customize_compiler(self.cc) self.assertEqual(self.cc.linker_so[0], 'my_cc') @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for OS X') def test_osx_explicit_ldshared(self): # Issue #18080: # ensure that setting CC env variable does not change # explicit LDSHARED setting for linker def gcv(v): if v == 'LDSHARED': return 'gcc-4.2 -bundle -undefined dynamic_lookup ' return 'gcc-4.2' sysconfig.get_config_var = gcv with EnvironmentVarGuard() as env: env['CC'] = 'my_cc' env['LDSHARED'] = 'my_ld -bundle -dynamic' sysconfig.customize_compiler(self.cc) self.assertEqual(self.cc.linker_so[0], 'my_ld') def test_suite(): return unittest.makeSuite(UnixCCompilerTestCase) if __name__ == "__main__": run_unittest(test_suite())
4,862
150
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_build_ext.py
import sys import os from io import StringIO import textwrap from distutils.core import Distribution from distutils.command.build_ext import build_ext from distutils import sysconfig from distutils.tests.support import (TempdirManager, LoggingSilencer, copy_xxmodule_c, fixup_build_ext) from distutils.extension import Extension from distutils.errors import ( CompileError, DistutilsPlatformError, DistutilsSetupError, UnknownFileError) import unittest from test import support # http://bugs.python.org/issue4373 # Don't load the xx module more than once. ALREADY_TESTED = False class BuildExtTestCase(TempdirManager, LoggingSilencer, unittest.TestCase): def setUp(self): # Create a simple test environment # Note that we're making changes to sys.path super(BuildExtTestCase, self).setUp() self.tmp_dir = self.mkdtemp() self.sys_path = sys.path, sys.path[:] sys.path.append(self.tmp_dir) import site self.old_user_base = site.USER_BASE site.USER_BASE = self.mkdtemp() from distutils.command import build_ext build_ext.USER_BASE = site.USER_BASE # bpo-30132: On Windows, a .pdb file may be created in the current # working directory. Create a temporary working directory to cleanup # everything at the end of the test. self.temp_cwd = support.temp_cwd() self.temp_cwd.__enter__() self.addCleanup(self.temp_cwd.__exit__, None, None, None) def build_ext(self, *args, **kwargs): return build_ext(*args, **kwargs) def test_build_ext(self): cmd = support.missing_compiler_executable() if cmd is not None: self.skipTest('The %r command is not found' % cmd) global ALREADY_TESTED copy_xxmodule_c(self.tmp_dir) xx_c = os.path.join(self.tmp_dir, 'xxmodule.c') xx_ext = Extension('xx', [xx_c]) dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]}) dist.package_dir = self.tmp_dir cmd = self.build_ext(dist) fixup_build_ext(cmd) cmd.build_lib = self.tmp_dir cmd.build_temp = self.tmp_dir old_stdout = sys.stdout if not support.verbose: # silence compiler output sys.stdout = StringIO() try: cmd.ensure_finalized() cmd.run() finally: sys.stdout = old_stdout if ALREADY_TESTED: self.skipTest('Already tested in %s' % ALREADY_TESTED) else: ALREADY_TESTED = type(self).__name__ import xx for attr in ('error', 'foo', 'new', 'roj'): self.assertTrue(hasattr(xx, attr)) self.assertEqual(xx.foo(2, 5), 7) self.assertEqual(xx.foo(13,15), 28) self.assertEqual(xx.new().demo(), None) if support.HAVE_DOCSTRINGS: doc = 'This is a template module just for instruction.' self.assertEqual(xx.__doc__, doc) self.assertIsInstance(xx.Null(), xx.Null) self.assertIsInstance(xx.Str(), xx.Str) def tearDown(self): # Get everything back to normal support.unload('xx') sys.path = self.sys_path[0] sys.path[:] = self.sys_path[1] import site site.USER_BASE = self.old_user_base from distutils.command import build_ext build_ext.USER_BASE = self.old_user_base super(BuildExtTestCase, self).tearDown() def test_solaris_enable_shared(self): dist = Distribution({'name': 'xx'}) cmd = self.build_ext(dist) old = sys.platform sys.platform = 'sunos' # fooling finalize_options from distutils.sysconfig import _config_vars old_var = _config_vars.get('Py_ENABLE_SHARED') _config_vars['Py_ENABLE_SHARED'] = 1 try: cmd.ensure_finalized() finally: sys.platform = old if old_var is None: del _config_vars['Py_ENABLE_SHARED'] else: _config_vars['Py_ENABLE_SHARED'] = old_var # make sure we get some library dirs under solaris self.assertGreater(len(cmd.library_dirs), 0) def test_user_site(self): import site dist = Distribution({'name': 'xx'}) cmd = self.build_ext(dist) # making sure the user option is there options = [name for name, short, lable in cmd.user_options] self.assertIn('user', options) # setting a value cmd.user = 1 # setting user based lib and include lib = os.path.join(site.USER_BASE, 'lib') incl = os.path.join(site.USER_BASE, 'include') os.mkdir(lib) os.mkdir(incl) # let's run finalize cmd.ensure_finalized() # see if include_dirs and library_dirs # were set self.assertIn(lib, cmd.library_dirs) self.assertIn(lib, cmd.rpath) self.assertIn(incl, cmd.include_dirs) def test_optional_extension(self): # this extension will fail, but let's ignore this failure # with the optional argument. modules = [Extension('foo', ['xxx'], optional=False)] dist = Distribution({'name': 'xx', 'ext_modules': modules}) cmd = self.build_ext(dist) cmd.ensure_finalized() self.assertRaises((UnknownFileError, CompileError), cmd.run) # should raise an error modules = [Extension('foo', ['xxx'], optional=True)] dist = Distribution({'name': 'xx', 'ext_modules': modules}) cmd = self.build_ext(dist) cmd.ensure_finalized() cmd.run() # should pass def test_finalize_options(self): # Make sure Python's include directories (for Python.h, pyconfig.h, # etc.) are in the include search path. modules = [Extension('foo', ['xxx'], optional=False)] dist = Distribution({'name': 'xx', 'ext_modules': modules}) cmd = self.build_ext(dist) cmd.finalize_options() py_include = sysconfig.get_python_inc() self.assertIn(py_include, cmd.include_dirs) plat_py_include = sysconfig.get_python_inc(plat_specific=1) self.assertIn(plat_py_include, cmd.include_dirs) # make sure cmd.libraries is turned into a list # if it's a string cmd = self.build_ext(dist) cmd.libraries = 'my_lib, other_lib lastlib' cmd.finalize_options() self.assertEqual(cmd.libraries, ['my_lib', 'other_lib', 'lastlib']) # make sure cmd.library_dirs is turned into a list # if it's a string cmd = self.build_ext(dist) cmd.library_dirs = 'my_lib_dir%sother_lib_dir' % os.pathsep cmd.finalize_options() self.assertIn('my_lib_dir', cmd.library_dirs) self.assertIn('other_lib_dir', cmd.library_dirs) # make sure rpath is turned into a list # if it's a string cmd = self.build_ext(dist) cmd.rpath = 'one%stwo' % os.pathsep cmd.finalize_options() self.assertEqual(cmd.rpath, ['one', 'two']) # make sure cmd.link_objects is turned into a list # if it's a string cmd = build_ext(dist) cmd.link_objects = 'one two,three' cmd.finalize_options() self.assertEqual(cmd.link_objects, ['one', 'two', 'three']) # XXX more tests to perform for win32 # make sure define is turned into 2-tuples # strings if they are ','-separated strings cmd = self.build_ext(dist) cmd.define = 'one,two' cmd.finalize_options() self.assertEqual(cmd.define, [('one', '1'), ('two', '1')]) # make sure undef is turned into a list of # strings if they are ','-separated strings cmd = self.build_ext(dist) cmd.undef = 'one,two' cmd.finalize_options() self.assertEqual(cmd.undef, ['one', 'two']) # make sure swig_opts is turned into a list cmd = self.build_ext(dist) cmd.swig_opts = None cmd.finalize_options() self.assertEqual(cmd.swig_opts, []) cmd = self.build_ext(dist) cmd.swig_opts = '1 2' cmd.finalize_options() self.assertEqual(cmd.swig_opts, ['1', '2']) def test_check_extensions_list(self): dist = Distribution() cmd = self.build_ext(dist) cmd.finalize_options() #'extensions' option must be a list of Extension instances self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, 'foo') # each element of 'ext_modules' option must be an # Extension instance or 2-tuple exts = [('bar', 'foo', 'bar'), 'foo'] self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts) # first element of each tuple in 'ext_modules' # must be the extension name (a string) and match # a python dotted-separated name exts = [('foo-bar', '')] self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts) # second element of each tuple in 'ext_modules' # must be a dictionary (build info) exts = [('foo.bar', '')] self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts) # ok this one should pass exts = [('foo.bar', {'sources': [''], 'libraries': 'foo', 'some': 'bar'})] cmd.check_extensions_list(exts) ext = exts[0] self.assertIsInstance(ext, Extension) # check_extensions_list adds in ext the values passed # when they are in ('include_dirs', 'library_dirs', 'libraries' # 'extra_objects', 'extra_compile_args', 'extra_link_args') self.assertEqual(ext.libraries, 'foo') self.assertFalse(hasattr(ext, 'some')) # 'macros' element of build info dict must be 1- or 2-tuple exts = [('foo.bar', {'sources': [''], 'libraries': 'foo', 'some': 'bar', 'macros': [('1', '2', '3'), 'foo']})] self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts) exts[0][1]['macros'] = [('1', '2'), ('3',)] cmd.check_extensions_list(exts) self.assertEqual(exts[0].undef_macros, ['3']) self.assertEqual(exts[0].define_macros, [('1', '2')]) def test_get_source_files(self): modules = [Extension('foo', ['xxx'], optional=False)] dist = Distribution({'name': 'xx', 'ext_modules': modules}) cmd = self.build_ext(dist) cmd.ensure_finalized() self.assertEqual(cmd.get_source_files(), ['xxx']) def test_compiler_option(self): # cmd.compiler is an option and # should not be overridden by a compiler instance # when the command is run dist = Distribution() cmd = self.build_ext(dist) cmd.compiler = 'unix' cmd.ensure_finalized() cmd.run() self.assertEqual(cmd.compiler, 'unix') def test_get_outputs(self): cmd = support.missing_compiler_executable() if cmd is not None: self.skipTest('The %r command is not found' % cmd) tmp_dir = self.mkdtemp() c_file = os.path.join(tmp_dir, 'foo.c') self.write_file(c_file, 'void PyInit_foo(void) {}\n') ext = Extension('foo', [c_file], optional=False) dist = Distribution({'name': 'xx', 'ext_modules': [ext]}) cmd = self.build_ext(dist) fixup_build_ext(cmd) cmd.ensure_finalized() self.assertEqual(len(cmd.get_outputs()), 1) cmd.build_lib = os.path.join(self.tmp_dir, 'build') cmd.build_temp = os.path.join(self.tmp_dir, 'tempt') # issue #5977 : distutils build_ext.get_outputs # returns wrong result with --inplace other_tmp_dir = os.path.realpath(self.mkdtemp()) old_wd = os.getcwd() os.chdir(other_tmp_dir) try: cmd.inplace = 1 cmd.run() so_file = cmd.get_outputs()[0] finally: os.chdir(old_wd) self.assertTrue(os.path.exists(so_file)) ext_suffix = sysconfig.get_config_var('EXT_SUFFIX') self.assertTrue(so_file.endswith(ext_suffix)) so_dir = os.path.dirname(so_file) self.assertEqual(so_dir, other_tmp_dir) cmd.inplace = 0 cmd.compiler = None cmd.run() so_file = cmd.get_outputs()[0] self.assertTrue(os.path.exists(so_file)) self.assertTrue(so_file.endswith(ext_suffix)) so_dir = os.path.dirname(so_file) self.assertEqual(so_dir, cmd.build_lib) # inplace = 0, cmd.package = 'bar' build_py = cmd.get_finalized_command('build_py') build_py.package_dir = {'': 'bar'} path = cmd.get_ext_fullpath('foo') # checking that the last directory is the build_dir path = os.path.split(path)[0] self.assertEqual(path, cmd.build_lib) # inplace = 1, cmd.package = 'bar' cmd.inplace = 1 other_tmp_dir = os.path.realpath(self.mkdtemp()) old_wd = os.getcwd() os.chdir(other_tmp_dir) try: path = cmd.get_ext_fullpath('foo') finally: os.chdir(old_wd) # checking that the last directory is bar path = os.path.split(path)[0] lastdir = os.path.split(path)[-1] self.assertEqual(lastdir, 'bar') def test_ext_fullpath(self): ext = sysconfig.get_config_var('EXT_SUFFIX') # building lxml.etree inplace #etree_c = os.path.join(self.tmp_dir, 'lxml.etree.c') #etree_ext = Extension('lxml.etree', [etree_c]) #dist = Distribution({'name': 'lxml', 'ext_modules': [etree_ext]}) dist = Distribution() cmd = self.build_ext(dist) cmd.inplace = 1 cmd.distribution.package_dir = {'': 'src'} cmd.distribution.packages = ['lxml', 'lxml.html'] curdir = os.getcwd() wanted = os.path.join(curdir, 'src', 'lxml', 'etree' + ext) path = cmd.get_ext_fullpath('lxml.etree') self.assertEqual(wanted, path) # building lxml.etree not inplace cmd.inplace = 0 cmd.build_lib = os.path.join(curdir, 'tmpdir') wanted = os.path.join(curdir, 'tmpdir', 'lxml', 'etree' + ext) path = cmd.get_ext_fullpath('lxml.etree') self.assertEqual(wanted, path) # building twisted.runner.portmap not inplace build_py = cmd.get_finalized_command('build_py') build_py.package_dir = {} cmd.distribution.packages = ['twisted', 'twisted.runner.portmap'] path = cmd.get_ext_fullpath('twisted.runner.portmap') wanted = os.path.join(curdir, 'tmpdir', 'twisted', 'runner', 'portmap' + ext) self.assertEqual(wanted, path) # building twisted.runner.portmap inplace cmd.inplace = 1 path = cmd.get_ext_fullpath('twisted.runner.portmap') wanted = os.path.join(curdir, 'twisted', 'runner', 'portmap' + ext) self.assertEqual(wanted, path) @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for MacOSX') def test_deployment_target_default(self): # Issue 9516: Test that, in the absence of the environment variable, # an extension module is compiled with the same deployment target as # the interpreter. self._try_compile_deployment_target('==', None) @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for MacOSX') def test_deployment_target_too_low(self): # Issue 9516: Test that an extension module is not allowed to be # compiled with a deployment target less than that of the interpreter. self.assertRaises(DistutilsPlatformError, self._try_compile_deployment_target, '>', '10.1') @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for MacOSX') def test_deployment_target_higher_ok(self): # Issue 9516: Test that an extension module can be compiled with a # deployment target higher than that of the interpreter: the ext # module may depend on some newer OS feature. deptarget = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') if deptarget: # increment the minor version number (i.e. 10.6 -> 10.7) deptarget = [int(x) for x in deptarget.split('.')] deptarget[-1] += 1 deptarget = '.'.join(str(i) for i in deptarget) self._try_compile_deployment_target('<', deptarget) def _try_compile_deployment_target(self, operator, target): orig_environ = os.environ os.environ = orig_environ.copy() self.addCleanup(setattr, os, 'environ', orig_environ) if target is None: if os.environ.get('MACOSX_DEPLOYMENT_TARGET'): del os.environ['MACOSX_DEPLOYMENT_TARGET'] else: os.environ['MACOSX_DEPLOYMENT_TARGET'] = target deptarget_c = os.path.join(self.tmp_dir, 'deptargetmodule.c') with open(deptarget_c, 'w') as fp: fp.write(textwrap.dedent('''\ #include <AvailabilityMacros.h> int dummy; #if TARGET %s MAC_OS_X_VERSION_MIN_REQUIRED #else #error "Unexpected target" #endif ''' % operator)) # get the deployment target that the interpreter was built with target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') target = tuple(map(int, target.split('.')[0:2])) # format the target value as defined in the Apple # Availability Macros. We can't use the macro names since # at least one value we test with will not exist yet. if target[1] < 10: # for 10.1 through 10.9.x -> "10n0" target = '%02d%01d0' % target else: # for 10.10 and beyond -> "10nn00" target = '%02d%02d00' % target deptarget_ext = Extension( 'deptarget', [deptarget_c], extra_compile_args=['-DTARGET=%s'%(target,)], ) dist = Distribution({ 'name': 'deptarget', 'ext_modules': [deptarget_ext] }) dist.package_dir = self.tmp_dir cmd = self.build_ext(dist) cmd.build_lib = self.tmp_dir cmd.build_temp = self.tmp_dir try: old_stdout = sys.stdout if not support.verbose: # silence compiler output sys.stdout = StringIO() try: cmd.ensure_finalized() cmd.run() finally: sys.stdout = old_stdout except CompileError: self.fail("Wrong deployment target during compilation") class ParallelBuildExtTestCase(BuildExtTestCase): def build_ext(self, *args, **kwargs): build_ext = super().build_ext(*args, **kwargs) build_ext.parallel = True return build_ext def test_suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(BuildExtTestCase)) suite.addTest(unittest.makeSuite(ParallelBuildExtTestCase)) return suite if __name__ == '__main__': support.run_unittest(__name__)
19,446
522
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_dist.py
"""Tests for distutils.dist.""" import os import io import sys import unittest import warnings import textwrap from unittest import mock from distutils.dist import Distribution, fix_help_options, DistributionMetadata from distutils.cmd import Command from test.support import TESTFN, captured_stdout, run_unittest from distutils.tests import support from distutils import log class test_dist(Command): """Sample distutils extension command.""" user_options = [ ("sample-option=", "S", "help text"), ] def initialize_options(self): self.sample_option = None class TestDistribution(Distribution): """Distribution subclasses that avoids the default search for configuration files. The ._config_files attribute must be set before .parse_config_files() is called. """ def find_config_files(self): return self._config_files class DistributionTestCase(support.LoggingSilencer, support.TempdirManager, support.EnvironGuard, unittest.TestCase): def setUp(self): super(DistributionTestCase, self).setUp() self.argv = sys.argv, sys.argv[:] del sys.argv[1:] def tearDown(self): sys.argv = self.argv[0] sys.argv[:] = self.argv[1] super(DistributionTestCase, self).tearDown() def create_distribution(self, configfiles=()): d = TestDistribution() d._config_files = configfiles d.parse_config_files() d.parse_command_line() return d def test_command_packages_unspecified(self): sys.argv.append("build") d = self.create_distribution() self.assertEqual(d.get_command_packages(), ["distutils.command"]) def test_command_packages_cmdline(self): from distutils.tests.test_dist import test_dist sys.argv.extend(["--command-packages", "foo.bar,distutils.tests", "test_dist", "-Ssometext", ]) d = self.create_distribution() # let's actually try to load our test command: self.assertEqual(d.get_command_packages(), ["distutils.command", "foo.bar", "distutils.tests"]) cmd = d.get_command_obj("test_dist") self.assertIsInstance(cmd, test_dist) self.assertEqual(cmd.sample_option, "sometext") def test_venv_install_options(self): sys.argv.append("install") self.addCleanup(os.unlink, TESTFN) fakepath = '/somedir' with open(TESTFN, "w") as f: print(("[install]\n" "install-base = {0}\n" "install-platbase = {0}\n" "install-lib = {0}\n" "install-platlib = {0}\n" "install-purelib = {0}\n" "install-headers = {0}\n" "install-scripts = {0}\n" "install-data = {0}\n" "prefix = {0}\n" "exec-prefix = {0}\n" "home = {0}\n" "user = {0}\n" "root = {0}").format(fakepath), file=f) # Base case: Not in a Virtual Environment with mock.patch.multiple(sys, prefix='/a', base_prefix='/a') as values: d = self.create_distribution([TESTFN]) option_tuple = (TESTFN, fakepath) result_dict = { 'install_base': option_tuple, 'install_platbase': option_tuple, 'install_lib': option_tuple, 'install_platlib': option_tuple, 'install_purelib': option_tuple, 'install_headers': option_tuple, 'install_scripts': option_tuple, 'install_data': option_tuple, 'prefix': option_tuple, 'exec_prefix': option_tuple, 'home': option_tuple, 'user': option_tuple, 'root': option_tuple, } self.assertEqual( sorted(d.command_options.get('install').keys()), sorted(result_dict.keys())) for (key, value) in d.command_options.get('install').items(): self.assertEqual(value, result_dict[key]) # Test case: In a Virtual Environment with mock.patch.multiple(sys, prefix='/a', base_prefix='/b') as values: d = self.create_distribution([TESTFN]) for key in result_dict.keys(): self.assertNotIn(key, d.command_options.get('install', {})) def test_command_packages_configfile(self): sys.argv.append("build") self.addCleanup(os.unlink, TESTFN) f = open(TESTFN, "w") try: print("[global]", file=f) print("command_packages = foo.bar, splat", file=f) finally: f.close() d = self.create_distribution([TESTFN]) self.assertEqual(d.get_command_packages(), ["distutils.command", "foo.bar", "splat"]) # ensure command line overrides config: sys.argv[1:] = ["--command-packages", "spork", "build"] d = self.create_distribution([TESTFN]) self.assertEqual(d.get_command_packages(), ["distutils.command", "spork"]) # Setting --command-packages to '' should cause the default to # be used even if a config file specified something else: sys.argv[1:] = ["--command-packages", "", "build"] d = self.create_distribution([TESTFN]) self.assertEqual(d.get_command_packages(), ["distutils.command"]) def test_empty_options(self): # an empty options dictionary should not stay in the # list of attributes # catching warnings warns = [] def _warn(msg): warns.append(msg) self.addCleanup(setattr, warnings, 'warn', warnings.warn) warnings.warn = _warn dist = Distribution(attrs={'author': 'xxx', 'name': 'xxx', 'version': 'xxx', 'url': 'xxxx', 'options': {}}) self.assertEqual(len(warns), 0) self.assertNotIn('options', dir(dist)) def test_finalize_options(self): attrs = {'keywords': 'one,two', 'platforms': 'one,two'} dist = Distribution(attrs=attrs) dist.finalize_options() # finalize_option splits platforms and keywords self.assertEqual(dist.metadata.platforms, ['one', 'two']) self.assertEqual(dist.metadata.keywords, ['one', 'two']) def test_get_command_packages(self): dist = Distribution() self.assertEqual(dist.command_packages, None) cmds = dist.get_command_packages() self.assertEqual(cmds, ['distutils.command']) self.assertEqual(dist.command_packages, ['distutils.command']) dist.command_packages = 'one,two' cmds = dist.get_command_packages() self.assertEqual(cmds, ['distutils.command', 'one', 'two']) def test_announce(self): # make sure the level is known dist = Distribution() args = ('ok',) kwargs = {'level': 'ok2'} self.assertRaises(ValueError, dist.announce, args, kwargs) def test_find_config_files_disable(self): # Ticket #1180: Allow user to disable their home config file. temp_home = self.mkdtemp() if os.name == 'posix': user_filename = os.path.join(temp_home, ".pydistutils.cfg") else: user_filename = os.path.join(temp_home, "pydistutils.cfg") with open(user_filename, 'w') as f: f.write('[distutils]\n') def _expander(path): return temp_home old_expander = os.path.expanduser os.path.expanduser = _expander try: d = Distribution() all_files = d.find_config_files() d = Distribution(attrs={'script_args': ['--no-user-cfg']}) files = d.find_config_files() finally: os.path.expanduser = old_expander # make sure --no-user-cfg disables the user cfg file self.assertEqual(len(all_files)-1, len(files)) class MetadataTestCase(support.TempdirManager, support.EnvironGuard, unittest.TestCase): def setUp(self): super(MetadataTestCase, self).setUp() self.argv = sys.argv, sys.argv[:] def tearDown(self): sys.argv = self.argv[0] sys.argv[:] = self.argv[1] super(MetadataTestCase, self).tearDown() def format_metadata(self, dist): sio = io.StringIO() dist.metadata.write_pkg_file(sio) return sio.getvalue() def test_simple_metadata(self): attrs = {"name": "package", "version": "1.0"} dist = Distribution(attrs) meta = self.format_metadata(dist) self.assertIn("Metadata-Version: 1.0", meta) self.assertNotIn("provides:", meta.lower()) self.assertNotIn("requires:", meta.lower()) self.assertNotIn("obsoletes:", meta.lower()) def test_provides(self): attrs = {"name": "package", "version": "1.0", "provides": ["package", "package.sub"]} dist = Distribution(attrs) self.assertEqual(dist.metadata.get_provides(), ["package", "package.sub"]) self.assertEqual(dist.get_provides(), ["package", "package.sub"]) meta = self.format_metadata(dist) self.assertIn("Metadata-Version: 1.1", meta) self.assertNotIn("requires:", meta.lower()) self.assertNotIn("obsoletes:", meta.lower()) def test_provides_illegal(self): self.assertRaises(ValueError, Distribution, {"name": "package", "version": "1.0", "provides": ["my.pkg (splat)"]}) def test_requires(self): attrs = {"name": "package", "version": "1.0", "requires": ["other", "another (==1.0)"]} dist = Distribution(attrs) self.assertEqual(dist.metadata.get_requires(), ["other", "another (==1.0)"]) self.assertEqual(dist.get_requires(), ["other", "another (==1.0)"]) meta = self.format_metadata(dist) self.assertIn("Metadata-Version: 1.1", meta) self.assertNotIn("provides:", meta.lower()) self.assertIn("Requires: other", meta) self.assertIn("Requires: another (==1.0)", meta) self.assertNotIn("obsoletes:", meta.lower()) def test_requires_illegal(self): self.assertRaises(ValueError, Distribution, {"name": "package", "version": "1.0", "requires": ["my.pkg (splat)"]}) def test_obsoletes(self): attrs = {"name": "package", "version": "1.0", "obsoletes": ["other", "another (<1.0)"]} dist = Distribution(attrs) self.assertEqual(dist.metadata.get_obsoletes(), ["other", "another (<1.0)"]) self.assertEqual(dist.get_obsoletes(), ["other", "another (<1.0)"]) meta = self.format_metadata(dist) self.assertIn("Metadata-Version: 1.1", meta) self.assertNotIn("provides:", meta.lower()) self.assertNotIn("requires:", meta.lower()) self.assertIn("Obsoletes: other", meta) self.assertIn("Obsoletes: another (<1.0)", meta) def test_obsoletes_illegal(self): self.assertRaises(ValueError, Distribution, {"name": "package", "version": "1.0", "obsoletes": ["my.pkg (splat)"]}) def test_classifier(self): attrs = {'name': 'Boa', 'version': '3.0', 'classifiers': ['Programming Language :: Python :: 3']} dist = Distribution(attrs) meta = self.format_metadata(dist) self.assertIn('Metadata-Version: 1.1', meta) def test_download_url(self): attrs = {'name': 'Boa', 'version': '3.0', 'download_url': 'http://example.org/boa'} dist = Distribution(attrs) meta = self.format_metadata(dist) self.assertIn('Metadata-Version: 1.1', meta) def test_long_description(self): long_desc = textwrap.dedent("""\ example:: We start here and continue here and end here.""") attrs = {"name": "package", "version": "1.0", "long_description": long_desc} dist = Distribution(attrs) meta = self.format_metadata(dist) meta = meta.replace('\n' + 8 * ' ', '\n') self.assertIn(long_desc, meta) def test_custom_pydistutils(self): # fixes #2166 # make sure pydistutils.cfg is found if os.name == 'posix': user_filename = ".pydistutils.cfg" else: user_filename = "pydistutils.cfg" temp_dir = self.mkdtemp() user_filename = os.path.join(temp_dir, user_filename) f = open(user_filename, 'w') try: f.write('.') finally: f.close() try: dist = Distribution() # linux-style if sys.platform in ('linux', 'darwin'): os.environ['HOME'] = temp_dir files = dist.find_config_files() self.assertIn(user_filename, files) # win32-style if sys.platform == 'win32': # home drive should be found os.environ['HOME'] = temp_dir files = dist.find_config_files() self.assertIn(user_filename, files, '%r not found in %r' % (user_filename, files)) finally: os.remove(user_filename) def test_fix_help_options(self): help_tuples = [('a', 'b', 'c', 'd'), (1, 2, 3, 4)] fancy_options = fix_help_options(help_tuples) self.assertEqual(fancy_options[0], ('a', 'b', 'c')) self.assertEqual(fancy_options[1], (1, 2, 3)) def test_show_help(self): # smoke test, just makes sure some help is displayed self.addCleanup(log.set_threshold, log._global_log.threshold) dist = Distribution() sys.argv = [] dist.help = 1 dist.script_name = 'setup.py' with captured_stdout() as s: dist.parse_command_line() output = [line for line in s.getvalue().split('\n') if line.strip() != ''] self.assertTrue(output) def test_read_metadata(self): attrs = {"name": "package", "version": "1.0", "long_description": "desc", "description": "xxx", "download_url": "http://example.com", "keywords": ['one', 'two'], "requires": ['foo']} dist = Distribution(attrs) metadata = dist.metadata # write it then reloads it PKG_INFO = io.StringIO() metadata.write_pkg_file(PKG_INFO) PKG_INFO.seek(0) metadata.read_pkg_file(PKG_INFO) self.assertEqual(metadata.name, "package") self.assertEqual(metadata.version, "1.0") self.assertEqual(metadata.description, "xxx") self.assertEqual(metadata.download_url, 'http://example.com') self.assertEqual(metadata.keywords, ['one', 'two']) self.assertEqual(metadata.platforms, ['UNKNOWN']) self.assertEqual(metadata.obsoletes, None) self.assertEqual(metadata.requires, ['foo']) def test_suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(DistributionTestCase)) suite.addTest(unittest.makeSuite(MetadataTestCase)) return suite if __name__ == "__main__": run_unittest(test_suite())
16,050
457
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_bdist_wininst.py
"""Tests for distutils.command.bdist_wininst.""" import unittest from test.support import run_unittest from distutils.command.bdist_wininst import bdist_wininst from distutils.tests import support @unittest.skipIf(getattr(bdist_wininst, '_unsupported', False), 'bdist_wininst is not supported in this install') class BuildWinInstTestCase(support.TempdirManager, support.LoggingSilencer, unittest.TestCase): def test_get_exe_bytes(self): # issue5731: command was broken on non-windows platforms # this test makes sure it works now for every platform # let's create a command pkg_pth, dist = self.create_dist() cmd = bdist_wininst(dist) cmd.ensure_finalized() # let's run the code that finds the right wininst*.exe file # and make sure it finds it and returns its content # no matter what platform we have exe_file = cmd.get_exe_bytes() self.assertGreater(len(exe_file), 10) def test_suite(): return unittest.makeSuite(BuildWinInstTestCase) if __name__ == '__main__': run_unittest(test_suite())
1,158
34
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_dir_util.py
"""Tests for distutils.dir_util.""" import unittest import os import stat import sys from unittest.mock import patch from distutils import dir_util, errors from distutils.dir_util import (mkpath, remove_tree, create_tree, copy_tree, ensure_relative) from distutils import log from distutils.tests import support from test.support import run_unittest class DirUtilTestCase(support.TempdirManager, unittest.TestCase): def _log(self, msg, *args): if len(args) > 0: self._logs.append(msg % args) else: self._logs.append(msg) def setUp(self): super(DirUtilTestCase, self).setUp() self._logs = [] tmp_dir = self.mkdtemp() self.root_target = os.path.join(tmp_dir, 'deep') self.target = os.path.join(self.root_target, 'here') self.target2 = os.path.join(tmp_dir, 'deep2') self.old_log = log.info log.info = self._log def tearDown(self): log.info = self.old_log super(DirUtilTestCase, self).tearDown() def test_mkpath_remove_tree_verbosity(self): mkpath(self.target, verbose=0) wanted = [] self.assertEqual(self._logs, wanted) remove_tree(self.root_target, verbose=0) mkpath(self.target, verbose=1) wanted = ['creating %s' % self.root_target, 'creating %s' % self.target] self.assertEqual(self._logs, wanted) self._logs = [] remove_tree(self.root_target, verbose=1) wanted = ["removing '%s' (and everything under it)" % self.root_target] self.assertEqual(self._logs, wanted) @unittest.skipIf(sys.platform.startswith('win'), "This test is only appropriate for POSIX-like systems.") def test_mkpath_with_custom_mode(self): # Get and set the current umask value for testing mode bits. umask = os.umask(0o002) os.umask(umask) mkpath(self.target, 0o700) self.assertEqual( stat.S_IMODE(os.stat(self.target).st_mode), 0o700 & ~umask) mkpath(self.target2, 0o555) self.assertEqual( stat.S_IMODE(os.stat(self.target2).st_mode), 0o555 & ~umask) def test_create_tree_verbosity(self): create_tree(self.root_target, ['one', 'two', 'three'], verbose=0) self.assertEqual(self._logs, []) remove_tree(self.root_target, verbose=0) wanted = ['creating %s' % self.root_target] create_tree(self.root_target, ['one', 'two', 'three'], verbose=1) self.assertEqual(self._logs, wanted) remove_tree(self.root_target, verbose=0) def test_copy_tree_verbosity(self): mkpath(self.target, verbose=0) copy_tree(self.target, self.target2, verbose=0) self.assertEqual(self._logs, []) remove_tree(self.root_target, verbose=0) mkpath(self.target, verbose=0) a_file = os.path.join(self.target, 'ok.txt') with open(a_file, 'w') as f: f.write('some content') wanted = ['copying %s -> %s' % (a_file, self.target2)] copy_tree(self.target, self.target2, verbose=1) self.assertEqual(self._logs, wanted) remove_tree(self.root_target, verbose=0) remove_tree(self.target2, verbose=0) def test_copy_tree_skips_nfs_temp_files(self): mkpath(self.target, verbose=0) a_file = os.path.join(self.target, 'ok.txt') nfs_file = os.path.join(self.target, '.nfs123abc') for f in a_file, nfs_file: with open(f, 'w') as fh: fh.write('some content') copy_tree(self.target, self.target2) self.assertEqual(os.listdir(self.target2), ['ok.txt']) remove_tree(self.root_target, verbose=0) remove_tree(self.target2, verbose=0) def test_ensure_relative(self): if os.sep == '/': self.assertEqual(ensure_relative('/home/foo'), 'home/foo') self.assertEqual(ensure_relative('some/path'), 'some/path') else: # \\ self.assertEqual(ensure_relative('c:\\home\\foo'), 'c:home\\foo') self.assertEqual(ensure_relative('home\\foo'), 'home\\foo') def test_copy_tree_exception_in_listdir(self): """ An exception in listdir should raise a DistutilsFileError """ with patch("os.listdir", side_effect=OSError()), \ self.assertRaises(errors.DistutilsFileError): src = self.tempdirs[-1] dir_util.copy_tree(src, None) def test_suite(): return unittest.makeSuite(DirUtilTestCase) if __name__ == "__main__": run_unittest(test_suite())
4,654
140
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_build_py.py
"""Tests for distutils.command.build_py.""" import os import sys import unittest from distutils.command.build_py import build_py from distutils.core import Distribution from distutils.errors import DistutilsFileError from distutils.tests import support from test.support import run_unittest class BuildPyTestCase(support.TempdirManager, support.LoggingSilencer, unittest.TestCase): def test_package_data(self): sources = self.mkdtemp() f = open(os.path.join(sources, "__init__.py"), "w") try: f.write("# Pretend this is a package.") finally: f.close() f = open(os.path.join(sources, "README.txt"), "w") try: f.write("Info about this package") finally: f.close() destination = self.mkdtemp() dist = Distribution({"packages": ["pkg"], "package_dir": {"pkg": sources}}) # script_name need not exist, it just need to be initialized dist.script_name = os.path.join(sources, "setup.py") dist.command_obj["build"] = support.DummyCommand( force=0, build_lib=destination) dist.packages = ["pkg"] dist.package_data = {"pkg": ["README.txt"]} dist.package_dir = {"pkg": sources} cmd = build_py(dist) cmd.compile = 1 cmd.ensure_finalized() self.assertEqual(cmd.package_data, dist.package_data) cmd.run() # This makes sure the list of outputs includes byte-compiled # files for Python modules but not for package data files # (there shouldn't *be* byte-code files for those!). self.assertEqual(len(cmd.get_outputs()), 3) pkgdest = os.path.join(destination, "pkg") files = os.listdir(pkgdest) pycache_dir = os.path.join(pkgdest, "__pycache__") self.assertIn("__init__.py", files) self.assertIn("README.txt", files) if sys.dont_write_bytecode: self.assertFalse(os.path.exists(pycache_dir)) else: pyc_files = os.listdir(pycache_dir) self.assertIn("__init__.%s.pyc" % sys.implementation.cache_tag, pyc_files) def test_empty_package_dir(self): # See bugs #1668596/#1720897 sources = self.mkdtemp() open(os.path.join(sources, "__init__.py"), "w").close() testdir = os.path.join(sources, "doc") os.mkdir(testdir) open(os.path.join(testdir, "testfile"), "w").close() os.chdir(sources) dist = Distribution({"packages": ["pkg"], "package_dir": {"pkg": ""}, "package_data": {"pkg": ["doc/*"]}}) # script_name need not exist, it just need to be initialized dist.script_name = os.path.join(sources, "setup.py") dist.script_args = ["build"] dist.parse_command_line() try: dist.run_commands() except DistutilsFileError: self.fail("failed package_data test when package_dir is ''") @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled') def test_byte_compile(self): project_dir, dist = self.create_dist(py_modules=['boiledeggs']) os.chdir(project_dir) self.write_file('boiledeggs.py', 'import antigravity') cmd = build_py(dist) cmd.compile = 1 cmd.build_lib = 'here' cmd.finalize_options() cmd.run() found = os.listdir(cmd.build_lib) self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py']) found = os.listdir(os.path.join(cmd.build_lib, '__pycache__')) self.assertEqual(found, ['boiledeggs.%s.pyc' % sys.implementation.cache_tag]) @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled') def test_byte_compile_optimized(self): project_dir, dist = self.create_dist(py_modules=['boiledeggs']) os.chdir(project_dir) self.write_file('boiledeggs.py', 'import antigravity') cmd = build_py(dist) cmd.compile = 0 cmd.optimize = 1 cmd.build_lib = 'here' cmd.finalize_options() cmd.run() found = os.listdir(cmd.build_lib) self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py']) found = os.listdir(os.path.join(cmd.build_lib, '__pycache__')) expect = 'boiledeggs.{}.opt-1.pyc'.format(sys.implementation.cache_tag) self.assertEqual(sorted(found), [expect]) def test_dir_in_package_data(self): """ A directory in package_data should not be added to the filelist. """ # See bug 19286 sources = self.mkdtemp() pkg_dir = os.path.join(sources, "pkg") os.mkdir(pkg_dir) open(os.path.join(pkg_dir, "__init__.py"), "w").close() docdir = os.path.join(pkg_dir, "doc") os.mkdir(docdir) open(os.path.join(docdir, "testfile"), "w").close() # create the directory that could be incorrectly detected as a file os.mkdir(os.path.join(docdir, 'otherdir')) os.chdir(sources) dist = Distribution({"packages": ["pkg"], "package_data": {"pkg": ["doc/*"]}}) # script_name need not exist, it just need to be initialized dist.script_name = os.path.join(sources, "setup.py") dist.script_args = ["build"] dist.parse_command_line() try: dist.run_commands() except DistutilsFileError: self.fail("failed package_data when data dir includes a dir") def test_dont_write_bytecode(self): # makes sure byte_compile is not used dist = self.create_dist()[1] cmd = build_py(dist) cmd.compile = 1 cmd.optimize = 1 old_dont_write_bytecode = sys.dont_write_bytecode sys.dont_write_bytecode = True try: cmd.byte_compile([]) finally: sys.dont_write_bytecode = old_dont_write_bytecode self.assertIn('byte-compiling is disabled', self.logs[0][1] % self.logs[0][2]) def test_suite(): return unittest.makeSuite(BuildPyTestCase) if __name__ == "__main__": run_unittest(test_suite())
6,335
180
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_build_scripts.py
"""Tests for distutils.command.build_scripts.""" import os import unittest from distutils.command.build_scripts import build_scripts from distutils.core import Distribution from distutils import sysconfig from distutils.tests import support from test.support import run_unittest class BuildScriptsTestCase(support.TempdirManager, support.LoggingSilencer, unittest.TestCase): def test_default_settings(self): cmd = self.get_build_scripts_cmd("/foo/bar", []) self.assertFalse(cmd.force) self.assertIsNone(cmd.build_dir) cmd.finalize_options() self.assertTrue(cmd.force) self.assertEqual(cmd.build_dir, "/foo/bar") def test_build(self): source = self.mkdtemp() target = self.mkdtemp() expected = self.write_sample_scripts(source) cmd = self.get_build_scripts_cmd(target, [os.path.join(source, fn) for fn in expected]) cmd.finalize_options() cmd.run() built = os.listdir(target) for name in expected: self.assertIn(name, built) def get_build_scripts_cmd(self, target, scripts): import sys dist = Distribution() dist.scripts = scripts dist.command_obj["build"] = support.DummyCommand( build_scripts=target, force=1, executable=sys.executable ) return build_scripts(dist) def write_sample_scripts(self, dir): expected = [] expected.append("script1.py") self.write_script(dir, "script1.py", ("#! /usr/bin/env python2.3\n" "# bogus script w/ Python sh-bang\n" "pass\n")) expected.append("script2.py") self.write_script(dir, "script2.py", ("#!/usr/bin/python\n" "# bogus script w/ Python sh-bang\n" "pass\n")) expected.append("shell.sh") self.write_script(dir, "shell.sh", ("#!/bin/sh\n" "# bogus shell script w/ sh-bang\n" "exit 0\n")) return expected def write_script(self, dir, name, text): f = open(os.path.join(dir, name), "w") try: f.write(text) finally: f.close() def test_version_int(self): source = self.mkdtemp() target = self.mkdtemp() expected = self.write_sample_scripts(source) cmd = self.get_build_scripts_cmd(target, [os.path.join(source, fn) for fn in expected]) cmd.finalize_options() # http://bugs.python.org/issue4524 # # On linux-g++-32 with command line `./configure --enable-ipv6 # --with-suffix=3`, python is compiled okay but the build scripts # failed when writing the name of the executable old = sysconfig.get_config_vars().get('VERSION') sysconfig._config_vars['VERSION'] = 4 try: cmd.run() finally: if old is not None: sysconfig._config_vars['VERSION'] = old built = os.listdir(target) for name in expected: self.assertIn(name, built) def test_suite(): return unittest.makeSuite(BuildScriptsTestCase) if __name__ == "__main__": run_unittest(test_suite())
3,593
113
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_install.py
"""Tests for distutils.command.install.""" import os import sys import unittest import site from test.support import captured_stdout, run_unittest from distutils import sysconfig from distutils.command.install import install from distutils.command import install as install_module from distutils.command.build_ext import build_ext from distutils.command.install import INSTALL_SCHEMES from distutils.core import Distribution from distutils.errors import DistutilsOptionError from distutils.extension import Extension from distutils.tests import support from test import support as test_support def _make_ext_name(modname): return modname + sysconfig.get_config_var('EXT_SUFFIX') class InstallTestCase(support.TempdirManager, support.EnvironGuard, support.LoggingSilencer, unittest.TestCase): def test_home_installation_scheme(self): # This ensure two things: # - that --home generates the desired set of directory names # - test --home is supported on all platforms builddir = self.mkdtemp() destination = os.path.join(builddir, "installation") dist = Distribution({"name": "foopkg"}) # script_name need not exist, it just need to be initialized dist.script_name = os.path.join(builddir, "setup.py") dist.command_obj["build"] = support.DummyCommand( build_base=builddir, build_lib=os.path.join(builddir, "lib"), ) cmd = install(dist) cmd.home = destination cmd.ensure_finalized() self.assertEqual(cmd.install_base, destination) self.assertEqual(cmd.install_platbase, destination) def check_path(got, expected): got = os.path.normpath(got) expected = os.path.normpath(expected) self.assertEqual(got, expected) libdir = os.path.join(destination, "lib", "python") check_path(cmd.install_lib, libdir) check_path(cmd.install_platlib, libdir) check_path(cmd.install_purelib, libdir) check_path(cmd.install_headers, os.path.join(destination, "include", "python", "foopkg")) check_path(cmd.install_scripts, os.path.join(destination, "bin")) check_path(cmd.install_data, destination) def test_user_site(self): # test install with --user # preparing the environment for the test self.old_user_base = site.USER_BASE self.old_user_site = site.USER_SITE self.tmpdir = self.mkdtemp() self.user_base = os.path.join(self.tmpdir, 'B') self.user_site = os.path.join(self.tmpdir, 'S') site.USER_BASE = self.user_base site.USER_SITE = self.user_site install_module.USER_BASE = self.user_base install_module.USER_SITE = self.user_site def _expanduser(path): return self.tmpdir self.old_expand = os.path.expanduser os.path.expanduser = _expanduser def cleanup(): site.USER_BASE = self.old_user_base site.USER_SITE = self.old_user_site install_module.USER_BASE = self.old_user_base install_module.USER_SITE = self.old_user_site os.path.expanduser = self.old_expand self.addCleanup(cleanup) for key in ('nt_user', 'unix_user'): self.assertIn(key, INSTALL_SCHEMES) dist = Distribution({'name': 'xx'}) cmd = install(dist) # making sure the user option is there options = [name for name, short, lable in cmd.user_options] self.assertIn('user', options) # setting a value cmd.user = 1 # user base and site shouldn't be created yet self.assertFalse(os.path.exists(self.user_base)) self.assertFalse(os.path.exists(self.user_site)) # let's run finalize cmd.ensure_finalized() # now they should self.assertTrue(os.path.exists(self.user_base)) self.assertTrue(os.path.exists(self.user_site)) self.assertIn('userbase', cmd.config_vars) self.assertIn('usersite', cmd.config_vars) def test_handle_extra_path(self): dist = Distribution({'name': 'xx', 'extra_path': 'path,dirs'}) cmd = install(dist) # two elements cmd.handle_extra_path() self.assertEqual(cmd.extra_path, ['path', 'dirs']) self.assertEqual(cmd.extra_dirs, 'dirs') self.assertEqual(cmd.path_file, 'path') # one element cmd.extra_path = ['path'] cmd.handle_extra_path() self.assertEqual(cmd.extra_path, ['path']) self.assertEqual(cmd.extra_dirs, 'path') self.assertEqual(cmd.path_file, 'path') # none dist.extra_path = cmd.extra_path = None cmd.handle_extra_path() self.assertEqual(cmd.extra_path, None) self.assertEqual(cmd.extra_dirs, '') self.assertEqual(cmd.path_file, None) # three elements (no way !) cmd.extra_path = 'path,dirs,again' self.assertRaises(DistutilsOptionError, cmd.handle_extra_path) def test_finalize_options(self): dist = Distribution({'name': 'xx'}) cmd = install(dist) # must supply either prefix/exec-prefix/home or # install-base/install-platbase -- not both cmd.prefix = 'prefix' cmd.install_base = 'base' self.assertRaises(DistutilsOptionError, cmd.finalize_options) # must supply either home or prefix/exec-prefix -- not both cmd.install_base = None cmd.home = 'home' self.assertRaises(DistutilsOptionError, cmd.finalize_options) # can't combine user with prefix/exec_prefix/home or # install_(plat)base cmd.prefix = None cmd.user = 'user' self.assertRaises(DistutilsOptionError, cmd.finalize_options) def test_record(self): install_dir = self.mkdtemp() project_dir, dist = self.create_dist(py_modules=['hello'], scripts=['sayhi']) os.chdir(project_dir) self.write_file('hello.py', "def main(): print('o hai')") self.write_file('sayhi', 'from hello import main; main()') cmd = install(dist) dist.command_obj['install'] = cmd cmd.root = install_dir cmd.record = os.path.join(project_dir, 'filelist') cmd.ensure_finalized() cmd.run() f = open(cmd.record) try: content = f.read() finally: f.close() found = [os.path.basename(line) for line in content.splitlines()] expected = ['hello.py', 'hello.%s.pyc' % sys.implementation.cache_tag, 'sayhi', 'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]] self.assertEqual(found, expected) def test_record_extensions(self): cmd = test_support.missing_compiler_executable() if cmd is not None: self.skipTest('The %r command is not found' % cmd) install_dir = self.mkdtemp() project_dir, dist = self.create_dist(ext_modules=[ Extension('xx', ['xxmodule.c'])]) os.chdir(project_dir) support.copy_xxmodule_c(project_dir) buildextcmd = build_ext(dist) support.fixup_build_ext(buildextcmd) buildextcmd.ensure_finalized() cmd = install(dist) dist.command_obj['install'] = cmd dist.command_obj['build_ext'] = buildextcmd cmd.root = install_dir cmd.record = os.path.join(project_dir, 'filelist') cmd.ensure_finalized() cmd.run() f = open(cmd.record) try: content = f.read() finally: f.close() found = [os.path.basename(line) for line in content.splitlines()] expected = [_make_ext_name('xx'), 'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]] self.assertEqual(found, expected) def test_debug_mode(self): # this covers the code called when DEBUG is set old_logs_len = len(self.logs) install_module.DEBUG = True try: with captured_stdout(): self.test_record() finally: install_module.DEBUG = False self.assertGreater(len(self.logs), old_logs_len) def test_suite(): return unittest.makeSuite(InstallTestCase) if __name__ == "__main__": run_unittest(test_suite())
8,535
249
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_version.py
"""Tests for distutils.version.""" import unittest from distutils.version import LooseVersion from distutils.version import StrictVersion from test.support import run_unittest class VersionTestCase(unittest.TestCase): def test_prerelease(self): version = StrictVersion('1.2.3a1') self.assertEqual(version.version, (1, 2, 3)) self.assertEqual(version.prerelease, ('a', 1)) self.assertEqual(str(version), '1.2.3a1') version = StrictVersion('1.2.0') self.assertEqual(str(version), '1.2') def test_cmp_strict(self): versions = (('1.5.1', '1.5.2b2', -1), ('161', '3.10a', ValueError), ('8.02', '8.02', 0), ('3.4j', '1996.07.12', ValueError), ('3.2.pl0', '3.1.1.6', ValueError), ('2g6', '11g', ValueError), ('0.9', '2.2', -1), ('1.2.1', '1.2', 1), ('1.1', '1.2.2', -1), ('1.2', '1.1', 1), ('1.2.1', '1.2.2', -1), ('1.2.2', '1.2', 1), ('1.2', '1.2.2', -1), ('0.4.0', '0.4', 0), ('1.13++', '5.5.kw', ValueError)) for v1, v2, wanted in versions: try: res = StrictVersion(v1)._cmp(StrictVersion(v2)) except ValueError: if wanted is ValueError: continue else: raise AssertionError(("cmp(%s, %s) " "shouldn't raise ValueError") % (v1, v2)) self.assertEqual(res, wanted, 'cmp(%s, %s) should be %s, got %s' % (v1, v2, wanted, res)) def test_cmp(self): versions = (('1.5.1', '1.5.2b2', -1), ('161', '3.10a', 1), ('8.02', '8.02', 0), ('3.4j', '1996.07.12', -1), ('3.2.pl0', '3.1.1.6', 1), ('2g6', '11g', -1), ('0.960923', '2.2beta29', -1), ('1.13++', '5.5.kw', -1)) for v1, v2, wanted in versions: res = LooseVersion(v1)._cmp(LooseVersion(v2)) self.assertEqual(res, wanted, 'cmp(%s, %s) should be %s, got %s' % (v1, v2, wanted, res)) def test_suite(): return unittest.makeSuite(VersionTestCase) if __name__ == "__main__": run_unittest(test_suite())
2,614
72
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_install_scripts.py
"""Tests for distutils.command.install_scripts.""" import os import unittest from distutils.command.install_scripts import install_scripts from distutils.core import Distribution from distutils.tests import support from test.support import run_unittest class InstallScriptsTestCase(support.TempdirManager, support.LoggingSilencer, unittest.TestCase): def test_default_settings(self): dist = Distribution() dist.command_obj["build"] = support.DummyCommand( build_scripts="/foo/bar") dist.command_obj["install"] = support.DummyCommand( install_scripts="/splat/funk", force=1, skip_build=1, ) cmd = install_scripts(dist) self.assertFalse(cmd.force) self.assertFalse(cmd.skip_build) self.assertIsNone(cmd.build_dir) self.assertIsNone(cmd.install_dir) cmd.finalize_options() self.assertTrue(cmd.force) self.assertTrue(cmd.skip_build) self.assertEqual(cmd.build_dir, "/foo/bar") self.assertEqual(cmd.install_dir, "/splat/funk") def test_installation(self): source = self.mkdtemp() expected = [] def write_script(name, text): expected.append(name) f = open(os.path.join(source, name), "w") try: f.write(text) finally: f.close() write_script("script1.py", ("#! /usr/bin/env python2.3\n" "# bogus script w/ Python sh-bang\n" "pass\n")) write_script("script2.py", ("#!/usr/bin/python\n" "# bogus script w/ Python sh-bang\n" "pass\n")) write_script("shell.sh", ("#!/bin/sh\n" "# bogus shell script w/ sh-bang\n" "exit 0\n")) target = self.mkdtemp() dist = Distribution() dist.command_obj["build"] = support.DummyCommand(build_scripts=source) dist.command_obj["install"] = support.DummyCommand( install_scripts=target, force=1, skip_build=1, ) cmd = install_scripts(dist) cmd.finalize_options() cmd.run() installed = os.listdir(target) for name in expected: self.assertIn(name, installed) def test_suite(): return unittest.makeSuite(InstallScriptsTestCase) if __name__ == "__main__": run_unittest(test_suite())
2,625
83
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_msvccompiler.py
"""Tests for distutils._msvccompiler.""" import sys import unittest import os from distutils.errors import DistutilsPlatformError from distutils.tests import support from test.support import run_unittest SKIP_MESSAGE = (None if sys.platform == "win32" else "These tests are only for win32") @unittest.skipUnless(SKIP_MESSAGE is None, SKIP_MESSAGE) class msvccompilerTestCase(support.TempdirManager, unittest.TestCase): def test_no_compiler(self): import distutils._msvccompiler as _msvccompiler # makes sure query_vcvarsall raises # a DistutilsPlatformError if the compiler # is not found def _find_vcvarsall(plat_spec): return None, None old_find_vcvarsall = _msvccompiler._find_vcvarsall _msvccompiler._find_vcvarsall = _find_vcvarsall try: self.assertRaises(DistutilsPlatformError, _msvccompiler._get_vc_env, 'wont find this version') finally: _msvccompiler._find_vcvarsall = old_find_vcvarsall def test_compiler_options(self): import distutils._msvccompiler as _msvccompiler # suppress path to vcruntime from _find_vcvarsall to # check that /MT is added to compile options old_find_vcvarsall = _msvccompiler._find_vcvarsall def _find_vcvarsall(plat_spec): return old_find_vcvarsall(plat_spec)[0], None _msvccompiler._find_vcvarsall = _find_vcvarsall try: compiler = _msvccompiler.MSVCCompiler() compiler.initialize() self.assertIn('/MT', compiler.compile_options) self.assertNotIn('/MD', compiler.compile_options) finally: _msvccompiler._find_vcvarsall = old_find_vcvarsall def test_vcruntime_copy(self): import distutils._msvccompiler as _msvccompiler # force path to a known file - it doesn't matter # what we copy as long as its name is not in # _msvccompiler._BUNDLED_DLLS old_find_vcvarsall = _msvccompiler._find_vcvarsall def _find_vcvarsall(plat_spec): return old_find_vcvarsall(plat_spec)[0], __file__ _msvccompiler._find_vcvarsall = _find_vcvarsall try: tempdir = self.mkdtemp() compiler = _msvccompiler.MSVCCompiler() compiler.initialize() compiler._copy_vcruntime(tempdir) self.assertTrue(os.path.isfile(os.path.join( tempdir, os.path.basename(__file__)))) finally: _msvccompiler._find_vcvarsall = old_find_vcvarsall def test_vcruntime_skip_copy(self): import distutils._msvccompiler as _msvccompiler tempdir = self.mkdtemp() compiler = _msvccompiler.MSVCCompiler() compiler.initialize() dll = compiler._vcruntime_redist self.assertTrue(os.path.isfile(dll), dll or "<None>") compiler._copy_vcruntime(tempdir) self.assertFalse(os.path.isfile(os.path.join( tempdir, os.path.basename(dll))), dll or "<None>") def test_get_vc_env_unicode(self): import distutils._msvccompiler as _msvccompiler test_var = 'ṰḖṤṪ┅ṼẨṜ' test_value = '₃⁴₅' # Ensure we don't early exit from _get_vc_env old_distutils_use_sdk = os.environ.pop('DISTUTILS_USE_SDK', None) os.environ[test_var] = test_value try: env = _msvccompiler._get_vc_env('x86') self.assertIn(test_var.lower(), env) self.assertEqual(test_value, env[test_var.lower()]) finally: os.environ.pop(test_var) if old_distutils_use_sdk: os.environ['DISTUTILS_USE_SDK'] = old_distutils_use_sdk def test_get_vc2017(self): import distutils._msvccompiler as _msvccompiler # This function cannot be mocked, so pass it if we find VS 2017 # and mark it skipped if we do not. version, path = _msvccompiler._find_vc2017() if version: self.assertGreaterEqual(version, 15) self.assertTrue(os.path.isdir(path)) else: raise unittest.SkipTest("VS 2017 is not installed") def test_get_vc2015(self): import distutils._msvccompiler as _msvccompiler # This function cannot be mocked, so pass it if we find VS 2015 # and mark it skipped if we do not. version, path = _msvccompiler._find_vc2015() if version: self.assertGreaterEqual(version, 14) self.assertTrue(os.path.isdir(path)) else: raise unittest.SkipTest("VS 2015 is not installed") def test_suite(): return unittest.makeSuite(msvccompilerTestCase) if __name__ == "__main__": run_unittest(test_suite())
4,872
133
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_spawn.py
"""Tests for distutils.spawn.""" import os import stat import sys import unittest from unittest import mock from test.support import run_unittest, unix_shell from test import support as test_support from distutils.spawn import find_executable from distutils.spawn import _nt_quote_args from distutils.spawn import spawn from distutils.errors import DistutilsExecError from distutils.tests import support class SpawnTestCase(support.TempdirManager, support.LoggingSilencer, unittest.TestCase): def test_nt_quote_args(self): for (args, wanted) in ((['with space', 'nospace'], ['"with space"', 'nospace']), (['nochange', 'nospace'], ['nochange', 'nospace'])): res = _nt_quote_args(args) self.assertEqual(res, wanted) @unittest.skipUnless(os.name in ('nt', 'posix'), 'Runs only under posix or nt') def test_spawn(self): tmpdir = self.mkdtemp() # creating something executable # through the shell that returns 1 if sys.platform != 'win32': exe = os.path.join(tmpdir, 'foo.sh') self.write_file(exe, '#!%s\nexit 1' % unix_shell) else: exe = os.path.join(tmpdir, 'foo.bat') self.write_file(exe, 'exit 1') os.chmod(exe, 0o777) self.assertRaises(DistutilsExecError, spawn, [exe]) # now something that works if sys.platform != 'win32': exe = os.path.join(tmpdir, 'foo.sh') self.write_file(exe, '#!%s\nexit 0' % unix_shell) else: exe = os.path.join(tmpdir, 'foo.bat') self.write_file(exe, 'exit 0') os.chmod(exe, 0o777) spawn([exe]) # should work without any error def test_find_executable(self): with test_support.temp_dir() as tmp_dir: # use TESTFN to get a pseudo-unique filename program_noeext = test_support.TESTFN # Give the temporary program an ".exe" suffix for all. # It's needed on Windows and not harmful on other platforms. program = program_noeext + ".exe" filename = os.path.join(tmp_dir, program) with open(filename, "wb"): pass os.chmod(filename, stat.S_IXUSR) # test path parameter rv = find_executable(program, path=tmp_dir) self.assertEqual(rv, filename) if sys.platform == 'win32': # test without ".exe" extension rv = find_executable(program_noeext, path=tmp_dir) self.assertEqual(rv, filename) # test find in the current directory with test_support.change_cwd(tmp_dir): rv = find_executable(program) self.assertEqual(rv, program) # test non-existent program dont_exist_program = "dontexist_" + program rv = find_executable(dont_exist_program , path=tmp_dir) self.assertIsNone(rv) # test os.defpath: missing PATH environment variable with test_support.EnvironmentVarGuard() as env: with mock.patch('distutils.spawn.os.defpath', tmp_dir): env.pop('PATH') rv = find_executable(program) self.assertEqual(rv, filename) def test_suite(): return unittest.makeSuite(SpawnTestCase) if __name__ == "__main__": run_unittest(test_suite())
3,587
104
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_bdist_dumb.py
"""Tests for distutils.command.bdist_dumb.""" import os import sys import zipfile import unittest from test.support import run_unittest from distutils.core import Distribution from distutils.command.bdist_dumb import bdist_dumb from distutils.tests import support SETUP_PY = """\ from distutils.core import setup import foo setup(name='foo', version='0.1', py_modules=['foo'], url='xxx', author='xxx', author_email='xxx') """ try: import zlib ZLIB_SUPPORT = True except ImportError: ZLIB_SUPPORT = False class BuildDumbTestCase(support.TempdirManager, support.LoggingSilencer, support.EnvironGuard, unittest.TestCase): def setUp(self): super(BuildDumbTestCase, self).setUp() self.old_location = os.getcwd() self.old_sys_argv = sys.argv, sys.argv[:] def tearDown(self): os.chdir(self.old_location) sys.argv = self.old_sys_argv[0] sys.argv[:] = self.old_sys_argv[1] super(BuildDumbTestCase, self).tearDown() @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') def test_simple_built(self): # let's create a simple package tmp_dir = self.mkdtemp() pkg_dir = os.path.join(tmp_dir, 'foo') os.mkdir(pkg_dir) self.write_file((pkg_dir, 'setup.py'), SETUP_PY) self.write_file((pkg_dir, 'foo.py'), '#') self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py') self.write_file((pkg_dir, 'README'), '') dist = Distribution({'name': 'foo', 'version': '0.1', 'py_modules': ['foo'], 'url': 'xxx', 'author': 'xxx', 'author_email': 'xxx'}) dist.script_name = 'setup.py' os.chdir(pkg_dir) sys.argv = ['setup.py'] cmd = bdist_dumb(dist) # so the output is the same no matter # what is the platform cmd.format = 'zip' cmd.ensure_finalized() cmd.run() # see what we have dist_created = os.listdir(os.path.join(pkg_dir, 'dist')) base = "%s.%s.zip" % (dist.get_fullname(), cmd.plat_name) self.assertEqual(dist_created, [base]) # now let's check what we have in the zip file fp = zipfile.ZipFile(os.path.join('dist', base)) try: contents = fp.namelist() finally: fp.close() contents = sorted(filter(None, map(os.path.basename, contents))) wanted = ['foo-0.1-py%s.%s.egg-info' % sys.version_info[:2], 'foo.py'] if not sys.dont_write_bytecode: wanted.append('foo.%s.pyc' % sys.implementation.cache_tag) self.assertEqual(contents, sorted(wanted)) def test_suite(): return unittest.makeSuite(BuildDumbTestCase) if __name__ == '__main__': run_unittest(test_suite())
2,905
98
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_build.py
"""Tests for distutils.command.build.""" import unittest import os import sys from test.support import run_unittest from distutils.command.build import build from distutils.tests import support from sysconfig import get_platform class BuildTestCase(support.TempdirManager, support.LoggingSilencer, unittest.TestCase): def test_finalize_options(self): pkg_dir, dist = self.create_dist() cmd = build(dist) cmd.finalize_options() # if not specified, plat_name gets the current platform self.assertEqual(cmd.plat_name, get_platform()) # build_purelib is build + lib wanted = os.path.join(cmd.build_base, 'lib') self.assertEqual(cmd.build_purelib, wanted) # build_platlib is 'build/lib.platform-x.x[-pydebug]' # examples: # build/lib.macosx-10.3-i386-2.7 plat_spec = '.%s-%d.%d' % (cmd.plat_name, *sys.version_info[:2]) if hasattr(sys, 'gettotalrefcount'): self.assertTrue(cmd.build_platlib.endswith('-pydebug')) plat_spec += '-pydebug' wanted = os.path.join(cmd.build_base, 'lib' + plat_spec) self.assertEqual(cmd.build_platlib, wanted) # by default, build_lib = build_purelib self.assertEqual(cmd.build_lib, cmd.build_purelib) # build_temp is build/temp.<plat> wanted = os.path.join(cmd.build_base, 'temp' + plat_spec) self.assertEqual(cmd.build_temp, wanted) # build_scripts is build/scripts-x.x wanted = os.path.join(cmd.build_base, 'scripts-%d.%d' % sys.version_info[:2]) self.assertEqual(cmd.build_scripts, wanted) # executable is os.path.normpath(sys.executable) self.assertEqual(cmd.executable, os.path.normpath(sys.executable)) def test_suite(): return unittest.makeSuite(BuildTestCase) if __name__ == "__main__": run_unittest(test_suite())
1,965
57
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/__init__.py
"""Test suite for distutils. This test suite consists of a collection of test modules in the distutils.tests package. Each test module has a name starting with 'test' and contains a function test_suite(). The function is expected to return an initialized unittest.TestSuite instance. Tests for the command classes in the distutils.command package are included in distutils.tests as well, instead of using a separate distutils.command.tests package, since command identification is done by import rather than matching pre-defined names. """ import os import sys import unittest from test.support import run_unittest here = os.path.dirname(__file__) or os.curdir def test_suite(): suite = unittest.TestSuite() for fn in os.listdir(here): if fn.startswith("test") and fn.endswith(".py"): modname = "distutils.tests." + fn[:-3] __import__(modname) module = sys.modules[modname] suite.addTest(module.test_suite()) return suite if __name__ == "__main__": run_unittest(test_suite())
1,060
37
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_install_headers.py
"""Tests for distutils.command.install_headers.""" import os import unittest from distutils.command.install_headers import install_headers from distutils.tests import support from test.support import run_unittest class InstallHeadersTestCase(support.TempdirManager, support.LoggingSilencer, support.EnvironGuard, unittest.TestCase): def test_simple_run(self): # we have two headers header_list = self.mkdtemp() header1 = os.path.join(header_list, 'header1') header2 = os.path.join(header_list, 'header2') self.write_file(header1) self.write_file(header2) headers = [header1, header2] pkg_dir, dist = self.create_dist(headers=headers) cmd = install_headers(dist) self.assertEqual(cmd.get_inputs(), headers) # let's run the command cmd.install_dir = os.path.join(pkg_dir, 'inst') cmd.ensure_finalized() cmd.run() # let's check the results self.assertEqual(len(cmd.get_outputs()), 2) def test_suite(): return unittest.makeSuite(InstallHeadersTestCase) if __name__ == "__main__": run_unittest(test_suite())
1,238
40
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_install_data.py
"""Tests for distutils.command.install_data.""" import os import unittest from distutils.command.install_data import install_data from distutils.tests import support from test.support import run_unittest class InstallDataTestCase(support.TempdirManager, support.LoggingSilencer, support.EnvironGuard, unittest.TestCase): def test_simple_run(self): pkg_dir, dist = self.create_dist() cmd = install_data(dist) cmd.install_dir = inst = os.path.join(pkg_dir, 'inst') # data_files can contain # - simple files # - a tuple with a path, and a list of file one = os.path.join(pkg_dir, 'one') self.write_file(one, 'xxx') inst2 = os.path.join(pkg_dir, 'inst2') two = os.path.join(pkg_dir, 'two') self.write_file(two, 'xxx') cmd.data_files = [one, (inst2, [two])] self.assertEqual(cmd.get_inputs(), [one, (inst2, [two])]) # let's run the command cmd.ensure_finalized() cmd.run() # let's check the result self.assertEqual(len(cmd.get_outputs()), 2) rtwo = os.path.split(two)[-1] self.assertTrue(os.path.exists(os.path.join(inst2, rtwo))) rone = os.path.split(one)[-1] self.assertTrue(os.path.exists(os.path.join(inst, rone))) cmd.outfiles = [] # let's try with warn_dir one cmd.warn_dir = 1 cmd.ensure_finalized() cmd.run() # let's check the result self.assertEqual(len(cmd.get_outputs()), 2) self.assertTrue(os.path.exists(os.path.join(inst2, rtwo))) self.assertTrue(os.path.exists(os.path.join(inst, rone))) cmd.outfiles = [] # now using root and empty dir cmd.root = os.path.join(pkg_dir, 'root') inst3 = os.path.join(cmd.install_dir, 'inst3') inst4 = os.path.join(pkg_dir, 'inst4') three = os.path.join(cmd.install_dir, 'three') self.write_file(three, 'xx') cmd.data_files = [one, (inst2, [two]), ('inst3', [three]), (inst4, [])] cmd.ensure_finalized() cmd.run() # let's check the result self.assertEqual(len(cmd.get_outputs()), 4) self.assertTrue(os.path.exists(os.path.join(inst2, rtwo))) self.assertTrue(os.path.exists(os.path.join(inst, rone))) def test_suite(): return unittest.makeSuite(InstallDataTestCase) if __name__ == "__main__": run_unittest(test_suite())
2,577
76
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/distutils/tests/test_build_clib.py
"""Tests for distutils.command.build_clib.""" import unittest import os import sys from test.support import run_unittest, missing_compiler_executable from distutils.command.build_clib import build_clib from distutils.errors import DistutilsSetupError from distutils.tests import support from distutils.spawn import find_executable class BuildCLibTestCase(support.TempdirManager, support.LoggingSilencer, unittest.TestCase): def test_check_library_dist(self): pkg_dir, dist = self.create_dist() cmd = build_clib(dist) # 'libraries' option must be a list self.assertRaises(DistutilsSetupError, cmd.check_library_list, 'foo') # each element of 'libraries' must a 2-tuple self.assertRaises(DistutilsSetupError, cmd.check_library_list, ['foo1', 'foo2']) # first element of each tuple in 'libraries' # must be a string (the library name) self.assertRaises(DistutilsSetupError, cmd.check_library_list, [(1, 'foo1'), ('name', 'foo2')]) # library name may not contain directory separators self.assertRaises(DistutilsSetupError, cmd.check_library_list, [('name', 'foo1'), ('another/name', 'foo2')]) # second element of each tuple must be a dictionary (build info) self.assertRaises(DistutilsSetupError, cmd.check_library_list, [('name', {}), ('another', 'foo2')]) # those work libs = [('name', {}), ('name', {'ok': 'good'})] cmd.check_library_list(libs) def test_get_source_files(self): pkg_dir, dist = self.create_dist() cmd = build_clib(dist) # "in 'libraries' option 'sources' must be present and must be # a list of source filenames cmd.libraries = [('name', {})] self.assertRaises(DistutilsSetupError, cmd.get_source_files) cmd.libraries = [('name', {'sources': 1})] self.assertRaises(DistutilsSetupError, cmd.get_source_files) cmd.libraries = [('name', {'sources': ['a', 'b']})] self.assertEqual(cmd.get_source_files(), ['a', 'b']) cmd.libraries = [('name', {'sources': ('a', 'b')})] self.assertEqual(cmd.get_source_files(), ['a', 'b']) cmd.libraries = [('name', {'sources': ('a', 'b')}), ('name2', {'sources': ['c', 'd']})] self.assertEqual(cmd.get_source_files(), ['a', 'b', 'c', 'd']) def test_build_libraries(self): pkg_dir, dist = self.create_dist() cmd = build_clib(dist) class FakeCompiler: def compile(*args, **kw): pass create_static_lib = compile cmd.compiler = FakeCompiler() # build_libraries is also doing a bit of typo checking lib = [('name', {'sources': 'notvalid'})] self.assertRaises(DistutilsSetupError, cmd.build_libraries, lib) lib = [('name', {'sources': list()})] cmd.build_libraries(lib) lib = [('name', {'sources': tuple()})] cmd.build_libraries(lib) def test_finalize_options(self): pkg_dir, dist = self.create_dist() cmd = build_clib(dist) cmd.include_dirs = 'one-dir' cmd.finalize_options() self.assertEqual(cmd.include_dirs, ['one-dir']) cmd.include_dirs = None cmd.finalize_options() self.assertEqual(cmd.include_dirs, []) cmd.distribution.libraries = 'WONTWORK' self.assertRaises(DistutilsSetupError, cmd.finalize_options) @unittest.skipIf(sys.platform == 'win32', "can't test on Windows") def test_run(self): pkg_dir, dist = self.create_dist() cmd = build_clib(dist) foo_c = os.path.join(pkg_dir, 'foo.c') self.write_file(foo_c, 'int main(void) { return 1;}\n') cmd.libraries = [('foo', {'sources': [foo_c]})] build_temp = os.path.join(pkg_dir, 'build') os.mkdir(build_temp) cmd.build_temp = build_temp cmd.build_clib = build_temp # Before we run the command, we want to make sure # all commands are present on the system. ccmd = missing_compiler_executable() if ccmd is not None: self.skipTest('The %r command is not found' % ccmd) # this should work cmd.run() # let's check the result self.assertIn('libfoo.a', os.listdir(build_temp)) def test_suite(): return unittest.makeSuite(BuildCLibTestCase) if __name__ == "__main__": run_unittest(test_suite())
4,675
136
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/wsgiref/headers.py
"""Manage HTTP Response Headers Much of this module is red-handedly pilfered from email.message in the stdlib, so portions are Copyright (C) 2001,2002 Python Software Foundation, and were written by Barry Warsaw. """ # Regular expression that matches `special' characters in parameters, the # existence of which force quoting of the parameter value. import re tspecials = re.compile(r'[ \(\)<>@,;:\\"/\[\]\?=]') def _formatparam(param, value=None, quote=1): """Convenience function to format and return a key=value pair. This will quote the value if needed or if quote is true. """ if value is not None and len(value) > 0: if quote or tspecials.search(value): value = value.replace('\\', '\\\\').replace('"', r'\"') return '%s="%s"' % (param, value) else: return '%s=%s' % (param, value) else: return param class Headers: """Manage a collection of HTTP response headers""" def __init__(self, headers=None): headers = headers if headers is not None else [] if type(headers) is not list: raise TypeError("Headers must be a list of name/value tuples") self._headers = headers if __debug__: for k, v in headers: self._convert_string_type(k) self._convert_string_type(v) def _convert_string_type(self, value): """Convert/check value type.""" if type(value) is str: return value raise AssertionError("Header names/values must be" " of type str (got {0})".format(repr(value))) def __len__(self): """Return the total number of headers, including duplicates.""" return len(self._headers) def __setitem__(self, name, val): """Set the value of a header.""" del self[name] self._headers.append( (self._convert_string_type(name), self._convert_string_type(val))) def __delitem__(self,name): """Delete all occurrences of a header, if present. Does *not* raise an exception if the header is missing. """ name = self._convert_string_type(name.lower()) self._headers[:] = [kv for kv in self._headers if kv[0].lower() != name] def __getitem__(self,name): """Get the first header value for 'name' Return None if the header is missing instead of raising an exception. Note that if the header appeared multiple times, the first exactly which occurrence gets returned is undefined. Use getall() to get all the values matching a header field name. """ return self.get(name) def __contains__(self, name): """Return true if the message contains the header.""" return self.get(name) is not None def get_all(self, name): """Return a list of all the values for the named field. These will be sorted in the order they appeared in the original header list or were added to this instance, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list. If no fields exist with the given name, returns an empty list. """ name = self._convert_string_type(name.lower()) return [kv[1] for kv in self._headers if kv[0].lower()==name] def get(self,name,default=None): """Get the first header value for 'name', or return 'default'""" name = self._convert_string_type(name.lower()) for k,v in self._headers: if k.lower()==name: return v return default def keys(self): """Return a list of all the header field names. These will be sorted in the order they appeared in the original header list, or were added to this instance, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list. """ return [k for k, v in self._headers] def values(self): """Return a list of all header values. These will be sorted in the order they appeared in the original header list, or were added to this instance, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list. """ return [v for k, v in self._headers] def items(self): """Get all the header fields and values. These will be sorted in the order they were in the original header list, or were added to this instance, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list. """ return self._headers[:] def __repr__(self): return "%s(%r)" % (self.__class__.__name__, self._headers) def __str__(self): """str() returns the formatted headers, complete with end line, suitable for direct HTTP transmission.""" return '\r\n'.join(["%s: %s" % kv for kv in self._headers]+['','']) def __bytes__(self): return str(self).encode('iso-8859-1') def setdefault(self,name,value): """Return first matching header value for 'name', or 'value' If there is no header named 'name', add a new header with name 'name' and value 'value'.""" result = self.get(name) if result is None: self._headers.append((self._convert_string_type(name), self._convert_string_type(value))) return value else: return result def add_header(self, _name, _value, **_params): """Extended header setting. _name is the header field to add. keyword arguments can be used to set additional parameters for the header field, with underscores converted to dashes. Normally the parameter will be added as key="value" unless value is None, in which case only the key will be added. Example: h.add_header('content-disposition', 'attachment', filename='bud.gif') Note that unlike the corresponding 'email.message' method, this does *not* handle '(charset, language, value)' tuples: all values must be strings or None. """ parts = [] if _value is not None: _value = self._convert_string_type(_value) parts.append(_value) for k, v in _params.items(): k = self._convert_string_type(k) if v is None: parts.append(k.replace('_', '-')) else: v = self._convert_string_type(v) parts.append(_formatparam(k.replace('_', '-'), v)) self._headers.append((self._convert_string_type(_name), "; ".join(parts)))
6,766
185
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/wsgiref/simple_server.py
"""BaseHTTPServer that implements the Python WSGI protocol (PEP 3333) This is both an example of how WSGI can be implemented, and a basis for running simple web applications on a local machine, such as might be done when testing or debugging an application. It has not been reviewed for security issues, however, and we strongly recommend that you use a "real" web server for production use. For example usage, see the 'if __name__=="__main__"' block at the end of the module. See also the BaseHTTPServer module docs for other API information. """ from http.server import BaseHTTPRequestHandler, HTTPServer import sys import urllib.parse from wsgiref.handlers import SimpleHandler from platform import python_implementation __version__ = "0.2" __all__ = ['WSGIServer', 'WSGIRequestHandler', 'demo_app', 'make_server'] server_version = "WSGIServer/" + __version__ sys_version = python_implementation() + "/" + sys.version.split()[0] software_version = server_version + ' ' + sys_version class ServerHandler(SimpleHandler): server_software = software_version def close(self): try: self.request_handler.log_request( self.status.split(' ',1)[0], self.bytes_sent ) finally: SimpleHandler.close(self) class WSGIServer(HTTPServer): """BaseHTTPServer that implements the Python WSGI protocol""" application = None def server_bind(self): """Override server_bind to store the server name.""" HTTPServer.server_bind(self) self.setup_environ() def setup_environ(self): # Set up base environment env = self.base_environ = {} env['SERVER_NAME'] = self.server_name env['GATEWAY_INTERFACE'] = 'CGI/1.1' env['SERVER_PORT'] = str(self.server_port) env['REMOTE_HOST']='' env['CONTENT_LENGTH']='' env['SCRIPT_NAME'] = '' def get_app(self): return self.application def set_app(self,application): self.application = application class WSGIRequestHandler(BaseHTTPRequestHandler): server_version = "WSGIServer/" + __version__ def get_environ(self): env = self.server.base_environ.copy() env['SERVER_PROTOCOL'] = self.request_version env['SERVER_SOFTWARE'] = self.server_version env['REQUEST_METHOD'] = self.command if '?' in self.path: path,query = self.path.split('?',1) else: path,query = self.path,'' env['PATH_INFO'] = urllib.parse.unquote(path, 'iso-8859-1') env['QUERY_STRING'] = query host = self.address_string() if host != self.client_address[0]: env['REMOTE_HOST'] = host env['REMOTE_ADDR'] = self.client_address[0] if self.headers.get('content-type') is None: env['CONTENT_TYPE'] = self.headers.get_content_type() else: env['CONTENT_TYPE'] = self.headers['content-type'] length = self.headers.get('content-length') if length: env['CONTENT_LENGTH'] = length for k, v in self.headers.items(): k=k.replace('-','_').upper(); v=v.strip() if k in env: continue # skip content length, type,etc. if 'HTTP_'+k in env: env['HTTP_'+k] += ','+v # comma-separate multiple headers else: env['HTTP_'+k] = v return env def get_stderr(self): return sys.stderr def handle(self): """Handle a single HTTP request""" self.raw_requestline = self.rfile.readline(65537) if len(self.raw_requestline) > 65536: self.requestline = '' self.request_version = '' self.command = '' self.send_error(414) return if not self.parse_request(): # An error code has been sent, just exit return handler = ServerHandler( self.rfile, self.wfile, self.get_stderr(), self.get_environ() ) handler.request_handler = self # backpointer for logging handler.run(self.server.get_app()) def demo_app(environ,start_response): from io import StringIO stdout = StringIO() print("Hello world!", file=stdout) print(file=stdout) h = sorted(environ.items()) for k,v in h: print(k,'=',repr(v), file=stdout) start_response("200 OK", [('Content-Type','text/plain; charset=utf-8')]) return [stdout.getvalue().encode("utf-8")] def make_server( host, port, app, server_class=WSGIServer, handler_class=WSGIRequestHandler ): """Create a new WSGI server listening on `host` and `port` for `app`""" server = server_class((host, port), handler_class) server.set_app(app) return server if __name__ == '__main__': with make_server('', 8000, demo_app) as httpd: sa = httpd.socket.getsockname() print("Serving HTTP on", sa[0], "port", sa[1], "...") import webbrowser webbrowser.open('http://localhost:8000/xyz?abc') httpd.handle_request() # serve one request, then exit
5,139
165
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/wsgiref/util.py
"""Miscellaneous WSGI-related Utilities""" import posixpath __all__ = [ 'FileWrapper', 'guess_scheme', 'application_uri', 'request_uri', 'shift_path_info', 'setup_testing_defaults', ] class FileWrapper: """Wrapper to convert file-like objects to iterables""" def __init__(self, filelike, blksize=8192): self.filelike = filelike self.blksize = blksize if hasattr(filelike,'close'): self.close = filelike.close def __getitem__(self,key): data = self.filelike.read(self.blksize) if data: return data raise IndexError def __iter__(self): return self def __next__(self): data = self.filelike.read(self.blksize) if data: return data raise StopIteration def guess_scheme(environ): """Return a guess for whether 'wsgi.url_scheme' should be 'http' or 'https' """ if environ.get("HTTPS") in ('yes','on','1'): return 'https' else: return 'http' def application_uri(environ): """Return the application's base URI (no PATH_INFO or QUERY_STRING)""" url = environ['wsgi.url_scheme']+'://' from urllib.parse import quote if environ.get('HTTP_HOST'): url += environ['HTTP_HOST'] else: url += environ['SERVER_NAME'] if environ['wsgi.url_scheme'] == 'https': if environ['SERVER_PORT'] != '443': url += ':' + environ['SERVER_PORT'] else: if environ['SERVER_PORT'] != '80': url += ':' + environ['SERVER_PORT'] url += quote(environ.get('SCRIPT_NAME') or '/', encoding='latin1') return url def request_uri(environ, include_query=True): """Return the full request URI, optionally including the query string""" url = application_uri(environ) from urllib.parse import quote path_info = quote(environ.get('PATH_INFO',''), safe='/;=,', encoding='latin1') if not environ.get('SCRIPT_NAME'): url += path_info[1:] else: url += path_info if include_query and environ.get('QUERY_STRING'): url += '?' + environ['QUERY_STRING'] return url def shift_path_info(environ): """Shift a name from PATH_INFO to SCRIPT_NAME, returning it If there are no remaining path segments in PATH_INFO, return None. Note: 'environ' is modified in-place; use a copy if you need to keep the original PATH_INFO or SCRIPT_NAME. Note: when PATH_INFO is just a '/', this returns '' and appends a trailing '/' to SCRIPT_NAME, even though empty path segments are normally ignored, and SCRIPT_NAME doesn't normally end in a '/'. This is intentional behavior, to ensure that an application can tell the difference between '/x' and '/x/' when traversing to objects. """ path_info = environ.get('PATH_INFO','') if not path_info: return None path_parts = path_info.split('/') path_parts[1:-1] = [p for p in path_parts[1:-1] if p and p != '.'] name = path_parts[1] del path_parts[1] script_name = environ.get('SCRIPT_NAME','') script_name = posixpath.normpath(script_name+'/'+name) if script_name.endswith('/'): script_name = script_name[:-1] if not name and not script_name.endswith('/'): script_name += '/' environ['SCRIPT_NAME'] = script_name environ['PATH_INFO'] = '/'.join(path_parts) # Special case: '/.' on PATH_INFO doesn't get stripped, # because we don't strip the last element of PATH_INFO # if there's only one path part left. Instead of fixing this # above, we fix it here so that PATH_INFO gets normalized to # an empty string in the environ. if name=='.': name = None return name def setup_testing_defaults(environ): """Update 'environ' with trivial defaults for testing purposes This adds various parameters required for WSGI, including HTTP_HOST, SERVER_NAME, SERVER_PORT, REQUEST_METHOD, SCRIPT_NAME, PATH_INFO, and all of the wsgi.* variables. It only supplies default values, and does not replace any existing settings for these variables. This routine is intended to make it easier for unit tests of WSGI servers and applications to set up dummy environments. It should *not* be used by actual WSGI servers or applications, since the data is fake! """ environ.setdefault('SERVER_NAME','127.0.0.1') environ.setdefault('SERVER_PROTOCOL','HTTP/1.0') environ.setdefault('HTTP_HOST',environ['SERVER_NAME']) environ.setdefault('REQUEST_METHOD','GET') if 'SCRIPT_NAME' not in environ and 'PATH_INFO' not in environ: environ.setdefault('SCRIPT_NAME','') environ.setdefault('PATH_INFO','/') environ.setdefault('wsgi.version', (1,0)) environ.setdefault('wsgi.run_once', 0) environ.setdefault('wsgi.multithread', 0) environ.setdefault('wsgi.multiprocess', 0) from io import StringIO, BytesIO environ.setdefault('wsgi.input', BytesIO()) environ.setdefault('wsgi.errors', StringIO()) environ.setdefault('wsgi.url_scheme',guess_scheme(environ)) if environ['wsgi.url_scheme']=='http': environ.setdefault('SERVER_PORT', '80') elif environ['wsgi.url_scheme']=='https': environ.setdefault('SERVER_PORT', '443') _hoppish = { 'connection':1, 'keep-alive':1, 'proxy-authenticate':1, 'proxy-authorization':1, 'te':1, 'trailers':1, 'transfer-encoding':1, 'upgrade':1 }.__contains__ def is_hop_by_hop(header_name): """Return true if 'header_name' is an HTTP/1.1 "Hop-by-Hop" header""" return _hoppish(header_name.lower())
5,634
166
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/wsgiref/handlers.py
"""Base classes for server/gateway implementations""" from .util import FileWrapper, guess_scheme, is_hop_by_hop from .headers import Headers import sys, os, time __all__ = [ 'BaseHandler', 'SimpleHandler', 'BaseCGIHandler', 'CGIHandler', 'IISCGIHandler', 'read_environ' ] # Weekday and month names for HTTP date/time formatting; always English! _weekdayname = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] _monthname = [None, # Dummy so we can use 1-based month numbers "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] def format_date_time(timestamp): year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp) return "%s, %02d %3s %4d %02d:%02d:%02d GMT" % ( _weekdayname[wd], day, _monthname[month], year, hh, mm, ss ) _is_request = { 'SCRIPT_NAME', 'PATH_INFO', 'QUERY_STRING', 'REQUEST_METHOD', 'AUTH_TYPE', 'CONTENT_TYPE', 'CONTENT_LENGTH', 'HTTPS', 'REMOTE_USER', 'REMOTE_IDENT', }.__contains__ def _needs_transcode(k): return _is_request(k) or k.startswith('HTTP_') or k.startswith('SSL_') \ or (k.startswith('REDIRECT_') and _needs_transcode(k[9:])) def read_environ(): """Read environment, fixing HTTP variables""" enc = sys.getfilesystemencoding() esc = 'surrogateescape' try: ''.encode('utf-8', esc) except LookupError: esc = 'replace' environ = {} # Take the basic environment from native-unicode os.environ. Attempt to # fix up the variables that come from the HTTP request to compensate for # the bytes->unicode decoding step that will already have taken place. for k, v in os.environ.items(): if _needs_transcode(k): # On win32, the os.environ is natively Unicode. Different servers # decode the request bytes using different encodings. if sys.platform == 'win32': software = os.environ.get('SERVER_SOFTWARE', '').lower() # On IIS, the HTTP request will be decoded as UTF-8 as long # as the input is a valid UTF-8 sequence. Otherwise it is # decoded using the system code page (mbcs), with no way to # detect this has happened. Because UTF-8 is the more likely # encoding, and mbcs is inherently unreliable (an mbcs string # that happens to be valid UTF-8 will not be decoded as mbcs) # always recreate the original bytes as UTF-8. if software.startswith('microsoft-iis/'): v = v.encode('utf-8').decode('iso-8859-1') # Apache mod_cgi writes bytes-as-unicode (as if ISO-8859-1) direct # to the Unicode environ. No modification needed. elif software.startswith('apache/'): pass # Python 3's http.server.CGIHTTPRequestHandler decodes # using the urllib.unquote default of UTF-8, amongst other # issues. elif ( software.startswith('simplehttp/') and 'python/3' in software ): v = v.encode('utf-8').decode('iso-8859-1') # For other servers, guess that they have written bytes to # the environ using stdio byte-oriented interfaces, ending up # with the system code page. else: v = v.encode(enc, 'replace').decode('iso-8859-1') # Recover bytes from unicode environ, using surrogate escapes # where available (Python 3.1+). else: v = v.encode(enc, esc).decode('iso-8859-1') environ[k] = v return environ class BaseHandler: """Manage the invocation of a WSGI application""" # Configuration parameters; can override per-subclass or per-instance wsgi_version = (1,0) wsgi_multithread = True wsgi_multiprocess = True wsgi_run_once = False origin_server = True # We are transmitting direct to client http_version = "1.0" # Version that should be used for response server_software = None # String name of server software, if any # os_environ is used to supply configuration from the OS environment: # by default it's a copy of 'os.environ' as of import time, but you can # override this in e.g. your __init__ method. os_environ= read_environ() # Collaborator classes wsgi_file_wrapper = FileWrapper # set to None to disable headers_class = Headers # must be a Headers-like class # Error handling (also per-subclass or per-instance) traceback_limit = None # Print entire traceback to self.get_stderr() error_status = "500 Internal Server Error" error_headers = [('Content-Type','text/plain')] error_body = b"A server error occurred. Please contact the administrator." # State variables (don't mess with these) status = result = None headers_sent = False headers = None bytes_sent = 0 def run(self, application): """Invoke the application""" # Note to self: don't move the close()! Asynchronous servers shouldn't # call close() from finish_response(), so if you close() anywhere but # the double-error branch here, you'll break asynchronous servers by # prematurely closing. Async servers must return from 'run()' without # closing if there might still be output to iterate over. try: self.setup_environ() self.result = application(self.environ, self.start_response) self.finish_response() except: try: self.handle_error() except: # If we get an error handling an error, just give up already! self.close() raise # ...and let the actual server figure it out. def setup_environ(self): """Set up the environment for one request""" env = self.environ = self.os_environ.copy() self.add_cgi_vars() env['wsgi.input'] = self.get_stdin() env['wsgi.errors'] = self.get_stderr() env['wsgi.version'] = self.wsgi_version env['wsgi.run_once'] = self.wsgi_run_once env['wsgi.url_scheme'] = self.get_scheme() env['wsgi.multithread'] = self.wsgi_multithread env['wsgi.multiprocess'] = self.wsgi_multiprocess if self.wsgi_file_wrapper is not None: env['wsgi.file_wrapper'] = self.wsgi_file_wrapper if self.origin_server and self.server_software: env.setdefault('SERVER_SOFTWARE',self.server_software) def finish_response(self): """Send any iterable data, then close self and the iterable Subclasses intended for use in asynchronous servers will want to redefine this method, such that it sets up callbacks in the event loop to iterate over the data, and to call 'self.close()' once the response is finished. """ try: if not self.result_is_file() or not self.sendfile(): for data in self.result: self.write(data) self.finish_content() finally: self.close() def get_scheme(self): """Return the URL scheme being used""" return guess_scheme(self.environ) def set_content_length(self): """Compute Content-Length or switch to chunked encoding if possible""" try: blocks = len(self.result) except (TypeError,AttributeError,NotImplementedError): pass else: if blocks==1: self.headers['Content-Length'] = str(self.bytes_sent) return # XXX Try for chunked encoding if origin server and client is 1.1 def cleanup_headers(self): """Make any necessary header changes or defaults Subclasses can extend this to add other defaults. """ if 'Content-Length' not in self.headers: self.set_content_length() def start_response(self, status, headers,exc_info=None): """'start_response()' callable as specified by PEP 3333""" if exc_info: try: if self.headers_sent: # Re-raise original exception if headers sent raise exc_info[0](exc_info[1]).with_traceback(exc_info[2]) finally: exc_info = None # avoid dangling circular ref elif self.headers is not None: raise AssertionError("Headers already set!") self.status = status self.headers = self.headers_class(headers) status = self._convert_string_type(status, "Status") assert len(status)>=4,"Status must be at least 4 characters" assert status[:3].isdigit(), "Status message must begin w/3-digit code" assert status[3]==" ", "Status message must have a space after code" if __debug__: for name, val in headers: name = self._convert_string_type(name, "Header name") val = self._convert_string_type(val, "Header value") assert not is_hop_by_hop(name),"Hop-by-hop headers not allowed" return self.write def _convert_string_type(self, value, title): """Convert/check value type.""" if type(value) is str: return value raise AssertionError( "{0} must be of type str (got {1})".format(title, repr(value)) ) def send_preamble(self): """Transmit version/status/date/server, via self._write()""" if self.origin_server: if self.client_is_modern(): self._write(('HTTP/%s %s\r\n' % (self.http_version,self.status)).encode('iso-8859-1')) if 'Date' not in self.headers: self._write( ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1') ) if self.server_software and 'Server' not in self.headers: self._write(('Server: %s\r\n' % self.server_software).encode('iso-8859-1')) else: self._write(('Status: %s\r\n' % self.status).encode('iso-8859-1')) def write(self, data): """'write()' callable as specified by PEP 3333""" assert type(data) is bytes, \ "write() argument must be a bytes instance" if not self.status: raise AssertionError("write() before start_response()") elif not self.headers_sent: # Before the first output, send the stored headers self.bytes_sent = len(data) # make sure we know content-length self.send_headers() else: self.bytes_sent += len(data) # XXX check Content-Length and truncate if too many bytes written? self._write(data) self._flush() def sendfile(self): """Platform-specific file transmission Override this method in subclasses to support platform-specific file transmission. It is only called if the application's return iterable ('self.result') is an instance of 'self.wsgi_file_wrapper'. This method should return a true value if it was able to actually transmit the wrapped file-like object using a platform-specific approach. It should return a false value if normal iteration should be used instead. An exception can be raised to indicate that transmission was attempted, but failed. NOTE: this method should call 'self.send_headers()' if 'self.headers_sent' is false and it is going to attempt direct transmission of the file. """ return False # No platform-specific transmission by default def finish_content(self): """Ensure headers and content have both been sent""" if not self.headers_sent: # Only zero Content-Length if not set by the application (so # that HEAD requests can be satisfied properly, see #3839) self.headers.setdefault('Content-Length', "0") self.send_headers() else: pass # XXX check if content-length was too short? def close(self): """Close the iterable (if needed) and reset all instance vars Subclasses may want to also drop the client connection. """ try: if hasattr(self.result,'close'): self.result.close() finally: self.result = self.headers = self.status = self.environ = None self.bytes_sent = 0; self.headers_sent = False def send_headers(self): """Transmit headers to the client, via self._write()""" self.cleanup_headers() self.headers_sent = True if not self.origin_server or self.client_is_modern(): self.send_preamble() self._write(bytes(self.headers)) def result_is_file(self): """True if 'self.result' is an instance of 'self.wsgi_file_wrapper'""" wrapper = self.wsgi_file_wrapper return wrapper is not None and isinstance(self.result,wrapper) def client_is_modern(self): """True if client can accept status and headers""" return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9' def log_exception(self,exc_info): """Log the 'exc_info' tuple in the server log Subclasses may override to retarget the output or change its format. """ try: from traceback import print_exception stderr = self.get_stderr() print_exception( exc_info[0], exc_info[1], exc_info[2], self.traceback_limit, stderr ) stderr.flush() finally: exc_info = None def handle_error(self): """Log current error, and send error output to client if possible""" self.log_exception(sys.exc_info()) if not self.headers_sent: self.result = self.error_output(self.environ, self.start_response) self.finish_response() # XXX else: attempt advanced recovery techniques for HTML or text? def error_output(self, environ, start_response): """WSGI mini-app to create error output By default, this just uses the 'error_status', 'error_headers', and 'error_body' attributes to generate an output page. It can be overridden in a subclass to dynamically generate diagnostics, choose an appropriate message for the user's preferred language, etc. Note, however, that it's not recommended from a security perspective to spit out diagnostics to any old user; ideally, you should have to do something special to enable diagnostic output, which is why we don't include any here! """ start_response(self.error_status,self.error_headers[:],sys.exc_info()) return [self.error_body] # Pure abstract methods; *must* be overridden in subclasses def _write(self,data): """Override in subclass to buffer data for send to client It's okay if this method actually transmits the data; BaseHandler just separates write and flush operations for greater efficiency when the underlying system actually has such a distinction. """ raise NotImplementedError def _flush(self): """Override in subclass to force sending of recent '_write()' calls It's okay if this method is a no-op (i.e., if '_write()' actually sends the data. """ raise NotImplementedError def get_stdin(self): """Override in subclass to return suitable 'wsgi.input'""" raise NotImplementedError def get_stderr(self): """Override in subclass to return suitable 'wsgi.errors'""" raise NotImplementedError def add_cgi_vars(self): """Override in subclass to insert CGI variables in 'self.environ'""" raise NotImplementedError class SimpleHandler(BaseHandler): """Handler that's just initialized with streams, environment, etc. This handler subclass is intended for synchronous HTTP/1.0 origin servers, and handles sending the entire response output, given the correct inputs. Usage:: handler = SimpleHandler( inp,out,err,env, multithread=False, multiprocess=True ) handler.run(app)""" def __init__(self,stdin,stdout,stderr,environ, multithread=True, multiprocess=False ): self.stdin = stdin self.stdout = stdout self.stderr = stderr self.base_env = environ self.wsgi_multithread = multithread self.wsgi_multiprocess = multiprocess def get_stdin(self): return self.stdin def get_stderr(self): return self.stderr def add_cgi_vars(self): self.environ.update(self.base_env) def _write(self,data): result = self.stdout.write(data) if result is None or result == len(data): return from warnings import warn warn("SimpleHandler.stdout.write() should not do partial writes", DeprecationWarning) while True: data = data[result:] if not data: break result = self.stdout.write(data) def _flush(self): self.stdout.flush() self._flush = self.stdout.flush class BaseCGIHandler(SimpleHandler): """CGI-like systems using input/output/error streams and environ mapping Usage:: handler = BaseCGIHandler(inp,out,err,env) handler.run(app) This handler class is useful for gateway protocols like ReadyExec and FastCGI, that have usable input/output/error streams and an environment mapping. It's also the base class for CGIHandler, which just uses sys.stdin, os.environ, and so on. The constructor also takes keyword arguments 'multithread' and 'multiprocess' (defaulting to 'True' and 'False' respectively) to control the configuration sent to the application. It sets 'origin_server' to False (to enable CGI-like output), and assumes that 'wsgi.run_once' is False. """ origin_server = False class CGIHandler(BaseCGIHandler): """CGI-based invocation via sys.stdin/stdout/stderr and os.environ Usage:: CGIHandler().run(app) The difference between this class and BaseCGIHandler is that it always uses 'wsgi.run_once' of 'True', 'wsgi.multithread' of 'False', and 'wsgi.multiprocess' of 'True'. It does not take any initialization parameters, but always uses 'sys.stdin', 'os.environ', and friends. If you need to override any of these parameters, use BaseCGIHandler instead. """ wsgi_run_once = True # Do not allow os.environ to leak between requests in Google App Engine # and other multi-run CGI use cases. This is not easily testable. # See http://bugs.python.org/issue7250 os_environ = {} def __init__(self): BaseCGIHandler.__init__( self, sys.stdin.buffer, sys.stdout.buffer, sys.stderr, read_environ(), multithread=False, multiprocess=True ) class IISCGIHandler(BaseCGIHandler): """CGI-based invocation with workaround for IIS path bug This handler should be used in preference to CGIHandler when deploying on Microsoft IIS without having set the config allowPathInfo option (IIS>=7) or metabase allowPathInfoForScriptMappings (IIS<7). """ wsgi_run_once = True os_environ = {} # By default, IIS gives a PATH_INFO that duplicates the SCRIPT_NAME at # the front, causing problems for WSGI applications that wish to implement # routing. This handler strips any such duplicated path. # IIS can be configured to pass the correct PATH_INFO, but this causes # another bug where PATH_TRANSLATED is wrong. Luckily this variable is # rarely used and is not guaranteed by WSGI. On IIS<7, though, the # setting can only be made on a vhost level, affecting all other script # mappings, many of which break when exposed to the PATH_TRANSLATED bug. # For this reason IIS<7 is almost never deployed with the fix. (Even IIS7 # rarely uses it because there is still no UI for it.) # There is no way for CGI code to tell whether the option was set, so a # separate handler class is provided. def __init__(self): environ= read_environ() path = environ.get('PATH_INFO', '') script = environ.get('SCRIPT_NAME', '') if (path+'/').startswith(script+'/'): environ['PATH_INFO'] = path[len(script):] BaseCGIHandler.__init__( self, sys.stdin.buffer, sys.stdout.buffer, sys.stderr, environ, multithread=False, multiprocess=True )
21,001
558
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/wsgiref/validate.py
# (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org) # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php # Also licenced under the Apache License, 2.0: http://opensource.org/licenses/apache2.0.php # Licensed to PSF under a Contributor Agreement """ Middleware to check for obedience to the WSGI specification. Some of the things this checks: * Signature of the application and start_response (including that keyword arguments are not used). * Environment checks: - Environment is a dictionary (and not a subclass). - That all the required keys are in the environment: REQUEST_METHOD, SERVER_NAME, SERVER_PORT, wsgi.version, wsgi.input, wsgi.errors, wsgi.multithread, wsgi.multiprocess, wsgi.run_once - That HTTP_CONTENT_TYPE and HTTP_CONTENT_LENGTH are not in the environment (these headers should appear as CONTENT_LENGTH and CONTENT_TYPE). - Warns if QUERY_STRING is missing, as the cgi module acts unpredictably in that case. - That CGI-style variables (that don't contain a .) have (non-unicode) string values - That wsgi.version is a tuple - That wsgi.url_scheme is 'http' or 'https' (@@: is this too restrictive?) - Warns if the REQUEST_METHOD is not known (@@: probably too restrictive). - That SCRIPT_NAME and PATH_INFO are empty or start with / - That at least one of SCRIPT_NAME or PATH_INFO are set. - That CONTENT_LENGTH is a positive integer. - That SCRIPT_NAME is not '/' (it should be '', and PATH_INFO should be '/'). - That wsgi.input has the methods read, readline, readlines, and __iter__ - That wsgi.errors has the methods flush, write, writelines * The status is a string, contains a space, starts with an integer, and that integer is in range (> 100). * That the headers is a list (not a subclass, not another kind of sequence). * That the items of the headers are tuples of strings. * That there is no 'status' header (that is used in CGI, but not in WSGI). * That the headers don't contain newlines or colons, end in _ or -, or contain characters codes below 037. * That Content-Type is given if there is content (CGI often has a default content type, but WSGI does not). * That no Content-Type is given when there is no content (@@: is this too restrictive?) * That the exc_info argument to start_response is a tuple or None. * That all calls to the writer are with strings, and no other methods on the writer are accessed. * That wsgi.input is used properly: - .read() is called with zero or one argument - That it returns a string - That readline, readlines, and __iter__ return strings - That .close() is not called - No other methods are provided * That wsgi.errors is used properly: - .write() and .writelines() is called with a string - That .close() is not called, and no other methods are provided. * The response iterator: - That it is not a string (it should be a list of a single string; a string will work, but perform horribly). - That .__next__() returns a string - That the iterator is not iterated over until start_response has been called (that can signal either a server or application error). - That .close() is called (doesn't raise exception, only prints to sys.stderr, because we only know it isn't called when the object is garbage collected). """ __all__ = ['validator'] import re import sys import warnings header_re = re.compile(r'^[a-zA-Z][a-zA-Z0-9\-_]*$') bad_header_value_re = re.compile(r'[\000-\037]') class WSGIWarning(Warning): """ Raised in response to WSGI-spec-related warnings """ def assert_(cond, *args): if not cond: raise AssertionError(*args) def check_string_type(value, title): if type (value) is str: return value raise AssertionError( "{0} must be of type str (got {1})".format(title, repr(value))) def validator(application): """ When applied between a WSGI server and a WSGI application, this middleware will check for WSGI compliancy on a number of levels. This middleware does not modify the request or response in any way, but will raise an AssertionError if anything seems off (except for a failure to close the application iterator, which will be printed to stderr -- there's no way to raise an exception at that point). """ def lint_app(*args, **kw): assert_(len(args) == 2, "Two arguments required") assert_(not kw, "No keyword arguments allowed") environ, start_response = args check_environ(environ) # We use this to check if the application returns without # calling start_response: start_response_started = [] def start_response_wrapper(*args, **kw): assert_(len(args) == 2 or len(args) == 3, ( "Invalid number of arguments: %s" % (args,))) assert_(not kw, "No keyword arguments allowed") status = args[0] headers = args[1] if len(args) == 3: exc_info = args[2] else: exc_info = None check_status(status) check_headers(headers) check_content_type(status, headers) check_exc_info(exc_info) start_response_started.append(None) return WriteWrapper(start_response(*args)) environ['wsgi.input'] = InputWrapper(environ['wsgi.input']) environ['wsgi.errors'] = ErrorWrapper(environ['wsgi.errors']) iterator = application(environ, start_response_wrapper) assert_(iterator is not None and iterator != False, "The application must return an iterator, if only an empty list") check_iterator(iterator) return IteratorWrapper(iterator, start_response_started) return lint_app class InputWrapper: def __init__(self, wsgi_input): self.input = wsgi_input def read(self, *args): assert_(len(args) == 1) v = self.input.read(*args) assert_(type(v) is bytes) return v def readline(self, *args): assert_(len(args) <= 1) v = self.input.readline(*args) assert_(type(v) is bytes) return v def readlines(self, *args): assert_(len(args) <= 1) lines = self.input.readlines(*args) assert_(type(lines) is list) for line in lines: assert_(type(line) is bytes) return lines def __iter__(self): while 1: line = self.readline() if not line: return yield line def close(self): assert_(0, "input.close() must not be called") class ErrorWrapper: def __init__(self, wsgi_errors): self.errors = wsgi_errors def write(self, s): assert_(type(s) is str) self.errors.write(s) def flush(self): self.errors.flush() def writelines(self, seq): for line in seq: self.write(line) def close(self): assert_(0, "errors.close() must not be called") class WriteWrapper: def __init__(self, wsgi_writer): self.writer = wsgi_writer def __call__(self, s): assert_(type(s) is bytes) self.writer(s) class PartialIteratorWrapper: def __init__(self, wsgi_iterator): self.iterator = wsgi_iterator def __iter__(self): # We want to make sure __iter__ is called return IteratorWrapper(self.iterator, None) class IteratorWrapper: def __init__(self, wsgi_iterator, check_start_response): self.original_iterator = wsgi_iterator self.iterator = iter(wsgi_iterator) self.closed = False self.check_start_response = check_start_response def __iter__(self): return self def __next__(self): assert_(not self.closed, "Iterator read after closed") v = next(self.iterator) if type(v) is not bytes: assert_(False, "Iterator yielded non-bytestring (%r)" % (v,)) if self.check_start_response is not None: assert_(self.check_start_response, "The application returns and we started iterating over its body, but start_response has not yet been called") self.check_start_response = None return v def close(self): self.closed = True if hasattr(self.original_iterator, 'close'): self.original_iterator.close() def __del__(self): if not self.closed: sys.stderr.write( "Iterator garbage collected without being closed") assert_(self.closed, "Iterator garbage collected without being closed") def check_environ(environ): assert_(type(environ) is dict, "Environment is not of the right type: %r (environment: %r)" % (type(environ), environ)) for key in ['REQUEST_METHOD', 'SERVER_NAME', 'SERVER_PORT', 'wsgi.version', 'wsgi.input', 'wsgi.errors', 'wsgi.multithread', 'wsgi.multiprocess', 'wsgi.run_once']: assert_(key in environ, "Environment missing required key: %r" % (key,)) for key in ['HTTP_CONTENT_TYPE', 'HTTP_CONTENT_LENGTH']: assert_(key not in environ, "Environment should not have the key: %s " "(use %s instead)" % (key, key[5:])) if 'QUERY_STRING' not in environ: warnings.warn( 'QUERY_STRING is not in the WSGI environment; the cgi ' 'module will use sys.argv when this variable is missing, ' 'so application errors are more likely', WSGIWarning) for key in environ.keys(): if '.' in key: # Extension, we don't care about its type continue assert_(type(environ[key]) is str, "Environmental variable %s is not a string: %r (value: %r)" % (key, type(environ[key]), environ[key])) assert_(type(environ['wsgi.version']) is tuple, "wsgi.version should be a tuple (%r)" % (environ['wsgi.version'],)) assert_(environ['wsgi.url_scheme'] in ('http', 'https'), "wsgi.url_scheme unknown: %r" % environ['wsgi.url_scheme']) check_input(environ['wsgi.input']) check_errors(environ['wsgi.errors']) # @@: these need filling out: if environ['REQUEST_METHOD'] not in ( 'GET', 'HEAD', 'POST', 'OPTIONS', 'PATCH', 'PUT', 'DELETE', 'TRACE'): warnings.warn( "Unknown REQUEST_METHOD: %r" % environ['REQUEST_METHOD'], WSGIWarning) assert_(not environ.get('SCRIPT_NAME') or environ['SCRIPT_NAME'].startswith('/'), "SCRIPT_NAME doesn't start with /: %r" % environ['SCRIPT_NAME']) assert_(not environ.get('PATH_INFO') or environ['PATH_INFO'].startswith('/'), "PATH_INFO doesn't start with /: %r" % environ['PATH_INFO']) if environ.get('CONTENT_LENGTH'): assert_(int(environ['CONTENT_LENGTH']) >= 0, "Invalid CONTENT_LENGTH: %r" % environ['CONTENT_LENGTH']) if not environ.get('SCRIPT_NAME'): assert_('PATH_INFO' in environ, "One of SCRIPT_NAME or PATH_INFO are required (PATH_INFO " "should at least be '/' if SCRIPT_NAME is empty)") assert_(environ.get('SCRIPT_NAME') != '/', "SCRIPT_NAME cannot be '/'; it should instead be '', and " "PATH_INFO should be '/'") def check_input(wsgi_input): for attr in ['read', 'readline', 'readlines', '__iter__']: assert_(hasattr(wsgi_input, attr), "wsgi.input (%r) doesn't have the attribute %s" % (wsgi_input, attr)) def check_errors(wsgi_errors): for attr in ['flush', 'write', 'writelines']: assert_(hasattr(wsgi_errors, attr), "wsgi.errors (%r) doesn't have the attribute %s" % (wsgi_errors, attr)) def check_status(status): status = check_string_type(status, "Status") # Implicitly check that we can turn it into an integer: status_code = status.split(None, 1)[0] assert_(len(status_code) == 3, "Status codes must be three characters: %r" % status_code) status_int = int(status_code) assert_(status_int >= 100, "Status code is invalid: %r" % status_int) if len(status) < 4 or status[3] != ' ': warnings.warn( "The status string (%r) should be a three-digit integer " "followed by a single space and a status explanation" % status, WSGIWarning) def check_headers(headers): assert_(type(headers) is list, "Headers (%r) must be of type list: %r" % (headers, type(headers))) header_names = {} for item in headers: assert_(type(item) is tuple, "Individual headers (%r) must be of type tuple: %r" % (item, type(item))) assert_(len(item) == 2) name, value = item name = check_string_type(name, "Header name") value = check_string_type(value, "Header value") assert_(name.lower() != 'status', "The Status header cannot be used; it conflicts with CGI " "script, and HTTP status is not given through headers " "(value: %r)." % value) header_names[name.lower()] = None assert_('\n' not in name and ':' not in name, "Header names may not contain ':' or '\\n': %r" % name) assert_(header_re.search(name), "Bad header name: %r" % name) assert_(not name.endswith('-') and not name.endswith('_'), "Names may not end in '-' or '_': %r" % name) if bad_header_value_re.search(value): assert_(0, "Bad header value: %r (bad char: %r)" % (value, bad_header_value_re.search(value).group(0))) def check_content_type(status, headers): status = check_string_type(status, "Status") code = int(status.split(None, 1)[0]) # @@: need one more person to verify this interpretation of RFC 2616 # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html NO_MESSAGE_BODY = (204, 304) for name, value in headers: name = check_string_type(name, "Header name") if name.lower() == 'content-type': if code not in NO_MESSAGE_BODY: return assert_(0, ("Content-Type header found in a %s response, " "which must not return content.") % code) if code not in NO_MESSAGE_BODY: assert_(0, "No Content-Type header found in headers (%s)" % headers) def check_exc_info(exc_info): assert_(exc_info is None or type(exc_info) is tuple, "exc_info (%r) is not a tuple: %r" % (exc_info, type(exc_info))) # More exc_info checks? def check_iterator(iterator): # Technically a bytestring is legal, which is why it's a really bad # idea, because it may cause the response to be returned # character-by-character assert_(not isinstance(iterator, (str, bytes)), "You should not return a string as your application iterator, " "instead return a single-item list containing a bytestring.")
15,163
444
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/wsgiref/__init__.py
"""wsgiref -- a WSGI (PEP 3333) Reference Library Current Contents: * util -- Miscellaneous useful functions and wrappers * headers -- Manage response headers * handlers -- base classes for server/gateway implementations * simple_server -- a simple BaseHTTPServer that supports WSGI * validate -- validation wrapper that sits between an app and a server to detect errors in either To-Do: * cgi_gateway -- Run WSGI apps under CGI (pending a deployment standard) * cgi_wrapper -- Run CGI apps under WSGI * router -- a simple middleware component that handles URL traversal """
587
24
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/sqlite3/dump.py
# Mimic the sqlite3 console shell's .dump command # Author: Paul Kippes <[email protected]> """SQLite Python Dump Module Every identifier in sql is quoted based on a comment in sqlite documentation "SQLite adds new keywords from time to time when it takes on new features. So to prevent your code from being broken by future enhancements, you should normally quote any identifier that is an English language word, even if you do not have to." """ def _iterdump(connection): """ Returns an iterator to the dump of the database in an SQL text format. Used to produce an SQL dump of the database. Useful to save an in-memory database for later restoration. This function should not be called directly but instead called from the Connection method, iterdump(). """ cu = connection.cursor() yield('BEGIN TRANSACTION;') # sqlite_master table contains the SQL CREATE statements for the database. q = """ SELECT "name", "type", "sql" FROM "sqlite_master" WHERE "sql" NOT NULL AND "type" == 'table' ORDER BY "name" """ schema_res = cu.execute(q) for table_name, type, sql in schema_res.fetchall(): if table_name == 'sqlite_sequence': yield('DELETE FROM "sqlite_sequence";') elif table_name == 'sqlite_stat1': yield('ANALYZE "sqlite_master";') elif table_name.startswith('sqlite_'): continue # NOTE: Virtual table support not implemented #elif sql.startswith('CREATE VIRTUAL TABLE'): # qtable = table_name.replace("'", "''") # yield("INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"\ # "VALUES('table','{0}','{0}',0,'{1}');".format( # qtable, # sql.replace("''"))) else: yield('{0};'.format(sql)) # Build the insert statement for each row of the current table table_name_ident = table_name.replace('"', '""') res = cu.execute('PRAGMA table_info("{0}")'.format(table_name_ident)) column_names = [str(table_info[1]) for table_info in res.fetchall()] q = """SELECT 'INSERT INTO "{0}" VALUES({1})' FROM "{0}";""".format( table_name_ident, ",".join("""'||quote("{0}")||'""".format(col.replace('"', '""')) for col in column_names)) query_res = cu.execute(q) for row in query_res: yield("{0};".format(row[0])) # Now when the type is 'index', 'trigger', or 'view' q = """ SELECT "name", "type", "sql" FROM "sqlite_master" WHERE "sql" NOT NULL AND "type" IN ('index', 'trigger', 'view') """ schema_res = cu.execute(q) for name, type, sql in schema_res.fetchall(): yield('{0};'.format(sql)) yield('COMMIT;')
2,849
74
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/sqlite3/dbapi2.py
# pysqlite2/dbapi2.py: the DB-API 2.0 interface # # Copyright (C) 2004-2005 Gerhard Häring <[email protected]> # # This file is part of pysqlite. # # This software is provided 'as-is', without any express or implied # warranty. In no event will the authors be held liable for any damages # arising from the use of this software. # # Permission is granted to anyone to use this software for any purpose, # including commercial applications, and to alter it and redistribute it # freely, subject to the following restrictions: # # 1. The origin of this software must not be misrepresented; you must not # claim that you wrote the original software. If you use this software # in a product, an acknowledgment in the product documentation would be # appreciated but is not required. # 2. Altered source versions must be plainly marked as such, and must not be # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. """SQLite Python Database API Module """ import datetime import time import collections.abc from _sqlite3 import * paramstyle = "qmark" threadsafety = 1 apilevel = "2.0" Date = datetime.date Time = datetime.time Timestamp = datetime.datetime def DateFromTicks(ticks): return Date(*time.localtime(ticks)[:3]) def TimeFromTicks(ticks): return Time(*time.localtime(ticks)[3:6]) def TimestampFromTicks(ticks): return Timestamp(*time.localtime(ticks)[:6]) version_info = tuple([int(x) for x in version.split(".")]) sqlite_version_info = tuple([int(x) for x in sqlite_version.split(".")]) Binary = memoryview collections.abc.Sequence.register(Row) def register_adapters_and_converters(): def adapt_date(val): return val.isoformat() def adapt_datetime(val): return val.isoformat(" ") def convert_date(val): return datetime.date(*map(int, val.split(b"-"))) def convert_timestamp(val): datepart, timepart = val.split(b" ") year, month, day = map(int, datepart.split(b"-")) timepart_full = timepart.split(b".") hours, minutes, seconds = map(int, timepart_full[0].split(b":")) if len(timepart_full) == 2: microseconds = int('{:0<6.6}'.format(timepart_full[1].decode())) else: microseconds = 0 val = datetime.datetime(year, month, day, hours, minutes, seconds, microseconds) return val register_adapter(datetime.date, adapt_date) register_adapter(datetime.datetime, adapt_datetime) register_converter("date", convert_date) register_converter("timestamp", convert_timestamp) register_adapters_and_converters() # Clean up namespace del(register_adapters_and_converters)
2,729
93
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/sqlite3/__init__.py
# pysqlite2/__init__.py: the pysqlite2 package. # # Copyright (C) 2005 Gerhard Häring <[email protected]> # # This file is part of pysqlite. # # This software is provided 'as-is', without any express or implied # warranty. In no event will the authors be held liable for any damages # arising from the use of this software. # # Permission is granted to anyone to use this software for any purpose, # including commercial applications, and to alter it and redistribute it # freely, subject to the following restrictions: # # 1. The origin of this software must not be misrepresented; you must not # claim that you wrote the original software. If you use this software # in a product, an acknowledgment in the product documentation would be # appreciated but is not required. # 2. Altered source versions must be plainly marked as such, and must not be # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. """SQLite Python Bindings Module """ from sqlite3.dbapi2 import * if __name__ == 'PYOBJ.COM': Binary = 0 Cache = 0 Connection = 0 Cursor = 0 DataError = 0 DatabaseError = 0 Date = 0 DateFromTicks = 0 Error = 0 IntegrityError = 0 InterfaceError = 0 InternalError = 0 NotSupportedError = 0 OperationalError = 0 OptimizedUnicode = 0 PARSE_COLNAMES = 0 PARSE_DECLTYPES = 0 PrepareProtocol = 0 ProgrammingError = 0 Row = 0 SQLITE_ALTER_TABLE = 0 SQLITE_ANALYZE = 0 SQLITE_ATTACH = 0 SQLITE_CREATE_INDEX = 0 SQLITE_CREATE_TABLE = 0 SQLITE_CREATE_TEMP_INDEX = 0 SQLITE_CREATE_TEMP_TABLE = 0 SQLITE_CREATE_TEMP_TRIGGER = 0 SQLITE_CREATE_TEMP_VIEW = 0 SQLITE_CREATE_TRIGGER = 0 SQLITE_CREATE_VIEW = 0 SQLITE_DELETE = 0 SQLITE_DENY = 0 SQLITE_DETACH = 0 SQLITE_DROP_INDEX = 0 SQLITE_DROP_TABLE = 0 SQLITE_DROP_TEMP_INDEX = 0 SQLITE_DROP_TEMP_TABLE = 0 SQLITE_DROP_TEMP_TRIGGER = 0 SQLITE_DROP_TEMP_VIEW = 0 SQLITE_DROP_TRIGGER = 0 SQLITE_DROP_VIEW = 0 SQLITE_IGNORE = 0 SQLITE_INSERT = 0 SQLITE_OK = 0 SQLITE_PRAGMA = 0 SQLITE_READ = 0 SQLITE_REINDEX = 0 SQLITE_SELECT = 0 SQLITE_TRANSACTION = 0 SQLITE_UPDATE = 0 Statement = 0 Time = 0 TimeFromTicks = 0 Timestamp = 0 TimestampFromTicks = 0 Warning = 0 adapt = 0 adapters = 0 apilevel = 0 collections = 0 complete_statement = 0 connect = 0 converters = 0 datetime = 0 dbapi2 = 0 enable_callback_tracebacks = 0 paramstyle = 0 register_adapter = 0 register_converter = 0 sqlite_version = 0 sqlite_version_info = 0 threadsafety = 0 time = 0 version = 0 version_info = 0
2,803
105
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/sqlite3/test/regression.py
#-*- coding: iso-8859-1 -*- # pysqlite2/test/regression.py: pysqlite regression tests # # Copyright (C) 2006-2010 Gerhard Häring <[email protected]> # # This file is part of pysqlite. # # This software is provided 'as-is', without any express or implied # warranty. In no event will the authors be held liable for any damages # arising from the use of this software. # # Permission is granted to anyone to use this software for any purpose, # including commercial applications, and to alter it and redistribute it # freely, subject to the following restrictions: # # 1. The origin of this software must not be misrepresented; you must not # claim that you wrote the original software. If you use this software # in a product, an acknowledgment in the product documentation would be # appreciated but is not required. # 2. Altered source versions must be plainly marked as such, and must not be # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. import datetime import unittest import sqlite3 as sqlite import weakref from test import support class RegressionTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") def tearDown(self): self.con.close() def CheckPragmaUserVersion(self): # This used to crash pysqlite because this pragma command returns NULL for the column name cur = self.con.cursor() cur.execute("pragma user_version") def CheckPragmaSchemaVersion(self): # This still crashed pysqlite <= 2.2.1 con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_COLNAMES) try: cur = self.con.cursor() cur.execute("pragma schema_version") finally: cur.close() con.close() def CheckStatementReset(self): # pysqlite 2.1.0 to 2.2.0 have the problem that not all statements are # reset before a rollback, but only those that are still in the # statement cache. The others are not accessible from the connection object. con = sqlite.connect(":memory:", cached_statements=5) cursors = [con.cursor() for x in range(5)] cursors[0].execute("create table test(x)") for i in range(10): cursors[0].executemany("insert into test(x) values (?)", [(x,) for x in range(10)]) for i in range(5): cursors[i].execute(" " * i + "select x from test") con.rollback() def CheckColumnNameWithSpaces(self): cur = self.con.cursor() cur.execute('select 1 as "foo bar [datetime]"') self.assertEqual(cur.description[0][0], "foo bar") cur.execute('select 1 as "foo baz"') self.assertEqual(cur.description[0][0], "foo baz") def CheckStatementFinalizationOnCloseDb(self): # pysqlite versions <= 2.3.3 only finalized statements in the statement # cache when closing the database. statements that were still # referenced in cursors weren't closed and could provoke " # "OperationalError: Unable to close due to unfinalised statements". con = sqlite.connect(":memory:") cursors = [] # default statement cache size is 100 for i in range(105): cur = con.cursor() cursors.append(cur) cur.execute("select 1 x union select " + str(i)) con.close() @unittest.skipIf(sqlite.sqlite_version_info < (3, 2, 2), 'needs sqlite 3.2.2 or newer') def CheckOnConflictRollback(self): con = sqlite.connect(":memory:") con.execute("create table foo(x, unique(x) on conflict rollback)") con.execute("insert into foo(x) values (1)") try: con.execute("insert into foo(x) values (1)") except sqlite.DatabaseError: pass con.execute("insert into foo(x) values (2)") try: con.commit() except sqlite.OperationalError: self.fail("pysqlite knew nothing about the implicit ROLLBACK") def CheckWorkaroundForBuggySqliteTransferBindings(self): """ pysqlite would crash with older SQLite versions unless a workaround is implemented. """ self.con.execute("create table foo(bar)") self.con.execute("drop table foo") self.con.execute("create table foo(bar)") def CheckEmptyStatement(self): """ pysqlite used to segfault with SQLite versions 3.5.x. These return NULL for "no-operation" statements """ self.con.execute("") def CheckTypeMapUsage(self): """ pysqlite until 2.4.1 did not rebuild the row_cast_map when recompiling a statement. This test exhibits the problem. """ SELECT = "select * from foo" con = sqlite.connect(":memory:",detect_types=sqlite.PARSE_DECLTYPES) con.execute("create table foo(bar timestamp)") con.execute("insert into foo(bar) values (?)", (datetime.datetime.now(),)) con.execute(SELECT) con.execute("drop table foo") con.execute("create table foo(bar integer)") con.execute("insert into foo(bar) values (5)") con.execute(SELECT) def CheckErrorMsgDecodeError(self): # When porting the module to Python 3.0, the error message about # decoding errors disappeared. This verifies they're back again. with self.assertRaises(sqlite.OperationalError) as cm: self.con.execute("select 'xxx' || ? || 'yyy' colname", (bytes(bytearray([250])),)).fetchone() msg = "Could not decode to UTF-8 column 'colname' with text 'xxx" self.assertIn(msg, str(cm.exception)) def CheckRegisterAdapter(self): """ See issue 3312. """ self.assertRaises(TypeError, sqlite.register_adapter, {}, None) def CheckSetIsolationLevel(self): # See issue 27881. class CustomStr(str): def upper(self): return None def __del__(self): con.isolation_level = "" con = sqlite.connect(":memory:") con.isolation_level = None for level in "", "DEFERRED", "IMMEDIATE", "EXCLUSIVE": with self.subTest(level=level): con.isolation_level = level con.isolation_level = level.lower() con.isolation_level = level.capitalize() con.isolation_level = CustomStr(level) # setting isolation_level failure should not alter previous state con.isolation_level = None con.isolation_level = "DEFERRED" pairs = [ (1, TypeError), (b'', TypeError), ("abc", ValueError), ("IMMEDIATE\0EXCLUSIVE", ValueError), ("\xe9", ValueError), ] for value, exc in pairs: with self.subTest(level=value): with self.assertRaises(exc): con.isolation_level = value self.assertEqual(con.isolation_level, "DEFERRED") def CheckCursorConstructorCallCheck(self): """ Verifies that cursor methods check whether base class __init__ was called. """ class Cursor(sqlite.Cursor): def __init__(self, con): pass con = sqlite.connect(":memory:") cur = Cursor(con) with self.assertRaises(sqlite.ProgrammingError): cur.execute("select 4+5").fetchall() with self.assertRaisesRegex(sqlite.ProgrammingError, r'^Base Cursor\.__init__ not called\.$'): cur.close() def CheckStrSubclass(self): """ The Python 3.0 port of the module didn't cope with values of subclasses of str. """ class MyStr(str): pass self.con.execute("select ?", (MyStr("abc"),)) def CheckConnectionConstructorCallCheck(self): """ Verifies that connection methods check whether base class __init__ was called. """ class Connection(sqlite.Connection): def __init__(self, name): pass con = Connection(":memory:") with self.assertRaises(sqlite.ProgrammingError): cur = con.cursor() def CheckCursorRegistration(self): """ Verifies that subclassed cursor classes are correctly registered with the connection object, too. (fetch-across-rollback problem) """ class Connection(sqlite.Connection): def cursor(self): return Cursor(self) class Cursor(sqlite.Cursor): def __init__(self, con): sqlite.Cursor.__init__(self, con) con = Connection(":memory:") cur = con.cursor() cur.execute("create table foo(x)") cur.executemany("insert into foo(x) values (?)", [(3,), (4,), (5,)]) cur.execute("select x from foo") con.rollback() with self.assertRaises(sqlite.InterfaceError): cur.fetchall() def CheckAutoCommit(self): """ Verifies that creating a connection in autocommit mode works. 2.5.3 introduced a regression so that these could no longer be created. """ con = sqlite.connect(":memory:", isolation_level=None) def CheckPragmaAutocommit(self): """ Verifies that running a PRAGMA statement that does an autocommit does work. This did not work in 2.5.3/2.5.4. """ cur = self.con.cursor() cur.execute("create table foo(bar)") cur.execute("insert into foo(bar) values (5)") cur.execute("pragma page_size") row = cur.fetchone() def CheckConnectionCall(self): """ Call a connection with a non-string SQL request: check error handling of the statement constructor. """ self.assertRaises(sqlite.Warning, self.con, 1) def CheckCollation(self): def collation_cb(a, b): return 1 self.assertRaises(sqlite.ProgrammingError, self.con.create_collation, # Lone surrogate cannot be encoded to the default encoding (utf8) "\uDC80", collation_cb) def CheckRecursiveCursorUse(self): """ http://bugs.python.org/issue10811 Recursively using a cursor, such as when reusing it from a generator led to segfaults. Now we catch recursive cursor usage and raise a ProgrammingError. """ con = sqlite.connect(":memory:") cur = con.cursor() cur.execute("create table a (bar)") cur.execute("create table b (baz)") def foo(): cur.execute("insert into a (bar) values (?)", (1,)) yield 1 with self.assertRaises(sqlite.ProgrammingError): cur.executemany("insert into b (baz) values (?)", ((i,) for i in foo())) def CheckConvertTimestampMicrosecondPadding(self): """ http://bugs.python.org/issue14720 The microsecond parsing of convert_timestamp() should pad with zeros, since the microsecond string "456" actually represents "456000". """ con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES) cur = con.cursor() cur.execute("CREATE TABLE t (x TIMESTAMP)") # Microseconds should be 456000 cur.execute("INSERT INTO t (x) VALUES ('2012-04-04 15:06:00.456')") # Microseconds should be truncated to 123456 cur.execute("INSERT INTO t (x) VALUES ('2012-04-04 15:06:00.123456789')") cur.execute("SELECT * FROM t") values = [x[0] for x in cur.fetchall()] self.assertEqual(values, [ datetime.datetime(2012, 4, 4, 15, 6, 0, 456000), datetime.datetime(2012, 4, 4, 15, 6, 0, 123456), ]) def CheckInvalidIsolationLevelType(self): # isolation level is a string, not an integer self.assertRaises(TypeError, sqlite.connect, ":memory:", isolation_level=123) def CheckNullCharacter(self): # Issue #21147 con = sqlite.connect(":memory:") self.assertRaises(ValueError, con, "\0select 1") self.assertRaises(ValueError, con, "select 1\0") cur = con.cursor() self.assertRaises(ValueError, cur.execute, " \0select 2") self.assertRaises(ValueError, cur.execute, "select 2\0") def CheckCommitCursorReset(self): """ Connection.commit() did reset cursors, which made sqlite3 to return rows multiple times when fetched from cursors after commit. See issues 10513 and 23129 for details. """ con = sqlite.connect(":memory:") con.executescript(""" create table t(c); create table t2(c); insert into t values(0); insert into t values(1); insert into t values(2); """) self.assertEqual(con.isolation_level, "") counter = 0 for i, row in enumerate(con.execute("select c from t")): with self.subTest(i=i, row=row): con.execute("insert into t2(c) values (?)", (i,)) con.commit() if counter == 0: self.assertEqual(row[0], 0) elif counter == 1: self.assertEqual(row[0], 1) elif counter == 2: self.assertEqual(row[0], 2) counter += 1 self.assertEqual(counter, 3, "should have returned exactly three rows") def CheckBpo31770(self): """ The interpreter shouldn't crash in case Cursor.__init__() is called more than once. """ def callback(*args): pass con = sqlite.connect(":memory:") cur = sqlite.Cursor(con) ref = weakref.ref(cur, callback) cur.__init__(con) del cur # The interpreter shouldn't crash when ref is collected. del ref support.gc_collect() class UnhashableFunc: __hash__ = None def __init__(self, return_value=None): self.calls = 0 self.return_value = return_value def __call__(self, *args, **kwargs): self.calls += 1 return self.return_value class UnhashableCallbacksTestCase(unittest.TestCase): """ https://bugs.python.org/issue34052 Registering unhashable callbacks raises TypeError, callbacks are not registered in SQLite after such registration attempt. """ def setUp(self): self.con = sqlite.connect(':memory:') def tearDown(self): self.con.close() def test_progress_handler(self): f = UnhashableFunc(return_value=0) with self.assertRaisesRegex(TypeError, 'unhashable type'): self.con.set_progress_handler(f, 1) self.con.execute('SELECT 1') self.assertFalse(f.calls) def test_func(self): func_name = 'func_name' f = UnhashableFunc() with self.assertRaisesRegex(TypeError, 'unhashable type'): self.con.create_function(func_name, 0, f) msg = 'no such function: %s' % func_name with self.assertRaisesRegex(sqlite.OperationalError, msg): self.con.execute('SELECT %s()' % func_name) self.assertFalse(f.calls) def test_authorizer(self): f = UnhashableFunc(return_value=sqlite.SQLITE_DENY) with self.assertRaisesRegex(TypeError, 'unhashable type'): self.con.set_authorizer(f) self.con.execute('SELECT 1') self.assertFalse(f.calls) def test_aggr(self): class UnhashableType(type): __hash__ = None aggr_name = 'aggr_name' with self.assertRaisesRegex(TypeError, 'unhashable type'): self.con.create_aggregate(aggr_name, 0, UnhashableType('Aggr', (), {})) msg = 'no such function: %s' % aggr_name with self.assertRaisesRegex(sqlite.OperationalError, msg): self.con.execute('SELECT %s()' % aggr_name) def suite(): regression_suite = unittest.makeSuite(RegressionTests, "Check") return unittest.TestSuite(( regression_suite, unittest.makeSuite(UnhashableCallbacksTestCase), )) def test(): runner = unittest.TextTestRunner() runner.run(suite()) if __name__ == "__main__": test()
16,401
456
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/sqlite3/test/dump.py
# Author: Paul Kippes <[email protected]> import unittest import sqlite3 as sqlite class DumpTests(unittest.TestCase): def setUp(self): self.cx = sqlite.connect(":memory:") self.cu = self.cx.cursor() def tearDown(self): self.cx.close() def CheckTableDump(self): expected_sqls = [ """CREATE TABLE "index"("index" blob);""" , """INSERT INTO "index" VALUES(X'01');""" , """CREATE TABLE "quoted""table"("quoted""field" text);""" , """INSERT INTO "quoted""table" VALUES('quoted''value');""" , "CREATE TABLE t1(id integer primary key, s1 text, " \ "t1_i1 integer not null, i2 integer, unique (s1), " \ "constraint t1_idx1 unique (i2));" , "INSERT INTO \"t1\" VALUES(1,'foo',10,20);" , "INSERT INTO \"t1\" VALUES(2,'foo2',30,30);" , "CREATE TABLE t2(id integer, t2_i1 integer, " \ "t2_i2 integer, primary key (id)," \ "foreign key(t2_i1) references t1(t1_i1));" , "CREATE TRIGGER trigger_1 update of t1_i1 on t1 " \ "begin " \ "update t2 set t2_i1 = new.t1_i1 where t2_i1 = old.t1_i1; " \ "end;" , "CREATE VIEW v1 as select * from t1 left join t2 " \ "using (id);" ] [self.cu.execute(s) for s in expected_sqls] i = self.cx.iterdump() actual_sqls = [s for s in i] expected_sqls = ['BEGIN TRANSACTION;'] + expected_sqls + \ ['COMMIT;'] [self.assertEqual(expected_sqls[i], actual_sqls[i]) for i in range(len(expected_sqls))] def CheckUnorderableRow(self): # iterdump() should be able to cope with unorderable row types (issue #15545) class UnorderableRow: def __init__(self, cursor, row): self.row = row def __getitem__(self, index): return self.row[index] self.cx.row_factory = UnorderableRow CREATE_ALPHA = """CREATE TABLE "alpha" ("one");""" CREATE_BETA = """CREATE TABLE "beta" ("two");""" expected = [ "BEGIN TRANSACTION;", CREATE_ALPHA, CREATE_BETA, "COMMIT;" ] self.cu.execute(CREATE_BETA) self.cu.execute(CREATE_ALPHA) got = list(self.cx.iterdump()) self.assertEqual(expected, got) def suite(): return unittest.TestSuite(unittest.makeSuite(DumpTests, "Check")) def test(): runner = unittest.TextTestRunner() runner.run(suite()) if __name__ == "__main__": test()
2,840
82
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/sqlite3/test/dbapi.py
#-*- coding: iso-8859-1 -*- # pysqlite2/test/dbapi.py: tests for DB-API compliance # # Copyright (C) 2004-2010 Gerhard Häring <[email protected]> # # This file is part of pysqlite. # # This software is provided 'as-is', without any express or implied # warranty. In no event will the authors be held liable for any damages # arising from the use of this software. # # Permission is granted to anyone to use this software for any purpose, # including commercial applications, and to alter it and redistribute it # freely, subject to the following restrictions: # # 1. The origin of this software must not be misrepresented; you must not # claim that you wrote the original software. If you use this software # in a product, an acknowledgment in the product documentation would be # appreciated but is not required. # 2. Altered source versions must be plainly marked as such, and must not be # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. import unittest import sqlite3 as sqlite try: import _thread import threading except ImportError: threading = None from test.support import TESTFN, unlink class ModuleTests(unittest.TestCase): def CheckAPILevel(self): self.assertEqual(sqlite.apilevel, "2.0", "apilevel is %s, should be 2.0" % sqlite.apilevel) def CheckThreadSafety(self): self.assertEqual(sqlite.threadsafety, 1, "threadsafety is %d, should be 1" % sqlite.threadsafety) def CheckParamStyle(self): self.assertEqual(sqlite.paramstyle, "qmark", "paramstyle is '%s', should be 'qmark'" % sqlite.paramstyle) def CheckWarning(self): self.assertTrue(issubclass(sqlite.Warning, Exception), "Warning is not a subclass of Exception") def CheckError(self): self.assertTrue(issubclass(sqlite.Error, Exception), "Error is not a subclass of Exception") def CheckInterfaceError(self): self.assertTrue(issubclass(sqlite.InterfaceError, sqlite.Error), "InterfaceError is not a subclass of Error") def CheckDatabaseError(self): self.assertTrue(issubclass(sqlite.DatabaseError, sqlite.Error), "DatabaseError is not a subclass of Error") def CheckDataError(self): self.assertTrue(issubclass(sqlite.DataError, sqlite.DatabaseError), "DataError is not a subclass of DatabaseError") def CheckOperationalError(self): self.assertTrue(issubclass(sqlite.OperationalError, sqlite.DatabaseError), "OperationalError is not a subclass of DatabaseError") def CheckIntegrityError(self): self.assertTrue(issubclass(sqlite.IntegrityError, sqlite.DatabaseError), "IntegrityError is not a subclass of DatabaseError") def CheckInternalError(self): self.assertTrue(issubclass(sqlite.InternalError, sqlite.DatabaseError), "InternalError is not a subclass of DatabaseError") def CheckProgrammingError(self): self.assertTrue(issubclass(sqlite.ProgrammingError, sqlite.DatabaseError), "ProgrammingError is not a subclass of DatabaseError") def CheckNotSupportedError(self): self.assertTrue(issubclass(sqlite.NotSupportedError, sqlite.DatabaseError), "NotSupportedError is not a subclass of DatabaseError") class ConnectionTests(unittest.TestCase): def setUp(self): self.cx = sqlite.connect(":memory:") cu = self.cx.cursor() cu.execute("create table test(id integer primary key, name text)") cu.execute("insert into test(name) values (?)", ("foo",)) def tearDown(self): self.cx.close() def CheckCommit(self): self.cx.commit() def CheckCommitAfterNoChanges(self): """ A commit should also work when no changes were made to the database. """ self.cx.commit() self.cx.commit() def CheckRollback(self): self.cx.rollback() def CheckRollbackAfterNoChanges(self): """ A rollback should also work when no changes were made to the database. """ self.cx.rollback() self.cx.rollback() def CheckCursor(self): cu = self.cx.cursor() def CheckFailedOpen(self): YOU_CANNOT_OPEN_THIS = "/foo/bar/bla/23534/mydb.db" with self.assertRaises(sqlite.OperationalError): con = sqlite.connect(YOU_CANNOT_OPEN_THIS) def CheckClose(self): self.cx.close() def CheckExceptions(self): # Optional DB-API extension. self.assertEqual(self.cx.Warning, sqlite.Warning) self.assertEqual(self.cx.Error, sqlite.Error) self.assertEqual(self.cx.InterfaceError, sqlite.InterfaceError) self.assertEqual(self.cx.DatabaseError, sqlite.DatabaseError) self.assertEqual(self.cx.DataError, sqlite.DataError) self.assertEqual(self.cx.OperationalError, sqlite.OperationalError) self.assertEqual(self.cx.IntegrityError, sqlite.IntegrityError) self.assertEqual(self.cx.InternalError, sqlite.InternalError) self.assertEqual(self.cx.ProgrammingError, sqlite.ProgrammingError) self.assertEqual(self.cx.NotSupportedError, sqlite.NotSupportedError) def CheckInTransaction(self): # Can't use db from setUp because we want to test initial state. cx = sqlite.connect(":memory:") cu = cx.cursor() self.assertEqual(cx.in_transaction, False) cu.execute("create table transactiontest(id integer primary key, name text)") self.assertEqual(cx.in_transaction, False) cu.execute("insert into transactiontest(name) values (?)", ("foo",)) self.assertEqual(cx.in_transaction, True) cu.execute("select name from transactiontest where name=?", ["foo"]) row = cu.fetchone() self.assertEqual(cx.in_transaction, True) cx.commit() self.assertEqual(cx.in_transaction, False) cu.execute("select name from transactiontest where name=?", ["foo"]) row = cu.fetchone() self.assertEqual(cx.in_transaction, False) def CheckInTransactionRO(self): with self.assertRaises(AttributeError): self.cx.in_transaction = True def CheckOpenUri(self): if sqlite.sqlite_version_info < (3, 7, 7): with self.assertRaises(sqlite.NotSupportedError): sqlite.connect(':memory:', uri=True) return self.addCleanup(unlink, TESTFN) with sqlite.connect(TESTFN) as cx: cx.execute('create table test(id integer)') with sqlite.connect('file:' + TESTFN, uri=True) as cx: cx.execute('insert into test(id) values(0)') with sqlite.connect('file:' + TESTFN + '?mode=ro', uri=True) as cx: with self.assertRaises(sqlite.OperationalError): cx.execute('insert into test(id) values(1)') @unittest.skipIf(sqlite.sqlite_version_info >= (3, 3, 1), 'needs sqlite versions older than 3.3.1') def CheckSameThreadErrorOnOldVersion(self): with self.assertRaises(sqlite.NotSupportedError) as cm: sqlite.connect(':memory:', check_same_thread=False) self.assertEqual(str(cm.exception), 'shared connections not available') class CursorTests(unittest.TestCase): def setUp(self): self.cx = sqlite.connect(":memory:") self.cu = self.cx.cursor() self.cu.execute( "create table test(id integer primary key, name text, " "income number, unique_test text unique)" ) self.cu.execute("insert into test(name) values (?)", ("foo",)) def tearDown(self): self.cu.close() self.cx.close() def CheckExecuteNoArgs(self): self.cu.execute("delete from test") def CheckExecuteIllegalSql(self): with self.assertRaises(sqlite.OperationalError): self.cu.execute("select asdf") def CheckExecuteTooMuchSql(self): with self.assertRaises(sqlite.Warning): self.cu.execute("select 5+4; select 4+5") def CheckExecuteTooMuchSql2(self): self.cu.execute("select 5+4; -- foo bar") def CheckExecuteTooMuchSql3(self): self.cu.execute(""" select 5+4; /* foo */ """) def CheckExecuteWrongSqlArg(self): with self.assertRaises(ValueError): self.cu.execute(42) def CheckExecuteArgInt(self): self.cu.execute("insert into test(id) values (?)", (42,)) def CheckExecuteArgFloat(self): self.cu.execute("insert into test(income) values (?)", (2500.32,)) def CheckExecuteArgString(self): self.cu.execute("insert into test(name) values (?)", ("Hugo",)) def CheckExecuteArgStringWithZeroByte(self): self.cu.execute("insert into test(name) values (?)", ("Hu\x00go",)) self.cu.execute("select name from test where id=?", (self.cu.lastrowid,)) row = self.cu.fetchone() self.assertEqual(row[0], "Hu\x00go") def CheckExecuteNonIterable(self): with self.assertRaises(ValueError) as cm: self.cu.execute("insert into test(id) values (?)", 42) self.assertEqual(str(cm.exception), 'parameters are of unsupported type') def CheckExecuteWrongNoOfArgs1(self): # too many parameters with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("insert into test(id) values (?)", (17, "Egon")) def CheckExecuteWrongNoOfArgs2(self): # too little parameters with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("insert into test(id) values (?)") def CheckExecuteWrongNoOfArgs3(self): # no parameters, parameters are needed with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("insert into test(id) values (?)") def CheckExecuteParamList(self): self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("select name from test where name=?", ["foo"]) row = self.cu.fetchone() self.assertEqual(row[0], "foo") def CheckExecuteParamSequence(self): class L(object): def __len__(self): return 1 def __getitem__(self, x): assert x == 0 return "foo" self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("select name from test where name=?", L()) row = self.cu.fetchone() self.assertEqual(row[0], "foo") def CheckExecuteDictMapping(self): self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("select name from test where name=:name", {"name": "foo"}) row = self.cu.fetchone() self.assertEqual(row[0], "foo") def CheckExecuteDictMapping_Mapping(self): class D(dict): def __missing__(self, key): return "foo" self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("select name from test where name=:name", D()) row = self.cu.fetchone() self.assertEqual(row[0], "foo") def CheckExecuteDictMappingTooLittleArgs(self): self.cu.execute("insert into test(name) values ('foo')") with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("select name from test where name=:name and id=:id", {"name": "foo"}) def CheckExecuteDictMappingNoArgs(self): self.cu.execute("insert into test(name) values ('foo')") with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("select name from test where name=:name") def CheckExecuteDictMappingUnnamed(self): self.cu.execute("insert into test(name) values ('foo')") with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("select name from test where name=?", {"name": "foo"}) def CheckClose(self): self.cu.close() def CheckRowcountExecute(self): self.cu.execute("delete from test") self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("update test set name='bar'") self.assertEqual(self.cu.rowcount, 2) def CheckRowcountSelect(self): """ pysqlite does not know the rowcount of SELECT statements, because we don't fetch all rows after executing the select statement. The rowcount has thus to be -1. """ self.cu.execute("select 5 union select 6") self.assertEqual(self.cu.rowcount, -1) def CheckRowcountExecutemany(self): self.cu.execute("delete from test") self.cu.executemany("insert into test(name) values (?)", [(1,), (2,), (3,)]) self.assertEqual(self.cu.rowcount, 3) def CheckTotalChanges(self): self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("insert into test(name) values ('foo')") self.assertLess(2, self.cx.total_changes, msg='total changes reported wrong value') # Checks for executemany: # Sequences are required by the DB-API, iterators # enhancements in pysqlite. def CheckExecuteManySequence(self): self.cu.executemany("insert into test(income) values (?)", [(x,) for x in range(100, 110)]) def CheckExecuteManyIterator(self): class MyIter: def __init__(self): self.value = 5 def __next__(self): if self.value == 10: raise StopIteration else: self.value += 1 return (self.value,) self.cu.executemany("insert into test(income) values (?)", MyIter()) def CheckExecuteManyGenerator(self): def mygen(): for i in range(5): yield (i,) self.cu.executemany("insert into test(income) values (?)", mygen()) def CheckExecuteManyWrongSqlArg(self): with self.assertRaises(ValueError): self.cu.executemany(42, [(3,)]) def CheckExecuteManySelect(self): with self.assertRaises(sqlite.ProgrammingError): self.cu.executemany("select ?", [(3,)]) def CheckExecuteManyNotIterable(self): with self.assertRaises(TypeError): self.cu.executemany("insert into test(income) values (?)", 42) def CheckFetchIter(self): # Optional DB-API extension. self.cu.execute("delete from test") self.cu.execute("insert into test(id) values (?)", (5,)) self.cu.execute("insert into test(id) values (?)", (6,)) self.cu.execute("select id from test order by id") lst = [] for row in self.cu: lst.append(row[0]) self.assertEqual(lst[0], 5) self.assertEqual(lst[1], 6) def CheckFetchone(self): self.cu.execute("select name from test") row = self.cu.fetchone() self.assertEqual(row[0], "foo") row = self.cu.fetchone() self.assertEqual(row, None) def CheckFetchoneNoStatement(self): cur = self.cx.cursor() row = cur.fetchone() self.assertEqual(row, None) def CheckArraySize(self): # must default ot 1 self.assertEqual(self.cu.arraysize, 1) # now set to 2 self.cu.arraysize = 2 # now make the query return 3 rows self.cu.execute("delete from test") self.cu.execute("insert into test(name) values ('A')") self.cu.execute("insert into test(name) values ('B')") self.cu.execute("insert into test(name) values ('C')") self.cu.execute("select name from test") res = self.cu.fetchmany() self.assertEqual(len(res), 2) def CheckFetchmany(self): self.cu.execute("select name from test") res = self.cu.fetchmany(100) self.assertEqual(len(res), 1) res = self.cu.fetchmany(100) self.assertEqual(res, []) def CheckFetchmanyKwArg(self): """Checks if fetchmany works with keyword arguments""" self.cu.execute("select name from test") res = self.cu.fetchmany(size=100) self.assertEqual(len(res), 1) def CheckFetchall(self): self.cu.execute("select name from test") res = self.cu.fetchall() self.assertEqual(len(res), 1) res = self.cu.fetchall() self.assertEqual(res, []) def CheckSetinputsizes(self): self.cu.setinputsizes([3, 4, 5]) def CheckSetoutputsize(self): self.cu.setoutputsize(5, 0) def CheckSetoutputsizeNoColumn(self): self.cu.setoutputsize(42) def CheckCursorConnection(self): # Optional DB-API extension. self.assertEqual(self.cu.connection, self.cx) def CheckWrongCursorCallable(self): with self.assertRaises(TypeError): def f(): pass cur = self.cx.cursor(f) def CheckCursorWrongClass(self): class Foo: pass foo = Foo() with self.assertRaises(TypeError): cur = sqlite.Cursor(foo) def CheckLastRowIDOnReplace(self): """ INSERT OR REPLACE and REPLACE INTO should produce the same behavior. """ sql = '{} INTO test(id, unique_test) VALUES (?, ?)' for statement in ('INSERT OR REPLACE', 'REPLACE'): with self.subTest(statement=statement): self.cu.execute(sql.format(statement), (1, 'foo')) self.assertEqual(self.cu.lastrowid, 1) def CheckLastRowIDOnIgnore(self): self.cu.execute( "insert or ignore into test(unique_test) values (?)", ('test',)) self.assertEqual(self.cu.lastrowid, 2) self.cu.execute( "insert or ignore into test(unique_test) values (?)", ('test',)) self.assertEqual(self.cu.lastrowid, 2) def CheckLastRowIDInsertOR(self): results = [] for statement in ('FAIL', 'ABORT', 'ROLLBACK'): sql = 'INSERT OR {} INTO test(unique_test) VALUES (?)' with self.subTest(statement='INSERT OR {}'.format(statement)): self.cu.execute(sql.format(statement), (statement,)) results.append((statement, self.cu.lastrowid)) with self.assertRaises(sqlite.IntegrityError): self.cu.execute(sql.format(statement), (statement,)) results.append((statement, self.cu.lastrowid)) expected = [ ('FAIL', 2), ('FAIL', 2), ('ABORT', 3), ('ABORT', 3), ('ROLLBACK', 4), ('ROLLBACK', 4), ] self.assertEqual(results, expected) @unittest.skipUnless(threading, 'This test requires threading.') class ThreadTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") self.cur = self.con.cursor() self.cur.execute("create table test(id integer primary key, name text, bin binary, ratio number, ts timestamp)") def tearDown(self): self.cur.close() self.con.close() def CheckConCursor(self): def run(con, errors): try: cur = con.cursor() errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def CheckConCommit(self): def run(con, errors): try: con.commit() errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def CheckConRollback(self): def run(con, errors): try: con.rollback() errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def CheckConClose(self): def run(con, errors): try: con.close() errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def CheckCurImplicitBegin(self): def run(cur, errors): try: cur.execute("insert into test(name) values ('a')") errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def CheckCurClose(self): def run(cur, errors): try: cur.close() errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def CheckCurExecute(self): def run(cur, errors): try: cur.execute("select name from test") errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] self.cur.execute("insert into test(name) values ('a')") t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def CheckCurIterNext(self): def run(cur, errors): try: row = cur.fetchone() errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] self.cur.execute("insert into test(name) values ('a')") self.cur.execute("select name from test") t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) class ConstructorTests(unittest.TestCase): def CheckDate(self): d = sqlite.Date(2004, 10, 28) def CheckTime(self): t = sqlite.Time(12, 39, 35) def CheckTimestamp(self): ts = sqlite.Timestamp(2004, 10, 28, 12, 39, 35) def CheckDateFromTicks(self): d = sqlite.DateFromTicks(42) def CheckTimeFromTicks(self): t = sqlite.TimeFromTicks(42) def CheckTimestampFromTicks(self): ts = sqlite.TimestampFromTicks(42) def CheckBinary(self): b = sqlite.Binary(b"\0'") class ExtensionTests(unittest.TestCase): def CheckScriptStringSql(self): con = sqlite.connect(":memory:") cur = con.cursor() cur.executescript(""" -- bla bla /* a stupid comment */ create table a(i); insert into a(i) values (5); """) cur.execute("select i from a") res = cur.fetchone()[0] self.assertEqual(res, 5) def CheckScriptSyntaxError(self): con = sqlite.connect(":memory:") cur = con.cursor() with self.assertRaises(sqlite.OperationalError): cur.executescript("create table test(x); asdf; create table test2(x)") def CheckScriptErrorNormal(self): con = sqlite.connect(":memory:") cur = con.cursor() with self.assertRaises(sqlite.OperationalError): cur.executescript("create table test(sadfsadfdsa); select foo from hurz;") def CheckCursorExecutescriptAsBytes(self): con = sqlite.connect(":memory:") cur = con.cursor() with self.assertRaises(ValueError) as cm: cur.executescript(b"create table test(foo); insert into test(foo) values (5);") self.assertEqual(str(cm.exception), 'script argument must be unicode.') def CheckConnectionExecute(self): con = sqlite.connect(":memory:") result = con.execute("select 5").fetchone()[0] self.assertEqual(result, 5, "Basic test of Connection.execute") def CheckConnectionExecutemany(self): con = sqlite.connect(":memory:") con.execute("create table test(foo)") con.executemany("insert into test(foo) values (?)", [(3,), (4,)]) result = con.execute("select foo from test order by foo").fetchall() self.assertEqual(result[0][0], 3, "Basic test of Connection.executemany") self.assertEqual(result[1][0], 4, "Basic test of Connection.executemany") def CheckConnectionExecutescript(self): con = sqlite.connect(":memory:") con.executescript("create table test(foo); insert into test(foo) values (5);") result = con.execute("select foo from test").fetchone()[0] self.assertEqual(result, 5, "Basic test of Connection.executescript") class ClosedConTests(unittest.TestCase): def CheckClosedConCursor(self): con = sqlite.connect(":memory:") con.close() with self.assertRaises(sqlite.ProgrammingError): cur = con.cursor() def CheckClosedConCommit(self): con = sqlite.connect(":memory:") con.close() with self.assertRaises(sqlite.ProgrammingError): con.commit() def CheckClosedConRollback(self): con = sqlite.connect(":memory:") con.close() with self.assertRaises(sqlite.ProgrammingError): con.rollback() def CheckClosedCurExecute(self): con = sqlite.connect(":memory:") cur = con.cursor() con.close() with self.assertRaises(sqlite.ProgrammingError): cur.execute("select 4") def CheckClosedCreateFunction(self): con = sqlite.connect(":memory:") con.close() def f(x): return 17 with self.assertRaises(sqlite.ProgrammingError): con.create_function("foo", 1, f) def CheckClosedCreateAggregate(self): con = sqlite.connect(":memory:") con.close() class Agg: def __init__(self): pass def step(self, x): pass def finalize(self): return 17 with self.assertRaises(sqlite.ProgrammingError): con.create_aggregate("foo", 1, Agg) def CheckClosedSetAuthorizer(self): con = sqlite.connect(":memory:") con.close() def authorizer(*args): return sqlite.DENY with self.assertRaises(sqlite.ProgrammingError): con.set_authorizer(authorizer) def CheckClosedSetProgressCallback(self): con = sqlite.connect(":memory:") con.close() def progress(): pass with self.assertRaises(sqlite.ProgrammingError): con.set_progress_handler(progress, 100) def CheckClosedCall(self): con = sqlite.connect(":memory:") con.close() with self.assertRaises(sqlite.ProgrammingError): con() class ClosedCurTests(unittest.TestCase): def CheckClosed(self): con = sqlite.connect(":memory:") cur = con.cursor() cur.close() for method_name in ("execute", "executemany", "executescript", "fetchall", "fetchmany", "fetchone"): if method_name in ("execute", "executescript"): params = ("select 4 union select 5",) elif method_name == "executemany": params = ("insert into foo(bar) values (?)", [(3,), (4,)]) else: params = [] with self.assertRaises(sqlite.ProgrammingError): method = getattr(cur, method_name) method(*params) class SqliteOnConflictTests(unittest.TestCase): """ Tests for SQLite's "insert on conflict" feature. See https://www.sqlite.org/lang_conflict.html for details. """ def setUp(self): self.cx = sqlite.connect(":memory:") self.cu = self.cx.cursor() self.cu.execute(""" CREATE TABLE test( id INTEGER PRIMARY KEY, name TEXT, unique_name TEXT UNIQUE ); """) def tearDown(self): self.cu.close() self.cx.close() def CheckOnConflictRollbackWithExplicitTransaction(self): self.cx.isolation_level = None # autocommit mode self.cu = self.cx.cursor() # Start an explicit transaction. self.cu.execute("BEGIN") self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") # Use connection to commit. self.cx.commit() self.cu.execute("SELECT name, unique_name from test") # Transaction should have rolled back and nothing should be in table. self.assertEqual(self.cu.fetchall(), []) def CheckOnConflictAbortRaisesWithExplicitTransactions(self): # Abort cancels the current sql statement but doesn't change anything # about the current transaction. self.cx.isolation_level = None # autocommit mode self.cu = self.cx.cursor() # Start an explicit transaction. self.cu.execute("BEGIN") self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") self.cx.commit() self.cu.execute("SELECT name, unique_name FROM test") # Expect the first two inserts to work, third to do nothing. self.assertEqual(self.cu.fetchall(), [('abort_test', None), (None, 'foo',)]) def CheckOnConflictRollbackWithoutTransaction(self): # Start of implicit transaction self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") self.cu.execute("SELECT name, unique_name FROM test") # Implicit transaction is rolled back on error. self.assertEqual(self.cu.fetchall(), []) def CheckOnConflictAbortRaisesWithoutTransactions(self): # Abort cancels the current sql statement but doesn't change anything # about the current transaction. self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") # Make sure all other values were inserted. self.cu.execute("SELECT name, unique_name FROM test") self.assertEqual(self.cu.fetchall(), [('abort_test', None), (None, 'foo',)]) def CheckOnConflictFail(self): self.cu.execute("INSERT OR FAIL INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR FAIL INTO test(unique_name) VALUES ('foo')") self.assertEqual(self.cu.fetchall(), []) def CheckOnConflictIgnore(self): self.cu.execute("INSERT OR IGNORE INTO test(unique_name) VALUES ('foo')") # Nothing should happen. self.cu.execute("INSERT OR IGNORE INTO test(unique_name) VALUES ('foo')") self.cu.execute("SELECT unique_name FROM test") self.assertEqual(self.cu.fetchall(), [('foo',)]) def CheckOnConflictReplace(self): self.cu.execute("INSERT OR REPLACE INTO test(name, unique_name) VALUES ('Data!', 'foo')") # There shouldn't be an IntegrityError exception. self.cu.execute("INSERT OR REPLACE INTO test(name, unique_name) VALUES ('Very different data!', 'foo')") self.cu.execute("SELECT name, unique_name FROM test") self.assertEqual(self.cu.fetchall(), [('Very different data!', 'foo')]) def suite(): module_suite = unittest.makeSuite(ModuleTests, "Check") connection_suite = unittest.makeSuite(ConnectionTests, "Check") cursor_suite = unittest.makeSuite(CursorTests, "Check") thread_suite = unittest.makeSuite(ThreadTests, "Check") constructor_suite = unittest.makeSuite(ConstructorTests, "Check") ext_suite = unittest.makeSuite(ExtensionTests, "Check") closed_con_suite = unittest.makeSuite(ClosedConTests, "Check") closed_cur_suite = unittest.makeSuite(ClosedCurTests, "Check") on_conflict_suite = unittest.makeSuite(SqliteOnConflictTests, "Check") return unittest.TestSuite(( module_suite, connection_suite, cursor_suite, thread_suite, constructor_suite, ext_suite, closed_con_suite, closed_cur_suite, on_conflict_suite, )) def test(): runner = unittest.TextTestRunner() runner.run(suite()) if __name__ == "__main__": test()
35,076
940
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/sqlite3/test/types.py
#-*- coding: iso-8859-1 -*- # pysqlite2/test/types.py: tests for type conversion and detection # # Copyright (C) 2005 Gerhard Häring <[email protected]> # # This file is part of pysqlite. # # This software is provided 'as-is', without any express or implied # warranty. In no event will the authors be held liable for any damages # arising from the use of this software. # # Permission is granted to anyone to use this software for any purpose, # including commercial applications, and to alter it and redistribute it # freely, subject to the following restrictions: # # 1. The origin of this software must not be misrepresented; you must not # claim that you wrote the original software. If you use this software # in a product, an acknowledgment in the product documentation would be # appreciated but is not required. # 2. Altered source versions must be plainly marked as such, and must not be # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. import datetime import unittest import sqlite3 as sqlite try: import zlib except ImportError: zlib = None class SqliteTypeTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") self.cur = self.con.cursor() self.cur.execute("create table test(i integer, s varchar, f number, b blob)") def tearDown(self): self.cur.close() self.con.close() def CheckString(self): self.cur.execute("insert into test(s) values (?)", ("Österreich",)) self.cur.execute("select s from test") row = self.cur.fetchone() self.assertEqual(row[0], "Österreich") def CheckSmallInt(self): self.cur.execute("insert into test(i) values (?)", (42,)) self.cur.execute("select i from test") row = self.cur.fetchone() self.assertEqual(row[0], 42) def CheckLargeInt(self): num = 2**40 self.cur.execute("insert into test(i) values (?)", (num,)) self.cur.execute("select i from test") row = self.cur.fetchone() self.assertEqual(row[0], num) def CheckFloat(self): val = 3.14 self.cur.execute("insert into test(f) values (?)", (val,)) self.cur.execute("select f from test") row = self.cur.fetchone() self.assertEqual(row[0], val) def CheckBlob(self): sample = b"Guglhupf" val = memoryview(sample) self.cur.execute("insert into test(b) values (?)", (val,)) self.cur.execute("select b from test") row = self.cur.fetchone() self.assertEqual(row[0], sample) def CheckUnicodeExecute(self): self.cur.execute("select 'Österreich'") row = self.cur.fetchone() self.assertEqual(row[0], "Österreich") class DeclTypesTests(unittest.TestCase): class Foo: def __init__(self, _val): if isinstance(_val, bytes): # sqlite3 always calls __init__ with a bytes created from a # UTF-8 string when __conform__ was used to store the object. _val = _val.decode('utf-8') self.val = _val def __eq__(self, other): if not isinstance(other, DeclTypesTests.Foo): return NotImplemented return self.val == other.val def __conform__(self, protocol): if protocol is sqlite.PrepareProtocol: return self.val else: return None def __str__(self): return "<%s>" % self.val def setUp(self): self.con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES) self.cur = self.con.cursor() self.cur.execute("create table test(i int, s str, f float, b bool, u unicode, foo foo, bin blob, n1 number, n2 number(5))") # override float, make them always return the same number sqlite.converters["FLOAT"] = lambda x: 47.2 # and implement two custom ones sqlite.converters["BOOL"] = lambda x: bool(int(x)) sqlite.converters["FOO"] = DeclTypesTests.Foo sqlite.converters["WRONG"] = lambda x: "WRONG" sqlite.converters["NUMBER"] = float def tearDown(self): del sqlite.converters["FLOAT"] del sqlite.converters["BOOL"] del sqlite.converters["FOO"] del sqlite.converters["NUMBER"] self.cur.close() self.con.close() def CheckString(self): # default self.cur.execute("insert into test(s) values (?)", ("foo",)) self.cur.execute('select s as "s [WRONG]" from test') row = self.cur.fetchone() self.assertEqual(row[0], "foo") def CheckSmallInt(self): # default self.cur.execute("insert into test(i) values (?)", (42,)) self.cur.execute("select i from test") row = self.cur.fetchone() self.assertEqual(row[0], 42) def CheckLargeInt(self): # default num = 2**40 self.cur.execute("insert into test(i) values (?)", (num,)) self.cur.execute("select i from test") row = self.cur.fetchone() self.assertEqual(row[0], num) def CheckFloat(self): # custom val = 3.14 self.cur.execute("insert into test(f) values (?)", (val,)) self.cur.execute("select f from test") row = self.cur.fetchone() self.assertEqual(row[0], 47.2) def CheckBool(self): # custom self.cur.execute("insert into test(b) values (?)", (False,)) self.cur.execute("select b from test") row = self.cur.fetchone() self.assertEqual(row[0], False) self.cur.execute("delete from test") self.cur.execute("insert into test(b) values (?)", (True,)) self.cur.execute("select b from test") row = self.cur.fetchone() self.assertEqual(row[0], True) def CheckUnicode(self): # default val = "\xd6sterreich" self.cur.execute("insert into test(u) values (?)", (val,)) self.cur.execute("select u from test") row = self.cur.fetchone() self.assertEqual(row[0], val) def CheckFoo(self): val = DeclTypesTests.Foo("bla") self.cur.execute("insert into test(foo) values (?)", (val,)) self.cur.execute("select foo from test") row = self.cur.fetchone() self.assertEqual(row[0], val) def CheckUnsupportedSeq(self): class Bar: pass val = Bar() with self.assertRaises(sqlite.InterfaceError): self.cur.execute("insert into test(f) values (?)", (val,)) def CheckUnsupportedDict(self): class Bar: pass val = Bar() with self.assertRaises(sqlite.InterfaceError): self.cur.execute("insert into test(f) values (:val)", {"val": val}) def CheckBlob(self): # default sample = b"Guglhupf" val = memoryview(sample) self.cur.execute("insert into test(bin) values (?)", (val,)) self.cur.execute("select bin from test") row = self.cur.fetchone() self.assertEqual(row[0], sample) def CheckNumber1(self): self.cur.execute("insert into test(n1) values (5)") value = self.cur.execute("select n1 from test").fetchone()[0] # if the converter is not used, it's an int instead of a float self.assertEqual(type(value), float) def CheckNumber2(self): """Checks whether converter names are cut off at '(' characters""" self.cur.execute("insert into test(n2) values (5)") value = self.cur.execute("select n2 from test").fetchone()[0] # if the converter is not used, it's an int instead of a float self.assertEqual(type(value), float) class ColNamesTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_COLNAMES) self.cur = self.con.cursor() self.cur.execute("create table test(x foo)") sqlite.converters["FOO"] = lambda x: "[%s]" % x.decode("ascii") sqlite.converters["BAR"] = lambda x: "<%s>" % x.decode("ascii") sqlite.converters["EXC"] = lambda x: 5/0 sqlite.converters["B1B1"] = lambda x: "MARKER" def tearDown(self): del sqlite.converters["FOO"] del sqlite.converters["BAR"] del sqlite.converters["EXC"] del sqlite.converters["B1B1"] self.cur.close() self.con.close() def CheckDeclTypeNotUsed(self): """ Assures that the declared type is not used when PARSE_DECLTYPES is not set. """ self.cur.execute("insert into test(x) values (?)", ("xxx",)) self.cur.execute("select x from test") val = self.cur.fetchone()[0] self.assertEqual(val, "xxx") def CheckNone(self): self.cur.execute("insert into test(x) values (?)", (None,)) self.cur.execute("select x from test") val = self.cur.fetchone()[0] self.assertEqual(val, None) def CheckColName(self): self.cur.execute("insert into test(x) values (?)", ("xxx",)) self.cur.execute('select x as "x [bar]" from test') val = self.cur.fetchone()[0] self.assertEqual(val, "<xxx>") # Check if the stripping of colnames works. Everything after the first # whitespace should be stripped. self.assertEqual(self.cur.description[0][0], "x") def CheckCaseInConverterName(self): self.cur.execute("select 'other' as \"x [b1b1]\"") val = self.cur.fetchone()[0] self.assertEqual(val, "MARKER") def CheckCursorDescriptionNoRow(self): """ cursor.description should at least provide the column name(s), even if no row returned. """ self.cur.execute("select * from test where 0 = 1") self.assertEqual(self.cur.description[0][0], "x") def CheckCursorDescriptionInsert(self): self.cur.execute("insert into test values (1)") self.assertIsNone(self.cur.description) @unittest.skipIf(sqlite.sqlite_version_info < (3, 8, 3), "CTEs not supported") class CommonTableExpressionTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") self.cur = self.con.cursor() self.cur.execute("create table test(x foo)") def tearDown(self): self.cur.close() self.con.close() def CheckCursorDescriptionCTESimple(self): self.cur.execute("with one as (select 1) select * from one") self.assertIsNotNone(self.cur.description) self.assertEqual(self.cur.description[0][0], "1") def CheckCursorDescriptionCTESMultipleColumns(self): self.cur.execute("insert into test values(1)") self.cur.execute("insert into test values(2)") self.cur.execute("with testCTE as (select * from test) select * from testCTE") self.assertIsNotNone(self.cur.description) self.assertEqual(self.cur.description[0][0], "x") def CheckCursorDescriptionCTE(self): self.cur.execute("insert into test values (1)") self.cur.execute("with bar as (select * from test) select * from test where x = 1") self.assertIsNotNone(self.cur.description) self.assertEqual(self.cur.description[0][0], "x") self.cur.execute("with bar as (select * from test) select * from test where x = 2") self.assertIsNotNone(self.cur.description) self.assertEqual(self.cur.description[0][0], "x") class ObjectAdaptationTests(unittest.TestCase): def cast(obj): return float(obj) cast = staticmethod(cast) def setUp(self): self.con = sqlite.connect(":memory:") try: del sqlite.adapters[int] except: pass sqlite.register_adapter(int, ObjectAdaptationTests.cast) self.cur = self.con.cursor() def tearDown(self): del sqlite.adapters[(int, sqlite.PrepareProtocol)] self.cur.close() self.con.close() def CheckCasterIsUsed(self): self.cur.execute("select ?", (4,)) val = self.cur.fetchone()[0] self.assertEqual(type(val), float) @unittest.skipUnless(zlib, "requires zlib") class BinaryConverterTests(unittest.TestCase): def convert(s): return zlib.decompress(s) convert = staticmethod(convert) def setUp(self): self.con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_COLNAMES) sqlite.register_converter("bin", BinaryConverterTests.convert) def tearDown(self): self.con.close() def CheckBinaryInputForConverter(self): testdata = b"abcdefg" * 10 result = self.con.execute('select ? as "x [bin]"', (memoryview(zlib.compress(testdata)),)).fetchone()[0] self.assertEqual(testdata, result) class DateTimeTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES) self.cur = self.con.cursor() self.cur.execute("create table test(d date, ts timestamp)") def tearDown(self): self.cur.close() self.con.close() def CheckSqliteDate(self): d = sqlite.Date(2004, 2, 14) self.cur.execute("insert into test(d) values (?)", (d,)) self.cur.execute("select d from test") d2 = self.cur.fetchone()[0] self.assertEqual(d, d2) def CheckSqliteTimestamp(self): ts = sqlite.Timestamp(2004, 2, 14, 7, 15, 0) self.cur.execute("insert into test(ts) values (?)", (ts,)) self.cur.execute("select ts from test") ts2 = self.cur.fetchone()[0] self.assertEqual(ts, ts2) @unittest.skipIf(sqlite.sqlite_version_info < (3, 1), 'the date functions are available on 3.1 or later') def CheckSqlTimestamp(self): now = datetime.datetime.utcnow() self.cur.execute("insert into test(ts) values (current_timestamp)") self.cur.execute("select ts from test") ts = self.cur.fetchone()[0] self.assertEqual(type(ts), datetime.datetime) self.assertEqual(ts.year, now.year) def CheckDateTimeSubSeconds(self): ts = sqlite.Timestamp(2004, 2, 14, 7, 15, 0, 500000) self.cur.execute("insert into test(ts) values (?)", (ts,)) self.cur.execute("select ts from test") ts2 = self.cur.fetchone()[0] self.assertEqual(ts, ts2) def CheckDateTimeSubSecondsFloatingPoint(self): ts = sqlite.Timestamp(2004, 2, 14, 7, 15, 0, 510241) self.cur.execute("insert into test(ts) values (?)", (ts,)) self.cur.execute("select ts from test") ts2 = self.cur.fetchone()[0] self.assertEqual(ts, ts2) def suite(): sqlite_type_suite = unittest.makeSuite(SqliteTypeTests, "Check") decltypes_type_suite = unittest.makeSuite(DeclTypesTests, "Check") colnames_type_suite = unittest.makeSuite(ColNamesTests, "Check") adaptation_suite = unittest.makeSuite(ObjectAdaptationTests, "Check") bin_suite = unittest.makeSuite(BinaryConverterTests, "Check") date_suite = unittest.makeSuite(DateTimeTests, "Check") cte_suite = unittest.makeSuite(CommonTableExpressionTests, "Check") return unittest.TestSuite((sqlite_type_suite, decltypes_type_suite, colnames_type_suite, adaptation_suite, bin_suite, date_suite, cte_suite)) def test(): runner = unittest.TextTestRunner() runner.run(suite()) if __name__ == "__main__": test()
15,489
422
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/sqlite3/test/factory.py
#-*- coding: iso-8859-1 -*- # pysqlite2/test/factory.py: tests for the various factories in pysqlite # # Copyright (C) 2005-2007 Gerhard Häring <[email protected]> # # This file is part of pysqlite. # # This software is provided 'as-is', without any express or implied # warranty. In no event will the authors be held liable for any damages # arising from the use of this software. # # Permission is granted to anyone to use this software for any purpose, # including commercial applications, and to alter it and redistribute it # freely, subject to the following restrictions: # # 1. The origin of this software must not be misrepresented; you must not # claim that you wrote the original software. If you use this software # in a product, an acknowledgment in the product documentation would be # appreciated but is not required. # 2. Altered source versions must be plainly marked as such, and must not be # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. import unittest import sqlite3 as sqlite from collections.abc import Sequence class MyConnection(sqlite.Connection): def __init__(self, *args, **kwargs): sqlite.Connection.__init__(self, *args, **kwargs) def dict_factory(cursor, row): d = {} for idx, col in enumerate(cursor.description): d[col[0]] = row[idx] return d class MyCursor(sqlite.Cursor): def __init__(self, *args, **kwargs): sqlite.Cursor.__init__(self, *args, **kwargs) self.row_factory = dict_factory class ConnectionFactoryTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:", factory=MyConnection) def tearDown(self): self.con.close() def CheckIsInstance(self): self.assertIsInstance(self.con, MyConnection) class CursorFactoryTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") def tearDown(self): self.con.close() def CheckIsInstance(self): cur = self.con.cursor() self.assertIsInstance(cur, sqlite.Cursor) cur = self.con.cursor(MyCursor) self.assertIsInstance(cur, MyCursor) cur = self.con.cursor(factory=lambda con: MyCursor(con)) self.assertIsInstance(cur, MyCursor) def CheckInvalidFactory(self): # not a callable at all self.assertRaises(TypeError, self.con.cursor, None) # invalid callable with not exact one argument self.assertRaises(TypeError, self.con.cursor, lambda: None) # invalid callable returning non-cursor self.assertRaises(TypeError, self.con.cursor, lambda con: None) class RowFactoryTestsBackwardsCompat(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") def CheckIsProducedByFactory(self): cur = self.con.cursor(factory=MyCursor) cur.execute("select 4+5 as foo") row = cur.fetchone() self.assertIsInstance(row, dict) cur.close() def tearDown(self): self.con.close() class RowFactoryTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") def CheckCustomFactory(self): self.con.row_factory = lambda cur, row: list(row) row = self.con.execute("select 1, 2").fetchone() self.assertIsInstance(row, list) def CheckSqliteRowIndex(self): self.con.row_factory = sqlite.Row row = self.con.execute("select 1 as a, 2 as b").fetchone() self.assertIsInstance(row, sqlite.Row) col1, col2 = row["a"], row["b"] self.assertEqual(col1, 1, "by name: wrong result for column 'a'") self.assertEqual(col2, 2, "by name: wrong result for column 'a'") col1, col2 = row["A"], row["B"] self.assertEqual(col1, 1, "by name: wrong result for column 'A'") self.assertEqual(col2, 2, "by name: wrong result for column 'B'") self.assertEqual(row[0], 1, "by index: wrong result for column 0") self.assertEqual(row[1], 2, "by index: wrong result for column 1") self.assertEqual(row[-1], 2, "by index: wrong result for column -1") self.assertEqual(row[-2], 1, "by index: wrong result for column -2") with self.assertRaises(IndexError): row['c'] with self.assertRaises(IndexError): row[2] with self.assertRaises(IndexError): row[-3] with self.assertRaises(IndexError): row[2**1000] def CheckSqliteRowSlice(self): # A sqlite.Row can be sliced like a list. self.con.row_factory = sqlite.Row row = self.con.execute("select 1, 2, 3, 4").fetchone() self.assertEqual(row[0:0], ()) self.assertEqual(row[0:1], (1,)) self.assertEqual(row[1:3], (2, 3)) self.assertEqual(row[3:1], ()) # Explicit bounds are optional. self.assertEqual(row[1:], (2, 3, 4)) self.assertEqual(row[:3], (1, 2, 3)) # Slices can use negative indices. self.assertEqual(row[-2:-1], (3,)) self.assertEqual(row[-2:], (3, 4)) # Slicing supports steps. self.assertEqual(row[0:4:2], (1, 3)) self.assertEqual(row[3:0:-2], (4, 2)) def CheckSqliteRowIter(self): """Checks if the row object is iterable""" self.con.row_factory = sqlite.Row row = self.con.execute("select 1 as a, 2 as b").fetchone() for col in row: pass def CheckSqliteRowAsTuple(self): """Checks if the row object can be converted to a tuple""" self.con.row_factory = sqlite.Row row = self.con.execute("select 1 as a, 2 as b").fetchone() t = tuple(row) self.assertEqual(t, (row['a'], row['b'])) def CheckSqliteRowAsDict(self): """Checks if the row object can be correctly converted to a dictionary""" self.con.row_factory = sqlite.Row row = self.con.execute("select 1 as a, 2 as b").fetchone() d = dict(row) self.assertEqual(d["a"], row["a"]) self.assertEqual(d["b"], row["b"]) def CheckSqliteRowHashCmp(self): """Checks if the row object compares and hashes correctly""" self.con.row_factory = sqlite.Row row_1 = self.con.execute("select 1 as a, 2 as b").fetchone() row_2 = self.con.execute("select 1 as a, 2 as b").fetchone() row_3 = self.con.execute("select 1 as a, 3 as b").fetchone() self.assertEqual(row_1, row_1) self.assertEqual(row_1, row_2) self.assertTrue(row_2 != row_3) self.assertFalse(row_1 != row_1) self.assertFalse(row_1 != row_2) self.assertFalse(row_2 == row_3) self.assertEqual(row_1, row_2) self.assertEqual(hash(row_1), hash(row_2)) self.assertNotEqual(row_1, row_3) self.assertNotEqual(hash(row_1), hash(row_3)) def CheckSqliteRowAsSequence(self): """ Checks if the row object can act like a sequence """ self.con.row_factory = sqlite.Row row = self.con.execute("select 1 as a, 2 as b").fetchone() as_tuple = tuple(row) self.assertEqual(list(reversed(row)), list(reversed(as_tuple))) self.assertIsInstance(row, Sequence) def CheckFakeCursorClass(self): # Issue #24257: Incorrect use of PyObject_IsInstance() caused # segmentation fault. # Issue #27861: Also applies for cursor factory. class FakeCursor(str): __class__ = sqlite.Cursor self.con.row_factory = sqlite.Row self.assertRaises(TypeError, self.con.cursor, FakeCursor) self.assertRaises(TypeError, sqlite.Row, FakeCursor(), ()) def tearDown(self): self.con.close() class TextFactoryTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") def CheckUnicode(self): austria = "Österreich" row = self.con.execute("select ?", (austria,)).fetchone() self.assertEqual(type(row[0]), str, "type of row[0] must be unicode") def CheckString(self): self.con.text_factory = bytes austria = "Österreich" row = self.con.execute("select ?", (austria,)).fetchone() self.assertEqual(type(row[0]), bytes, "type of row[0] must be bytes") self.assertEqual(row[0], austria.encode("utf-8"), "column must equal original data in UTF-8") def CheckCustom(self): self.con.text_factory = lambda x: str(x, "utf-8", "ignore") austria = "Österreich" row = self.con.execute("select ?", (austria,)).fetchone() self.assertEqual(type(row[0]), str, "type of row[0] must be unicode") self.assertTrue(row[0].endswith("reich"), "column must contain original data") def CheckOptimizedUnicode(self): # In py3k, str objects are always returned when text_factory # is OptimizedUnicode self.con.text_factory = sqlite.OptimizedUnicode austria = "Österreich" germany = "Deutchland" a_row = self.con.execute("select ?", (austria,)).fetchone() d_row = self.con.execute("select ?", (germany,)).fetchone() self.assertEqual(type(a_row[0]), str, "type of non-ASCII row must be str") self.assertEqual(type(d_row[0]), str, "type of ASCII-only row must be str") def tearDown(self): self.con.close() class TextFactoryTestsWithEmbeddedZeroBytes(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") self.con.execute("create table test (value text)") self.con.execute("insert into test (value) values (?)", ("a\x00b",)) def CheckString(self): # text_factory defaults to str row = self.con.execute("select value from test").fetchone() self.assertIs(type(row[0]), str) self.assertEqual(row[0], "a\x00b") def CheckBytes(self): self.con.text_factory = bytes row = self.con.execute("select value from test").fetchone() self.assertIs(type(row[0]), bytes) self.assertEqual(row[0], b"a\x00b") def CheckBytearray(self): self.con.text_factory = bytearray row = self.con.execute("select value from test").fetchone() self.assertIs(type(row[0]), bytearray) self.assertEqual(row[0], b"a\x00b") def CheckCustom(self): # A custom factory should receive a bytes argument self.con.text_factory = lambda x: x row = self.con.execute("select value from test").fetchone() self.assertIs(type(row[0]), bytes) self.assertEqual(row[0], b"a\x00b") def tearDown(self): self.con.close() def suite(): connection_suite = unittest.makeSuite(ConnectionFactoryTests, "Check") cursor_suite = unittest.makeSuite(CursorFactoryTests, "Check") row_suite_compat = unittest.makeSuite(RowFactoryTestsBackwardsCompat, "Check") row_suite = unittest.makeSuite(RowFactoryTests, "Check") text_suite = unittest.makeSuite(TextFactoryTests, "Check") text_zero_bytes_suite = unittest.makeSuite(TextFactoryTestsWithEmbeddedZeroBytes, "Check") return unittest.TestSuite((connection_suite, cursor_suite, row_suite_compat, row_suite, text_suite, text_zero_bytes_suite)) def test(): runner = unittest.TextTestRunner() runner.run(suite()) if __name__ == "__main__": test()
11,350
294
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/sqlite3/test/hooks.py
#-*- coding: iso-8859-1 -*- # pysqlite2/test/hooks.py: tests for various SQLite-specific hooks # # Copyright (C) 2006-2007 Gerhard Häring <[email protected]> # # This file is part of pysqlite. # # This software is provided 'as-is', without any express or implied # warranty. In no event will the authors be held liable for any damages # arising from the use of this software. # # Permission is granted to anyone to use this software for any purpose, # including commercial applications, and to alter it and redistribute it # freely, subject to the following restrictions: # # 1. The origin of this software must not be misrepresented; you must not # claim that you wrote the original software. If you use this software # in a product, an acknowledgment in the product documentation would be # appreciated but is not required. # 2. Altered source versions must be plainly marked as such, and must not be # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. import unittest import sqlite3 as sqlite class CollationTests(unittest.TestCase): def CheckCreateCollationNotString(self): con = sqlite.connect(":memory:") with self.assertRaises(TypeError): con.create_collation(None, lambda x, y: (x > y) - (x < y)) def CheckCreateCollationNotCallable(self): con = sqlite.connect(":memory:") with self.assertRaises(TypeError) as cm: con.create_collation("X", 42) self.assertEqual(str(cm.exception), 'parameter must be callable') def CheckCreateCollationNotAscii(self): con = sqlite.connect(":memory:") with self.assertRaises(sqlite.ProgrammingError): con.create_collation("collä", lambda x, y: (x > y) - (x < y)) def CheckCreateCollationBadUpper(self): class BadUpperStr(str): def upper(self): return None con = sqlite.connect(":memory:") mycoll = lambda x, y: -((x > y) - (x < y)) con.create_collation(BadUpperStr("mycoll"), mycoll) result = con.execute(""" select x from ( select 'a' as x union select 'b' as x ) order by x collate mycoll """).fetchall() self.assertEqual(result[0][0], 'b') self.assertEqual(result[1][0], 'a') @unittest.skipIf(sqlite.sqlite_version_info < (3, 2, 1), 'old SQLite versions crash on this test') def CheckCollationIsUsed(self): def mycoll(x, y): # reverse order return -((x > y) - (x < y)) con = sqlite.connect(":memory:") con.create_collation("mycoll", mycoll) sql = """ select x from ( select 'a' as x union select 'b' as x union select 'c' as x ) order by x collate mycoll """ result = con.execute(sql).fetchall() self.assertEqual(result, [('c',), ('b',), ('a',)], msg='the expected order was not returned') con.create_collation("mycoll", None) with self.assertRaises(sqlite.OperationalError) as cm: result = con.execute(sql).fetchall() self.assertEqual(str(cm.exception), 'no such collation sequence: mycoll') def CheckCollationReturnsLargeInteger(self): def mycoll(x, y): # reverse order return -((x > y) - (x < y)) * 2**32 con = sqlite.connect(":memory:") con.create_collation("mycoll", mycoll) sql = """ select x from ( select 'a' as x union select 'b' as x union select 'c' as x ) order by x collate mycoll """ result = con.execute(sql).fetchall() self.assertEqual(result, [('c',), ('b',), ('a',)], msg="the expected order was not returned") def CheckCollationRegisterTwice(self): """ Register two different collation functions under the same name. Verify that the last one is actually used. """ con = sqlite.connect(":memory:") con.create_collation("mycoll", lambda x, y: (x > y) - (x < y)) con.create_collation("mycoll", lambda x, y: -((x > y) - (x < y))) result = con.execute(""" select x from (select 'a' as x union select 'b' as x) order by x collate mycoll """).fetchall() self.assertEqual(result[0][0], 'b') self.assertEqual(result[1][0], 'a') def CheckDeregisterCollation(self): """ Register a collation, then deregister it. Make sure an error is raised if we try to use it. """ con = sqlite.connect(":memory:") con.create_collation("mycoll", lambda x, y: (x > y) - (x < y)) con.create_collation("mycoll", None) with self.assertRaises(sqlite.OperationalError) as cm: con.execute("select 'a' as x union select 'b' as x order by x collate mycoll") self.assertEqual(str(cm.exception), 'no such collation sequence: mycoll') class ProgressTests(unittest.TestCase): def CheckProgressHandlerUsed(self): """ Test that the progress handler is invoked once it is set. """ con = sqlite.connect(":memory:") progress_calls = [] def progress(): progress_calls.append(None) return 0 con.set_progress_handler(progress, 1) con.execute(""" create table foo(a, b) """) self.assertTrue(progress_calls) def CheckOpcodeCount(self): """ Test that the opcode argument is respected. """ con = sqlite.connect(":memory:") progress_calls = [] def progress(): progress_calls.append(None) return 0 con.set_progress_handler(progress, 1) curs = con.cursor() curs.execute(""" create table foo (a, b) """) first_count = len(progress_calls) progress_calls = [] con.set_progress_handler(progress, 2) curs.execute(""" create table bar (a, b) """) second_count = len(progress_calls) self.assertGreaterEqual(first_count, second_count) def CheckCancelOperation(self): """ Test that returning a non-zero value stops the operation in progress. """ con = sqlite.connect(":memory:") progress_calls = [] def progress(): progress_calls.append(None) return 1 con.set_progress_handler(progress, 1) curs = con.cursor() self.assertRaises( sqlite.OperationalError, curs.execute, "create table bar (a, b)") def CheckClearHandler(self): """ Test that setting the progress handler to None clears the previously set handler. """ con = sqlite.connect(":memory:") action = 0 def progress(): nonlocal action action = 1 return 0 con.set_progress_handler(progress, 1) con.set_progress_handler(None, 1) con.execute("select 1 union select 2 union select 3").fetchall() self.assertEqual(action, 0, "progress handler was not cleared") class TraceCallbackTests(unittest.TestCase): def CheckTraceCallbackUsed(self): """ Test that the trace callback is invoked once it is set. """ con = sqlite.connect(":memory:") traced_statements = [] def trace(statement): traced_statements.append(statement) con.set_trace_callback(trace) con.execute("create table foo(a, b)") self.assertTrue(traced_statements) self.assertTrue(any("create table foo" in stmt for stmt in traced_statements)) def CheckClearTraceCallback(self): """ Test that setting the trace callback to None clears the previously set callback. """ con = sqlite.connect(":memory:") traced_statements = [] def trace(statement): traced_statements.append(statement) con.set_trace_callback(trace) con.set_trace_callback(None) con.execute("create table foo(a, b)") self.assertFalse(traced_statements, "trace callback was not cleared") def CheckUnicodeContent(self): """ Test that the statement can contain unicode literals. """ unicode_value = '\xf6\xe4\xfc\xd6\xc4\xdc\xdf\u20ac' con = sqlite.connect(":memory:") traced_statements = [] def trace(statement): traced_statements.append(statement) con.set_trace_callback(trace) con.execute("create table foo(x)") # Can't execute bound parameters as their values don't appear # in traced statements before SQLite 3.6.21 # (cf. http://www.sqlite.org/draft/releaselog/3_6_21.html) con.execute('insert into foo(x) values ("%s")' % unicode_value) con.commit() self.assertTrue(any(unicode_value in stmt for stmt in traced_statements), "Unicode data %s garbled in trace callback: %s" % (ascii(unicode_value), ', '.join(map(ascii, traced_statements)))) def suite(): collation_suite = unittest.makeSuite(CollationTests, "Check") progress_suite = unittest.makeSuite(ProgressTests, "Check") trace_suite = unittest.makeSuite(TraceCallbackTests, "Check") return unittest.TestSuite((collation_suite, progress_suite, trace_suite)) def test(): runner = unittest.TextTestRunner() runner.run(suite()) if __name__ == "__main__": test()
9,784
265
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/sqlite3/test/userfunctions.py
#-*- coding: iso-8859-1 -*- # pysqlite2/test/userfunctions.py: tests for user-defined functions and # aggregates. # # Copyright (C) 2005-2007 Gerhard Häring <[email protected]> # # This file is part of pysqlite. # # This software is provided 'as-is', without any express or implied # warranty. In no event will the authors be held liable for any damages # arising from the use of this software. # # Permission is granted to anyone to use this software for any purpose, # including commercial applications, and to alter it and redistribute it # freely, subject to the following restrictions: # # 1. The origin of this software must not be misrepresented; you must not # claim that you wrote the original software. If you use this software # in a product, an acknowledgment in the product documentation would be # appreciated but is not required. # 2. Altered source versions must be plainly marked as such, and must not be # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. import unittest import sqlite3 as sqlite def func_returntext(): return "foo" def func_returnunicode(): return "bar" def func_returnint(): return 42 def func_returnfloat(): return 3.14 def func_returnnull(): return None def func_returnblob(): return b"blob" def func_returnlonglong(): return 1<<31 def func_raiseexception(): 5/0 def func_isstring(v): return type(v) is str def func_isint(v): return type(v) is int def func_isfloat(v): return type(v) is float def func_isnone(v): return type(v) is type(None) def func_isblob(v): return isinstance(v, (bytes, memoryview)) def func_islonglong(v): return isinstance(v, int) and v >= 1<<31 def func(*args): return len(args) class AggrNoStep: def __init__(self): pass def finalize(self): return 1 class AggrNoFinalize: def __init__(self): pass def step(self, x): pass class AggrExceptionInInit: def __init__(self): 5/0 def step(self, x): pass def finalize(self): pass class AggrExceptionInStep: def __init__(self): pass def step(self, x): 5/0 def finalize(self): return 42 class AggrExceptionInFinalize: def __init__(self): pass def step(self, x): pass def finalize(self): 5/0 class AggrCheckType: def __init__(self): self.val = None def step(self, whichType, val): theType = {"str": str, "int": int, "float": float, "None": type(None), "blob": bytes} self.val = int(theType[whichType] is type(val)) def finalize(self): return self.val class AggrCheckTypes: def __init__(self): self.val = 0 def step(self, whichType, *vals): theType = {"str": str, "int": int, "float": float, "None": type(None), "blob": bytes} for val in vals: self.val += int(theType[whichType] is type(val)) def finalize(self): return self.val class AggrSum: def __init__(self): self.val = 0.0 def step(self, val): self.val += val def finalize(self): return self.val class FunctionTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") self.con.create_function("returntext", 0, func_returntext) self.con.create_function("returnunicode", 0, func_returnunicode) self.con.create_function("returnint", 0, func_returnint) self.con.create_function("returnfloat", 0, func_returnfloat) self.con.create_function("returnnull", 0, func_returnnull) self.con.create_function("returnblob", 0, func_returnblob) self.con.create_function("returnlonglong", 0, func_returnlonglong) self.con.create_function("raiseexception", 0, func_raiseexception) self.con.create_function("isstring", 1, func_isstring) self.con.create_function("isint", 1, func_isint) self.con.create_function("isfloat", 1, func_isfloat) self.con.create_function("isnone", 1, func_isnone) self.con.create_function("isblob", 1, func_isblob) self.con.create_function("islonglong", 1, func_islonglong) self.con.create_function("spam", -1, func) def tearDown(self): self.con.close() def CheckFuncErrorOnCreate(self): with self.assertRaises(sqlite.OperationalError): self.con.create_function("bla", -100, lambda x: 2*x) def CheckFuncRefCount(self): def getfunc(): def f(): return 1 return f f = getfunc() globals()["foo"] = f # self.con.create_function("reftest", 0, getfunc()) self.con.create_function("reftest", 0, f) cur = self.con.cursor() cur.execute("select reftest()") def CheckFuncReturnText(self): cur = self.con.cursor() cur.execute("select returntext()") val = cur.fetchone()[0] self.assertEqual(type(val), str) self.assertEqual(val, "foo") def CheckFuncReturnUnicode(self): cur = self.con.cursor() cur.execute("select returnunicode()") val = cur.fetchone()[0] self.assertEqual(type(val), str) self.assertEqual(val, "bar") def CheckFuncReturnInt(self): cur = self.con.cursor() cur.execute("select returnint()") val = cur.fetchone()[0] self.assertEqual(type(val), int) self.assertEqual(val, 42) def CheckFuncReturnFloat(self): cur = self.con.cursor() cur.execute("select returnfloat()") val = cur.fetchone()[0] self.assertEqual(type(val), float) if val < 3.139 or val > 3.141: self.fail("wrong value") def CheckFuncReturnNull(self): cur = self.con.cursor() cur.execute("select returnnull()") val = cur.fetchone()[0] self.assertEqual(type(val), type(None)) self.assertEqual(val, None) def CheckFuncReturnBlob(self): cur = self.con.cursor() cur.execute("select returnblob()") val = cur.fetchone()[0] self.assertEqual(type(val), bytes) self.assertEqual(val, b"blob") def CheckFuncReturnLongLong(self): cur = self.con.cursor() cur.execute("select returnlonglong()") val = cur.fetchone()[0] self.assertEqual(val, 1<<31) def CheckFuncException(self): cur = self.con.cursor() with self.assertRaises(sqlite.OperationalError) as cm: cur.execute("select raiseexception()") cur.fetchone() self.assertEqual(str(cm.exception), 'user-defined function raised exception') def CheckParamString(self): cur = self.con.cursor() cur.execute("select isstring(?)", ("foo",)) val = cur.fetchone()[0] self.assertEqual(val, 1) def CheckParamInt(self): cur = self.con.cursor() cur.execute("select isint(?)", (42,)) val = cur.fetchone()[0] self.assertEqual(val, 1) def CheckParamFloat(self): cur = self.con.cursor() cur.execute("select isfloat(?)", (3.14,)) val = cur.fetchone()[0] self.assertEqual(val, 1) def CheckParamNone(self): cur = self.con.cursor() cur.execute("select isnone(?)", (None,)) val = cur.fetchone()[0] self.assertEqual(val, 1) def CheckParamBlob(self): cur = self.con.cursor() cur.execute("select isblob(?)", (memoryview(b"blob"),)) val = cur.fetchone()[0] self.assertEqual(val, 1) def CheckParamLongLong(self): cur = self.con.cursor() cur.execute("select islonglong(?)", (1<<42,)) val = cur.fetchone()[0] self.assertEqual(val, 1) def CheckAnyArguments(self): cur = self.con.cursor() cur.execute("select spam(?, ?)", (1, 2)) val = cur.fetchone()[0] self.assertEqual(val, 2) class AggregateTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") cur = self.con.cursor() cur.execute(""" create table test( t text, i integer, f float, n, b blob ) """) cur.execute("insert into test(t, i, f, n, b) values (?, ?, ?, ?, ?)", ("foo", 5, 3.14, None, memoryview(b"blob"),)) self.con.create_aggregate("nostep", 1, AggrNoStep) self.con.create_aggregate("nofinalize", 1, AggrNoFinalize) self.con.create_aggregate("excInit", 1, AggrExceptionInInit) self.con.create_aggregate("excStep", 1, AggrExceptionInStep) self.con.create_aggregate("excFinalize", 1, AggrExceptionInFinalize) self.con.create_aggregate("checkType", 2, AggrCheckType) self.con.create_aggregate("checkTypes", -1, AggrCheckTypes) self.con.create_aggregate("mysum", 1, AggrSum) def tearDown(self): #self.cur.close() #self.con.close() pass def CheckAggrErrorOnCreate(self): with self.assertRaises(sqlite.OperationalError): self.con.create_function("bla", -100, AggrSum) def CheckAggrNoStep(self): cur = self.con.cursor() with self.assertRaises(AttributeError) as cm: cur.execute("select nostep(t) from test") self.assertEqual(str(cm.exception), "'AggrNoStep' object has no attribute 'step'") def CheckAggrNoFinalize(self): cur = self.con.cursor() with self.assertRaises(sqlite.OperationalError) as cm: cur.execute("select nofinalize(t) from test") val = cur.fetchone()[0] self.assertEqual(str(cm.exception), "user-defined aggregate's 'finalize' method raised error") def CheckAggrExceptionInInit(self): cur = self.con.cursor() with self.assertRaises(sqlite.OperationalError) as cm: cur.execute("select excInit(t) from test") val = cur.fetchone()[0] self.assertEqual(str(cm.exception), "user-defined aggregate's '__init__' method raised error") def CheckAggrExceptionInStep(self): cur = self.con.cursor() with self.assertRaises(sqlite.OperationalError) as cm: cur.execute("select excStep(t) from test") val = cur.fetchone()[0] self.assertEqual(str(cm.exception), "user-defined aggregate's 'step' method raised error") def CheckAggrExceptionInFinalize(self): cur = self.con.cursor() with self.assertRaises(sqlite.OperationalError) as cm: cur.execute("select excFinalize(t) from test") val = cur.fetchone()[0] self.assertEqual(str(cm.exception), "user-defined aggregate's 'finalize' method raised error") def CheckAggrCheckParamStr(self): cur = self.con.cursor() cur.execute("select checkType('str', ?)", ("foo",)) val = cur.fetchone()[0] self.assertEqual(val, 1) def CheckAggrCheckParamInt(self): cur = self.con.cursor() cur.execute("select checkType('int', ?)", (42,)) val = cur.fetchone()[0] self.assertEqual(val, 1) def CheckAggrCheckParamsInt(self): cur = self.con.cursor() cur.execute("select checkTypes('int', ?, ?)", (42, 24)) val = cur.fetchone()[0] self.assertEqual(val, 2) def CheckAggrCheckParamFloat(self): cur = self.con.cursor() cur.execute("select checkType('float', ?)", (3.14,)) val = cur.fetchone()[0] self.assertEqual(val, 1) def CheckAggrCheckParamNone(self): cur = self.con.cursor() cur.execute("select checkType('None', ?)", (None,)) val = cur.fetchone()[0] self.assertEqual(val, 1) def CheckAggrCheckParamBlob(self): cur = self.con.cursor() cur.execute("select checkType('blob', ?)", (memoryview(b"blob"),)) val = cur.fetchone()[0] self.assertEqual(val, 1) def CheckAggrCheckAggrSum(self): cur = self.con.cursor() cur.execute("delete from test") cur.executemany("insert into test(i) values (?)", [(10,), (20,), (30,)]) cur.execute("select mysum(i) from test") val = cur.fetchone()[0] self.assertEqual(val, 60) class AuthorizerTests(unittest.TestCase): @staticmethod def authorizer_cb(action, arg1, arg2, dbname, source): if action != sqlite.SQLITE_SELECT: return sqlite.SQLITE_DENY if arg2 == 'c2' or arg1 == 't2': return sqlite.SQLITE_DENY return sqlite.SQLITE_OK def setUp(self): self.con = sqlite.connect(":memory:") self.con.executescript(""" create table t1 (c1, c2); create table t2 (c1, c2); insert into t1 (c1, c2) values (1, 2); insert into t2 (c1, c2) values (4, 5); """) # For our security test: self.con.execute("select c2 from t2") self.con.set_authorizer(self.authorizer_cb) def tearDown(self): pass def test_table_access(self): with self.assertRaises(sqlite.DatabaseError) as cm: self.con.execute("select * from t2") self.assertIn('prohibited', str(cm.exception)) def test_column_access(self): with self.assertRaises(sqlite.DatabaseError) as cm: self.con.execute("select c2 from t1") self.assertIn('prohibited', str(cm.exception)) class AuthorizerRaiseExceptionTests(AuthorizerTests): @staticmethod def authorizer_cb(action, arg1, arg2, dbname, source): if action != sqlite.SQLITE_SELECT: raise ValueError if arg2 == 'c2' or arg1 == 't2': raise ValueError return sqlite.SQLITE_OK class AuthorizerIllegalTypeTests(AuthorizerTests): @staticmethod def authorizer_cb(action, arg1, arg2, dbname, source): if action != sqlite.SQLITE_SELECT: return 0.0 if arg2 == 'c2' or arg1 == 't2': return 0.0 return sqlite.SQLITE_OK class AuthorizerLargeIntegerTests(AuthorizerTests): @staticmethod def authorizer_cb(action, arg1, arg2, dbname, source): if action != sqlite.SQLITE_SELECT: return 2**32 if arg2 == 'c2' or arg1 == 't2': return 2**32 return sqlite.SQLITE_OK def suite(): function_suite = unittest.makeSuite(FunctionTests, "Check") aggregate_suite = unittest.makeSuite(AggregateTests, "Check") authorizer_suite = unittest.makeSuite(AuthorizerTests) return unittest.TestSuite(( function_suite, aggregate_suite, authorizer_suite, unittest.makeSuite(AuthorizerRaiseExceptionTests), unittest.makeSuite(AuthorizerIllegalTypeTests), unittest.makeSuite(AuthorizerLargeIntegerTests), )) def test(): runner = unittest.TextTestRunner() runner.run(suite()) if __name__ == "__main__": test()
15,125
474
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/sqlite3/test/transactions.py
#-*- coding: iso-8859-1 -*- # pysqlite2/test/transactions.py: tests transactions # # Copyright (C) 2005-2007 Gerhard Häring <[email protected]> # # This file is part of pysqlite. # # This software is provided 'as-is', without any express or implied # warranty. In no event will the authors be held liable for any damages # arising from the use of this software. # # Permission is granted to anyone to use this software for any purpose, # including commercial applications, and to alter it and redistribute it # freely, subject to the following restrictions: # # 1. The origin of this software must not be misrepresented; you must not # claim that you wrote the original software. If you use this software # in a product, an acknowledgment in the product documentation would be # appreciated but is not required. # 2. Altered source versions must be plainly marked as such, and must not be # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. import os, unittest import sqlite3 as sqlite def get_db_path(): return "sqlite_testdb" class TransactionTests(unittest.TestCase): def setUp(self): try: os.remove(get_db_path()) except OSError: pass self.con1 = sqlite.connect(get_db_path(), timeout=0.1) self.cur1 = self.con1.cursor() self.con2 = sqlite.connect(get_db_path(), timeout=0.1) self.cur2 = self.con2.cursor() def tearDown(self): self.cur1.close() self.con1.close() self.cur2.close() self.con2.close() try: os.unlink(get_db_path()) except OSError: pass def CheckDMLDoesNotAutoCommitBefore(self): self.cur1.execute("create table test(i)") self.cur1.execute("insert into test(i) values (5)") self.cur1.execute("create table test2(j)") self.cur2.execute("select i from test") res = self.cur2.fetchall() self.assertEqual(len(res), 0) def CheckInsertStartsTransaction(self): self.cur1.execute("create table test(i)") self.cur1.execute("insert into test(i) values (5)") self.cur2.execute("select i from test") res = self.cur2.fetchall() self.assertEqual(len(res), 0) def CheckUpdateStartsTransaction(self): self.cur1.execute("create table test(i)") self.cur1.execute("insert into test(i) values (5)") self.con1.commit() self.cur1.execute("update test set i=6") self.cur2.execute("select i from test") res = self.cur2.fetchone()[0] self.assertEqual(res, 5) def CheckDeleteStartsTransaction(self): self.cur1.execute("create table test(i)") self.cur1.execute("insert into test(i) values (5)") self.con1.commit() self.cur1.execute("delete from test") self.cur2.execute("select i from test") res = self.cur2.fetchall() self.assertEqual(len(res), 1) def CheckReplaceStartsTransaction(self): self.cur1.execute("create table test(i)") self.cur1.execute("insert into test(i) values (5)") self.con1.commit() self.cur1.execute("replace into test(i) values (6)") self.cur2.execute("select i from test") res = self.cur2.fetchall() self.assertEqual(len(res), 1) self.assertEqual(res[0][0], 5) def CheckToggleAutoCommit(self): self.cur1.execute("create table test(i)") self.cur1.execute("insert into test(i) values (5)") self.con1.isolation_level = None self.assertEqual(self.con1.isolation_level, None) self.cur2.execute("select i from test") res = self.cur2.fetchall() self.assertEqual(len(res), 1) self.con1.isolation_level = "DEFERRED" self.assertEqual(self.con1.isolation_level , "DEFERRED") self.cur1.execute("insert into test(i) values (5)") self.cur2.execute("select i from test") res = self.cur2.fetchall() self.assertEqual(len(res), 1) @unittest.skipIf(sqlite.sqlite_version_info < (3, 2, 2), 'test hangs on sqlite versions older than 3.2.2') def CheckRaiseTimeout(self): self.cur1.execute("create table test(i)") self.cur1.execute("insert into test(i) values (5)") with self.assertRaises(sqlite.OperationalError): self.cur2.execute("insert into test(i) values (5)") @unittest.skipIf(sqlite.sqlite_version_info < (3, 2, 2), 'test hangs on sqlite versions older than 3.2.2') def CheckLocking(self): """ This tests the improved concurrency with pysqlite 2.3.4. You needed to roll back con2 before you could commit con1. """ self.cur1.execute("create table test(i)") self.cur1.execute("insert into test(i) values (5)") with self.assertRaises(sqlite.OperationalError): self.cur2.execute("insert into test(i) values (5)") # NO self.con2.rollback() HERE!!! self.con1.commit() def CheckRollbackCursorConsistency(self): """ Checks if cursors on the connection are set into a "reset" state when a rollback is done on the connection. """ con = sqlite.connect(":memory:") cur = con.cursor() cur.execute("create table test(x)") cur.execute("insert into test(x) values (5)") cur.execute("select 1 union select 2 union select 3") con.rollback() with self.assertRaises(sqlite.InterfaceError): cur.fetchall() class SpecialCommandTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") self.cur = self.con.cursor() def CheckDropTable(self): self.cur.execute("create table test(i)") self.cur.execute("insert into test(i) values (5)") self.cur.execute("drop table test") def CheckPragma(self): self.cur.execute("create table test(i)") self.cur.execute("insert into test(i) values (5)") self.cur.execute("pragma count_changes=1") def tearDown(self): self.cur.close() self.con.close() class TransactionalDDL(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") def CheckDdlDoesNotAutostartTransaction(self): # For backwards compatibility reasons, DDL statements should not # implicitly start a transaction. self.con.execute("create table test(i)") self.con.rollback() result = self.con.execute("select * from test").fetchall() self.assertEqual(result, []) def CheckImmediateTransactionalDDL(self): # You can achieve transactional DDL by issuing a BEGIN # statement manually. self.con.execute("begin immediate") self.con.execute("create table test(i)") self.con.rollback() with self.assertRaises(sqlite.OperationalError): self.con.execute("select * from test") def CheckTransactionalDDL(self): # You can achieve transactional DDL by issuing a BEGIN # statement manually. self.con.execute("begin") self.con.execute("create table test(i)") self.con.rollback() with self.assertRaises(sqlite.OperationalError): self.con.execute("select * from test") def tearDown(self): self.con.close() def suite(): default_suite = unittest.makeSuite(TransactionTests, "Check") special_command_suite = unittest.makeSuite(SpecialCommandTests, "Check") ddl_suite = unittest.makeSuite(TransactionalDDL, "Check") return unittest.TestSuite((default_suite, special_command_suite, ddl_suite)) def test(): runner = unittest.TextTestRunner() runner.run(suite()) if __name__ == "__main__": test()
7,855
215
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/sqlite3/test/__init__.py
0
1
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/Grammar.txt
# Grammar for 2to3. This grammar supports Python 2.x and 3.x. # NOTE WELL: You should also follow all the steps listed at # https://devguide.python.org/grammar/ # Start symbols for the grammar: # file_input is a module or sequence of commands read from an input file; # single_input is a single interactive statement; # eval_input is the input for the eval() and input() functions. # NB: compound_stmt in single_input is followed by extra NEWLINE! file_input: (NEWLINE | stmt)* ENDMARKER single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE eval_input: testlist NEWLINE* ENDMARKER decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE decorators: decorator+ decorated: decorators (classdef | funcdef | async_funcdef) async_funcdef: ASYNC funcdef funcdef: 'def' NAME parameters ['->' test] ':' suite parameters: '(' [typedargslist] ')' typedargslist: ((tfpdef ['=' test] ',')* ('*' [tname] (',' tname ['=' test])* [',' ['**' tname [',']]] | '**' tname [',']) | tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) tname: NAME [':' test] tfpdef: tname | '(' tfplist ')' tfplist: tfpdef (',' tfpdef)* [','] varargslist: ((vfpdef ['=' test] ',')* ('*' [vname] (',' vname ['=' test])* [',' ['**' vname [',']]] | '**' vname [',']) | vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) vname: NAME vfpdef: vname | '(' vfplist ')' vfplist: vfpdef (',' vfpdef)* [','] stmt: simple_stmt | compound_stmt simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | exec_stmt | assert_stmt) expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | ('=' (yield_expr|testlist_star_expr))*) annassign: ':' test ['=' test] testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=' | '**=' | '//=') # For normal and annotated assignments, additional restrictions enforced by the interpreter print_stmt: 'print' ( [ test (',' test)* [','] ] | '>>' test [ (',' test)+ [','] ] ) del_stmt: 'del' exprlist pass_stmt: 'pass' flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt break_stmt: 'break' continue_stmt: 'continue' return_stmt: 'return' [testlist] yield_stmt: yield_expr raise_stmt: 'raise' [test ['from' test | ',' test [',' test]]] import_stmt: import_name | import_from import_name: 'import' dotted_as_names import_from: ('from' ('.'* dotted_name | '.'+) 'import' ('*' | '(' import_as_names ')' | import_as_names)) import_as_name: NAME ['as' NAME] dotted_as_name: dotted_name ['as' NAME] import_as_names: import_as_name (',' import_as_name)* [','] dotted_as_names: dotted_as_name (',' dotted_as_name)* dotted_name: NAME ('.' NAME)* global_stmt: ('global' | 'nonlocal') NAME (',' NAME)* exec_stmt: 'exec' expr ['in' test [',' test]] assert_stmt: 'assert' test [',' test] compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt async_stmt: ASYNC (funcdef | with_stmt | for_stmt) if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] while_stmt: 'while' test ':' suite ['else' ':' suite] for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] try_stmt: ('try' ':' suite ((except_clause ':' suite)+ ['else' ':' suite] ['finally' ':' suite] | 'finally' ':' suite)) with_stmt: 'with' with_item (',' with_item)* ':' suite with_item: test ['as' expr] with_var: 'as' expr # NB compile.c makes sure that the default except clause is last except_clause: 'except' [test [(',' | 'as') test]] suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT # Backward compatibility cruft to support: # [ x for x in lambda: True, lambda: False if x() ] # even while also allowing: # lambda x: 5 if x else 2 # (But not a mix of the two) testlist_safe: old_test [(',' old_test)+ [',']] old_test: or_test | old_lambdef old_lambdef: 'lambda' [varargslist] ':' old_test test: or_test ['if' or_test 'else' test] | lambdef or_test: and_test ('or' and_test)* and_test: not_test ('and' not_test)* not_test: 'not' not_test | comparison comparison: expr (comp_op expr)* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' star_expr: '*' expr expr: xor_expr ('|' xor_expr)* xor_expr: and_expr ('^' and_expr)* and_expr: shift_expr ('&' shift_expr)* shift_expr: arith_expr (('<<'|'>>') arith_expr)* arith_expr: term (('+'|'-') term)* term: factor (('*'|'@'|'/'|'%'|'//') factor)* factor: ('+'|'-'|'~') factor | power power: [AWAIT] atom trailer* ['**' factor] atom: ('(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']' | '{' [dictsetmaker] '}' | '`' testlist1 '`' | NAME | NUMBER | STRING+ | '.' '.' '.') listmaker: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] ) testlist_gexp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] ) lambdef: 'lambda' [varargslist] ':' test trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME subscriptlist: subscript (',' subscript)* [','] subscript: test | [test] ':' [test] [sliceop] sliceop: ':' [test] exprlist: (expr|star_expr) (',' (expr|star_expr))* [','] testlist: test (',' test)* [','] dictsetmaker: ( ((test ':' test | '**' expr) (comp_for | (',' (test ':' test | '**' expr))* [','])) | ((test | star_expr) (comp_for | (',' (test | star_expr))* [','])) ) classdef: 'class' NAME ['(' [arglist] ')'] ':' suite arglist: argument (',' argument)* [','] # "test '=' test" is really "keyword '=' test", but we have no such token. # These need to be in a single rule to avoid grammar that is ambiguous # to our LL(1) parser. Even though 'test' includes '*expr' in star_expr, # we explicitly match '*' here, too, to give it proper precedence. # Illegal combinations and orderings are blocked in ast.c: # multiple (test comp_for) arguments are blocked; keyword unpackings # that precede iterable unpackings are blocked; etc. argument: ( test [comp_for] | test '=' test | '**' expr | star_expr ) comp_iter: comp_for | comp_if comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [comp_iter] comp_if: 'if' old_test [comp_iter] testlist1: test (',' test)* # not used in grammar, but may appear in "node" passed from Parser to Compiler encoding_decl: NAME yield_expr: 'yield' [yield_arg] yield_arg: 'from' test | testlist
6,562
155
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/pytree.py
# Copyright 2006 Google, Inc. All Rights Reserved. # Licensed to PSF under a Contributor Agreement. """ Python parse tree definitions. This is a very concrete parse tree; we need to keep every token and even the comments and whitespace between tokens. There's also a pattern matching implementation here. """ __author__ = "Guido van Rossum <[email protected]>" import sys import warnings from io import StringIO HUGE = 0x7FFFFFFF # maximum repeat count, default max _type_reprs = {} def type_repr(type_num): global _type_reprs if not _type_reprs: from .pygram import python_symbols # printing tokens is possible but not as useful # from .pgen2 import token // token.__dict__.items(): for name, val in python_symbols.__dict__.items(): if type(val) == int: _type_reprs[val] = name return _type_reprs.setdefault(type_num, type_num) class Base(object): """ Abstract base class for Node and Leaf. This provides some default functionality and boilerplate using the template pattern. A node may be a subnode of at most one parent. """ # Default values for instance variables type = None # int: token number (< 256) or symbol number (>= 256) parent = None # Parent node pointer, or None children = () # Tuple of subnodes was_changed = False was_checked = False def __new__(cls, *args, **kwds): """Constructor that prevents Base from being instantiated.""" assert cls is not Base, "Cannot instantiate Base" return object.__new__(cls) def __eq__(self, other): """ Compare two nodes for equality. This calls the method _eq(). """ if self.__class__ is not other.__class__: return NotImplemented return self._eq(other) __hash__ = None # For Py3 compatibility. def _eq(self, other): """ Compare two nodes for equality. This is called by __eq__ and __ne__. It is only called if the two nodes have the same type. This must be implemented by the concrete subclass. Nodes should be considered equal if they have the same structure, ignoring the prefix string and other context information. """ raise NotImplementedError def clone(self): """ Return a cloned (deep) copy of self. This must be implemented by the concrete subclass. """ raise NotImplementedError def post_order(self): """ Return a post-order iterator for the tree. This must be implemented by the concrete subclass. """ raise NotImplementedError def pre_order(self): """ Return a pre-order iterator for the tree. This must be implemented by the concrete subclass. """ raise NotImplementedError def replace(self, new): """Replace this node with a new one in the parent.""" assert self.parent is not None, str(self) assert new is not None if not isinstance(new, list): new = [new] l_children = [] found = False for ch in self.parent.children: if ch is self: assert not found, (self.parent.children, self, new) if new is not None: l_children.extend(new) found = True else: l_children.append(ch) assert found, (self.children, self, new) self.parent.changed() self.parent.children = l_children for x in new: x.parent = self.parent self.parent = None def get_lineno(self): """Return the line number which generated the invocant node.""" node = self while not isinstance(node, Leaf): if not node.children: return node = node.children[0] return node.lineno def changed(self): if self.parent: self.parent.changed() self.was_changed = True def remove(self): """ Remove the node from the tree. Returns the position of the node in its parent's children before it was removed. """ if self.parent: for i, node in enumerate(self.parent.children): if node is self: self.parent.changed() del self.parent.children[i] self.parent = None return i @property def next_sibling(self): """ The node immediately following the invocant in their parent's children list. If the invocant does not have a next sibling, it is None """ if self.parent is None: return None # Can't use index(); we need to test by identity for i, child in enumerate(self.parent.children): if child is self: try: return self.parent.children[i+1] except IndexError: return None @property def prev_sibling(self): """ The node immediately preceding the invocant in their parent's children list. If the invocant does not have a previous sibling, it is None. """ if self.parent is None: return None # Can't use index(); we need to test by identity for i, child in enumerate(self.parent.children): if child is self: if i == 0: return None return self.parent.children[i-1] def leaves(self): for child in self.children: yield from child.leaves() def depth(self): if self.parent is None: return 0 return 1 + self.parent.depth() def get_suffix(self): """ Return the string immediately following the invocant node. This is effectively equivalent to node.next_sibling.prefix """ next_sib = self.next_sibling if next_sib is None: return "" return next_sib.prefix if sys.version_info < (3, 0): def __str__(self): return str(self).encode("ascii") class Node(Base): """Concrete implementation for interior nodes.""" def __init__(self,type, children, context=None, prefix=None, fixers_applied=None): """ Initializer. Takes a type constant (a symbol number >= 256), a sequence of child nodes, and an optional context keyword argument. As a side effect, the parent pointers of the children are updated. """ assert type >= 256, type self.type = type self.children = list(children) for ch in self.children: assert ch.parent is None, repr(ch) ch.parent = self if prefix is not None: self.prefix = prefix if fixers_applied: self.fixers_applied = fixers_applied[:] else: self.fixers_applied = None def __repr__(self): """Return a canonical string representation.""" return "%s(%s, %r)" % (self.__class__.__name__, type_repr(self.type), self.children) def __unicode__(self): """ Return a pretty string representation. This reproduces the input source exactly. """ return "".join(map(str, self.children)) if sys.version_info > (3, 0): __str__ = __unicode__ def _eq(self, other): """Compare two nodes for equality.""" return (self.type, self.children) == (other.type, other.children) def clone(self): """Return a cloned (deep) copy of self.""" return Node(self.type, [ch.clone() for ch in self.children], fixers_applied=self.fixers_applied) def post_order(self): """Return a post-order iterator for the tree.""" for child in self.children: yield from child.post_order() yield self def pre_order(self): """Return a pre-order iterator for the tree.""" yield self for child in self.children: yield from child.pre_order() def _prefix_getter(self): """ The whitespace and comments preceding this node in the input. """ if not self.children: return "" return self.children[0].prefix def _prefix_setter(self, prefix): if self.children: self.children[0].prefix = prefix prefix = property(_prefix_getter, _prefix_setter) def set_child(self, i, child): """ Equivalent to 'node.children[i] = child'. This method also sets the child's parent attribute appropriately. """ child.parent = self self.children[i].parent = None self.children[i] = child self.changed() def insert_child(self, i, child): """ Equivalent to 'node.children.insert(i, child)'. This method also sets the child's parent attribute appropriately. """ child.parent = self self.children.insert(i, child) self.changed() def append_child(self, child): """ Equivalent to 'node.children.append(child)'. This method also sets the child's parent attribute appropriately. """ child.parent = self self.children.append(child) self.changed() class Leaf(Base): """Concrete implementation for leaf nodes.""" # Default values for instance variables _prefix = "" # Whitespace and comments preceding this token in the input lineno = 0 # Line where this token starts in the input column = 0 # Column where this token tarts in the input def __init__(self, type, value, context=None, prefix=None, fixers_applied=[]): """ Initializer. Takes a type constant (a token number < 256), a string value, and an optional context keyword argument. """ assert 0 <= type < 256, type if context is not None: self._prefix, (self.lineno, self.column) = context self.type = type self.value = value if prefix is not None: self._prefix = prefix self.fixers_applied = fixers_applied[:] def __repr__(self): """Return a canonical string representation.""" return "%s(%r, %r)" % (self.__class__.__name__, self.type, self.value) def __unicode__(self): """ Return a pretty string representation. This reproduces the input source exactly. """ return self.prefix + str(self.value) if sys.version_info > (3, 0): __str__ = __unicode__ def _eq(self, other): """Compare two nodes for equality.""" return (self.type, self.value) == (other.type, other.value) def clone(self): """Return a cloned (deep) copy of self.""" return Leaf(self.type, self.value, (self.prefix, (self.lineno, self.column)), fixers_applied=self.fixers_applied) def leaves(self): yield self def post_order(self): """Return a post-order iterator for the tree.""" yield self def pre_order(self): """Return a pre-order iterator for the tree.""" yield self def _prefix_getter(self): """ The whitespace and comments preceding this token in the input. """ return self._prefix def _prefix_setter(self, prefix): self.changed() self._prefix = prefix prefix = property(_prefix_getter, _prefix_setter) def convert(gr, raw_node): """ Convert raw node information to a Node or Leaf instance. This is passed to the parser driver which calls it whenever a reduction of a grammar rule produces a new complete node, so that the tree is build strictly bottom-up. """ type, value, context, children = raw_node if children or type in gr.number2symbol: # If there's exactly one child, return that child instead of # creating a new node. if len(children) == 1: return children[0] return Node(type, children, context=context) else: return Leaf(type, value, context=context) class BasePattern(object): """ A pattern is a tree matching pattern. It looks for a specific node type (token or symbol), and optionally for a specific content. This is an abstract base class. There are three concrete subclasses: - LeafPattern matches a single leaf node; - NodePattern matches a single node (usually non-leaf); - WildcardPattern matches a sequence of nodes of variable length. """ # Defaults for instance variables type = None # Node type (token if < 256, symbol if >= 256) content = None # Optional content matching pattern name = None # Optional name used to store match in results dict def __new__(cls, *args, **kwds): """Constructor that prevents BasePattern from being instantiated.""" assert cls is not BasePattern, "Cannot instantiate BasePattern" return object.__new__(cls) def __repr__(self): args = [type_repr(self.type), self.content, self.name] while args and args[-1] is None: del args[-1] return "%s(%s)" % (self.__class__.__name__, ", ".join(map(repr, args))) def optimize(self): """ A subclass can define this as a hook for optimizations. Returns either self or another node with the same effect. """ return self def match(self, node, results=None): """ Does this pattern exactly match a node? Returns True if it matches, False if not. If results is not None, it must be a dict which will be updated with the nodes matching named subpatterns. Default implementation for non-wildcard patterns. """ if self.type is not None and node.type != self.type: return False if self.content is not None: r = None if results is not None: r = {} if not self._submatch(node, r): return False if r: results.update(r) if results is not None and self.name: results[self.name] = node return True def match_seq(self, nodes, results=None): """ Does this pattern exactly match a sequence of nodes? Default implementation for non-wildcard patterns. """ if len(nodes) != 1: return False return self.match(nodes[0], results) def generate_matches(self, nodes): """ Generator yielding all matches for this pattern. Default implementation for non-wildcard patterns. """ r = {} if nodes and self.match(nodes[0], r): yield 1, r class LeafPattern(BasePattern): def __init__(self, type=None, content=None, name=None): """ Initializer. Takes optional type, content, and name. The type, if given must be a token type (< 256). If not given, this matches any *leaf* node; the content may still be required. The content, if given, must be a string. If a name is given, the matching node is stored in the results dict under that key. """ if type is not None: assert 0 <= type < 256, type if content is not None: assert isinstance(content, str), repr(content) self.type = type self.content = content self.name = name def match(self, node, results=None): """Override match() to insist on a leaf node.""" if not isinstance(node, Leaf): return False return BasePattern.match(self, node, results) def _submatch(self, node, results=None): """ Match the pattern's content to the node's children. This assumes the node type matches and self.content is not None. Returns True if it matches, False if not. If results is not None, it must be a dict which will be updated with the nodes matching named subpatterns. When returning False, the results dict may still be updated. """ return self.content == node.value class NodePattern(BasePattern): wildcards = False def __init__(self, type=None, content=None, name=None): """ Initializer. Takes optional type, content, and name. The type, if given, must be a symbol type (>= 256). If the type is None this matches *any* single node (leaf or not), except if content is not None, in which it only matches non-leaf nodes that also match the content pattern. The content, if not None, must be a sequence of Patterns that must match the node's children exactly. If the content is given, the type must not be None. If a name is given, the matching node is stored in the results dict under that key. """ if type is not None: assert type >= 256, type if content is not None: assert not isinstance(content, str), repr(content) content = list(content) for i, item in enumerate(content): assert isinstance(item, BasePattern), (i, item) if isinstance(item, WildcardPattern): self.wildcards = True self.type = type self.content = content self.name = name def _submatch(self, node, results=None): """ Match the pattern's content to the node's children. This assumes the node type matches and self.content is not None. Returns True if it matches, False if not. If results is not None, it must be a dict which will be updated with the nodes matching named subpatterns. When returning False, the results dict may still be updated. """ if self.wildcards: for c, r in generate_matches(self.content, node.children): if c == len(node.children): if results is not None: results.update(r) return True return False if len(self.content) != len(node.children): return False for subpattern, child in zip(self.content, node.children): if not subpattern.match(child, results): return False return True class WildcardPattern(BasePattern): """ A wildcard pattern can match zero or more nodes. This has all the flexibility needed to implement patterns like: .* .+ .? .{m,n} (a b c | d e | f) (...)* (...)+ (...)? (...){m,n} except it always uses non-greedy matching. """ def __init__(self, content=None, min=0, max=HUGE, name=None): """ Initializer. Args: content: optional sequence of subsequences of patterns; if absent, matches one node; if present, each subsequence is an alternative [*] min: optional minimum number of times to match, default 0 max: optional maximum number of times to match, default HUGE name: optional name assigned to this match [*] Thus, if content is [[a, b, c], [d, e], [f, g, h]] this is equivalent to (a b c | d e | f g h); if content is None, this is equivalent to '.' in regular expression terms. The min and max parameters work as follows: min=0, max=maxint: .* min=1, max=maxint: .+ min=0, max=1: .? min=1, max=1: . If content is not None, replace the dot with the parenthesized list of alternatives, e.g. (a b c | d e | f g h)* """ assert 0 <= min <= max <= HUGE, (min, max) if content is not None: content = tuple(map(tuple, content)) # Protect against alterations # Check sanity of alternatives assert len(content), repr(content) # Can't have zero alternatives for alt in content: assert len(alt), repr(alt) # Can have empty alternatives self.content = content self.min = min self.max = max self.name = name def optimize(self): """Optimize certain stacked wildcard patterns.""" subpattern = None if (self.content is not None and len(self.content) == 1 and len(self.content[0]) == 1): subpattern = self.content[0][0] if self.min == 1 and self.max == 1: if self.content is None: return NodePattern(name=self.name) if subpattern is not None and self.name == subpattern.name: return subpattern.optimize() if (self.min <= 1 and isinstance(subpattern, WildcardPattern) and subpattern.min <= 1 and self.name == subpattern.name): return WildcardPattern(subpattern.content, self.min*subpattern.min, self.max*subpattern.max, subpattern.name) return self def match(self, node, results=None): """Does this pattern exactly match a node?""" return self.match_seq([node], results) def match_seq(self, nodes, results=None): """Does this pattern exactly match a sequence of nodes?""" for c, r in self.generate_matches(nodes): if c == len(nodes): if results is not None: results.update(r) if self.name: results[self.name] = list(nodes) return True return False def generate_matches(self, nodes): """ Generator yielding matches for a sequence of nodes. Args: nodes: sequence of nodes Yields: (count, results) tuples where: count: the match comprises nodes[:count]; results: dict containing named submatches. """ if self.content is None: # Shortcut for special case (see __init__.__doc__) for count in range(self.min, 1 + min(len(nodes), self.max)): r = {} if self.name: r[self.name] = nodes[:count] yield count, r elif self.name == "bare_name": yield self._bare_name_matches(nodes) else: # The reason for this is that hitting the recursion limit usually # results in some ugly messages about how RuntimeErrors are being # ignored. We only have to do this on CPython, though, because other # implementations don't have this nasty bug in the first place. if hasattr(sys, "getrefcount"): save_stderr = sys.stderr sys.stderr = StringIO() try: for count, r in self._recursive_matches(nodes, 0): if self.name: r[self.name] = nodes[:count] yield count, r except RuntimeError: # We fall back to the iterative pattern matching scheme if the recursive # scheme hits the recursion limit. for count, r in self._iterative_matches(nodes): if self.name: r[self.name] = nodes[:count] yield count, r finally: if hasattr(sys, "getrefcount"): sys.stderr = save_stderr def _iterative_matches(self, nodes): """Helper to iteratively yield the matches.""" nodelen = len(nodes) if 0 >= self.min: yield 0, {} results = [] # generate matches that use just one alt from self.content for alt in self.content: for c, r in generate_matches(alt, nodes): yield c, r results.append((c, r)) # for each match, iterate down the nodes while results: new_results = [] for c0, r0 in results: # stop if the entire set of nodes has been matched if c0 < nodelen and c0 <= self.max: for alt in self.content: for c1, r1 in generate_matches(alt, nodes[c0:]): if c1 > 0: r = {} r.update(r0) r.update(r1) yield c0 + c1, r new_results.append((c0 + c1, r)) results = new_results def _bare_name_matches(self, nodes): """Special optimized matcher for bare_name.""" count = 0 r = {} done = False max = len(nodes) while not done and count < max: done = True for leaf in self.content: if leaf[0].match(nodes[count], r): count += 1 done = False break r[self.name] = nodes[:count] return count, r def _recursive_matches(self, nodes, count): """Helper to recursively yield the matches.""" assert self.content is not None if count >= self.min: yield 0, {} if count < self.max: for alt in self.content: for c0, r0 in generate_matches(alt, nodes): for c1, r1 in self._recursive_matches(nodes[c0:], count+1): r = {} r.update(r0) r.update(r1) yield c0 + c1, r class NegatedPattern(BasePattern): def __init__(self, content=None): """ Initializer. The argument is either a pattern or None. If it is None, this only matches an empty sequence (effectively '$' in regex lingo). If it is not None, this matches whenever the argument pattern doesn't have any matches. """ if content is not None: assert isinstance(content, BasePattern), repr(content) self.content = content def match(self, node): # We never match a node in its entirety return False def match_seq(self, nodes): # We only match an empty sequence of nodes in its entirety return len(nodes) == 0 def generate_matches(self, nodes): if self.content is None: # Return a match if there is an empty sequence if len(nodes) == 0: yield 0, {} else: # Return a match if the argument pattern has no matches for c, r in self.content.generate_matches(nodes): return yield 0, {} def generate_matches(patterns, nodes): """ Generator yielding matches for a sequence of patterns and nodes. Args: patterns: a sequence of patterns nodes: a sequence of nodes Yields: (count, results) tuples where: count: the entire sequence of patterns matches nodes[:count]; results: dict containing named submatches. """ if not patterns: yield 0, {} else: p, rest = patterns[0], patterns[1:] for c0, r0 in p.generate_matches(nodes): if not rest: yield c0, r0 else: for c1, r1 in generate_matches(rest, nodes[c0:]): r = {} r.update(r0) r.update(r1) yield c0 + c1, r
28,052
855
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/fixer_util.py
"""Utility functions, node construction macros, etc.""" # Author: Collin Winter # Local imports from .pgen2 import token from .pytree import Leaf, Node from .pygram import python_symbols as syms from . import patcomp ########################################################### ### Common node-construction "macros" ########################################################### def KeywordArg(keyword, value): return Node(syms.argument, [keyword, Leaf(token.EQUAL, "="), value]) def LParen(): return Leaf(token.LPAR, "(") def RParen(): return Leaf(token.RPAR, ")") def Assign(target, source): """Build an assignment statement""" if not isinstance(target, list): target = [target] if not isinstance(source, list): source.prefix = " " source = [source] return Node(syms.atom, target + [Leaf(token.EQUAL, "=", prefix=" ")] + source) def Name(name, prefix=None): """Return a NAME leaf""" return Leaf(token.NAME, name, prefix=prefix) def Attr(obj, attr): """A node tuple for obj.attr""" return [obj, Node(syms.trailer, [Dot(), attr])] def Comma(): """A comma leaf""" return Leaf(token.COMMA, ",") def Dot(): """A period (.) leaf""" return Leaf(token.DOT, ".") def ArgList(args, lparen=LParen(), rparen=RParen()): """A parenthesised argument list, used by Call()""" node = Node(syms.trailer, [lparen.clone(), rparen.clone()]) if args: node.insert_child(1, Node(syms.arglist, args)) return node def Call(func_name, args=None, prefix=None): """A function call""" node = Node(syms.power, [func_name, ArgList(args)]) if prefix is not None: node.prefix = prefix return node def Newline(): """A newline literal""" return Leaf(token.NEWLINE, "\n") def BlankLine(): """A blank line""" return Leaf(token.NEWLINE, "") def Number(n, prefix=None): return Leaf(token.NUMBER, n, prefix=prefix) def Subscript(index_node): """A numeric or string subscript""" return Node(syms.trailer, [Leaf(token.LBRACE, "["), index_node, Leaf(token.RBRACE, "]")]) def String(string, prefix=None): """A string leaf""" return Leaf(token.STRING, string, prefix=prefix) def ListComp(xp, fp, it, test=None): """A list comprehension of the form [xp for fp in it if test]. If test is None, the "if test" part is omitted. """ xp.prefix = "" fp.prefix = " " it.prefix = " " for_leaf = Leaf(token.NAME, "for") for_leaf.prefix = " " in_leaf = Leaf(token.NAME, "in") in_leaf.prefix = " " inner_args = [for_leaf, fp, in_leaf, it] if test: test.prefix = " " if_leaf = Leaf(token.NAME, "if") if_leaf.prefix = " " inner_args.append(Node(syms.comp_if, [if_leaf, test])) inner = Node(syms.listmaker, [xp, Node(syms.comp_for, inner_args)]) return Node(syms.atom, [Leaf(token.LBRACE, "["), inner, Leaf(token.RBRACE, "]")]) def FromImport(package_name, name_leafs): """ Return an import statement in the form: from package import name_leafs""" # XXX: May not handle dotted imports properly (eg, package_name='foo.bar') #assert package_name == '.' or '.' not in package_name, "FromImport has "\ # "not been tested with dotted package names -- use at your own "\ # "peril!" for leaf in name_leafs: # Pull the leaves out of their old tree leaf.remove() children = [Leaf(token.NAME, "from"), Leaf(token.NAME, package_name, prefix=" "), Leaf(token.NAME, "import", prefix=" "), Node(syms.import_as_names, name_leafs)] imp = Node(syms.import_from, children) return imp def ImportAndCall(node, results, names): """Returns an import statement and calls a method of the module: import module module.name()""" obj = results["obj"].clone() if obj.type == syms.arglist: newarglist = obj.clone() else: newarglist = Node(syms.arglist, [obj.clone()]) after = results["after"] if after: after = [n.clone() for n in after] new = Node(syms.power, Attr(Name(names[0]), Name(names[1])) + [Node(syms.trailer, [results["lpar"].clone(), newarglist, results["rpar"].clone()])] + after) new.prefix = node.prefix return new ########################################################### ### Determine whether a node represents a given literal ########################################################### def is_tuple(node): """Does the node represent a tuple literal?""" if isinstance(node, Node) and node.children == [LParen(), RParen()]: return True return (isinstance(node, Node) and len(node.children) == 3 and isinstance(node.children[0], Leaf) and isinstance(node.children[1], Node) and isinstance(node.children[2], Leaf) and node.children[0].value == "(" and node.children[2].value == ")") def is_list(node): """Does the node represent a list literal?""" return (isinstance(node, Node) and len(node.children) > 1 and isinstance(node.children[0], Leaf) and isinstance(node.children[-1], Leaf) and node.children[0].value == "[" and node.children[-1].value == "]") ########################################################### ### Misc ########################################################### def parenthesize(node): return Node(syms.atom, [LParen(), node, RParen()]) consuming_calls = {"sorted", "list", "set", "any", "all", "tuple", "sum", "min", "max", "enumerate"} def attr_chain(obj, attr): """Follow an attribute chain. If you have a chain of objects where a.foo -> b, b.foo-> c, etc, use this to iterate over all objects in the chain. Iteration is terminated by getattr(x, attr) is None. Args: obj: the starting object attr: the name of the chaining attribute Yields: Each successive object in the chain. """ next = getattr(obj, attr) while next: yield next next = getattr(next, attr) p0 = """for_stmt< 'for' any 'in' node=any ':' any* > | comp_for< 'for' any 'in' node=any any* > """ p1 = """ power< ( 'iter' | 'list' | 'tuple' | 'sorted' | 'set' | 'sum' | 'any' | 'all' | 'enumerate' | (any* trailer< '.' 'join' >) ) trailer< '(' node=any ')' > any* > """ p2 = """ power< ( 'sorted' | 'enumerate' ) trailer< '(' arglist<node=any any*> ')' > any* > """ pats_built = False def in_special_context(node): """ Returns true if node is in an environment where all that is required of it is being iterable (ie, it doesn't matter if it returns a list or an iterator). See test_map_nochange in test_fixers.py for some examples and tests. """ global p0, p1, p2, pats_built if not pats_built: p0 = patcomp.compile_pattern(p0) p1 = patcomp.compile_pattern(p1) p2 = patcomp.compile_pattern(p2) pats_built = True patterns = [p0, p1, p2] for pattern, parent in zip(patterns, attr_chain(node, "parent")): results = {} if pattern.match(parent, results) and results["node"] is node: return True return False def is_probably_builtin(node): """ Check that something isn't an attribute or function name etc. """ prev = node.prev_sibling if prev is not None and prev.type == token.DOT: # Attribute lookup. return False parent = node.parent if parent.type in (syms.funcdef, syms.classdef): return False if parent.type == syms.expr_stmt and parent.children[0] is node: # Assignment. return False if parent.type == syms.parameters or \ (parent.type == syms.typedargslist and ( (prev is not None and prev.type == token.COMMA) or parent.children[0] is node )): # The name of an argument. return False return True def find_indentation(node): """Find the indentation of *node*.""" while node is not None: if node.type == syms.suite and len(node.children) > 2: indent = node.children[1] if indent.type == token.INDENT: return indent.value node = node.parent return "" ########################################################### ### The following functions are to find bindings in a suite ########################################################### def make_suite(node): if node.type == syms.suite: return node node = node.clone() parent, node.parent = node.parent, None suite = Node(syms.suite, [node]) suite.parent = parent return suite def find_root(node): """Find the top level namespace.""" # Scamper up to the top level namespace while node.type != syms.file_input: node = node.parent if not node: raise ValueError("root found before file_input node was found.") return node def does_tree_import(package, name, node): """ Returns true if name is imported from package at the top level of the tree which node belongs to. To cover the case of an import like 'import foo', use None for the package and 'foo' for the name. """ binding = find_binding(name, find_root(node), package) return bool(binding) def is_import(node): """Returns true if the node is an import statement.""" return node.type in (syms.import_name, syms.import_from) def touch_import(package, name, node): """ Works like `does_tree_import` but adds an import statement if it was not imported. """ def is_import_stmt(node): return (node.type == syms.simple_stmt and node.children and is_import(node.children[0])) root = find_root(node) if does_tree_import(package, name, root): return # figure out where to insert the new import. First try to find # the first import and then skip to the last one. insert_pos = offset = 0 for idx, node in enumerate(root.children): if not is_import_stmt(node): continue for offset, node2 in enumerate(root.children[idx:]): if not is_import_stmt(node2): break insert_pos = idx + offset break # if there are no imports where we can insert, find the docstring. # if that also fails, we stick to the beginning of the file if insert_pos == 0: for idx, node in enumerate(root.children): if (node.type == syms.simple_stmt and node.children and node.children[0].type == token.STRING): insert_pos = idx + 1 break if package is None: import_ = Node(syms.import_name, [ Leaf(token.NAME, "import"), Leaf(token.NAME, name, prefix=" ") ]) else: import_ = FromImport(package, [Leaf(token.NAME, name, prefix=" ")]) children = [import_, Newline()] root.insert_child(insert_pos, Node(syms.simple_stmt, children)) _def_syms = {syms.classdef, syms.funcdef} def find_binding(name, node, package=None): """ Returns the node which binds variable name, otherwise None. If optional argument package is supplied, only imports will be returned. See test cases for examples.""" for child in node.children: ret = None if child.type == syms.for_stmt: if _find(name, child.children[1]): return child n = find_binding(name, make_suite(child.children[-1]), package) if n: ret = n elif child.type in (syms.if_stmt, syms.while_stmt): n = find_binding(name, make_suite(child.children[-1]), package) if n: ret = n elif child.type == syms.try_stmt: n = find_binding(name, make_suite(child.children[2]), package) if n: ret = n else: for i, kid in enumerate(child.children[3:]): if kid.type == token.COLON and kid.value == ":": # i+3 is the colon, i+4 is the suite n = find_binding(name, make_suite(child.children[i+4]), package) if n: ret = n elif child.type in _def_syms and child.children[1].value == name: ret = child elif _is_import_binding(child, name, package): ret = child elif child.type == syms.simple_stmt: ret = find_binding(name, child, package) elif child.type == syms.expr_stmt: if _find(name, child.children[0]): ret = child if ret: if not package: return ret if is_import(ret): return ret return None _block_syms = {syms.funcdef, syms.classdef, syms.trailer} def _find(name, node): nodes = [node] while nodes: node = nodes.pop() if node.type > 256 and node.type not in _block_syms: nodes.extend(node.children) elif node.type == token.NAME and node.value == name: return node return None def _is_import_binding(node, name, package=None): """ Will reuturn node if node will import name, or node will import * from package. None is returned otherwise. See test cases for examples. """ if node.type == syms.import_name and not package: imp = node.children[1] if imp.type == syms.dotted_as_names: for child in imp.children: if child.type == syms.dotted_as_name: if child.children[2].value == name: return node elif child.type == token.NAME and child.value == name: return node elif imp.type == syms.dotted_as_name: last = imp.children[-1] if last.type == token.NAME and last.value == name: return node elif imp.type == token.NAME and imp.value == name: return node elif node.type == syms.import_from: # str(...) is used to make life easier here, because # from a.b import parses to ['import', ['a', '.', 'b'], ...] if package and str(node.children[1]).strip() != package: return None n = node.children[3] if package and _find("as", n): # See test_from_import_as for explanation return None elif n.type == syms.import_as_names and _find(name, n): return node elif n.type == syms.import_as_name: child = n.children[2] if child.type == token.NAME and child.value == name: return node elif n.type == token.NAME and n.value == name: return node elif package and n.type == token.STAR: return node return None
15,207
454
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/PatternGrammar.txt
# Copyright 2006 Google, Inc. All Rights Reserved. # Licensed to PSF under a Contributor Agreement. # A grammar to describe tree matching patterns. # Not shown here: # - 'TOKEN' stands for any token (leaf node) # - 'any' stands for any node (leaf or interior) # With 'any' we can still specify the sub-structure. # The start symbol is 'Matcher'. Matcher: Alternatives ENDMARKER Alternatives: Alternative ('|' Alternative)* Alternative: (Unit | NegatedUnit)+ Unit: [NAME '='] ( STRING [Repeater] | NAME [Details] [Repeater] | '(' Alternatives ')' [Repeater] | '[' Alternatives ']' ) NegatedUnit: 'not' (STRING | NAME [Details] | '(' Alternatives ')') Repeater: '*' | '+' | '{' NUMBER [',' NUMBER] '}' Details: '<' Alternatives '>'
793
29
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/__main__.py
import sys from .main import main sys.exit(main("lib2to3.fixes"))
67
5
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/main.py
""" Main program for 2to3. """ from __future__ import with_statement, print_function import sys import os import difflib import logging import shutil import optparse from . import refactor def diff_texts(a, b, filename): """Return a unified diff of two strings.""" a = a.splitlines() b = b.splitlines() return difflib.unified_diff(a, b, filename, filename, "(original)", "(refactored)", lineterm="") class StdoutRefactoringTool(refactor.MultiprocessRefactoringTool): """ A refactoring tool that can avoid overwriting its input files. Prints output to stdout. Output files can optionally be written to a different directory and or have an extra file suffix appended to their name for use in situations where you do not want to replace the input files. """ def __init__(self, fixers, options, explicit, nobackups, show_diffs, input_base_dir='', output_dir='', append_suffix=''): """ Args: fixers: A list of fixers to import. options: A dict with RefactoringTool configuration. explicit: A list of fixers to run even if they are explicit. nobackups: If true no backup '.bak' files will be created for those files that are being refactored. show_diffs: Should diffs of the refactoring be printed to stdout? input_base_dir: The base directory for all input files. This class will strip this path prefix off of filenames before substituting it with output_dir. Only meaningful if output_dir is supplied. All files processed by refactor() must start with this path. output_dir: If supplied, all converted files will be written into this directory tree instead of input_base_dir. append_suffix: If supplied, all files output by this tool will have this appended to their filename. Useful for changing .py to .py3 for example by passing append_suffix='3'. """ self.nobackups = nobackups self.show_diffs = show_diffs if input_base_dir and not input_base_dir.endswith(os.sep): input_base_dir += os.sep self._input_base_dir = input_base_dir self._output_dir = output_dir self._append_suffix = append_suffix super(StdoutRefactoringTool, self).__init__(fixers, options, explicit) def log_error(self, msg, *args, **kwargs): self.errors.append((msg, args, kwargs)) self.logger.error(msg, *args, **kwargs) def write_file(self, new_text, filename, old_text, encoding): orig_filename = filename if self._output_dir: if filename.startswith(self._input_base_dir): filename = os.path.join(self._output_dir, filename[len(self._input_base_dir):]) else: raise ValueError('filename %s does not start with the ' 'input_base_dir %s' % ( filename, self._input_base_dir)) if self._append_suffix: filename += self._append_suffix if orig_filename != filename: output_dir = os.path.dirname(filename) if not os.path.isdir(output_dir) and output_dir: os.makedirs(output_dir) self.log_message('Writing converted %s to %s.', orig_filename, filename) if not self.nobackups: # Make backup backup = filename + ".bak" if os.path.lexists(backup): try: os.remove(backup) except OSError as err: self.log_message("Can't remove backup %s", backup) try: os.rename(filename, backup) except OSError as err: self.log_message("Can't rename %s to %s", filename, backup) # Actually write the new file write = super(StdoutRefactoringTool, self).write_file write(new_text, filename, old_text, encoding) if not self.nobackups: shutil.copymode(backup, filename) if orig_filename != filename: # Preserve the file mode in the new output directory. shutil.copymode(orig_filename, filename) def print_output(self, old, new, filename, equal): if equal: self.log_message("No changes to %s", filename) else: self.log_message("Refactored %s", filename) if self.show_diffs: diff_lines = diff_texts(old, new, filename) try: if self.output_lock is not None: with self.output_lock: for line in diff_lines: print(line) sys.stdout.flush() else: for line in diff_lines: print(line) except UnicodeEncodeError: warn("couldn't encode %s's diff for your terminal" % (filename,)) return def warn(msg): print("WARNING: %s" % (msg,), file=sys.stderr) def main(fixer_pkg, args=None): """Main program. Args: fixer_pkg: the name of a package where the fixers are located. args: optional; a list of command line arguments. If omitted, sys.argv[1:] is used. Returns a suggested exit status (0, 1, 2). """ # Set up option parser parser = optparse.OptionParser(usage="2to3 [options] file|dir ...") parser.add_option("-d", "--doctests_only", action="store_true", help="Fix up doctests only") parser.add_option("-f", "--fix", action="append", default=[], help="Each FIX specifies a transformation; default: all") parser.add_option("-j", "--processes", action="store", default=1, type="int", help="Run 2to3 concurrently") parser.add_option("-x", "--nofix", action="append", default=[], help="Prevent a transformation from being run") parser.add_option("-l", "--list-fixes", action="store_true", help="List available transformations") parser.add_option("-p", "--print-function", action="store_true", help="Modify the grammar so that print() is a function") parser.add_option("-v", "--verbose", action="store_true", help="More verbose logging") parser.add_option("--no-diffs", action="store_true", help="Don't show diffs of the refactoring") parser.add_option("-w", "--write", action="store_true", help="Write back modified files") parser.add_option("-n", "--nobackups", action="store_true", default=False, help="Don't write backups for modified files") parser.add_option("-o", "--output-dir", action="store", type="str", default="", help="Put output files in this directory " "instead of overwriting the input files. Requires -n.") parser.add_option("-W", "--write-unchanged-files", action="store_true", help="Also write files even if no changes were required" " (useful with --output-dir); implies -w.") parser.add_option("--add-suffix", action="store", type="str", default="", help="Append this string to all output filenames." " Requires -n if non-empty. " "ex: --add-suffix='3' will generate .py3 files.") # Parse command line arguments refactor_stdin = False flags = {} options, args = parser.parse_args(args) if options.write_unchanged_files: flags["write_unchanged_files"] = True if not options.write: warn("--write-unchanged-files/-W implies -w.") options.write = True # If we allowed these, the original files would be renamed to backup names # but not replaced. if options.output_dir and not options.nobackups: parser.error("Can't use --output-dir/-o without -n.") if options.add_suffix and not options.nobackups: parser.error("Can't use --add-suffix without -n.") if not options.write and options.no_diffs: warn("not writing files and not printing diffs; that's not very useful") if not options.write and options.nobackups: parser.error("Can't use -n without -w") if options.list_fixes: print("Available transformations for the -f/--fix option:") for fixname in refactor.get_all_fix_names(fixer_pkg): print(fixname) if not args: return 0 if not args: print("At least one file or directory argument required.", file=sys.stderr) print("Use --help to show usage.", file=sys.stderr) return 2 if "-" in args: refactor_stdin = True if options.write: print("Can't write to stdin.", file=sys.stderr) return 2 if options.print_function: flags["print_function"] = True # Set up logging handler level = logging.DEBUG if options.verbose else logging.INFO logging.basicConfig(format='%(name)s: %(message)s', level=level) logger = logging.getLogger('lib2to3.main') # Initialize the refactoring tool avail_fixes = set(refactor.get_fixers_from_package(fixer_pkg)) unwanted_fixes = set(fixer_pkg + ".fix_" + fix for fix in options.nofix) explicit = set() if options.fix: all_present = False for fix in options.fix: if fix == "all": all_present = True else: explicit.add(fixer_pkg + ".fix_" + fix) requested = avail_fixes.union(explicit) if all_present else explicit else: requested = avail_fixes.union(explicit) fixer_names = requested.difference(unwanted_fixes) input_base_dir = os.path.commonprefix(args) if (input_base_dir and not input_base_dir.endswith(os.sep) and not os.path.isdir(input_base_dir)): # One or more similar names were passed, their directory is the base. # os.path.commonprefix() is ignorant of path elements, this corrects # for that weird API. input_base_dir = os.path.dirname(input_base_dir) if options.output_dir: input_base_dir = input_base_dir.rstrip(os.sep) logger.info('Output in %r will mirror the input directory %r layout.', options.output_dir, input_base_dir) rt = StdoutRefactoringTool( sorted(fixer_names), flags, sorted(explicit), options.nobackups, not options.no_diffs, input_base_dir=input_base_dir, output_dir=options.output_dir, append_suffix=options.add_suffix) # Refactor all files and directories passed as arguments if not rt.errors: if refactor_stdin: rt.refactor_stdin() else: try: rt.refactor(args, options.write, options.doctests_only, options.processes) except refactor.MultiprocessingUnsupported: assert options.processes > 1 print("Sorry, -j isn't supported on this platform.", file=sys.stderr) return 1 rt.summarize() # Return error status (0 if rt.errors is zero) return int(bool(rt.errors))
11,653
269
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/patcomp.py
# Copyright 2006 Google, Inc. All Rights Reserved. # Licensed to PSF under a Contributor Agreement. """Pattern compiler. The grammar is taken from PatternGrammar.txt. The compiler compiles a pattern to a pytree.*Pattern instance. """ __author__ = "Guido van Rossum <[email protected]>" # Python imports import io # Fairly local imports from .pgen2 import driver, literals, token, tokenize, parse, grammar # Really local imports from . import pytree from . import pygram class PatternSyntaxError(Exception): pass def tokenize_wrapper(input): """Tokenizes a string suppressing significant whitespace.""" skip = {token.NEWLINE, token.INDENT, token.DEDENT} tokens = tokenize.generate_tokens(io.StringIO(input).readline) for quintuple in tokens: type, value, start, end, line_text = quintuple if type not in skip: yield quintuple class PatternCompiler(object): def __init__(self, grammar_file=None): """Initializer. Takes an optional alternative filename for the pattern grammar. """ if grammar_file is None: self.grammar = pygram.pattern_grammar self.syms = pygram.pattern_symbols else: self.grammar = driver.load_grammar(grammar_file) self.syms = pygram.Symbols(self.grammar) self.pygrammar = pygram.python_grammar self.pysyms = pygram.python_symbols self.driver = driver.Driver(self.grammar, convert=pattern_convert) def compile_pattern(self, input, debug=False, with_tree=False): """Compiles a pattern string to a nested pytree.*Pattern object.""" tokens = tokenize_wrapper(input) try: root = self.driver.parse_tokens(tokens, debug=debug) except parse.ParseError as e: raise PatternSyntaxError(str(e)) if with_tree: return self.compile_node(root), root else: return self.compile_node(root) def compile_node(self, node): """Compiles a node, recursively. This is one big switch on the node type. """ # XXX Optimize certain Wildcard-containing-Wildcard patterns # that can be merged if node.type == self.syms.Matcher: node = node.children[0] # Avoid unneeded recursion if node.type == self.syms.Alternatives: # Skip the odd children since they are just '|' tokens alts = [self.compile_node(ch) for ch in node.children[::2]] if len(alts) == 1: return alts[0] p = pytree.WildcardPattern([[a] for a in alts], min=1, max=1) return p.optimize() if node.type == self.syms.Alternative: units = [self.compile_node(ch) for ch in node.children] if len(units) == 1: return units[0] p = pytree.WildcardPattern([units], min=1, max=1) return p.optimize() if node.type == self.syms.NegatedUnit: pattern = self.compile_basic(node.children[1:]) p = pytree.NegatedPattern(pattern) return p.optimize() assert node.type == self.syms.Unit name = None nodes = node.children if len(nodes) >= 3 and nodes[1].type == token.EQUAL: name = nodes[0].value nodes = nodes[2:] repeat = None if len(nodes) >= 2 and nodes[-1].type == self.syms.Repeater: repeat = nodes[-1] nodes = nodes[:-1] # Now we've reduced it to: STRING | NAME [Details] | (...) | [...] pattern = self.compile_basic(nodes, repeat) if repeat is not None: assert repeat.type == self.syms.Repeater children = repeat.children child = children[0] if child.type == token.STAR: min = 0 max = pytree.HUGE elif child.type == token.PLUS: min = 1 max = pytree.HUGE elif child.type == token.LBRACE: assert children[-1].type == token.RBRACE assert len(children) in (3, 5) min = max = self.get_int(children[1]) if len(children) == 5: max = self.get_int(children[3]) else: assert False if min != 1 or max != 1: pattern = pattern.optimize() pattern = pytree.WildcardPattern([[pattern]], min=min, max=max) if name is not None: pattern.name = name return pattern.optimize() def compile_basic(self, nodes, repeat=None): # Compile STRING | NAME [Details] | (...) | [...] assert len(nodes) >= 1 node = nodes[0] if node.type == token.STRING: value = str(literals.evalString(node.value)) return pytree.LeafPattern(_type_of_literal(value), value) elif node.type == token.NAME: value = node.value if value.isupper(): if value not in TOKEN_MAP: raise PatternSyntaxError("Invalid token: %r" % value) if nodes[1:]: raise PatternSyntaxError("Can't have details for token") return pytree.LeafPattern(TOKEN_MAP[value]) else: if value == "any": type = None elif not value.startswith("_"): type = getattr(self.pysyms, value, None) if type is None: raise PatternSyntaxError("Invalid symbol: %r" % value) if nodes[1:]: # Details present content = [self.compile_node(nodes[1].children[1])] else: content = None return pytree.NodePattern(type, content) elif node.value == "(": return self.compile_node(nodes[1]) elif node.value == "[": assert repeat is None subpattern = self.compile_node(nodes[1]) return pytree.WildcardPattern([[subpattern]], min=0, max=1) assert False, node def get_int(self, node): assert node.type == token.NUMBER return int(node.value) # Map named tokens to the type value for a LeafPattern TOKEN_MAP = {"NAME": token.NAME, "STRING": token.STRING, "NUMBER": token.NUMBER, "TOKEN": None} def _type_of_literal(value): if value[0].isalpha(): return token.NAME elif value in grammar.opmap: return grammar.opmap[value] else: return None def pattern_convert(grammar, raw_node_info): """Converts raw node information to a Node or Leaf instance.""" type, value, context, children = raw_node_info if children or type in grammar.number2symbol: return pytree.Node(type, children, context=context) else: return pytree.Leaf(type, value, context=context) def compile_pattern(pattern): return PatternCompiler().compile_pattern(pattern)
7,044
205
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/pygram.py
# Copyright 2006 Google, Inc. All Rights Reserved. # Licensed to PSF under a Contributor Agreement. """Export the Python grammar and symbols.""" # Python imports import os # Local imports from .pgen2 import token from .pgen2 import driver from . import pytree # The grammar file _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt") _PATTERN_GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "PatternGrammar.txt") class Symbols(object): def __init__(self, grammar): """Initializer. Creates an attribute for each grammar symbol (nonterminal), whose value is the symbol's type (an int >= 256). """ for name, symbol in grammar.symbol2number.items(): setattr(self, name, symbol) python_grammar = driver.load_packaged_grammar("lib2to3", _GRAMMAR_FILE) python_symbols = Symbols(python_grammar) python_grammar_no_print_statement = python_grammar.copy() del python_grammar_no_print_statement.keywords["print"] pattern_grammar = driver.load_packaged_grammar("lib2to3", _PATTERN_GRAMMAR_FILE) pattern_symbols = Symbols(pattern_grammar)
1,154
41
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/fixer_base.py
# Copyright 2006 Google, Inc. All Rights Reserved. # Licensed to PSF under a Contributor Agreement. """Base class for fixers (optional, but recommended).""" # Python imports import itertools # Local imports from .patcomp import PatternCompiler from . import pygram from .fixer_util import does_tree_import class BaseFix(object): """Optional base class for fixers. The subclass name must be FixFooBar where FooBar is the result of removing underscores and capitalizing the words of the fix name. For example, the class name for a fixer named 'has_key' should be FixHasKey. """ PATTERN = None # Most subclasses should override with a string literal pattern = None # Compiled pattern, set by compile_pattern() pattern_tree = None # Tree representation of the pattern options = None # Options object passed to initializer filename = None # The filename (set by set_filename) numbers = itertools.count(1) # For new_name() used_names = set() # A set of all used NAMEs order = "post" # Does the fixer prefer pre- or post-order traversal explicit = False # Is this ignored by refactor.py -f all? run_order = 5 # Fixers will be sorted by run order before execution # Lower numbers will be run first. _accept_type = None # [Advanced and not public] This tells RefactoringTool # which node type to accept when there's not a pattern. keep_line_order = False # For the bottom matcher: match with the # original line order BM_compatible = False # Compatibility with the bottom matching # module; every fixer should set this # manually # Shortcut for access to Python grammar symbols syms = pygram.python_symbols def __init__(self, options, log): """Initializer. Subclass may override. Args: options: a dict containing the options passed to RefactoringTool that could be used to customize the fixer through the command line. log: a list to append warnings and other messages to. """ self.options = options self.log = log self.compile_pattern() def compile_pattern(self): """Compiles self.PATTERN into self.pattern. Subclass may override if it doesn't want to use self.{pattern,PATTERN} in .match(). """ if self.PATTERN is not None: PC = PatternCompiler() self.pattern, self.pattern_tree = PC.compile_pattern(self.PATTERN, with_tree=True) def set_filename(self, filename): """Set the filename. The main refactoring tool should call this. """ self.filename = filename def match(self, node): """Returns match for a given parse tree node. Should return a true or false object (not necessarily a bool). It may return a non-empty dict of matching sub-nodes as returned by a matching pattern. Subclass may override. """ results = {"node": node} return self.pattern.match(node, results) and results def transform(self, node, results): """Returns the transformation for a given parse tree node. Args: node: the root of the parse tree that matched the fixer. results: a dict mapping symbolic names to part of the match. Returns: None, or a node that is a modified copy of the argument node. The node argument may also be modified in-place to effect the same change. Subclass *must* override. """ raise NotImplementedError() def new_name(self, template="xxx_todo_changeme"): """Return a string suitable for use as an identifier The new name is guaranteed not to conflict with other identifiers. """ name = template while name in self.used_names: name = template + str(next(self.numbers)) self.used_names.add(name) return name def log_message(self, message): if self.first_log: self.first_log = False self.log.append("### In file %s ###" % self.filename) self.log.append(message) def cannot_convert(self, node, reason=None): """Warn the user that a given chunk of code is not valid Python 3, but that it cannot be converted automatically. First argument is the top-level node for the code in question. Optional second argument is why it can't be converted. """ lineno = node.get_lineno() for_output = node.clone() for_output.prefix = "" msg = "Line %d: could not convert: %s" self.log_message(msg % (lineno, for_output)) if reason: self.log_message(reason) def warning(self, node, reason): """Used for warning the user about possible uncertainty in the translation. First argument is the top-level node for the code in question. Optional second argument is why it can't be converted. """ lineno = node.get_lineno() self.log_message("Line %d: %s" % (lineno, reason)) def start_tree(self, tree, filename): """Some fixers need to maintain tree-wide state. This method is called once, at the start of tree fix-up. tree - the root node of the tree to be processed. filename - the name of the file the tree came from. """ self.used_names = tree.used_names self.set_filename(filename) self.numbers = itertools.count(1) self.first_log = True def finish_tree(self, tree, filename): """Some fixers need to maintain tree-wide state. This method is called once, at the conclusion of tree fix-up. tree - the root node of the tree to be processed. filename - the name of the file the tree came from. """ pass class ConditionalFix(BaseFix): """ Base class for fixers which not execute if an import is found. """ # This is the name of the import which, if found, will cause the test to be skipped skip_on = None def start_tree(self, *args): super(ConditionalFix, self).start_tree(*args) self._should_skip = None def should_skip(self, node): if self._should_skip is not None: return self._should_skip pkg = self.skip_on.split(".") name = pkg[-1] pkg = ".".join(pkg[:-1]) self._should_skip = does_tree_import(pkg, name, node) return self._should_skip
6,690
187
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/btm_matcher.py
"""A bottom-up tree matching algorithm implementation meant to speed up 2to3's matching process. After the tree patterns are reduced to their rarest linear path, a linear Aho-Corasick automaton is created. The linear automaton traverses the linear paths from the leaves to the root of the AST and returns a set of nodes for further matching. This reduces significantly the number of candidate nodes.""" __author__ = "George Boutsioukis <[email protected]>" import logging import itertools from collections import defaultdict from . import pytree from .btm_utils import reduce_tree class BMNode(object): """Class for a node of the Aho-Corasick automaton used in matching""" count = itertools.count() def __init__(self): self.transition_table = {} self.fixers = [] self.id = next(BMNode.count) self.content = '' class BottomMatcher(object): """The main matcher class. After instantiating the patterns should be added using the add_fixer method""" def __init__(self): self.match = set() self.root = BMNode() self.nodes = [self.root] self.fixers = [] self.logger = logging.getLogger("RefactoringTool") def add_fixer(self, fixer): """Reduces a fixer's pattern tree to a linear path and adds it to the matcher(a common Aho-Corasick automaton). The fixer is appended on the matching states and called when they are reached""" self.fixers.append(fixer) tree = reduce_tree(fixer.pattern_tree) linear = tree.get_linear_subpattern() match_nodes = self.add(linear, start=self.root) for match_node in match_nodes: match_node.fixers.append(fixer) def add(self, pattern, start): "Recursively adds a linear pattern to the AC automaton" #print("adding pattern", pattern, "to", start) if not pattern: #print("empty pattern") return [start] if isinstance(pattern[0], tuple): #alternatives #print("alternatives") match_nodes = [] for alternative in pattern[0]: #add all alternatives, and add the rest of the pattern #to each end node end_nodes = self.add(alternative, start=start) for end in end_nodes: match_nodes.extend(self.add(pattern[1:], end)) return match_nodes else: #single token #not last if pattern[0] not in start.transition_table: #transition did not exist, create new next_node = BMNode() start.transition_table[pattern[0]] = next_node else: #transition exists already, follow next_node = start.transition_table[pattern[0]] if pattern[1:]: end_nodes = self.add(pattern[1:], start=next_node) else: end_nodes = [next_node] return end_nodes def run(self, leaves): """The main interface with the bottom matcher. The tree is traversed from the bottom using the constructed automaton. Nodes are only checked once as the tree is retraversed. When the automaton fails, we give it one more shot(in case the above tree matches as a whole with the rejected leaf), then we break for the next leaf. There is the special case of multiple arguments(see code comments) where we recheck the nodes Args: The leaves of the AST tree to be matched Returns: A dictionary of node matches with fixers as the keys """ current_ac_node = self.root results = defaultdict(list) for leaf in leaves: current_ast_node = leaf while current_ast_node: current_ast_node.was_checked = True for child in current_ast_node.children: # multiple statements, recheck if isinstance(child, pytree.Leaf) and child.value == ";": current_ast_node.was_checked = False break if current_ast_node.type == 1: #name node_token = current_ast_node.value else: node_token = current_ast_node.type if node_token in current_ac_node.transition_table: #token matches current_ac_node = current_ac_node.transition_table[node_token] for fixer in current_ac_node.fixers: if not fixer in results: results[fixer] = [] results[fixer].append(current_ast_node) else: #matching failed, reset automaton current_ac_node = self.root if (current_ast_node.parent is not None and current_ast_node.parent.was_checked): #the rest of the tree upwards has been checked, next leaf break #recheck the rejected node once from the root if node_token in current_ac_node.transition_table: #token matches current_ac_node = current_ac_node.transition_table[node_token] for fixer in current_ac_node.fixers: if not fixer in results.keys(): results[fixer] = [] results[fixer].append(current_ast_node) current_ast_node = current_ast_node.parent return results def print_ac(self): "Prints a graphviz diagram of the BM automaton(for debugging)" print("digraph g{") def print_node(node): for subnode_key in node.transition_table.keys(): subnode = node.transition_table[subnode_key] print("%d -> %d [label=%s] //%s" % (node.id, subnode.id, type_repr(subnode_key), str(subnode.fixers))) if subnode_key == 1: print(subnode.content) print_node(subnode) print_node(self.root) print("}") # taken from pytree.py for debugging; only used by print_ac _type_reprs = {} def type_repr(type_num): global _type_reprs if not _type_reprs: from .pygram import python_symbols # printing tokens is possible but not as useful # from .pgen2 import token // token.__dict__.items(): for name, val in python_symbols.__dict__.items(): if type(val) == int: _type_reprs[val] = name return _type_reprs.setdefault(type_num, type_num)
6,833
169
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/__init__.py
#empty
7
2
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/btm_utils.py
"Utility functions used by the btm_matcher module" from . import pytree from .pgen2 import grammar, token from .pygram import pattern_symbols, python_symbols syms = pattern_symbols pysyms = python_symbols tokens = grammar.opmap token_labels = token TYPE_ANY = -1 TYPE_ALTERNATIVES = -2 TYPE_GROUP = -3 class MinNode(object): """This class serves as an intermediate representation of the pattern tree during the conversion to sets of leaf-to-root subpatterns""" def __init__(self, type=None, name=None): self.type = type self.name = name self.children = [] self.leaf = False self.parent = None self.alternatives = [] self.group = [] def __repr__(self): return str(self.type) + ' ' + str(self.name) def leaf_to_root(self): """Internal method. Returns a characteristic path of the pattern tree. This method must be run for all leaves until the linear subpatterns are merged into a single""" node = self subp = [] while node: if node.type == TYPE_ALTERNATIVES: node.alternatives.append(subp) if len(node.alternatives) == len(node.children): #last alternative subp = [tuple(node.alternatives)] node.alternatives = [] node = node.parent continue else: node = node.parent subp = None break if node.type == TYPE_GROUP: node.group.append(subp) #probably should check the number of leaves if len(node.group) == len(node.children): subp = get_characteristic_subpattern(node.group) node.group = [] node = node.parent continue else: node = node.parent subp = None break if node.type == token_labels.NAME and node.name: #in case of type=name, use the name instead subp.append(node.name) else: subp.append(node.type) node = node.parent return subp def get_linear_subpattern(self): """Drives the leaf_to_root method. The reason that leaf_to_root must be run multiple times is because we need to reject 'group' matches; for example the alternative form (a | b c) creates a group [b c] that needs to be matched. Since matching multiple linear patterns overcomes the automaton's capabilities, leaf_to_root merges each group into a single choice based on 'characteristic'ity, i.e. (a|b c) -> (a|b) if b more characteristic than c Returns: The most 'characteristic'(as defined by get_characteristic_subpattern) path for the compiled pattern tree. """ for l in self.leaves(): subp = l.leaf_to_root() if subp: return subp def leaves(self): "Generator that returns the leaves of the tree" for child in self.children: yield from child.leaves() if not self.children: yield self def reduce_tree(node, parent=None): """ Internal function. Reduces a compiled pattern tree to an intermediate representation suitable for feeding the automaton. This also trims off any optional pattern elements(like [a], a*). """ new_node = None #switch on the node type if node.type == syms.Matcher: #skip node = node.children[0] if node.type == syms.Alternatives : #2 cases if len(node.children) <= 2: #just a single 'Alternative', skip this node new_node = reduce_tree(node.children[0], parent) else: #real alternatives new_node = MinNode(type=TYPE_ALTERNATIVES) #skip odd children('|' tokens) for child in node.children: if node.children.index(child)%2: continue reduced = reduce_tree(child, new_node) if reduced is not None: new_node.children.append(reduced) elif node.type == syms.Alternative: if len(node.children) > 1: new_node = MinNode(type=TYPE_GROUP) for child in node.children: reduced = reduce_tree(child, new_node) if reduced: new_node.children.append(reduced) if not new_node.children: # delete the group if all of the children were reduced to None new_node = None else: new_node = reduce_tree(node.children[0], parent) elif node.type == syms.Unit: if (isinstance(node.children[0], pytree.Leaf) and node.children[0].value == '('): #skip parentheses return reduce_tree(node.children[1], parent) if ((isinstance(node.children[0], pytree.Leaf) and node.children[0].value == '[') or (len(node.children)>1 and hasattr(node.children[1], "value") and node.children[1].value == '[')): #skip whole unit if its optional return None leaf = True details_node = None alternatives_node = None has_repeater = False repeater_node = None has_variable_name = False for child in node.children: if child.type == syms.Details: leaf = False details_node = child elif child.type == syms.Repeater: has_repeater = True repeater_node = child elif child.type == syms.Alternatives: alternatives_node = child if hasattr(child, 'value') and child.value == '=': # variable name has_variable_name = True #skip variable name if has_variable_name: #skip variable name, '=' name_leaf = node.children[2] if hasattr(name_leaf, 'value') and name_leaf.value == '(': # skip parenthesis name_leaf = node.children[3] else: name_leaf = node.children[0] #set node type if name_leaf.type == token_labels.NAME: #(python) non-name or wildcard if name_leaf.value == 'any': new_node = MinNode(type=TYPE_ANY) else: if hasattr(token_labels, name_leaf.value): new_node = MinNode(type=getattr(token_labels, name_leaf.value)) else: new_node = MinNode(type=getattr(pysyms, name_leaf.value)) elif name_leaf.type == token_labels.STRING: #(python) name or character; remove the apostrophes from #the string value name = name_leaf.value.strip("'") if name in tokens: new_node = MinNode(type=tokens[name]) else: new_node = MinNode(type=token_labels.NAME, name=name) elif name_leaf.type == syms.Alternatives: new_node = reduce_tree(alternatives_node, parent) #handle repeaters if has_repeater: if repeater_node.children[0].value == '*': #reduce to None new_node = None elif repeater_node.children[0].value == '+': #reduce to a single occurrence i.e. do nothing pass else: #TODO: handle {min, max} repeaters raise NotImplementedError pass #add children if details_node and new_node is not None: for child in details_node.children[1:-1]: #skip '<', '>' markers reduced = reduce_tree(child, new_node) if reduced is not None: new_node.children.append(reduced) if new_node: new_node.parent = parent return new_node def get_characteristic_subpattern(subpatterns): """Picks the most characteristic from a list of linear patterns Current order used is: names > common_names > common_chars """ if not isinstance(subpatterns, list): return subpatterns if len(subpatterns)==1: return subpatterns[0] # first pick out the ones containing variable names subpatterns_with_names = [] subpatterns_with_common_names = [] common_names = ['in', 'for', 'if' , 'not', 'None'] subpatterns_with_common_chars = [] common_chars = "[]().,:" for subpattern in subpatterns: if any(rec_test(subpattern, lambda x: type(x) is str)): if any(rec_test(subpattern, lambda x: isinstance(x, str) and x in common_chars)): subpatterns_with_common_chars.append(subpattern) elif any(rec_test(subpattern, lambda x: isinstance(x, str) and x in common_names)): subpatterns_with_common_names.append(subpattern) else: subpatterns_with_names.append(subpattern) if subpatterns_with_names: subpatterns = subpatterns_with_names elif subpatterns_with_common_names: subpatterns = subpatterns_with_common_names elif subpatterns_with_common_chars: subpatterns = subpatterns_with_common_chars # of the remaining subpatterns pick out the longest one return max(subpatterns, key=len) def rec_test(sequence, test_func): """Tests test_func on all items of sequence and items of included sub-iterables""" for x in sequence: if isinstance(x, (list, tuple)): yield from rec_test(x, test_func) else: yield test_func(x)
9,966
282
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/refactor.py
# Copyright 2006 Google, Inc. All Rights Reserved. # Licensed to PSF under a Contributor Agreement. """Refactoring framework. Used as a main program, this can refactor any number of files and/or recursively descend down directories. Imported as a module, this provides infrastructure to write your own refactoring tool. """ __author__ = "Guido van Rossum <[email protected]>" # Python imports import os import sys import logging import operator import collections import io from itertools import chain # Local imports from .pgen2 import driver, tokenize, token from .fixer_util import find_root from . import pytree, pygram from . import btm_matcher as bm def get_all_fix_names(fixer_pkg, remove_prefix=True): """Return a sorted list of all available fix names in the given package.""" pkg = __import__(fixer_pkg, [], [], ["*"]) fixer_dir = os.path.dirname(pkg.__file__) fix_names = [] for name in sorted(os.listdir(fixer_dir)): if name.startswith("fix_") and name.endswith(".py"): if remove_prefix: name = name[4:] fix_names.append(name[:-3]) return fix_names class _EveryNode(Exception): pass def _get_head_types(pat): """ Accepts a pytree Pattern Node and returns a set of the pattern types which will match first. """ if isinstance(pat, (pytree.NodePattern, pytree.LeafPattern)): # NodePatters must either have no type and no content # or a type and content -- so they don't get any farther # Always return leafs if pat.type is None: raise _EveryNode return {pat.type} if isinstance(pat, pytree.NegatedPattern): if pat.content: return _get_head_types(pat.content) raise _EveryNode # Negated Patterns don't have a type if isinstance(pat, pytree.WildcardPattern): # Recurse on each node in content r = set() for p in pat.content: for x in p: r.update(_get_head_types(x)) return r raise Exception("Oh no! I don't understand pattern %s" %(pat)) def _get_headnode_dict(fixer_list): """ Accepts a list of fixers and returns a dictionary of head node type --> fixer list. """ head_nodes = collections.defaultdict(list) every = [] for fixer in fixer_list: if fixer.pattern: try: heads = _get_head_types(fixer.pattern) except _EveryNode: every.append(fixer) else: for node_type in heads: head_nodes[node_type].append(fixer) else: if fixer._accept_type is not None: head_nodes[fixer._accept_type].append(fixer) else: every.append(fixer) for node_type in chain(pygram.python_grammar.symbol2number.values(), pygram.python_grammar.tokens): head_nodes[node_type].extend(every) return dict(head_nodes) def get_fixers_from_package(pkg_name): """ Return the fully qualified names for fixers in the package pkg_name. """ return [pkg_name + "." + fix_name for fix_name in get_all_fix_names(pkg_name, False)] def _identity(obj): return obj if sys.version_info < (3, 0): import codecs _open_with_encoding = codecs.open # codecs.open doesn't translate newlines sadly. def _from_system_newlines(input): return input.replace("\r\n", "\n") def _to_system_newlines(input): if os.linesep != "\n": return input.replace("\n", os.linesep) else: return input else: _open_with_encoding = open _from_system_newlines = _identity _to_system_newlines = _identity def _detect_future_features(source): have_docstring = False gen = tokenize.generate_tokens(io.StringIO(source).readline) def advance(): tok = next(gen) return tok[0], tok[1] ignore = frozenset({token.NEWLINE, tokenize.NL, token.COMMENT}) features = set() try: while True: tp, value = advance() if tp in ignore: continue elif tp == token.STRING: if have_docstring: break have_docstring = True elif tp == token.NAME and value == "from": tp, value = advance() if tp != token.NAME or value != "__future__": break tp, value = advance() if tp != token.NAME or value != "import": break tp, value = advance() if tp == token.OP and value == "(": tp, value = advance() while tp == token.NAME: features.add(value) tp, value = advance() if tp != token.OP or value != ",": break tp, value = advance() else: break except StopIteration: pass return frozenset(features) class FixerError(Exception): """A fixer could not be loaded.""" class RefactoringTool(object): _default_options = {"print_function" : False, "write_unchanged_files" : False} CLASS_PREFIX = "Fix" # The prefix for fixer classes FILE_PREFIX = "fix_" # The prefix for modules with a fixer within def __init__(self, fixer_names, options=None, explicit=None): """Initializer. Args: fixer_names: a list of fixers to import options: a dict with configuration. explicit: a list of fixers to run even if they are explicit. """ self.fixers = fixer_names self.explicit = explicit or [] self.options = self._default_options.copy() if options is not None: self.options.update(options) if self.options["print_function"]: self.grammar = pygram.python_grammar_no_print_statement else: self.grammar = pygram.python_grammar # When this is True, the refactor*() methods will call write_file() for # files processed even if they were not changed during refactoring. If # and only if the refactor method's write parameter was True. self.write_unchanged_files = self.options.get("write_unchanged_files") self.errors = [] self.logger = logging.getLogger("RefactoringTool") self.fixer_log = [] self.wrote = False self.driver = driver.Driver(self.grammar, convert=pytree.convert, logger=self.logger) self.pre_order, self.post_order = self.get_fixers() self.files = [] # List of files that were or should be modified self.BM = bm.BottomMatcher() self.bmi_pre_order = [] # Bottom Matcher incompatible fixers self.bmi_post_order = [] for fixer in chain(self.post_order, self.pre_order): if fixer.BM_compatible: self.BM.add_fixer(fixer) # remove fixers that will be handled by the bottom-up # matcher elif fixer in self.pre_order: self.bmi_pre_order.append(fixer) elif fixer in self.post_order: self.bmi_post_order.append(fixer) self.bmi_pre_order_heads = _get_headnode_dict(self.bmi_pre_order) self.bmi_post_order_heads = _get_headnode_dict(self.bmi_post_order) def get_fixers(self): """Inspects the options to load the requested patterns and handlers. Returns: (pre_order, post_order), where pre_order is the list of fixers that want a pre-order AST traversal, and post_order is the list that want post-order traversal. """ pre_order_fixers = [] post_order_fixers = [] for fix_mod_path in self.fixers: mod = __import__(fix_mod_path, {}, {}, ["*"]) fix_name = fix_mod_path.rsplit(".", 1)[-1] if fix_name.startswith(self.FILE_PREFIX): fix_name = fix_name[len(self.FILE_PREFIX):] parts = fix_name.split("_") class_name = self.CLASS_PREFIX + "".join([p.title() for p in parts]) try: fix_class = getattr(mod, class_name) except AttributeError: raise FixerError("Can't find %s.%s" % (fix_name, class_name)) fixer = fix_class(self.options, self.fixer_log) if fixer.explicit and self.explicit is not True and \ fix_mod_path not in self.explicit: self.log_message("Skipping optional fixer: %s", fix_name) continue self.log_debug("Adding transformation: %s", fix_name) if fixer.order == "pre": pre_order_fixers.append(fixer) elif fixer.order == "post": post_order_fixers.append(fixer) else: raise FixerError("Illegal fixer order: %r" % fixer.order) key_func = operator.attrgetter("run_order") pre_order_fixers.sort(key=key_func) post_order_fixers.sort(key=key_func) return (pre_order_fixers, post_order_fixers) def log_error(self, msg, *args, **kwds): """Called when an error occurs.""" raise def log_message(self, msg, *args): """Hook to log a message.""" if args: msg = msg % args self.logger.info(msg) def log_debug(self, msg, *args): if args: msg = msg % args self.logger.debug(msg) def print_output(self, old_text, new_text, filename, equal): """Called with the old version, new version, and filename of a refactored file.""" pass def refactor(self, items, write=False, doctests_only=False): """Refactor a list of files and directories.""" for dir_or_file in items: if os.path.isdir(dir_or_file): self.refactor_dir(dir_or_file, write, doctests_only) else: self.refactor_file(dir_or_file, write, doctests_only) def refactor_dir(self, dir_name, write=False, doctests_only=False): """Descends down a directory and refactor every Python file found. Python files are assumed to have a .py extension. Files and subdirectories starting with '.' are skipped. """ py_ext = os.extsep + "py" for dirpath, dirnames, filenames in os.walk(dir_name): self.log_debug("Descending into %s", dirpath) dirnames.sort() filenames.sort() for name in filenames: if (not name.startswith(".") and os.path.splitext(name)[1] == py_ext): fullname = os.path.join(dirpath, name) self.refactor_file(fullname, write, doctests_only) # Modify dirnames in-place to remove subdirs with leading dots dirnames[:] = [dn for dn in dirnames if not dn.startswith(".")] def _read_python_source(self, filename): """ Do our best to decode a Python source file correctly. """ try: f = open(filename, "rb") except OSError as err: self.log_error("Can't open %s: %s", filename, err) return None, None try: encoding = tokenize.detect_encoding(f.readline)[0] finally: f.close() with _open_with_encoding(filename, "r", encoding=encoding) as f: return _from_system_newlines(f.read()), encoding def refactor_file(self, filename, write=False, doctests_only=False): """Refactors a file.""" input, encoding = self._read_python_source(filename) if input is None: # Reading the file failed. return input += "\n" # Silence certain parse errors if doctests_only: self.log_debug("Refactoring doctests in %s", filename) output = self.refactor_docstring(input, filename) if self.write_unchanged_files or output != input: self.processed_file(output, filename, input, write, encoding) else: self.log_debug("No doctest changes in %s", filename) else: tree = self.refactor_string(input, filename) if self.write_unchanged_files or (tree and tree.was_changed): # The [:-1] is to take off the \n we added earlier self.processed_file(str(tree)[:-1], filename, write=write, encoding=encoding) else: self.log_debug("No changes in %s", filename) def refactor_string(self, data, name): """Refactor a given input string. Args: data: a string holding the code to be refactored. name: a human-readable name for use in error/log messages. Returns: An AST corresponding to the refactored input stream; None if there were errors during the parse. """ features = _detect_future_features(data) if "print_function" in features: self.driver.grammar = pygram.python_grammar_no_print_statement try: tree = self.driver.parse_string(data) except Exception as err: self.log_error("Can't parse %s: %s: %s", name, err.__class__.__name__, err) return finally: self.driver.grammar = self.grammar tree.future_features = features self.log_debug("Refactoring %s", name) self.refactor_tree(tree, name) return tree def refactor_stdin(self, doctests_only=False): input = sys.stdin.read() if doctests_only: self.log_debug("Refactoring doctests in stdin") output = self.refactor_docstring(input, "<stdin>") if self.write_unchanged_files or output != input: self.processed_file(output, "<stdin>", input) else: self.log_debug("No doctest changes in stdin") else: tree = self.refactor_string(input, "<stdin>") if self.write_unchanged_files or (tree and tree.was_changed): self.processed_file(str(tree), "<stdin>", input) else: self.log_debug("No changes in stdin") def refactor_tree(self, tree, name): """Refactors a parse tree (modifying the tree in place). For compatible patterns the bottom matcher module is used. Otherwise the tree is traversed node-to-node for matches. Args: tree: a pytree.Node instance representing the root of the tree to be refactored. name: a human-readable name for this tree. Returns: True if the tree was modified, False otherwise. """ for fixer in chain(self.pre_order, self.post_order): fixer.start_tree(tree, name) #use traditional matching for the incompatible fixers self.traverse_by(self.bmi_pre_order_heads, tree.pre_order()) self.traverse_by(self.bmi_post_order_heads, tree.post_order()) # obtain a set of candidate nodes match_set = self.BM.run(tree.leaves()) while any(match_set.values()): for fixer in self.BM.fixers: if fixer in match_set and match_set[fixer]: #sort by depth; apply fixers from bottom(of the AST) to top match_set[fixer].sort(key=pytree.Base.depth, reverse=True) if fixer.keep_line_order: #some fixers(eg fix_imports) must be applied #with the original file's line order match_set[fixer].sort(key=pytree.Base.get_lineno) for node in list(match_set[fixer]): if node in match_set[fixer]: match_set[fixer].remove(node) try: find_root(node) except ValueError: # this node has been cut off from a # previous transformation ; skip continue if node.fixers_applied and fixer in node.fixers_applied: # do not apply the same fixer again continue results = fixer.match(node) if results: new = fixer.transform(node, results) if new is not None: node.replace(new) #new.fixers_applied.append(fixer) for node in new.post_order(): # do not apply the fixer again to # this or any subnode if not node.fixers_applied: node.fixers_applied = [] node.fixers_applied.append(fixer) # update the original match set for # the added code new_matches = self.BM.run(new.leaves()) for fxr in new_matches: if not fxr in match_set: match_set[fxr]=[] match_set[fxr].extend(new_matches[fxr]) for fixer in chain(self.pre_order, self.post_order): fixer.finish_tree(tree, name) return tree.was_changed def traverse_by(self, fixers, traversal): """Traverse an AST, applying a set of fixers to each node. This is a helper method for refactor_tree(). Args: fixers: a list of fixer instances. traversal: a generator that yields AST nodes. Returns: None """ if not fixers: return for node in traversal: for fixer in fixers[node.type]: results = fixer.match(node) if results: new = fixer.transform(node, results) if new is not None: node.replace(new) node = new def processed_file(self, new_text, filename, old_text=None, write=False, encoding=None): """ Called when a file has been refactored and there may be changes. """ self.files.append(filename) if old_text is None: old_text = self._read_python_source(filename)[0] if old_text is None: return equal = old_text == new_text self.print_output(old_text, new_text, filename, equal) if equal: self.log_debug("No changes to %s", filename) if not self.write_unchanged_files: return if write: self.write_file(new_text, filename, old_text, encoding) else: self.log_debug("Not writing changes to %s", filename) def write_file(self, new_text, filename, old_text, encoding=None): """Writes a string to a file. It first shows a unified diff between the old text and the new text, and then rewrites the file; the latter is only done if the write option is set. """ try: f = _open_with_encoding(filename, "w", encoding=encoding) except OSError as err: self.log_error("Can't create %s: %s", filename, err) return try: f.write(_to_system_newlines(new_text)) except OSError as err: self.log_error("Can't write %s: %s", filename, err) finally: f.close() self.log_debug("Wrote changes to %s", filename) self.wrote = True PS1 = ">>> " PS2 = "... " def refactor_docstring(self, input, filename): """Refactors a docstring, looking for doctests. This returns a modified version of the input string. It looks for doctests, which start with a ">>>" prompt, and may be continued with "..." prompts, as long as the "..." is indented the same as the ">>>". (Unfortunately we can't use the doctest module's parser, since, like most parsers, it is not geared towards preserving the original source.) """ result = [] block = None block_lineno = None indent = None lineno = 0 for line in input.splitlines(keepends=True): lineno += 1 if line.lstrip().startswith(self.PS1): if block is not None: result.extend(self.refactor_doctest(block, block_lineno, indent, filename)) block_lineno = lineno block = [line] i = line.find(self.PS1) indent = line[:i] elif (indent is not None and (line.startswith(indent + self.PS2) or line == indent + self.PS2.rstrip() + "\n")): block.append(line) else: if block is not None: result.extend(self.refactor_doctest(block, block_lineno, indent, filename)) block = None indent = None result.append(line) if block is not None: result.extend(self.refactor_doctest(block, block_lineno, indent, filename)) return "".join(result) def refactor_doctest(self, block, lineno, indent, filename): """Refactors one doctest. A doctest is given as a block of lines, the first of which starts with ">>>" (possibly indented), while the remaining lines start with "..." (identically indented). """ try: tree = self.parse_block(block, lineno, indent) except Exception as err: if self.logger.isEnabledFor(logging.DEBUG): for line in block: self.log_debug("Source: %s", line.rstrip("\n")) self.log_error("Can't parse docstring in %s line %s: %s: %s", filename, lineno, err.__class__.__name__, err) return block if self.refactor_tree(tree, filename): new = str(tree).splitlines(keepends=True) # Undo the adjustment of the line numbers in wrap_toks() below. clipped, new = new[:lineno-1], new[lineno-1:] assert clipped == ["\n"] * (lineno-1), clipped if not new[-1].endswith("\n"): new[-1] += "\n" block = [indent + self.PS1 + new.pop(0)] if new: block += [indent + self.PS2 + line for line in new] return block def summarize(self): if self.wrote: were = "were" else: were = "need to be" if not self.files: self.log_message("No files %s modified.", were) else: self.log_message("Files that %s modified:", were) for file in self.files: self.log_message(file) if self.fixer_log: self.log_message("Warnings/messages while refactoring:") for message in self.fixer_log: self.log_message(message) if self.errors: if len(self.errors) == 1: self.log_message("There was 1 error:") else: self.log_message("There were %d errors:", len(self.errors)) for msg, args, kwds in self.errors: self.log_message(msg, *args, **kwds) def parse_block(self, block, lineno, indent): """Parses a block into a tree. This is necessary to get correct line number / offset information in the parser diagnostics and embedded into the parse tree. """ tree = self.driver.parse_tokens(self.wrap_toks(block, lineno, indent)) tree.future_features = frozenset() return tree def wrap_toks(self, block, lineno, indent): """Wraps a tokenize stream to systematically modify start/end.""" tokens = tokenize.generate_tokens(self.gen_lines(block, indent).__next__) for type, value, (line0, col0), (line1, col1), line_text in tokens: line0 += lineno - 1 line1 += lineno - 1 # Don't bother updating the columns; this is too complicated # since line_text would also have to be updated and it would # still break for tokens spanning lines. Let the user guess # that the column numbers for doctests are relative to the # end of the prompt string (PS1 or PS2). yield type, value, (line0, col0), (line1, col1), line_text def gen_lines(self, block, indent): """Generates lines as expected by tokenize from a list of lines. This strips the first len(indent + self.PS1) characters off each line. """ prefix1 = indent + self.PS1 prefix2 = indent + self.PS2 prefix = prefix1 for line in block: if line.startswith(prefix): yield line[len(prefix):] elif line == prefix.rstrip() + "\n": yield "\n" else: raise AssertionError("line=%r, prefix=%r" % (line, prefix)) prefix = prefix2 while True: yield "" class MultiprocessingUnsupported(Exception): pass class MultiprocessRefactoringTool(RefactoringTool): def __init__(self, *args, **kwargs): super(MultiprocessRefactoringTool, self).__init__(*args, **kwargs) self.queue = None self.output_lock = None def refactor(self, items, write=False, doctests_only=False, num_processes=1): if num_processes == 1: return super(MultiprocessRefactoringTool, self).refactor( items, write, doctests_only) try: import multiprocessing except ImportError: raise MultiprocessingUnsupported if self.queue is not None: raise RuntimeError("already doing multiple processes") self.queue = multiprocessing.JoinableQueue() self.output_lock = multiprocessing.Lock() processes = [multiprocessing.Process(target=self._child) for i in range(num_processes)] try: for p in processes: p.start() super(MultiprocessRefactoringTool, self).refactor(items, write, doctests_only) finally: self.queue.join() for i in range(num_processes): self.queue.put(None) for p in processes: if p.is_alive(): p.join() self.queue = None def _child(self): task = self.queue.get() while task is not None: args, kwargs = task try: super(MultiprocessRefactoringTool, self).refactor_file( *args, **kwargs) finally: self.queue.task_done() task = self.queue.get() def refactor_file(self, *args, **kwargs): if self.queue is not None: self.queue.put((args, kwargs)) else: return super(MultiprocessRefactoringTool, self).refactor_file( *args, **kwargs)
27,965
745
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/test_all_fixers.py
"""Tests that run all fixer modules over an input stream. This has been broken out into its own test module because of its running time. """ # Author: Collin Winter # Python imports import unittest import test.support # Local imports from . import support @test.support.requires_resource('cpu') class Test_all(support.TestCase): def setUp(self): self.refactor = support.get_refactorer() def test_all_project_files(self): for filepath in support.all_project_files(): self.refactor.refactor_file(filepath) if __name__ == '__main__': unittest.main()
595
28
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/pytree_idempotency.py
#!/usr/bin/env python3 # Copyright 2006 Google, Inc. All Rights Reserved. # Licensed to PSF under a Contributor Agreement. """Main program for testing the infrastructure.""" from __future__ import print_function __author__ = "Guido van Rossum <[email protected]>" # Support imports (need to be imported first) from . import support # Python imports import os import sys import logging # Local imports from .. import pytree from .. import pgen2 from ..pgen2 import driver logging.basicConfig() def main(): gr = driver.load_grammar("Grammar.txt") dr = driver.Driver(gr, convert=pytree.convert) fn = "example.py" tree = dr.parse_file(fn, debug=True) if not diff(fn, tree): print("No diffs.") if not sys.argv[1:]: return # Pass a dummy argument to run the complete test suite below problems = [] # Process every imported module for name in sys.modules: mod = sys.modules[name] if mod is None or not hasattr(mod, "__file__"): continue fn = mod.__file__ if fn.endswith(".pyc"): fn = fn[:-1] if not fn.endswith(".py"): continue print("Parsing", fn, file=sys.stderr) tree = dr.parse_file(fn, debug=True) if diff(fn, tree): problems.append(fn) # Process every single module on sys.path (but not in packages) for dir in sys.path: try: names = os.listdir(dir) except OSError: continue print("Scanning", dir, "...", file=sys.stderr) for name in names: if not name.endswith(".py"): continue print("Parsing", name, file=sys.stderr) fn = os.path.join(dir, name) try: tree = dr.parse_file(fn, debug=True) except pgen2.parse.ParseError as err: print("ParseError:", err) else: if diff(fn, tree): problems.append(fn) # Show summary of problem files if not problems: print("No problems. Congratulations!") else: print("Problems in following files:") for fn in problems: print("***", fn) def diff(fn, tree): f = open("@", "w") try: f.write(str(tree)) finally: f.close() try: return os.system("diff -u %s @" % fn) finally: os.remove("@") if __name__ == "__main__": main()
2,453
95
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/test_util.py
""" Test suite for the code in fixer_util """ # Testing imports from . import support # Local imports from lib2to3.pytree import Node, Leaf from lib2to3 import fixer_util from lib2to3.fixer_util import Attr, Name, Call, Comma from lib2to3.pgen2 import token def parse(code, strip_levels=0): # The topmost node is file_input, which we don't care about. # The next-topmost node is a *_stmt node, which we also don't care about tree = support.parse_string(code) for i in range(strip_levels): tree = tree.children[0] tree.parent = None return tree class MacroTestCase(support.TestCase): def assertStr(self, node, string): if isinstance(node, (tuple, list)): node = Node(fixer_util.syms.simple_stmt, node) self.assertEqual(str(node), string) class Test_is_tuple(support.TestCase): def is_tuple(self, string): return fixer_util.is_tuple(parse(string, strip_levels=2)) def test_valid(self): self.assertTrue(self.is_tuple("(a, b)")) self.assertTrue(self.is_tuple("(a, (b, c))")) self.assertTrue(self.is_tuple("((a, (b, c)),)")) self.assertTrue(self.is_tuple("(a,)")) self.assertTrue(self.is_tuple("()")) def test_invalid(self): self.assertFalse(self.is_tuple("(a)")) self.assertFalse(self.is_tuple("('foo') % (b, c)")) class Test_is_list(support.TestCase): def is_list(self, string): return fixer_util.is_list(parse(string, strip_levels=2)) def test_valid(self): self.assertTrue(self.is_list("[]")) self.assertTrue(self.is_list("[a]")) self.assertTrue(self.is_list("[a, b]")) self.assertTrue(self.is_list("[a, [b, c]]")) self.assertTrue(self.is_list("[[a, [b, c]],]")) def test_invalid(self): self.assertFalse(self.is_list("[]+[]")) class Test_Attr(MacroTestCase): def test(self): call = parse("foo()", strip_levels=2) self.assertStr(Attr(Name("a"), Name("b")), "a.b") self.assertStr(Attr(call, Name("b")), "foo().b") def test_returns(self): attr = Attr(Name("a"), Name("b")) self.assertEqual(type(attr), list) class Test_Name(MacroTestCase): def test(self): self.assertStr(Name("a"), "a") self.assertStr(Name("foo.foo().bar"), "foo.foo().bar") self.assertStr(Name("a", prefix="b"), "ba") class Test_Call(MacroTestCase): def _Call(self, name, args=None, prefix=None): """Help the next test""" children = [] if isinstance(args, list): for arg in args: children.append(arg) children.append(Comma()) children.pop() return Call(Name(name), children, prefix) def test(self): kids = [None, [Leaf(token.NUMBER, 1), Leaf(token.NUMBER, 2), Leaf(token.NUMBER, 3)], [Leaf(token.NUMBER, 1), Leaf(token.NUMBER, 3), Leaf(token.NUMBER, 2), Leaf(token.NUMBER, 4)], [Leaf(token.STRING, "b"), Leaf(token.STRING, "j", prefix=" ")] ] self.assertStr(self._Call("A"), "A()") self.assertStr(self._Call("b", kids[1]), "b(1,2,3)") self.assertStr(self._Call("a.b().c", kids[2]), "a.b().c(1,3,2,4)") self.assertStr(self._Call("d", kids[3], prefix=" "), " d(b, j)") class Test_does_tree_import(support.TestCase): def _find_bind_rec(self, name, node): # Search a tree for a binding -- used to find the starting # point for these tests. c = fixer_util.find_binding(name, node) if c: return c for child in node.children: c = self._find_bind_rec(name, child) if c: return c def does_tree_import(self, package, name, string): node = parse(string) # Find the binding of start -- that's what we'll go from node = self._find_bind_rec('start', node) return fixer_util.does_tree_import(package, name, node) def try_with(self, string): failing_tests = (("a", "a", "from a import b"), ("a.d", "a", "from a.d import b"), ("d.a", "a", "from d.a import b"), (None, "a", "import b"), (None, "a", "import b, c, d")) for package, name, import_ in failing_tests: n = self.does_tree_import(package, name, import_ + "\n" + string) self.assertFalse(n) n = self.does_tree_import(package, name, string + "\n" + import_) self.assertFalse(n) passing_tests = (("a", "a", "from a import a"), ("x", "a", "from x import a"), ("x", "a", "from x import b, c, a, d"), ("x.b", "a", "from x.b import a"), ("x.b", "a", "from x.b import b, c, a, d"), (None, "a", "import a"), (None, "a", "import b, c, a, d")) for package, name, import_ in passing_tests: n = self.does_tree_import(package, name, import_ + "\n" + string) self.assertTrue(n) n = self.does_tree_import(package, name, string + "\n" + import_) self.assertTrue(n) def test_in_function(self): self.try_with("def foo():\n\tbar.baz()\n\tstart=3") class Test_find_binding(support.TestCase): def find_binding(self, name, string, package=None): return fixer_util.find_binding(name, parse(string), package) def test_simple_assignment(self): self.assertTrue(self.find_binding("a", "a = b")) self.assertTrue(self.find_binding("a", "a = [b, c, d]")) self.assertTrue(self.find_binding("a", "a = foo()")) self.assertTrue(self.find_binding("a", "a = foo().foo.foo[6][foo]")) self.assertFalse(self.find_binding("a", "foo = a")) self.assertFalse(self.find_binding("a", "foo = (a, b, c)")) def test_tuple_assignment(self): self.assertTrue(self.find_binding("a", "(a,) = b")) self.assertTrue(self.find_binding("a", "(a, b, c) = [b, c, d]")) self.assertTrue(self.find_binding("a", "(c, (d, a), b) = foo()")) self.assertTrue(self.find_binding("a", "(a, b) = foo().foo[6][foo]")) self.assertFalse(self.find_binding("a", "(foo, b) = (b, a)")) self.assertFalse(self.find_binding("a", "(foo, (b, c)) = (a, b, c)")) def test_list_assignment(self): self.assertTrue(self.find_binding("a", "[a] = b")) self.assertTrue(self.find_binding("a", "[a, b, c] = [b, c, d]")) self.assertTrue(self.find_binding("a", "[c, [d, a], b] = foo()")) self.assertTrue(self.find_binding("a", "[a, b] = foo().foo[a][foo]")) self.assertFalse(self.find_binding("a", "[foo, b] = (b, a)")) self.assertFalse(self.find_binding("a", "[foo, [b, c]] = (a, b, c)")) def test_invalid_assignments(self): self.assertFalse(self.find_binding("a", "foo.a = 5")) self.assertFalse(self.find_binding("a", "foo[a] = 5")) self.assertFalse(self.find_binding("a", "foo(a) = 5")) self.assertFalse(self.find_binding("a", "foo(a, b) = 5")) def test_simple_import(self): self.assertTrue(self.find_binding("a", "import a")) self.assertTrue(self.find_binding("a", "import b, c, a, d")) self.assertFalse(self.find_binding("a", "import b")) self.assertFalse(self.find_binding("a", "import b, c, d")) def test_from_import(self): self.assertTrue(self.find_binding("a", "from x import a")) self.assertTrue(self.find_binding("a", "from a import a")) self.assertTrue(self.find_binding("a", "from x import b, c, a, d")) self.assertTrue(self.find_binding("a", "from x.b import a")) self.assertTrue(self.find_binding("a", "from x.b import b, c, a, d")) self.assertFalse(self.find_binding("a", "from a import b")) self.assertFalse(self.find_binding("a", "from a.d import b")) self.assertFalse(self.find_binding("a", "from d.a import b")) def test_import_as(self): self.assertTrue(self.find_binding("a", "import b as a")) self.assertTrue(self.find_binding("a", "import b as a, c, a as f, d")) self.assertFalse(self.find_binding("a", "import a as f")) self.assertFalse(self.find_binding("a", "import b, c as f, d as e")) def test_from_import_as(self): self.assertTrue(self.find_binding("a", "from x import b as a")) self.assertTrue(self.find_binding("a", "from x import g as a, d as b")) self.assertTrue(self.find_binding("a", "from x.b import t as a")) self.assertTrue(self.find_binding("a", "from x.b import g as a, d")) self.assertFalse(self.find_binding("a", "from a import b as t")) self.assertFalse(self.find_binding("a", "from a.d import b as t")) self.assertFalse(self.find_binding("a", "from d.a import b as t")) def test_simple_import_with_package(self): self.assertTrue(self.find_binding("b", "import b")) self.assertTrue(self.find_binding("b", "import b, c, d")) self.assertFalse(self.find_binding("b", "import b", "b")) self.assertFalse(self.find_binding("b", "import b, c, d", "c")) def test_from_import_with_package(self): self.assertTrue(self.find_binding("a", "from x import a", "x")) self.assertTrue(self.find_binding("a", "from a import a", "a")) self.assertTrue(self.find_binding("a", "from x import *", "x")) self.assertTrue(self.find_binding("a", "from x import b, c, a, d", "x")) self.assertTrue(self.find_binding("a", "from x.b import a", "x.b")) self.assertTrue(self.find_binding("a", "from x.b import *", "x.b")) self.assertTrue(self.find_binding("a", "from x.b import b, c, a, d", "x.b")) self.assertFalse(self.find_binding("a", "from a import b", "a")) self.assertFalse(self.find_binding("a", "from a.d import b", "a.d")) self.assertFalse(self.find_binding("a", "from d.a import b", "a.d")) self.assertFalse(self.find_binding("a", "from x.y import *", "a.b")) def test_import_as_with_package(self): self.assertFalse(self.find_binding("a", "import b.c as a", "b.c")) self.assertFalse(self.find_binding("a", "import a as f", "f")) self.assertFalse(self.find_binding("a", "import a as f", "a")) def test_from_import_as_with_package(self): # Because it would take a lot of special-case code in the fixers # to deal with from foo import bar as baz, we'll simply always # fail if there is an "from ... import ... as ..." self.assertFalse(self.find_binding("a", "from x import b as a", "x")) self.assertFalse(self.find_binding("a", "from x import g as a, d as b", "x")) self.assertFalse(self.find_binding("a", "from x.b import t as a", "x.b")) self.assertFalse(self.find_binding("a", "from x.b import g as a, d", "x.b")) self.assertFalse(self.find_binding("a", "from a import b as t", "a")) self.assertFalse(self.find_binding("a", "from a import b as t", "b")) self.assertFalse(self.find_binding("a", "from a import b as t", "t")) def test_function_def(self): self.assertTrue(self.find_binding("a", "def a(): pass")) self.assertTrue(self.find_binding("a", "def a(b, c, d): pass")) self.assertTrue(self.find_binding("a", "def a(): b = 7")) self.assertFalse(self.find_binding("a", "def d(b, (c, a), e): pass")) self.assertFalse(self.find_binding("a", "def d(a=7): pass")) self.assertFalse(self.find_binding("a", "def d(a): pass")) self.assertFalse(self.find_binding("a", "def d(): a = 7")) s = """ def d(): def a(): pass""" self.assertFalse(self.find_binding("a", s)) def test_class_def(self): self.assertTrue(self.find_binding("a", "class a: pass")) self.assertTrue(self.find_binding("a", "class a(): pass")) self.assertTrue(self.find_binding("a", "class a(b): pass")) self.assertTrue(self.find_binding("a", "class a(b, c=8): pass")) self.assertFalse(self.find_binding("a", "class d: pass")) self.assertFalse(self.find_binding("a", "class d(a): pass")) self.assertFalse(self.find_binding("a", "class d(b, a=7): pass")) self.assertFalse(self.find_binding("a", "class d(b, *a): pass")) self.assertFalse(self.find_binding("a", "class d(b, **a): pass")) self.assertFalse(self.find_binding("a", "class d: a = 7")) s = """ class d(): class a(): pass""" self.assertFalse(self.find_binding("a", s)) def test_for(self): self.assertTrue(self.find_binding("a", "for a in r: pass")) self.assertTrue(self.find_binding("a", "for a, b in r: pass")) self.assertTrue(self.find_binding("a", "for (a, b) in r: pass")) self.assertTrue(self.find_binding("a", "for c, (a,) in r: pass")) self.assertTrue(self.find_binding("a", "for c, (a, b) in r: pass")) self.assertTrue(self.find_binding("a", "for c in r: a = c")) self.assertFalse(self.find_binding("a", "for c in a: pass")) def test_for_nested(self): s = """ for b in r: for a in b: pass""" self.assertTrue(self.find_binding("a", s)) s = """ for b in r: for a, c in b: pass""" self.assertTrue(self.find_binding("a", s)) s = """ for b in r: for (a, c) in b: pass""" self.assertTrue(self.find_binding("a", s)) s = """ for b in r: for (a,) in b: pass""" self.assertTrue(self.find_binding("a", s)) s = """ for b in r: for c, (a, d) in b: pass""" self.assertTrue(self.find_binding("a", s)) s = """ for b in r: for c in b: a = 7""" self.assertTrue(self.find_binding("a", s)) s = """ for b in r: for c in b: d = a""" self.assertFalse(self.find_binding("a", s)) s = """ for b in r: for c in a: d = 7""" self.assertFalse(self.find_binding("a", s)) def test_if(self): self.assertTrue(self.find_binding("a", "if b in r: a = c")) self.assertFalse(self.find_binding("a", "if a in r: d = e")) def test_if_nested(self): s = """ if b in r: if c in d: a = c""" self.assertTrue(self.find_binding("a", s)) s = """ if b in r: if c in d: c = a""" self.assertFalse(self.find_binding("a", s)) def test_while(self): self.assertTrue(self.find_binding("a", "while b in r: a = c")) self.assertFalse(self.find_binding("a", "while a in r: d = e")) def test_while_nested(self): s = """ while b in r: while c in d: a = c""" self.assertTrue(self.find_binding("a", s)) s = """ while b in r: while c in d: c = a""" self.assertFalse(self.find_binding("a", s)) def test_try_except(self): s = """ try: a = 6 except: b = 8""" self.assertTrue(self.find_binding("a", s)) s = """ try: b = 8 except: a = 6""" self.assertTrue(self.find_binding("a", s)) s = """ try: b = 8 except KeyError: pass except: a = 6""" self.assertTrue(self.find_binding("a", s)) s = """ try: b = 8 except: b = 6""" self.assertFalse(self.find_binding("a", s)) def test_try_except_nested(self): s = """ try: try: a = 6 except: pass except: b = 8""" self.assertTrue(self.find_binding("a", s)) s = """ try: b = 8 except: try: a = 6 except: pass""" self.assertTrue(self.find_binding("a", s)) s = """ try: b = 8 except: try: pass except: a = 6""" self.assertTrue(self.find_binding("a", s)) s = """ try: try: b = 8 except KeyError: pass except: a = 6 except: pass""" self.assertTrue(self.find_binding("a", s)) s = """ try: pass except: try: b = 8 except KeyError: pass except: a = 6""" self.assertTrue(self.find_binding("a", s)) s = """ try: b = 8 except: b = 6""" self.assertFalse(self.find_binding("a", s)) s = """ try: try: b = 8 except: c = d except: try: b = 6 except: t = 8 except: o = y""" self.assertFalse(self.find_binding("a", s)) def test_try_except_finally(self): s = """ try: c = 6 except: b = 8 finally: a = 9""" self.assertTrue(self.find_binding("a", s)) s = """ try: b = 8 finally: a = 6""" self.assertTrue(self.find_binding("a", s)) s = """ try: b = 8 finally: b = 6""" self.assertFalse(self.find_binding("a", s)) s = """ try: b = 8 except: b = 9 finally: b = 6""" self.assertFalse(self.find_binding("a", s)) def test_try_except_finally_nested(self): s = """ try: c = 6 except: b = 8 finally: try: a = 9 except: b = 9 finally: c = 9""" self.assertTrue(self.find_binding("a", s)) s = """ try: b = 8 finally: try: pass finally: a = 6""" self.assertTrue(self.find_binding("a", s)) s = """ try: b = 8 finally: try: b = 6 finally: b = 7""" self.assertFalse(self.find_binding("a", s)) class Test_touch_import(support.TestCase): def test_after_docstring(self): node = parse('"""foo"""\nbar()') fixer_util.touch_import(None, "foo", node) self.assertEqual(str(node), '"""foo"""\nimport foo\nbar()\n\n') def test_after_imports(self): node = parse('"""foo"""\nimport bar\nbar()') fixer_util.touch_import(None, "foo", node) self.assertEqual(str(node), '"""foo"""\nimport bar\nimport foo\nbar()\n\n') def test_beginning(self): node = parse('bar()') fixer_util.touch_import(None, "foo", node) self.assertEqual(str(node), 'import foo\nbar()\n\n') def test_from_import(self): node = parse('bar()') fixer_util.touch_import("html", "escape", node) self.assertEqual(str(node), 'from html import escape\nbar()\n\n') def test_name_import(self): node = parse('bar()') fixer_util.touch_import(None, "cgi", node) self.assertEqual(str(node), 'import cgi\nbar()\n\n') class Test_find_indentation(support.TestCase): def test_nothing(self): fi = fixer_util.find_indentation node = parse("node()") self.assertEqual(fi(node), "") node = parse("") self.assertEqual(fi(node), "") def test_simple(self): fi = fixer_util.find_indentation node = parse("def f():\n x()") self.assertEqual(fi(node), "") self.assertEqual(fi(node.children[0].children[4].children[2]), " ") node = parse("def f():\n x()\n y()") self.assertEqual(fi(node.children[0].children[4].children[4]), " ")
21,207
592
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/support.py
"""Support code for test_*.py files""" # Author: Collin Winter # Python imports import unittest import os import os.path from textwrap import dedent # Local imports from lib2to3 import pytree, refactor from lib2to3.pgen2 import driver as pgen2_driver test_dir = os.path.dirname(__file__) proj_dir = os.path.normpath(os.path.join(test_dir, "..")) grammar_path = os.path.join(test_dir, "..", "Grammar.txt") grammar = pgen2_driver.load_grammar(grammar_path) grammar_no_print_statement = pgen2_driver.load_grammar(grammar_path) del grammar_no_print_statement.keywords["print"] driver = pgen2_driver.Driver(grammar, convert=pytree.convert) driver_no_print_statement = pgen2_driver.Driver( grammar_no_print_statement, convert=pytree.convert ) def parse_string(string): return driver.parse_string(reformat(string), debug=True) def run_all_tests(test_mod=None, tests=None): if tests is None: tests = unittest.TestLoader().loadTestsFromModule(test_mod) unittest.TextTestRunner(verbosity=2).run(tests) def reformat(string): return dedent(string) + "\n\n" def get_refactorer(fixer_pkg="lib2to3", fixers=None, options=None): """ A convenience function for creating a RefactoringTool for tests. fixers is a list of fixers for the RefactoringTool to use. By default "lib2to3.fixes.*" is used. options is an optional dictionary of options to be passed to the RefactoringTool. """ if fixers is not None: fixers = [fixer_pkg + ".fixes.fix_" + fix for fix in fixers] else: fixers = refactor.get_fixers_from_package(fixer_pkg + ".fixes") options = options or {} return refactor.RefactoringTool(fixers, options, explicit=True) def all_project_files(): for dirpath, dirnames, filenames in os.walk(proj_dir): for filename in filenames: if filename.endswith(".py"): yield os.path.join(dirpath, filename) TestCase = unittest.TestCase
1,948
59
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/__main__.py
from . import load_tests import unittest unittest.main()
58
5
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/test_parser.py
"""Test suite for 2to3's parser and grammar files. This is the place to add tests for changes to 2to3's grammar, such as those merging the grammars for Python 2 and 3. In addition to specific tests for parts of the grammar we've changed, we also make sure we can parse the test_grammar.py files from both Python 2 and Python 3. """ # Testing imports from . import support from .support import driver, driver_no_print_statement # Python imports import difflib import importlib import operator import os import pickle import shutil import subprocess import sys import tempfile import unittest # Local imports from lib2to3.pgen2 import driver as pgen2_driver from lib2to3.pgen2 import tokenize from ..pgen2.parse import ParseError from lib2to3.pygram import python_symbols as syms class TestDriver(support.TestCase): def test_formfeed(self): s = """print 1\n\x0Cprint 2\n""" t = driver.parse_string(s) self.assertEqual(t.children[0].children[0].type, syms.print_stmt) self.assertEqual(t.children[1].children[0].type, syms.print_stmt) class TestPgen2Caching(support.TestCase): def test_load_grammar_from_txt_file(self): pgen2_driver.load_grammar(support.grammar_path, save=False, force=True) def test_load_grammar_from_pickle(self): # Make a copy of the grammar file in a temp directory we are # guaranteed to be able to write to. tmpdir = tempfile.mkdtemp() try: grammar_copy = os.path.join( tmpdir, os.path.basename(support.grammar_path)) shutil.copy(support.grammar_path, grammar_copy) pickle_name = pgen2_driver._generate_pickle_name(grammar_copy) pgen2_driver.load_grammar(grammar_copy, save=True, force=True) self.assertTrue(os.path.exists(pickle_name)) os.unlink(grammar_copy) # Only the pickle remains... pgen2_driver.load_grammar(grammar_copy, save=False, force=False) finally: shutil.rmtree(tmpdir) @unittest.skipIf(sys.executable is None, 'sys.executable required') def test_load_grammar_from_subprocess(self): tmpdir = tempfile.mkdtemp() tmpsubdir = os.path.join(tmpdir, 'subdir') try: os.mkdir(tmpsubdir) grammar_base = os.path.basename(support.grammar_path) grammar_copy = os.path.join(tmpdir, grammar_base) grammar_sub_copy = os.path.join(tmpsubdir, grammar_base) shutil.copy(support.grammar_path, grammar_copy) shutil.copy(support.grammar_path, grammar_sub_copy) pickle_name = pgen2_driver._generate_pickle_name(grammar_copy) pickle_sub_name = pgen2_driver._generate_pickle_name( grammar_sub_copy) self.assertNotEqual(pickle_name, pickle_sub_name) # Generate a pickle file from this process. pgen2_driver.load_grammar(grammar_copy, save=True, force=True) self.assertTrue(os.path.exists(pickle_name)) # Generate a new pickle file in a subprocess with a most likely # different hash randomization seed. sub_env = dict(os.environ) sub_env['PYTHONHASHSEED'] = 'random' subprocess.check_call( [sys.executable, '-c', """ from lib2to3.pgen2 import driver as pgen2_driver pgen2_driver.load_grammar(%r, save=True, force=True) """ % (grammar_sub_copy,)], env=sub_env) self.assertTrue(os.path.exists(pickle_sub_name)) with open(pickle_name, 'rb') as pickle_f_1, \ open(pickle_sub_name, 'rb') as pickle_f_2: self.assertEqual( pickle_f_1.read(), pickle_f_2.read(), msg='Grammar caches generated using different hash seeds' ' were not identical.') finally: shutil.rmtree(tmpdir) def test_load_packaged_grammar(self): modname = __name__ + '.load_test' class MyLoader: def get_data(self, where): return pickle.dumps({'elephant': 19}) class MyModule: __file__ = 'parsertestmodule' __spec__ = importlib.util.spec_from_loader(modname, MyLoader()) sys.modules[modname] = MyModule() self.addCleanup(operator.delitem, sys.modules, modname) g = pgen2_driver.load_packaged_grammar(modname, 'Grammar.txt') self.assertEqual(g.elephant, 19) class GrammarTest(support.TestCase): def validate(self, code): support.parse_string(code) def invalid_syntax(self, code): try: self.validate(code) except ParseError: pass else: raise AssertionError("Syntax shouldn't have been valid") class TestMatrixMultiplication(GrammarTest): def test_matrix_multiplication_operator(self): self.validate("a @ b") self.validate("a @= b") class TestYieldFrom(GrammarTest): def test_yield_from(self): self.validate("yield from x") self.validate("(yield from x) + y") self.invalid_syntax("yield from") class TestAsyncAwait(GrammarTest): def test_await_expr(self): self.validate("""async def foo(): await x """) self.validate("""async def foo(): [i async for i in b] """) self.validate("""async def foo(): {i for i in b async for i in a if await i for b in i} """) self.validate("""async def foo(): [await i for i in b if await c] """) self.validate("""async def foo(): [ i for i in b if c] """) self.validate("""async def foo(): def foo(): pass def foo(): pass await x """) self.validate("""async def foo(): return await a""") self.validate("""def foo(): def foo(): pass async def foo(): await x """) self.invalid_syntax("await x") self.invalid_syntax("""def foo(): await x""") self.invalid_syntax("""def foo(): def foo(): pass async def foo(): pass await x """) def test_async_var(self): self.validate("""async = 1""") self.validate("""await = 1""") self.validate("""def async(): pass""") def test_async_with(self): self.validate("""async def foo(): async for a in b: pass""") self.invalid_syntax("""def foo(): async for a in b: pass""") def test_async_for(self): self.validate("""async def foo(): async with a: pass""") self.invalid_syntax("""def foo(): async with a: pass""") class TestRaiseChanges(GrammarTest): def test_2x_style_1(self): self.validate("raise") def test_2x_style_2(self): self.validate("raise E, V") def test_2x_style_3(self): self.validate("raise E, V, T") def test_2x_style_invalid_1(self): self.invalid_syntax("raise E, V, T, Z") def test_3x_style(self): self.validate("raise E1 from E2") def test_3x_style_invalid_1(self): self.invalid_syntax("raise E, V from E1") def test_3x_style_invalid_2(self): self.invalid_syntax("raise E from E1, E2") def test_3x_style_invalid_3(self): self.invalid_syntax("raise from E1, E2") def test_3x_style_invalid_4(self): self.invalid_syntax("raise E from") # Modelled after Lib/test/test_grammar.py:TokenTests.test_funcdef issue2292 # and Lib/test/text_parser.py test_list_displays, test_set_displays, # test_dict_displays, test_argument_unpacking, ... changes. class TestUnpackingGeneralizations(GrammarTest): def test_mid_positional_star(self): self.validate("""func(1, *(2, 3), 4)""") def test_double_star_dict_literal(self): self.validate("""func(**{'eggs':'scrambled', 'spam':'fried'})""") def test_double_star_dict_literal_after_keywords(self): self.validate("""func(spam='fried', **{'eggs':'scrambled'})""") def test_list_display(self): self.validate("""[*{2}, 3, *[4]]""") def test_set_display(self): self.validate("""{*{2}, 3, *[4]}""") def test_dict_display_1(self): self.validate("""{**{}}""") def test_dict_display_2(self): self.validate("""{**{}, 3:4, **{5:6, 7:8}}""") def test_argument_unpacking_1(self): self.validate("""f(a, *b, *c, d)""") def test_argument_unpacking_2(self): self.validate("""f(**a, **b)""") def test_argument_unpacking_3(self): self.validate("""f(2, *a, *b, **b, **c, **d)""") def test_trailing_commas_1(self): self.validate("def f(a, b): call(a, b)") self.validate("def f(a, b,): call(a, b,)") def test_trailing_commas_2(self): self.validate("def f(a, *b): call(a, *b)") self.validate("def f(a, *b,): call(a, *b,)") def test_trailing_commas_3(self): self.validate("def f(a, b=1): call(a, b=1)") self.validate("def f(a, b=1,): call(a, b=1,)") def test_trailing_commas_4(self): self.validate("def f(a, **b): call(a, **b)") self.validate("def f(a, **b,): call(a, **b,)") def test_trailing_commas_5(self): self.validate("def f(*a, b=1): call(*a, b=1)") self.validate("def f(*a, b=1,): call(*a, b=1,)") def test_trailing_commas_6(self): self.validate("def f(*a, **b): call(*a, **b)") self.validate("def f(*a, **b,): call(*a, **b,)") def test_trailing_commas_7(self): self.validate("def f(*, b=1): call(*b)") self.validate("def f(*, b=1,): call(*b,)") def test_trailing_commas_8(self): self.validate("def f(a=1, b=2): call(a=1, b=2)") self.validate("def f(a=1, b=2,): call(a=1, b=2,)") def test_trailing_commas_9(self): self.validate("def f(a=1, **b): call(a=1, **b)") self.validate("def f(a=1, **b,): call(a=1, **b,)") def test_trailing_commas_lambda_1(self): self.validate("f = lambda a, b: call(a, b)") self.validate("f = lambda a, b,: call(a, b,)") def test_trailing_commas_lambda_2(self): self.validate("f = lambda a, *b: call(a, *b)") self.validate("f = lambda a, *b,: call(a, *b,)") def test_trailing_commas_lambda_3(self): self.validate("f = lambda a, b=1: call(a, b=1)") self.validate("f = lambda a, b=1,: call(a, b=1,)") def test_trailing_commas_lambda_4(self): self.validate("f = lambda a, **b: call(a, **b)") self.validate("f = lambda a, **b,: call(a, **b,)") def test_trailing_commas_lambda_5(self): self.validate("f = lambda *a, b=1: call(*a, b=1)") self.validate("f = lambda *a, b=1,: call(*a, b=1,)") def test_trailing_commas_lambda_6(self): self.validate("f = lambda *a, **b: call(*a, **b)") self.validate("f = lambda *a, **b,: call(*a, **b,)") def test_trailing_commas_lambda_7(self): self.validate("f = lambda *, b=1: call(*b)") self.validate("f = lambda *, b=1,: call(*b,)") def test_trailing_commas_lambda_8(self): self.validate("f = lambda a=1, b=2: call(a=1, b=2)") self.validate("f = lambda a=1, b=2,: call(a=1, b=2,)") def test_trailing_commas_lambda_9(self): self.validate("f = lambda a=1, **b: call(a=1, **b)") self.validate("f = lambda a=1, **b,: call(a=1, **b,)") # Adapted from Python 3's Lib/test/test_grammar.py:GrammarTests.testFuncdef class TestFunctionAnnotations(GrammarTest): def test_1(self): self.validate("""def f(x) -> list: pass""") def test_2(self): self.validate("""def f(x:int): pass""") def test_3(self): self.validate("""def f(*x:str): pass""") def test_4(self): self.validate("""def f(**x:float): pass""") def test_5(self): self.validate("""def f(x, y:1+2): pass""") def test_6(self): self.validate("""def f(a, (b:1, c:2, d)): pass""") def test_7(self): self.validate("""def f(a, (b:1, c:2, d), e:3=4, f=5, *g:6): pass""") def test_8(self): s = """def f(a, (b:1, c:2, d), e:3=4, f=5, *g:6, h:7, i=8, j:9=10, **k:11) -> 12: pass""" self.validate(s) def test_9(self): s = """def f( a: str, b: int, *, c: bool = False, **kwargs, ) -> None: call(c=c, **kwargs,)""" self.validate(s) def test_10(self): s = """def f( a: str, ) -> None: call(a,)""" self.validate(s) def test_11(self): s = """def f( a: str = '', ) -> None: call(a=a,)""" self.validate(s) def test_12(self): s = """def f( *args: str, ) -> None: call(*args,)""" self.validate(s) def test_13(self): self.validate("def f(a: str, b: int) -> None: call(a, b)") self.validate("def f(a: str, b: int,) -> None: call(a, b,)") def test_14(self): self.validate("def f(a: str, *b: int) -> None: call(a, *b)") self.validate("def f(a: str, *b: int,) -> None: call(a, *b,)") def test_15(self): self.validate("def f(a: str, b: int=1) -> None: call(a, b=1)") self.validate("def f(a: str, b: int=1,) -> None: call(a, b=1,)") def test_16(self): self.validate("def f(a: str, **b: int) -> None: call(a, **b)") self.validate("def f(a: str, **b: int,) -> None: call(a, **b,)") def test_17(self): self.validate("def f(*a: str, b: int=1) -> None: call(*a, b=1)") self.validate("def f(*a: str, b: int=1,) -> None: call(*a, b=1,)") def test_18(self): self.validate("def f(*a: str, **b: int) -> None: call(*a, **b)") self.validate("def f(*a: str, **b: int,) -> None: call(*a, **b,)") def test_19(self): self.validate("def f(*, b: int=1) -> None: call(*b)") self.validate("def f(*, b: int=1,) -> None: call(*b,)") def test_20(self): self.validate("def f(a: str='', b: int=2) -> None: call(a=a, b=2)") self.validate("def f(a: str='', b: int=2,) -> None: call(a=a, b=2,)") def test_21(self): self.validate("def f(a: str='', **b: int) -> None: call(a=a, **b)") self.validate("def f(a: str='', **b: int,) -> None: call(a=a, **b,)") # Adapted from Python 3's Lib/test/test_grammar.py:GrammarTests.test_var_annot class TestVarAnnotations(GrammarTest): def test_1(self): self.validate("var1: int = 5") def test_2(self): self.validate("var2: [int, str]") def test_3(self): self.validate("def f():\n" " st: str = 'Hello'\n" " a.b: int = (1, 2)\n" " return st\n") def test_4(self): self.validate("def fbad():\n" " x: int\n" " print(x)\n") def test_5(self): self.validate("class C:\n" " x: int\n" " s: str = 'attr'\n" " z = 2\n" " def __init__(self, x):\n" " self.x: int = x\n") def test_6(self): self.validate("lst: List[int] = []") class TestExcept(GrammarTest): def test_new(self): s = """ try: x except E as N: y""" self.validate(s) def test_old(self): s = """ try: x except E, N: y""" self.validate(s) class TestStringLiterals(GrammarTest): prefixes = ("'", '"', "r'", 'r"', "R'", 'R"', "u'", 'u"', "U'", 'U"', "b'", 'b"', "B'", 'B"', "f'", 'f"', "F'", 'F"', "ur'", 'ur"', "Ur'", 'Ur"', "uR'", 'uR"', "UR'", 'UR"', "br'", 'br"', "Br'", 'Br"', "bR'", 'bR"', "BR'", 'BR"', "rb'", 'rb"', "Rb'", 'Rb"', "rB'", 'rB"', "RB'", 'RB"',) def test_lit(self): for pre in self.prefixes: single = "{p}spamspamspam{s}".format(p=pre, s=pre[-1]) self.validate(single) triple = "{p}{s}{s}eggs{s}{s}{s}".format(p=pre, s=pre[-1]) self.validate(triple) # Adapted from Python 3's Lib/test/test_grammar.py:GrammarTests.testAtoms class TestSetLiteral(GrammarTest): def test_1(self): self.validate("""x = {'one'}""") def test_2(self): self.validate("""x = {'one', 1,}""") def test_3(self): self.validate("""x = {'one', 'two', 'three'}""") def test_4(self): self.validate("""x = {2, 3, 4,}""") class TestNumericLiterals(GrammarTest): def test_new_octal_notation(self): self.validate("""0o7777777777777""") self.invalid_syntax("""0o7324528887""") def test_new_binary_notation(self): self.validate("""0b101010""") self.invalid_syntax("""0b0101021""") class TestClassDef(GrammarTest): def test_new_syntax(self): self.validate("class B(t=7): pass") self.validate("class B(t, *args): pass") self.validate("class B(t, **kwargs): pass") self.validate("class B(t, *args, **kwargs): pass") self.validate("class B(t, y=9, *args, **kwargs,): pass") class TestParserIdempotency(support.TestCase): """A cut-down version of pytree_idempotency.py.""" def test_all_project_files(self): for filepath in support.all_project_files(): with open(filepath, "rb") as fp: encoding = tokenize.detect_encoding(fp.readline)[0] self.assertIsNotNone(encoding, "can't detect encoding for %s" % filepath) with open(filepath, "r", encoding=encoding) as fp: source = fp.read() try: tree = driver.parse_string(source) except ParseError: try: tree = driver_no_print_statement.parse_string(source) except ParseError as err: self.fail('ParseError on file %s (%s)' % (filepath, err)) new = str(tree) if new != source: print(diff_texts(source, new, filepath)) self.fail("Idempotency failed: %s" % filepath) def test_extended_unpacking(self): driver.parse_string("a, *b, c = x\n") driver.parse_string("[*a, b] = x\n") driver.parse_string("(z, *y, w) = m\n") driver.parse_string("for *z, m in d: pass\n") class TestLiterals(GrammarTest): def validate(self, s): driver.parse_string(support.dedent(s) + "\n\n") def test_multiline_bytes_literals(self): s = """ md5test(b"\xaa" * 80, (b"Test Using Larger Than Block-Size Key " b"and Larger Than One Block-Size Data"), "6f630fad67cda0ee1fb1f562db3aa53e") """ self.validate(s) def test_multiline_bytes_tripquote_literals(self): s = ''' b""" <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"> """ ''' self.validate(s) def test_multiline_str_literals(self): s = """ md5test("\xaa" * 80, ("Test Using Larger Than Block-Size Key " "and Larger Than One Block-Size Data"), "6f630fad67cda0ee1fb1f562db3aa53e") """ self.validate(s) def diff_texts(a, b, filename): a = a.splitlines() b = b.splitlines() return difflib.unified_diff(a, b, filename, filename, "(original)", "(reserialized)", lineterm="")
20,334
621
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/test_fixers.py
""" Test suite for the fixer modules """ # Python imports import os from itertools import chain from operator import itemgetter # Local imports from lib2to3 import pygram, fixer_util from lib2to3.tests import support class FixerTestCase(support.TestCase): # Other test cases can subclass this class and replace "fixer_pkg" with # their own. def setUp(self, fix_list=None, fixer_pkg="lib2to3", options=None): if fix_list is None: fix_list = [self.fixer] self.refactor = support.get_refactorer(fixer_pkg, fix_list, options) self.fixer_log = [] self.filename = "<string>" for fixer in chain(self.refactor.pre_order, self.refactor.post_order): fixer.log = self.fixer_log def _check(self, before, after): before = support.reformat(before) after = support.reformat(after) tree = self.refactor.refactor_string(before, self.filename) self.assertEqual(after, str(tree)) return tree def check(self, before, after, ignore_warnings=False): tree = self._check(before, after) self.assertTrue(tree.was_changed) if not ignore_warnings: self.assertEqual(self.fixer_log, []) def warns(self, before, after, message, unchanged=False): tree = self._check(before, after) self.assertIn(message, "".join(self.fixer_log)) if not unchanged: self.assertTrue(tree.was_changed) def warns_unchanged(self, before, message): self.warns(before, before, message, unchanged=True) def unchanged(self, before, ignore_warnings=False): self._check(before, before) if not ignore_warnings: self.assertEqual(self.fixer_log, []) def assert_runs_after(self, *names): fixes = [self.fixer] fixes.extend(names) r = support.get_refactorer("lib2to3", fixes) (pre, post) = r.get_fixers() n = "fix_" + self.fixer if post and post[-1].__class__.__module__.endswith(n): # We're the last fixer to run return if pre and pre[-1].__class__.__module__.endswith(n) and not post: # We're the last in pre and post is empty return self.fail("Fixer run order (%s) is incorrect; %s should be last."\ %(", ".join([x.__class__.__module__ for x in (pre+post)]), n)) class Test_ne(FixerTestCase): fixer = "ne" def test_basic(self): b = """if x <> y: pass""" a = """if x != y: pass""" self.check(b, a) def test_no_spaces(self): b = """if x<>y: pass""" a = """if x!=y: pass""" self.check(b, a) def test_chained(self): b = """if x<>y<>z: pass""" a = """if x!=y!=z: pass""" self.check(b, a) class Test_has_key(FixerTestCase): fixer = "has_key" def test_1(self): b = """x = d.has_key("x") or d.has_key("y")""" a = """x = "x" in d or "y" in d""" self.check(b, a) def test_2(self): b = """x = a.b.c.d.has_key("x") ** 3""" a = """x = ("x" in a.b.c.d) ** 3""" self.check(b, a) def test_3(self): b = """x = a.b.has_key(1 + 2).__repr__()""" a = """x = (1 + 2 in a.b).__repr__()""" self.check(b, a) def test_4(self): b = """x = a.b.has_key(1 + 2).__repr__() ** -3 ** 4""" a = """x = (1 + 2 in a.b).__repr__() ** -3 ** 4""" self.check(b, a) def test_5(self): b = """x = a.has_key(f or g)""" a = """x = (f or g) in a""" self.check(b, a) def test_6(self): b = """x = a + b.has_key(c)""" a = """x = a + (c in b)""" self.check(b, a) def test_7(self): b = """x = a.has_key(lambda: 12)""" a = """x = (lambda: 12) in a""" self.check(b, a) def test_8(self): b = """x = a.has_key(a for a in b)""" a = """x = (a for a in b) in a""" self.check(b, a) def test_9(self): b = """if not a.has_key(b): pass""" a = """if b not in a: pass""" self.check(b, a) def test_10(self): b = """if not a.has_key(b).__repr__(): pass""" a = """if not (b in a).__repr__(): pass""" self.check(b, a) def test_11(self): b = """if not a.has_key(b) ** 2: pass""" a = """if not (b in a) ** 2: pass""" self.check(b, a) class Test_apply(FixerTestCase): fixer = "apply" def test_1(self): b = """x = apply(f, g + h)""" a = """x = f(*g + h)""" self.check(b, a) def test_2(self): b = """y = apply(f, g, h)""" a = """y = f(*g, **h)""" self.check(b, a) def test_3(self): b = """z = apply(fs[0], g or h, h or g)""" a = """z = fs[0](*g or h, **h or g)""" self.check(b, a) def test_4(self): b = """apply(f, (x, y) + t)""" a = """f(*(x, y) + t)""" self.check(b, a) def test_5(self): b = """apply(f, args,)""" a = """f(*args)""" self.check(b, a) def test_6(self): b = """apply(f, args, kwds,)""" a = """f(*args, **kwds)""" self.check(b, a) # Test that complex functions are parenthesized def test_complex_1(self): b = """x = apply(f+g, args)""" a = """x = (f+g)(*args)""" self.check(b, a) def test_complex_2(self): b = """x = apply(f*g, args)""" a = """x = (f*g)(*args)""" self.check(b, a) def test_complex_3(self): b = """x = apply(f**g, args)""" a = """x = (f**g)(*args)""" self.check(b, a) # But dotted names etc. not def test_dotted_name(self): b = """x = apply(f.g, args)""" a = """x = f.g(*args)""" self.check(b, a) def test_subscript(self): b = """x = apply(f[x], args)""" a = """x = f[x](*args)""" self.check(b, a) def test_call(self): b = """x = apply(f(), args)""" a = """x = f()(*args)""" self.check(b, a) # Extreme case def test_extreme(self): b = """x = apply(a.b.c.d.e.f, args, kwds)""" a = """x = a.b.c.d.e.f(*args, **kwds)""" self.check(b, a) # XXX Comments in weird places still get lost def test_weird_comments(self): b = """apply( # foo f, # bar args)""" a = """f(*args)""" self.check(b, a) # These should *not* be touched def test_unchanged_1(self): s = """apply()""" self.unchanged(s) def test_unchanged_2(self): s = """apply(f)""" self.unchanged(s) def test_unchanged_3(self): s = """apply(f,)""" self.unchanged(s) def test_unchanged_4(self): s = """apply(f, args, kwds, extras)""" self.unchanged(s) def test_unchanged_5(self): s = """apply(f, *args, **kwds)""" self.unchanged(s) def test_unchanged_6(self): s = """apply(f, *args)""" self.unchanged(s) def test_unchanged_6b(self): s = """apply(f, **kwds)""" self.unchanged(s) def test_unchanged_7(self): s = """apply(func=f, args=args, kwds=kwds)""" self.unchanged(s) def test_unchanged_8(self): s = """apply(f, args=args, kwds=kwds)""" self.unchanged(s) def test_unchanged_9(self): s = """apply(f, args, kwds=kwds)""" self.unchanged(s) def test_space_1(self): a = """apply( f, args, kwds)""" b = """f(*args, **kwds)""" self.check(a, b) def test_space_2(self): a = """apply( f ,args,kwds )""" b = """f(*args, **kwds)""" self.check(a, b) class Test_reload(FixerTestCase): fixer = "reload" def test(self): b = """reload(a)""" a = """import imp\nimp.reload(a)""" self.check(b, a) def test_comment(self): b = """reload( a ) # comment""" a = """import imp\nimp.reload( a ) # comment""" self.check(b, a) # PEP 8 comments b = """reload( a ) # comment""" a = """import imp\nimp.reload( a ) # comment""" self.check(b, a) def test_space(self): b = """reload( a )""" a = """import imp\nimp.reload( a )""" self.check(b, a) b = """reload( a)""" a = """import imp\nimp.reload( a)""" self.check(b, a) b = """reload(a )""" a = """import imp\nimp.reload(a )""" self.check(b, a) def test_unchanged(self): s = """reload(a=1)""" self.unchanged(s) s = """reload(f, g)""" self.unchanged(s) s = """reload(f, *h)""" self.unchanged(s) s = """reload(f, *h, **i)""" self.unchanged(s) s = """reload(f, **i)""" self.unchanged(s) s = """reload(*h, **i)""" self.unchanged(s) s = """reload(*h)""" self.unchanged(s) s = """reload(**i)""" self.unchanged(s) s = """reload()""" self.unchanged(s) class Test_intern(FixerTestCase): fixer = "intern" def test_prefix_preservation(self): b = """x = intern( a )""" a = """import sys\nx = sys.intern( a )""" self.check(b, a) b = """y = intern("b" # test )""" a = """import sys\ny = sys.intern("b" # test )""" self.check(b, a) b = """z = intern(a+b+c.d, )""" a = """import sys\nz = sys.intern(a+b+c.d, )""" self.check(b, a) def test(self): b = """x = intern(a)""" a = """import sys\nx = sys.intern(a)""" self.check(b, a) b = """z = intern(a+b+c.d,)""" a = """import sys\nz = sys.intern(a+b+c.d,)""" self.check(b, a) b = """intern("y%s" % 5).replace("y", "")""" a = """import sys\nsys.intern("y%s" % 5).replace("y", "")""" self.check(b, a) # These should not be refactored def test_unchanged(self): s = """intern(a=1)""" self.unchanged(s) s = """intern(f, g)""" self.unchanged(s) s = """intern(*h)""" self.unchanged(s) s = """intern(**i)""" self.unchanged(s) s = """intern()""" self.unchanged(s) class Test_reduce(FixerTestCase): fixer = "reduce" def test_simple_call(self): b = "reduce(a, b, c)" a = "from functools import reduce\nreduce(a, b, c)" self.check(b, a) def test_bug_7253(self): # fix_tuple_params was being bad and orphaning nodes in the tree. b = "def x(arg): reduce(sum, [])" a = "from functools import reduce\ndef x(arg): reduce(sum, [])" self.check(b, a) def test_call_with_lambda(self): b = "reduce(lambda x, y: x + y, seq)" a = "from functools import reduce\nreduce(lambda x, y: x + y, seq)" self.check(b, a) def test_unchanged(self): s = "reduce(a)" self.unchanged(s) s = "reduce(a, b=42)" self.unchanged(s) s = "reduce(a, b, c, d)" self.unchanged(s) s = "reduce(**c)" self.unchanged(s) s = "reduce()" self.unchanged(s) class Test_print(FixerTestCase): fixer = "print" def test_prefix_preservation(self): b = """print 1, 1+1, 1+1+1""" a = """print(1, 1+1, 1+1+1)""" self.check(b, a) def test_idempotency(self): s = """print()""" self.unchanged(s) s = """print('')""" self.unchanged(s) def test_idempotency_print_as_function(self): self.refactor.driver.grammar = pygram.python_grammar_no_print_statement s = """print(1, 1+1, 1+1+1)""" self.unchanged(s) s = """print()""" self.unchanged(s) s = """print('')""" self.unchanged(s) def test_1(self): b = """print 1, 1+1, 1+1+1""" a = """print(1, 1+1, 1+1+1)""" self.check(b, a) def test_2(self): b = """print 1, 2""" a = """print(1, 2)""" self.check(b, a) def test_3(self): b = """print""" a = """print()""" self.check(b, a) def test_4(self): # from bug 3000 b = """print whatever; print""" a = """print(whatever); print()""" self.check(b, a) def test_5(self): b = """print; print whatever;""" a = """print(); print(whatever);""" self.check(b, a) def test_tuple(self): b = """print (a, b, c)""" a = """print((a, b, c))""" self.check(b, a) # trailing commas def test_trailing_comma_1(self): b = """print 1, 2, 3,""" a = """print(1, 2, 3, end=' ')""" self.check(b, a) def test_trailing_comma_2(self): b = """print 1, 2,""" a = """print(1, 2, end=' ')""" self.check(b, a) def test_trailing_comma_3(self): b = """print 1,""" a = """print(1, end=' ')""" self.check(b, a) # >> stuff def test_vargs_without_trailing_comma(self): b = """print >>sys.stderr, 1, 2, 3""" a = """print(1, 2, 3, file=sys.stderr)""" self.check(b, a) def test_with_trailing_comma(self): b = """print >>sys.stderr, 1, 2,""" a = """print(1, 2, end=' ', file=sys.stderr)""" self.check(b, a) def test_no_trailing_comma(self): b = """print >>sys.stderr, 1+1""" a = """print(1+1, file=sys.stderr)""" self.check(b, a) def test_spaces_before_file(self): b = """print >> sys.stderr""" a = """print(file=sys.stderr)""" self.check(b, a) def test_with_future_print_function(self): s = "from __future__ import print_function\n" \ "print('Hai!', end=' ')" self.unchanged(s) b = "print 'Hello, world!'" a = "print('Hello, world!')" self.check(b, a) class Test_exec(FixerTestCase): fixer = "exec" def test_prefix_preservation(self): b = """ exec code in ns1, ns2""" a = """ exec(code, ns1, ns2)""" self.check(b, a) def test_basic(self): b = """exec code""" a = """exec(code)""" self.check(b, a) def test_with_globals(self): b = """exec code in ns""" a = """exec(code, ns)""" self.check(b, a) def test_with_globals_locals(self): b = """exec code in ns1, ns2""" a = """exec(code, ns1, ns2)""" self.check(b, a) def test_complex_1(self): b = """exec (a.b()) in ns""" a = """exec((a.b()), ns)""" self.check(b, a) def test_complex_2(self): b = """exec a.b() + c in ns""" a = """exec(a.b() + c, ns)""" self.check(b, a) # These should not be touched def test_unchanged_1(self): s = """exec(code)""" self.unchanged(s) def test_unchanged_2(self): s = """exec (code)""" self.unchanged(s) def test_unchanged_3(self): s = """exec(code, ns)""" self.unchanged(s) def test_unchanged_4(self): s = """exec(code, ns1, ns2)""" self.unchanged(s) class Test_repr(FixerTestCase): fixer = "repr" def test_prefix_preservation(self): b = """x = `1 + 2`""" a = """x = repr(1 + 2)""" self.check(b, a) def test_simple_1(self): b = """x = `1 + 2`""" a = """x = repr(1 + 2)""" self.check(b, a) def test_simple_2(self): b = """y = `x`""" a = """y = repr(x)""" self.check(b, a) def test_complex(self): b = """z = `y`.__repr__()""" a = """z = repr(y).__repr__()""" self.check(b, a) def test_tuple(self): b = """x = `1, 2, 3`""" a = """x = repr((1, 2, 3))""" self.check(b, a) def test_nested(self): b = """x = `1 + `2``""" a = """x = repr(1 + repr(2))""" self.check(b, a) def test_nested_tuples(self): b = """x = `1, 2 + `3, 4``""" a = """x = repr((1, 2 + repr((3, 4))))""" self.check(b, a) class Test_except(FixerTestCase): fixer = "except" def test_prefix_preservation(self): b = """ try: pass except (RuntimeError, ImportError), e: pass""" a = """ try: pass except (RuntimeError, ImportError) as e: pass""" self.check(b, a) def test_simple(self): b = """ try: pass except Foo, e: pass""" a = """ try: pass except Foo as e: pass""" self.check(b, a) def test_simple_no_space_before_target(self): b = """ try: pass except Foo,e: pass""" a = """ try: pass except Foo as e: pass""" self.check(b, a) def test_tuple_unpack(self): b = """ def foo(): try: pass except Exception, (f, e): pass except ImportError, e: pass""" a = """ def foo(): try: pass except Exception as xxx_todo_changeme: (f, e) = xxx_todo_changeme.args pass except ImportError as e: pass""" self.check(b, a) def test_multi_class(self): b = """ try: pass except (RuntimeError, ImportError), e: pass""" a = """ try: pass except (RuntimeError, ImportError) as e: pass""" self.check(b, a) def test_list_unpack(self): b = """ try: pass except Exception, [a, b]: pass""" a = """ try: pass except Exception as xxx_todo_changeme: [a, b] = xxx_todo_changeme.args pass""" self.check(b, a) def test_weird_target_1(self): b = """ try: pass except Exception, d[5]: pass""" a = """ try: pass except Exception as xxx_todo_changeme: d[5] = xxx_todo_changeme pass""" self.check(b, a) def test_weird_target_2(self): b = """ try: pass except Exception, a.foo: pass""" a = """ try: pass except Exception as xxx_todo_changeme: a.foo = xxx_todo_changeme pass""" self.check(b, a) def test_weird_target_3(self): b = """ try: pass except Exception, a().foo: pass""" a = """ try: pass except Exception as xxx_todo_changeme: a().foo = xxx_todo_changeme pass""" self.check(b, a) def test_bare_except(self): b = """ try: pass except Exception, a: pass except: pass""" a = """ try: pass except Exception as a: pass except: pass""" self.check(b, a) def test_bare_except_and_else_finally(self): b = """ try: pass except Exception, a: pass except: pass else: pass finally: pass""" a = """ try: pass except Exception as a: pass except: pass else: pass finally: pass""" self.check(b, a) def test_multi_fixed_excepts_before_bare_except(self): b = """ try: pass except TypeError, b: pass except Exception, a: pass except: pass""" a = """ try: pass except TypeError as b: pass except Exception as a: pass except: pass""" self.check(b, a) def test_one_line_suites(self): b = """ try: raise TypeError except TypeError, e: pass """ a = """ try: raise TypeError except TypeError as e: pass """ self.check(b, a) b = """ try: raise TypeError except TypeError, e: pass """ a = """ try: raise TypeError except TypeError as e: pass """ self.check(b, a) b = """ try: raise TypeError except TypeError, e: pass """ a = """ try: raise TypeError except TypeError as e: pass """ self.check(b, a) b = """ try: raise TypeError except TypeError, e: pass else: function() finally: done() """ a = """ try: raise TypeError except TypeError as e: pass else: function() finally: done() """ self.check(b, a) # These should not be touched: def test_unchanged_1(self): s = """ try: pass except: pass""" self.unchanged(s) def test_unchanged_2(self): s = """ try: pass except Exception: pass""" self.unchanged(s) def test_unchanged_3(self): s = """ try: pass except (Exception, SystemExit): pass""" self.unchanged(s) class Test_raise(FixerTestCase): fixer = "raise" def test_basic(self): b = """raise Exception, 5""" a = """raise Exception(5)""" self.check(b, a) def test_prefix_preservation(self): b = """raise Exception,5""" a = """raise Exception(5)""" self.check(b, a) b = """raise Exception, 5""" a = """raise Exception(5)""" self.check(b, a) def test_with_comments(self): b = """raise Exception, 5 # foo""" a = """raise Exception(5) # foo""" self.check(b, a) b = """raise E, (5, 6) % (a, b) # foo""" a = """raise E((5, 6) % (a, b)) # foo""" self.check(b, a) b = """def foo(): raise Exception, 5, 6 # foo""" a = """def foo(): raise Exception(5).with_traceback(6) # foo""" self.check(b, a) def test_None_value(self): b = """raise Exception(5), None, tb""" a = """raise Exception(5).with_traceback(tb)""" self.check(b, a) def test_tuple_value(self): b = """raise Exception, (5, 6, 7)""" a = """raise Exception(5, 6, 7)""" self.check(b, a) def test_tuple_detection(self): b = """raise E, (5, 6) % (a, b)""" a = """raise E((5, 6) % (a, b))""" self.check(b, a) def test_tuple_exc_1(self): b = """raise (((E1, E2), E3), E4), V""" a = """raise E1(V)""" self.check(b, a) def test_tuple_exc_2(self): b = """raise (E1, (E2, E3), E4), V""" a = """raise E1(V)""" self.check(b, a) # These should produce a warning def test_string_exc(self): s = """raise 'foo'""" self.warns_unchanged(s, "Python 3 does not support string exceptions") def test_string_exc_val(self): s = """raise "foo", 5""" self.warns_unchanged(s, "Python 3 does not support string exceptions") def test_string_exc_val_tb(self): s = """raise "foo", 5, 6""" self.warns_unchanged(s, "Python 3 does not support string exceptions") # These should result in traceback-assignment def test_tb_1(self): b = """def foo(): raise Exception, 5, 6""" a = """def foo(): raise Exception(5).with_traceback(6)""" self.check(b, a) def test_tb_2(self): b = """def foo(): a = 5 raise Exception, 5, 6 b = 6""" a = """def foo(): a = 5 raise Exception(5).with_traceback(6) b = 6""" self.check(b, a) def test_tb_3(self): b = """def foo(): raise Exception,5,6""" a = """def foo(): raise Exception(5).with_traceback(6)""" self.check(b, a) def test_tb_4(self): b = """def foo(): a = 5 raise Exception,5,6 b = 6""" a = """def foo(): a = 5 raise Exception(5).with_traceback(6) b = 6""" self.check(b, a) def test_tb_5(self): b = """def foo(): raise Exception, (5, 6, 7), 6""" a = """def foo(): raise Exception(5, 6, 7).with_traceback(6)""" self.check(b, a) def test_tb_6(self): b = """def foo(): a = 5 raise Exception, (5, 6, 7), 6 b = 6""" a = """def foo(): a = 5 raise Exception(5, 6, 7).with_traceback(6) b = 6""" self.check(b, a) class Test_throw(FixerTestCase): fixer = "throw" def test_1(self): b = """g.throw(Exception, 5)""" a = """g.throw(Exception(5))""" self.check(b, a) def test_2(self): b = """g.throw(Exception,5)""" a = """g.throw(Exception(5))""" self.check(b, a) def test_3(self): b = """g.throw(Exception, (5, 6, 7))""" a = """g.throw(Exception(5, 6, 7))""" self.check(b, a) def test_4(self): b = """5 + g.throw(Exception, 5)""" a = """5 + g.throw(Exception(5))""" self.check(b, a) # These should produce warnings def test_warn_1(self): s = """g.throw("foo")""" self.warns_unchanged(s, "Python 3 does not support string exceptions") def test_warn_2(self): s = """g.throw("foo", 5)""" self.warns_unchanged(s, "Python 3 does not support string exceptions") def test_warn_3(self): s = """g.throw("foo", 5, 6)""" self.warns_unchanged(s, "Python 3 does not support string exceptions") # These should not be touched def test_untouched_1(self): s = """g.throw(Exception)""" self.unchanged(s) def test_untouched_2(self): s = """g.throw(Exception(5, 6))""" self.unchanged(s) def test_untouched_3(self): s = """5 + g.throw(Exception(5, 6))""" self.unchanged(s) # These should result in traceback-assignment def test_tb_1(self): b = """def foo(): g.throw(Exception, 5, 6)""" a = """def foo(): g.throw(Exception(5).with_traceback(6))""" self.check(b, a) def test_tb_2(self): b = """def foo(): a = 5 g.throw(Exception, 5, 6) b = 6""" a = """def foo(): a = 5 g.throw(Exception(5).with_traceback(6)) b = 6""" self.check(b, a) def test_tb_3(self): b = """def foo(): g.throw(Exception,5,6)""" a = """def foo(): g.throw(Exception(5).with_traceback(6))""" self.check(b, a) def test_tb_4(self): b = """def foo(): a = 5 g.throw(Exception,5,6) b = 6""" a = """def foo(): a = 5 g.throw(Exception(5).with_traceback(6)) b = 6""" self.check(b, a) def test_tb_5(self): b = """def foo(): g.throw(Exception, (5, 6, 7), 6)""" a = """def foo(): g.throw(Exception(5, 6, 7).with_traceback(6))""" self.check(b, a) def test_tb_6(self): b = """def foo(): a = 5 g.throw(Exception, (5, 6, 7), 6) b = 6""" a = """def foo(): a = 5 g.throw(Exception(5, 6, 7).with_traceback(6)) b = 6""" self.check(b, a) def test_tb_7(self): b = """def foo(): a + g.throw(Exception, 5, 6)""" a = """def foo(): a + g.throw(Exception(5).with_traceback(6))""" self.check(b, a) def test_tb_8(self): b = """def foo(): a = 5 a + g.throw(Exception, 5, 6) b = 6""" a = """def foo(): a = 5 a + g.throw(Exception(5).with_traceback(6)) b = 6""" self.check(b, a) class Test_long(FixerTestCase): fixer = "long" def test_1(self): b = """x = long(x)""" a = """x = int(x)""" self.check(b, a) def test_2(self): b = """y = isinstance(x, long)""" a = """y = isinstance(x, int)""" self.check(b, a) def test_3(self): b = """z = type(x) in (int, long)""" a = """z = type(x) in (int, int)""" self.check(b, a) def test_unchanged(self): s = """long = True""" self.unchanged(s) s = """s.long = True""" self.unchanged(s) s = """def long(): pass""" self.unchanged(s) s = """class long(): pass""" self.unchanged(s) s = """def f(long): pass""" self.unchanged(s) s = """def f(g, long): pass""" self.unchanged(s) s = """def f(x, long=True): pass""" self.unchanged(s) def test_prefix_preservation(self): b = """x = long( x )""" a = """x = int( x )""" self.check(b, a) class Test_execfile(FixerTestCase): fixer = "execfile" def test_conversion(self): b = """execfile("fn")""" a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'))""" self.check(b, a) b = """execfile("fn", glob)""" a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), glob)""" self.check(b, a) b = """execfile("fn", glob, loc)""" a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), glob, loc)""" self.check(b, a) b = """execfile("fn", globals=glob)""" a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), globals=glob)""" self.check(b, a) b = """execfile("fn", locals=loc)""" a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), locals=loc)""" self.check(b, a) b = """execfile("fn", globals=glob, locals=loc)""" a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), globals=glob, locals=loc)""" self.check(b, a) def test_spacing(self): b = """execfile( "fn" )""" a = """exec(compile(open( "fn", "rb" ).read(), "fn", 'exec'))""" self.check(b, a) b = """execfile("fn", globals = glob)""" a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), globals = glob)""" self.check(b, a) class Test_isinstance(FixerTestCase): fixer = "isinstance" def test_remove_multiple_items(self): b = """isinstance(x, (int, int, int))""" a = """isinstance(x, int)""" self.check(b, a) b = """isinstance(x, (int, float, int, int, float))""" a = """isinstance(x, (int, float))""" self.check(b, a) b = """isinstance(x, (int, float, int, int, float, str))""" a = """isinstance(x, (int, float, str))""" self.check(b, a) b = """isinstance(foo() + bar(), (x(), y(), x(), int, int))""" a = """isinstance(foo() + bar(), (x(), y(), x(), int))""" self.check(b, a) def test_prefix_preservation(self): b = """if isinstance( foo(), ( bar, bar, baz )) : pass""" a = """if isinstance( foo(), ( bar, baz )) : pass""" self.check(b, a) def test_unchanged(self): self.unchanged("isinstance(x, (str, int))") class Test_dict(FixerTestCase): fixer = "dict" def test_prefix_preservation(self): b = "if d. keys ( ) : pass" a = "if list(d. keys ( )) : pass" self.check(b, a) b = "if d. items ( ) : pass" a = "if list(d. items ( )) : pass" self.check(b, a) b = "if d. iterkeys ( ) : pass" a = "if iter(d. keys ( )) : pass" self.check(b, a) b = "[i for i in d. iterkeys( ) ]" a = "[i for i in d. keys( ) ]" self.check(b, a) b = "if d. viewkeys ( ) : pass" a = "if d. keys ( ) : pass" self.check(b, a) b = "[i for i in d. viewkeys( ) ]" a = "[i for i in d. keys( ) ]" self.check(b, a) def test_trailing_comment(self): b = "d.keys() # foo" a = "list(d.keys()) # foo" self.check(b, a) b = "d.items() # foo" a = "list(d.items()) # foo" self.check(b, a) b = "d.iterkeys() # foo" a = "iter(d.keys()) # foo" self.check(b, a) b = """[i for i in d.iterkeys() # foo ]""" a = """[i for i in d.keys() # foo ]""" self.check(b, a) b = """[i for i in d.iterkeys() # foo ]""" a = """[i for i in d.keys() # foo ]""" self.check(b, a) b = "d.viewitems() # foo" a = "d.items() # foo" self.check(b, a) def test_unchanged(self): for wrapper in fixer_util.consuming_calls: s = "s = %s(d.keys())" % wrapper self.unchanged(s) s = "s = %s(d.values())" % wrapper self.unchanged(s) s = "s = %s(d.items())" % wrapper self.unchanged(s) def test_01(self): b = "d.keys()" a = "list(d.keys())" self.check(b, a) b = "a[0].foo().keys()" a = "list(a[0].foo().keys())" self.check(b, a) def test_02(self): b = "d.items()" a = "list(d.items())" self.check(b, a) def test_03(self): b = "d.values()" a = "list(d.values())" self.check(b, a) def test_04(self): b = "d.iterkeys()" a = "iter(d.keys())" self.check(b, a) def test_05(self): b = "d.iteritems()" a = "iter(d.items())" self.check(b, a) def test_06(self): b = "d.itervalues()" a = "iter(d.values())" self.check(b, a) def test_07(self): s = "list(d.keys())" self.unchanged(s) def test_08(self): s = "sorted(d.keys())" self.unchanged(s) def test_09(self): b = "iter(d.keys())" a = "iter(list(d.keys()))" self.check(b, a) def test_10(self): b = "foo(d.keys())" a = "foo(list(d.keys()))" self.check(b, a) def test_11(self): b = "for i in d.keys(): print i" a = "for i in list(d.keys()): print i" self.check(b, a) def test_12(self): b = "for i in d.iterkeys(): print i" a = "for i in d.keys(): print i" self.check(b, a) def test_13(self): b = "[i for i in d.keys()]" a = "[i for i in list(d.keys())]" self.check(b, a) def test_14(self): b = "[i for i in d.iterkeys()]" a = "[i for i in d.keys()]" self.check(b, a) def test_15(self): b = "(i for i in d.keys())" a = "(i for i in list(d.keys()))" self.check(b, a) def test_16(self): b = "(i for i in d.iterkeys())" a = "(i for i in d.keys())" self.check(b, a) def test_17(self): b = "iter(d.iterkeys())" a = "iter(d.keys())" self.check(b, a) def test_18(self): b = "list(d.iterkeys())" a = "list(d.keys())" self.check(b, a) def test_19(self): b = "sorted(d.iterkeys())" a = "sorted(d.keys())" self.check(b, a) def test_20(self): b = "foo(d.iterkeys())" a = "foo(iter(d.keys()))" self.check(b, a) def test_21(self): b = "print h.iterkeys().next()" a = "print iter(h.keys()).next()" self.check(b, a) def test_22(self): b = "print h.keys()[0]" a = "print list(h.keys())[0]" self.check(b, a) def test_23(self): b = "print list(h.iterkeys().next())" a = "print list(iter(h.keys()).next())" self.check(b, a) def test_24(self): b = "for x in h.keys()[0]: print x" a = "for x in list(h.keys())[0]: print x" self.check(b, a) def test_25(self): b = "d.viewkeys()" a = "d.keys()" self.check(b, a) def test_26(self): b = "d.viewitems()" a = "d.items()" self.check(b, a) def test_27(self): b = "d.viewvalues()" a = "d.values()" self.check(b, a) def test_28(self): b = "[i for i in d.viewkeys()]" a = "[i for i in d.keys()]" self.check(b, a) def test_29(self): b = "(i for i in d.viewkeys())" a = "(i for i in d.keys())" self.check(b, a) def test_30(self): b = "iter(d.viewkeys())" a = "iter(d.keys())" self.check(b, a) def test_31(self): b = "list(d.viewkeys())" a = "list(d.keys())" self.check(b, a) def test_32(self): b = "sorted(d.viewkeys())" a = "sorted(d.keys())" self.check(b, a) class Test_xrange(FixerTestCase): fixer = "xrange" def test_prefix_preservation(self): b = """x = xrange( 10 )""" a = """x = range( 10 )""" self.check(b, a) b = """x = xrange( 1 , 10 )""" a = """x = range( 1 , 10 )""" self.check(b, a) b = """x = xrange( 0 , 10 , 2 )""" a = """x = range( 0 , 10 , 2 )""" self.check(b, a) def test_single_arg(self): b = """x = xrange(10)""" a = """x = range(10)""" self.check(b, a) def test_two_args(self): b = """x = xrange(1, 10)""" a = """x = range(1, 10)""" self.check(b, a) def test_three_args(self): b = """x = xrange(0, 10, 2)""" a = """x = range(0, 10, 2)""" self.check(b, a) def test_wrap_in_list(self): b = """x = range(10, 3, 9)""" a = """x = list(range(10, 3, 9))""" self.check(b, a) b = """x = foo(range(10, 3, 9))""" a = """x = foo(list(range(10, 3, 9)))""" self.check(b, a) b = """x = range(10, 3, 9) + [4]""" a = """x = list(range(10, 3, 9)) + [4]""" self.check(b, a) b = """x = range(10)[::-1]""" a = """x = list(range(10))[::-1]""" self.check(b, a) b = """x = range(10) [3]""" a = """x = list(range(10)) [3]""" self.check(b, a) def test_xrange_in_for(self): b = """for i in xrange(10):\n j=i""" a = """for i in range(10):\n j=i""" self.check(b, a) b = """[i for i in xrange(10)]""" a = """[i for i in range(10)]""" self.check(b, a) def test_range_in_for(self): self.unchanged("for i in range(10): pass") self.unchanged("[i for i in range(10)]") def test_in_contains_test(self): self.unchanged("x in range(10, 3, 9)") def test_in_consuming_context(self): for call in fixer_util.consuming_calls: self.unchanged("a = %s(range(10))" % call) class Test_xrange_with_reduce(FixerTestCase): def setUp(self): super(Test_xrange_with_reduce, self).setUp(["xrange", "reduce"]) def test_double_transform(self): b = """reduce(x, xrange(5))""" a = """from functools import reduce reduce(x, range(5))""" self.check(b, a) class Test_raw_input(FixerTestCase): fixer = "raw_input" def test_prefix_preservation(self): b = """x = raw_input( )""" a = """x = input( )""" self.check(b, a) b = """x = raw_input( '' )""" a = """x = input( '' )""" self.check(b, a) def test_1(self): b = """x = raw_input()""" a = """x = input()""" self.check(b, a) def test_2(self): b = """x = raw_input('')""" a = """x = input('')""" self.check(b, a) def test_3(self): b = """x = raw_input('prompt')""" a = """x = input('prompt')""" self.check(b, a) def test_4(self): b = """x = raw_input(foo(a) + 6)""" a = """x = input(foo(a) + 6)""" self.check(b, a) def test_5(self): b = """x = raw_input(invite).split()""" a = """x = input(invite).split()""" self.check(b, a) def test_6(self): b = """x = raw_input(invite) . split ()""" a = """x = input(invite) . split ()""" self.check(b, a) def test_8(self): b = "x = int(raw_input())" a = "x = int(input())" self.check(b, a) class Test_funcattrs(FixerTestCase): fixer = "funcattrs" attrs = ["closure", "doc", "name", "defaults", "code", "globals", "dict"] def test(self): for attr in self.attrs: b = "a.func_%s" % attr a = "a.__%s__" % attr self.check(b, a) b = "self.foo.func_%s.foo_bar" % attr a = "self.foo.__%s__.foo_bar" % attr self.check(b, a) def test_unchanged(self): for attr in self.attrs: s = "foo(func_%s + 5)" % attr self.unchanged(s) s = "f(foo.__%s__)" % attr self.unchanged(s) s = "f(foo.__%s__.foo)" % attr self.unchanged(s) class Test_xreadlines(FixerTestCase): fixer = "xreadlines" def test_call(self): b = "for x in f.xreadlines(): pass" a = "for x in f: pass" self.check(b, a) b = "for x in foo().xreadlines(): pass" a = "for x in foo(): pass" self.check(b, a) b = "for x in (5 + foo()).xreadlines(): pass" a = "for x in (5 + foo()): pass" self.check(b, a) def test_attr_ref(self): b = "foo(f.xreadlines + 5)" a = "foo(f.__iter__ + 5)" self.check(b, a) b = "foo(f().xreadlines + 5)" a = "foo(f().__iter__ + 5)" self.check(b, a) b = "foo((5 + f()).xreadlines + 5)" a = "foo((5 + f()).__iter__ + 5)" self.check(b, a) def test_unchanged(self): s = "for x in f.xreadlines(5): pass" self.unchanged(s) s = "for x in f.xreadlines(k=5): pass" self.unchanged(s) s = "for x in f.xreadlines(*k, **v): pass" self.unchanged(s) s = "foo(xreadlines)" self.unchanged(s) class ImportsFixerTests: def test_import_module(self): for old, new in self.modules.items(): b = "import %s" % old a = "import %s" % new self.check(b, a) b = "import foo, %s, bar" % old a = "import foo, %s, bar" % new self.check(b, a) def test_import_from(self): for old, new in self.modules.items(): b = "from %s import foo" % old a = "from %s import foo" % new self.check(b, a) b = "from %s import foo, bar" % old a = "from %s import foo, bar" % new self.check(b, a) b = "from %s import (yes, no)" % old a = "from %s import (yes, no)" % new self.check(b, a) def test_import_module_as(self): for old, new in self.modules.items(): b = "import %s as foo_bar" % old a = "import %s as foo_bar" % new self.check(b, a) b = "import %s as foo_bar" % old a = "import %s as foo_bar" % new self.check(b, a) def test_import_from_as(self): for old, new in self.modules.items(): b = "from %s import foo as bar" % old a = "from %s import foo as bar" % new self.check(b, a) def test_star(self): for old, new in self.modules.items(): b = "from %s import *" % old a = "from %s import *" % new self.check(b, a) def test_import_module_usage(self): for old, new in self.modules.items(): b = """ import %s foo(%s.bar) """ % (old, old) a = """ import %s foo(%s.bar) """ % (new, new) self.check(b, a) b = """ from %s import x %s = 23 """ % (old, old) a = """ from %s import x %s = 23 """ % (new, old) self.check(b, a) s = """ def f(): %s.method() """ % (old,) self.unchanged(s) # test nested usage b = """ import %s %s.bar(%s.foo) """ % (old, old, old) a = """ import %s %s.bar(%s.foo) """ % (new, new, new) self.check(b, a) b = """ import %s x.%s """ % (old, old) a = """ import %s x.%s """ % (new, old) self.check(b, a) class Test_imports(FixerTestCase, ImportsFixerTests): fixer = "imports" from ..fixes.fix_imports import MAPPING as modules def test_multiple_imports(self): b = """import urlparse, cStringIO""" a = """import urllib.parse, io""" self.check(b, a) def test_multiple_imports_as(self): b = """ import copy_reg as bar, HTMLParser as foo, urlparse s = urlparse.spam(bar.foo()) """ a = """ import copyreg as bar, html.parser as foo, urllib.parse s = urllib.parse.spam(bar.foo()) """ self.check(b, a) class Test_imports2(FixerTestCase, ImportsFixerTests): fixer = "imports2" from ..fixes.fix_imports2 import MAPPING as modules class Test_imports_fixer_order(FixerTestCase, ImportsFixerTests): def setUp(self): super(Test_imports_fixer_order, self).setUp(['imports', 'imports2']) from ..fixes.fix_imports2 import MAPPING as mapping2 self.modules = mapping2.copy() from ..fixes.fix_imports import MAPPING as mapping1 for key in ('dbhash', 'dumbdbm', 'dbm', 'gdbm'): self.modules[key] = mapping1[key] def test_after_local_imports_refactoring(self): for fix in ("imports", "imports2"): self.fixer = fix self.assert_runs_after("import") class Test_urllib(FixerTestCase): fixer = "urllib" from ..fixes.fix_urllib import MAPPING as modules def test_import_module(self): for old, changes in self.modules.items(): b = "import %s" % old a = "import %s" % ", ".join(map(itemgetter(0), changes)) self.check(b, a) def test_import_from(self): for old, changes in self.modules.items(): all_members = [] for new, members in changes: for member in members: all_members.append(member) b = "from %s import %s" % (old, member) a = "from %s import %s" % (new, member) self.check(b, a) s = "from foo import %s" % member self.unchanged(s) b = "from %s import %s" % (old, ", ".join(members)) a = "from %s import %s" % (new, ", ".join(members)) self.check(b, a) s = "from foo import %s" % ", ".join(members) self.unchanged(s) # test the breaking of a module into multiple replacements b = "from %s import %s" % (old, ", ".join(all_members)) a = "\n".join(["from %s import %s" % (new, ", ".join(members)) for (new, members) in changes]) self.check(b, a) def test_import_module_as(self): for old in self.modules: s = "import %s as foo" % old self.warns_unchanged(s, "This module is now multiple modules") def test_import_from_as(self): for old, changes in self.modules.items(): for new, members in changes: for member in members: b = "from %s import %s as foo_bar" % (old, member) a = "from %s import %s as foo_bar" % (new, member) self.check(b, a) b = "from %s import %s as blah, %s" % (old, member, member) a = "from %s import %s as blah, %s" % (new, member, member) self.check(b, a) def test_star(self): for old in self.modules: s = "from %s import *" % old self.warns_unchanged(s, "Cannot handle star imports") def test_indented(self): b = """ def foo(): from urllib import urlencode, urlopen """ a = """ def foo(): from urllib.parse import urlencode from urllib.request import urlopen """ self.check(b, a) b = """ def foo(): other() from urllib import urlencode, urlopen """ a = """ def foo(): other() from urllib.parse import urlencode from urllib.request import urlopen """ self.check(b, a) def test_import_module_usage(self): for old, changes in self.modules.items(): for new, members in changes: for member in members: new_import = ", ".join([n for (n, mems) in self.modules[old]]) b = """ import %s foo(%s.%s) """ % (old, old, member) a = """ import %s foo(%s.%s) """ % (new_import, new, member) self.check(b, a) b = """ import %s %s.%s(%s.%s) """ % (old, old, member, old, member) a = """ import %s %s.%s(%s.%s) """ % (new_import, new, member, new, member) self.check(b, a) class Test_input(FixerTestCase): fixer = "input" def test_prefix_preservation(self): b = """x = input( )""" a = """x = eval(input( ))""" self.check(b, a) b = """x = input( '' )""" a = """x = eval(input( '' ))""" self.check(b, a) def test_trailing_comment(self): b = """x = input() # foo""" a = """x = eval(input()) # foo""" self.check(b, a) def test_idempotency(self): s = """x = eval(input())""" self.unchanged(s) s = """x = eval(input(''))""" self.unchanged(s) s = """x = eval(input(foo(5) + 9))""" self.unchanged(s) def test_1(self): b = """x = input()""" a = """x = eval(input())""" self.check(b, a) def test_2(self): b = """x = input('')""" a = """x = eval(input(''))""" self.check(b, a) def test_3(self): b = """x = input('prompt')""" a = """x = eval(input('prompt'))""" self.check(b, a) def test_4(self): b = """x = input(foo(5) + 9)""" a = """x = eval(input(foo(5) + 9))""" self.check(b, a) class Test_tuple_params(FixerTestCase): fixer = "tuple_params" def test_unchanged_1(self): s = """def foo(): pass""" self.unchanged(s) def test_unchanged_2(self): s = """def foo(a, b, c): pass""" self.unchanged(s) def test_unchanged_3(self): s = """def foo(a=3, b=4, c=5): pass""" self.unchanged(s) def test_1(self): b = """ def foo(((a, b), c)): x = 5""" a = """ def foo(xxx_todo_changeme): ((a, b), c) = xxx_todo_changeme x = 5""" self.check(b, a) def test_2(self): b = """ def foo(((a, b), c), d): x = 5""" a = """ def foo(xxx_todo_changeme, d): ((a, b), c) = xxx_todo_changeme x = 5""" self.check(b, a) def test_3(self): b = """ def foo(((a, b), c), d) -> e: x = 5""" a = """ def foo(xxx_todo_changeme, d) -> e: ((a, b), c) = xxx_todo_changeme x = 5""" self.check(b, a) def test_semicolon(self): b = """ def foo(((a, b), c)): x = 5; y = 7""" a = """ def foo(xxx_todo_changeme): ((a, b), c) = xxx_todo_changeme; x = 5; y = 7""" self.check(b, a) def test_keywords(self): b = """ def foo(((a, b), c), d, e=5) -> z: x = 5""" a = """ def foo(xxx_todo_changeme, d, e=5) -> z: ((a, b), c) = xxx_todo_changeme x = 5""" self.check(b, a) def test_varargs(self): b = """ def foo(((a, b), c), d, *vargs, **kwargs) -> z: x = 5""" a = """ def foo(xxx_todo_changeme, d, *vargs, **kwargs) -> z: ((a, b), c) = xxx_todo_changeme x = 5""" self.check(b, a) def test_multi_1(self): b = """ def foo(((a, b), c), (d, e, f)) -> z: x = 5""" a = """ def foo(xxx_todo_changeme, xxx_todo_changeme1) -> z: ((a, b), c) = xxx_todo_changeme (d, e, f) = xxx_todo_changeme1 x = 5""" self.check(b, a) def test_multi_2(self): b = """ def foo(x, ((a, b), c), d, (e, f, g), y) -> z: x = 5""" a = """ def foo(x, xxx_todo_changeme, d, xxx_todo_changeme1, y) -> z: ((a, b), c) = xxx_todo_changeme (e, f, g) = xxx_todo_changeme1 x = 5""" self.check(b, a) def test_docstring(self): b = """ def foo(((a, b), c), (d, e, f)) -> z: "foo foo foo foo" x = 5""" a = """ def foo(xxx_todo_changeme, xxx_todo_changeme1) -> z: "foo foo foo foo" ((a, b), c) = xxx_todo_changeme (d, e, f) = xxx_todo_changeme1 x = 5""" self.check(b, a) def test_lambda_no_change(self): s = """lambda x: x + 5""" self.unchanged(s) def test_lambda_parens_single_arg(self): b = """lambda (x): x + 5""" a = """lambda x: x + 5""" self.check(b, a) b = """lambda(x): x + 5""" a = """lambda x: x + 5""" self.check(b, a) b = """lambda ((((x)))): x + 5""" a = """lambda x: x + 5""" self.check(b, a) b = """lambda((((x)))): x + 5""" a = """lambda x: x + 5""" self.check(b, a) def test_lambda_simple(self): b = """lambda (x, y): x + f(y)""" a = """lambda x_y: x_y[0] + f(x_y[1])""" self.check(b, a) b = """lambda(x, y): x + f(y)""" a = """lambda x_y: x_y[0] + f(x_y[1])""" self.check(b, a) b = """lambda (((x, y))): x + f(y)""" a = """lambda x_y: x_y[0] + f(x_y[1])""" self.check(b, a) b = """lambda(((x, y))): x + f(y)""" a = """lambda x_y: x_y[0] + f(x_y[1])""" self.check(b, a) def test_lambda_one_tuple(self): b = """lambda (x,): x + f(x)""" a = """lambda x1: x1[0] + f(x1[0])""" self.check(b, a) b = """lambda (((x,))): x + f(x)""" a = """lambda x1: x1[0] + f(x1[0])""" self.check(b, a) def test_lambda_simple_multi_use(self): b = """lambda (x, y): x + x + f(x) + x""" a = """lambda x_y: x_y[0] + x_y[0] + f(x_y[0]) + x_y[0]""" self.check(b, a) def test_lambda_simple_reverse(self): b = """lambda (x, y): y + x""" a = """lambda x_y: x_y[1] + x_y[0]""" self.check(b, a) def test_lambda_nested(self): b = """lambda (x, (y, z)): x + y + z""" a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]""" self.check(b, a) b = """lambda (((x, (y, z)))): x + y + z""" a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]""" self.check(b, a) def test_lambda_nested_multi_use(self): b = """lambda (x, (y, z)): x + y + f(y)""" a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + f(x_y_z[1][0])""" self.check(b, a) class Test_methodattrs(FixerTestCase): fixer = "methodattrs" attrs = ["func", "self", "class"] def test(self): for attr in self.attrs: b = "a.im_%s" % attr if attr == "class": a = "a.__self__.__class__" else: a = "a.__%s__" % attr self.check(b, a) b = "self.foo.im_%s.foo_bar" % attr if attr == "class": a = "self.foo.__self__.__class__.foo_bar" else: a = "self.foo.__%s__.foo_bar" % attr self.check(b, a) def test_unchanged(self): for attr in self.attrs: s = "foo(im_%s + 5)" % attr self.unchanged(s) s = "f(foo.__%s__)" % attr self.unchanged(s) s = "f(foo.__%s__.foo)" % attr self.unchanged(s) class Test_next(FixerTestCase): fixer = "next" def test_1(self): b = """it.next()""" a = """next(it)""" self.check(b, a) def test_2(self): b = """a.b.c.d.next()""" a = """next(a.b.c.d)""" self.check(b, a) def test_3(self): b = """(a + b).next()""" a = """next((a + b))""" self.check(b, a) def test_4(self): b = """a().next()""" a = """next(a())""" self.check(b, a) def test_5(self): b = """a().next() + b""" a = """next(a()) + b""" self.check(b, a) def test_6(self): b = """c( a().next() + b)""" a = """c( next(a()) + b)""" self.check(b, a) def test_prefix_preservation_1(self): b = """ for a in b: foo(a) a.next() """ a = """ for a in b: foo(a) next(a) """ self.check(b, a) def test_prefix_preservation_2(self): b = """ for a in b: foo(a) # abc # def a.next() """ a = """ for a in b: foo(a) # abc # def next(a) """ self.check(b, a) def test_prefix_preservation_3(self): b = """ next = 5 for a in b: foo(a) a.next() """ a = """ next = 5 for a in b: foo(a) a.__next__() """ self.check(b, a, ignore_warnings=True) def test_prefix_preservation_4(self): b = """ next = 5 for a in b: foo(a) # abc # def a.next() """ a = """ next = 5 for a in b: foo(a) # abc # def a.__next__() """ self.check(b, a, ignore_warnings=True) def test_prefix_preservation_5(self): b = """ next = 5 for a in b: foo(foo(a), # abc a.next()) """ a = """ next = 5 for a in b: foo(foo(a), # abc a.__next__()) """ self.check(b, a, ignore_warnings=True) def test_prefix_preservation_6(self): b = """ for a in b: foo(foo(a), # abc a.next()) """ a = """ for a in b: foo(foo(a), # abc next(a)) """ self.check(b, a) def test_method_1(self): b = """ class A: def next(self): pass """ a = """ class A: def __next__(self): pass """ self.check(b, a) def test_method_2(self): b = """ class A(object): def next(self): pass """ a = """ class A(object): def __next__(self): pass """ self.check(b, a) def test_method_3(self): b = """ class A: def next(x): pass """ a = """ class A: def __next__(x): pass """ self.check(b, a) def test_method_4(self): b = """ class A: def __init__(self, foo): self.foo = foo def next(self): pass def __iter__(self): return self """ a = """ class A: def __init__(self, foo): self.foo = foo def __next__(self): pass def __iter__(self): return self """ self.check(b, a) def test_method_unchanged(self): s = """ class A: def next(self, a, b): pass """ self.unchanged(s) def test_shadowing_assign_simple(self): s = """ next = foo class A: def next(self, a, b): pass """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_shadowing_assign_tuple_1(self): s = """ (next, a) = foo class A: def next(self, a, b): pass """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_shadowing_assign_tuple_2(self): s = """ (a, (b, (next, c)), a) = foo class A: def next(self, a, b): pass """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_shadowing_assign_list_1(self): s = """ [next, a] = foo class A: def next(self, a, b): pass """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_shadowing_assign_list_2(self): s = """ [a, [b, [next, c]], a] = foo class A: def next(self, a, b): pass """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_builtin_assign(self): s = """ def foo(): __builtin__.next = foo class A: def next(self, a, b): pass """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_builtin_assign_in_tuple(self): s = """ def foo(): (a, __builtin__.next) = foo class A: def next(self, a, b): pass """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_builtin_assign_in_list(self): s = """ def foo(): [a, __builtin__.next] = foo class A: def next(self, a, b): pass """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_assign_to_next(self): s = """ def foo(): A.next = foo class A: def next(self, a, b): pass """ self.unchanged(s) def test_assign_to_next_in_tuple(self): s = """ def foo(): (a, A.next) = foo class A: def next(self, a, b): pass """ self.unchanged(s) def test_assign_to_next_in_list(self): s = """ def foo(): [a, A.next] = foo class A: def next(self, a, b): pass """ self.unchanged(s) def test_shadowing_import_1(self): s = """ import foo.bar as next class A: def next(self, a, b): pass """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_shadowing_import_2(self): s = """ import bar, bar.foo as next class A: def next(self, a, b): pass """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_shadowing_import_3(self): s = """ import bar, bar.foo as next, baz class A: def next(self, a, b): pass """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_shadowing_import_from_1(self): s = """ from x import next class A: def next(self, a, b): pass """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_shadowing_import_from_2(self): s = """ from x.a import next class A: def next(self, a, b): pass """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_shadowing_import_from_3(self): s = """ from x import a, next, b class A: def next(self, a, b): pass """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_shadowing_import_from_4(self): s = """ from x.a import a, next, b class A: def next(self, a, b): pass """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_shadowing_funcdef_1(self): s = """ def next(a): pass class A: def next(self, a, b): pass """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_shadowing_funcdef_2(self): b = """ def next(a): pass class A: def next(self): pass it.next() """ a = """ def next(a): pass class A: def __next__(self): pass it.__next__() """ self.warns(b, a, "Calls to builtin next() possibly shadowed") def test_shadowing_global_1(self): s = """ def f(): global next next = 5 """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_shadowing_global_2(self): s = """ def f(): global a, next, b next = 5 """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_shadowing_for_simple(self): s = """ for next in it(): pass b = 5 c = 6 """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_shadowing_for_tuple_1(self): s = """ for next, b in it(): pass b = 5 c = 6 """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_shadowing_for_tuple_2(self): s = """ for a, (next, c), b in it(): pass b = 5 c = 6 """ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") def test_noncall_access_1(self): b = """gnext = g.next""" a = """gnext = g.__next__""" self.check(b, a) def test_noncall_access_2(self): b = """f(g.next + 5)""" a = """f(g.__next__ + 5)""" self.check(b, a) def test_noncall_access_3(self): b = """f(g().next + 5)""" a = """f(g().__next__ + 5)""" self.check(b, a) class Test_nonzero(FixerTestCase): fixer = "nonzero" def test_1(self): b = """ class A: def __nonzero__(self): pass """ a = """ class A: def __bool__(self): pass """ self.check(b, a) def test_2(self): b = """ class A(object): def __nonzero__(self): pass """ a = """ class A(object): def __bool__(self): pass """ self.check(b, a) def test_unchanged_1(self): s = """ class A(object): def __bool__(self): pass """ self.unchanged(s) def test_unchanged_2(self): s = """ class A(object): def __nonzero__(self, a): pass """ self.unchanged(s) def test_unchanged_func(self): s = """ def __nonzero__(self): pass """ self.unchanged(s) class Test_numliterals(FixerTestCase): fixer = "numliterals" def test_octal_1(self): b = """0755""" a = """0o755""" self.check(b, a) def test_long_int_1(self): b = """a = 12L""" a = """a = 12""" self.check(b, a) def test_long_int_2(self): b = """a = 12l""" a = """a = 12""" self.check(b, a) def test_long_hex(self): b = """b = 0x12l""" a = """b = 0x12""" self.check(b, a) def test_comments_and_spacing(self): b = """b = 0x12L""" a = """b = 0x12""" self.check(b, a) b = """b = 0755 # spam""" a = """b = 0o755 # spam""" self.check(b, a) def test_unchanged_int(self): s = """5""" self.unchanged(s) def test_unchanged_float(self): s = """5.0""" self.unchanged(s) def test_unchanged_octal(self): s = """0o755""" self.unchanged(s) def test_unchanged_hex(self): s = """0xABC""" self.unchanged(s) def test_unchanged_exp(self): s = """5.0e10""" self.unchanged(s) def test_unchanged_complex_int(self): s = """5 + 4j""" self.unchanged(s) def test_unchanged_complex_float(self): s = """5.4 + 4.9j""" self.unchanged(s) def test_unchanged_complex_bare(self): s = """4j""" self.unchanged(s) s = """4.4j""" self.unchanged(s) class Test_renames(FixerTestCase): fixer = "renames" modules = {"sys": ("maxint", "maxsize"), } def test_import_from(self): for mod, (old, new) in list(self.modules.items()): b = "from %s import %s" % (mod, old) a = "from %s import %s" % (mod, new) self.check(b, a) s = "from foo import %s" % old self.unchanged(s) def test_import_from_as(self): for mod, (old, new) in list(self.modules.items()): b = "from %s import %s as foo_bar" % (mod, old) a = "from %s import %s as foo_bar" % (mod, new) self.check(b, a) def test_import_module_usage(self): for mod, (old, new) in list(self.modules.items()): b = """ import %s foo(%s, %s.%s) """ % (mod, mod, mod, old) a = """ import %s foo(%s, %s.%s) """ % (mod, mod, mod, new) self.check(b, a) def XXX_test_from_import_usage(self): # not implemented yet for mod, (old, new) in list(self.modules.items()): b = """ from %s import %s foo(%s, %s) """ % (mod, old, mod, old) a = """ from %s import %s foo(%s, %s) """ % (mod, new, mod, new) self.check(b, a) class Test_unicode(FixerTestCase): fixer = "unicode" def test_whitespace(self): b = """unicode( x)""" a = """str( x)""" self.check(b, a) b = """ unicode(x )""" a = """ str(x )""" self.check(b, a) b = """ u'h'""" a = """ 'h'""" self.check(b, a) def test_unicode_call(self): b = """unicode(x, y, z)""" a = """str(x, y, z)""" self.check(b, a) def test_unichr(self): b = """unichr(u'h')""" a = """chr('h')""" self.check(b, a) def test_unicode_literal_1(self): b = '''u"x"''' a = '''"x"''' self.check(b, a) def test_unicode_literal_2(self): b = """ur'x'""" a = """r'x'""" self.check(b, a) def test_unicode_literal_3(self): b = """UR'''x''' """ a = """R'''x''' """ self.check(b, a) def test_native_literal_escape_u(self): b = r"""'\\\u20ac\U0001d121\\u20ac'""" a = r"""'\\\\u20ac\\U0001d121\\u20ac'""" self.check(b, a) b = r"""r'\\\u20ac\U0001d121\\u20ac'""" a = r"""r'\\\u20ac\U0001d121\\u20ac'""" self.check(b, a) def test_bytes_literal_escape_u(self): b = r"""b'\\\u20ac\U0001d121\\u20ac'""" a = r"""b'\\\u20ac\U0001d121\\u20ac'""" self.check(b, a) b = r"""br'\\\u20ac\U0001d121\\u20ac'""" a = r"""br'\\\u20ac\U0001d121\\u20ac'""" self.check(b, a) def test_unicode_literal_escape_u(self): b = r"""u'\\\u20ac\U0001d121\\u20ac'""" a = r"""'\\\u20ac\U0001d121\\u20ac'""" self.check(b, a) b = r"""ur'\\\u20ac\U0001d121\\u20ac'""" a = r"""r'\\\u20ac\U0001d121\\u20ac'""" self.check(b, a) def test_native_unicode_literal_escape_u(self): f = 'from __future__ import unicode_literals\n' b = f + r"""'\\\u20ac\U0001d121\\u20ac'""" a = f + r"""'\\\u20ac\U0001d121\\u20ac'""" self.check(b, a) b = f + r"""r'\\\u20ac\U0001d121\\u20ac'""" a = f + r"""r'\\\u20ac\U0001d121\\u20ac'""" self.check(b, a) class Test_filter(FixerTestCase): fixer = "filter" def test_prefix_preservation(self): b = """x = filter( foo, 'abc' )""" a = """x = list(filter( foo, 'abc' ))""" self.check(b, a) b = """x = filter( None , 'abc' )""" a = """x = [_f for _f in 'abc' if _f]""" self.check(b, a) def test_filter_basic(self): b = """x = filter(None, 'abc')""" a = """x = [_f for _f in 'abc' if _f]""" self.check(b, a) b = """x = len(filter(f, 'abc'))""" a = """x = len(list(filter(f, 'abc')))""" self.check(b, a) b = """x = filter(lambda x: x%2 == 0, range(10))""" a = """x = [x for x in range(10) if x%2 == 0]""" self.check(b, a) # Note the parens around x b = """x = filter(lambda (x): x%2 == 0, range(10))""" a = """x = [x for x in range(10) if x%2 == 0]""" self.check(b, a) def test_filter_trailers(self): b = """x = filter(None, 'abc')[0]""" a = """x = [_f for _f in 'abc' if _f][0]""" self.check(b, a) b = """x = len(filter(f, 'abc')[0])""" a = """x = len(list(filter(f, 'abc'))[0])""" self.check(b, a) b = """x = filter(lambda x: x%2 == 0, range(10))[0]""" a = """x = [x for x in range(10) if x%2 == 0][0]""" self.check(b, a) # Note the parens around x b = """x = filter(lambda (x): x%2 == 0, range(10))[0]""" a = """x = [x for x in range(10) if x%2 == 0][0]""" self.check(b, a) def test_filter_nochange(self): a = """b.join(filter(f, 'abc'))""" self.unchanged(a) a = """(a + foo(5)).join(filter(f, 'abc'))""" self.unchanged(a) a = """iter(filter(f, 'abc'))""" self.unchanged(a) a = """list(filter(f, 'abc'))""" self.unchanged(a) a = """list(filter(f, 'abc'))[0]""" self.unchanged(a) a = """set(filter(f, 'abc'))""" self.unchanged(a) a = """set(filter(f, 'abc')).pop()""" self.unchanged(a) a = """tuple(filter(f, 'abc'))""" self.unchanged(a) a = """any(filter(f, 'abc'))""" self.unchanged(a) a = """all(filter(f, 'abc'))""" self.unchanged(a) a = """sum(filter(f, 'abc'))""" self.unchanged(a) a = """sorted(filter(f, 'abc'))""" self.unchanged(a) a = """sorted(filter(f, 'abc'), key=blah)""" self.unchanged(a) a = """sorted(filter(f, 'abc'), key=blah)[0]""" self.unchanged(a) a = """enumerate(filter(f, 'abc'))""" self.unchanged(a) a = """enumerate(filter(f, 'abc'), start=1)""" self.unchanged(a) a = """for i in filter(f, 'abc'): pass""" self.unchanged(a) a = """[x for x in filter(f, 'abc')]""" self.unchanged(a) a = """(x for x in filter(f, 'abc'))""" self.unchanged(a) def test_future_builtins(self): a = "from future_builtins import spam, filter; filter(f, 'ham')" self.unchanged(a) b = """from future_builtins import spam; x = filter(f, 'abc')""" a = """from future_builtins import spam; x = list(filter(f, 'abc'))""" self.check(b, a) a = "from future_builtins import *; filter(f, 'ham')" self.unchanged(a) class Test_map(FixerTestCase): fixer = "map" def check(self, b, a): self.unchanged("from future_builtins import map; " + b, a) super(Test_map, self).check(b, a) def test_prefix_preservation(self): b = """x = map( f, 'abc' )""" a = """x = list(map( f, 'abc' ))""" self.check(b, a) def test_map_trailers(self): b = """x = map(f, 'abc')[0]""" a = """x = list(map(f, 'abc'))[0]""" self.check(b, a) b = """x = map(None, l)[0]""" a = """x = list(l)[0]""" self.check(b, a) b = """x = map(lambda x:x, l)[0]""" a = """x = [x for x in l][0]""" self.check(b, a) b = """x = map(f, 'abc')[0][1]""" a = """x = list(map(f, 'abc'))[0][1]""" self.check(b, a) def test_trailing_comment(self): b = """x = map(f, 'abc') # foo""" a = """x = list(map(f, 'abc')) # foo""" self.check(b, a) def test_None_with_multiple_arguments(self): s = """x = map(None, a, b, c)""" self.warns_unchanged(s, "cannot convert map(None, ...) with " "multiple arguments") def test_map_basic(self): b = """x = map(f, 'abc')""" a = """x = list(map(f, 'abc'))""" self.check(b, a) b = """x = len(map(f, 'abc', 'def'))""" a = """x = len(list(map(f, 'abc', 'def')))""" self.check(b, a) b = """x = map(None, 'abc')""" a = """x = list('abc')""" self.check(b, a) b = """x = map(lambda x: x+1, range(4))""" a = """x = [x+1 for x in range(4)]""" self.check(b, a) # Note the parens around x b = """x = map(lambda (x): x+1, range(4))""" a = """x = [x+1 for x in range(4)]""" self.check(b, a) b = """ foo() # foo map(f, x) """ a = """ foo() # foo list(map(f, x)) """ self.warns(b, a, "You should use a for loop here") def test_map_nochange(self): a = """b.join(map(f, 'abc'))""" self.unchanged(a) a = """(a + foo(5)).join(map(f, 'abc'))""" self.unchanged(a) a = """iter(map(f, 'abc'))""" self.unchanged(a) a = """list(map(f, 'abc'))""" self.unchanged(a) a = """list(map(f, 'abc'))[0]""" self.unchanged(a) a = """set(map(f, 'abc'))""" self.unchanged(a) a = """set(map(f, 'abc')).pop()""" self.unchanged(a) a = """tuple(map(f, 'abc'))""" self.unchanged(a) a = """any(map(f, 'abc'))""" self.unchanged(a) a = """all(map(f, 'abc'))""" self.unchanged(a) a = """sum(map(f, 'abc'))""" self.unchanged(a) a = """sorted(map(f, 'abc'))""" self.unchanged(a) a = """sorted(map(f, 'abc'), key=blah)""" self.unchanged(a) a = """sorted(map(f, 'abc'), key=blah)[0]""" self.unchanged(a) a = """enumerate(map(f, 'abc'))""" self.unchanged(a) a = """enumerate(map(f, 'abc'), start=1)""" self.unchanged(a) a = """for i in map(f, 'abc'): pass""" self.unchanged(a) a = """[x for x in map(f, 'abc')]""" self.unchanged(a) a = """(x for x in map(f, 'abc'))""" self.unchanged(a) def test_future_builtins(self): a = "from future_builtins import spam, map, eggs; map(f, 'ham')" self.unchanged(a) b = """from future_builtins import spam, eggs; x = map(f, 'abc')""" a = """from future_builtins import spam, eggs; x = list(map(f, 'abc'))""" self.check(b, a) a = "from future_builtins import *; map(f, 'ham')" self.unchanged(a) class Test_zip(FixerTestCase): fixer = "zip" def check(self, b, a): self.unchanged("from future_builtins import zip; " + b, a) super(Test_zip, self).check(b, a) def test_zip_basic(self): b = """x = zip()""" a = """x = list(zip())""" self.check(b, a) b = """x = zip(a, b, c)""" a = """x = list(zip(a, b, c))""" self.check(b, a) b = """x = len(zip(a, b))""" a = """x = len(list(zip(a, b)))""" self.check(b, a) def test_zip_trailers(self): b = """x = zip(a, b, c)[0]""" a = """x = list(zip(a, b, c))[0]""" self.check(b, a) b = """x = zip(a, b, c)[0][1]""" a = """x = list(zip(a, b, c))[0][1]""" self.check(b, a) def test_zip_nochange(self): a = """b.join(zip(a, b))""" self.unchanged(a) a = """(a + foo(5)).join(zip(a, b))""" self.unchanged(a) a = """iter(zip(a, b))""" self.unchanged(a) a = """list(zip(a, b))""" self.unchanged(a) a = """list(zip(a, b))[0]""" self.unchanged(a) a = """set(zip(a, b))""" self.unchanged(a) a = """set(zip(a, b)).pop()""" self.unchanged(a) a = """tuple(zip(a, b))""" self.unchanged(a) a = """any(zip(a, b))""" self.unchanged(a) a = """all(zip(a, b))""" self.unchanged(a) a = """sum(zip(a, b))""" self.unchanged(a) a = """sorted(zip(a, b))""" self.unchanged(a) a = """sorted(zip(a, b), key=blah)""" self.unchanged(a) a = """sorted(zip(a, b), key=blah)[0]""" self.unchanged(a) a = """enumerate(zip(a, b))""" self.unchanged(a) a = """enumerate(zip(a, b), start=1)""" self.unchanged(a) a = """for i in zip(a, b): pass""" self.unchanged(a) a = """[x for x in zip(a, b)]""" self.unchanged(a) a = """(x for x in zip(a, b))""" self.unchanged(a) def test_future_builtins(self): a = "from future_builtins import spam, zip, eggs; zip(a, b)" self.unchanged(a) b = """from future_builtins import spam, eggs; x = zip(a, b)""" a = """from future_builtins import spam, eggs; x = list(zip(a, b))""" self.check(b, a) a = "from future_builtins import *; zip(a, b)" self.unchanged(a) class Test_standarderror(FixerTestCase): fixer = "standarderror" def test(self): b = """x = StandardError()""" a = """x = Exception()""" self.check(b, a) b = """x = StandardError(a, b, c)""" a = """x = Exception(a, b, c)""" self.check(b, a) b = """f(2 + StandardError(a, b, c))""" a = """f(2 + Exception(a, b, c))""" self.check(b, a) class Test_types(FixerTestCase): fixer = "types" def test_basic_types_convert(self): b = """types.StringType""" a = """bytes""" self.check(b, a) b = """types.DictType""" a = """dict""" self.check(b, a) b = """types . IntType""" a = """int""" self.check(b, a) b = """types.ListType""" a = """list""" self.check(b, a) b = """types.LongType""" a = """int""" self.check(b, a) b = """types.NoneType""" a = """type(None)""" self.check(b, a) b = "types.StringTypes" a = "(str,)" self.check(b, a) class Test_idioms(FixerTestCase): fixer = "idioms" def test_while(self): b = """while 1: foo()""" a = """while True: foo()""" self.check(b, a) b = """while 1: foo()""" a = """while True: foo()""" self.check(b, a) b = """ while 1: foo() """ a = """ while True: foo() """ self.check(b, a) def test_while_unchanged(self): s = """while 11: foo()""" self.unchanged(s) s = """while 0: foo()""" self.unchanged(s) s = """while foo(): foo()""" self.unchanged(s) s = """while []: foo()""" self.unchanged(s) def test_eq_simple(self): b = """type(x) == T""" a = """isinstance(x, T)""" self.check(b, a) b = """if type(x) == T: pass""" a = """if isinstance(x, T): pass""" self.check(b, a) def test_eq_reverse(self): b = """T == type(x)""" a = """isinstance(x, T)""" self.check(b, a) b = """if T == type(x): pass""" a = """if isinstance(x, T): pass""" self.check(b, a) def test_eq_expression(self): b = """type(x+y) == d.get('T')""" a = """isinstance(x+y, d.get('T'))""" self.check(b, a) b = """type( x + y) == d.get('T')""" a = """isinstance(x + y, d.get('T'))""" self.check(b, a) def test_is_simple(self): b = """type(x) is T""" a = """isinstance(x, T)""" self.check(b, a) b = """if type(x) is T: pass""" a = """if isinstance(x, T): pass""" self.check(b, a) def test_is_reverse(self): b = """T is type(x)""" a = """isinstance(x, T)""" self.check(b, a) b = """if T is type(x): pass""" a = """if isinstance(x, T): pass""" self.check(b, a) def test_is_expression(self): b = """type(x+y) is d.get('T')""" a = """isinstance(x+y, d.get('T'))""" self.check(b, a) b = """type( x + y) is d.get('T')""" a = """isinstance(x + y, d.get('T'))""" self.check(b, a) def test_is_not_simple(self): b = """type(x) is not T""" a = """not isinstance(x, T)""" self.check(b, a) b = """if type(x) is not T: pass""" a = """if not isinstance(x, T): pass""" self.check(b, a) def test_is_not_reverse(self): b = """T is not type(x)""" a = """not isinstance(x, T)""" self.check(b, a) b = """if T is not type(x): pass""" a = """if not isinstance(x, T): pass""" self.check(b, a) def test_is_not_expression(self): b = """type(x+y) is not d.get('T')""" a = """not isinstance(x+y, d.get('T'))""" self.check(b, a) b = """type( x + y) is not d.get('T')""" a = """not isinstance(x + y, d.get('T'))""" self.check(b, a) def test_ne_simple(self): b = """type(x) != T""" a = """not isinstance(x, T)""" self.check(b, a) b = """if type(x) != T: pass""" a = """if not isinstance(x, T): pass""" self.check(b, a) def test_ne_reverse(self): b = """T != type(x)""" a = """not isinstance(x, T)""" self.check(b, a) b = """if T != type(x): pass""" a = """if not isinstance(x, T): pass""" self.check(b, a) def test_ne_expression(self): b = """type(x+y) != d.get('T')""" a = """not isinstance(x+y, d.get('T'))""" self.check(b, a) b = """type( x + y) != d.get('T')""" a = """not isinstance(x + y, d.get('T'))""" self.check(b, a) def test_type_unchanged(self): a = """type(x).__name__""" self.unchanged(a) def test_sort_list_call(self): b = """ v = list(t) v.sort() foo(v) """ a = """ v = sorted(t) foo(v) """ self.check(b, a) b = """ v = list(foo(b) + d) v.sort() foo(v) """ a = """ v = sorted(foo(b) + d) foo(v) """ self.check(b, a) b = """ while x: v = list(t) v.sort() foo(v) """ a = """ while x: v = sorted(t) foo(v) """ self.check(b, a) b = """ v = list(t) # foo v.sort() foo(v) """ a = """ v = sorted(t) # foo foo(v) """ self.check(b, a) b = r""" v = list( t) v.sort() foo(v) """ a = r""" v = sorted( t) foo(v) """ self.check(b, a) b = r""" try: m = list(s) m.sort() except: pass """ a = r""" try: m = sorted(s) except: pass """ self.check(b, a) b = r""" try: m = list(s) # foo m.sort() except: pass """ a = r""" try: m = sorted(s) # foo except: pass """ self.check(b, a) b = r""" m = list(s) # more comments m.sort()""" a = r""" m = sorted(s) # more comments""" self.check(b, a) def test_sort_simple_expr(self): b = """ v = t v.sort() foo(v) """ a = """ v = sorted(t) foo(v) """ self.check(b, a) b = """ v = foo(b) v.sort() foo(v) """ a = """ v = sorted(foo(b)) foo(v) """ self.check(b, a) b = """ v = b.keys() v.sort() foo(v) """ a = """ v = sorted(b.keys()) foo(v) """ self.check(b, a) b = """ v = foo(b) + d v.sort() foo(v) """ a = """ v = sorted(foo(b) + d) foo(v) """ self.check(b, a) b = """ while x: v = t v.sort() foo(v) """ a = """ while x: v = sorted(t) foo(v) """ self.check(b, a) b = """ v = t # foo v.sort() foo(v) """ a = """ v = sorted(t) # foo foo(v) """ self.check(b, a) b = r""" v = t v.sort() foo(v) """ a = r""" v = sorted(t) foo(v) """ self.check(b, a) def test_sort_unchanged(self): s = """ v = list(t) w.sort() foo(w) """ self.unchanged(s) s = """ v = list(t) v.sort(u) foo(v) """ self.unchanged(s) class Test_basestring(FixerTestCase): fixer = "basestring" def test_basestring(self): b = """isinstance(x, basestring)""" a = """isinstance(x, str)""" self.check(b, a) class Test_buffer(FixerTestCase): fixer = "buffer" def test_buffer(self): b = """x = buffer(y)""" a = """x = memoryview(y)""" self.check(b, a) def test_slicing(self): b = """buffer(y)[4:5]""" a = """memoryview(y)[4:5]""" self.check(b, a) class Test_future(FixerTestCase): fixer = "future" def test_future(self): b = """from __future__ import braces""" a = """""" self.check(b, a) b = """# comment\nfrom __future__ import braces""" a = """# comment\n""" self.check(b, a) b = """from __future__ import braces\n# comment""" a = """\n# comment""" self.check(b, a) def test_run_order(self): self.assert_runs_after('print') class Test_itertools(FixerTestCase): fixer = "itertools" def checkall(self, before, after): # Because we need to check with and without the itertools prefix # and on each of the three functions, these loops make it all # much easier for i in ('itertools.', ''): for f in ('map', 'filter', 'zip'): b = before %(i+'i'+f) a = after %(f) self.check(b, a) def test_0(self): # A simple example -- test_1 covers exactly the same thing, # but it's not quite as clear. b = "itertools.izip(a, b)" a = "zip(a, b)" self.check(b, a) def test_1(self): b = """%s(f, a)""" a = """%s(f, a)""" self.checkall(b, a) def test_qualified(self): b = """itertools.ifilterfalse(a, b)""" a = """itertools.filterfalse(a, b)""" self.check(b, a) b = """itertools.izip_longest(a, b)""" a = """itertools.zip_longest(a, b)""" self.check(b, a) def test_2(self): b = """ifilterfalse(a, b)""" a = """filterfalse(a, b)""" self.check(b, a) b = """izip_longest(a, b)""" a = """zip_longest(a, b)""" self.check(b, a) def test_space_1(self): b = """ %s(f, a)""" a = """ %s(f, a)""" self.checkall(b, a) def test_space_2(self): b = """ itertools.ifilterfalse(a, b)""" a = """ itertools.filterfalse(a, b)""" self.check(b, a) b = """ itertools.izip_longest(a, b)""" a = """ itertools.zip_longest(a, b)""" self.check(b, a) def test_run_order(self): self.assert_runs_after('map', 'zip', 'filter') class Test_itertools_imports(FixerTestCase): fixer = 'itertools_imports' def test_reduced(self): b = "from itertools import imap, izip, foo" a = "from itertools import foo" self.check(b, a) b = "from itertools import bar, imap, izip, foo" a = "from itertools import bar, foo" self.check(b, a) b = "from itertools import chain, imap, izip" a = "from itertools import chain" self.check(b, a) def test_comments(self): b = "#foo\nfrom itertools import imap, izip" a = "#foo\n" self.check(b, a) def test_none(self): b = "from itertools import imap, izip" a = "" self.check(b, a) b = "from itertools import izip" a = "" self.check(b, a) def test_import_as(self): b = "from itertools import izip, bar as bang, imap" a = "from itertools import bar as bang" self.check(b, a) b = "from itertools import izip as _zip, imap, bar" a = "from itertools import bar" self.check(b, a) b = "from itertools import imap as _map" a = "" self.check(b, a) b = "from itertools import imap as _map, izip as _zip" a = "" self.check(b, a) s = "from itertools import bar as bang" self.unchanged(s) def test_ifilter_and_zip_longest(self): for name in "filterfalse", "zip_longest": b = "from itertools import i%s" % (name,) a = "from itertools import %s" % (name,) self.check(b, a) b = "from itertools import imap, i%s, foo" % (name,) a = "from itertools import %s, foo" % (name,) self.check(b, a) b = "from itertools import bar, i%s, foo" % (name,) a = "from itertools import bar, %s, foo" % (name,) self.check(b, a) def test_import_star(self): s = "from itertools import *" self.unchanged(s) def test_unchanged(self): s = "from itertools import foo" self.unchanged(s) class Test_import(FixerTestCase): fixer = "import" def setUp(self): super(Test_import, self).setUp() # Need to replace fix_import's exists method # so we can check that it's doing the right thing self.files_checked = [] self.present_files = set() self.always_exists = True def fake_exists(name): self.files_checked.append(name) return self.always_exists or (name in self.present_files) from lib2to3.fixes import fix_import fix_import.exists = fake_exists def tearDown(self): from lib2to3.fixes import fix_import fix_import.exists = os.path.exists def check_both(self, b, a): self.always_exists = True super(Test_import, self).check(b, a) self.always_exists = False super(Test_import, self).unchanged(b) def test_files_checked(self): def p(path): # Takes a unix path and returns a path with correct separators return os.path.pathsep.join(path.split("/")) self.always_exists = False self.present_files = set(['__init__.py']) expected_extensions = ('.py', os.path.sep, '.pyc', '.so', '.sl', '.pyd') names_to_test = (p("/spam/eggs.py"), "ni.py", p("../../shrubbery.py")) for name in names_to_test: self.files_checked = [] self.filename = name self.unchanged("import jam") if os.path.dirname(name): name = os.path.dirname(name) + '/jam' else: name = 'jam' expected_checks = set(name + ext for ext in expected_extensions) expected_checks.add("__init__.py") self.assertEqual(set(self.files_checked), expected_checks) def test_not_in_package(self): s = "import bar" self.always_exists = False self.present_files = set(["bar.py"]) self.unchanged(s) def test_with_absolute_import_enabled(self): s = "from __future__ import absolute_import\nimport bar" self.always_exists = False self.present_files = set(["__init__.py", "bar.py"]) self.unchanged(s) def test_in_package(self): b = "import bar" a = "from . import bar" self.always_exists = False self.present_files = set(["__init__.py", "bar.py"]) self.check(b, a) def test_import_from_package(self): b = "import bar" a = "from . import bar" self.always_exists = False self.present_files = set(["__init__.py", "bar" + os.path.sep]) self.check(b, a) def test_already_relative_import(self): s = "from . import bar" self.unchanged(s) def test_comments_and_indent(self): b = "import bar # Foo" a = "from . import bar # Foo" self.check(b, a) def test_from(self): b = "from foo import bar, baz" a = "from .foo import bar, baz" self.check_both(b, a) b = "from foo import bar" a = "from .foo import bar" self.check_both(b, a) b = "from foo import (bar, baz)" a = "from .foo import (bar, baz)" self.check_both(b, a) def test_dotted_from(self): b = "from green.eggs import ham" a = "from .green.eggs import ham" self.check_both(b, a) def test_from_as(self): b = "from green.eggs import ham as spam" a = "from .green.eggs import ham as spam" self.check_both(b, a) def test_import(self): b = "import foo" a = "from . import foo" self.check_both(b, a) b = "import foo, bar" a = "from . import foo, bar" self.check_both(b, a) b = "import foo, bar, x" a = "from . import foo, bar, x" self.check_both(b, a) b = "import x, y, z" a = "from . import x, y, z" self.check_both(b, a) def test_import_as(self): b = "import foo as x" a = "from . import foo as x" self.check_both(b, a) b = "import a as b, b as c, c as d" a = "from . import a as b, b as c, c as d" self.check_both(b, a) def test_local_and_absolute(self): self.always_exists = False self.present_files = set(["foo.py", "__init__.py"]) s = "import foo, bar" self.warns_unchanged(s, "absolute and local imports together") def test_dotted_import(self): b = "import foo.bar" a = "from . import foo.bar" self.check_both(b, a) def test_dotted_import_as(self): b = "import foo.bar as bang" a = "from . import foo.bar as bang" self.check_both(b, a) def test_prefix(self): b = """ # prefix import foo.bar """ a = """ # prefix from . import foo.bar """ self.check_both(b, a) class Test_set_literal(FixerTestCase): fixer = "set_literal" def test_basic(self): b = """set([1, 2, 3])""" a = """{1, 2, 3}""" self.check(b, a) b = """set((1, 2, 3))""" a = """{1, 2, 3}""" self.check(b, a) b = """set((1,))""" a = """{1}""" self.check(b, a) b = """set([1])""" self.check(b, a) b = """set((a, b))""" a = """{a, b}""" self.check(b, a) b = """set([a, b])""" self.check(b, a) b = """set((a*234, f(args=23)))""" a = """{a*234, f(args=23)}""" self.check(b, a) b = """set([a*23, f(23)])""" a = """{a*23, f(23)}""" self.check(b, a) b = """set([a-234**23])""" a = """{a-234**23}""" self.check(b, a) def test_listcomps(self): b = """set([x for x in y])""" a = """{x for x in y}""" self.check(b, a) b = """set([x for x in y if x == m])""" a = """{x for x in y if x == m}""" self.check(b, a) b = """set([x for x in y for a in b])""" a = """{x for x in y for a in b}""" self.check(b, a) b = """set([f(x) - 23 for x in y])""" a = """{f(x) - 23 for x in y}""" self.check(b, a) def test_whitespace(self): b = """set( [1, 2])""" a = """{1, 2}""" self.check(b, a) b = """set([1 , 2])""" a = """{1 , 2}""" self.check(b, a) b = """set([ 1 ])""" a = """{ 1 }""" self.check(b, a) b = """set( [1] )""" a = """{1}""" self.check(b, a) b = """set([ 1, 2 ])""" a = """{ 1, 2 }""" self.check(b, a) b = """set([x for x in y ])""" a = """{x for x in y }""" self.check(b, a) b = """set( [1, 2] ) """ a = """{1, 2}\n""" self.check(b, a) def test_comments(self): b = """set((1, 2)) # Hi""" a = """{1, 2} # Hi""" self.check(b, a) # This isn't optimal behavior, but the fixer is optional. b = """ # Foo set( # Bar (1, 2) ) """ a = """ # Foo {1, 2} """ self.check(b, a) def test_unchanged(self): s = """set()""" self.unchanged(s) s = """set(a)""" self.unchanged(s) s = """set(a, b, c)""" self.unchanged(s) # Don't transform generators because they might have to be lazy. s = """set(x for x in y)""" self.unchanged(s) s = """set(x for x in y if z)""" self.unchanged(s) s = """set(a*823-23**2 + f(23))""" self.unchanged(s) class Test_sys_exc(FixerTestCase): fixer = "sys_exc" def test_0(self): b = "sys.exc_type" a = "sys.exc_info()[0]" self.check(b, a) def test_1(self): b = "sys.exc_value" a = "sys.exc_info()[1]" self.check(b, a) def test_2(self): b = "sys.exc_traceback" a = "sys.exc_info()[2]" self.check(b, a) def test_3(self): b = "sys.exc_type # Foo" a = "sys.exc_info()[0] # Foo" self.check(b, a) def test_4(self): b = "sys. exc_type" a = "sys. exc_info()[0]" self.check(b, a) def test_5(self): b = "sys .exc_type" a = "sys .exc_info()[0]" self.check(b, a) class Test_paren(FixerTestCase): fixer = "paren" def test_0(self): b = """[i for i in 1, 2 ]""" a = """[i for i in (1, 2) ]""" self.check(b, a) def test_1(self): b = """[i for i in 1, 2, ]""" a = """[i for i in (1, 2,) ]""" self.check(b, a) def test_2(self): b = """[i for i in 1, 2 ]""" a = """[i for i in (1, 2) ]""" self.check(b, a) def test_3(self): b = """[i for i in 1, 2 if i]""" a = """[i for i in (1, 2) if i]""" self.check(b, a) def test_4(self): b = """[i for i in 1, 2 ]""" a = """[i for i in (1, 2) ]""" self.check(b, a) def test_5(self): b = """(i for i in 1, 2)""" a = """(i for i in (1, 2))""" self.check(b, a) def test_6(self): b = """(i for i in 1 ,2 if i)""" a = """(i for i in (1 ,2) if i)""" self.check(b, a) def test_unchanged_0(self): s = """[i for i in (1, 2)]""" self.unchanged(s) def test_unchanged_1(self): s = """[i for i in foo()]""" self.unchanged(s) def test_unchanged_2(self): s = """[i for i in (1, 2) if nothing]""" self.unchanged(s) def test_unchanged_3(self): s = """(i for i in (1, 2))""" self.unchanged(s) def test_unchanged_4(self): s = """[i for i in m]""" self.unchanged(s) class Test_metaclass(FixerTestCase): fixer = 'metaclass' def test_unchanged(self): self.unchanged("class X(): pass") self.unchanged("class X(object): pass") self.unchanged("class X(object1, object2): pass") self.unchanged("class X(object1, object2, object3): pass") self.unchanged("class X(metaclass=Meta): pass") self.unchanged("class X(b, arg=23, metclass=Meta): pass") self.unchanged("class X(b, arg=23, metaclass=Meta, other=42): pass") s = """ class X: def __metaclass__(self): pass """ self.unchanged(s) s = """ class X: a[23] = 74 """ self.unchanged(s) def test_comments(self): b = """ class X: # hi __metaclass__ = AppleMeta """ a = """ class X(metaclass=AppleMeta): # hi pass """ self.check(b, a) b = """ class X: __metaclass__ = Meta # Bedtime! """ a = """ class X(metaclass=Meta): pass # Bedtime! """ self.check(b, a) def test_meta(self): # no-parent class, odd body b = """ class X(): __metaclass__ = Q pass """ a = """ class X(metaclass=Q): pass """ self.check(b, a) # one parent class, no body b = """class X(object): __metaclass__ = Q""" a = """class X(object, metaclass=Q): pass""" self.check(b, a) # one parent, simple body b = """ class X(object): __metaclass__ = Meta bar = 7 """ a = """ class X(object, metaclass=Meta): bar = 7 """ self.check(b, a) b = """ class X: __metaclass__ = Meta; x = 4; g = 23 """ a = """ class X(metaclass=Meta): x = 4; g = 23 """ self.check(b, a) # one parent, simple body, __metaclass__ last b = """ class X(object): bar = 7 __metaclass__ = Meta """ a = """ class X(object, metaclass=Meta): bar = 7 """ self.check(b, a) # redefining __metaclass__ b = """ class X(): __metaclass__ = A __metaclass__ = B bar = 7 """ a = """ class X(metaclass=B): bar = 7 """ self.check(b, a) # multiple inheritance, simple body b = """ class X(clsA, clsB): __metaclass__ = Meta bar = 7 """ a = """ class X(clsA, clsB, metaclass=Meta): bar = 7 """ self.check(b, a) # keywords in the class statement b = """class m(a, arg=23): __metaclass__ = Meta""" a = """class m(a, arg=23, metaclass=Meta): pass""" self.check(b, a) b = """ class X(expression(2 + 4)): __metaclass__ = Meta """ a = """ class X(expression(2 + 4), metaclass=Meta): pass """ self.check(b, a) b = """ class X(expression(2 + 4), x**4): __metaclass__ = Meta """ a = """ class X(expression(2 + 4), x**4, metaclass=Meta): pass """ self.check(b, a) b = """ class X: __metaclass__ = Meta save.py = 23 """ a = """ class X(metaclass=Meta): save.py = 23 """ self.check(b, a) class Test_getcwdu(FixerTestCase): fixer = 'getcwdu' def test_basic(self): b = """os.getcwdu""" a = """os.getcwd""" self.check(b, a) b = """os.getcwdu()""" a = """os.getcwd()""" self.check(b, a) b = """meth = os.getcwdu""" a = """meth = os.getcwd""" self.check(b, a) b = """os.getcwdu(args)""" a = """os.getcwd(args)""" self.check(b, a) def test_comment(self): b = """os.getcwdu() # Foo""" a = """os.getcwd() # Foo""" self.check(b, a) def test_unchanged(self): s = """os.getcwd()""" self.unchanged(s) s = """getcwdu()""" self.unchanged(s) s = """os.getcwdb()""" self.unchanged(s) def test_indentation(self): b = """ if 1: os.getcwdu() """ a = """ if 1: os.getcwd() """ self.check(b, a) def test_multilation(self): b = """os .getcwdu()""" a = """os .getcwd()""" self.check(b, a) b = """os. getcwdu""" a = """os. getcwd""" self.check(b, a) b = """os.getcwdu ( )""" a = """os.getcwd ( )""" self.check(b, a) class Test_operator(FixerTestCase): fixer = "operator" def test_operator_isCallable(self): b = "operator.isCallable(x)" a = "hasattr(x, '__call__')" self.check(b, a) def test_operator_sequenceIncludes(self): b = "operator.sequenceIncludes(x, y)" a = "operator.contains(x, y)" self.check(b, a) b = "operator .sequenceIncludes(x, y)" a = "operator .contains(x, y)" self.check(b, a) b = "operator. sequenceIncludes(x, y)" a = "operator. contains(x, y)" self.check(b, a) def test_operator_isSequenceType(self): b = "operator.isSequenceType(x)" a = "import collections\nisinstance(x, collections.Sequence)" self.check(b, a) def test_operator_isMappingType(self): b = "operator.isMappingType(x)" a = "import collections\nisinstance(x, collections.Mapping)" self.check(b, a) def test_operator_isNumberType(self): b = "operator.isNumberType(x)" a = "import numbers\nisinstance(x, numbers.Number)" self.check(b, a) def test_operator_repeat(self): b = "operator.repeat(x, n)" a = "operator.mul(x, n)" self.check(b, a) b = "operator .repeat(x, n)" a = "operator .mul(x, n)" self.check(b, a) b = "operator. repeat(x, n)" a = "operator. mul(x, n)" self.check(b, a) def test_operator_irepeat(self): b = "operator.irepeat(x, n)" a = "operator.imul(x, n)" self.check(b, a) b = "operator .irepeat(x, n)" a = "operator .imul(x, n)" self.check(b, a) b = "operator. irepeat(x, n)" a = "operator. imul(x, n)" self.check(b, a) def test_bare_isCallable(self): s = "isCallable(x)" t = "You should use 'hasattr(x, '__call__')' here." self.warns_unchanged(s, t) def test_bare_sequenceIncludes(self): s = "sequenceIncludes(x, y)" t = "You should use 'operator.contains(x, y)' here." self.warns_unchanged(s, t) def test_bare_operator_isSequenceType(self): s = "isSequenceType(z)" t = "You should use 'isinstance(z, collections.Sequence)' here." self.warns_unchanged(s, t) def test_bare_operator_isMappingType(self): s = "isMappingType(x)" t = "You should use 'isinstance(x, collections.Mapping)' here." self.warns_unchanged(s, t) def test_bare_operator_isNumberType(self): s = "isNumberType(y)" t = "You should use 'isinstance(y, numbers.Number)' here." self.warns_unchanged(s, t) def test_bare_operator_repeat(self): s = "repeat(x, n)" t = "You should use 'operator.mul(x, n)' here." self.warns_unchanged(s, t) def test_bare_operator_irepeat(self): s = "irepeat(y, 187)" t = "You should use 'operator.imul(y, 187)' here." self.warns_unchanged(s, t) class Test_exitfunc(FixerTestCase): fixer = "exitfunc" def test_simple(self): b = """ import sys sys.exitfunc = my_atexit """ a = """ import sys import atexit atexit.register(my_atexit) """ self.check(b, a) def test_names_import(self): b = """ import sys, crumbs sys.exitfunc = my_func """ a = """ import sys, crumbs, atexit atexit.register(my_func) """ self.check(b, a) def test_complex_expression(self): b = """ import sys sys.exitfunc = do(d)/a()+complex(f=23, g=23)*expression """ a = """ import sys import atexit atexit.register(do(d)/a()+complex(f=23, g=23)*expression) """ self.check(b, a) def test_comments(self): b = """ import sys # Foo sys.exitfunc = f # Blah """ a = """ import sys import atexit # Foo atexit.register(f) # Blah """ self.check(b, a) b = """ import apples, sys, crumbs, larry # Pleasant comments sys.exitfunc = func """ a = """ import apples, sys, crumbs, larry, atexit # Pleasant comments atexit.register(func) """ self.check(b, a) def test_in_a_function(self): b = """ import sys def f(): sys.exitfunc = func """ a = """ import sys import atexit def f(): atexit.register(func) """ self.check(b, a) def test_no_sys_import(self): b = """sys.exitfunc = f""" a = """atexit.register(f)""" msg = ("Can't find sys import; Please add an atexit import at the " "top of your file.") self.warns(b, a, msg) def test_unchanged(self): s = """f(sys.exitfunc)""" self.unchanged(s) class Test_asserts(FixerTestCase): fixer = "asserts" def test_deprecated_names(self): tests = [ ('self.assert_(True)', 'self.assertTrue(True)'), ('self.assertEquals(2, 2)', 'self.assertEqual(2, 2)'), ('self.assertNotEquals(2, 3)', 'self.assertNotEqual(2, 3)'), ('self.assertAlmostEquals(2, 3)', 'self.assertAlmostEqual(2, 3)'), ('self.assertNotAlmostEquals(2, 8)', 'self.assertNotAlmostEqual(2, 8)'), ('self.failUnlessEqual(2, 2)', 'self.assertEqual(2, 2)'), ('self.failIfEqual(2, 3)', 'self.assertNotEqual(2, 3)'), ('self.failUnlessAlmostEqual(2, 3)', 'self.assertAlmostEqual(2, 3)'), ('self.failIfAlmostEqual(2, 8)', 'self.assertNotAlmostEqual(2, 8)'), ('self.failUnless(True)', 'self.assertTrue(True)'), ('self.failUnlessRaises(foo)', 'self.assertRaises(foo)'), ('self.failIf(False)', 'self.assertFalse(False)'), ] for b, a in tests: self.check(b, a) def test_variants(self): b = 'eq = self.assertEquals' a = 'eq = self.assertEqual' self.check(b, a) b = 'self.assertEquals(2, 3, msg="fail")' a = 'self.assertEqual(2, 3, msg="fail")' self.check(b, a) b = 'self.assertEquals(2, 3, msg="fail") # foo' a = 'self.assertEqual(2, 3, msg="fail") # foo' self.check(b, a) b = 'self.assertEquals (2, 3)' a = 'self.assertEqual (2, 3)' self.check(b, a) b = ' self.assertEquals (2, 3)' a = ' self.assertEqual (2, 3)' self.check(b, a) b = 'with self.failUnlessRaises(Explosion): explode()' a = 'with self.assertRaises(Explosion): explode()' self.check(b, a) b = 'with self.failUnlessRaises(Explosion) as cm: explode()' a = 'with self.assertRaises(Explosion) as cm: explode()' self.check(b, a) def test_unchanged(self): self.unchanged('self.assertEqualsOnSaturday') self.unchanged('self.assertEqualsOnSaturday(3, 5)')
122,488
4,641
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/test_pytree.py
# Copyright 2006 Google, Inc. All Rights Reserved. # Licensed to PSF under a Contributor Agreement. """Unit tests for pytree.py. NOTE: Please *don't* add doc strings to individual test methods! In verbose mode, printing of the module, class and method name is much more helpful than printing of (the first line of) the docstring, especially when debugging a test. """ # Testing imports from . import support from lib2to3 import pytree try: sorted except NameError: def sorted(lst): l = list(lst) l.sort() return l class TestNodes(support.TestCase): """Unit tests for nodes (Base, Leaf, Node).""" def test_instantiate_base(self): if __debug__: # Test that instantiating Base() raises an AssertionError self.assertRaises(AssertionError, pytree.Base) def test_leaf(self): l1 = pytree.Leaf(100, "foo") self.assertEqual(l1.type, 100) self.assertEqual(l1.value, "foo") def test_leaf_repr(self): l1 = pytree.Leaf(100, "foo") self.assertEqual(repr(l1), "Leaf(100, 'foo')") def test_leaf_str(self): l1 = pytree.Leaf(100, "foo") self.assertEqual(str(l1), "foo") l2 = pytree.Leaf(100, "foo", context=(" ", (10, 1))) self.assertEqual(str(l2), " foo") def test_leaf_str_numeric_value(self): # Make sure that the Leaf's value is stringified. Failing to # do this can cause a TypeError in certain situations. l1 = pytree.Leaf(2, 5) l1.prefix = "foo_" self.assertEqual(str(l1), "foo_5") def test_leaf_equality(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "foo", context=(" ", (1, 0))) self.assertEqual(l1, l2) l3 = pytree.Leaf(101, "foo") l4 = pytree.Leaf(100, "bar") self.assertNotEqual(l1, l3) self.assertNotEqual(l1, l4) def test_leaf_prefix(self): l1 = pytree.Leaf(100, "foo") self.assertEqual(l1.prefix, "") self.assertFalse(l1.was_changed) l1.prefix = " ##\n\n" self.assertEqual(l1.prefix, " ##\n\n") self.assertTrue(l1.was_changed) def test_node(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(200, "bar") n1 = pytree.Node(1000, [l1, l2]) self.assertEqual(n1.type, 1000) self.assertEqual(n1.children, [l1, l2]) def test_node_repr(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0))) n1 = pytree.Node(1000, [l1, l2]) self.assertEqual(repr(n1), "Node(1000, [%s, %s])" % (repr(l1), repr(l2))) def test_node_str(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0))) n1 = pytree.Node(1000, [l1, l2]) self.assertEqual(str(n1), "foo bar") def test_node_prefix(self): l1 = pytree.Leaf(100, "foo") self.assertEqual(l1.prefix, "") n1 = pytree.Node(1000, [l1]) self.assertEqual(n1.prefix, "") n1.prefix = " " self.assertEqual(n1.prefix, " ") self.assertEqual(l1.prefix, " ") def test_get_suffix(self): l1 = pytree.Leaf(100, "foo", prefix="a") l2 = pytree.Leaf(100, "bar", prefix="b") n1 = pytree.Node(1000, [l1, l2]) self.assertEqual(l1.get_suffix(), l2.prefix) self.assertEqual(l2.get_suffix(), "") self.assertEqual(n1.get_suffix(), "") l3 = pytree.Leaf(100, "bar", prefix="c") n2 = pytree.Node(1000, [n1, l3]) self.assertEqual(n1.get_suffix(), l3.prefix) self.assertEqual(l3.get_suffix(), "") self.assertEqual(n2.get_suffix(), "") def test_node_equality(self): n1 = pytree.Node(1000, ()) n2 = pytree.Node(1000, [], context=(" ", (1, 0))) self.assertEqual(n1, n2) n3 = pytree.Node(1001, ()) self.assertNotEqual(n1, n3) def test_node_recursive_equality(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "foo") n1 = pytree.Node(1000, [l1]) n2 = pytree.Node(1000, [l2]) self.assertEqual(n1, n2) l3 = pytree.Leaf(100, "bar") n3 = pytree.Node(1000, [l3]) self.assertNotEqual(n1, n3) def test_replace(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "+") l3 = pytree.Leaf(100, "bar") n1 = pytree.Node(1000, [l1, l2, l3]) self.assertEqual(n1.children, [l1, l2, l3]) self.assertIsInstance(n1.children, list) self.assertFalse(n1.was_changed) l2new = pytree.Leaf(100, "-") l2.replace(l2new) self.assertEqual(n1.children, [l1, l2new, l3]) self.assertIsInstance(n1.children, list) self.assertTrue(n1.was_changed) def test_replace_with_list(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "+") l3 = pytree.Leaf(100, "bar") n1 = pytree.Node(1000, [l1, l2, l3]) l2.replace([pytree.Leaf(100, "*"), pytree.Leaf(100, "*")]) self.assertEqual(str(n1), "foo**bar") self.assertIsInstance(n1.children, list) def test_leaves(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "bar") l3 = pytree.Leaf(100, "fooey") n2 = pytree.Node(1000, [l1, l2]) n3 = pytree.Node(1000, [l3]) n1 = pytree.Node(1000, [n2, n3]) self.assertEqual(list(n1.leaves()), [l1, l2, l3]) def test_depth(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "bar") n2 = pytree.Node(1000, [l1, l2]) n3 = pytree.Node(1000, []) n1 = pytree.Node(1000, [n2, n3]) self.assertEqual(l1.depth(), 2) self.assertEqual(n3.depth(), 1) self.assertEqual(n1.depth(), 0) def test_post_order(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "bar") l3 = pytree.Leaf(100, "fooey") c1 = pytree.Node(1000, [l1, l2]) n1 = pytree.Node(1000, [c1, l3]) self.assertEqual(list(n1.post_order()), [l1, l2, c1, l3, n1]) def test_pre_order(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "bar") l3 = pytree.Leaf(100, "fooey") c1 = pytree.Node(1000, [l1, l2]) n1 = pytree.Node(1000, [c1, l3]) self.assertEqual(list(n1.pre_order()), [n1, c1, l1, l2, l3]) def test_changed(self): l1 = pytree.Leaf(100, "f") self.assertFalse(l1.was_changed) l1.changed() self.assertTrue(l1.was_changed) l1 = pytree.Leaf(100, "f") n1 = pytree.Node(1000, [l1]) self.assertFalse(n1.was_changed) n1.changed() self.assertTrue(n1.was_changed) l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "+") l3 = pytree.Leaf(100, "bar") n1 = pytree.Node(1000, [l1, l2, l3]) n2 = pytree.Node(1000, [n1]) self.assertFalse(l1.was_changed) self.assertFalse(n1.was_changed) self.assertFalse(n2.was_changed) n1.changed() self.assertTrue(n1.was_changed) self.assertTrue(n2.was_changed) self.assertFalse(l1.was_changed) def test_leaf_constructor_prefix(self): for prefix in ("xyz_", ""): l1 = pytree.Leaf(100, "self", prefix=prefix) self.assertTrue(str(l1), prefix + "self") self.assertEqual(l1.prefix, prefix) def test_node_constructor_prefix(self): for prefix in ("xyz_", ""): l1 = pytree.Leaf(100, "self") l2 = pytree.Leaf(100, "foo", prefix="_") n1 = pytree.Node(1000, [l1, l2], prefix=prefix) self.assertTrue(str(n1), prefix + "self_foo") self.assertEqual(n1.prefix, prefix) self.assertEqual(l1.prefix, prefix) self.assertEqual(l2.prefix, "_") def test_remove(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "foo") n1 = pytree.Node(1000, [l1, l2]) n2 = pytree.Node(1000, [n1]) self.assertEqual(n1.remove(), 0) self.assertEqual(n2.children, []) self.assertEqual(l1.parent, n1) self.assertEqual(n1.parent, None) self.assertEqual(n2.parent, None) self.assertFalse(n1.was_changed) self.assertTrue(n2.was_changed) self.assertEqual(l2.remove(), 1) self.assertEqual(l1.remove(), 0) self.assertEqual(n1.children, []) self.assertEqual(l1.parent, None) self.assertEqual(n1.parent, None) self.assertEqual(n2.parent, None) self.assertTrue(n1.was_changed) self.assertTrue(n2.was_changed) def test_remove_parentless(self): n1 = pytree.Node(1000, []) n1.remove() self.assertEqual(n1.parent, None) l1 = pytree.Leaf(100, "foo") l1.remove() self.assertEqual(l1.parent, None) def test_node_set_child(self): l1 = pytree.Leaf(100, "foo") n1 = pytree.Node(1000, [l1]) l2 = pytree.Leaf(100, "bar") n1.set_child(0, l2) self.assertEqual(l1.parent, None) self.assertEqual(l2.parent, n1) self.assertEqual(n1.children, [l2]) n2 = pytree.Node(1000, [l1]) n2.set_child(0, n1) self.assertEqual(l1.parent, None) self.assertEqual(n1.parent, n2) self.assertEqual(n2.parent, None) self.assertEqual(n2.children, [n1]) self.assertRaises(IndexError, n1.set_child, 4, l2) # I don't care what it raises, so long as it's an exception self.assertRaises(Exception, n1.set_child, 0, list) def test_node_insert_child(self): l1 = pytree.Leaf(100, "foo") n1 = pytree.Node(1000, [l1]) l2 = pytree.Leaf(100, "bar") n1.insert_child(0, l2) self.assertEqual(l2.parent, n1) self.assertEqual(n1.children, [l2, l1]) l3 = pytree.Leaf(100, "abc") n1.insert_child(2, l3) self.assertEqual(n1.children, [l2, l1, l3]) # I don't care what it raises, so long as it's an exception self.assertRaises(Exception, n1.insert_child, 0, list) def test_node_append_child(self): n1 = pytree.Node(1000, []) l1 = pytree.Leaf(100, "foo") n1.append_child(l1) self.assertEqual(l1.parent, n1) self.assertEqual(n1.children, [l1]) l2 = pytree.Leaf(100, "bar") n1.append_child(l2) self.assertEqual(l2.parent, n1) self.assertEqual(n1.children, [l1, l2]) # I don't care what it raises, so long as it's an exception self.assertRaises(Exception, n1.append_child, list) def test_node_next_sibling(self): n1 = pytree.Node(1000, []) n2 = pytree.Node(1000, []) p1 = pytree.Node(1000, [n1, n2]) self.assertIs(n1.next_sibling, n2) self.assertEqual(n2.next_sibling, None) self.assertEqual(p1.next_sibling, None) def test_leaf_next_sibling(self): l1 = pytree.Leaf(100, "a") l2 = pytree.Leaf(100, "b") p1 = pytree.Node(1000, [l1, l2]) self.assertIs(l1.next_sibling, l2) self.assertEqual(l2.next_sibling, None) self.assertEqual(p1.next_sibling, None) def test_node_prev_sibling(self): n1 = pytree.Node(1000, []) n2 = pytree.Node(1000, []) p1 = pytree.Node(1000, [n1, n2]) self.assertIs(n2.prev_sibling, n1) self.assertEqual(n1.prev_sibling, None) self.assertEqual(p1.prev_sibling, None) def test_leaf_prev_sibling(self): l1 = pytree.Leaf(100, "a") l2 = pytree.Leaf(100, "b") p1 = pytree.Node(1000, [l1, l2]) self.assertIs(l2.prev_sibling, l1) self.assertEqual(l1.prev_sibling, None) self.assertEqual(p1.prev_sibling, None) class TestPatterns(support.TestCase): """Unit tests for tree matching patterns.""" def test_basic_patterns(self): # Build a tree l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "bar") l3 = pytree.Leaf(100, "foo") n1 = pytree.Node(1000, [l1, l2]) n2 = pytree.Node(1000, [l3]) root = pytree.Node(1000, [n1, n2]) # Build a pattern matching a leaf pl = pytree.LeafPattern(100, "foo", name="pl") r = {} self.assertFalse(pl.match(root, results=r)) self.assertEqual(r, {}) self.assertFalse(pl.match(n1, results=r)) self.assertEqual(r, {}) self.assertFalse(pl.match(n2, results=r)) self.assertEqual(r, {}) self.assertTrue(pl.match(l1, results=r)) self.assertEqual(r, {"pl": l1}) r = {} self.assertFalse(pl.match(l2, results=r)) self.assertEqual(r, {}) # Build a pattern matching a node pn = pytree.NodePattern(1000, [pl], name="pn") self.assertFalse(pn.match(root, results=r)) self.assertEqual(r, {}) self.assertFalse(pn.match(n1, results=r)) self.assertEqual(r, {}) self.assertTrue(pn.match(n2, results=r)) self.assertEqual(r, {"pn": n2, "pl": l3}) r = {} self.assertFalse(pn.match(l1, results=r)) self.assertEqual(r, {}) self.assertFalse(pn.match(l2, results=r)) self.assertEqual(r, {}) def test_wildcard(self): # Build a tree for testing l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "bar") l3 = pytree.Leaf(100, "foo") n1 = pytree.Node(1000, [l1, l2]) n2 = pytree.Node(1000, [l3]) root = pytree.Node(1000, [n1, n2]) # Build a pattern pl = pytree.LeafPattern(100, "foo", name="pl") pn = pytree.NodePattern(1000, [pl], name="pn") pw = pytree.WildcardPattern([[pn], [pl, pl]], name="pw") r = {} self.assertFalse(pw.match_seq([root], r)) self.assertEqual(r, {}) self.assertFalse(pw.match_seq([n1], r)) self.assertEqual(r, {}) self.assertTrue(pw.match_seq([n2], r)) # These are easier to debug self.assertEqual(sorted(r.keys()), ["pl", "pn", "pw"]) self.assertEqual(r["pl"], l1) self.assertEqual(r["pn"], n2) self.assertEqual(r["pw"], [n2]) # But this is equivalent self.assertEqual(r, {"pl": l1, "pn": n2, "pw": [n2]}) r = {} self.assertTrue(pw.match_seq([l1, l3], r)) self.assertEqual(r, {"pl": l3, "pw": [l1, l3]}) self.assertIs(r["pl"], l3) r = {} def test_generate_matches(self): la = pytree.Leaf(1, "a") lb = pytree.Leaf(1, "b") lc = pytree.Leaf(1, "c") ld = pytree.Leaf(1, "d") le = pytree.Leaf(1, "e") lf = pytree.Leaf(1, "f") leaves = [la, lb, lc, ld, le, lf] root = pytree.Node(1000, leaves) pa = pytree.LeafPattern(1, "a", "pa") pb = pytree.LeafPattern(1, "b", "pb") pc = pytree.LeafPattern(1, "c", "pc") pd = pytree.LeafPattern(1, "d", "pd") pe = pytree.LeafPattern(1, "e", "pe") pf = pytree.LeafPattern(1, "f", "pf") pw = pytree.WildcardPattern([[pa, pb, pc], [pd, pe], [pa, pb], [pc, pd], [pe, pf]], min=1, max=4, name="pw") self.assertEqual([x[0] for x in pw.generate_matches(leaves)], [3, 5, 2, 4, 6]) pr = pytree.NodePattern(type=1000, content=[pw], name="pr") matches = list(pytree.generate_matches([pr], [root])) self.assertEqual(len(matches), 1) c, r = matches[0] self.assertEqual(c, 1) self.assertEqual(str(r["pr"]), "abcdef") self.assertEqual(r["pw"], [la, lb, lc, ld, le, lf]) for c in "abcdef": self.assertEqual(r["p" + c], pytree.Leaf(1, c)) def test_has_key_example(self): pattern = pytree.NodePattern(331, (pytree.LeafPattern(7), pytree.WildcardPattern(name="args"), pytree.LeafPattern(8))) l1 = pytree.Leaf(7, "(") l2 = pytree.Leaf(3, "x") l3 = pytree.Leaf(8, ")") node = pytree.Node(331, [l1, l2, l3]) r = {} self.assertTrue(pattern.match(node, r)) self.assertEqual(r["args"], [l2])
16,382
473
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/test_refactor.py
""" Unit tests for refactor.py. """ import sys import os import codecs import io import re import tempfile import shutil import unittest from lib2to3 import refactor, pygram, fixer_base from lib2to3.pgen2 import token TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data") FIXER_DIR = os.path.join(TEST_DATA_DIR, "fixers") sys.path.append(FIXER_DIR) try: _DEFAULT_FIXERS = refactor.get_fixers_from_package("myfixes") finally: sys.path.pop() _2TO3_FIXERS = refactor.get_fixers_from_package("lib2to3.fixes") class TestRefactoringTool(unittest.TestCase): def setUp(self): sys.path.append(FIXER_DIR) def tearDown(self): sys.path.pop() def check_instances(self, instances, classes): for inst, cls in zip(instances, classes): if not isinstance(inst, cls): self.fail("%s are not instances of %s" % instances, classes) def rt(self, options=None, fixers=_DEFAULT_FIXERS, explicit=None): return refactor.RefactoringTool(fixers, options, explicit) def test_print_function_option(self): rt = self.rt({"print_function" : True}) self.assertIs(rt.grammar, pygram.python_grammar_no_print_statement) self.assertIs(rt.driver.grammar, pygram.python_grammar_no_print_statement) def test_write_unchanged_files_option(self): rt = self.rt() self.assertFalse(rt.write_unchanged_files) rt = self.rt({"write_unchanged_files" : True}) self.assertTrue(rt.write_unchanged_files) def test_fixer_loading_helpers(self): contents = ["explicit", "first", "last", "parrot", "preorder"] non_prefixed = refactor.get_all_fix_names("myfixes") prefixed = refactor.get_all_fix_names("myfixes", False) full_names = refactor.get_fixers_from_package("myfixes") self.assertEqual(prefixed, ["fix_" + name for name in contents]) self.assertEqual(non_prefixed, contents) self.assertEqual(full_names, ["myfixes.fix_" + name for name in contents]) def test_detect_future_features(self): run = refactor._detect_future_features fs = frozenset empty = fs() self.assertEqual(run(""), empty) self.assertEqual(run("from __future__ import print_function"), fs(("print_function",))) self.assertEqual(run("from __future__ import generators"), fs(("generators",))) self.assertEqual(run("from __future__ import generators, feature"), fs(("generators", "feature"))) inp = "from __future__ import generators, print_function" self.assertEqual(run(inp), fs(("generators", "print_function"))) inp ="from __future__ import print_function, generators" self.assertEqual(run(inp), fs(("print_function", "generators"))) inp = "from __future__ import (print_function,)" self.assertEqual(run(inp), fs(("print_function",))) inp = "from __future__ import (generators, print_function)" self.assertEqual(run(inp), fs(("generators", "print_function"))) inp = "from __future__ import (generators, nested_scopes)" self.assertEqual(run(inp), fs(("generators", "nested_scopes"))) inp = """from __future__ import generators from __future__ import print_function""" self.assertEqual(run(inp), fs(("generators", "print_function"))) invalid = ("from", "from 4", "from x", "from x 5", "from x im", "from x import", "from x import 4", ) for inp in invalid: self.assertEqual(run(inp), empty) inp = "'docstring'\nfrom __future__ import print_function" self.assertEqual(run(inp), fs(("print_function",))) inp = "'docstring'\n'somng'\nfrom __future__ import print_function" self.assertEqual(run(inp), empty) inp = "# comment\nfrom __future__ import print_function" self.assertEqual(run(inp), fs(("print_function",))) inp = "# comment\n'doc'\nfrom __future__ import print_function" self.assertEqual(run(inp), fs(("print_function",))) inp = "class x: pass\nfrom __future__ import print_function" self.assertEqual(run(inp), empty) def test_get_headnode_dict(self): class NoneFix(fixer_base.BaseFix): pass class FileInputFix(fixer_base.BaseFix): PATTERN = "file_input< any * >" class SimpleFix(fixer_base.BaseFix): PATTERN = "'name'" no_head = NoneFix({}, []) with_head = FileInputFix({}, []) simple = SimpleFix({}, []) d = refactor._get_headnode_dict([no_head, with_head, simple]) top_fixes = d.pop(pygram.python_symbols.file_input) self.assertEqual(top_fixes, [with_head, no_head]) name_fixes = d.pop(token.NAME) self.assertEqual(name_fixes, [simple, no_head]) for fixes in d.values(): self.assertEqual(fixes, [no_head]) def test_fixer_loading(self): from myfixes.fix_first import FixFirst from myfixes.fix_last import FixLast from myfixes.fix_parrot import FixParrot from myfixes.fix_preorder import FixPreorder rt = self.rt() pre, post = rt.get_fixers() self.check_instances(pre, [FixPreorder]) self.check_instances(post, [FixFirst, FixParrot, FixLast]) def test_naughty_fixers(self): self.assertRaises(ImportError, self.rt, fixers=["not_here"]) self.assertRaises(refactor.FixerError, self.rt, fixers=["no_fixer_cls"]) self.assertRaises(refactor.FixerError, self.rt, fixers=["bad_order"]) def test_refactor_string(self): rt = self.rt() input = "def parrot(): pass\n\n" tree = rt.refactor_string(input, "<test>") self.assertNotEqual(str(tree), input) input = "def f(): pass\n\n" tree = rt.refactor_string(input, "<test>") self.assertEqual(str(tree), input) def test_refactor_stdin(self): class MyRT(refactor.RefactoringTool): def print_output(self, old_text, new_text, filename, equal): results.extend([old_text, new_text, filename, equal]) results = [] rt = MyRT(_DEFAULT_FIXERS) save = sys.stdin sys.stdin = io.StringIO("def parrot(): pass\n\n") try: rt.refactor_stdin() finally: sys.stdin = save expected = ["def parrot(): pass\n\n", "def cheese(): pass\n\n", "<stdin>", False] self.assertEqual(results, expected) def check_file_refactoring(self, test_file, fixers=_2TO3_FIXERS, options=None, mock_log_debug=None, actually_write=True): tmpdir = tempfile.mkdtemp(prefix="2to3-test_refactor") self.addCleanup(shutil.rmtree, tmpdir) # make a copy of the tested file that we can write to shutil.copy(test_file, tmpdir) test_file = os.path.join(tmpdir, os.path.basename(test_file)) os.chmod(test_file, 0o644) def read_file(): with open(test_file, "rb") as fp: return fp.read() old_contents = read_file() rt = self.rt(fixers=fixers, options=options) if mock_log_debug: rt.log_debug = mock_log_debug rt.refactor_file(test_file) self.assertEqual(old_contents, read_file()) if not actually_write: return rt.refactor_file(test_file, True) new_contents = read_file() self.assertNotEqual(old_contents, new_contents) return new_contents def test_refactor_file(self): test_file = os.path.join(FIXER_DIR, "parrot_example.py") self.check_file_refactoring(test_file, _DEFAULT_FIXERS) def test_refactor_file_write_unchanged_file(self): test_file = os.path.join(FIXER_DIR, "parrot_example.py") debug_messages = [] def recording_log_debug(msg, *args): debug_messages.append(msg % args) self.check_file_refactoring(test_file, fixers=(), options={"write_unchanged_files": True}, mock_log_debug=recording_log_debug, actually_write=False) # Testing that it logged this message when write=False was passed is # sufficient to see that it did not bail early after "No changes". message_regex = r"Not writing changes to .*%s" % \ re.escape(os.sep + os.path.basename(test_file)) for message in debug_messages: if "Not writing changes" in message: self.assertRegex(message, message_regex) break else: self.fail("%r not matched in %r" % (message_regex, debug_messages)) def test_refactor_dir(self): def check(structure, expected): def mock_refactor_file(self, f, *args): got.append(f) save_func = refactor.RefactoringTool.refactor_file refactor.RefactoringTool.refactor_file = mock_refactor_file rt = self.rt() got = [] dir = tempfile.mkdtemp(prefix="2to3-test_refactor") try: os.mkdir(os.path.join(dir, "a_dir")) for fn in structure: open(os.path.join(dir, fn), "wb").close() rt.refactor_dir(dir) finally: refactor.RefactoringTool.refactor_file = save_func shutil.rmtree(dir) self.assertEqual(got, [os.path.join(dir, path) for path in expected]) check([], []) tree = ["nothing", "hi.py", ".dumb", ".after.py", "notpy.npy", "sappy"] expected = ["hi.py"] check(tree, expected) tree = ["hi.py", os.path.join("a_dir", "stuff.py")] check(tree, tree) def test_file_encoding(self): fn = os.path.join(TEST_DATA_DIR, "different_encoding.py") self.check_file_refactoring(fn) def test_false_file_encoding(self): fn = os.path.join(TEST_DATA_DIR, "false_encoding.py") data = self.check_file_refactoring(fn) def test_bom(self): fn = os.path.join(TEST_DATA_DIR, "bom.py") data = self.check_file_refactoring(fn) self.assertTrue(data.startswith(codecs.BOM_UTF8)) def test_crlf_newlines(self): old_sep = os.linesep os.linesep = "\r\n" try: fn = os.path.join(TEST_DATA_DIR, "crlf.py") fixes = refactor.get_fixers_from_package("lib2to3.fixes") self.check_file_refactoring(fn, fixes) finally: os.linesep = old_sep def test_refactor_docstring(self): rt = self.rt() doc = """ >>> example() 42 """ out = rt.refactor_docstring(doc, "<test>") self.assertEqual(out, doc) doc = """ >>> def parrot(): ... return 43 """ out = rt.refactor_docstring(doc, "<test>") self.assertNotEqual(out, doc) def test_explicit(self): from myfixes.fix_explicit import FixExplicit rt = self.rt(fixers=["myfixes.fix_explicit"]) self.assertEqual(len(rt.post_order), 0) rt = self.rt(explicit=["myfixes.fix_explicit"]) for fix in rt.post_order: if isinstance(fix, FixExplicit): break else: self.fail("explicit fixer not loaded")
11,772
317
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/test_main.py
# -*- coding: utf-8 -*- import codecs import io import logging import os import re import shutil import sys import tempfile import unittest from lib2to3 import main TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data") PY2_TEST_MODULE = os.path.join(TEST_DATA_DIR, "py2_test_grammar.py") class TestMain(unittest.TestCase): def setUp(self): self.temp_dir = None # tearDown() will rmtree this directory if set. def tearDown(self): # Clean up logging configuration down by main. del logging.root.handlers[:] if self.temp_dir: shutil.rmtree(self.temp_dir) def run_2to3_capture(self, args, in_capture, out_capture, err_capture): save_stdin = sys.stdin save_stdout = sys.stdout save_stderr = sys.stderr sys.stdin = in_capture sys.stdout = out_capture sys.stderr = err_capture try: return main.main("lib2to3.fixes", args) finally: sys.stdin = save_stdin sys.stdout = save_stdout sys.stderr = save_stderr def test_unencodable_diff(self): input_stream = io.StringIO("print 'nothing'\nprint u'über'\n") out = io.BytesIO() out_enc = codecs.getwriter("ascii")(out) err = io.StringIO() ret = self.run_2to3_capture(["-"], input_stream, out_enc, err) self.assertEqual(ret, 0) output = out.getvalue().decode("ascii") self.assertIn("-print 'nothing'", output) self.assertIn("WARNING: couldn't encode <stdin>'s diff for " "your terminal", err.getvalue()) def setup_test_source_trees(self): """Setup a test source tree and output destination tree.""" self.temp_dir = tempfile.mkdtemp() # tearDown() cleans this up. self.py2_src_dir = os.path.join(self.temp_dir, "python2_project") self.py3_dest_dir = os.path.join(self.temp_dir, "python3_project") os.mkdir(self.py2_src_dir) os.mkdir(self.py3_dest_dir) # Turn it into a package with a few files. self.setup_files = [] open(os.path.join(self.py2_src_dir, "__init__.py"), "w").close() self.setup_files.append("__init__.py") shutil.copy(PY2_TEST_MODULE, self.py2_src_dir) self.setup_files.append(os.path.basename(PY2_TEST_MODULE)) self.trivial_py2_file = os.path.join(self.py2_src_dir, "trivial.py") self.init_py2_file = os.path.join(self.py2_src_dir, "__init__.py") with open(self.trivial_py2_file, "w") as trivial: trivial.write("print 'I need a simple conversion.'") self.setup_files.append("trivial.py") def test_filename_changing_on_output_single_dir(self): """2to3 a single directory with a new output dir and suffix.""" self.setup_test_source_trees() out = io.StringIO() err = io.StringIO() suffix = "TEST" ret = self.run_2to3_capture( ["-n", "--add-suffix", suffix, "--write-unchanged-files", "--no-diffs", "--output-dir", self.py3_dest_dir, self.py2_src_dir], io.StringIO(""), out, err) self.assertEqual(ret, 0) stderr = err.getvalue() self.assertIn(" implies -w.", stderr) self.assertIn( "Output in %r will mirror the input directory %r layout" % ( self.py3_dest_dir, self.py2_src_dir), stderr) self.assertEqual(set(name+suffix for name in self.setup_files), set(os.listdir(self.py3_dest_dir))) for name in self.setup_files: self.assertIn("Writing converted %s to %s" % ( os.path.join(self.py2_src_dir, name), os.path.join(self.py3_dest_dir, name+suffix)), stderr) sep = re.escape(os.sep) self.assertRegex( stderr, r"No changes to .*/__init__\.py".replace("/", sep)) self.assertNotRegex( stderr, r"No changes to .*/trivial\.py".replace("/", sep)) def test_filename_changing_on_output_two_files(self): """2to3 two files in one directory with a new output dir.""" self.setup_test_source_trees() err = io.StringIO() py2_files = [self.trivial_py2_file, self.init_py2_file] expected_files = set(os.path.basename(name) for name in py2_files) ret = self.run_2to3_capture( ["-n", "-w", "--write-unchanged-files", "--no-diffs", "--output-dir", self.py3_dest_dir] + py2_files, io.StringIO(""), io.StringIO(), err) self.assertEqual(ret, 0) stderr = err.getvalue() self.assertIn( "Output in %r will mirror the input directory %r layout" % ( self.py3_dest_dir, self.py2_src_dir), stderr) self.assertEqual(expected_files, set(os.listdir(self.py3_dest_dir))) def test_filename_changing_on_output_single_file(self): """2to3 a single file with a new output dir.""" self.setup_test_source_trees() err = io.StringIO() ret = self.run_2to3_capture( ["-n", "-w", "--no-diffs", "--output-dir", self.py3_dest_dir, self.trivial_py2_file], io.StringIO(""), io.StringIO(), err) self.assertEqual(ret, 0) stderr = err.getvalue() self.assertIn( "Output in %r will mirror the input directory %r layout" % ( self.py3_dest_dir, self.py2_src_dir), stderr) self.assertEqual(set([os.path.basename(self.trivial_py2_file)]), set(os.listdir(self.py3_dest_dir))) if __name__ == '__main__': unittest.main()
5,740
140
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/__init__.py
# Author: Collin Winter import os import unittest from test.support import load_package_tests def load_tests(*args): return load_package_tests(os.path.dirname(__file__), *args)
184
10
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/data/different_encoding.py
#!/usr/bin/env python # -*- coding: utf-8 -*- print u'ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ' def f(x): print '%s\t-> α(%2i):%s β(%s)'
230
7
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/data/bom.py
# coding: utf-8 print "BOM BOOM!"
37
3
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/data/crlf.py
print "hi" print "Like bad Windows newlines?"
47
4
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/data/README
In this directory: - py2_test_grammar.py -- test file that exercises most/all of Python 2.x's grammar. - py3_test_grammar.py -- test file that exercises most/all of Python 3.x's grammar. - infinite_recursion.py -- test file that causes lib2to3's faster recursive pattern matching scheme to fail, but passes when lib2to3 falls back to iterative pattern matching. - fixes/ -- for use by test_refactor.py
404
7
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/data/false_encoding.py
#!/usr/bin/env python print '#coding=0'
40
3
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/data/infinite_recursion.py
# This file is used to verify that 2to3 falls back to a slower, iterative pattern matching # scheme in the event that the faster recursive system fails due to infinite recursion. from ctypes import * STRING = c_char_p OSUnknownByteOrder = 0 UIT_PROMPT = 1 P_PGID = 2 P_PID = 1 UIT_ERROR = 5 UIT_INFO = 4 UIT_NONE = 0 P_ALL = 0 UIT_VERIFY = 2 OSBigEndian = 2 UIT_BOOLEAN = 3 OSLittleEndian = 1 __darwin_nl_item = c_int __darwin_wctrans_t = c_int __darwin_wctype_t = c_ulong __int8_t = c_byte __uint8_t = c_ubyte __int16_t = c_short __uint16_t = c_ushort __int32_t = c_int __uint32_t = c_uint __int64_t = c_longlong __uint64_t = c_ulonglong __darwin_intptr_t = c_long __darwin_natural_t = c_uint __darwin_ct_rune_t = c_int class __mbstate_t(Union): pass __mbstate_t._pack_ = 4 __mbstate_t._fields_ = [ ('__mbstate8', c_char * 128), ('_mbstateL', c_longlong), ] assert sizeof(__mbstate_t) == 128, sizeof(__mbstate_t) assert alignment(__mbstate_t) == 4, alignment(__mbstate_t) __darwin_mbstate_t = __mbstate_t __darwin_ptrdiff_t = c_int __darwin_size_t = c_ulong __darwin_va_list = STRING __darwin_wchar_t = c_int __darwin_rune_t = __darwin_wchar_t __darwin_wint_t = c_int __darwin_clock_t = c_ulong __darwin_socklen_t = __uint32_t __darwin_ssize_t = c_long __darwin_time_t = c_long sig_atomic_t = c_int class sigcontext(Structure): pass sigcontext._fields_ = [ ('sc_onstack', c_int), ('sc_mask', c_int), ('sc_eax', c_uint), ('sc_ebx', c_uint), ('sc_ecx', c_uint), ('sc_edx', c_uint), ('sc_edi', c_uint), ('sc_esi', c_uint), ('sc_ebp', c_uint), ('sc_esp', c_uint), ('sc_ss', c_uint), ('sc_eflags', c_uint), ('sc_eip', c_uint), ('sc_cs', c_uint), ('sc_ds', c_uint), ('sc_es', c_uint), ('sc_fs', c_uint), ('sc_gs', c_uint), ] assert sizeof(sigcontext) == 72, sizeof(sigcontext) assert alignment(sigcontext) == 4, alignment(sigcontext) u_int8_t = c_ubyte u_int16_t = c_ushort u_int32_t = c_uint u_int64_t = c_ulonglong int32_t = c_int register_t = int32_t user_addr_t = u_int64_t user_size_t = u_int64_t int64_t = c_longlong user_ssize_t = int64_t user_long_t = int64_t user_ulong_t = u_int64_t user_time_t = int64_t syscall_arg_t = u_int64_t # values for unnamed enumeration class aes_key_st(Structure): pass aes_key_st._fields_ = [ ('rd_key', c_ulong * 60), ('rounds', c_int), ] assert sizeof(aes_key_st) == 244, sizeof(aes_key_st) assert alignment(aes_key_st) == 4, alignment(aes_key_st) AES_KEY = aes_key_st class asn1_ctx_st(Structure): pass asn1_ctx_st._fields_ = [ ('p', POINTER(c_ubyte)), ('eos', c_int), ('error', c_int), ('inf', c_int), ('tag', c_int), ('xclass', c_int), ('slen', c_long), ('max', POINTER(c_ubyte)), ('q', POINTER(c_ubyte)), ('pp', POINTER(POINTER(c_ubyte))), ('line', c_int), ] assert sizeof(asn1_ctx_st) == 44, sizeof(asn1_ctx_st) assert alignment(asn1_ctx_st) == 4, alignment(asn1_ctx_st) ASN1_CTX = asn1_ctx_st class asn1_object_st(Structure): pass asn1_object_st._fields_ = [ ('sn', STRING), ('ln', STRING), ('nid', c_int), ('length', c_int), ('data', POINTER(c_ubyte)), ('flags', c_int), ] assert sizeof(asn1_object_st) == 24, sizeof(asn1_object_st) assert alignment(asn1_object_st) == 4, alignment(asn1_object_st) ASN1_OBJECT = asn1_object_st class asn1_string_st(Structure): pass asn1_string_st._fields_ = [ ('length', c_int), ('type', c_int), ('data', POINTER(c_ubyte)), ('flags', c_long), ] assert sizeof(asn1_string_st) == 16, sizeof(asn1_string_st) assert alignment(asn1_string_st) == 4, alignment(asn1_string_st) ASN1_STRING = asn1_string_st class ASN1_ENCODING_st(Structure): pass ASN1_ENCODING_st._fields_ = [ ('enc', POINTER(c_ubyte)), ('len', c_long), ('modified', c_int), ] assert sizeof(ASN1_ENCODING_st) == 12, sizeof(ASN1_ENCODING_st) assert alignment(ASN1_ENCODING_st) == 4, alignment(ASN1_ENCODING_st) ASN1_ENCODING = ASN1_ENCODING_st class asn1_string_table_st(Structure): pass asn1_string_table_st._fields_ = [ ('nid', c_int), ('minsize', c_long), ('maxsize', c_long), ('mask', c_ulong), ('flags', c_ulong), ] assert sizeof(asn1_string_table_st) == 20, sizeof(asn1_string_table_st) assert alignment(asn1_string_table_st) == 4, alignment(asn1_string_table_st) ASN1_STRING_TABLE = asn1_string_table_st class ASN1_TEMPLATE_st(Structure): pass ASN1_TEMPLATE_st._fields_ = [ ] ASN1_TEMPLATE = ASN1_TEMPLATE_st class ASN1_ITEM_st(Structure): pass ASN1_ITEM = ASN1_ITEM_st ASN1_ITEM_st._fields_ = [ ] class ASN1_TLC_st(Structure): pass ASN1_TLC = ASN1_TLC_st ASN1_TLC_st._fields_ = [ ] class ASN1_VALUE_st(Structure): pass ASN1_VALUE_st._fields_ = [ ] ASN1_VALUE = ASN1_VALUE_st ASN1_ITEM_EXP = ASN1_ITEM class asn1_type_st(Structure): pass class N12asn1_type_st4DOLLAR_11E(Union): pass ASN1_BOOLEAN = c_int ASN1_INTEGER = asn1_string_st ASN1_ENUMERATED = asn1_string_st ASN1_BIT_STRING = asn1_string_st ASN1_OCTET_STRING = asn1_string_st ASN1_PRINTABLESTRING = asn1_string_st ASN1_T61STRING = asn1_string_st ASN1_IA5STRING = asn1_string_st ASN1_GENERALSTRING = asn1_string_st ASN1_BMPSTRING = asn1_string_st ASN1_UNIVERSALSTRING = asn1_string_st ASN1_UTCTIME = asn1_string_st ASN1_GENERALIZEDTIME = asn1_string_st ASN1_VISIBLESTRING = asn1_string_st ASN1_UTF8STRING = asn1_string_st N12asn1_type_st4DOLLAR_11E._fields_ = [ ('ptr', STRING), ('boolean', ASN1_BOOLEAN), ('asn1_string', POINTER(ASN1_STRING)), ('object', POINTER(ASN1_OBJECT)), ('integer', POINTER(ASN1_INTEGER)), ('enumerated', POINTER(ASN1_ENUMERATED)), ('bit_string', POINTER(ASN1_BIT_STRING)), ('octet_string', POINTER(ASN1_OCTET_STRING)), ('printablestring', POINTER(ASN1_PRINTABLESTRING)), ('t61string', POINTER(ASN1_T61STRING)), ('ia5string', POINTER(ASN1_IA5STRING)), ('generalstring', POINTER(ASN1_GENERALSTRING)), ('bmpstring', POINTER(ASN1_BMPSTRING)), ('universalstring', POINTER(ASN1_UNIVERSALSTRING)), ('utctime', POINTER(ASN1_UTCTIME)), ('generalizedtime', POINTER(ASN1_GENERALIZEDTIME)), ('visiblestring', POINTER(ASN1_VISIBLESTRING)), ('utf8string', POINTER(ASN1_UTF8STRING)), ('set', POINTER(ASN1_STRING)), ('sequence', POINTER(ASN1_STRING)), ] assert sizeof(N12asn1_type_st4DOLLAR_11E) == 4, sizeof(N12asn1_type_st4DOLLAR_11E) assert alignment(N12asn1_type_st4DOLLAR_11E) == 4, alignment(N12asn1_type_st4DOLLAR_11E) asn1_type_st._fields_ = [ ('type', c_int), ('value', N12asn1_type_st4DOLLAR_11E), ] assert sizeof(asn1_type_st) == 8, sizeof(asn1_type_st) assert alignment(asn1_type_st) == 4, alignment(asn1_type_st) ASN1_TYPE = asn1_type_st class asn1_method_st(Structure): pass asn1_method_st._fields_ = [ ('i2d', CFUNCTYPE(c_int)), ('d2i', CFUNCTYPE(STRING)), ('create', CFUNCTYPE(STRING)), ('destroy', CFUNCTYPE(None)), ] assert sizeof(asn1_method_st) == 16, sizeof(asn1_method_st) assert alignment(asn1_method_st) == 4, alignment(asn1_method_st) ASN1_METHOD = asn1_method_st class asn1_header_st(Structure): pass asn1_header_st._fields_ = [ ('header', POINTER(ASN1_OCTET_STRING)), ('data', STRING), ('meth', POINTER(ASN1_METHOD)), ] assert sizeof(asn1_header_st) == 12, sizeof(asn1_header_st) assert alignment(asn1_header_st) == 4, alignment(asn1_header_st) ASN1_HEADER = asn1_header_st class BIT_STRING_BITNAME_st(Structure): pass BIT_STRING_BITNAME_st._fields_ = [ ('bitnum', c_int), ('lname', STRING), ('sname', STRING), ] assert sizeof(BIT_STRING_BITNAME_st) == 12, sizeof(BIT_STRING_BITNAME_st) assert alignment(BIT_STRING_BITNAME_st) == 4, alignment(BIT_STRING_BITNAME_st) BIT_STRING_BITNAME = BIT_STRING_BITNAME_st class bio_st(Structure): pass BIO = bio_st bio_info_cb = CFUNCTYPE(None, POINTER(bio_st), c_int, STRING, c_int, c_long, c_long) class bio_method_st(Structure): pass bio_method_st._fields_ = [ ('type', c_int), ('name', STRING), ('bwrite', CFUNCTYPE(c_int, POINTER(BIO), STRING, c_int)), ('bread', CFUNCTYPE(c_int, POINTER(BIO), STRING, c_int)), ('bputs', CFUNCTYPE(c_int, POINTER(BIO), STRING)), ('bgets', CFUNCTYPE(c_int, POINTER(BIO), STRING, c_int)), ('ctrl', CFUNCTYPE(c_long, POINTER(BIO), c_int, c_long, c_void_p)), ('create', CFUNCTYPE(c_int, POINTER(BIO))), ('destroy', CFUNCTYPE(c_int, POINTER(BIO))), ('callback_ctrl', CFUNCTYPE(c_long, POINTER(BIO), c_int, POINTER(bio_info_cb))), ] assert sizeof(bio_method_st) == 40, sizeof(bio_method_st) assert alignment(bio_method_st) == 4, alignment(bio_method_st) BIO_METHOD = bio_method_st class crypto_ex_data_st(Structure): pass class stack_st(Structure): pass STACK = stack_st crypto_ex_data_st._fields_ = [ ('sk', POINTER(STACK)), ('dummy', c_int), ] assert sizeof(crypto_ex_data_st) == 8, sizeof(crypto_ex_data_st) assert alignment(crypto_ex_data_st) == 4, alignment(crypto_ex_data_st) CRYPTO_EX_DATA = crypto_ex_data_st bio_st._fields_ = [ ('method', POINTER(BIO_METHOD)), ('callback', CFUNCTYPE(c_long, POINTER(bio_st), c_int, STRING, c_int, c_long, c_long)), ('cb_arg', STRING), ('init', c_int), ('shutdown', c_int), ('flags', c_int), ('retry_reason', c_int), ('num', c_int), ('ptr', c_void_p), ('next_bio', POINTER(bio_st)), ('prev_bio', POINTER(bio_st)), ('references', c_int), ('num_read', c_ulong), ('num_write', c_ulong), ('ex_data', CRYPTO_EX_DATA), ] assert sizeof(bio_st) == 64, sizeof(bio_st) assert alignment(bio_st) == 4, alignment(bio_st) class bio_f_buffer_ctx_struct(Structure): pass bio_f_buffer_ctx_struct._fields_ = [ ('ibuf_size', c_int), ('obuf_size', c_int), ('ibuf', STRING), ('ibuf_len', c_int), ('ibuf_off', c_int), ('obuf', STRING), ('obuf_len', c_int), ('obuf_off', c_int), ] assert sizeof(bio_f_buffer_ctx_struct) == 32, sizeof(bio_f_buffer_ctx_struct) assert alignment(bio_f_buffer_ctx_struct) == 4, alignment(bio_f_buffer_ctx_struct) BIO_F_BUFFER_CTX = bio_f_buffer_ctx_struct class hostent(Structure): pass hostent._fields_ = [ ] class bf_key_st(Structure): pass bf_key_st._fields_ = [ ('P', c_uint * 18), ('S', c_uint * 1024), ] assert sizeof(bf_key_st) == 4168, sizeof(bf_key_st) assert alignment(bf_key_st) == 4, alignment(bf_key_st) BF_KEY = bf_key_st class bignum_st(Structure): pass bignum_st._fields_ = [ ('d', POINTER(c_ulong)), ('top', c_int), ('dmax', c_int), ('neg', c_int), ('flags', c_int), ] assert sizeof(bignum_st) == 20, sizeof(bignum_st) assert alignment(bignum_st) == 4, alignment(bignum_st) BIGNUM = bignum_st class bignum_ctx(Structure): pass bignum_ctx._fields_ = [ ] BN_CTX = bignum_ctx class bn_blinding_st(Structure): pass bn_blinding_st._fields_ = [ ('init', c_int), ('A', POINTER(BIGNUM)), ('Ai', POINTER(BIGNUM)), ('mod', POINTER(BIGNUM)), ('thread_id', c_ulong), ] assert sizeof(bn_blinding_st) == 20, sizeof(bn_blinding_st) assert alignment(bn_blinding_st) == 4, alignment(bn_blinding_st) BN_BLINDING = bn_blinding_st class bn_mont_ctx_st(Structure): pass bn_mont_ctx_st._fields_ = [ ('ri', c_int), ('RR', BIGNUM), ('N', BIGNUM), ('Ni', BIGNUM), ('n0', c_ulong), ('flags', c_int), ] assert sizeof(bn_mont_ctx_st) == 72, sizeof(bn_mont_ctx_st) assert alignment(bn_mont_ctx_st) == 4, alignment(bn_mont_ctx_st) BN_MONT_CTX = bn_mont_ctx_st class bn_recp_ctx_st(Structure): pass bn_recp_ctx_st._fields_ = [ ('N', BIGNUM), ('Nr', BIGNUM), ('num_bits', c_int), ('shift', c_int), ('flags', c_int), ] assert sizeof(bn_recp_ctx_st) == 52, sizeof(bn_recp_ctx_st) assert alignment(bn_recp_ctx_st) == 4, alignment(bn_recp_ctx_st) BN_RECP_CTX = bn_recp_ctx_st class buf_mem_st(Structure): pass buf_mem_st._fields_ = [ ('length', c_int), ('data', STRING), ('max', c_int), ] assert sizeof(buf_mem_st) == 12, sizeof(buf_mem_st) assert alignment(buf_mem_st) == 4, alignment(buf_mem_st) BUF_MEM = buf_mem_st class cast_key_st(Structure): pass cast_key_st._fields_ = [ ('data', c_ulong * 32), ('short_key', c_int), ] assert sizeof(cast_key_st) == 132, sizeof(cast_key_st) assert alignment(cast_key_st) == 4, alignment(cast_key_st) CAST_KEY = cast_key_st class comp_method_st(Structure): pass comp_method_st._fields_ = [ ('type', c_int), ('name', STRING), ('init', CFUNCTYPE(c_int)), ('finish', CFUNCTYPE(None)), ('compress', CFUNCTYPE(c_int)), ('expand', CFUNCTYPE(c_int)), ('ctrl', CFUNCTYPE(c_long)), ('callback_ctrl', CFUNCTYPE(c_long)), ] assert sizeof(comp_method_st) == 32, sizeof(comp_method_st) assert alignment(comp_method_st) == 4, alignment(comp_method_st) COMP_METHOD = comp_method_st class comp_ctx_st(Structure): pass comp_ctx_st._fields_ = [ ('meth', POINTER(COMP_METHOD)), ('compress_in', c_ulong), ('compress_out', c_ulong), ('expand_in', c_ulong), ('expand_out', c_ulong), ('ex_data', CRYPTO_EX_DATA), ] assert sizeof(comp_ctx_st) == 28, sizeof(comp_ctx_st) assert alignment(comp_ctx_st) == 4, alignment(comp_ctx_st) COMP_CTX = comp_ctx_st class CRYPTO_dynlock_value(Structure): pass CRYPTO_dynlock_value._fields_ = [ ] class CRYPTO_dynlock(Structure): pass CRYPTO_dynlock._fields_ = [ ('references', c_int), ('data', POINTER(CRYPTO_dynlock_value)), ] assert sizeof(CRYPTO_dynlock) == 8, sizeof(CRYPTO_dynlock) assert alignment(CRYPTO_dynlock) == 4, alignment(CRYPTO_dynlock) BIO_dummy = bio_st CRYPTO_EX_new = CFUNCTYPE(c_int, c_void_p, c_void_p, POINTER(CRYPTO_EX_DATA), c_int, c_long, c_void_p) CRYPTO_EX_free = CFUNCTYPE(None, c_void_p, c_void_p, POINTER(CRYPTO_EX_DATA), c_int, c_long, c_void_p) CRYPTO_EX_dup = CFUNCTYPE(c_int, POINTER(CRYPTO_EX_DATA), POINTER(CRYPTO_EX_DATA), c_void_p, c_int, c_long, c_void_p) class crypto_ex_data_func_st(Structure): pass crypto_ex_data_func_st._fields_ = [ ('argl', c_long), ('argp', c_void_p), ('new_func', POINTER(CRYPTO_EX_new)), ('free_func', POINTER(CRYPTO_EX_free)), ('dup_func', POINTER(CRYPTO_EX_dup)), ] assert sizeof(crypto_ex_data_func_st) == 20, sizeof(crypto_ex_data_func_st) assert alignment(crypto_ex_data_func_st) == 4, alignment(crypto_ex_data_func_st) CRYPTO_EX_DATA_FUNCS = crypto_ex_data_func_st class st_CRYPTO_EX_DATA_IMPL(Structure): pass CRYPTO_EX_DATA_IMPL = st_CRYPTO_EX_DATA_IMPL st_CRYPTO_EX_DATA_IMPL._fields_ = [ ] CRYPTO_MEM_LEAK_CB = CFUNCTYPE(c_void_p, c_ulong, STRING, c_int, c_int, c_void_p) DES_cblock = c_ubyte * 8 const_DES_cblock = c_ubyte * 8 class DES_ks(Structure): pass class N6DES_ks3DOLLAR_9E(Union): pass N6DES_ks3DOLLAR_9E._fields_ = [ ('cblock', DES_cblock), ('deslong', c_ulong * 2), ] assert sizeof(N6DES_ks3DOLLAR_9E) == 8, sizeof(N6DES_ks3DOLLAR_9E) assert alignment(N6DES_ks3DOLLAR_9E) == 4, alignment(N6DES_ks3DOLLAR_9E) DES_ks._fields_ = [ ('ks', N6DES_ks3DOLLAR_9E * 16), ] assert sizeof(DES_ks) == 128, sizeof(DES_ks) assert alignment(DES_ks) == 4, alignment(DES_ks) DES_key_schedule = DES_ks _ossl_old_des_cblock = c_ubyte * 8 class _ossl_old_des_ks_struct(Structure): pass class N23_ossl_old_des_ks_struct4DOLLAR_10E(Union): pass N23_ossl_old_des_ks_struct4DOLLAR_10E._fields_ = [ ('_', _ossl_old_des_cblock), ('pad', c_ulong * 2), ] assert sizeof(N23_ossl_old_des_ks_struct4DOLLAR_10E) == 8, sizeof(N23_ossl_old_des_ks_struct4DOLLAR_10E) assert alignment(N23_ossl_old_des_ks_struct4DOLLAR_10E) == 4, alignment(N23_ossl_old_des_ks_struct4DOLLAR_10E) _ossl_old_des_ks_struct._fields_ = [ ('ks', N23_ossl_old_des_ks_struct4DOLLAR_10E), ] assert sizeof(_ossl_old_des_ks_struct) == 8, sizeof(_ossl_old_des_ks_struct) assert alignment(_ossl_old_des_ks_struct) == 4, alignment(_ossl_old_des_ks_struct) _ossl_old_des_key_schedule = _ossl_old_des_ks_struct * 16 class dh_st(Structure): pass DH = dh_st class dh_method(Structure): pass dh_method._fields_ = [ ('name', STRING), ('generate_key', CFUNCTYPE(c_int, POINTER(DH))), ('compute_key', CFUNCTYPE(c_int, POINTER(c_ubyte), POINTER(BIGNUM), POINTER(DH))), ('bn_mod_exp', CFUNCTYPE(c_int, POINTER(DH), POINTER(BIGNUM), POINTER(BIGNUM), POINTER(BIGNUM), POINTER(BIGNUM), POINTER(BN_CTX), POINTER(BN_MONT_CTX))), ('init', CFUNCTYPE(c_int, POINTER(DH))), ('finish', CFUNCTYPE(c_int, POINTER(DH))), ('flags', c_int), ('app_data', STRING), ] assert sizeof(dh_method) == 32, sizeof(dh_method) assert alignment(dh_method) == 4, alignment(dh_method) DH_METHOD = dh_method class engine_st(Structure): pass ENGINE = engine_st dh_st._fields_ = [ ('pad', c_int), ('version', c_int), ('p', POINTER(BIGNUM)), ('g', POINTER(BIGNUM)), ('length', c_long), ('pub_key', POINTER(BIGNUM)), ('priv_key', POINTER(BIGNUM)), ('flags', c_int), ('method_mont_p', STRING), ('q', POINTER(BIGNUM)), ('j', POINTER(BIGNUM)), ('seed', POINTER(c_ubyte)), ('seedlen', c_int), ('counter', POINTER(BIGNUM)), ('references', c_int), ('ex_data', CRYPTO_EX_DATA), ('meth', POINTER(DH_METHOD)), ('engine', POINTER(ENGINE)), ] assert sizeof(dh_st) == 76, sizeof(dh_st) assert alignment(dh_st) == 4, alignment(dh_st) class dsa_st(Structure): pass DSA = dsa_st class DSA_SIG_st(Structure): pass DSA_SIG_st._fields_ = [ ('r', POINTER(BIGNUM)), ('s', POINTER(BIGNUM)), ] assert sizeof(DSA_SIG_st) == 8, sizeof(DSA_SIG_st) assert alignment(DSA_SIG_st) == 4, alignment(DSA_SIG_st) DSA_SIG = DSA_SIG_st class dsa_method(Structure): pass dsa_method._fields_ = [ ('name', STRING), ('dsa_do_sign', CFUNCTYPE(POINTER(DSA_SIG), POINTER(c_ubyte), c_int, POINTER(DSA))), ('dsa_sign_setup', CFUNCTYPE(c_int, POINTER(DSA), POINTER(BN_CTX), POINTER(POINTER(BIGNUM)), POINTER(POINTER(BIGNUM)))), ('dsa_do_verify', CFUNCTYPE(c_int, POINTER(c_ubyte), c_int, POINTER(DSA_SIG), POINTER(DSA))), ('dsa_mod_exp', CFUNCTYPE(c_int, POINTER(DSA), POINTER(BIGNUM), POINTER(BIGNUM), POINTER(BIGNUM), POINTER(BIGNUM), POINTER(BIGNUM), POINTER(BIGNUM), POINTER(BN_CTX), POINTER(BN_MONT_CTX))), ('bn_mod_exp', CFUNCTYPE(c_int, POINTER(DSA), POINTER(BIGNUM), POINTER(BIGNUM), POINTER(BIGNUM), POINTER(BIGNUM), POINTER(BN_CTX), POINTER(BN_MONT_CTX))), ('init', CFUNCTYPE(c_int, POINTER(DSA))), ('finish', CFUNCTYPE(c_int, POINTER(DSA))), ('flags', c_int), ('app_data', STRING), ] assert sizeof(dsa_method) == 40, sizeof(dsa_method) assert alignment(dsa_method) == 4, alignment(dsa_method) DSA_METHOD = dsa_method dsa_st._fields_ = [ ('pad', c_int), ('version', c_long), ('write_params', c_int), ('p', POINTER(BIGNUM)), ('q', POINTER(BIGNUM)), ('g', POINTER(BIGNUM)), ('pub_key', POINTER(BIGNUM)), ('priv_key', POINTER(BIGNUM)), ('kinv', POINTER(BIGNUM)), ('r', POINTER(BIGNUM)), ('flags', c_int), ('method_mont_p', STRING), ('references', c_int), ('ex_data', CRYPTO_EX_DATA), ('meth', POINTER(DSA_METHOD)), ('engine', POINTER(ENGINE)), ] assert sizeof(dsa_st) == 68, sizeof(dsa_st) assert alignment(dsa_st) == 4, alignment(dsa_st) class evp_pkey_st(Structure): pass class N11evp_pkey_st4DOLLAR_12E(Union): pass class rsa_st(Structure): pass N11evp_pkey_st4DOLLAR_12E._fields_ = [ ('ptr', STRING), ('rsa', POINTER(rsa_st)), ('dsa', POINTER(dsa_st)), ('dh', POINTER(dh_st)), ] assert sizeof(N11evp_pkey_st4DOLLAR_12E) == 4, sizeof(N11evp_pkey_st4DOLLAR_12E) assert alignment(N11evp_pkey_st4DOLLAR_12E) == 4, alignment(N11evp_pkey_st4DOLLAR_12E) evp_pkey_st._fields_ = [ ('type', c_int), ('save_type', c_int), ('references', c_int), ('pkey', N11evp_pkey_st4DOLLAR_12E), ('save_parameters', c_int), ('attributes', POINTER(STACK)), ] assert sizeof(evp_pkey_st) == 24, sizeof(evp_pkey_st) assert alignment(evp_pkey_st) == 4, alignment(evp_pkey_st) class env_md_st(Structure): pass class env_md_ctx_st(Structure): pass EVP_MD_CTX = env_md_ctx_st env_md_st._fields_ = [ ('type', c_int), ('pkey_type', c_int), ('md_size', c_int), ('flags', c_ulong), ('init', CFUNCTYPE(c_int, POINTER(EVP_MD_CTX))), ('update', CFUNCTYPE(c_int, POINTER(EVP_MD_CTX), c_void_p, c_ulong)), ('final', CFUNCTYPE(c_int, POINTER(EVP_MD_CTX), POINTER(c_ubyte))), ('copy', CFUNCTYPE(c_int, POINTER(EVP_MD_CTX), POINTER(EVP_MD_CTX))), ('cleanup', CFUNCTYPE(c_int, POINTER(EVP_MD_CTX))), ('sign', CFUNCTYPE(c_int)), ('verify', CFUNCTYPE(c_int)), ('required_pkey_type', c_int * 5), ('block_size', c_int), ('ctx_size', c_int), ] assert sizeof(env_md_st) == 72, sizeof(env_md_st) assert alignment(env_md_st) == 4, alignment(env_md_st) EVP_MD = env_md_st env_md_ctx_st._fields_ = [ ('digest', POINTER(EVP_MD)), ('engine', POINTER(ENGINE)), ('flags', c_ulong), ('md_data', c_void_p), ] assert sizeof(env_md_ctx_st) == 16, sizeof(env_md_ctx_st) assert alignment(env_md_ctx_st) == 4, alignment(env_md_ctx_st) class evp_cipher_st(Structure): pass class evp_cipher_ctx_st(Structure): pass EVP_CIPHER_CTX = evp_cipher_ctx_st evp_cipher_st._fields_ = [ ('nid', c_int), ('block_size', c_int), ('key_len', c_int), ('iv_len', c_int), ('flags', c_ulong), ('init', CFUNCTYPE(c_int, POINTER(EVP_CIPHER_CTX), POINTER(c_ubyte), POINTER(c_ubyte), c_int)), ('do_cipher', CFUNCTYPE(c_int, POINTER(EVP_CIPHER_CTX), POINTER(c_ubyte), POINTER(c_ubyte), c_uint)), ('cleanup', CFUNCTYPE(c_int, POINTER(EVP_CIPHER_CTX))), ('ctx_size', c_int), ('set_asn1_parameters', CFUNCTYPE(c_int, POINTER(EVP_CIPHER_CTX), POINTER(ASN1_TYPE))), ('get_asn1_parameters', CFUNCTYPE(c_int, POINTER(EVP_CIPHER_CTX), POINTER(ASN1_TYPE))), ('ctrl', CFUNCTYPE(c_int, POINTER(EVP_CIPHER_CTX), c_int, c_int, c_void_p)), ('app_data', c_void_p), ] assert sizeof(evp_cipher_st) == 52, sizeof(evp_cipher_st) assert alignment(evp_cipher_st) == 4, alignment(evp_cipher_st) class evp_cipher_info_st(Structure): pass EVP_CIPHER = evp_cipher_st evp_cipher_info_st._fields_ = [ ('cipher', POINTER(EVP_CIPHER)), ('iv', c_ubyte * 16), ] assert sizeof(evp_cipher_info_st) == 20, sizeof(evp_cipher_info_st) assert alignment(evp_cipher_info_st) == 4, alignment(evp_cipher_info_st) EVP_CIPHER_INFO = evp_cipher_info_st evp_cipher_ctx_st._fields_ = [ ('cipher', POINTER(EVP_CIPHER)), ('engine', POINTER(ENGINE)), ('encrypt', c_int), ('buf_len', c_int), ('oiv', c_ubyte * 16), ('iv', c_ubyte * 16), ('buf', c_ubyte * 32), ('num', c_int), ('app_data', c_void_p), ('key_len', c_int), ('flags', c_ulong), ('cipher_data', c_void_p), ('final_used', c_int), ('block_mask', c_int), ('final', c_ubyte * 32), ] assert sizeof(evp_cipher_ctx_st) == 140, sizeof(evp_cipher_ctx_st) assert alignment(evp_cipher_ctx_st) == 4, alignment(evp_cipher_ctx_st) class evp_Encode_Ctx_st(Structure): pass evp_Encode_Ctx_st._fields_ = [ ('num', c_int), ('length', c_int), ('enc_data', c_ubyte * 80), ('line_num', c_int), ('expect_nl', c_int), ] assert sizeof(evp_Encode_Ctx_st) == 96, sizeof(evp_Encode_Ctx_st) assert alignment(evp_Encode_Ctx_st) == 4, alignment(evp_Encode_Ctx_st) EVP_ENCODE_CTX = evp_Encode_Ctx_st EVP_PBE_KEYGEN = CFUNCTYPE(c_int, POINTER(EVP_CIPHER_CTX), STRING, c_int, POINTER(ASN1_TYPE), POINTER(EVP_CIPHER), POINTER(EVP_MD), c_int) class lhash_node_st(Structure): pass lhash_node_st._fields_ = [ ('data', c_void_p), ('next', POINTER(lhash_node_st)), ('hash', c_ulong), ] assert sizeof(lhash_node_st) == 12, sizeof(lhash_node_st) assert alignment(lhash_node_st) == 4, alignment(lhash_node_st) LHASH_NODE = lhash_node_st LHASH_COMP_FN_TYPE = CFUNCTYPE(c_int, c_void_p, c_void_p) LHASH_HASH_FN_TYPE = CFUNCTYPE(c_ulong, c_void_p) LHASH_DOALL_FN_TYPE = CFUNCTYPE(None, c_void_p) LHASH_DOALL_ARG_FN_TYPE = CFUNCTYPE(None, c_void_p, c_void_p) class lhash_st(Structure): pass lhash_st._fields_ = [ ('b', POINTER(POINTER(LHASH_NODE))), ('comp', LHASH_COMP_FN_TYPE), ('hash', LHASH_HASH_FN_TYPE), ('num_nodes', c_uint), ('num_alloc_nodes', c_uint), ('p', c_uint), ('pmax', c_uint), ('up_load', c_ulong), ('down_load', c_ulong), ('num_items', c_ulong), ('num_expands', c_ulong), ('num_expand_reallocs', c_ulong), ('num_contracts', c_ulong), ('num_contract_reallocs', c_ulong), ('num_hash_calls', c_ulong), ('num_comp_calls', c_ulong), ('num_insert', c_ulong), ('num_replace', c_ulong), ('num_delete', c_ulong), ('num_no_delete', c_ulong), ('num_retrieve', c_ulong), ('num_retrieve_miss', c_ulong), ('num_hash_comps', c_ulong), ('error', c_int), ] assert sizeof(lhash_st) == 96, sizeof(lhash_st) assert alignment(lhash_st) == 4, alignment(lhash_st) LHASH = lhash_st class MD2state_st(Structure): pass MD2state_st._fields_ = [ ('num', c_int), ('data', c_ubyte * 16), ('cksm', c_uint * 16), ('state', c_uint * 16), ] assert sizeof(MD2state_st) == 148, sizeof(MD2state_st) assert alignment(MD2state_st) == 4, alignment(MD2state_st) MD2_CTX = MD2state_st class MD4state_st(Structure): pass MD4state_st._fields_ = [ ('A', c_uint), ('B', c_uint), ('C', c_uint), ('D', c_uint), ('Nl', c_uint), ('Nh', c_uint), ('data', c_uint * 16), ('num', c_int), ] assert sizeof(MD4state_st) == 92, sizeof(MD4state_st) assert alignment(MD4state_st) == 4, alignment(MD4state_st) MD4_CTX = MD4state_st class MD5state_st(Structure): pass MD5state_st._fields_ = [ ('A', c_uint), ('B', c_uint), ('C', c_uint), ('D', c_uint), ('Nl', c_uint), ('Nh', c_uint), ('data', c_uint * 16), ('num', c_int), ] assert sizeof(MD5state_st) == 92, sizeof(MD5state_st) assert alignment(MD5state_st) == 4, alignment(MD5state_st) MD5_CTX = MD5state_st class mdc2_ctx_st(Structure): pass mdc2_ctx_st._fields_ = [ ('num', c_int), ('data', c_ubyte * 8), ('h', DES_cblock), ('hh', DES_cblock), ('pad_type', c_int), ] assert sizeof(mdc2_ctx_st) == 32, sizeof(mdc2_ctx_st) assert alignment(mdc2_ctx_st) == 4, alignment(mdc2_ctx_st) MDC2_CTX = mdc2_ctx_st class obj_name_st(Structure): pass obj_name_st._fields_ = [ ('type', c_int), ('alias', c_int), ('name', STRING), ('data', STRING), ] assert sizeof(obj_name_st) == 16, sizeof(obj_name_st) assert alignment(obj_name_st) == 4, alignment(obj_name_st) OBJ_NAME = obj_name_st ASN1_TIME = asn1_string_st ASN1_NULL = c_int EVP_PKEY = evp_pkey_st class x509_st(Structure): pass X509 = x509_st class X509_algor_st(Structure): pass X509_ALGOR = X509_algor_st class X509_crl_st(Structure): pass X509_CRL = X509_crl_st class X509_name_st(Structure): pass X509_NAME = X509_name_st class x509_store_st(Structure): pass X509_STORE = x509_store_st class x509_store_ctx_st(Structure): pass X509_STORE_CTX = x509_store_ctx_st engine_st._fields_ = [ ] class PEM_Encode_Seal_st(Structure): pass PEM_Encode_Seal_st._fields_ = [ ('encode', EVP_ENCODE_CTX), ('md', EVP_MD_CTX), ('cipher', EVP_CIPHER_CTX), ] assert sizeof(PEM_Encode_Seal_st) == 252, sizeof(PEM_Encode_Seal_st) assert alignment(PEM_Encode_Seal_st) == 4, alignment(PEM_Encode_Seal_st) PEM_ENCODE_SEAL_CTX = PEM_Encode_Seal_st class pem_recip_st(Structure): pass pem_recip_st._fields_ = [ ('name', STRING), ('dn', POINTER(X509_NAME)), ('cipher', c_int), ('key_enc', c_int), ] assert sizeof(pem_recip_st) == 16, sizeof(pem_recip_st) assert alignment(pem_recip_st) == 4, alignment(pem_recip_st) PEM_USER = pem_recip_st class pem_ctx_st(Structure): pass class N10pem_ctx_st4DOLLAR_16E(Structure): pass N10pem_ctx_st4DOLLAR_16E._fields_ = [ ('version', c_int), ('mode', c_int), ] assert sizeof(N10pem_ctx_st4DOLLAR_16E) == 8, sizeof(N10pem_ctx_st4DOLLAR_16E) assert alignment(N10pem_ctx_st4DOLLAR_16E) == 4, alignment(N10pem_ctx_st4DOLLAR_16E) class N10pem_ctx_st4DOLLAR_17E(Structure): pass N10pem_ctx_st4DOLLAR_17E._fields_ = [ ('cipher', c_int), ] assert sizeof(N10pem_ctx_st4DOLLAR_17E) == 4, sizeof(N10pem_ctx_st4DOLLAR_17E) assert alignment(N10pem_ctx_st4DOLLAR_17E) == 4, alignment(N10pem_ctx_st4DOLLAR_17E) pem_ctx_st._fields_ = [ ('type', c_int), ('proc_type', N10pem_ctx_st4DOLLAR_16E), ('domain', STRING), ('DEK_info', N10pem_ctx_st4DOLLAR_17E), ('originator', POINTER(PEM_USER)), ('num_recipient', c_int), ('recipient', POINTER(POINTER(PEM_USER))), ('x509_chain', POINTER(STACK)), ('md', POINTER(EVP_MD)), ('md_enc', c_int), ('md_len', c_int), ('md_data', STRING), ('dec', POINTER(EVP_CIPHER)), ('key_len', c_int), ('key', POINTER(c_ubyte)), ('data_enc', c_int), ('data_len', c_int), ('data', POINTER(c_ubyte)), ] assert sizeof(pem_ctx_st) == 76, sizeof(pem_ctx_st) assert alignment(pem_ctx_st) == 4, alignment(pem_ctx_st) PEM_CTX = pem_ctx_st pem_password_cb = CFUNCTYPE(c_int, STRING, c_int, c_int, c_void_p) class pkcs7_issuer_and_serial_st(Structure): pass pkcs7_issuer_and_serial_st._fields_ = [ ('issuer', POINTER(X509_NAME)), ('serial', POINTER(ASN1_INTEGER)), ] assert sizeof(pkcs7_issuer_and_serial_st) == 8, sizeof(pkcs7_issuer_and_serial_st) assert alignment(pkcs7_issuer_and_serial_st) == 4, alignment(pkcs7_issuer_and_serial_st) PKCS7_ISSUER_AND_SERIAL = pkcs7_issuer_and_serial_st class pkcs7_signer_info_st(Structure): pass pkcs7_signer_info_st._fields_ = [ ('version', POINTER(ASN1_INTEGER)), ('issuer_and_serial', POINTER(PKCS7_ISSUER_AND_SERIAL)), ('digest_alg', POINTER(X509_ALGOR)), ('auth_attr', POINTER(STACK)), ('digest_enc_alg', POINTER(X509_ALGOR)), ('enc_digest', POINTER(ASN1_OCTET_STRING)), ('unauth_attr', POINTER(STACK)), ('pkey', POINTER(EVP_PKEY)), ] assert sizeof(pkcs7_signer_info_st) == 32, sizeof(pkcs7_signer_info_st) assert alignment(pkcs7_signer_info_st) == 4, alignment(pkcs7_signer_info_st) PKCS7_SIGNER_INFO = pkcs7_signer_info_st class pkcs7_recip_info_st(Structure): pass pkcs7_recip_info_st._fields_ = [ ('version', POINTER(ASN1_INTEGER)), ('issuer_and_serial', POINTER(PKCS7_ISSUER_AND_SERIAL)), ('key_enc_algor', POINTER(X509_ALGOR)), ('enc_key', POINTER(ASN1_OCTET_STRING)), ('cert', POINTER(X509)), ] assert sizeof(pkcs7_recip_info_st) == 20, sizeof(pkcs7_recip_info_st) assert alignment(pkcs7_recip_info_st) == 4, alignment(pkcs7_recip_info_st) PKCS7_RECIP_INFO = pkcs7_recip_info_st class pkcs7_signed_st(Structure): pass class pkcs7_st(Structure): pass pkcs7_signed_st._fields_ = [ ('version', POINTER(ASN1_INTEGER)), ('md_algs', POINTER(STACK)), ('cert', POINTER(STACK)), ('crl', POINTER(STACK)), ('signer_info', POINTER(STACK)), ('contents', POINTER(pkcs7_st)), ] assert sizeof(pkcs7_signed_st) == 24, sizeof(pkcs7_signed_st) assert alignment(pkcs7_signed_st) == 4, alignment(pkcs7_signed_st) PKCS7_SIGNED = pkcs7_signed_st class pkcs7_enc_content_st(Structure): pass pkcs7_enc_content_st._fields_ = [ ('content_type', POINTER(ASN1_OBJECT)), ('algorithm', POINTER(X509_ALGOR)), ('enc_data', POINTER(ASN1_OCTET_STRING)), ('cipher', POINTER(EVP_CIPHER)), ] assert sizeof(pkcs7_enc_content_st) == 16, sizeof(pkcs7_enc_content_st) assert alignment(pkcs7_enc_content_st) == 4, alignment(pkcs7_enc_content_st) PKCS7_ENC_CONTENT = pkcs7_enc_content_st class pkcs7_enveloped_st(Structure): pass pkcs7_enveloped_st._fields_ = [ ('version', POINTER(ASN1_INTEGER)), ('recipientinfo', POINTER(STACK)), ('enc_data', POINTER(PKCS7_ENC_CONTENT)), ] assert sizeof(pkcs7_enveloped_st) == 12, sizeof(pkcs7_enveloped_st) assert alignment(pkcs7_enveloped_st) == 4, alignment(pkcs7_enveloped_st) PKCS7_ENVELOPE = pkcs7_enveloped_st class pkcs7_signedandenveloped_st(Structure): pass pkcs7_signedandenveloped_st._fields_ = [ ('version', POINTER(ASN1_INTEGER)), ('md_algs', POINTER(STACK)), ('cert', POINTER(STACK)), ('crl', POINTER(STACK)), ('signer_info', POINTER(STACK)), ('enc_data', POINTER(PKCS7_ENC_CONTENT)), ('recipientinfo', POINTER(STACK)), ] assert sizeof(pkcs7_signedandenveloped_st) == 28, sizeof(pkcs7_signedandenveloped_st) assert alignment(pkcs7_signedandenveloped_st) == 4, alignment(pkcs7_signedandenveloped_st) PKCS7_SIGN_ENVELOPE = pkcs7_signedandenveloped_st class pkcs7_digest_st(Structure): pass pkcs7_digest_st._fields_ = [ ('version', POINTER(ASN1_INTEGER)), ('md', POINTER(X509_ALGOR)), ('contents', POINTER(pkcs7_st)), ('digest', POINTER(ASN1_OCTET_STRING)), ] assert sizeof(pkcs7_digest_st) == 16, sizeof(pkcs7_digest_st) assert alignment(pkcs7_digest_st) == 4, alignment(pkcs7_digest_st) PKCS7_DIGEST = pkcs7_digest_st class pkcs7_encrypted_st(Structure): pass pkcs7_encrypted_st._fields_ = [ ('version', POINTER(ASN1_INTEGER)), ('enc_data', POINTER(PKCS7_ENC_CONTENT)), ] assert sizeof(pkcs7_encrypted_st) == 8, sizeof(pkcs7_encrypted_st) assert alignment(pkcs7_encrypted_st) == 4, alignment(pkcs7_encrypted_st) PKCS7_ENCRYPT = pkcs7_encrypted_st class N8pkcs7_st4DOLLAR_15E(Union): pass N8pkcs7_st4DOLLAR_15E._fields_ = [ ('ptr', STRING), ('data', POINTER(ASN1_OCTET_STRING)), ('sign', POINTER(PKCS7_SIGNED)), ('enveloped', POINTER(PKCS7_ENVELOPE)), ('signed_and_enveloped', POINTER(PKCS7_SIGN_ENVELOPE)), ('digest', POINTER(PKCS7_DIGEST)), ('encrypted', POINTER(PKCS7_ENCRYPT)), ('other', POINTER(ASN1_TYPE)), ] assert sizeof(N8pkcs7_st4DOLLAR_15E) == 4, sizeof(N8pkcs7_st4DOLLAR_15E) assert alignment(N8pkcs7_st4DOLLAR_15E) == 4, alignment(N8pkcs7_st4DOLLAR_15E) pkcs7_st._fields_ = [ ('asn1', POINTER(c_ubyte)), ('length', c_long), ('state', c_int), ('detached', c_int), ('type', POINTER(ASN1_OBJECT)), ('d', N8pkcs7_st4DOLLAR_15E), ] assert sizeof(pkcs7_st) == 24, sizeof(pkcs7_st) assert alignment(pkcs7_st) == 4, alignment(pkcs7_st) PKCS7 = pkcs7_st class rc2_key_st(Structure): pass rc2_key_st._fields_ = [ ('data', c_uint * 64), ] assert sizeof(rc2_key_st) == 256, sizeof(rc2_key_st) assert alignment(rc2_key_st) == 4, alignment(rc2_key_st) RC2_KEY = rc2_key_st class rc4_key_st(Structure): pass rc4_key_st._fields_ = [ ('x', c_ubyte), ('y', c_ubyte), ('data', c_ubyte * 256), ] assert sizeof(rc4_key_st) == 258, sizeof(rc4_key_st) assert alignment(rc4_key_st) == 1, alignment(rc4_key_st) RC4_KEY = rc4_key_st class rc5_key_st(Structure): pass rc5_key_st._fields_ = [ ('rounds', c_int), ('data', c_ulong * 34), ] assert sizeof(rc5_key_st) == 140, sizeof(rc5_key_st) assert alignment(rc5_key_st) == 4, alignment(rc5_key_st) RC5_32_KEY = rc5_key_st class RIPEMD160state_st(Structure): pass RIPEMD160state_st._fields_ = [ ('A', c_uint), ('B', c_uint), ('C', c_uint), ('D', c_uint), ('E', c_uint), ('Nl', c_uint), ('Nh', c_uint), ('data', c_uint * 16), ('num', c_int), ] assert sizeof(RIPEMD160state_st) == 96, sizeof(RIPEMD160state_st) assert alignment(RIPEMD160state_st) == 4, alignment(RIPEMD160state_st) RIPEMD160_CTX = RIPEMD160state_st RSA = rsa_st class rsa_meth_st(Structure): pass rsa_meth_st._fields_ = [ ('name', STRING), ('rsa_pub_enc', CFUNCTYPE(c_int, c_int, POINTER(c_ubyte), POINTER(c_ubyte), POINTER(RSA), c_int)), ('rsa_pub_dec', CFUNCTYPE(c_int, c_int, POINTER(c_ubyte), POINTER(c_ubyte), POINTER(RSA), c_int)), ('rsa_priv_enc', CFUNCTYPE(c_int, c_int, POINTER(c_ubyte), POINTER(c_ubyte), POINTER(RSA), c_int)), ('rsa_priv_dec', CFUNCTYPE(c_int, c_int, POINTER(c_ubyte), POINTER(c_ubyte), POINTER(RSA), c_int)), ('rsa_mod_exp', CFUNCTYPE(c_int, POINTER(BIGNUM), POINTER(BIGNUM), POINTER(RSA))), ('bn_mod_exp', CFUNCTYPE(c_int, POINTER(BIGNUM), POINTER(BIGNUM), POINTER(BIGNUM), POINTER(BIGNUM), POINTER(BN_CTX), POINTER(BN_MONT_CTX))), ('init', CFUNCTYPE(c_int, POINTER(RSA))), ('finish', CFUNCTYPE(c_int, POINTER(RSA))), ('flags', c_int), ('app_data', STRING), ('rsa_sign', CFUNCTYPE(c_int, c_int, POINTER(c_ubyte), c_uint, POINTER(c_ubyte), POINTER(c_uint), POINTER(RSA))), ('rsa_verify', CFUNCTYPE(c_int, c_int, POINTER(c_ubyte), c_uint, POINTER(c_ubyte), c_uint, POINTER(RSA))), ] assert sizeof(rsa_meth_st) == 52, sizeof(rsa_meth_st) assert alignment(rsa_meth_st) == 4, alignment(rsa_meth_st) RSA_METHOD = rsa_meth_st rsa_st._fields_ = [ ('pad', c_int), ('version', c_long), ('meth', POINTER(RSA_METHOD)), ('engine', POINTER(ENGINE)), ('n', POINTER(BIGNUM)), ('e', POINTER(BIGNUM)), ('d', POINTER(BIGNUM)), ('p', POINTER(BIGNUM)), ('q', POINTER(BIGNUM)), ('dmp1', POINTER(BIGNUM)), ('dmq1', POINTER(BIGNUM)), ('iqmp', POINTER(BIGNUM)), ('ex_data', CRYPTO_EX_DATA), ('references', c_int), ('flags', c_int), ('_method_mod_n', POINTER(BN_MONT_CTX)), ('_method_mod_p', POINTER(BN_MONT_CTX)), ('_method_mod_q', POINTER(BN_MONT_CTX)), ('bignum_data', STRING), ('blinding', POINTER(BN_BLINDING)), ] assert sizeof(rsa_st) == 84, sizeof(rsa_st) assert alignment(rsa_st) == 4, alignment(rsa_st) openssl_fptr = CFUNCTYPE(None) class SHAstate_st(Structure): pass SHAstate_st._fields_ = [ ('h0', c_uint), ('h1', c_uint), ('h2', c_uint), ('h3', c_uint), ('h4', c_uint), ('Nl', c_uint), ('Nh', c_uint), ('data', c_uint * 16), ('num', c_int), ] assert sizeof(SHAstate_st) == 96, sizeof(SHAstate_st) assert alignment(SHAstate_st) == 4, alignment(SHAstate_st) SHA_CTX = SHAstate_st class ssl_st(Structure): pass ssl_crock_st = POINTER(ssl_st) class ssl_cipher_st(Structure): pass ssl_cipher_st._fields_ = [ ('valid', c_int), ('name', STRING), ('id', c_ulong), ('algorithms', c_ulong), ('algo_strength', c_ulong), ('algorithm2', c_ulong), ('strength_bits', c_int), ('alg_bits', c_int), ('mask', c_ulong), ('mask_strength', c_ulong), ] assert sizeof(ssl_cipher_st) == 40, sizeof(ssl_cipher_st) assert alignment(ssl_cipher_st) == 4, alignment(ssl_cipher_st) SSL_CIPHER = ssl_cipher_st SSL = ssl_st class ssl_ctx_st(Structure): pass SSL_CTX = ssl_ctx_st class ssl_method_st(Structure): pass class ssl3_enc_method(Structure): pass ssl_method_st._fields_ = [ ('version', c_int), ('ssl_new', CFUNCTYPE(c_int, POINTER(SSL))), ('ssl_clear', CFUNCTYPE(None, POINTER(SSL))), ('ssl_free', CFUNCTYPE(None, POINTER(SSL))), ('ssl_accept', CFUNCTYPE(c_int, POINTER(SSL))), ('ssl_connect', CFUNCTYPE(c_int, POINTER(SSL))), ('ssl_read', CFUNCTYPE(c_int, POINTER(SSL), c_void_p, c_int)), ('ssl_peek', CFUNCTYPE(c_int, POINTER(SSL), c_void_p, c_int)), ('ssl_write', CFUNCTYPE(c_int, POINTER(SSL), c_void_p, c_int)), ('ssl_shutdown', CFUNCTYPE(c_int, POINTER(SSL))), ('ssl_renegotiate', CFUNCTYPE(c_int, POINTER(SSL))), ('ssl_renegotiate_check', CFUNCTYPE(c_int, POINTER(SSL))), ('ssl_ctrl', CFUNCTYPE(c_long, POINTER(SSL), c_int, c_long, c_void_p)), ('ssl_ctx_ctrl', CFUNCTYPE(c_long, POINTER(SSL_CTX), c_int, c_long, c_void_p)), ('get_cipher_by_char', CFUNCTYPE(POINTER(SSL_CIPHER), POINTER(c_ubyte))), ('put_cipher_by_char', CFUNCTYPE(c_int, POINTER(SSL_CIPHER), POINTER(c_ubyte))), ('ssl_pending', CFUNCTYPE(c_int, POINTER(SSL))), ('num_ciphers', CFUNCTYPE(c_int)), ('get_cipher', CFUNCTYPE(POINTER(SSL_CIPHER), c_uint)), ('get_ssl_method', CFUNCTYPE(POINTER(ssl_method_st), c_int)), ('get_timeout', CFUNCTYPE(c_long)), ('ssl3_enc', POINTER(ssl3_enc_method)), ('ssl_version', CFUNCTYPE(c_int)), ('ssl_callback_ctrl', CFUNCTYPE(c_long, POINTER(SSL), c_int, CFUNCTYPE(None))), ('ssl_ctx_callback_ctrl', CFUNCTYPE(c_long, POINTER(SSL_CTX), c_int, CFUNCTYPE(None))), ] assert sizeof(ssl_method_st) == 100, sizeof(ssl_method_st) assert alignment(ssl_method_st) == 4, alignment(ssl_method_st) ssl3_enc_method._fields_ = [ ] SSL_METHOD = ssl_method_st class ssl_session_st(Structure): pass class sess_cert_st(Structure): pass ssl_session_st._fields_ = [ ('ssl_version', c_int), ('key_arg_length', c_uint), ('key_arg', c_ubyte * 8), ('master_key_length', c_int), ('master_key', c_ubyte * 48), ('session_id_length', c_uint), ('session_id', c_ubyte * 32), ('sid_ctx_length', c_uint), ('sid_ctx', c_ubyte * 32), ('not_resumable', c_int), ('sess_cert', POINTER(sess_cert_st)), ('peer', POINTER(X509)), ('verify_result', c_long), ('references', c_int), ('timeout', c_long), ('time', c_long), ('compress_meth', c_int), ('cipher', POINTER(SSL_CIPHER)), ('cipher_id', c_ulong), ('ciphers', POINTER(STACK)), ('ex_data', CRYPTO_EX_DATA), ('prev', POINTER(ssl_session_st)), ('next', POINTER(ssl_session_st)), ] assert sizeof(ssl_session_st) == 200, sizeof(ssl_session_st) assert alignment(ssl_session_st) == 4, alignment(ssl_session_st) sess_cert_st._fields_ = [ ] SSL_SESSION = ssl_session_st GEN_SESSION_CB = CFUNCTYPE(c_int, POINTER(SSL), POINTER(c_ubyte), POINTER(c_uint)) class ssl_comp_st(Structure): pass ssl_comp_st._fields_ = [ ('id', c_int), ('name', STRING), ('method', POINTER(COMP_METHOD)), ] assert sizeof(ssl_comp_st) == 12, sizeof(ssl_comp_st) assert alignment(ssl_comp_st) == 4, alignment(ssl_comp_st) SSL_COMP = ssl_comp_st class N10ssl_ctx_st4DOLLAR_18E(Structure): pass N10ssl_ctx_st4DOLLAR_18E._fields_ = [ ('sess_connect', c_int), ('sess_connect_renegotiate', c_int), ('sess_connect_good', c_int), ('sess_accept', c_int), ('sess_accept_renegotiate', c_int), ('sess_accept_good', c_int), ('sess_miss', c_int), ('sess_timeout', c_int), ('sess_cache_full', c_int), ('sess_hit', c_int), ('sess_cb_hit', c_int), ] assert sizeof(N10ssl_ctx_st4DOLLAR_18E) == 44, sizeof(N10ssl_ctx_st4DOLLAR_18E) assert alignment(N10ssl_ctx_st4DOLLAR_18E) == 4, alignment(N10ssl_ctx_st4DOLLAR_18E) class cert_st(Structure): pass ssl_ctx_st._fields_ = [ ('method', POINTER(SSL_METHOD)), ('cipher_list', POINTER(STACK)), ('cipher_list_by_id', POINTER(STACK)), ('cert_store', POINTER(x509_store_st)), ('sessions', POINTER(lhash_st)), ('session_cache_size', c_ulong), ('session_cache_head', POINTER(ssl_session_st)), ('session_cache_tail', POINTER(ssl_session_st)), ('session_cache_mode', c_int), ('session_timeout', c_long), ('new_session_cb', CFUNCTYPE(c_int, POINTER(ssl_st), POINTER(SSL_SESSION))), ('remove_session_cb', CFUNCTYPE(None, POINTER(ssl_ctx_st), POINTER(SSL_SESSION))), ('get_session_cb', CFUNCTYPE(POINTER(SSL_SESSION), POINTER(ssl_st), POINTER(c_ubyte), c_int, POINTER(c_int))), ('stats', N10ssl_ctx_st4DOLLAR_18E), ('references', c_int), ('app_verify_callback', CFUNCTYPE(c_int, POINTER(X509_STORE_CTX), c_void_p)), ('app_verify_arg', c_void_p), ('default_passwd_callback', POINTER(pem_password_cb)), ('default_passwd_callback_userdata', c_void_p), ('client_cert_cb', CFUNCTYPE(c_int, POINTER(SSL), POINTER(POINTER(X509)), POINTER(POINTER(EVP_PKEY)))), ('ex_data', CRYPTO_EX_DATA), ('rsa_md5', POINTER(EVP_MD)), ('md5', POINTER(EVP_MD)), ('sha1', POINTER(EVP_MD)), ('extra_certs', POINTER(STACK)), ('comp_methods', POINTER(STACK)), ('info_callback', CFUNCTYPE(None, POINTER(SSL), c_int, c_int)), ('client_CA', POINTER(STACK)), ('options', c_ulong), ('mode', c_ulong), ('max_cert_list', c_long), ('cert', POINTER(cert_st)), ('read_ahead', c_int), ('msg_callback', CFUNCTYPE(None, c_int, c_int, c_int, c_void_p, c_ulong, POINTER(SSL), c_void_p)), ('msg_callback_arg', c_void_p), ('verify_mode', c_int), ('verify_depth', c_int), ('sid_ctx_length', c_uint), ('sid_ctx', c_ubyte * 32), ('default_verify_callback', CFUNCTYPE(c_int, c_int, POINTER(X509_STORE_CTX))), ('generate_session_id', GEN_SESSION_CB), ('purpose', c_int), ('trust', c_int), ('quiet_shutdown', c_int), ] assert sizeof(ssl_ctx_st) == 248, sizeof(ssl_ctx_st) assert alignment(ssl_ctx_st) == 4, alignment(ssl_ctx_st) cert_st._fields_ = [ ] class ssl2_state_st(Structure): pass class ssl3_state_st(Structure): pass ssl_st._fields_ = [ ('version', c_int), ('type', c_int), ('method', POINTER(SSL_METHOD)), ('rbio', POINTER(BIO)), ('wbio', POINTER(BIO)), ('bbio', POINTER(BIO)), ('rwstate', c_int), ('in_handshake', c_int), ('handshake_func', CFUNCTYPE(c_int)), ('server', c_int), ('new_session', c_int), ('quiet_shutdown', c_int), ('shutdown', c_int), ('state', c_int), ('rstate', c_int), ('init_buf', POINTER(BUF_MEM)), ('init_msg', c_void_p), ('init_num', c_int), ('init_off', c_int), ('packet', POINTER(c_ubyte)), ('packet_length', c_uint), ('s2', POINTER(ssl2_state_st)), ('s3', POINTER(ssl3_state_st)), ('read_ahead', c_int), ('msg_callback', CFUNCTYPE(None, c_int, c_int, c_int, c_void_p, c_ulong, POINTER(SSL), c_void_p)), ('msg_callback_arg', c_void_p), ('hit', c_int), ('purpose', c_int), ('trust', c_int), ('cipher_list', POINTER(STACK)), ('cipher_list_by_id', POINTER(STACK)), ('enc_read_ctx', POINTER(EVP_CIPHER_CTX)), ('read_hash', POINTER(EVP_MD)), ('expand', POINTER(COMP_CTX)), ('enc_write_ctx', POINTER(EVP_CIPHER_CTX)), ('write_hash', POINTER(EVP_MD)), ('compress', POINTER(COMP_CTX)), ('cert', POINTER(cert_st)), ('sid_ctx_length', c_uint), ('sid_ctx', c_ubyte * 32), ('session', POINTER(SSL_SESSION)), ('generate_session_id', GEN_SESSION_CB), ('verify_mode', c_int), ('verify_depth', c_int), ('verify_callback', CFUNCTYPE(c_int, c_int, POINTER(X509_STORE_CTX))), ('info_callback', CFUNCTYPE(None, POINTER(SSL), c_int, c_int)), ('error', c_int), ('error_code', c_int), ('ctx', POINTER(SSL_CTX)), ('debug', c_int), ('verify_result', c_long), ('ex_data', CRYPTO_EX_DATA), ('client_CA', POINTER(STACK)), ('references', c_int), ('options', c_ulong), ('mode', c_ulong), ('max_cert_list', c_long), ('first_packet', c_int), ('client_version', c_int), ] assert sizeof(ssl_st) == 268, sizeof(ssl_st) assert alignment(ssl_st) == 4, alignment(ssl_st) class N13ssl2_state_st4DOLLAR_19E(Structure): pass N13ssl2_state_st4DOLLAR_19E._fields_ = [ ('conn_id_length', c_uint), ('cert_type', c_uint), ('cert_length', c_uint), ('csl', c_uint), ('clear', c_uint), ('enc', c_uint), ('ccl', c_ubyte * 32), ('cipher_spec_length', c_uint), ('session_id_length', c_uint), ('clen', c_uint), ('rlen', c_uint), ] assert sizeof(N13ssl2_state_st4DOLLAR_19E) == 72, sizeof(N13ssl2_state_st4DOLLAR_19E) assert alignment(N13ssl2_state_st4DOLLAR_19E) == 4, alignment(N13ssl2_state_st4DOLLAR_19E) ssl2_state_st._fields_ = [ ('three_byte_header', c_int), ('clear_text', c_int), ('escape', c_int), ('ssl2_rollback', c_int), ('wnum', c_uint), ('wpend_tot', c_int), ('wpend_buf', POINTER(c_ubyte)), ('wpend_off', c_int), ('wpend_len', c_int), ('wpend_ret', c_int), ('rbuf_left', c_int), ('rbuf_offs', c_int), ('rbuf', POINTER(c_ubyte)), ('wbuf', POINTER(c_ubyte)), ('write_ptr', POINTER(c_ubyte)), ('padding', c_uint), ('rlength', c_uint), ('ract_data_length', c_int), ('wlength', c_uint), ('wact_data_length', c_int), ('ract_data', POINTER(c_ubyte)), ('wact_data', POINTER(c_ubyte)), ('mac_data', POINTER(c_ubyte)), ('read_key', POINTER(c_ubyte)), ('write_key', POINTER(c_ubyte)), ('challenge_length', c_uint), ('challenge', c_ubyte * 32), ('conn_id_length', c_uint), ('conn_id', c_ubyte * 16), ('key_material_length', c_uint), ('key_material', c_ubyte * 48), ('read_sequence', c_ulong), ('write_sequence', c_ulong), ('tmp', N13ssl2_state_st4DOLLAR_19E), ] assert sizeof(ssl2_state_st) == 288, sizeof(ssl2_state_st) assert alignment(ssl2_state_st) == 4, alignment(ssl2_state_st) SSL2_STATE = ssl2_state_st class ssl3_record_st(Structure): pass ssl3_record_st._fields_ = [ ('type', c_int), ('length', c_uint), ('off', c_uint), ('data', POINTER(c_ubyte)), ('input', POINTER(c_ubyte)), ('comp', POINTER(c_ubyte)), ] assert sizeof(ssl3_record_st) == 24, sizeof(ssl3_record_st) assert alignment(ssl3_record_st) == 4, alignment(ssl3_record_st) SSL3_RECORD = ssl3_record_st class ssl3_buffer_st(Structure): pass size_t = __darwin_size_t ssl3_buffer_st._fields_ = [ ('buf', POINTER(c_ubyte)), ('len', size_t), ('offset', c_int), ('left', c_int), ] assert sizeof(ssl3_buffer_st) == 16, sizeof(ssl3_buffer_st) assert alignment(ssl3_buffer_st) == 4, alignment(ssl3_buffer_st) SSL3_BUFFER = ssl3_buffer_st class N13ssl3_state_st4DOLLAR_20E(Structure): pass N13ssl3_state_st4DOLLAR_20E._fields_ = [ ('cert_verify_md', c_ubyte * 72), ('finish_md', c_ubyte * 72), ('finish_md_len', c_int), ('peer_finish_md', c_ubyte * 72), ('peer_finish_md_len', c_int), ('message_size', c_ulong), ('message_type', c_int), ('new_cipher', POINTER(SSL_CIPHER)), ('dh', POINTER(DH)), ('next_state', c_int), ('reuse_message', c_int), ('cert_req', c_int), ('ctype_num', c_int), ('ctype', c_char * 7), ('ca_names', POINTER(STACK)), ('use_rsa_tmp', c_int), ('key_block_length', c_int), ('key_block', POINTER(c_ubyte)), ('new_sym_enc', POINTER(EVP_CIPHER)), ('new_hash', POINTER(EVP_MD)), ('new_compression', POINTER(SSL_COMP)), ('cert_request', c_int), ] assert sizeof(N13ssl3_state_st4DOLLAR_20E) == 296, sizeof(N13ssl3_state_st4DOLLAR_20E) assert alignment(N13ssl3_state_st4DOLLAR_20E) == 4, alignment(N13ssl3_state_st4DOLLAR_20E) ssl3_state_st._fields_ = [ ('flags', c_long), ('delay_buf_pop_ret', c_int), ('read_sequence', c_ubyte * 8), ('read_mac_secret', c_ubyte * 36), ('write_sequence', c_ubyte * 8), ('write_mac_secret', c_ubyte * 36), ('server_random', c_ubyte * 32), ('client_random', c_ubyte * 32), ('need_empty_fragments', c_int), ('empty_fragment_done', c_int), ('rbuf', SSL3_BUFFER), ('wbuf', SSL3_BUFFER), ('rrec', SSL3_RECORD), ('wrec', SSL3_RECORD), ('alert_fragment', c_ubyte * 2), ('alert_fragment_len', c_uint), ('handshake_fragment', c_ubyte * 4), ('handshake_fragment_len', c_uint), ('wnum', c_uint), ('wpend_tot', c_int), ('wpend_type', c_int), ('wpend_ret', c_int), ('wpend_buf', POINTER(c_ubyte)), ('finish_dgst1', EVP_MD_CTX), ('finish_dgst2', EVP_MD_CTX), ('change_cipher_spec', c_int), ('warn_alert', c_int), ('fatal_alert', c_int), ('alert_dispatch', c_int), ('send_alert', c_ubyte * 2), ('renegotiate', c_int), ('total_renegotiations', c_int), ('num_renegotiations', c_int), ('in_read_app_data', c_int), ('tmp', N13ssl3_state_st4DOLLAR_20E), ] assert sizeof(ssl3_state_st) == 648, sizeof(ssl3_state_st) assert alignment(ssl3_state_st) == 4, alignment(ssl3_state_st) SSL3_STATE = ssl3_state_st stack_st._fields_ = [ ('num', c_int), ('data', POINTER(STRING)), ('sorted', c_int), ('num_alloc', c_int), ('comp', CFUNCTYPE(c_int, POINTER(STRING), POINTER(STRING))), ] assert sizeof(stack_st) == 20, sizeof(stack_st) assert alignment(stack_st) == 4, alignment(stack_st) class ui_st(Structure): pass ui_st._fields_ = [ ] UI = ui_st class ui_method_st(Structure): pass ui_method_st._fields_ = [ ] UI_METHOD = ui_method_st class ui_string_st(Structure): pass ui_string_st._fields_ = [ ] UI_STRING = ui_string_st # values for enumeration 'UI_string_types' UI_string_types = c_int # enum class X509_objects_st(Structure): pass X509_objects_st._fields_ = [ ('nid', c_int), ('a2i', CFUNCTYPE(c_int)), ('i2a', CFUNCTYPE(c_int)), ] assert sizeof(X509_objects_st) == 12, sizeof(X509_objects_st) assert alignment(X509_objects_st) == 4, alignment(X509_objects_st) X509_OBJECTS = X509_objects_st X509_algor_st._fields_ = [ ('algorithm', POINTER(ASN1_OBJECT)), ('parameter', POINTER(ASN1_TYPE)), ] assert sizeof(X509_algor_st) == 8, sizeof(X509_algor_st) assert alignment(X509_algor_st) == 4, alignment(X509_algor_st) class X509_val_st(Structure): pass X509_val_st._fields_ = [ ('notBefore', POINTER(ASN1_TIME)), ('notAfter', POINTER(ASN1_TIME)), ] assert sizeof(X509_val_st) == 8, sizeof(X509_val_st) assert alignment(X509_val_st) == 4, alignment(X509_val_st) X509_VAL = X509_val_st class X509_pubkey_st(Structure): pass X509_pubkey_st._fields_ = [ ('algor', POINTER(X509_ALGOR)), ('public_key', POINTER(ASN1_BIT_STRING)), ('pkey', POINTER(EVP_PKEY)), ] assert sizeof(X509_pubkey_st) == 12, sizeof(X509_pubkey_st) assert alignment(X509_pubkey_st) == 4, alignment(X509_pubkey_st) X509_PUBKEY = X509_pubkey_st class X509_sig_st(Structure): pass X509_sig_st._fields_ = [ ('algor', POINTER(X509_ALGOR)), ('digest', POINTER(ASN1_OCTET_STRING)), ] assert sizeof(X509_sig_st) == 8, sizeof(X509_sig_st) assert alignment(X509_sig_st) == 4, alignment(X509_sig_st) X509_SIG = X509_sig_st class X509_name_entry_st(Structure): pass X509_name_entry_st._fields_ = [ ('object', POINTER(ASN1_OBJECT)), ('value', POINTER(ASN1_STRING)), ('set', c_int), ('size', c_int), ] assert sizeof(X509_name_entry_st) == 16, sizeof(X509_name_entry_st) assert alignment(X509_name_entry_st) == 4, alignment(X509_name_entry_st) X509_NAME_ENTRY = X509_name_entry_st X509_name_st._fields_ = [ ('entries', POINTER(STACK)), ('modified', c_int), ('bytes', POINTER(BUF_MEM)), ('hash', c_ulong), ] assert sizeof(X509_name_st) == 16, sizeof(X509_name_st) assert alignment(X509_name_st) == 4, alignment(X509_name_st) class X509_extension_st(Structure): pass X509_extension_st._fields_ = [ ('object', POINTER(ASN1_OBJECT)), ('critical', ASN1_BOOLEAN), ('value', POINTER(ASN1_OCTET_STRING)), ] assert sizeof(X509_extension_st) == 12, sizeof(X509_extension_st) assert alignment(X509_extension_st) == 4, alignment(X509_extension_st) X509_EXTENSION = X509_extension_st class x509_attributes_st(Structure): pass class N18x509_attributes_st4DOLLAR_13E(Union): pass N18x509_attributes_st4DOLLAR_13E._fields_ = [ ('ptr', STRING), ('set', POINTER(STACK)), ('single', POINTER(ASN1_TYPE)), ] assert sizeof(N18x509_attributes_st4DOLLAR_13E) == 4, sizeof(N18x509_attributes_st4DOLLAR_13E) assert alignment(N18x509_attributes_st4DOLLAR_13E) == 4, alignment(N18x509_attributes_st4DOLLAR_13E) x509_attributes_st._fields_ = [ ('object', POINTER(ASN1_OBJECT)), ('single', c_int), ('value', N18x509_attributes_st4DOLLAR_13E), ] assert sizeof(x509_attributes_st) == 12, sizeof(x509_attributes_st) assert alignment(x509_attributes_st) == 4, alignment(x509_attributes_st) X509_ATTRIBUTE = x509_attributes_st class X509_req_info_st(Structure): pass X509_req_info_st._fields_ = [ ('enc', ASN1_ENCODING), ('version', POINTER(ASN1_INTEGER)), ('subject', POINTER(X509_NAME)), ('pubkey', POINTER(X509_PUBKEY)), ('attributes', POINTER(STACK)), ] assert sizeof(X509_req_info_st) == 28, sizeof(X509_req_info_st) assert alignment(X509_req_info_st) == 4, alignment(X509_req_info_st) X509_REQ_INFO = X509_req_info_st class X509_req_st(Structure): pass X509_req_st._fields_ = [ ('req_info', POINTER(X509_REQ_INFO)), ('sig_alg', POINTER(X509_ALGOR)), ('signature', POINTER(ASN1_BIT_STRING)), ('references', c_int), ] assert sizeof(X509_req_st) == 16, sizeof(X509_req_st) assert alignment(X509_req_st) == 4, alignment(X509_req_st) X509_REQ = X509_req_st class x509_cinf_st(Structure): pass x509_cinf_st._fields_ = [ ('version', POINTER(ASN1_INTEGER)), ('serialNumber', POINTER(ASN1_INTEGER)), ('signature', POINTER(X509_ALGOR)), ('issuer', POINTER(X509_NAME)), ('validity', POINTER(X509_VAL)), ('subject', POINTER(X509_NAME)), ('key', POINTER(X509_PUBKEY)), ('issuerUID', POINTER(ASN1_BIT_STRING)), ('subjectUID', POINTER(ASN1_BIT_STRING)), ('extensions', POINTER(STACK)), ] assert sizeof(x509_cinf_st) == 40, sizeof(x509_cinf_st) assert alignment(x509_cinf_st) == 4, alignment(x509_cinf_st) X509_CINF = x509_cinf_st class x509_cert_aux_st(Structure): pass x509_cert_aux_st._fields_ = [ ('trust', POINTER(STACK)), ('reject', POINTER(STACK)), ('alias', POINTER(ASN1_UTF8STRING)), ('keyid', POINTER(ASN1_OCTET_STRING)), ('other', POINTER(STACK)), ] assert sizeof(x509_cert_aux_st) == 20, sizeof(x509_cert_aux_st) assert alignment(x509_cert_aux_st) == 4, alignment(x509_cert_aux_st) X509_CERT_AUX = x509_cert_aux_st class AUTHORITY_KEYID_st(Structure): pass x509_st._fields_ = [ ('cert_info', POINTER(X509_CINF)), ('sig_alg', POINTER(X509_ALGOR)), ('signature', POINTER(ASN1_BIT_STRING)), ('valid', c_int), ('references', c_int), ('name', STRING), ('ex_data', CRYPTO_EX_DATA), ('ex_pathlen', c_long), ('ex_flags', c_ulong), ('ex_kusage', c_ulong), ('ex_xkusage', c_ulong), ('ex_nscert', c_ulong), ('skid', POINTER(ASN1_OCTET_STRING)), ('akid', POINTER(AUTHORITY_KEYID_st)), ('sha1_hash', c_ubyte * 20), ('aux', POINTER(X509_CERT_AUX)), ] assert sizeof(x509_st) == 84, sizeof(x509_st) assert alignment(x509_st) == 4, alignment(x509_st) AUTHORITY_KEYID_st._fields_ = [ ] class x509_trust_st(Structure): pass x509_trust_st._fields_ = [ ('trust', c_int), ('flags', c_int), ('check_trust', CFUNCTYPE(c_int, POINTER(x509_trust_st), POINTER(X509), c_int)), ('name', STRING), ('arg1', c_int), ('arg2', c_void_p), ] assert sizeof(x509_trust_st) == 24, sizeof(x509_trust_st) assert alignment(x509_trust_st) == 4, alignment(x509_trust_st) X509_TRUST = x509_trust_st class X509_revoked_st(Structure): pass X509_revoked_st._fields_ = [ ('serialNumber', POINTER(ASN1_INTEGER)), ('revocationDate', POINTER(ASN1_TIME)), ('extensions', POINTER(STACK)), ('sequence', c_int), ] assert sizeof(X509_revoked_st) == 16, sizeof(X509_revoked_st) assert alignment(X509_revoked_st) == 4, alignment(X509_revoked_st) X509_REVOKED = X509_revoked_st class X509_crl_info_st(Structure): pass X509_crl_info_st._fields_ = [ ('version', POINTER(ASN1_INTEGER)), ('sig_alg', POINTER(X509_ALGOR)), ('issuer', POINTER(X509_NAME)), ('lastUpdate', POINTER(ASN1_TIME)), ('nextUpdate', POINTER(ASN1_TIME)), ('revoked', POINTER(STACK)), ('extensions', POINTER(STACK)), ('enc', ASN1_ENCODING), ] assert sizeof(X509_crl_info_st) == 40, sizeof(X509_crl_info_st) assert alignment(X509_crl_info_st) == 4, alignment(X509_crl_info_st) X509_CRL_INFO = X509_crl_info_st X509_crl_st._fields_ = [ ('crl', POINTER(X509_CRL_INFO)), ('sig_alg', POINTER(X509_ALGOR)), ('signature', POINTER(ASN1_BIT_STRING)), ('references', c_int), ] assert sizeof(X509_crl_st) == 16, sizeof(X509_crl_st) assert alignment(X509_crl_st) == 4, alignment(X509_crl_st) class private_key_st(Structure): pass private_key_st._fields_ = [ ('version', c_int), ('enc_algor', POINTER(X509_ALGOR)), ('enc_pkey', POINTER(ASN1_OCTET_STRING)), ('dec_pkey', POINTER(EVP_PKEY)), ('key_length', c_int), ('key_data', STRING), ('key_free', c_int), ('cipher', EVP_CIPHER_INFO), ('references', c_int), ] assert sizeof(private_key_st) == 52, sizeof(private_key_st) assert alignment(private_key_st) == 4, alignment(private_key_st) X509_PKEY = private_key_st class X509_info_st(Structure): pass X509_info_st._fields_ = [ ('x509', POINTER(X509)), ('crl', POINTER(X509_CRL)), ('x_pkey', POINTER(X509_PKEY)), ('enc_cipher', EVP_CIPHER_INFO), ('enc_len', c_int), ('enc_data', STRING), ('references', c_int), ] assert sizeof(X509_info_st) == 44, sizeof(X509_info_st) assert alignment(X509_info_st) == 4, alignment(X509_info_st) X509_INFO = X509_info_st class Netscape_spkac_st(Structure): pass Netscape_spkac_st._fields_ = [ ('pubkey', POINTER(X509_PUBKEY)), ('challenge', POINTER(ASN1_IA5STRING)), ] assert sizeof(Netscape_spkac_st) == 8, sizeof(Netscape_spkac_st) assert alignment(Netscape_spkac_st) == 4, alignment(Netscape_spkac_st) NETSCAPE_SPKAC = Netscape_spkac_st class Netscape_spki_st(Structure): pass Netscape_spki_st._fields_ = [ ('spkac', POINTER(NETSCAPE_SPKAC)), ('sig_algor', POINTER(X509_ALGOR)), ('signature', POINTER(ASN1_BIT_STRING)), ] assert sizeof(Netscape_spki_st) == 12, sizeof(Netscape_spki_st) assert alignment(Netscape_spki_st) == 4, alignment(Netscape_spki_st) NETSCAPE_SPKI = Netscape_spki_st class Netscape_certificate_sequence(Structure): pass Netscape_certificate_sequence._fields_ = [ ('type', POINTER(ASN1_OBJECT)), ('certs', POINTER(STACK)), ] assert sizeof(Netscape_certificate_sequence) == 8, sizeof(Netscape_certificate_sequence) assert alignment(Netscape_certificate_sequence) == 4, alignment(Netscape_certificate_sequence) NETSCAPE_CERT_SEQUENCE = Netscape_certificate_sequence class PBEPARAM_st(Structure): pass PBEPARAM_st._fields_ = [ ('salt', POINTER(ASN1_OCTET_STRING)), ('iter', POINTER(ASN1_INTEGER)), ] assert sizeof(PBEPARAM_st) == 8, sizeof(PBEPARAM_st) assert alignment(PBEPARAM_st) == 4, alignment(PBEPARAM_st) PBEPARAM = PBEPARAM_st class PBE2PARAM_st(Structure): pass PBE2PARAM_st._fields_ = [ ('keyfunc', POINTER(X509_ALGOR)), ('encryption', POINTER(X509_ALGOR)), ] assert sizeof(PBE2PARAM_st) == 8, sizeof(PBE2PARAM_st) assert alignment(PBE2PARAM_st) == 4, alignment(PBE2PARAM_st) PBE2PARAM = PBE2PARAM_st class PBKDF2PARAM_st(Structure): pass PBKDF2PARAM_st._fields_ = [ ('salt', POINTER(ASN1_TYPE)), ('iter', POINTER(ASN1_INTEGER)), ('keylength', POINTER(ASN1_INTEGER)), ('prf', POINTER(X509_ALGOR)), ] assert sizeof(PBKDF2PARAM_st) == 16, sizeof(PBKDF2PARAM_st) assert alignment(PBKDF2PARAM_st) == 4, alignment(PBKDF2PARAM_st) PBKDF2PARAM = PBKDF2PARAM_st class pkcs8_priv_key_info_st(Structure): pass pkcs8_priv_key_info_st._fields_ = [ ('broken', c_int), ('version', POINTER(ASN1_INTEGER)), ('pkeyalg', POINTER(X509_ALGOR)), ('pkey', POINTER(ASN1_TYPE)), ('attributes', POINTER(STACK)), ] assert sizeof(pkcs8_priv_key_info_st) == 20, sizeof(pkcs8_priv_key_info_st) assert alignment(pkcs8_priv_key_info_st) == 4, alignment(pkcs8_priv_key_info_st) PKCS8_PRIV_KEY_INFO = pkcs8_priv_key_info_st class x509_hash_dir_st(Structure): pass x509_hash_dir_st._fields_ = [ ('num_dirs', c_int), ('dirs', POINTER(STRING)), ('dirs_type', POINTER(c_int)), ('num_dirs_alloced', c_int), ] assert sizeof(x509_hash_dir_st) == 16, sizeof(x509_hash_dir_st) assert alignment(x509_hash_dir_st) == 4, alignment(x509_hash_dir_st) X509_HASH_DIR_CTX = x509_hash_dir_st class x509_file_st(Structure): pass x509_file_st._fields_ = [ ('num_paths', c_int), ('num_alloced', c_int), ('paths', POINTER(STRING)), ('path_type', POINTER(c_int)), ] assert sizeof(x509_file_st) == 16, sizeof(x509_file_st) assert alignment(x509_file_st) == 4, alignment(x509_file_st) X509_CERT_FILE_CTX = x509_file_st class x509_object_st(Structure): pass class N14x509_object_st4DOLLAR_14E(Union): pass N14x509_object_st4DOLLAR_14E._fields_ = [ ('ptr', STRING), ('x509', POINTER(X509)), ('crl', POINTER(X509_CRL)), ('pkey', POINTER(EVP_PKEY)), ] assert sizeof(N14x509_object_st4DOLLAR_14E) == 4, sizeof(N14x509_object_st4DOLLAR_14E) assert alignment(N14x509_object_st4DOLLAR_14E) == 4, alignment(N14x509_object_st4DOLLAR_14E) x509_object_st._fields_ = [ ('type', c_int), ('data', N14x509_object_st4DOLLAR_14E), ] assert sizeof(x509_object_st) == 8, sizeof(x509_object_st) assert alignment(x509_object_st) == 4, alignment(x509_object_st) X509_OBJECT = x509_object_st class x509_lookup_st(Structure): pass X509_LOOKUP = x509_lookup_st class x509_lookup_method_st(Structure): pass x509_lookup_method_st._fields_ = [ ('name', STRING), ('new_item', CFUNCTYPE(c_int, POINTER(X509_LOOKUP))), ('free', CFUNCTYPE(None, POINTER(X509_LOOKUP))), ('init', CFUNCTYPE(c_int, POINTER(X509_LOOKUP))), ('shutdown', CFUNCTYPE(c_int, POINTER(X509_LOOKUP))), ('ctrl', CFUNCTYPE(c_int, POINTER(X509_LOOKUP), c_int, STRING, c_long, POINTER(STRING))), ('get_by_subject', CFUNCTYPE(c_int, POINTER(X509_LOOKUP), c_int, POINTER(X509_NAME), POINTER(X509_OBJECT))), ('get_by_issuer_serial', CFUNCTYPE(c_int, POINTER(X509_LOOKUP), c_int, POINTER(X509_NAME), POINTER(ASN1_INTEGER), POINTER(X509_OBJECT))), ('get_by_fingerprint', CFUNCTYPE(c_int, POINTER(X509_LOOKUP), c_int, POINTER(c_ubyte), c_int, POINTER(X509_OBJECT))), ('get_by_alias', CFUNCTYPE(c_int, POINTER(X509_LOOKUP), c_int, STRING, c_int, POINTER(X509_OBJECT))), ] assert sizeof(x509_lookup_method_st) == 40, sizeof(x509_lookup_method_st) assert alignment(x509_lookup_method_st) == 4, alignment(x509_lookup_method_st) X509_LOOKUP_METHOD = x509_lookup_method_st x509_store_st._fields_ = [ ('cache', c_int), ('objs', POINTER(STACK)), ('get_cert_methods', POINTER(STACK)), ('flags', c_ulong), ('purpose', c_int), ('trust', c_int), ('verify', CFUNCTYPE(c_int, POINTER(X509_STORE_CTX))), ('verify_cb', CFUNCTYPE(c_int, c_int, POINTER(X509_STORE_CTX))), ('get_issuer', CFUNCTYPE(c_int, POINTER(POINTER(X509)), POINTER(X509_STORE_CTX), POINTER(X509))), ('check_issued', CFUNCTYPE(c_int, POINTER(X509_STORE_CTX), POINTER(X509), POINTER(X509))), ('check_revocation', CFUNCTYPE(c_int, POINTER(X509_STORE_CTX))), ('get_crl', CFUNCTYPE(c_int, POINTER(X509_STORE_CTX), POINTER(POINTER(X509_CRL)), POINTER(X509))), ('check_crl', CFUNCTYPE(c_int, POINTER(X509_STORE_CTX), POINTER(X509_CRL))), ('cert_crl', CFUNCTYPE(c_int, POINTER(X509_STORE_CTX), POINTER(X509_CRL), POINTER(X509))), ('cleanup', CFUNCTYPE(c_int, POINTER(X509_STORE_CTX))), ('ex_data', CRYPTO_EX_DATA), ('references', c_int), ('depth', c_int), ] assert sizeof(x509_store_st) == 76, sizeof(x509_store_st) assert alignment(x509_store_st) == 4, alignment(x509_store_st) x509_lookup_st._fields_ = [ ('init', c_int), ('skip', c_int), ('method', POINTER(X509_LOOKUP_METHOD)), ('method_data', STRING), ('store_ctx', POINTER(X509_STORE)), ] assert sizeof(x509_lookup_st) == 20, sizeof(x509_lookup_st) assert alignment(x509_lookup_st) == 4, alignment(x509_lookup_st) time_t = __darwin_time_t x509_store_ctx_st._fields_ = [ ('ctx', POINTER(X509_STORE)), ('current_method', c_int), ('cert', POINTER(X509)), ('untrusted', POINTER(STACK)), ('purpose', c_int), ('trust', c_int), ('check_time', time_t), ('flags', c_ulong), ('other_ctx', c_void_p), ('verify', CFUNCTYPE(c_int, POINTER(X509_STORE_CTX))), ('verify_cb', CFUNCTYPE(c_int, c_int, POINTER(X509_STORE_CTX))), ('get_issuer', CFUNCTYPE(c_int, POINTER(POINTER(X509)), POINTER(X509_STORE_CTX), POINTER(X509))), ('check_issued', CFUNCTYPE(c_int, POINTER(X509_STORE_CTX), POINTER(X509), POINTER(X509))), ('check_revocation', CFUNCTYPE(c_int, POINTER(X509_STORE_CTX))), ('get_crl', CFUNCTYPE(c_int, POINTER(X509_STORE_CTX), POINTER(POINTER(X509_CRL)), POINTER(X509))), ('check_crl', CFUNCTYPE(c_int, POINTER(X509_STORE_CTX), POINTER(X509_CRL))), ('cert_crl', CFUNCTYPE(c_int, POINTER(X509_STORE_CTX), POINTER(X509_CRL), POINTER(X509))), ('cleanup', CFUNCTYPE(c_int, POINTER(X509_STORE_CTX))), ('depth', c_int), ('valid', c_int), ('last_untrusted', c_int), ('chain', POINTER(STACK)), ('error_depth', c_int), ('error', c_int), ('current_cert', POINTER(X509)), ('current_issuer', POINTER(X509)), ('current_crl', POINTER(X509_CRL)), ('ex_data', CRYPTO_EX_DATA), ] assert sizeof(x509_store_ctx_st) == 116, sizeof(x509_store_ctx_st) assert alignment(x509_store_ctx_st) == 4, alignment(x509_store_ctx_st) va_list = __darwin_va_list __darwin_off_t = __int64_t fpos_t = __darwin_off_t class __sbuf(Structure): pass __sbuf._fields_ = [ ('_base', POINTER(c_ubyte)), ('_size', c_int), ] assert sizeof(__sbuf) == 8, sizeof(__sbuf) assert alignment(__sbuf) == 4, alignment(__sbuf) class __sFILEX(Structure): pass __sFILEX._fields_ = [ ] class __sFILE(Structure): pass __sFILE._pack_ = 4 __sFILE._fields_ = [ ('_p', POINTER(c_ubyte)), ('_r', c_int), ('_w', c_int), ('_flags', c_short), ('_file', c_short), ('_bf', __sbuf), ('_lbfsize', c_int), ('_cookie', c_void_p), ('_close', CFUNCTYPE(c_int, c_void_p)), ('_read', CFUNCTYPE(c_int, c_void_p, STRING, c_int)), ('_seek', CFUNCTYPE(fpos_t, c_void_p, c_longlong, c_int)), ('_write', CFUNCTYPE(c_int, c_void_p, STRING, c_int)), ('_ub', __sbuf), ('_extra', POINTER(__sFILEX)), ('_ur', c_int), ('_ubuf', c_ubyte * 3), ('_nbuf', c_ubyte * 1), ('_lb', __sbuf), ('_blksize', c_int), ('_offset', fpos_t), ] assert sizeof(__sFILE) == 88, sizeof(__sFILE) assert alignment(__sFILE) == 4, alignment(__sFILE) FILE = __sFILE ct_rune_t = __darwin_ct_rune_t rune_t = __darwin_rune_t class div_t(Structure): pass div_t._fields_ = [ ('quot', c_int), ('rem', c_int), ] assert sizeof(div_t) == 8, sizeof(div_t) assert alignment(div_t) == 4, alignment(div_t) class ldiv_t(Structure): pass ldiv_t._fields_ = [ ('quot', c_long), ('rem', c_long), ] assert sizeof(ldiv_t) == 8, sizeof(ldiv_t) assert alignment(ldiv_t) == 4, alignment(ldiv_t) class lldiv_t(Structure): pass lldiv_t._pack_ = 4 lldiv_t._fields_ = [ ('quot', c_longlong), ('rem', c_longlong), ] assert sizeof(lldiv_t) == 16, sizeof(lldiv_t) assert alignment(lldiv_t) == 4, alignment(lldiv_t) __darwin_dev_t = __int32_t dev_t = __darwin_dev_t __darwin_mode_t = __uint16_t mode_t = __darwin_mode_t class mcontext(Structure): pass mcontext._fields_ = [ ] class mcontext64(Structure): pass mcontext64._fields_ = [ ] class __darwin_pthread_handler_rec(Structure): pass __darwin_pthread_handler_rec._fields_ = [ ('__routine', CFUNCTYPE(None, c_void_p)), ('__arg', c_void_p), ('__next', POINTER(__darwin_pthread_handler_rec)), ] assert sizeof(__darwin_pthread_handler_rec) == 12, sizeof(__darwin_pthread_handler_rec) assert alignment(__darwin_pthread_handler_rec) == 4, alignment(__darwin_pthread_handler_rec) class _opaque_pthread_attr_t(Structure): pass _opaque_pthread_attr_t._fields_ = [ ('__sig', c_long), ('__opaque', c_char * 36), ] assert sizeof(_opaque_pthread_attr_t) == 40, sizeof(_opaque_pthread_attr_t) assert alignment(_opaque_pthread_attr_t) == 4, alignment(_opaque_pthread_attr_t) class _opaque_pthread_cond_t(Structure): pass _opaque_pthread_cond_t._fields_ = [ ('__sig', c_long), ('__opaque', c_char * 24), ] assert sizeof(_opaque_pthread_cond_t) == 28, sizeof(_opaque_pthread_cond_t) assert alignment(_opaque_pthread_cond_t) == 4, alignment(_opaque_pthread_cond_t) class _opaque_pthread_condattr_t(Structure): pass _opaque_pthread_condattr_t._fields_ = [ ('__sig', c_long), ('__opaque', c_char * 4), ] assert sizeof(_opaque_pthread_condattr_t) == 8, sizeof(_opaque_pthread_condattr_t) assert alignment(_opaque_pthread_condattr_t) == 4, alignment(_opaque_pthread_condattr_t) class _opaque_pthread_mutex_t(Structure): pass _opaque_pthread_mutex_t._fields_ = [ ('__sig', c_long), ('__opaque', c_char * 40), ] assert sizeof(_opaque_pthread_mutex_t) == 44, sizeof(_opaque_pthread_mutex_t) assert alignment(_opaque_pthread_mutex_t) == 4, alignment(_opaque_pthread_mutex_t) class _opaque_pthread_mutexattr_t(Structure): pass _opaque_pthread_mutexattr_t._fields_ = [ ('__sig', c_long), ('__opaque', c_char * 8), ] assert sizeof(_opaque_pthread_mutexattr_t) == 12, sizeof(_opaque_pthread_mutexattr_t) assert alignment(_opaque_pthread_mutexattr_t) == 4, alignment(_opaque_pthread_mutexattr_t) class _opaque_pthread_once_t(Structure): pass _opaque_pthread_once_t._fields_ = [ ('__sig', c_long), ('__opaque', c_char * 4), ] assert sizeof(_opaque_pthread_once_t) == 8, sizeof(_opaque_pthread_once_t) assert alignment(_opaque_pthread_once_t) == 4, alignment(_opaque_pthread_once_t) class _opaque_pthread_rwlock_t(Structure): pass _opaque_pthread_rwlock_t._fields_ = [ ('__sig', c_long), ('__opaque', c_char * 124), ] assert sizeof(_opaque_pthread_rwlock_t) == 128, sizeof(_opaque_pthread_rwlock_t) assert alignment(_opaque_pthread_rwlock_t) == 4, alignment(_opaque_pthread_rwlock_t) class _opaque_pthread_rwlockattr_t(Structure): pass _opaque_pthread_rwlockattr_t._fields_ = [ ('__sig', c_long), ('__opaque', c_char * 12), ] assert sizeof(_opaque_pthread_rwlockattr_t) == 16, sizeof(_opaque_pthread_rwlockattr_t) assert alignment(_opaque_pthread_rwlockattr_t) == 4, alignment(_opaque_pthread_rwlockattr_t) class _opaque_pthread_t(Structure): pass _opaque_pthread_t._fields_ = [ ('__sig', c_long), ('__cleanup_stack', POINTER(__darwin_pthread_handler_rec)), ('__opaque', c_char * 596), ] assert sizeof(_opaque_pthread_t) == 604, sizeof(_opaque_pthread_t) assert alignment(_opaque_pthread_t) == 4, alignment(_opaque_pthread_t) __darwin_blkcnt_t = __int64_t __darwin_blksize_t = __int32_t __darwin_fsblkcnt_t = c_uint __darwin_fsfilcnt_t = c_uint __darwin_gid_t = __uint32_t __darwin_id_t = __uint32_t __darwin_ino_t = __uint32_t __darwin_mach_port_name_t = __darwin_natural_t __darwin_mach_port_t = __darwin_mach_port_name_t __darwin_mcontext_t = POINTER(mcontext) __darwin_mcontext64_t = POINTER(mcontext64) __darwin_pid_t = __int32_t __darwin_pthread_attr_t = _opaque_pthread_attr_t __darwin_pthread_cond_t = _opaque_pthread_cond_t __darwin_pthread_condattr_t = _opaque_pthread_condattr_t __darwin_pthread_key_t = c_ulong __darwin_pthread_mutex_t = _opaque_pthread_mutex_t __darwin_pthread_mutexattr_t = _opaque_pthread_mutexattr_t __darwin_pthread_once_t = _opaque_pthread_once_t __darwin_pthread_rwlock_t = _opaque_pthread_rwlock_t __darwin_pthread_rwlockattr_t = _opaque_pthread_rwlockattr_t __darwin_pthread_t = POINTER(_opaque_pthread_t) __darwin_sigset_t = __uint32_t __darwin_suseconds_t = __int32_t __darwin_uid_t = __uint32_t __darwin_useconds_t = __uint32_t __darwin_uuid_t = c_ubyte * 16 class sigaltstack(Structure): pass sigaltstack._fields_ = [ ('ss_sp', c_void_p), ('ss_size', __darwin_size_t), ('ss_flags', c_int), ] assert sizeof(sigaltstack) == 12, sizeof(sigaltstack) assert alignment(sigaltstack) == 4, alignment(sigaltstack) __darwin_stack_t = sigaltstack class ucontext(Structure): pass ucontext._fields_ = [ ('uc_onstack', c_int), ('uc_sigmask', __darwin_sigset_t), ('uc_stack', __darwin_stack_t), ('uc_link', POINTER(ucontext)), ('uc_mcsize', __darwin_size_t), ('uc_mcontext', __darwin_mcontext_t), ] assert sizeof(ucontext) == 32, sizeof(ucontext) assert alignment(ucontext) == 4, alignment(ucontext) __darwin_ucontext_t = ucontext class ucontext64(Structure): pass ucontext64._fields_ = [ ('uc_onstack', c_int), ('uc_sigmask', __darwin_sigset_t), ('uc_stack', __darwin_stack_t), ('uc_link', POINTER(ucontext64)), ('uc_mcsize', __darwin_size_t), ('uc_mcontext64', __darwin_mcontext64_t), ] assert sizeof(ucontext64) == 32, sizeof(ucontext64) assert alignment(ucontext64) == 4, alignment(ucontext64) __darwin_ucontext64_t = ucontext64 class timeval(Structure): pass timeval._fields_ = [ ('tv_sec', __darwin_time_t), ('tv_usec', __darwin_suseconds_t), ] assert sizeof(timeval) == 8, sizeof(timeval) assert alignment(timeval) == 4, alignment(timeval) rlim_t = __int64_t class rusage(Structure): pass rusage._fields_ = [ ('ru_utime', timeval), ('ru_stime', timeval), ('ru_maxrss', c_long), ('ru_ixrss', c_long), ('ru_idrss', c_long), ('ru_isrss', c_long), ('ru_minflt', c_long), ('ru_majflt', c_long), ('ru_nswap', c_long), ('ru_inblock', c_long), ('ru_oublock', c_long), ('ru_msgsnd', c_long), ('ru_msgrcv', c_long), ('ru_nsignals', c_long), ('ru_nvcsw', c_long), ('ru_nivcsw', c_long), ] assert sizeof(rusage) == 72, sizeof(rusage) assert alignment(rusage) == 4, alignment(rusage) class rlimit(Structure): pass rlimit._pack_ = 4 rlimit._fields_ = [ ('rlim_cur', rlim_t), ('rlim_max', rlim_t), ] assert sizeof(rlimit) == 16, sizeof(rlimit) assert alignment(rlimit) == 4, alignment(rlimit) mcontext_t = __darwin_mcontext_t mcontext64_t = __darwin_mcontext64_t pthread_attr_t = __darwin_pthread_attr_t sigset_t = __darwin_sigset_t ucontext_t = __darwin_ucontext_t ucontext64_t = __darwin_ucontext64_t uid_t = __darwin_uid_t class sigval(Union): pass sigval._fields_ = [ ('sival_int', c_int), ('sival_ptr', c_void_p), ] assert sizeof(sigval) == 4, sizeof(sigval) assert alignment(sigval) == 4, alignment(sigval) class sigevent(Structure): pass sigevent._fields_ = [ ('sigev_notify', c_int), ('sigev_signo', c_int), ('sigev_value', sigval), ('sigev_notify_function', CFUNCTYPE(None, sigval)), ('sigev_notify_attributes', POINTER(pthread_attr_t)), ] assert sizeof(sigevent) == 20, sizeof(sigevent) assert alignment(sigevent) == 4, alignment(sigevent) class __siginfo(Structure): pass pid_t = __darwin_pid_t __siginfo._fields_ = [ ('si_signo', c_int), ('si_errno', c_int), ('si_code', c_int), ('si_pid', pid_t), ('si_uid', uid_t), ('si_status', c_int), ('si_addr', c_void_p), ('si_value', sigval), ('si_band', c_long), ('pad', c_ulong * 7), ] assert sizeof(__siginfo) == 64, sizeof(__siginfo) assert alignment(__siginfo) == 4, alignment(__siginfo) siginfo_t = __siginfo class __sigaction_u(Union): pass __sigaction_u._fields_ = [ ('__sa_handler', CFUNCTYPE(None, c_int)), ('__sa_sigaction', CFUNCTYPE(None, c_int, POINTER(__siginfo), c_void_p)), ] assert sizeof(__sigaction_u) == 4, sizeof(__sigaction_u) assert alignment(__sigaction_u) == 4, alignment(__sigaction_u) class __sigaction(Structure): pass __sigaction._fields_ = [ ('__sigaction_u', __sigaction_u), ('sa_tramp', CFUNCTYPE(None, c_void_p, c_int, c_int, POINTER(siginfo_t), c_void_p)), ('sa_mask', sigset_t), ('sa_flags', c_int), ] assert sizeof(__sigaction) == 16, sizeof(__sigaction) assert alignment(__sigaction) == 4, alignment(__sigaction) class sigaction(Structure): pass sigaction._fields_ = [ ('__sigaction_u', __sigaction_u), ('sa_mask', sigset_t), ('sa_flags', c_int), ] assert sizeof(sigaction) == 12, sizeof(sigaction) assert alignment(sigaction) == 4, alignment(sigaction) sig_t = CFUNCTYPE(None, c_int) stack_t = __darwin_stack_t class sigvec(Structure): pass sigvec._fields_ = [ ('sv_handler', CFUNCTYPE(None, c_int)), ('sv_mask', c_int), ('sv_flags', c_int), ] assert sizeof(sigvec) == 12, sizeof(sigvec) assert alignment(sigvec) == 4, alignment(sigvec) class sigstack(Structure): pass sigstack._fields_ = [ ('ss_sp', STRING), ('ss_onstack', c_int), ] assert sizeof(sigstack) == 8, sizeof(sigstack) assert alignment(sigstack) == 4, alignment(sigstack) u_char = c_ubyte u_short = c_ushort u_int = c_uint u_long = c_ulong ushort = c_ushort uint = c_uint u_quad_t = u_int64_t quad_t = int64_t qaddr_t = POINTER(quad_t) caddr_t = STRING daddr_t = int32_t fixpt_t = u_int32_t blkcnt_t = __darwin_blkcnt_t blksize_t = __darwin_blksize_t gid_t = __darwin_gid_t in_addr_t = __uint32_t in_port_t = __uint16_t ino_t = __darwin_ino_t key_t = __int32_t nlink_t = __uint16_t off_t = __darwin_off_t segsz_t = int32_t swblk_t = int32_t clock_t = __darwin_clock_t ssize_t = __darwin_ssize_t useconds_t = __darwin_useconds_t suseconds_t = __darwin_suseconds_t fd_mask = __int32_t class fd_set(Structure): pass fd_set._fields_ = [ ('fds_bits', __int32_t * 32), ] assert sizeof(fd_set) == 128, sizeof(fd_set) assert alignment(fd_set) == 4, alignment(fd_set) pthread_cond_t = __darwin_pthread_cond_t pthread_condattr_t = __darwin_pthread_condattr_t pthread_mutex_t = __darwin_pthread_mutex_t pthread_mutexattr_t = __darwin_pthread_mutexattr_t pthread_once_t = __darwin_pthread_once_t pthread_rwlock_t = __darwin_pthread_rwlock_t pthread_rwlockattr_t = __darwin_pthread_rwlockattr_t pthread_t = __darwin_pthread_t pthread_key_t = __darwin_pthread_key_t fsblkcnt_t = __darwin_fsblkcnt_t fsfilcnt_t = __darwin_fsfilcnt_t # values for enumeration 'idtype_t' idtype_t = c_int # enum id_t = __darwin_id_t class wait(Union): pass class N4wait3DOLLAR_3E(Structure): pass N4wait3DOLLAR_3E._fields_ = [ ('w_Termsig', c_uint, 7), ('w_Coredump', c_uint, 1), ('w_Retcode', c_uint, 8), ('w_Filler', c_uint, 16), ] assert sizeof(N4wait3DOLLAR_3E) == 4, sizeof(N4wait3DOLLAR_3E) assert alignment(N4wait3DOLLAR_3E) == 4, alignment(N4wait3DOLLAR_3E) class N4wait3DOLLAR_4E(Structure): pass N4wait3DOLLAR_4E._fields_ = [ ('w_Stopval', c_uint, 8), ('w_Stopsig', c_uint, 8), ('w_Filler', c_uint, 16), ] assert sizeof(N4wait3DOLLAR_4E) == 4, sizeof(N4wait3DOLLAR_4E) assert alignment(N4wait3DOLLAR_4E) == 4, alignment(N4wait3DOLLAR_4E) wait._fields_ = [ ('w_status', c_int), ('w_T', N4wait3DOLLAR_3E), ('w_S', N4wait3DOLLAR_4E), ] assert sizeof(wait) == 4, sizeof(wait) assert alignment(wait) == 4, alignment(wait) class timespec(Structure): pass timespec._fields_ = [ ('tv_sec', time_t), ('tv_nsec', c_long), ] assert sizeof(timespec) == 8, sizeof(timespec) assert alignment(timespec) == 4, alignment(timespec) class tm(Structure): pass tm._fields_ = [ ('tm_sec', c_int), ('tm_min', c_int), ('tm_hour', c_int), ('tm_mday', c_int), ('tm_mon', c_int), ('tm_year', c_int), ('tm_wday', c_int), ('tm_yday', c_int), ('tm_isdst', c_int), ('tm_gmtoff', c_long), ('tm_zone', STRING), ] assert sizeof(tm) == 44, sizeof(tm) assert alignment(tm) == 4, alignment(tm) __gnuc_va_list = STRING ptrdiff_t = c_int int8_t = c_byte int16_t = c_short uint8_t = c_ubyte uint16_t = c_ushort uint32_t = c_uint uint64_t = c_ulonglong int_least8_t = int8_t int_least16_t = int16_t int_least32_t = int32_t int_least64_t = int64_t uint_least8_t = uint8_t uint_least16_t = uint16_t uint_least32_t = uint32_t uint_least64_t = uint64_t int_fast8_t = int8_t int_fast16_t = int16_t int_fast32_t = int32_t int_fast64_t = int64_t uint_fast8_t = uint8_t uint_fast16_t = uint16_t uint_fast32_t = uint32_t uint_fast64_t = uint64_t intptr_t = c_long uintptr_t = c_ulong intmax_t = c_longlong uintmax_t = c_ulonglong __all__ = ['ENGINE', 'pkcs7_enc_content_st', '__int16_t', 'X509_REVOKED', 'SSL_CTX', 'UIT_BOOLEAN', '__darwin_time_t', 'ucontext64_t', 'int_fast32_t', 'pem_ctx_st', 'uint8_t', 'fpos_t', 'X509', 'COMP_CTX', 'tm', 'N10pem_ctx_st4DOLLAR_17E', 'swblk_t', 'ASN1_TEMPLATE', '__darwin_pthread_t', 'fixpt_t', 'BIO_METHOD', 'ASN1_PRINTABLESTRING', 'EVP_ENCODE_CTX', 'dh_method', 'bio_f_buffer_ctx_struct', 'in_port_t', 'X509_SIG', '__darwin_ssize_t', '__darwin_sigset_t', 'wait', 'uint_fast16_t', 'N12asn1_type_st4DOLLAR_11E', 'uint_least8_t', 'pthread_rwlock_t', 'ASN1_IA5STRING', 'fsfilcnt_t', 'ucontext', '__uint64_t', 'timespec', 'x509_cinf_st', 'COMP_METHOD', 'MD5_CTX', 'buf_mem_st', 'ASN1_ENCODING_st', 'PBEPARAM', 'X509_NAME_ENTRY', '__darwin_va_list', 'ucontext_t', 'lhash_st', 'N4wait3DOLLAR_4E', '__darwin_uuid_t', '_ossl_old_des_ks_struct', 'id_t', 'ASN1_BIT_STRING', 'va_list', '__darwin_wchar_t', 'pthread_key_t', 'pkcs7_signer_info_st', 'ASN1_METHOD', 'DSA_SIG', 'DSA', 'UIT_NONE', 'pthread_t', '__darwin_useconds_t', 'uint_fast8_t', 'UI_STRING', 'DES_cblock', '__darwin_mcontext64_t', 'rlim_t', 'PEM_Encode_Seal_st', 'SHAstate_st', 'u_quad_t', 'openssl_fptr', '_opaque_pthread_rwlockattr_t', 'N18x509_attributes_st4DOLLAR_13E', '__darwin_pthread_rwlock_t', 'daddr_t', 'ui_string_st', 'x509_file_st', 'X509_req_info_st', 'int_least64_t', 'evp_Encode_Ctx_st', 'X509_OBJECTS', 'CRYPTO_EX_DATA', '__int8_t', 'AUTHORITY_KEYID_st', '_opaque_pthread_attr_t', 'sigstack', 'EVP_CIPHER_CTX', 'X509_extension_st', 'pid_t', 'RSA_METHOD', 'PEM_USER', 'pem_recip_st', 'env_md_ctx_st', 'rc5_key_st', 'ui_st', 'X509_PUBKEY', 'u_int8_t', 'ASN1_ITEM_st', 'pkcs7_recip_info_st', 'ssl2_state_st', 'off_t', 'N10ssl_ctx_st4DOLLAR_18E', 'crypto_ex_data_st', 'ui_method_st', '__darwin_pthread_rwlockattr_t', 'CRYPTO_EX_dup', '__darwin_ino_t', '__sFILE', 'OSUnknownByteOrder', 'BN_MONT_CTX', 'ASN1_NULL', 'time_t', 'CRYPTO_EX_new', 'asn1_type_st', 'CRYPTO_EX_DATA_FUNCS', 'user_time_t', 'BIGNUM', 'pthread_rwlockattr_t', 'ASN1_VALUE_st', 'DH_METHOD', '__darwin_off_t', '_opaque_pthread_t', 'bn_blinding_st', 'RSA', 'ssize_t', 'mcontext64_t', 'user_long_t', 'fsblkcnt_t', 'cert_st', '__darwin_pthread_condattr_t', 'X509_PKEY', '__darwin_id_t', '__darwin_nl_item', 'SSL2_STATE', 'FILE', 'pthread_mutexattr_t', 'size_t', '_ossl_old_des_key_schedule', 'pkcs7_issuer_and_serial_st', 'sigval', 'CRYPTO_MEM_LEAK_CB', 'X509_NAME', 'blkcnt_t', 'uint_least16_t', '__darwin_dev_t', 'evp_cipher_info_st', 'BN_BLINDING', 'ssl3_state_st', 'uint_least64_t', 'user_addr_t', 'DES_key_schedule', 'RIPEMD160_CTX', 'u_char', 'X509_algor_st', 'uid_t', 'sess_cert_st', 'u_int64_t', 'u_int16_t', 'sigset_t', '__darwin_ptrdiff_t', 'ASN1_CTX', 'STACK', '__int32_t', 'UI_METHOD', 'NETSCAPE_SPKI', 'UIT_PROMPT', 'st_CRYPTO_EX_DATA_IMPL', 'cast_key_st', 'X509_HASH_DIR_CTX', 'sigevent', 'user_ssize_t', 'clock_t', 'aes_key_st', '__darwin_socklen_t', '__darwin_intptr_t', 'int_fast64_t', 'asn1_string_table_st', 'uint_fast32_t', 'ASN1_VISIBLESTRING', 'DSA_SIG_st', 'obj_name_st', 'X509_LOOKUP_METHOD', 'u_int32_t', 'EVP_CIPHER_INFO', '__gnuc_va_list', 'AES_KEY', 'PKCS7_ISSUER_AND_SERIAL', 'BN_CTX', '__darwin_blkcnt_t', 'key_t', 'SHA_CTX', 'pkcs7_signed_st', 'SSL', 'N10pem_ctx_st4DOLLAR_16E', 'pthread_attr_t', 'EVP_MD', 'uint', 'ASN1_BOOLEAN', 'ino_t', '__darwin_clock_t', 'ASN1_OCTET_STRING', 'asn1_ctx_st', 'BIO_F_BUFFER_CTX', 'bn_mont_ctx_st', 'X509_REQ_INFO', 'PEM_CTX', 'sigvec', '__darwin_pthread_mutexattr_t', 'x509_attributes_st', 'stack_t', '__darwin_mode_t', '__mbstate_t', 'asn1_object_st', 'ASN1_ENCODING', '__uint8_t', 'LHASH_NODE', 'PKCS7_SIGNER_INFO', 'asn1_method_st', 'stack_st', 'bio_info_cb', 'div_t', 'UIT_VERIFY', 'PBEPARAM_st', 'N4wait3DOLLAR_3E', 'quad_t', '__siginfo', '__darwin_mbstate_t', 'rsa_st', 'ASN1_UNIVERSALSTRING', 'uint64_t', 'ssl_comp_st', 'X509_OBJECT', 'pthread_cond_t', 'DH', '__darwin_wctype_t', 'PKCS7_ENVELOPE', 'ASN1_TLC_st', 'sig_atomic_t', 'BIO', 'nlink_t', 'BUF_MEM', 'SSL3_RECORD', 'bio_method_st', 'timeval', 'UI_string_types', 'BIO_dummy', 'ssl_ctx_st', 'NETSCAPE_CERT_SEQUENCE', 'BIT_STRING_BITNAME_st', '__darwin_pthread_attr_t', 'int8_t', '__darwin_wint_t', 'OBJ_NAME', 'PKCS8_PRIV_KEY_INFO', 'PBE2PARAM_st', 'LHASH_DOALL_FN_TYPE', 'x509_st', 'X509_VAL', 'dev_t', 'ASN1_TEMPLATE_st', 'MD5state_st', '__uint16_t', 'LHASH_DOALL_ARG_FN_TYPE', 'mdc2_ctx_st', 'SSL3_STATE', 'ssl3_buffer_st', 'ASN1_ITEM_EXP', '_opaque_pthread_condattr_t', 'mode_t', 'ASN1_VALUE', 'qaddr_t', '__darwin_gid_t', 'EVP_PKEY', 'CRYPTO_EX_free', '_ossl_old_des_cblock', 'X509_INFO', 'asn1_string_st', 'intptr_t', 'UIT_INFO', 'int_fast8_t', 'sigaltstack', 'env_md_st', 'LHASH', '__darwin_ucontext_t', 'PKCS7_SIGN_ENVELOPE', '__darwin_mcontext_t', 'ct_rune_t', 'MD2_CTX', 'pthread_once_t', 'SSL3_BUFFER', 'fd_mask', 'ASN1_TYPE', 'PKCS7_SIGNED', 'ssl3_record_st', 'BF_KEY', 'MD4state_st', 'MD4_CTX', 'int16_t', 'SSL_CIPHER', 'rune_t', 'X509_TRUST', 'siginfo_t', 'X509_STORE', '__sbuf', 'X509_STORE_CTX', '__darwin_blksize_t', 'ldiv_t', 'ASN1_TIME', 'SSL_METHOD', 'X509_LOOKUP', 'Netscape_spki_st', 'P_PID', 'sigaction', 'sig_t', 'hostent', 'x509_cert_aux_st', '_opaque_pthread_cond_t', 'segsz_t', 'ushort', '__darwin_ct_rune_t', 'fd_set', 'BN_RECP_CTX', 'x509_lookup_st', 'uint16_t', 'pkcs7_st', 'asn1_header_st', '__darwin_pthread_key_t', 'x509_trust_st', '__darwin_pthread_handler_rec', 'int32_t', 'X509_CRL_INFO', 'N11evp_pkey_st4DOLLAR_12E', 'MDC2_CTX', 'N23_ossl_old_des_ks_struct4DOLLAR_10E', 'ASN1_HEADER', 'X509_crl_info_st', 'LHASH_HASH_FN_TYPE', '_opaque_pthread_mutexattr_t', 'ssl_st', 'N8pkcs7_st4DOLLAR_15E', 'evp_pkey_st', 'pkcs7_signedandenveloped_st', '__darwin_mach_port_t', 'EVP_PBE_KEYGEN', '_opaque_pthread_mutex_t', 'ASN1_UTCTIME', 'mcontext', 'crypto_ex_data_func_st', 'u_long', 'PBKDF2PARAM_st', 'rc4_key_st', 'DSA_METHOD', 'EVP_CIPHER', 'BIT_STRING_BITNAME', 'PKCS7_RECIP_INFO', 'ssl3_enc_method', 'X509_CERT_AUX', 'uintmax_t', 'int_fast16_t', 'RC5_32_KEY', 'ucontext64', 'ASN1_INTEGER', 'u_short', 'N14x509_object_st4DOLLAR_14E', 'mcontext64', 'X509_sig_st', 'ASN1_GENERALSTRING', 'PKCS7', '__sFILEX', 'X509_name_entry_st', 'ssl_session_st', 'caddr_t', 'bignum_st', 'X509_CINF', '__darwin_pthread_cond_t', 'ASN1_TLC', 'PKCS7_ENCRYPT', 'NETSCAPE_SPKAC', 'Netscape_spkac_st', 'idtype_t', 'UIT_ERROR', 'uint_fast64_t', 'in_addr_t', 'pthread_mutex_t', '__int64_t', 'ASN1_BMPSTRING', 'uint32_t', 'PEM_ENCODE_SEAL_CTX', 'suseconds_t', 'ASN1_OBJECT', 'X509_val_st', 'private_key_st', 'CRYPTO_dynlock', 'X509_objects_st', 'CRYPTO_EX_DATA_IMPL', 'pthread_condattr_t', 'PKCS7_DIGEST', 'uint_least32_t', 'ASN1_STRING', '__uint32_t', 'P_PGID', 'rsa_meth_st', 'X509_crl_st', 'RC2_KEY', '__darwin_fsfilcnt_t', 'X509_revoked_st', 'PBE2PARAM', 'blksize_t', 'Netscape_certificate_sequence', 'ssl_cipher_st', 'bignum_ctx', 'register_t', 'ASN1_UTF8STRING', 'pkcs7_encrypted_st', 'RC4_KEY', '__darwin_ucontext64_t', 'N13ssl2_state_st4DOLLAR_19E', 'bn_recp_ctx_st', 'CAST_KEY', 'X509_ATTRIBUTE', '__darwin_suseconds_t', '__sigaction', 'user_ulong_t', 'syscall_arg_t', 'evp_cipher_ctx_st', 'X509_ALGOR', 'mcontext_t', 'const_DES_cblock', '__darwin_fsblkcnt_t', 'dsa_st', 'int_least8_t', 'MD2state_st', 'X509_EXTENSION', 'GEN_SESSION_CB', 'int_least16_t', '__darwin_wctrans_t', 'PBKDF2PARAM', 'x509_lookup_method_st', 'pem_password_cb', 'X509_info_st', 'x509_store_st', '__darwin_natural_t', 'X509_pubkey_st', 'pkcs7_digest_st', '__darwin_size_t', 'ASN1_STRING_TABLE', 'OSLittleEndian', 'RIPEMD160state_st', 'pkcs7_enveloped_st', 'UI', 'ptrdiff_t', 'X509_REQ', 'CRYPTO_dynlock_value', 'X509_req_st', 'x509_store_ctx_st', 'N13ssl3_state_st4DOLLAR_20E', 'lhash_node_st', '__darwin_pthread_mutex_t', 'LHASH_COMP_FN_TYPE', '__darwin_rune_t', 'rlimit', '__darwin_pthread_once_t', 'OSBigEndian', 'uintptr_t', '__darwin_uid_t', 'u_int', 'ASN1_T61STRING', 'gid_t', 'ssl_method_st', 'ASN1_ITEM', 'ASN1_ENUMERATED', '_opaque_pthread_rwlock_t', 'pkcs8_priv_key_info_st', 'intmax_t', 'sigcontext', 'X509_CRL', 'rc2_key_st', 'engine_st', 'x509_object_st', '_opaque_pthread_once_t', 'DES_ks', 'SSL_COMP', 'dsa_method', 'int64_t', 'bio_st', 'bf_key_st', 'ASN1_GENERALIZEDTIME', 'PKCS7_ENC_CONTENT', '__darwin_pid_t', 'lldiv_t', 'comp_method_st', 'EVP_MD_CTX', 'evp_cipher_st', 'X509_name_st', 'x509_hash_dir_st', '__darwin_mach_port_name_t', 'useconds_t', 'user_size_t', 'SSL_SESSION', 'rusage', 'ssl_crock_st', 'int_least32_t', '__sigaction_u', 'dh_st', 'P_ALL', '__darwin_stack_t', 'N6DES_ks3DOLLAR_9E', 'comp_ctx_st', 'X509_CERT_FILE_CTX']
93,071
2,670
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/data/py3_test_grammar.py
# Python test set -- part 1, grammar. # This just tests whether the parser accepts them all. # NOTE: When you run this test as a script from the command line, you # get warnings about certain hex/oct constants. Since those are # issued by the parser, you can't suppress them by adding a # filterwarnings() call to this module. Therefore, to shut up the # regression test, the filterwarnings() call has been added to # regrtest.py. from test.support import run_unittest, check_syntax_error import unittest import sys # testing import * from sys import * class TokenTests(unittest.TestCase): def testBackslash(self): # Backslash means line continuation: x = 1 \ + 1 self.assertEquals(x, 2, 'backslash for line continuation') # Backslash does not means continuation in comments :\ x = 0 self.assertEquals(x, 0, 'backslash ending comment') def testPlainIntegers(self): self.assertEquals(type(000), type(0)) self.assertEquals(0xff, 255) self.assertEquals(0o377, 255) self.assertEquals(2147483647, 0o17777777777) self.assertEquals(0b1001, 9) # "0x" is not a valid literal self.assertRaises(SyntaxError, eval, "0x") from sys import maxsize if maxsize == 2147483647: self.assertEquals(-2147483647-1, -0o20000000000) # XXX -2147483648 self.assert_(0o37777777777 > 0) self.assert_(0xffffffff > 0) self.assert_(0b1111111111111111111111111111111 > 0) for s in ('2147483648', '0o40000000000', '0x100000000', '0b10000000000000000000000000000000'): try: x = eval(s) except OverflowError: self.fail("OverflowError on huge integer literal %r" % s) elif maxsize == 9223372036854775807: self.assertEquals(-9223372036854775807-1, -0o1000000000000000000000) self.assert_(0o1777777777777777777777 > 0) self.assert_(0xffffffffffffffff > 0) self.assert_(0b11111111111111111111111111111111111111111111111111111111111111 > 0) for s in '9223372036854775808', '0o2000000000000000000000', \ '0x10000000000000000', \ '0b100000000000000000000000000000000000000000000000000000000000000': try: x = eval(s) except OverflowError: self.fail("OverflowError on huge integer literal %r" % s) else: self.fail('Weird maxsize value %r' % maxsize) def testLongIntegers(self): x = 0 x = 0xffffffffffffffff x = 0Xffffffffffffffff x = 0o77777777777777777 x = 0O77777777777777777 x = 123456789012345678901234567890 x = 0b100000000000000000000000000000000000000000000000000000000000000000000 x = 0B111111111111111111111111111111111111111111111111111111111111111111111 def testUnderscoresInNumbers(self): # Integers x = 1_0 x = 123_456_7_89 x = 0xabc_123_4_5 x = 0X_abc_123 x = 0B11_01 x = 0b_11_01 x = 0o45_67 x = 0O_45_67 # Floats x = 3_1.4 x = 03_1.4 x = 3_1. x = .3_1 x = 3.1_4 x = 0_3.1_4 x = 3e1_4 x = 3_1e+4_1 x = 3_1E-4_1 def testFloats(self): x = 3.14 x = 314. x = 0.314 # XXX x = 000.314 x = .314 x = 3e14 x = 3E14 x = 3e-14 x = 3e+14 x = 3.e14 x = .3e14 x = 3.1e4 def testStringLiterals(self): x = ''; y = ""; self.assert_(len(x) == 0 and x == y) x = '\''; y = "'"; self.assert_(len(x) == 1 and x == y and ord(x) == 39) x = '"'; y = "\""; self.assert_(len(x) == 1 and x == y and ord(x) == 34) x = "doesn't \"shrink\" does it" y = 'doesn\'t "shrink" does it' self.assert_(len(x) == 24 and x == y) x = "does \"shrink\" doesn't it" y = 'does "shrink" doesn\'t it' self.assert_(len(x) == 24 and x == y) x = """ The "quick" brown fox jumps over the 'lazy' dog. """ y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n' self.assertEquals(x, y) y = ''' The "quick" brown fox jumps over the 'lazy' dog. ''' self.assertEquals(x, y) y = "\n\ The \"quick\"\n\ brown fox\n\ jumps over\n\ the 'lazy' dog.\n\ " self.assertEquals(x, y) y = '\n\ The \"quick\"\n\ brown fox\n\ jumps over\n\ the \'lazy\' dog.\n\ ' self.assertEquals(x, y) def testEllipsis(self): x = ... self.assert_(x is Ellipsis) self.assertRaises(SyntaxError, eval, ".. .") class GrammarTests(unittest.TestCase): # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE # XXX can't test in a script -- this rule is only used when interactive # file_input: (NEWLINE | stmt)* ENDMARKER # Being tested as this very moment this very module # expr_input: testlist NEWLINE # XXX Hard to test -- used only in calls to input() def testEvalInput(self): # testlist ENDMARKER x = eval('1, 0 or 1') def testFuncdef(self): ### [decorators] 'def' NAME parameters ['->' test] ':' suite ### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE ### decorators: decorator+ ### parameters: '(' [typedargslist] ')' ### typedargslist: ((tfpdef ['=' test] ',')* ### ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef) ### | tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) ### tfpdef: NAME [':' test] ### varargslist: ((vfpdef ['=' test] ',')* ### ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef) ### | vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) ### vfpdef: NAME def f1(): pass f1() f1(*()) f1(*(), **{}) def f2(one_argument): pass def f3(two, arguments): pass self.assertEquals(f2.__code__.co_varnames, ('one_argument',)) self.assertEquals(f3.__code__.co_varnames, ('two', 'arguments')) def a1(one_arg,): pass def a2(two, args,): pass def v0(*rest): pass def v1(a, *rest): pass def v2(a, b, *rest): pass f1() f2(1) f2(1,) f3(1, 2) f3(1, 2,) v0() v0(1) v0(1,) v0(1,2) v0(1,2,3,4,5,6,7,8,9,0) v1(1) v1(1,) v1(1,2) v1(1,2,3) v1(1,2,3,4,5,6,7,8,9,0) v2(1,2) v2(1,2,3) v2(1,2,3,4) v2(1,2,3,4,5,6,7,8,9,0) def d01(a=1): pass d01() d01(1) d01(*(1,)) d01(**{'a':2}) def d11(a, b=1): pass d11(1) d11(1, 2) d11(1, **{'b':2}) def d21(a, b, c=1): pass d21(1, 2) d21(1, 2, 3) d21(*(1, 2, 3)) d21(1, *(2, 3)) d21(1, 2, *(3,)) d21(1, 2, **{'c':3}) def d02(a=1, b=2): pass d02() d02(1) d02(1, 2) d02(*(1, 2)) d02(1, *(2,)) d02(1, **{'b':2}) d02(**{'a': 1, 'b': 2}) def d12(a, b=1, c=2): pass d12(1) d12(1, 2) d12(1, 2, 3) def d22(a, b, c=1, d=2): pass d22(1, 2) d22(1, 2, 3) d22(1, 2, 3, 4) def d01v(a=1, *rest): pass d01v() d01v(1) d01v(1, 2) d01v(*(1, 2, 3, 4)) d01v(*(1,)) d01v(**{'a':2}) def d11v(a, b=1, *rest): pass d11v(1) d11v(1, 2) d11v(1, 2, 3) def d21v(a, b, c=1, *rest): pass d21v(1, 2) d21v(1, 2, 3) d21v(1, 2, 3, 4) d21v(*(1, 2, 3, 4)) d21v(1, 2, **{'c': 3}) def d02v(a=1, b=2, *rest): pass d02v() d02v(1) d02v(1, 2) d02v(1, 2, 3) d02v(1, *(2, 3, 4)) d02v(**{'a': 1, 'b': 2}) def d12v(a, b=1, c=2, *rest): pass d12v(1) d12v(1, 2) d12v(1, 2, 3) d12v(1, 2, 3, 4) d12v(*(1, 2, 3, 4)) d12v(1, 2, *(3, 4, 5)) d12v(1, *(2,), **{'c': 3}) def d22v(a, b, c=1, d=2, *rest): pass d22v(1, 2) d22v(1, 2, 3) d22v(1, 2, 3, 4) d22v(1, 2, 3, 4, 5) d22v(*(1, 2, 3, 4)) d22v(1, 2, *(3, 4, 5)) d22v(1, *(2, 3), **{'d': 4}) # keyword argument type tests try: str('x', **{b'foo':1 }) except TypeError: pass else: self.fail('Bytes should not work as keyword argument names') # keyword only argument tests def pos0key1(*, key): return key pos0key1(key=100) def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2 pos2key2(1, 2, k1=100) pos2key2(1, 2, k1=100, k2=200) pos2key2(1, 2, k2=100, k1=200) def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200) pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100) # keyword arguments after *arglist def f(*args, **kwargs): return args, kwargs self.assertEquals(f(1, x=2, *[3, 4], y=5), ((1, 3, 4), {'x':2, 'y':5})) self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)") self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)") # argument annotation tests def f(x) -> list: pass self.assertEquals(f.__annotations__, {'return': list}) def f(x:int): pass self.assertEquals(f.__annotations__, {'x': int}) def f(*x:str): pass self.assertEquals(f.__annotations__, {'x': str}) def f(**x:float): pass self.assertEquals(f.__annotations__, {'x': float}) def f(x, y:1+2): pass self.assertEquals(f.__annotations__, {'y': 3}) def f(a, b:1, c:2, d): pass self.assertEquals(f.__annotations__, {'b': 1, 'c': 2}) def f(a, b:1, c:2, d, e:3=4, f=5, *g:6): pass self.assertEquals(f.__annotations__, {'b': 1, 'c': 2, 'e': 3, 'g': 6}) def f(a, b:1, c:2, d, e:3=4, f=5, *g:6, h:7, i=8, j:9=10, **k:11) -> 12: pass self.assertEquals(f.__annotations__, {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9, 'k': 11, 'return': 12}) # Check for SF Bug #1697248 - mixing decorators and a return annotation def null(x): return x @null def f(x) -> list: pass self.assertEquals(f.__annotations__, {'return': list}) # test closures with a variety of oparg's closure = 1 def f(): return closure def f(x=1): return closure def f(*, k=1): return closure def f() -> int: return closure # Check ast errors in *args and *kwargs check_syntax_error(self, "f(*g(1=2))") check_syntax_error(self, "f(**g(1=2))") def testLambdef(self): ### lambdef: 'lambda' [varargslist] ':' test l1 = lambda : 0 self.assertEquals(l1(), 0) l2 = lambda : a[d] # XXX just testing the expression l3 = lambda : [2 < x for x in [-1, 3, 0]] self.assertEquals(l3(), [0, 1, 0]) l4 = lambda x = lambda y = lambda z=1 : z : y() : x() self.assertEquals(l4(), 1) l5 = lambda x, y, z=2: x + y + z self.assertEquals(l5(1, 2), 5) self.assertEquals(l5(1, 2, 3), 6) check_syntax_error(self, "lambda x: x = 2") check_syntax_error(self, "lambda (None,): None") l6 = lambda x, y, *, k=20: x+y+k self.assertEquals(l6(1,2), 1+2+20) self.assertEquals(l6(1,2,k=10), 1+2+10) ### stmt: simple_stmt | compound_stmt # Tested below def testSimpleStmt(self): ### simple_stmt: small_stmt (';' small_stmt)* [';'] x = 1; pass; del x def foo(): # verify statements that end with semi-colons x = 1; pass; del x; foo() ### small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt # Tested below def testExprStmt(self): # (exprlist '=')* exprlist 1 1, 2, 3 x = 1 x = 1, 2, 3 x = y = z = 1, 2, 3 x, y, z = 1, 2, 3 abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4) check_syntax_error(self, "x + 1 = 1") check_syntax_error(self, "a + 1 = b + 2") def testDelStmt(self): # 'del' exprlist abc = [1,2,3] x, y, z = abc xyz = x, y, z del abc del x, y, (z, xyz) def testPassStmt(self): # 'pass' pass # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt # Tested below def testBreakStmt(self): # 'break' while 1: break def testContinueStmt(self): # 'continue' i = 1 while i: i = 0; continue msg = "" while not msg: msg = "ok" try: continue msg = "continue failed to continue inside try" except: msg = "continue inside try called except block" if msg != "ok": self.fail(msg) msg = "" while not msg: msg = "finally block not called" try: continue finally: msg = "ok" if msg != "ok": self.fail(msg) def test_break_continue_loop(self): # This test warrants an explanation. It is a test specifically for SF bugs # #463359 and #462937. The bug is that a 'break' statement executed or # exception raised inside a try/except inside a loop, *after* a continue # statement has been executed in that loop, will cause the wrong number of # arguments to be popped off the stack and the instruction pointer reset to # a very small number (usually 0.) Because of this, the following test # *must* written as a function, and the tracking vars *must* be function # arguments with default values. Otherwise, the test will loop and loop. def test_inner(extra_burning_oil = 1, count=0): big_hippo = 2 while big_hippo: count += 1 try: if extra_burning_oil and big_hippo == 1: extra_burning_oil -= 1 break big_hippo -= 1 continue except: raise if count > 2 or big_hippo != 1: self.fail("continue then break in try/except in loop broken!") test_inner() def testReturn(self): # 'return' [testlist] def g1(): return def g2(): return 1 g1() x = g2() check_syntax_error(self, "class foo:return 1") def testYield(self): check_syntax_error(self, "class foo:yield 1") def testRaise(self): # 'raise' test [',' test] try: raise RuntimeError('just testing') except RuntimeError: pass try: raise KeyboardInterrupt except KeyboardInterrupt: pass def testImport(self): # 'import' dotted_as_names import sys import time, sys # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names) from time import time from time import (time) # not testable inside a function, but already done at top of the module # from sys import * from sys import path, argv from sys import (path, argv) from sys import (path, argv,) def testGlobal(self): # 'global' NAME (',' NAME)* global a global a, b global one, two, three, four, five, six, seven, eight, nine, ten def testNonlocal(self): # 'nonlocal' NAME (',' NAME)* x = 0 y = 0 def f(): nonlocal x nonlocal x, y def testAssert(self): # assert_stmt: 'assert' test [',' test] assert 1 assert 1, 1 assert lambda x:x assert 1, lambda x:x+1 try: assert 0, "msg" except AssertionError as e: self.assertEquals(e.args[0], "msg") else: if __debug__: self.fail("AssertionError not raised by assert 0") ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef # Tested below def testIf(self): # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] if 1: pass if 1: pass else: pass if 0: pass elif 0: pass if 0: pass elif 0: pass elif 0: pass elif 0: pass else: pass def testWhile(self): # 'while' test ':' suite ['else' ':' suite] while 0: pass while 0: pass else: pass # Issue1920: "while 0" is optimized away, # ensure that the "else" clause is still present. x = 0 while 0: x = 1 else: x = 2 self.assertEquals(x, 2) def testFor(self): # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] for i in 1, 2, 3: pass for i, j, k in (): pass else: pass class Squares: def __init__(self, max): self.max = max self.sofar = [] def __len__(self): return len(self.sofar) def __getitem__(self, i): if not 0 <= i < self.max: raise IndexError n = len(self.sofar) while n <= i: self.sofar.append(n*n) n = n+1 return self.sofar[i] n = 0 for x in Squares(10): n = n+x if n != 285: self.fail('for over growing sequence') result = [] for x, in [(1,), (2,), (3,)]: result.append(x) self.assertEqual(result, [1, 2, 3]) def testTry(self): ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] ### | 'try' ':' suite 'finally' ':' suite ### except_clause: 'except' [expr ['as' expr]] try: 1/0 except ZeroDivisionError: pass else: pass try: 1/0 except EOFError: pass except TypeError as msg: pass except RuntimeError as msg: pass except: pass else: pass try: 1/0 except (EOFError, TypeError, ZeroDivisionError): pass try: 1/0 except (EOFError, TypeError, ZeroDivisionError) as msg: pass try: pass finally: pass def testSuite(self): # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT if 1: pass if 1: pass if 1: # # # pass pass # pass # def testTest(self): ### and_test ('or' and_test)* ### and_test: not_test ('and' not_test)* ### not_test: 'not' not_test | comparison if not 1: pass if 1 and 1: pass if 1 or 1: pass if not not not 1: pass if not 1 and 1 and 1: pass if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass def testComparison(self): ### comparison: expr (comp_op expr)* ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not' if 1: pass x = (1 == 1) if 1 == 1: pass if 1 != 1: pass if 1 < 1: pass if 1 > 1: pass if 1 <= 1: pass if 1 >= 1: pass if 1 is 1: pass if 1 is not 1: pass if 1 in (): pass if 1 not in (): pass if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass def testBinaryMaskOps(self): x = 1 & 1 x = 1 ^ 1 x = 1 | 1 def testShiftOps(self): x = 1 << 1 x = 1 >> 1 x = 1 << 1 >> 1 def testAdditiveOps(self): x = 1 x = 1 + 1 x = 1 - 1 - 1 x = 1 - 1 + 1 - 1 + 1 def testMultiplicativeOps(self): x = 1 * 1 x = 1 / 1 x = 1 % 1 x = 1 / 1 * 1 % 1 def testUnaryOps(self): x = +1 x = -1 x = ~1 x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 x = -1*1/1 + 1*1 - ---1*1 def testSelectors(self): ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME ### subscript: expr | [expr] ':' [expr] import sys, time c = sys.path[0] x = time.time() x = sys.modules['time'].time() a = '01234' c = a[0] c = a[-1] s = a[0:5] s = a[:5] s = a[0:] s = a[:] s = a[-5:] s = a[:-1] s = a[-4:-3] # A rough test of SF bug 1333982. http://python.org/sf/1333982 # The testing here is fairly incomplete. # Test cases should include: commas with 1 and 2 colons d = {} d[1] = 1 d[1,] = 2 d[1,2] = 3 d[1,2,3] = 4 L = list(d) L.sort(key=lambda x: x if isinstance(x, tuple) else ()) self.assertEquals(str(L), '[1, (1,), (1, 2), (1, 2, 3)]') def testAtoms(self): ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING ### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [',']) x = (1) x = (1 or 2 or 3) x = (1 or 2 or 3, 2, 3) x = [] x = [1] x = [1 or 2 or 3] x = [1 or 2 or 3, 2, 3] x = [] x = {} x = {'one': 1} x = {'one': 1,} x = {'one' or 'two': 1 or 2} x = {'one': 1, 'two': 2} x = {'one': 1, 'two': 2,} x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} x = {'one'} x = {'one', 1,} x = {'one', 'two', 'three'} x = {2, 3, 4,} x = x x = 'x' x = 123 ### exprlist: expr (',' expr)* [','] ### testlist: test (',' test)* [','] # These have been exercised enough above def testClassdef(self): # 'class' NAME ['(' [testlist] ')'] ':' suite class B: pass class B2(): pass class C1(B): pass class C2(B): pass class D(C1, C2, B): pass class C: def meth1(self): pass def meth2(self, arg): pass def meth3(self, a1, a2): pass # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE # decorators: decorator+ # decorated: decorators (classdef | funcdef) def class_decorator(x): return x @class_decorator class G: pass def testDictcomps(self): # dictorsetmaker: ( (test ':' test (comp_for | # (',' test ':' test)* [','])) | # (test (comp_for | (',' test)* [','])) ) nums = [1, 2, 3] self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4}) def testListcomps(self): # list comprehension tests nums = [1, 2, 3, 4, 5] strs = ["Apple", "Banana", "Coconut"] spcs = [" Apple", " Banana ", "Coco nut "] self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut']) self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15]) self.assertEqual([x for x in nums if x > 2], [3, 4, 5]) self.assertEqual([(i, s) for i in nums for s in strs], [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'), (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'), (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'), (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'), (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')]) self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]], [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), (5, 'Banana'), (5, 'Coconut')]) self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)], [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]]) def test_in_func(l): return [0 < x < 3 for x in l if x > 2] self.assertEqual(test_in_func(nums), [False, False, False]) def test_nested_front(): self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]], [[1, 2], [3, 4], [5, 6]]) test_nested_front() check_syntax_error(self, "[i, s for i in nums for s in strs]") check_syntax_error(self, "[x if y]") suppliers = [ (1, "Boeing"), (2, "Ford"), (3, "Macdonalds") ] parts = [ (10, "Airliner"), (20, "Engine"), (30, "Cheeseburger") ] suppart = [ (1, 10), (1, 20), (2, 20), (3, 30) ] x = [ (sname, pname) for (sno, sname) in suppliers for (pno, pname) in parts for (sp_sno, sp_pno) in suppart if sno == sp_sno and pno == sp_pno ] self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), ('Macdonalds', 'Cheeseburger')]) def testGenexps(self): # generator expression tests g = ([x for x in range(10)] for x in range(1)) self.assertEqual(next(g), [x for x in range(10)]) try: next(g) self.fail('should produce StopIteration exception') except StopIteration: pass a = 1 try: g = (a for d in a) next(g) self.fail('should produce TypeError') except TypeError: pass self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd']) self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy']) a = [x for x in range(10)] b = (x for x in (y for y in a)) self.assertEqual(sum(b), sum([x for x in range(10)])) self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)])) self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2])) self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)])) self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)])) self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)])) self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True)) if True), sum([x for x in range(10)])) self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0) check_syntax_error(self, "foo(x for x in range(10), 100)") check_syntax_error(self, "foo(100, x for x in range(10))") def testComprehensionSpecials(self): # test for outmost iterable precomputation x = 10; g = (i for i in range(x)); x = 5 self.assertEqual(len(list(g)), 10) # This should hold, since we're only precomputing outmost iterable. x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x)) x = 5; t = True; self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g)) # Grammar allows multiple adjacent 'if's in listcomps and genexps, # even though it's silly. Make sure it works (ifelse broke this.) self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7]) self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7]) # verify unpacking single element tuples in listcomp/genexp. self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6]) self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9]) def test_with_statement(self): class manager(object): def __enter__(self): return (1, 2) def __exit__(self, *args): pass with manager(): pass with manager() as x: pass with manager() as (x, y): pass with manager(), manager(): pass with manager() as x, manager() as y: pass with manager() as x, manager(): pass def testIfElseExpr(self): # Test ifelse expressions in various cases def _checkeval(msg, ret): "helper to check that evaluation of expressions is done correctly" print(x) return ret # the next line is not allowed anymore #self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True]) self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True]) self.assertEqual([ x(False) for x in (lambda x: False if x else True, lambda x: True if x else False) if x(False) ], [True]) self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5) self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5) self.assertEqual((5 and 6 if 0 else 1), 1) self.assertEqual(((5 and 6) if 0 else 1), 1) self.assertEqual((5 and (6 if 1 else 1)), 6) self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3) self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1) self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5) self.assertEqual((not 5 if 1 else 1), False) self.assertEqual((not 5 if 0 else 1), 1) self.assertEqual((6 + 1 if 1 else 2), 7) self.assertEqual((6 - 1 if 1 else 2), 5) self.assertEqual((6 * 2 if 1 else 4), 12) self.assertEqual((6 / 2 if 1 else 3), 3) self.assertEqual((6 < 4 if 0 else 2), 2) def test_main(): run_unittest(TokenTests, GrammarTests) if __name__ == '__main__': test_main()
30,777
946
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/data/py2_test_grammar.py
# Python test set -- part 1, grammar. # This just tests whether the parser accepts them all. # NOTE: When you run this test as a script from the command line, you # get warnings about certain hex/oct constants. Since those are # issued by the parser, you can't suppress them by adding a # filterwarnings() call to this module. Therefore, to shut up the # regression test, the filterwarnings() call has been added to # regrtest.py. from test.test_support import run_unittest, check_syntax_error import unittest import sys # testing import * from sys import * class TokenTests(unittest.TestCase): def testBackslash(self): # Backslash means line continuation: x = 1 \ + 1 self.assertEquals(x, 2, 'backslash for line continuation') # Backslash does not means continuation in comments :\ x = 0 self.assertEquals(x, 0, 'backslash ending comment') def testPlainIntegers(self): self.assertEquals(0xff, 255) self.assertEquals(0377, 255) self.assertEquals(2147483647, 017777777777) # "0x" is not a valid literal self.assertRaises(SyntaxError, eval, "0x") from sys import maxint if maxint == 2147483647: self.assertEquals(-2147483647-1, -020000000000) # XXX -2147483648 self.assert_(037777777777 > 0) self.assert_(0xffffffff > 0) for s in '2147483648', '040000000000', '0x100000000': try: x = eval(s) except OverflowError: self.fail("OverflowError on huge integer literal %r" % s) elif maxint == 9223372036854775807: self.assertEquals(-9223372036854775807-1, -01000000000000000000000) self.assert_(01777777777777777777777 > 0) self.assert_(0xffffffffffffffff > 0) for s in '9223372036854775808', '02000000000000000000000', \ '0x10000000000000000': try: x = eval(s) except OverflowError: self.fail("OverflowError on huge integer literal %r" % s) else: self.fail('Weird maxint value %r' % maxint) def testLongIntegers(self): x = 0L x = 0l x = 0xffffffffffffffffL x = 0xffffffffffffffffl x = 077777777777777777L x = 077777777777777777l x = 123456789012345678901234567890L x = 123456789012345678901234567890l def testFloats(self): x = 3.14 x = 314. x = 0.314 # XXX x = 000.314 x = .314 x = 3e14 x = 3E14 x = 3e-14 x = 3e+14 x = 3.e14 x = .3e14 x = 3.1e4 def testStringLiterals(self): x = ''; y = ""; self.assert_(len(x) == 0 and x == y) x = '\''; y = "'"; self.assert_(len(x) == 1 and x == y and ord(x) == 39) x = '"'; y = "\""; self.assert_(len(x) == 1 and x == y and ord(x) == 34) x = "doesn't \"shrink\" does it" y = 'doesn\'t "shrink" does it' self.assert_(len(x) == 24 and x == y) x = "does \"shrink\" doesn't it" y = 'does "shrink" doesn\'t it' self.assert_(len(x) == 24 and x == y) x = """ The "quick" brown fox jumps over the 'lazy' dog. """ y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n' self.assertEquals(x, y) y = ''' The "quick" brown fox jumps over the 'lazy' dog. ''' self.assertEquals(x, y) y = "\n\ The \"quick\"\n\ brown fox\n\ jumps over\n\ the 'lazy' dog.\n\ " self.assertEquals(x, y) y = '\n\ The \"quick\"\n\ brown fox\n\ jumps over\n\ the \'lazy\' dog.\n\ ' self.assertEquals(x, y) class GrammarTests(unittest.TestCase): # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE # XXX can't test in a script -- this rule is only used when interactive # file_input: (NEWLINE | stmt)* ENDMARKER # Being tested as this very moment this very module # expr_input: testlist NEWLINE # XXX Hard to test -- used only in calls to input() def testEvalInput(self): # testlist ENDMARKER x = eval('1, 0 or 1') def testFuncdef(self): ### 'def' NAME parameters ':' suite ### parameters: '(' [varargslist] ')' ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME] ### | ('**'|'*' '*') NAME) ### | fpdef ['=' test] (',' fpdef ['=' test])* [','] ### fpdef: NAME | '(' fplist ')' ### fplist: fpdef (',' fpdef)* [','] ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test) ### argument: [test '='] test # Really [keyword '='] test def f1(): pass f1() f1(*()) f1(*(), **{}) def f2(one_argument): pass def f3(two, arguments): pass def f4(two, (compound, (argument, list))): pass def f5((compound, first), two): pass self.assertEquals(f2.func_code.co_varnames, ('one_argument',)) self.assertEquals(f3.func_code.co_varnames, ('two', 'arguments')) if sys.platform.startswith('java'): self.assertEquals(f4.func_code.co_varnames, ('two', '(compound, (argument, list))', 'compound', 'argument', 'list',)) self.assertEquals(f5.func_code.co_varnames, ('(compound, first)', 'two', 'compound', 'first')) else: self.assertEquals(f4.func_code.co_varnames, ('two', '.1', 'compound', 'argument', 'list')) self.assertEquals(f5.func_code.co_varnames, ('.0', 'two', 'compound', 'first')) def a1(one_arg,): pass def a2(two, args,): pass def v0(*rest): pass def v1(a, *rest): pass def v2(a, b, *rest): pass def v3(a, (b, c), *rest): return a, b, c, rest f1() f2(1) f2(1,) f3(1, 2) f3(1, 2,) f4(1, (2, (3, 4))) v0() v0(1) v0(1,) v0(1,2) v0(1,2,3,4,5,6,7,8,9,0) v1(1) v1(1,) v1(1,2) v1(1,2,3) v1(1,2,3,4,5,6,7,8,9,0) v2(1,2) v2(1,2,3) v2(1,2,3,4) v2(1,2,3,4,5,6,7,8,9,0) v3(1,(2,3)) v3(1,(2,3),4) v3(1,(2,3),4,5,6,7,8,9,0) # ceval unpacks the formal arguments into the first argcount names; # thus, the names nested inside tuples must appear after these names. if sys.platform.startswith('java'): self.assertEquals(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c')) else: self.assertEquals(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c')) self.assertEquals(v3(1, (2, 3), 4), (1, 2, 3, (4,))) def d01(a=1): pass d01() d01(1) d01(*(1,)) d01(**{'a':2}) def d11(a, b=1): pass d11(1) d11(1, 2) d11(1, **{'b':2}) def d21(a, b, c=1): pass d21(1, 2) d21(1, 2, 3) d21(*(1, 2, 3)) d21(1, *(2, 3)) d21(1, 2, *(3,)) d21(1, 2, **{'c':3}) def d02(a=1, b=2): pass d02() d02(1) d02(1, 2) d02(*(1, 2)) d02(1, *(2,)) d02(1, **{'b':2}) d02(**{'a': 1, 'b': 2}) def d12(a, b=1, c=2): pass d12(1) d12(1, 2) d12(1, 2, 3) def d22(a, b, c=1, d=2): pass d22(1, 2) d22(1, 2, 3) d22(1, 2, 3, 4) def d01v(a=1, *rest): pass d01v() d01v(1) d01v(1, 2) d01v(*(1, 2, 3, 4)) d01v(*(1,)) d01v(**{'a':2}) def d11v(a, b=1, *rest): pass d11v(1) d11v(1, 2) d11v(1, 2, 3) def d21v(a, b, c=1, *rest): pass d21v(1, 2) d21v(1, 2, 3) d21v(1, 2, 3, 4) d21v(*(1, 2, 3, 4)) d21v(1, 2, **{'c': 3}) def d02v(a=1, b=2, *rest): pass d02v() d02v(1) d02v(1, 2) d02v(1, 2, 3) d02v(1, *(2, 3, 4)) d02v(**{'a': 1, 'b': 2}) def d12v(a, b=1, c=2, *rest): pass d12v(1) d12v(1, 2) d12v(1, 2, 3) d12v(1, 2, 3, 4) d12v(*(1, 2, 3, 4)) d12v(1, 2, *(3, 4, 5)) d12v(1, *(2,), **{'c': 3}) def d22v(a, b, c=1, d=2, *rest): pass d22v(1, 2) d22v(1, 2, 3) d22v(1, 2, 3, 4) d22v(1, 2, 3, 4, 5) d22v(*(1, 2, 3, 4)) d22v(1, 2, *(3, 4, 5)) d22v(1, *(2, 3), **{'d': 4}) def d31v((x)): pass d31v(1) def d32v((x,)): pass d32v((1,)) # keyword arguments after *arglist def f(*args, **kwargs): return args, kwargs self.assertEquals(f(1, x=2, *[3, 4], y=5), ((1, 3, 4), {'x':2, 'y':5})) self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)") self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)") # Check ast errors in *args and *kwargs check_syntax_error(self, "f(*g(1=2))") check_syntax_error(self, "f(**g(1=2))") def testLambdef(self): ### lambdef: 'lambda' [varargslist] ':' test l1 = lambda : 0 self.assertEquals(l1(), 0) l2 = lambda : a[d] # XXX just testing the expression l3 = lambda : [2 < x for x in [-1, 3, 0L]] self.assertEquals(l3(), [0, 1, 0]) l4 = lambda x = lambda y = lambda z=1 : z : y() : x() self.assertEquals(l4(), 1) l5 = lambda x, y, z=2: x + y + z self.assertEquals(l5(1, 2), 5) self.assertEquals(l5(1, 2, 3), 6) check_syntax_error(self, "lambda x: x = 2") check_syntax_error(self, "lambda (None,): None") ### stmt: simple_stmt | compound_stmt # Tested below def testSimpleStmt(self): ### simple_stmt: small_stmt (';' small_stmt)* [';'] x = 1; pass; del x def foo(): # verify statements that end with semi-colons x = 1; pass; del x; foo() ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt # Tested below def testExprStmt(self): # (exprlist '=')* exprlist 1 1, 2, 3 x = 1 x = 1, 2, 3 x = y = z = 1, 2, 3 x, y, z = 1, 2, 3 abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4) check_syntax_error(self, "x + 1 = 1") check_syntax_error(self, "a + 1 = b + 2") def testPrintStmt(self): # 'print' (test ',')* [test] import StringIO # Can't test printing to real stdout without comparing output # which is not available in unittest. save_stdout = sys.stdout sys.stdout = StringIO.StringIO() print 1, 2, 3 print 1, 2, 3, print print 0 or 1, 0 or 1, print 0 or 1 # 'print' '>>' test ',' print >> sys.stdout, 1, 2, 3 print >> sys.stdout, 1, 2, 3, print >> sys.stdout print >> sys.stdout, 0 or 1, 0 or 1, print >> sys.stdout, 0 or 1 # test printing to an instance class Gulp: def write(self, msg): pass gulp = Gulp() print >> gulp, 1, 2, 3 print >> gulp, 1, 2, 3, print >> gulp print >> gulp, 0 or 1, 0 or 1, print >> gulp, 0 or 1 # test print >> None def driver(): oldstdout = sys.stdout sys.stdout = Gulp() try: tellme(Gulp()) tellme() finally: sys.stdout = oldstdout # we should see this once def tellme(file=sys.stdout): print >> file, 'hello world' driver() # we should not see this at all def tellme(file=None): print >> file, 'goodbye universe' driver() self.assertEqual(sys.stdout.getvalue(), '''\ 1 2 3 1 2 3 1 1 1 1 2 3 1 2 3 1 1 1 hello world ''') sys.stdout = save_stdout # syntax errors check_syntax_error(self, 'print ,') check_syntax_error(self, 'print >> x,') def testDelStmt(self): # 'del' exprlist abc = [1,2,3] x, y, z = abc xyz = x, y, z del abc del x, y, (z, xyz) def testPassStmt(self): # 'pass' pass # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt # Tested below def testBreakStmt(self): # 'break' while 1: break def testContinueStmt(self): # 'continue' i = 1 while i: i = 0; continue msg = "" while not msg: msg = "ok" try: continue msg = "continue failed to continue inside try" except: msg = "continue inside try called except block" if msg != "ok": self.fail(msg) msg = "" while not msg: msg = "finally block not called" try: continue finally: msg = "ok" if msg != "ok": self.fail(msg) def test_break_continue_loop(self): # This test warrants an explanation. It is a test specifically for SF bugs # #463359 and #462937. The bug is that a 'break' statement executed or # exception raised inside a try/except inside a loop, *after* a continue # statement has been executed in that loop, will cause the wrong number of # arguments to be popped off the stack and the instruction pointer reset to # a very small number (usually 0.) Because of this, the following test # *must* written as a function, and the tracking vars *must* be function # arguments with default values. Otherwise, the test will loop and loop. def test_inner(extra_burning_oil = 1, count=0): big_hippo = 2 while big_hippo: count += 1 try: if extra_burning_oil and big_hippo == 1: extra_burning_oil -= 1 break big_hippo -= 1 continue except: raise if count > 2 or big_hippo <> 1: self.fail("continue then break in try/except in loop broken!") test_inner() def testReturn(self): # 'return' [testlist] def g1(): return def g2(): return 1 g1() x = g2() check_syntax_error(self, "class foo:return 1") def testYield(self): check_syntax_error(self, "class foo:yield 1") def testRaise(self): # 'raise' test [',' test] try: raise RuntimeError, 'just testing' except RuntimeError: pass try: raise KeyboardInterrupt except KeyboardInterrupt: pass def testImport(self): # 'import' dotted_as_names import sys import time, sys # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names) from time import time from time import (time) # not testable inside a function, but already done at top of the module # from sys import * from sys import path, argv from sys import (path, argv) from sys import (path, argv,) def testGlobal(self): # 'global' NAME (',' NAME)* global a global a, b global one, two, three, four, five, six, seven, eight, nine, ten def testExec(self): # 'exec' expr ['in' expr [',' expr]] z = None del z exec 'z=1+1\n' if z != 2: self.fail('exec \'z=1+1\'\\n') del z exec 'z=1+1' if z != 2: self.fail('exec \'z=1+1\'') z = None del z import types if hasattr(types, "UnicodeType"): exec r"""if 1: exec u'z=1+1\n' if z != 2: self.fail('exec u\'z=1+1\'\\n') del z exec u'z=1+1' if z != 2: self.fail('exec u\'z=1+1\'')""" g = {} exec 'z = 1' in g if g.has_key('__builtins__'): del g['__builtins__'] if g != {'z': 1}: self.fail('exec \'z = 1\' in g') g = {} l = {} import warnings warnings.filterwarnings("ignore", "global statement", module="<string>") exec 'global a; a = 1; b = 2' in g, l if g.has_key('__builtins__'): del g['__builtins__'] if l.has_key('__builtins__'): del l['__builtins__'] if (g, l) != ({'a':1}, {'b':2}): self.fail('exec ... in g (%s), l (%s)' %(g,l)) def testAssert(self): # assert_stmt: 'assert' test [',' test] assert 1 assert 1, 1 assert lambda x:x assert 1, lambda x:x+1 try: assert 0, "msg" except AssertionError, e: self.assertEquals(e.args[0], "msg") else: if __debug__: self.fail("AssertionError not raised by assert 0") ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef # Tested below def testIf(self): # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] if 1: pass if 1: pass else: pass if 0: pass elif 0: pass if 0: pass elif 0: pass elif 0: pass elif 0: pass else: pass def testWhile(self): # 'while' test ':' suite ['else' ':' suite] while 0: pass while 0: pass else: pass # Issue1920: "while 0" is optimized away, # ensure that the "else" clause is still present. x = 0 while 0: x = 1 else: x = 2 self.assertEquals(x, 2) def testFor(self): # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] for i in 1, 2, 3: pass for i, j, k in (): pass else: pass class Squares: def __init__(self, max): self.max = max self.sofar = [] def __len__(self): return len(self.sofar) def __getitem__(self, i): if not 0 <= i < self.max: raise IndexError n = len(self.sofar) while n <= i: self.sofar.append(n*n) n = n+1 return self.sofar[i] n = 0 for x in Squares(10): n = n+x if n != 285: self.fail('for over growing sequence') result = [] for x, in [(1,), (2,), (3,)]: result.append(x) self.assertEqual(result, [1, 2, 3]) def testTry(self): ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] ### | 'try' ':' suite 'finally' ':' suite ### except_clause: 'except' [expr [('as' | ',') expr]] try: 1/0 except ZeroDivisionError: pass else: pass try: 1/0 except EOFError: pass except TypeError as msg: pass except RuntimeError, msg: pass except: pass else: pass try: 1/0 except (EOFError, TypeError, ZeroDivisionError): pass try: 1/0 except (EOFError, TypeError, ZeroDivisionError), msg: pass try: pass finally: pass def testSuite(self): # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT if 1: pass if 1: pass if 1: # # # pass pass # pass # def testTest(self): ### and_test ('or' and_test)* ### and_test: not_test ('and' not_test)* ### not_test: 'not' not_test | comparison if not 1: pass if 1 and 1: pass if 1 or 1: pass if not not not 1: pass if not 1 and 1 and 1: pass if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass def testComparison(self): ### comparison: expr (comp_op expr)* ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' if 1: pass x = (1 == 1) if 1 == 1: pass if 1 != 1: pass if 1 <> 1: pass if 1 < 1: pass if 1 > 1: pass if 1 <= 1: pass if 1 >= 1: pass if 1 is 1: pass if 1 is not 1: pass if 1 in (): pass if 1 not in (): pass if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass def testBinaryMaskOps(self): x = 1 & 1 x = 1 ^ 1 x = 1 | 1 def testShiftOps(self): x = 1 << 1 x = 1 >> 1 x = 1 << 1 >> 1 def testAdditiveOps(self): x = 1 x = 1 + 1 x = 1 - 1 - 1 x = 1 - 1 + 1 - 1 + 1 def testMultiplicativeOps(self): x = 1 * 1 x = 1 / 1 x = 1 % 1 x = 1 / 1 * 1 % 1 def testUnaryOps(self): x = +1 x = -1 x = ~1 x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 x = -1*1/1 + 1*1 - ---1*1 def testSelectors(self): ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME ### subscript: expr | [expr] ':' [expr] import sys, time c = sys.path[0] x = time.time() x = sys.modules['time'].time() a = '01234' c = a[0] c = a[-1] s = a[0:5] s = a[:5] s = a[0:] s = a[:] s = a[-5:] s = a[:-1] s = a[-4:-3] # A rough test of SF bug 1333982. http://python.org/sf/1333982 # The testing here is fairly incomplete. # Test cases should include: commas with 1 and 2 colons d = {} d[1] = 1 d[1,] = 2 d[1,2] = 3 d[1,2,3] = 4 L = list(d) L.sort() self.assertEquals(str(L), '[1, (1,), (1, 2), (1, 2, 3)]') def testAtoms(self): ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING ### dictmaker: test ':' test (',' test ':' test)* [','] x = (1) x = (1 or 2 or 3) x = (1 or 2 or 3, 2, 3) x = [] x = [1] x = [1 or 2 or 3] x = [1 or 2 or 3, 2, 3] x = [] x = {} x = {'one': 1} x = {'one': 1,} x = {'one' or 'two': 1 or 2} x = {'one': 1, 'two': 2} x = {'one': 1, 'two': 2,} x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} x = `x` x = `1 or 2 or 3` self.assertEqual(`1,2`, '(1, 2)') x = x x = 'x' x = 123 ### exprlist: expr (',' expr)* [','] ### testlist: test (',' test)* [','] # These have been exercised enough above def testClassdef(self): # 'class' NAME ['(' [testlist] ')'] ':' suite class B: pass class B2(): pass class C1(B): pass class C2(B): pass class D(C1, C2, B): pass class C: def meth1(self): pass def meth2(self, arg): pass def meth3(self, a1, a2): pass # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE # decorators: decorator+ # decorated: decorators (classdef | funcdef) def class_decorator(x): x.decorated = True return x @class_decorator class G: pass self.assertEqual(G.decorated, True) def testListcomps(self): # list comprehension tests nums = [1, 2, 3, 4, 5] strs = ["Apple", "Banana", "Coconut"] spcs = [" Apple", " Banana ", "Coco nut "] self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut']) self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15]) self.assertEqual([x for x in nums if x > 2], [3, 4, 5]) self.assertEqual([(i, s) for i in nums for s in strs], [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'), (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'), (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'), (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'), (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')]) self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]], [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), (5, 'Banana'), (5, 'Coconut')]) self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)], [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]]) def test_in_func(l): return [None < x < 3 for x in l if x > 2] self.assertEqual(test_in_func(nums), [False, False, False]) def test_nested_front(): self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]], [[1, 2], [3, 4], [5, 6]]) test_nested_front() check_syntax_error(self, "[i, s for i in nums for s in strs]") check_syntax_error(self, "[x if y]") suppliers = [ (1, "Boeing"), (2, "Ford"), (3, "Macdonalds") ] parts = [ (10, "Airliner"), (20, "Engine"), (30, "Cheeseburger") ] suppart = [ (1, 10), (1, 20), (2, 20), (3, 30) ] x = [ (sname, pname) for (sno, sname) in suppliers for (pno, pname) in parts for (sp_sno, sp_pno) in suppart if sno == sp_sno and pno == sp_pno ] self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), ('Macdonalds', 'Cheeseburger')]) def testGenexps(self): # generator expression tests g = ([x for x in range(10)] for x in range(1)) self.assertEqual(g.next(), [x for x in range(10)]) try: g.next() self.fail('should produce StopIteration exception') except StopIteration: pass a = 1 try: g = (a for d in a) g.next() self.fail('should produce TypeError') except TypeError: pass self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd']) self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy']) a = [x for x in range(10)] b = (x for x in (y for y in a)) self.assertEqual(sum(b), sum([x for x in range(10)])) self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)])) self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2])) self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)])) self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)])) self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)])) self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True)) if True), sum([x for x in range(10)])) self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0) check_syntax_error(self, "foo(x for x in range(10), 100)") check_syntax_error(self, "foo(100, x for x in range(10))") def testComprehensionSpecials(self): # test for outmost iterable precomputation x = 10; g = (i for i in range(x)); x = 5 self.assertEqual(len(list(g)), 10) # This should hold, since we're only precomputing outmost iterable. x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x)) x = 5; t = True; self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g)) # Grammar allows multiple adjacent 'if's in listcomps and genexps, # even though it's silly. Make sure it works (ifelse broke this.) self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7]) self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7]) # verify unpacking single element tuples in listcomp/genexp. self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6]) self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9]) def test_with_statement(self): class manager(object): def __enter__(self): return (1, 2) def __exit__(self, *args): pass with manager(): pass with manager() as x: pass with manager() as (x, y): pass with manager(), manager(): pass with manager() as x, manager() as y: pass with manager() as x, manager(): pass def testIfElseExpr(self): # Test ifelse expressions in various cases def _checkeval(msg, ret): "helper to check that evaluation of expressions is done correctly" print x return ret self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True]) self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True]) self.assertEqual([ x(False) for x in (lambda x: False if x else True, lambda x: True if x else False) if x(False) ], [True]) self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5) self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5) self.assertEqual((5 and 6 if 0 else 1), 1) self.assertEqual(((5 and 6) if 0 else 1), 1) self.assertEqual((5 and (6 if 1 else 1)), 6) self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3) self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1) self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5) self.assertEqual((not 5 if 1 else 1), False) self.assertEqual((not 5 if 0 else 1), 1) self.assertEqual((6 + 1 if 1 else 2), 7) self.assertEqual((6 - 1 if 1 else 2), 5) self.assertEqual((6 * 2 if 1 else 4), 12) self.assertEqual((6 / 2 if 1 else 3), 3) self.assertEqual((6 < 4 if 0 else 2), 2) def test_main(): run_unittest(TokenTests, GrammarTests) if __name__ == '__main__': test_main()
30,980
975
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/data/fixers/no_fixer_cls.py
# This is empty so trying to fetch the fixer class gives an AttributeError
75
2
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/data/fixers/bad_order.py
from lib2to3.fixer_base import BaseFix class FixBadOrder(BaseFix): order = "crazy"
89
6
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/data/fixers/parrot_example.py
def parrot(): pass
23
3
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/data/fixers/myfixes/fix_last.py
from lib2to3.fixer_base import BaseFix class FixLast(BaseFix): run_order = 10 def match(self, node): return False
125
8
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/data/fixers/myfixes/fix_parrot.py
from lib2to3.fixer_base import BaseFix from lib2to3.fixer_util import Name class FixParrot(BaseFix): """ Change functions named 'parrot' to 'cheese'. """ PATTERN = """funcdef < 'def' name='parrot' any* >""" def transform(self, node, results): name = results["name"] name.replace(Name("cheese", name.prefix))
347
14
jart/cosmopolitan
false
cosmopolitan/third_party/python/Lib/lib2to3/tests/data/fixers/myfixes/fix_explicit.py
from lib2to3.fixer_base import BaseFix class FixExplicit(BaseFix): explicit = True def match(self): return False
123
7
jart/cosmopolitan
false