repo_name
stringlengths
6
100
path
stringlengths
4
294
copies
stringlengths
1
5
size
stringlengths
4
6
content
stringlengths
606
896k
license
stringclasses
15 values
Arzaroth/python_rapidxml
tests/test_basic.py
1
5638
#!/usr/bin/env python # -*- coding: utf-8 -*- # # File: simple.py # by Arzaroth Lekva # arzaroth@arzaroth.com # import os import rapidxml def test_unparse(init_rapidxml): assert init_rapidxml.unparse() == ('<root><test attr1="one" attr2="two" attr3="three"/>' '<test2><node id="1"/><node id="2"/><node id="3"/></test2>' '<test>some text</test></root>') assert init_rapidxml.unparse() == repr(init_rapidxml) assert init_rapidxml.unparse(False, False) == repr(init_rapidxml) assert init_rapidxml.unparse(raw=False) == repr(init_rapidxml) assert init_rapidxml.unparse(pretty=False) == repr(init_rapidxml) assert init_rapidxml.unparse(pretty=False, raw=False) == repr(init_rapidxml) assert init_rapidxml.unparse(True) == str(init_rapidxml) assert init_rapidxml.unparse(True, False) == str(init_rapidxml) assert init_rapidxml.unparse(pretty=True) == str(init_rapidxml) assert init_rapidxml.unparse(pretty=True, raw=False) == str(init_rapidxml) assert init_rapidxml.unparse(True, raw=False) == str(init_rapidxml) def test_parse(init_rapidxml): r = rapidxml.RapidXml() try: data = init_rapidxml.unparse().encode('utf-8') except UnicodeDecodeError: data = init_rapidxml.unparse() r.parse(data) assert str(r) == str(init_rapidxml) def test_parse_from_file(init_rapidxml, tmpdir): f = tmpdir.join("dump.xml") f.write(init_rapidxml.unparse()) r = rapidxml.RapidXml(str(f), from_file=True) assert str(r) == str(init_rapidxml) def test_equals(init_rapidxml): assert init_rapidxml == init_rapidxml root = init_rapidxml.first_node() assert root == root assert root == init_rapidxml.first_node() assert root.first_node() != root.first_node("test2") assert (root != root) == (not (root == root)) def test_parent(init_rapidxml): assert init_rapidxml.parent is None assert init_rapidxml.first_node().parent == init_rapidxml def test_assign(init_rapidxml): root = init_rapidxml.first_node() root.name = "new_root" assert root.name == "new_root" test = root.first_node() test.name = "new_test" test.first_attribute().name = "new_attr1" test.first_attribute().next_attribute().value = "new_two" test = root.first_node("test") test.value = "some new text" assert test.value == "some new text" assert init_rapidxml.unparse() == ('<new_root><new_test new_attr1="one" attr2="new_two" attr3="three"/>' '<test2><node id="1"/><node id="2"/><node id="3"/></test2>' '<test>some new text</test></new_root>') def test_init_cdata(init_rapidxml_with_CDADA): datra_str =('<root><test attr1="one" attr2="two" attr3="three"/>' '<test2><node id="1"/><node id="2"/><node id="3"/></test2>' '<test>some text</test>' "<ns2:AdditionalData><ns2:Data TID=\"AD_1\">" "<![CDATA[{\"Cart\":{\"expirationTime\":\"2017-04-22T09:40\"," "\"id\":\"b469df3b-f626-4fe3-898c-825373e546a2\",\"products\":[\"1223\"]," "\"creationTime\":\"2017-04-21T09:40\",\"totalPrice\":" "{\"currencyCode\":\"EUR\",\"amount\":\"138.000\"}}}]]>" "</ns2:Data></ns2:AdditionalData></root>") assert init_rapidxml_with_CDADA.unparse() == rapidxml.RapidXml(datra_str, from_file=False, attribute_prefix='@', cdata_key='#text', always_aslist=False, parse_cdata=True).unparse() assert init_rapidxml_with_CDADA.unparse() == repr(init_rapidxml_with_CDADA) assert init_rapidxml_with_CDADA.unparse(True) == str(init_rapidxml_with_CDADA) def test_parse_cdata(init_rapidxml_with_CDADA): r = rapidxml.RapidXml() try: data = init_rapidxml_with_CDADA.unparse().encode('utf-8') except UnicodeDecodeError: data = init_rapidxml_with_CDADA.unparse() r.parse(data, from_file=False, parse_cdata=True) assert str(r) == str(init_rapidxml_with_CDADA) def test_parse_from_file_cdata(init_rapidxml_with_CDADA, tmpdir): f = tmpdir.join("dump.xml") f.write(init_rapidxml_with_CDADA.unparse()) r = rapidxml.RapidXml(str(f), from_file=True, parse_cdata=True) assert str(r) == str(init_rapidxml_with_CDADA) def test_equals_cdata(init_rapidxml_with_CDADA): assert init_rapidxml_with_CDADA == init_rapidxml_with_CDADA root = init_rapidxml_with_CDADA.first_node() assert root == root assert root == init_rapidxml_with_CDADA.first_node() assert root.first_node() != root.first_node("test2") assert (root != root) == (not (root == root)) def test_parent_cdata(init_rapidxml_with_CDADA): assert init_rapidxml_with_CDADA.parent is None assert init_rapidxml_with_CDADA.first_node().parent == init_rapidxml_with_CDADA def test_assign_cdata(init_rapidxml_with_CDADA): root = init_rapidxml_with_CDADA.first_node() root.name = "new_root" assert root.name == "new_root" test = root.first_node() test.name = "new_test" test.first_attribute().name = "new_attr1" test.first_attribute().next_attribute().value = "new_two" test = root.first_node("test") test.value = "some new text" assert test.value == "some new text"
mit
henrysher/duplicity
setup.py
1
7472
#!/usr/bin/env python2 # -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- # # Copyright 2002 Ben Escoto <ben@emerose.org> # Copyright 2007 Kenneth Loafman <kenneth@loafman.com> # # This file is part of duplicity. # # Duplicity is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation; either version 2 of the License, or (at your # option) any later version. # # Duplicity is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with duplicity; if not, write to the Free Software Foundation, # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import sys import os from setuptools import setup, Extension from setuptools.command.test import test from setuptools.command.install import install from setuptools.command.sdist import sdist from distutils.command.build_scripts import build_scripts version_string = "$version" if sys.version_info[:2] < (2, 7) or sys.version_info[:2] > (2, 7): print("Sorry, duplicity requires version 2.7 of python.") sys.exit(1) incdir_list = libdir_list = None if os.name == 'posix': LIBRSYNC_DIR = os.environ.get('LIBRSYNC_DIR', '') args = sys.argv[:] for arg in args: if arg.startswith('--librsync-dir='): LIBRSYNC_DIR = arg.split('=')[1] sys.argv.remove(arg) if LIBRSYNC_DIR: incdir_list = [os.path.join(LIBRSYNC_DIR, 'include')] libdir_list = [os.path.join(LIBRSYNC_DIR, 'lib')] data_files = [('share/man/man1', ['bin/duplicity.1', 'bin/rdiffdir.1']), ('share/doc/duplicity-%s' % version_string, ['COPYING', 'README', 'README-REPO', 'README-LOG', 'CHANGELOG']), ] top_dir = os.path.dirname(os.path.abspath(__file__)) assert os.path.exists(os.path.join(top_dir, "po")), "Missing 'po' directory." for root, dirs, files in os.walk(os.path.join(top_dir, "po")): for file in files: path = os.path.join(root, file) if path.endswith("duplicity.mo"): lang = os.path.split(root)[-1] data_files.append( ('share/locale/%s/LC_MESSAGES' % lang, ["po/%s/duplicity.mo" % lang])) if not os.environ.get('READTHEDOCS') == 'True': ext_modules=[Extension("duplicity._librsync", ["duplicity/_librsyncmodule.c"], include_dirs=incdir_list, library_dirs=libdir_list, libraries=["rsync"])] else: ext_modules = [] class TestCommand(test): def run(self): # Make sure all modules are ready build_cmd = self.get_finalized_command("build_py") build_cmd.run() # And make sure our scripts are ready build_scripts_cmd = self.get_finalized_command("build_scripts") build_scripts_cmd.run() # make symlinks for test data if build_cmd.build_lib != top_dir: for path in ['testfiles.tar.gz', 'gnupg']: src = os.path.join(top_dir, 'testing', path) target = os.path.join(build_cmd.build_lib, 'testing', path) try: os.symlink(src, target) except Exception: pass os.environ['PATH'] = "%s:%s" % ( os.path.abspath(build_scripts_cmd.build_dir), os.environ.get('PATH')) test.run(self) class InstallCommand(install): def run(self): # Normally, install will call build(). But we want to delete the # testing dir between building and installing. So we manually build # and mark ourselves to skip building when we run() for real. self.run_command('build') self.skip_build = True # This should always be true, but just to make sure! if self.build_lib != top_dir: testing_dir = os.path.join(self.build_lib, 'testing') os.system("rm -rf %s" % testing_dir) install.run(self) # TODO: move logic from dist/makedist inline class SDistCommand(sdist): def run(self): version = version_string if version[0] == '$': version = "0.0dev" os.system(os.path.join(top_dir, "dist", "makedist") + " " + version) os.system("mkdir -p " + self.dist_dir) os.system("mv duplicity-" + version + ".tar.gz " + self.dist_dir) # don't touch my shebang class BSCommand (build_scripts): def run(self): """ Copy, chmod each script listed in 'self.scripts' essentially this is the stripped distutils.command.build_scripts.copy_scripts() routine """ from stat import ST_MODE from distutils.dep_util import newer from distutils import log self.mkpath(self.build_dir) outfiles = [] for script in self.scripts: outfile = os.path.join(self.build_dir, os.path.basename(script)) outfiles.append(outfile) if not self.force and not newer(script, outfile): log.debug("not copying %s (up-to-date)", script) continue log.info("copying and NOT adjusting %s -> %s", script, self.build_dir) self.copy_file(script, outfile) if os.name == 'posix': for file in outfiles: if self.dry_run: log.info("changing mode of %s", file) else: oldmode = os.stat(file)[ST_MODE] & 0o7777 newmode = (oldmode | 0o555) & 0o7777 if newmode != oldmode: log.info("changing mode of %s from %o to %o", file, oldmode, newmode) os.chmod(file, newmode) setup(name="duplicity", version=version_string, description="Encrypted backup using rsync algorithm", author="Ben Escoto <ben@emerose.org>", author_email="bescoto@stanford.edu", maintainer="Kenneth Loafman <kenneth@loafman.com>", maintainer_email="kenneth@loafman.com", url="http://duplicity.nongnu.org/index.html", packages=['duplicity', 'duplicity.backends', 'duplicity.backends.pyrax_identity', 'testing', 'testing.functional', 'testing.overrides', 'testing.unit'], package_dir={"duplicity": "duplicity", "duplicity.backends": "duplicity/backends", }, ext_modules=ext_modules, scripts=['bin/rdiffdir', 'bin/duplicity'], data_files=data_files, setup_requires=['pytest-runner'], install_requires=['fasteners', 'future'], tests_require=['pytest','fasteners', 'mock', 'pexpect'], test_suite='testing', cmdclass={'test': TestCommand, 'install': InstallCommand, 'sdist': SDistCommand, 'build_scripts': BSCommand}, classifiers=["Programming Language :: Python :: 2 :: Only", "Programming Language :: Python :: 2.7"] )
gpl-2.0
wndias/bc.repository
script.module.youtube.dl/lib/youtube_dl/extractor/bigflix.py
18
2676
# coding: utf-8 from __future__ import unicode_literals import base64 import re from .common import InfoExtractor from ..compat import compat_urllib_parse_unquote class BigflixIE(InfoExtractor): _VALID_URL = r'https?://(?:www\.)?bigflix\.com/.+/(?P<id>[0-9]+)' _TESTS = [{ 'url': 'http://www.bigflix.com/Hindi-movies/Action-movies/Singham-Returns/16537', 'md5': 'ec76aa9b1129e2e5b301a474e54fab74', 'info_dict': { 'id': '16537', 'ext': 'mp4', 'title': 'Singham Returns', 'description': 'md5:3d2ba5815f14911d5cc6a501ae0cf65d', } }, { # 2 formats 'url': 'http://www.bigflix.com/Tamil-movies/Drama-movies/Madarasapatinam/16070', 'info_dict': { 'id': '16070', 'ext': 'mp4', 'title': 'Madarasapatinam', 'description': 'md5:63b9b8ed79189c6f0418c26d9a3452ca', 'formats': 'mincount:2', }, 'params': { 'skip_download': True, } }, { # multiple formats 'url': 'http://www.bigflix.com/Malayalam-movies/Drama-movies/Indian-Rupee/15967', 'only_matching': True, }] def _real_extract(self, url): video_id = self._match_id(url) webpage = self._download_webpage(url, video_id) title = self._html_search_regex( r'<div[^>]+class=["\']pagetitle["\'][^>]*>(.+?)</div>', webpage, 'title') def decode_url(quoted_b64_url): return base64.b64decode(compat_urllib_parse_unquote( quoted_b64_url).encode('ascii')).decode('utf-8') formats = [] for height, encoded_url in re.findall( r'ContentURL_(\d{3,4})[pP][^=]+=([^&]+)', webpage): video_url = decode_url(encoded_url) f = { 'url': video_url, 'format_id': '%sp' % height, 'height': int(height), } if video_url.startswith('rtmp'): f['ext'] = 'flv' formats.append(f) file_url = self._search_regex( r'file=([^&]+)', webpage, 'video url', default=None) if file_url: video_url = decode_url(file_url) if all(f['url'] != video_url for f in formats): formats.append({ 'url': decode_url(file_url), }) self._sort_formats(formats) description = self._html_search_meta('description', webpage) return { 'id': video_id, 'title': title, 'description': description, 'formats': formats }
gpl-2.0
OpenNetworkingFoundation/PIF-Open-Intermediate-Representation
pif_ir/bir/tests/test_common.py
1
1166
# single BIRStruct description yaml_eth_struct_dict = { 'type' : 'struct', 'fields' : [ {'dst' : 48}, {'src' : 48}, {'type_' : 16} ] } yaml_udp_struct_dict = { 'type' : 'struct', 'fields' : [ {'sport' : 16}, {'dport' : 16}, {'len' : 16}, {'chksum' : 16} ] } yaml_req_struct_dict = { 'type' : 'struct', 'fields' : [ {'type_' : 16} ] } yaml_resp_struct_dict = { 'type' : 'struct', 'fields' : [ {'hit' : 1}, {'p4_action' : 2}, {'action_0_arg0' : 16}, {'action_1_arg0' : 16} ] } # single MetadataInstance description yaml_eth_meta_dict = { 'type' : 'metadata', 'values' : 'eth_t', 'visibility' : 'inout' } yaml_req_meta_dict = { 'type' : 'metadata', 'values' : 'req_t', 'visibility' : 'inout' } yaml_resp_meta_dict = { 'type' : 'metadata', 'values' : 'resp_t', 'visibility' : 'inout' } # single Table description yaml_table_dict = { 'type' : 'table', 'match_type' : 'ternary', 'depth' : 64, 'request' : 'req_t', 'response' : 'resp_t', 'operations' : None }
apache-2.0
bratsche/Neutron-Drive
google_appengine/lib/django_1_3/django/db/models/query_utils.py
240
5799
""" Various data structures used in query construction. Factored out from django.db.models.query to avoid making the main module very large and/or so that they can be used by other modules without getting into circular import difficulties. """ import weakref from django.utils.copycompat import deepcopy from django.db.backends import util from django.utils import tree from django.utils.datastructures import SortedDict class InvalidQuery(Exception): """ The query passed to raw isn't a safe query to use with raw. """ pass class QueryWrapper(object): """ A type that indicates the contents are an SQL fragment and the associate parameters. Can be used to pass opaque data to a where-clause, for example. """ def __init__(self, sql, params): self.data = sql, params def as_sql(self, qn=None, connection=None): return self.data class Q(tree.Node): """ Encapsulates filters as objects that can then be combined logically (using & and |). """ # Connection types AND = 'AND' OR = 'OR' default = AND def __init__(self, *args, **kwargs): super(Q, self).__init__(children=list(args) + kwargs.items()) def _combine(self, other, conn): if not isinstance(other, Q): raise TypeError(other) obj = type(self)() obj.add(self, conn) obj.add(other, conn) return obj def __or__(self, other): return self._combine(other, self.OR) def __and__(self, other): return self._combine(other, self.AND) def __invert__(self): obj = type(self)() obj.add(self, self.AND) obj.negate() return obj class DeferredAttribute(object): """ A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed. """ def __init__(self, field_name, model): self.field_name = field_name self.model_ref = weakref.ref(model) self.loaded = False def __get__(self, instance, owner): """ Retrieves and caches the value from the datastore on the first lookup. Returns the cached value. """ from django.db.models.fields import FieldDoesNotExist assert instance is not None cls = self.model_ref() data = instance.__dict__ if data.get(self.field_name, self) is self: # self.field_name is the attname of the field, but only() takes the # actual name, so we need to translate it here. try: cls._meta.get_field_by_name(self.field_name) name = self.field_name except FieldDoesNotExist: name = [f.name for f in cls._meta.fields if f.attname == self.field_name][0] # We use only() instead of values() here because we want the # various data coersion methods (to_python(), etc.) to be called # here. val = getattr( cls._base_manager.filter(pk=instance.pk).only(name).using( instance._state.db).get(), self.field_name ) data[self.field_name] = val return data[self.field_name] def __set__(self, instance, value): """ Deferred loading attributes can be set normally (which means there will never be a database lookup involved. """ instance.__dict__[self.field_name] = value def select_related_descend(field, restricted, requested, reverse=False): """ Returns True if this field should be used to descend deeper for select_related() purposes. Used by both the query construction code (sql.query.fill_related_selections()) and the model instance creation code (query.get_cached_row()). Arguments: * field - the field to be checked * restricted - a boolean field, indicating if the field list has been manually restricted using a requested clause) * requested - The select_related() dictionary. * reverse - boolean, True if we are checking a reverse select related """ if not field.rel: return False if field.rel.parent_link and not reverse: return False if restricted: if reverse and field.related_query_name() not in requested: return False if not reverse and field.name not in requested: return False if not restricted and field.null: return False return True # This function is needed because data descriptors must be defined on a class # object, not an instance, to have any effect. def deferred_class_factory(model, attrs): """ Returns a class object that is a copy of "model" with the specified "attrs" being replaced with DeferredAttribute objects. The "pk_value" ties the deferred attributes to a particular instance of the model. """ class Meta: proxy = True app_label = model._meta.app_label # The app_cache wants a unique name for each model, otherwise the new class # won't be created (we get an old one back). Therefore, we generate the # name using the passed in attrs. It's OK to reuse an existing class # object if the attrs are identical. name = "%s_Deferred_%s" % (model.__name__, '_'.join(sorted(list(attrs)))) name = util.truncate_name(name, 80, 32) overrides = dict([(attr, DeferredAttribute(attr, model)) for attr in attrs]) overrides["Meta"] = Meta overrides["__module__"] = model.__module__ overrides["_deferred"] = True return type(name, (model,), overrides) # The above function is also used to unpickle model instances with deferred # fields. deferred_class_factory.__safe_for_unpickling__ = True
bsd-3-clause
modsy/incubator-airflow
airflow/www/blueprints.py
5
1143
# -*- coding: utf-8 -*- # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from flask import ( url_for, Markup, Blueprint, redirect, ) import chartkick import markdown # Init for chartkick, the python wrapper for highcharts ck = Blueprint( 'ck_page', __name__, static_folder=chartkick.js(), static_url_path='/static') routes = Blueprint('routes', __name__) @routes.route('/') def index(): return redirect(url_for('admin.index')) @routes.route('/health') def health(): """ We can add an array of tests here to check the server's health """ content = Markup(markdown.markdown("The server is healthy!")) return content
apache-2.0
cloudbase/neutron-virtualbox
neutron/tests/post_mortem_debug.py
72
4237
# Copyright 2013 Red Hat, Inc. # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import functools import traceback def get_exception_handler(debugger_name): debugger = _get_debugger(debugger_name) return functools.partial(_exception_handler, debugger) def _get_debugger(debugger_name): try: debugger = __import__(debugger_name) except ImportError: raise ValueError("can't import %s module as a post mortem debugger" % debugger_name) if 'post_mortem' in dir(debugger): return debugger else: raise ValueError("%s is not a supported post mortem debugger" % debugger_name) def _exception_handler(debugger, exc_info): """Exception handler enabling post-mortem debugging. A class extending testtools.TestCase can add this handler in setUp(): self.addOnException(post_mortem_debug.exception_handler) When an exception occurs, the user will be dropped into a debugger session in the execution environment of the failure. Frames associated with the testing framework are excluded so that the post-mortem session for an assertion failure will start at the assertion call (e.g. self.assertTrue) rather than the framework code that raises the failure exception (e.g. the assertTrue method). """ tb = exc_info[2] ignored_traceback = get_ignored_traceback(tb) if ignored_traceback: tb = FilteredTraceback(tb, ignored_traceback) traceback.print_exception(exc_info[0], exc_info[1], tb) debugger.post_mortem(tb) def get_ignored_traceback(tb): """Retrieve the first traceback of an ignored trailing chain. Given an initial traceback, find the first traceback of a trailing chain of tracebacks that should be ignored. The criteria for whether a traceback should be ignored is whether its frame's globals include the __unittest marker variable. This criteria is culled from: unittest.TestResult._is_relevant_tb_level For example: tb.tb_next => tb0.tb_next => tb1.tb_next - If no tracebacks were to be ignored, None would be returned. - If only tb1 was to be ignored, tb1 would be returned. - If tb0 and tb1 were to be ignored, tb0 would be returned. - If either of only tb or only tb0 was to be ignored, None would be returned because neither tb or tb0 would be part of a trailing chain of ignored tracebacks. """ # Turn the traceback chain into a list tb_list = [] while tb: tb_list.append(tb) tb = tb.tb_next # Find all members of an ignored trailing chain ignored_tracebacks = [] for tb in reversed(tb_list): if '__unittest' in tb.tb_frame.f_globals: ignored_tracebacks.append(tb) else: break # Return the first member of the ignored trailing chain if ignored_tracebacks: return ignored_tracebacks[-1] class FilteredTraceback(object): """Wraps a traceback to filter unwanted frames.""" def __init__(self, tb, filtered_traceback): """Constructor. :param tb: The start of the traceback chain to filter. :param filtered_traceback: The first traceback of a trailing chain that is to be filtered. """ self._tb = tb self.tb_lasti = self._tb.tb_lasti self.tb_lineno = self._tb.tb_lineno self.tb_frame = self._tb.tb_frame self._filtered_traceback = filtered_traceback @property def tb_next(self): tb_next = self._tb.tb_next if tb_next and tb_next != self._filtered_traceback: return FilteredTraceback(tb_next, self._filtered_traceback)
apache-2.0
specter119/custodian
custodian/feff/handlers.py
1
4398
# coding: utf-8 from __future__ import unicode_literals, division from custodian.custodian import ErrorHandler import re from custodian.utils import backup from pymatgen.io.feff.sets import FEFFDictSet from custodian.feff.interpreter import FeffModder import logging """ This module implements specific error handler for FEFF runs. """ __author__ = "Chen Zheng" __copyright__ = "Copyright 2012, The Materials Project" __version__ = "0.1" __maintainer__ = "Chen Zheng" __email__ = "chz022@ucsd.edu" __date__ = "Oct 18, 2017" FEFF_BACKUP_FILES = ["ATOMS", "HEADER", "PARAMETERS", "POTENTIALS", "feff.inp", "*.cif", "pot.bin"] logger = logging.getLogger(__name__) class UnconvergedErrorHandler(ErrorHandler): """ Correct the unconverged error of FEFF's SCF calculation. """ is_monitor = False def __init__(self, output_filename='log1.dat'): """ Initializes the handler with the output file to check Args: output_filename (str): Filename for the log1.dat file. log1.dat file contains the SCF calculation convergence information. Change this only if it is different from the default (unlikely). """ self.output_filename = output_filename def check(self): """ If the FEFF run does not converge, the check will return "TRUE" """ return self._notconverge_check() def _notconverge_check(self): # Process the output file and get converge information not_converge_pattern = re.compile("Convergence not reached.*") converge_pattern = re.compile('Convergence reached.*') for _, line in enumerate(open(self.output_filename)): if len(not_converge_pattern.findall(line)) > 0: return True elif len(converge_pattern.findall(line)) > 0: return False def correct(self): backup(FEFF_BACKUP_FILES) feff_input = FEFFDictSet.from_directory(".") scf_values = feff_input.tags.get("SCF") nscmt = scf_values[2] ca = scf_values[3] nmix = scf_values[4] actions = [] #Add RESTART card to PARAMETERS if not "RESTART" in feff_input.tags: actions.append({"dict": "PARAMETERS", "action": {"_set": {"RESTART": []}}}) if nscmt < 100 and ca == 0.2: scf_values[2] = 100 scf_values[4] = 3 # Set nmix = 3 actions.append({"dict": "PARAMETERS", "action": {"_set": {"SCF": scf_values}}}) FeffModder().apply_actions(actions) return {"errors": ["Non-converging job"], "actions": actions} elif nscmt == 100 and nmix == 3 and ca > 0.01: # Reduce the convergence accelerator factor scf_values[3] = round(ca / 2, 2) actions.append({"dict": "PARAMETERS", "action": {"_set": {"SCF": scf_values}}}) FeffModder().apply_actions(actions) return {"errors": ["Non-converging job"], "actions": actions} elif nmix == 3 and ca == 0.01: # Set ca = 0.05 and set nmix scf_values[3] = 0.05 scf_values[4] = 5 actions.append({"dict": "PARAMETERS", "action": {"_set": {"SCF": scf_values}}}) FeffModder().apply_actions(actions) return {"errors": ["Non-converging job"], "actions": actions} elif nmix == 5 and ca == 0.05: # Set ca = 0.05 and set nmix scf_values[3] = 0.05 scf_values[4] = 10 actions.append({"dict": "PARAMETERS", "action": {"_set": {"SCF": scf_values}}}) FeffModder().apply_actions(actions) return {"errors": ["Non-converging job"], "actions": actions} elif nmix == 10 and ca < 0.2: # loop through ca with nmix = 10 scf_values[3] = round(ca * 2, 2) actions.append({"dict": "PARAMETERS", "action": {"_set": {"SCF": scf_values}}}) FeffModder().apply_actions(actions) return {"errors": ["Non-converging job"], "actions": actions} # Unfixable error. Just return None for actions. else: return {"errors": ["Non-converging job"], "actions": None}
mit
ActiveState/code
recipes/Python/271607_fiber_scheduler/recipe-271607.py
1
5269
import sys, select, time, socket, traceback class SEND: def __init__( self, sock, timeout ): self.fileno = sock.fileno() self.expire = time.time() + timeout def __str__( self ): return 'SEND(%i,%s)' % ( self.fileno, time.strftime( '%H:%M:%S', time.localtime( self.expire ) ) ) class RECV: def __init__( self, sock, timeout ): self.fileno = sock.fileno() self.expire = time.time() + timeout def __str__( self ): return 'RECV(%i,%s)' % ( self.fileno, time.strftime( '%H:%M:%S', time.localtime( self.expire ) ) ) class WAIT: def __init__( self, timeout = None ): self.expire = timeout and time.time() + timeout or None def __str__( self ): return 'WAIT(%s)' % ( self.expire and time.strftime( '%H:%M:%S', time.localtime( self.expire ) ) ) class Fiber: def __init__( self, generator ): self.__generator = generator self.state = WAIT() def step( self, throw=None ): self.state = None try: if throw: assert hasattr( self.__generator, 'throw' ), throw self.__generator.throw( AssertionError, throw ) state = self.__generator.next() assert isinstance( state, (SEND, RECV, WAIT) ), 'invalid waiting state %r' % state self.state = state except KeyboardInterrupt: raise except StopIteration: del self.__generator pass except AssertionError, msg: print 'Error:', msg except: traceback.print_exc() def __repr__( self ): return '%i: %s' % ( self.__generator.gi_frame.f_lineno, self.state ) class GatherFiber( Fiber ): def __init__( self, generator ): Fiber.__init__( self, generator ) self.__chunks = [ '[ 0.00 ] %s\n' % time.ctime() ] self.__start = time.time() self.__newline = True def step( self, throw=None ): stdout = sys.stdout stderr = sys.stderr try: sys.stdout = sys.stderr = self Fiber.step( self, throw ) finally: sys.stdout = stdout sys.stderr = stderr def write( self, string ): if self.__newline: self.__chunks.append( '%6.2f ' % ( time.time() - self.__start ) ) self.__chunks.append( string ) self.__newline = string.endswith( '\n' ) def __del__( self ): sys.stdout.writelines( self.__chunks ) if not self.__newline: sys.stdout.write( '\n' ) class DebugFiber( Fiber ): id = 0 def __init__( self, generator ): Fiber.__init__( self, generator ) self.__id = DebugFiber.id sys.stdout.write( '[ %04X ] %s\n' % ( self.__id, time.ctime() ) ) self.__newline = True self.__stdout = sys.stdout DebugFiber.id = ( self.id + 1 ) % 65535 def step( self, throw=None ): stdout = sys.stdout stderr = sys.stderr try: sys.stdout = sys.stderr = self Fiber.step( self, throw ) if self.state: print 'Waiting at', self finally: sys.stdout = stdout sys.stderr = stderr def write( self, string ): if self.__newline: self.__stdout.write( ' %04X ' % self.__id ) self.__stdout.write( string ) self.__newline = string.endswith( '\n' ) def spawn( generator, port, debug ): try: listener = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) listener.setblocking( 0 ) listener.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, listener.getsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR ) | 1 ) listener.bind( ( '', port ) ) listener.listen( 5 ) except Exception, e: print 'error: failed to create socket:', e return False if debug: myFiber = DebugFiber else: myFiber = GatherFiber print ' .... Server started' try: fibers = [] while True: tryrecv = { listener.fileno(): None } trysend = {} expire = None now = time.time() i = len( fibers ) while i: i -= 1 state = fibers[ i ].state if state and now > state.expire: if isinstance( state, WAIT ): fibers[ i ].step() else: fibers[ i ].step( throw='connection timed out' ) state = fibers[ i ].state if not state: del fibers[ i ] continue if isinstance( state, RECV ): tryrecv[ state.fileno ] = fibers[ i ] elif isinstance( state, SEND ): trysend[ state.fileno ] = fibers[ i ] elif state.expire is None: continue if state.expire < expire or expire is None: expire = state.expire if expire is None: print '[ IDLE ]', time.ctime() sys.stdout.flush() canrecv, cansend, dummy = select.select( tryrecv, trysend, [] ) print '[ BUSY ]', time.ctime() sys.stdout.flush() else: canrecv, cansend, dummy = select.select( tryrecv, trysend, [], max( expire - now, 0 ) ) for fileno in canrecv: if fileno is listener.fileno(): fibers.append( myFiber( generator( *listener.accept() ) ) ) else: tryrecv[ fileno ].step() for fileno in cansend: trysend[ fileno ].step() except KeyboardInterrupt: print ' .... Server terminated' return True except: print ' .... Server crashed' traceback.print_exc( file=sys.stdout ) return False
mit
GdZ/scriptfile
software/googleAppEngine/lib/django_1_4/tests/regressiontests/servers/tests.py
24
7504
""" Tests for django.core.servers. """ import os from urlparse import urljoin import urllib2 import django from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.test import TestCase, LiveServerTestCase from django.core.handlers.wsgi import WSGIHandler from django.core.servers.basehttp import AdminMediaHandler, WSGIServerException from django.test.utils import override_settings from .models import Person class AdminMediaHandlerTests(TestCase): def setUp(self): self.admin_media_url = urljoin(settings.STATIC_URL, 'admin/') self.admin_media_file_path = os.path.abspath( os.path.join(django.__path__[0], 'contrib', 'admin', 'static', 'admin') ) self.handler = AdminMediaHandler(WSGIHandler()) def test_media_urls(self): """ Tests that URLs that look like absolute file paths after the settings.STATIC_URL don't turn into absolute file paths. """ # Cases that should work on all platforms. data = ( ('%scss/base.css' % self.admin_media_url, ('css', 'base.css')), ) # Cases that should raise an exception. bad_data = () # Add platform-specific cases. if os.sep == '/': data += ( # URL, tuple of relative path parts. ('%s\\css/base.css' % self.admin_media_url, ('\\css', 'base.css')), ) bad_data += ( '%s/css/base.css' % self.admin_media_url, '%s///css/base.css' % self.admin_media_url, '%s../css/base.css' % self.admin_media_url, ) elif os.sep == '\\': bad_data += ( '%sC:\css/base.css' % self.admin_media_url, '%s/\\css/base.css' % self.admin_media_url, '%s\\css/base.css' % self.admin_media_url, '%s\\\\css/base.css' % self.admin_media_url ) for url, path_tuple in data: try: output = self.handler.file_path(url) except ValueError: self.fail("Got a ValueError exception, but wasn't expecting" " one. URL was: %s" % url) rel_path = os.path.join(*path_tuple) desired = os.path.join(self.admin_media_file_path, rel_path) self.assertEqual( os.path.normcase(output), os.path.normcase(desired), "Got: %s, Expected: %s, URL was: %s" % (output, desired, url)) for url in bad_data: try: output = self.handler.file_path(url) except ValueError: continue self.fail('URL: %s should have caused a ValueError exception.' % url) TEST_ROOT = os.path.dirname(__file__) TEST_SETTINGS = { 'MEDIA_URL': '/media/', 'MEDIA_ROOT': os.path.join(TEST_ROOT, 'media'), 'STATIC_URL': '/static/', 'STATIC_ROOT': os.path.join(TEST_ROOT, 'static'), } class LiveServerBase(LiveServerTestCase): urls = 'regressiontests.servers.urls' fixtures = ['testdata.json'] @classmethod def setUpClass(cls): # Override settings cls.settings_override = override_settings(**TEST_SETTINGS) cls.settings_override.enable() super(LiveServerBase, cls).setUpClass() @classmethod def tearDownClass(cls): # Restore original settings cls.settings_override.disable() super(LiveServerBase, cls).tearDownClass() def urlopen(self, url): return urllib2.urlopen(self.live_server_url + url) class LiveServerAddress(LiveServerBase): """ Ensure that the address set in the environment variable is valid. Refs #2879. """ @classmethod def setUpClass(cls): # Backup original environment variable address_predefined = 'DJANGO_LIVE_TEST_SERVER_ADDRESS' in os.environ old_address = os.environ.get('DJANGO_LIVE_TEST_SERVER_ADDRESS') # Just the host is not accepted cls.raises_exception('localhost', ImproperlyConfigured) # The host must be valid cls.raises_exception('blahblahblah:8081', WSGIServerException) # The list of ports must be in a valid format cls.raises_exception('localhost:8081,', ImproperlyConfigured) cls.raises_exception('localhost:8081,blah', ImproperlyConfigured) cls.raises_exception('localhost:8081-', ImproperlyConfigured) cls.raises_exception('localhost:8081-blah', ImproperlyConfigured) cls.raises_exception('localhost:8081-8082-8083', ImproperlyConfigured) # If contrib.staticfiles isn't configured properly, the exception # should bubble up to the main thread. old_STATIC_URL = TEST_SETTINGS['STATIC_URL'] TEST_SETTINGS['STATIC_URL'] = None cls.raises_exception('localhost:8081', ImproperlyConfigured) TEST_SETTINGS['STATIC_URL'] = old_STATIC_URL # Restore original environment variable if address_predefined: os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = old_address else: del os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] @classmethod def raises_exception(cls, address, exception): os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = address try: super(LiveServerAddress, cls).setUpClass() raise Exception("The line above should have raised an exception") except exception: pass def test_test_test(self): # Intentionally empty method so that the test is picked up by the # test runner and the overriden setUpClass() method is executed. pass class LiveServerViews(LiveServerBase): def test_404(self): """ Ensure that the LiveServerTestCase serves 404s. Refs #2879. """ try: self.urlopen('/') except urllib2.HTTPError, err: self.assertEquals(err.code, 404, 'Expected 404 response') else: self.fail('Expected 404 response') def test_view(self): """ Ensure that the LiveServerTestCase serves views. Refs #2879. """ f = self.urlopen('/example_view/') self.assertEquals(f.read(), 'example view') def test_static_files(self): """ Ensure that the LiveServerTestCase serves static files. Refs #2879. """ f = self.urlopen('/static/example_static_file.txt') self.assertEquals(f.read(), 'example static file\n') def test_media_files(self): """ Ensure that the LiveServerTestCase serves media files. Refs #2879. """ f = self.urlopen('/media/example_media_file.txt') self.assertEquals(f.read(), 'example media file\n') class LiveServerDatabase(LiveServerBase): def test_fixtures_loaded(self): """ Ensure that fixtures are properly loaded and visible to the live server thread. Refs #2879. """ f = self.urlopen('/model_view/') self.assertEquals(f.read().splitlines(), ['jane', 'robert']) def test_database_writes(self): """ Ensure that data written to the database by a view can be read. Refs #2879. """ self.urlopen('/create_model_instance/') names = [person.name for person in Person.objects.all()] self.assertEquals(names, ['jane', 'robert', 'emily'])
mit
pombredanne/pyjs
examples/showcase/src/demos_panels/flexTable.py
13
2925
""" The ``ui.FlexTable`` class implements a table that can have different numbers of cells in each row, and single cells can span multiple rows and columns. Each FlexTable has a ``FlexCellFormatter`` which you can use to format the cells in the table. The ``FlexCellFormatter`` has methods to set the row or column spans for a cell, as well as change the cell alignment, as shown below. Note that if you use row or column spanning, the cells on the rest of that row or column will be moved over. This can cause some surprising results. Imagine that you have a table like this: +---+---+---+ | A | B | C | +---+---+---+ | D | E | F | +---+---+---+ If you set up Cell 0,0 to span two columns, like this: flexTable.getFlexCellFormatter().setColSpan(0, 0, 2) This will cause the table to end up looking like this: +-------+---+---+ | A | B | C | +---+---+---+---+ | D | E | F | +---+---+---+ you might expect cell B to be above cell E, but to make this happen you need to place cell E at (1, 2) rather than (1, 1). Each FlexTable also has a ``RowFormatter`` which can be used to change style names, attributes, and the visibility of rows in the table. """ from pyjamas.ui.SimplePanel import SimplePanel from pyjamas.ui.FlexTable import FlexTable from pyjamas.ui import HasAlignment from pyjamas.ui.Button import Button class FlexTableDemo(SimplePanel): def __init__(self): SimplePanel.__init__(self) self._table = FlexTable(BorderWidth=1, Width="100%") cellFormatter = self._table.getFlexCellFormatter() rowFormatter = self._table.getRowFormatter() self._table.setHTML(0, 0, "<b>Mammals</b>") self._table.setText(1, 0, "Cow") self._table.setText(1, 1, "Rat") self._table.setText(1, 2, "Dog") cellFormatter.setColSpan(0, 0, 3) cellFormatter.setHorizontalAlignment(0, 0, HasAlignment.ALIGN_CENTER) self._table.setWidget(2, 0, Button("Hide", getattr(self, "hideRows"))) self._table.setText(2, 1, "1,1") self._table.setText(2, 2, "2,1") self._table.setText(3, 0, "1,2") self._table.setText(3, 1, "2,2") cellFormatter.setRowSpan(2, 0, 2) cellFormatter.setVerticalAlignment(2, 0, HasAlignment.ALIGN_MIDDLE) self._table.setWidget(4, 0, Button("Show", getattr(self, "showRows"))) cellFormatter.setColSpan(4, 0, 3) rowFormatter.setVisible(4, False) self.add(self._table) def hideRows(self, sender): rowFormatter = self._table.getRowFormatter() rowFormatter.setVisible(2, False) rowFormatter.setVisible(3, False) rowFormatter.setVisible(4, True) def showRows(self, sender): rowFormatter = self._table.getRowFormatter() rowFormatter.setVisible(2, True) rowFormatter.setVisible(3, True) rowFormatter.setVisible(4, False)
apache-2.0
theflofly/tensorflow
tensorflow/compiler/xla/python_api/xla_shape.py
36
5288
# Copyright 2018 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the 'License'); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an 'AS IS' BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ====================================== """XLA Shape utilities.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import numpy as _np # Avoids becoming a part of public Tensorflow API. from six.moves import xrange from tensorflow.compiler.xla import xla_data_pb2 from tensorflow.compiler.xla.python_api import types class Shape(object): """Wraps a xla_data_pb2.ShapeProto message with a convenient Python type. Provides direct access to the underlying xla_data_pb2.ShapeProto message in the message attribute, along with accessor wrappers to the message's fields. Avoid direct access to .message unless interacting directly with protobuf APIs like CopyFrom. In other words, prefer hauling the shape around in a Shape, and only access .message when strictly required by the protobuf API. """ def __init__(self, element_type, dimensions, layout=None): """Creates a new XLA Shape. Args: element_type: element type from xla_data_pb2. dimensions: sequence of dimensions sizes (integers), or sequence of Shapes in the case of a tuple, i.e. when element_type is TUPLE. layout: optional minor_to_major sequence for layout. If not given, the default major-to-minor layout is used. Raises: ValueError: if element_type is TUPLE but dimensions are not Shape objects. """ self.message = xla_data_pb2.ShapeProto() self.message.element_type = element_type if element_type == xla_data_pb2.TUPLE: if not all(isinstance(subshape, Shape) for subshape in dimensions): raise ValueError( 'XLA tuple requires sequence of Shape objects as dimensions') self._tuple_shapes = tuple(dimensions) for component_shape in self._tuple_shapes: component_message = self.message.tuple_shapes.add() component_message.CopyFrom(component_shape.message) else: self.message.dimensions.extend(dimensions) if layout is None: layout = list(reversed(range(len(dimensions)))) self.message.layout.format = xla_data_pb2.DENSE self.message.layout.minor_to_major.extend(layout) def element_type(self): return self.message.element_type def is_tuple(self): return self.element_type() == xla_data_pb2.TUPLE def dimensions(self): if self.is_tuple(): raise ValueError('Tuple shape has no dimensions. Try tuple_shapes()?') return self.message.dimensions def tuple_shapes(self): """If this is a tuple, returns its sequence of constituent Shape objects. Returns: Tuple sub-shapes. Raises: ValueError: if this is not a tuple. """ if not self.is_tuple(): raise ValueError('tuple_shapes() called on a non-tuple shape') return self._tuple_shapes def layout(self): return self.message.layout @staticmethod def from_pyval(pyval): return CreateShapeFromNumpy(pyval) def _CreateShapeFromNumpy(ndarray): # pylint: disable=invalid-name """Create a Shape from a given Numpy array. Args: ndarray: Numpy array. Returns: A Shape object. """ element_type = types.MAP_DTYPE_TO_RECORD[str(ndarray.dtype)].primitive_type dimensions = ndarray.shape # Set the shape's layout based on the ordering of ndarray. # Numpy arrays come in two orders: Fortran (column-major) and C (row-major). if _np.isfortran(ndarray): # Column-major layout. This corresponds to a "dimension order is # minor-to-major" layout in XLA. layout = range(ndarray.ndim) else: # Row-major layout. This corresponds to a "dimension order is # major-to-minor" layout int XLA. layout = list(reversed(xrange(ndarray.ndim))) return Shape(element_type, dimensions, layout) def CreateShapeFromNumpy(value): # pylint: disable=invalid-name """Create a Shape from a Numpy array or a nested tuple structure thereof. Args: value: Numpy array or (possibly nested) tuple structure that bottoms out in Numpy arrays. Returns: A Shape object. """ if isinstance(value, tuple): return Shape( xla_data_pb2.TUPLE, [CreateShapeFromNumpy(component) for component in value]) else: return _CreateShapeFromNumpy(value) def CreateShapeFromDtypeAndTuple(dtype, shape_tuple): # pylint: disable=invalid-name """Create a shape from a Numpy dtype and a sequence of nonnegative integers. Args: dtype: a numpy dtype, e.g. np.dtype('int32'). shape_tuple: a sequence of nonnegative integers. Returns: A Shape object. """ element_type = types.MAP_DTYPE_TO_RECORD[str(dtype)].primitive_type return Shape(element_type, shape_tuple)
apache-2.0
rodrigc/buildbot
master/buildbot/steps/shell.py
3
22571
# This file is part of Buildbot. Buildbot is free software: you can # redistribute it and/or modify it under the terms of the GNU General Public # License as published by the Free Software Foundation, version 2. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # details. # # You should have received a copy of the GNU General Public License along with # this program; if not, write to the Free Software Foundation, Inc., 51 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # # Copyright Buildbot Team Members import re from twisted.internet import defer from twisted.python.deprecate import deprecatedModuleAttribute from twisted.python.versions import Version from buildbot import config from buildbot.process import buildstep from buildbot.process import logobserver # for existing configurations that import WithProperties from here. We like # to move this class around just to keep our readers guessing. from buildbot.process.properties import WithProperties from buildbot.process.results import FAILURE from buildbot.process.results import SUCCESS from buildbot.process.results import WARNINGS from buildbot.process.results import Results from buildbot.process.results import worst_status from buildbot.steps.worker import CompositeStepMixin from buildbot.util import join_list _hush_pyflakes = [ WithProperties, ] del _hush_pyflakes class TreeSize(buildstep.ShellMixin, buildstep.BuildStep): name = "treesize" command = ["du", "-s", "-k", "."] description = ["measuring", "tree", "size"] def __init__(self, **kwargs): kwargs = self.setupShellMixin(kwargs) super().__init__(**kwargs) self.observer = logobserver.BufferLogObserver(wantStdout=True, wantStderr=True) self.addLogObserver('stdio', self.observer) @defer.inlineCallbacks def run(self): cmd = yield self.makeRemoteShellCommand() yield self.runCommand(cmd) stdio_log = yield self.getLog('stdio') yield stdio_log.finish() out = self.observer.getStdout() m = re.search(r'^(\d+)', out) kib = None if m: kib = int(m.group(1)) self.setProperty("tree-size-KiB", kib, "treesize") self.descriptionDone = "treesize {} KiB".format(kib) else: self.descriptionDone = "treesize unknown" if cmd.didFail(): return FAILURE if kib is None: return WARNINGS # not sure how 'du' could fail, but whatever return SUCCESS class SetPropertyFromCommand(buildstep.ShellMixin, buildstep.BuildStep): name = "setproperty" renderables = ['property'] def __init__(self, property=None, extract_fn=None, strip=True, includeStdout=True, includeStderr=False, **kwargs): kwargs = self.setupShellMixin(kwargs) self.property = property self.extract_fn = extract_fn self.strip = strip self.includeStdout = includeStdout self.includeStderr = includeStderr if not ((property is not None) ^ (extract_fn is not None)): config.error( "Exactly one of property and extract_fn must be set") super().__init__(**kwargs) if self.extract_fn: self.includeStderr = True self.observer = logobserver.BufferLogObserver( wantStdout=self.includeStdout, wantStderr=self.includeStderr) self.addLogObserver('stdio', self.observer) @defer.inlineCallbacks def run(self): cmd = yield self.makeRemoteShellCommand() yield self.runCommand(cmd) stdio_log = yield self.getLog('stdio') yield stdio_log.finish() property_changes = {} if self.property: if cmd.didFail(): return FAILURE result = self.observer.getStdout() if self.strip: result = result.strip() propname = self.property self.setProperty(propname, result, "SetPropertyFromCommand Step") property_changes[propname] = result else: new_props = self.extract_fn(cmd.rc, self.observer.getStdout(), self.observer.getStderr()) for k, v in new_props.items(): self.setProperty(k, v, "SetPropertyFromCommand Step") property_changes = new_props props_set = ["{}: {}".format(k, repr(v)) for k, v in sorted(property_changes.items())] yield self.addCompleteLog('property changes', "\n".join(props_set)) if len(property_changes) > 1: self.descriptionDone = '{} properties set'.format(len(property_changes)) elif len(property_changes) == 1: self.descriptionDone = 'property \'{}\' set'.format(list(property_changes)[0]) if cmd.didFail(): return FAILURE return SUCCESS SetPropertyFromCommandNewStyle = SetPropertyFromCommand deprecatedModuleAttribute( Version("buildbot", 3, 0, 0), message="Use SetPropertyFromCommand instead. This step will be removed in Buildbot 3.2.", moduleName="buildbot.steps.shell", name="SetPropertyFromCommandNewStyle", ) SetProperty = SetPropertyFromCommand deprecatedModuleAttribute(Version("Buildbot", 0, 8, 8), "It has been renamed to SetPropertyFromCommand", "buildbot.steps.shell", "SetProperty") class ShellCommand(buildstep.ShellMixin, buildstep.BuildStep): name = 'shell' def __init__(self, **kwargs): if self.__class__ is ShellCommand: if 'command' not in kwargs: config.error("ShellCommand's `command' argument is not specified") # check validity of arguments being passed to RemoteShellCommand valid_rsc_args = [ 'command', 'env', 'want_stdout', 'want_stderr', 'timeout', 'maxTime', 'sigtermTime', 'logfiles', 'usePTY', 'logEnviron', 'collectStdout', 'collectStderr', 'interruptSignal', 'initialStdin', 'decodeRC', 'stdioLogName', 'workdir', ] + buildstep.BuildStep.parms invalid_args = [] for arg in kwargs: if arg not in valid_rsc_args: invalid_args.append(arg) if invalid_args: config.error("Invalid argument(s) passed to ShellCommand: " + ', '.join(invalid_args)) kwargs = self.setupShellMixin(kwargs) super().__init__(**kwargs) @defer.inlineCallbacks def run(self): cmd = yield self.makeRemoteShellCommand() yield self.runCommand(cmd) return cmd.results() ShellCommandNewStyle = ShellCommand deprecatedModuleAttribute( Version("buildbot", 3, 0, 0), message="Use ShellCommand instead. This step will be removed in Buildbot 3.2.", moduleName="buildbot.steps.shell", name="ShellCommandNewStyle", ) class Configure(ShellCommand): name = "configure" haltOnFailure = 1 flunkOnFailure = 1 description = "configuring" descriptionDone = "configure" command = ["./configure"] ConfigureNewStyle = Configure deprecatedModuleAttribute( Version("buildbot", 3, 0, 0), message="Use Configure instead. This step will be removed in Buildbot 3.2.", moduleName="buildbot.steps.shell", name="ConfigureNewStyle", ) class WarningCountingShellCommand(buildstep.ShellMixin, CompositeStepMixin, buildstep.BuildStep): renderables = [ 'suppressionFile', 'suppressionList', 'warningPattern', 'directoryEnterPattern', 'directoryLeavePattern', 'maxWarnCount', ] warnCount = 0 warningPattern = '(?i).*warning[: ].*' # The defaults work for GNU Make. directoryEnterPattern = ("make.*: Entering directory " "[\u2019\"`'](.*)[\u2019'`\"]") directoryLeavePattern = "make.*: Leaving directory" suppressionFile = None commentEmptyLineRe = re.compile(r"^\s*(#.*)?$") suppressionLineRe = re.compile( r"^\s*(.+?)\s*:\s*(.+?)\s*(?:[:]\s*([0-9]+)(?:-([0-9]+))?\s*)?$") def __init__(self, warningPattern=None, warningExtractor=None, maxWarnCount=None, directoryEnterPattern=None, directoryLeavePattern=None, suppressionFile=None, suppressionList=None, **kwargs): # See if we've been given a regular expression to use to match # warnings. If not, use a default that assumes any line with "warning" # present is a warning. This may lead to false positives in some cases. if warningPattern: self.warningPattern = warningPattern if directoryEnterPattern: self.directoryEnterPattern = directoryEnterPattern if directoryLeavePattern: self.directoryLeavePattern = directoryLeavePattern if suppressionFile: self.suppressionFile = suppressionFile # self.suppressions is already taken, so use something else self.suppressionList = suppressionList if warningExtractor: self.warningExtractor = warningExtractor else: self.warningExtractor = WarningCountingShellCommand.warnExtractWholeLine self.maxWarnCount = maxWarnCount if self.__class__ is WarningCountingShellCommand and not kwargs.get('command'): # WarningCountingShellCommand class is directly instantiated. # Explicitly check that command is set to prevent runtime error # later. config.error("WarningCountingShellCommand's 'command' argument is not specified") kwargs = self.setupShellMixin(kwargs) super().__init__(**kwargs) self.suppressions = [] self.directoryStack = [] self.warnCount = 0 self.loggedWarnings = [] self.addLogObserver( 'stdio', logobserver.LineConsumerLogObserver(self.warningLogConsumer)) def addSuppression(self, suppressionList): """ This method can be used to add patters of warnings that should not be counted. It takes a single argument, a list of patterns. Each pattern is a 4-tuple (FILE-RE, WARN-RE, START, END). FILE-RE is a regular expression (string or compiled regexp), or None. If None, the pattern matches all files, else only files matching the regexp. If directoryEnterPattern is specified in the class constructor, matching is against the full path name, eg. src/main.c. WARN-RE is similarly a regular expression matched against the text of the warning, or None to match all warnings. START and END form an inclusive line number range to match against. If START is None, there is no lower bound, similarly if END is none there is no upper bound.""" for fileRe, warnRe, start, end in suppressionList: if fileRe is not None and isinstance(fileRe, str): fileRe = re.compile(fileRe) if warnRe is not None and isinstance(warnRe, str): warnRe = re.compile(warnRe) self.suppressions.append((fileRe, warnRe, start, end)) def warnExtractWholeLine(self, line, match): """ Extract warning text as the whole line. No file names or line numbers.""" return (None, None, line) def warnExtractFromRegexpGroups(self, line, match): """ Extract file name, line number, and warning text as groups (1,2,3) of warningPattern match.""" file = match.group(1) lineNo = match.group(2) if lineNo is not None: lineNo = int(lineNo) text = match.group(3) return (file, lineNo, text) def warningLogConsumer(self): # Now compile a regular expression from whichever warning pattern we're # using wre = self.warningPattern if isinstance(wre, str): wre = re.compile(wre) directoryEnterRe = self.directoryEnterPattern if (directoryEnterRe is not None and isinstance(directoryEnterRe, str)): directoryEnterRe = re.compile(directoryEnterRe) directoryLeaveRe = self.directoryLeavePattern if (directoryLeaveRe is not None and isinstance(directoryLeaveRe, str)): directoryLeaveRe = re.compile(directoryLeaveRe) # Check if each line in the output from this command matched our # warnings regular expressions. If did, bump the warnings count and # add the line to the collection of lines with warnings self.loggedWarnings = [] while True: stream, line = yield if directoryEnterRe: match = directoryEnterRe.search(line) if match: self.directoryStack.append(match.group(1)) continue if (directoryLeaveRe and self.directoryStack and directoryLeaveRe.search(line)): self.directoryStack.pop() continue match = wre.match(line) if match: self.maybeAddWarning(self.loggedWarnings, line, match) def maybeAddWarning(self, warnings, line, match): if self.suppressions: (file, lineNo, text) = self.warningExtractor(self, line, match) lineNo = lineNo and int(lineNo) if file is not None and file != "" and self.directoryStack: currentDirectory = '/'.join(self.directoryStack) if currentDirectory is not None and currentDirectory != "": file = "{}/{}".format(currentDirectory, file) # Skip adding the warning if any suppression matches. for fileRe, warnRe, start, end in self.suppressions: if not (file is None or fileRe is None or fileRe.match(file)): continue if not (warnRe is None or warnRe.search(text)): continue if ((start is not None and end is not None) and not (lineNo is not None and start <= lineNo <= end)): continue return warnings.append(line) self.warnCount += 1 @defer.inlineCallbacks def setup_suppression(self): if self.suppressionList is not None: self.addSuppression(self.suppressionList) if self.suppressionFile is not None: data = yield self.getFileContentFromWorker(self.suppressionFile, abandonOnFailure=True) lines = data.split("\n") list = [] for line in lines: if self.commentEmptyLineRe.match(line): continue match = self.suppressionLineRe.match(line) if (match): file, test, start, end = match.groups() if (end is not None): end = int(end) if (start is not None): start = int(start) if end is None: end = start list.append((file, test, start, end)) self.addSuppression(list) @defer.inlineCallbacks def run(self): yield self.setup_suppression() cmd = yield self.makeRemoteShellCommand() yield self.runCommand(cmd) yield self.finish_logs() yield self.createSummary() return self.evaluateCommand(cmd) @defer.inlineCallbacks def finish_logs(self): stdio_log = yield self.getLog('stdio') yield stdio_log.finish() def createSummary(self): """ Match log lines against warningPattern. Warnings are collected into another log for this step, and the build-wide 'warnings-count' is updated.""" # If there were any warnings, make the log if lines with warnings # available if self.warnCount: self.addCompleteLog("warnings (%d)" % self.warnCount, "\n".join(self.loggedWarnings) + "\n") warnings_stat = self.getStatistic('warnings', 0) self.setStatistic('warnings', warnings_stat + self.warnCount) old_count = self.getProperty("warnings-count", 0) self.setProperty( "warnings-count", old_count + self.warnCount, "WarningCountingShellCommand") def evaluateCommand(self, cmd): result = cmd.results() if (self.maxWarnCount is not None and self.warnCount > self.maxWarnCount): result = worst_status(result, FAILURE) elif self.warnCount: result = worst_status(result, WARNINGS) return result WarningCountingShellCommandNewStyle = WarningCountingShellCommand deprecatedModuleAttribute( Version("buildbot", 3, 0, 0), message="Use WarningCountingShellCommand instead. This step will be removed in Buildbot 3.2.", moduleName="buildbot.steps.shell", name="WarningCountingShellCommandNewStyle", ) class Compile(WarningCountingShellCommand): name = "compile" haltOnFailure = 1 flunkOnFailure = 1 description = ["compiling"] descriptionDone = ["compile"] command = ["make", "all"] CompileNewStyle = Compile deprecatedModuleAttribute( Version("buildbot", 3, 0, 0), message="Use Compile instead. This step will be removed in Buildbot 3.2.", moduleName="buildbot.steps.shell", name="CompileNewStyle", ) class Test(WarningCountingShellCommand): name = "test" warnOnFailure = 1 description = ["testing"] descriptionDone = ["test"] command = ["make", "test"] def setTestResults(self, total=0, failed=0, passed=0, warnings=0): """ Called by subclasses to set the relevant statistics; this actually adds to any statistics already present """ total += self.getStatistic('tests-total', 0) self.setStatistic('tests-total', total) failed += self.getStatistic('tests-failed', 0) self.setStatistic('tests-failed', failed) warnings += self.getStatistic('tests-warnings', 0) self.setStatistic('tests-warnings', warnings) passed += self.getStatistic('tests-passed', 0) self.setStatistic('tests-passed', passed) def getResultSummary(self): description = [] if self.hasStatistic('tests-total'): total = self.getStatistic("tests-total", 0) failed = self.getStatistic("tests-failed", 0) passed = self.getStatistic("tests-passed", 0) warnings = self.getStatistic("tests-warnings", 0) if not total: total = failed + passed + warnings if total: description += [str(total), 'tests'] if passed: description += [str(passed), 'passed'] if warnings: description += [str(warnings), 'warnings'] if failed: description += [str(failed), 'failed'] if description: summary = join_list(description) if self.results != SUCCESS: summary += ' ({})'.format(Results[self.results]) return {'step': summary} return super().getResultSummary() TestNewStyle = Test deprecatedModuleAttribute( Version("buildbot", 3, 0, 0), message="Use Test instead. This step will be removed in Buildbot 3.2.", moduleName="buildbot.steps.shell", name="TestNewStyle", ) class PerlModuleTestObserver(logobserver.LogLineObserver): def __init__(self, warningPattern): super().__init__() if warningPattern: self.warningPattern = re.compile(warningPattern) else: self.warningPattern = None self.rc = SUCCESS self.total = 0 self.failed = 0 self.warnings = 0 self.newStyle = False self.complete = False failedRe = re.compile(r"Tests: \d+ Failed: (\d+)\)") testsRe = re.compile(r"Files=\d+, Tests=(\d+)") oldFailureCountsRe = re.compile(r"(\d+)/(\d+) subtests failed") oldSuccessCountsRe = re.compile(r"Files=\d+, Tests=(\d+),") def outLineReceived(self, line): if self.warningPattern.match(line): self.warnings += 1 if self.newStyle: if line.startswith('Result: FAIL'): self.rc = FAILURE mo = self.failedRe.search(line) if mo: self.failed += int(mo.group(1)) if self.failed: self.rc = FAILURE mo = self.testsRe.search(line) if mo: self.total = int(mo.group(1)) else: if line.startswith('Test Summary Report'): self.newStyle = True mo = self.oldFailureCountsRe.search(line) if mo: self.failed = int(mo.group(1)) self.total = int(mo.group(2)) self.rc = FAILURE mo = self.oldSuccessCountsRe.search(line) if mo: self.total = int(mo.group(1)) class PerlModuleTest(Test): command = ["prove", "--lib", "lib", "-r", "t"] total = 0 def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.observer = PerlModuleTestObserver( warningPattern=self.warningPattern) self.addLogObserver('stdio', self.observer) def evaluateCommand(self, cmd): if self.observer.total: passed = self.observer.total - self.observer.failed self.setTestResults( total=self.observer.total, failed=self.observer.failed, passed=passed, warnings=self.observer.warnings) rc = self.observer.rc if rc == SUCCESS and self.observer.warnings: rc = WARNINGS return rc
gpl-2.0
eunchong/build
third_party/cherrypy/lib/caching.py
88
17413
""" CherryPy implements a simple caching system as a pluggable Tool. This tool tries to be an (in-process) HTTP/1.1-compliant cache. It's not quite there yet, but it's probably good enough for most sites. In general, GET responses are cached (along with selecting headers) and, if another request arrives for the same resource, the caching Tool will return 304 Not Modified if possible, or serve the cached response otherwise. It also sets request.cached to True if serving a cached representation, and sets request.cacheable to False (so it doesn't get cached again). If POST, PUT, or DELETE requests are made for a cached resource, they invalidate (delete) any cached response. Usage ===== Configuration file example:: [/] tools.caching.on = True tools.caching.delay = 3600 You may use a class other than the default :class:`MemoryCache<cherrypy.lib.caching.MemoryCache>` by supplying the config entry ``cache_class``; supply the full dotted name of the replacement class as the config value. It must implement the basic methods ``get``, ``put``, ``delete``, and ``clear``. You may set any attribute, including overriding methods, on the cache instance by providing them in config. The above sets the :attr:`delay<cherrypy.lib.caching.MemoryCache.delay>` attribute, for example. """ import datetime import sys import threading import time import cherrypy from cherrypy.lib import cptools, httputil from cherrypy._cpcompat import copyitems, ntob, set_daemon, sorted class Cache(object): """Base class for Cache implementations.""" def get(self): """Return the current variant if in the cache, else None.""" raise NotImplemented def put(self, obj, size): """Store the current variant in the cache.""" raise NotImplemented def delete(self): """Remove ALL cached variants of the current resource.""" raise NotImplemented def clear(self): """Reset the cache to its initial, empty state.""" raise NotImplemented # ------------------------------- Memory Cache ------------------------------- # class AntiStampedeCache(dict): """A storage system for cached items which reduces stampede collisions.""" def wait(self, key, timeout=5, debug=False): """Return the cached value for the given key, or None. If timeout is not None, and the value is already being calculated by another thread, wait until the given timeout has elapsed. If the value is available before the timeout expires, it is returned. If not, None is returned, and a sentinel placed in the cache to signal other threads to wait. If timeout is None, no waiting is performed nor sentinels used. """ value = self.get(key) if isinstance(value, threading._Event): if timeout is None: # Ignore the other thread and recalc it ourselves. if debug: cherrypy.log('No timeout', 'TOOLS.CACHING') return None # Wait until it's done or times out. if debug: cherrypy.log('Waiting up to %s seconds' % timeout, 'TOOLS.CACHING') value.wait(timeout) if value.result is not None: # The other thread finished its calculation. Use it. if debug: cherrypy.log('Result!', 'TOOLS.CACHING') return value.result # Timed out. Stick an Event in the slot so other threads wait # on this one to finish calculating the value. if debug: cherrypy.log('Timed out', 'TOOLS.CACHING') e = threading.Event() e.result = None dict.__setitem__(self, key, e) return None elif value is None: # Stick an Event in the slot so other threads wait # on this one to finish calculating the value. if debug: cherrypy.log('Timed out', 'TOOLS.CACHING') e = threading.Event() e.result = None dict.__setitem__(self, key, e) return value def __setitem__(self, key, value): """Set the cached value for the given key.""" existing = self.get(key) dict.__setitem__(self, key, value) if isinstance(existing, threading._Event): # Set Event.result so other threads waiting on it have # immediate access without needing to poll the cache again. existing.result = value existing.set() class MemoryCache(Cache): """An in-memory cache for varying response content. Each key in self.store is a URI, and each value is an AntiStampedeCache. The response for any given URI may vary based on the values of "selecting request headers"; that is, those named in the Vary response header. We assume the list of header names to be constant for each URI throughout the lifetime of the application, and store that list in ``self.store[uri].selecting_headers``. The items contained in ``self.store[uri]`` have keys which are tuples of request header values (in the same order as the names in its selecting_headers), and values which are the actual responses. """ maxobjects = 1000 """The maximum number of cached objects; defaults to 1000.""" maxobj_size = 100000 """The maximum size of each cached object in bytes; defaults to 100 KB.""" maxsize = 10000000 """The maximum size of the entire cache in bytes; defaults to 10 MB.""" delay = 600 """Seconds until the cached content expires; defaults to 600 (10 minutes).""" antistampede_timeout = 5 """Seconds to wait for other threads to release a cache lock.""" expire_freq = 0.1 """Seconds to sleep between cache expiration sweeps.""" debug = False def __init__(self): self.clear() # Run self.expire_cache in a separate daemon thread. t = threading.Thread(target=self.expire_cache, name='expire_cache') self.expiration_thread = t set_daemon(t, True) t.start() def clear(self): """Reset the cache to its initial, empty state.""" self.store = {} self.expirations = {} self.tot_puts = 0 self.tot_gets = 0 self.tot_hist = 0 self.tot_expires = 0 self.tot_non_modified = 0 self.cursize = 0 def expire_cache(self): """Continuously examine cached objects, expiring stale ones. This function is designed to be run in its own daemon thread, referenced at ``self.expiration_thread``. """ # It's possible that "time" will be set to None # arbitrarily, so we check "while time" to avoid exceptions. # See tickets #99 and #180 for more information. while time: now = time.time() # Must make a copy of expirations so it doesn't change size # during iteration for expiration_time, objects in copyitems(self.expirations): if expiration_time <= now: for obj_size, uri, sel_header_values in objects: try: del self.store[uri][tuple(sel_header_values)] self.tot_expires += 1 self.cursize -= obj_size except KeyError: # the key may have been deleted elsewhere pass del self.expirations[expiration_time] time.sleep(self.expire_freq) def get(self): """Return the current variant if in the cache, else None.""" request = cherrypy.serving.request self.tot_gets += 1 uri = cherrypy.url(qs=request.query_string) uricache = self.store.get(uri) if uricache is None: return None header_values = [request.headers.get(h, '') for h in uricache.selecting_headers] variant = uricache.wait(key=tuple(sorted(header_values)), timeout=self.antistampede_timeout, debug=self.debug) if variant is not None: self.tot_hist += 1 return variant def put(self, variant, size): """Store the current variant in the cache.""" request = cherrypy.serving.request response = cherrypy.serving.response uri = cherrypy.url(qs=request.query_string) uricache = self.store.get(uri) if uricache is None: uricache = AntiStampedeCache() uricache.selecting_headers = [ e.value for e in response.headers.elements('Vary')] self.store[uri] = uricache if len(self.store) < self.maxobjects: total_size = self.cursize + size # checks if there's space for the object if (size < self.maxobj_size and total_size < self.maxsize): # add to the expirations list expiration_time = response.time + self.delay bucket = self.expirations.setdefault(expiration_time, []) bucket.append((size, uri, uricache.selecting_headers)) # add to the cache header_values = [request.headers.get(h, '') for h in uricache.selecting_headers] uricache[tuple(sorted(header_values))] = variant self.tot_puts += 1 self.cursize = total_size def delete(self): """Remove ALL cached variants of the current resource.""" uri = cherrypy.url(qs=cherrypy.serving.request.query_string) self.store.pop(uri, None) def get(invalid_methods=("POST", "PUT", "DELETE"), debug=False, **kwargs): """Try to obtain cached output. If fresh enough, raise HTTPError(304). If POST, PUT, or DELETE: * invalidates (deletes) any cached response for this resource * sets request.cached = False * sets request.cacheable = False else if a cached copy exists: * sets request.cached = True * sets request.cacheable = False * sets response.headers to the cached values * checks the cached Last-Modified response header against the current If-(Un)Modified-Since request headers; raises 304 if necessary. * sets response.status and response.body to the cached values * returns True otherwise: * sets request.cached = False * sets request.cacheable = True * returns False """ request = cherrypy.serving.request response = cherrypy.serving.response if not hasattr(cherrypy, "_cache"): # Make a process-wide Cache object. cherrypy._cache = kwargs.pop("cache_class", MemoryCache)() # Take all remaining kwargs and set them on the Cache object. for k, v in kwargs.items(): setattr(cherrypy._cache, k, v) cherrypy._cache.debug = debug # POST, PUT, DELETE should invalidate (delete) the cached copy. # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.10. if request.method in invalid_methods: if debug: cherrypy.log('request.method %r in invalid_methods %r' % (request.method, invalid_methods), 'TOOLS.CACHING') cherrypy._cache.delete() request.cached = False request.cacheable = False return False if 'no-cache' in [e.value for e in request.headers.elements('Pragma')]: request.cached = False request.cacheable = True return False cache_data = cherrypy._cache.get() request.cached = bool(cache_data) request.cacheable = not request.cached if request.cached: # Serve the cached copy. max_age = cherrypy._cache.delay for v in [e.value for e in request.headers.elements('Cache-Control')]: atoms = v.split('=', 1) directive = atoms.pop(0) if directive == 'max-age': if len(atoms) != 1 or not atoms[0].isdigit(): raise cherrypy.HTTPError(400, "Invalid Cache-Control header") max_age = int(atoms[0]) break elif directive == 'no-cache': if debug: cherrypy.log('Ignoring cache due to Cache-Control: no-cache', 'TOOLS.CACHING') request.cached = False request.cacheable = True return False if debug: cherrypy.log('Reading response from cache', 'TOOLS.CACHING') s, h, b, create_time = cache_data age = int(response.time - create_time) if (age > max_age): if debug: cherrypy.log('Ignoring cache due to age > %d' % max_age, 'TOOLS.CACHING') request.cached = False request.cacheable = True return False # Copy the response headers. See http://www.cherrypy.org/ticket/721. response.headers = rh = httputil.HeaderMap() for k in h: dict.__setitem__(rh, k, dict.__getitem__(h, k)) # Add the required Age header response.headers["Age"] = str(age) try: # Note that validate_since depends on a Last-Modified header; # this was put into the cached copy, and should have been # resurrected just above (response.headers = cache_data[1]). cptools.validate_since() except cherrypy.HTTPRedirect: x = sys.exc_info()[1] if x.status == 304: cherrypy._cache.tot_non_modified += 1 raise # serve it & get out from the request response.status = s response.body = b else: if debug: cherrypy.log('request is not cached', 'TOOLS.CACHING') return request.cached def tee_output(): """Tee response output to cache storage. Internal.""" # Used by CachingTool by attaching to request.hooks request = cherrypy.serving.request if 'no-store' in request.headers.values('Cache-Control'): return def tee(body): """Tee response.body into a list.""" if ('no-cache' in response.headers.values('Pragma') or 'no-store' in response.headers.values('Cache-Control')): for chunk in body: yield chunk return output = [] for chunk in body: output.append(chunk) yield chunk # save the cache data body = ntob('').join(output) cherrypy._cache.put((response.status, response.headers or {}, body, response.time), len(body)) response = cherrypy.serving.response response.body = tee(response.body) def expires(secs=0, force=False, debug=False): """Tool for influencing cache mechanisms using the 'Expires' header. secs Must be either an int or a datetime.timedelta, and indicates the number of seconds between response.time and when the response should expire. The 'Expires' header will be set to response.time + secs. If secs is zero, the 'Expires' header is set one year in the past, and the following "cache prevention" headers are also set: * Pragma: no-cache * Cache-Control': no-cache, must-revalidate force If False, the following headers are checked: * Etag * Last-Modified * Age * Expires If any are already present, none of the above response headers are set. """ response = cherrypy.serving.response headers = response.headers cacheable = False if not force: # some header names that indicate that the response can be cached for indicator in ('Etag', 'Last-Modified', 'Age', 'Expires'): if indicator in headers: cacheable = True break if not cacheable and not force: if debug: cherrypy.log('request is not cacheable', 'TOOLS.EXPIRES') else: if debug: cherrypy.log('request is cacheable', 'TOOLS.EXPIRES') if isinstance(secs, datetime.timedelta): secs = (86400 * secs.days) + secs.seconds if secs == 0: if force or ("Pragma" not in headers): headers["Pragma"] = "no-cache" if cherrypy.serving.request.protocol >= (1, 1): if force or "Cache-Control" not in headers: headers["Cache-Control"] = "no-cache, must-revalidate" # Set an explicit Expires date in the past. expiry = httputil.HTTPDate(1169942400.0) else: expiry = httputil.HTTPDate(response.time + secs) if force or "Expires" not in headers: headers["Expires"] = expiry
bsd-3-clause
frederick-masterton/django
tests/max_lengths/tests.py
27
1588
from __future__ import unicode_literals import unittest from .models import PersonWithDefaultMaxLengths, PersonWithCustomMaxLengths class MaxLengthArgumentsTests(unittest.TestCase): def verify_max_length(self, model, field, length): self.assertEqual(model._meta.get_field(field).max_length, length) def test_default_max_lengths(self): self.verify_max_length(PersonWithDefaultMaxLengths, 'email', 75) self.verify_max_length(PersonWithDefaultMaxLengths, 'vcard', 100) self.verify_max_length(PersonWithDefaultMaxLengths, 'homepage', 200) self.verify_max_length(PersonWithDefaultMaxLengths, 'avatar', 100) def test_custom_max_lengths(self): self.verify_max_length(PersonWithCustomMaxLengths, 'email', 250) self.verify_max_length(PersonWithCustomMaxLengths, 'vcard', 250) self.verify_max_length(PersonWithCustomMaxLengths, 'homepage', 250) self.verify_max_length(PersonWithCustomMaxLengths, 'avatar', 250) class MaxLengthORMTests(unittest.TestCase): def test_custom_max_lengths(self): args = { "email": "someone@example.com", "vcard": "vcard", "homepage": "http://example.com/", "avatar": "me.jpg" } for field in ("email", "vcard", "homepage", "avatar"): new_args = args.copy() new_args[field] = "X" * 250 # a value longer than any of the default fields could hold. p = PersonWithCustomMaxLengths.objects.create(**new_args) self.assertEqual(getattr(p, field), ("X" * 250))
bsd-3-clause
DeanThompson/pyelong
pyelong/request.py
1
6017
# -*- coding: utf-8 -*- import hashlib import json import time import urllib import requests from requests import RequestException, ConnectionError, Timeout from tornado import gen from tornado.httpclient import AsyncHTTPClient from .api import ApiSpec from .exceptions import ElongException, ElongAPIError, \ RetryableException, RetryableAPIError from .response import RequestsResponse, TornadoResponse, logger from .util.retry import retry_on_error, is_retryable class Request(object): def __init__(self, client, host=ApiSpec.host, version=ApiSpec.version, local=ApiSpec.local): self.client = client self.verify_ssl = self.client.cert is not None self.host = host self.version = version self.local = local def do(self, api, params, https, raw=False): raise NotImplementedError() def prepare(self, api, params, https, raw): timestamp = str(int(time.time())) data = self.build_data(params, raw) scheme = 'https' if https else 'http' url = "%s://%s" % (scheme, self.host) params = { 'method': api, 'user': self.client.user, 'timestamp': timestamp, 'data': data, 'signature': self.signature(data, timestamp), 'format': 'json' } return url, params def build_data(self, params, raw=False): if not raw: data = { 'Version': self.version, 'Local': self.local, 'Request': params } else: data = params return json.dumps(data, separators=(',', ':')) def signature(self, data, timestamp): s = self._md5(data + self.client.app_key) return self._md5("%s%s%s" % (timestamp, s, self.client.secret_key)) @staticmethod def _md5(data): return hashlib.md5(data.encode('utf-8')).hexdigest() def check_response(self, resp): if not resp.ok and self.client.raise_api_error: # logger.error('pyelong calling api failed, url: %s', resp.url) if is_retryable(resp.code): raise RetryableAPIError(resp.code, resp.error) raise ElongAPIError(resp.code, resp.error) return resp def timing(self, api, delta): if self.client.statsd_client and \ hasattr(self.client.statsd_client, 'timing'): self.client.statsd_client.timing(api, delta) class SyncRequest(Request): @property def session(self): if not hasattr(self, '_session') or not self._session: self._session = requests.Session() if self.client.proxy_host and self.client.proxy_port: p = '%s:%s' % (self.client.proxy_host, self.client.proxy_port) self._session.proxies = {'http': p, 'https': p} return self._session @retry_on_error(retry_api_error=True) def do(self, api, params, https, raw=False): url, params = self.prepare(api, params, https, raw) try: result = self.session.get(url=url, params=params, verify=self.verify_ssl, cert=self.client.cert) except (ConnectionError, Timeout) as e: logger.exception('pyelong catches ConnectionError or Timeout, ' 'url: %s, params: %s', url, params) raise RetryableException('ConnectionError or Timeout: %s' % e) except RequestException as e: logger.exception('pyelong catches RequestException, url: %s,' ' params: %s', url, params) raise ElongException('RequestException: %s' % e) except Exception as e: logger.exception('pyelong catches unknown exception, url: %s, ' 'params: %s', url, params) raise ElongException('unknown exception: %s' % e) resp = RequestsResponse(result) self.timing(api, resp.request_time) return self.check_response(resp) class AsyncRequest(Request): @property def proxy_config(self): if not getattr(self, '_proxy_config', None): if self.client.proxy_host and self.client.proxy_port: self._proxy_config = { 'proxy_host': self.client.proxy_host, 'proxy_port': self.client.proxy_port } else: self._proxy_config = {} return self._proxy_config @staticmethod def _encode_params(data): """ :param dict data: params Taken from requests.models.RequestEncodingMixin._encode_params """ result = [] for k, vs in data.iteritems(): if isinstance(vs, basestring) or not hasattr(vs, '__iter__'): vs = [vs] for v in vs: if v is not None: result.append( (k.encode('utf-8') if isinstance(k, str) else k, v.encode('utf-8') if isinstance(v, str) else v)) return urllib.urlencode(result, doseq=True) def _prepare_url(self, url, params): if url.endswith('/'): url = url.strip('/') return '%s?%s' % (url, self._encode_params(params)) @gen.coroutine def do(self, api, params, https, raw=False): url, params = self.prepare(api, params, https, raw) # use the default SimpleAsyncHTTPClient resp = yield AsyncHTTPClient().fetch(self._prepare_url(url, params), validate_cert=self.verify_ssl, ca_certs=self.client.cert, **self.proxy_config) resp = TornadoResponse(resp) self.timing(api, resp.request_time) raise gen.Return(self.check_response(resp))
mit
catroot/rethinkdb
test/common/http_support/werkzeug/wsgi.py
146
37745
# -*- coding: utf-8 -*- """ werkzeug.wsgi ~~~~~~~~~~~~~ This module implements WSGI related helpers. :copyright: (c) 2014 by the Werkzeug Team, see AUTHORS for more details. :license: BSD, see LICENSE for more details. """ import re import os import sys import posixpath import mimetypes from itertools import chain from zlib import adler32 from time import time, mktime from datetime import datetime from functools import partial, update_wrapper from werkzeug._compat import iteritems, text_type, string_types, \ implements_iterator, make_literal_wrapper, to_unicode, to_bytes, \ wsgi_get_bytes, try_coerce_native, PY2 from werkzeug._internal import _empty_stream, _encode_idna from werkzeug.http import is_resource_modified, http_date from werkzeug.urls import uri_to_iri, url_quote, url_parse, url_join def responder(f): """Marks a function as responder. Decorate a function with it and it will automatically call the return value as WSGI application. Example:: @responder def application(environ, start_response): return Response('Hello World!') """ return update_wrapper(lambda *a: f(*a)(*a[-2:]), f) def get_current_url(environ, root_only=False, strip_querystring=False, host_only=False, trusted_hosts=None): """A handy helper function that recreates the full URL as IRI for the current request or parts of it. Here an example: >>> from werkzeug.test import create_environ >>> env = create_environ("/?param=foo", "http://localhost/script") >>> get_current_url(env) 'http://localhost/script/?param=foo' >>> get_current_url(env, root_only=True) 'http://localhost/script/' >>> get_current_url(env, host_only=True) 'http://localhost/' >>> get_current_url(env, strip_querystring=True) 'http://localhost/script/' This optionally it verifies that the host is in a list of trusted hosts. If the host is not in there it will raise a :exc:`~werkzeug.exceptions.SecurityError`. Note that the string returned might contain unicode characters as the representation is an IRI not an URI. If you need an ASCII only representation you can use the :func:`~werkzeug.urls.iri_to_uri` function: >>> from werkzeug.urls import iri_to_uri >>> iri_to_uri(get_current_url(env)) 'http://localhost/script/?param=foo' :param environ: the WSGI environment to get the current URL from. :param root_only: set `True` if you only want the root URL. :param strip_querystring: set to `True` if you don't want the querystring. :param host_only: set to `True` if the host URL should be returned. :param trusted_hosts: a list of trusted hosts, see :func:`host_is_trusted` for more information. """ tmp = [environ['wsgi.url_scheme'], '://', get_host(environ, trusted_hosts)] cat = tmp.append if host_only: return uri_to_iri(''.join(tmp) + '/') cat(url_quote(wsgi_get_bytes(environ.get('SCRIPT_NAME', ''))).rstrip('/')) cat('/') if not root_only: cat(url_quote(wsgi_get_bytes(environ.get('PATH_INFO', '')).lstrip(b'/'))) if not strip_querystring: qs = get_query_string(environ) if qs: cat('?' + qs) return uri_to_iri(''.join(tmp)) def host_is_trusted(hostname, trusted_list): """Checks if a host is trusted against a list. This also takes care of port normalization. .. versionadded:: 0.9 :param hostname: the hostname to check :param trusted_list: a list of hostnames to check against. If a hostname starts with a dot it will match against all subdomains as well. """ if not hostname: return False if isinstance(trusted_list, string_types): trusted_list = [trusted_list] def _normalize(hostname): if ':' in hostname: hostname = hostname.rsplit(':', 1)[0] return _encode_idna(hostname) hostname = _normalize(hostname) for ref in trusted_list: if ref.startswith('.'): ref = ref[1:] suffix_match = True else: suffix_match = False ref = _normalize(ref) if ref == hostname: return True if suffix_match and hostname.endswith('.' + ref): return True return False def get_host(environ, trusted_hosts=None): """Return the real host for the given WSGI environment. This takes care of the `X-Forwarded-Host` header. Optionally it verifies that the host is in a list of trusted hosts. If the host is not in there it will raise a :exc:`~werkzeug.exceptions.SecurityError`. :param environ: the WSGI environment to get the host of. :param trusted_hosts: a list of trusted hosts, see :func:`host_is_trusted` for more information. """ if 'HTTP_X_FORWARDED_HOST' in environ: rv = environ['HTTP_X_FORWARDED_HOST'].split(',')[0].strip() elif 'HTTP_HOST' in environ: rv = environ['HTTP_HOST'] else: rv = environ['SERVER_NAME'] if (environ['wsgi.url_scheme'], environ['SERVER_PORT']) not \ in (('https', '443'), ('http', '80')): rv += ':' + environ['SERVER_PORT'] if trusted_hosts is not None: if not host_is_trusted(rv, trusted_hosts): from werkzeug.exceptions import SecurityError raise SecurityError('Host "%s" is not trusted' % rv) return rv def get_content_length(environ): """Returns the content length from the WSGI environment as integer. If it's not available `None` is returned. .. versionadded:: 0.9 :param environ: the WSGI environ to fetch the content length from. """ content_length = environ.get('CONTENT_LENGTH') if content_length is not None: try: return max(0, int(content_length)) except (ValueError, TypeError): pass def get_input_stream(environ, safe_fallback=True): """Returns the input stream from the WSGI environment and wraps it in the most sensible way possible. The stream returned is not the raw WSGI stream in most cases but one that is safe to read from without taking into account the content length. .. versionadded:: 0.9 :param environ: the WSGI environ to fetch the stream from. :param safe: indicates weather the function should use an empty stream as safe fallback or just return the original WSGI input stream if it can't wrap it safely. The default is to return an empty string in those cases. """ stream = environ['wsgi.input'] content_length = get_content_length(environ) # A wsgi extension that tells us if the input is terminated. In # that case we return the stream unchanged as we know we can savely # read it until the end. if environ.get('wsgi.input_terminated'): return stream # If we don't have a content length we fall back to an empty stream # in case of a safe fallback, otherwise we return the stream unchanged. # The non-safe fallback is not recommended but might be useful in # some situations. if content_length is None: return safe_fallback and _empty_stream or stream # Otherwise limit the stream to the content length return LimitedStream(stream, content_length) def get_query_string(environ): """Returns the `QUERY_STRING` from the WSGI environment. This also takes care about the WSGI decoding dance on Python 3 environments as a native string. The string returned will be restricted to ASCII characters. .. versionadded:: 0.9 :param environ: the WSGI environment object to get the query string from. """ qs = wsgi_get_bytes(environ.get('QUERY_STRING', '')) # QUERY_STRING really should be ascii safe but some browsers # will send us some unicode stuff (I am looking at you IE). # In that case we want to urllib quote it badly. return try_coerce_native(url_quote(qs, safe=':&%=+$!*\'(),')) def get_path_info(environ, charset='utf-8', errors='replace'): """Returns the `PATH_INFO` from the WSGI environment and properly decodes it. This also takes care about the WSGI decoding dance on Python 3 environments. if the `charset` is set to `None` a bytestring is returned. .. versionadded:: 0.9 :param environ: the WSGI environment object to get the path from. :param charset: the charset for the path info, or `None` if no decoding should be performed. :param errors: the decoding error handling. """ path = wsgi_get_bytes(environ.get('PATH_INFO', '')) return to_unicode(path, charset, errors, allow_none_charset=True) def get_script_name(environ, charset='utf-8', errors='replace'): """Returns the `SCRIPT_NAME` from the WSGI environment and properly decodes it. This also takes care about the WSGI decoding dance on Python 3 environments. if the `charset` is set to `None` a bytestring is returned. .. versionadded:: 0.9 :param environ: the WSGI environment object to get the path from. :param charset: the charset for the path, or `None` if no decoding should be performed. :param errors: the decoding error handling. """ path = wsgi_get_bytes(environ.get('SCRIPT_NAME', '')) return to_unicode(path, charset, errors, allow_none_charset=True) def pop_path_info(environ, charset='utf-8', errors='replace'): """Removes and returns the next segment of `PATH_INFO`, pushing it onto `SCRIPT_NAME`. Returns `None` if there is nothing left on `PATH_INFO`. If the `charset` is set to `None` a bytestring is returned. If there are empty segments (``'/foo//bar``) these are ignored but properly pushed to the `SCRIPT_NAME`: >>> env = {'SCRIPT_NAME': '/foo', 'PATH_INFO': '/a/b'} >>> pop_path_info(env) 'a' >>> env['SCRIPT_NAME'] '/foo/a' >>> pop_path_info(env) 'b' >>> env['SCRIPT_NAME'] '/foo/a/b' .. versionadded:: 0.5 .. versionchanged:: 0.9 The path is now decoded and a charset and encoding parameter can be provided. :param environ: the WSGI environment that is modified. """ path = environ.get('PATH_INFO') if not path: return None script_name = environ.get('SCRIPT_NAME', '') # shift multiple leading slashes over old_path = path path = path.lstrip('/') if path != old_path: script_name += '/' * (len(old_path) - len(path)) if '/' not in path: environ['PATH_INFO'] = '' environ['SCRIPT_NAME'] = script_name + path rv = wsgi_get_bytes(path) else: segment, path = path.split('/', 1) environ['PATH_INFO'] = '/' + path environ['SCRIPT_NAME'] = script_name + segment rv = wsgi_get_bytes(segment) return to_unicode(rv, charset, errors, allow_none_charset=True) def peek_path_info(environ, charset='utf-8', errors='replace'): """Returns the next segment on the `PATH_INFO` or `None` if there is none. Works like :func:`pop_path_info` without modifying the environment: >>> env = {'SCRIPT_NAME': '/foo', 'PATH_INFO': '/a/b'} >>> peek_path_info(env) 'a' >>> peek_path_info(env) 'a' If the `charset` is set to `None` a bytestring is returned. .. versionadded:: 0.5 .. versionchanged:: 0.9 The path is now decoded and a charset and encoding parameter can be provided. :param environ: the WSGI environment that is checked. """ segments = environ.get('PATH_INFO', '').lstrip('/').split('/', 1) if segments: return to_unicode(wsgi_get_bytes(segments[0]), charset, errors, allow_none_charset=True) def extract_path_info(environ_or_baseurl, path_or_url, charset='utf-8', errors='replace', collapse_http_schemes=True): """Extracts the path info from the given URL (or WSGI environment) and path. The path info returned is a unicode string, not a bytestring suitable for a WSGI environment. The URLs might also be IRIs. If the path info could not be determined, `None` is returned. Some examples: >>> extract_path_info('http://example.com/app', '/app/hello') u'/hello' >>> extract_path_info('http://example.com/app', ... 'https://example.com/app/hello') u'/hello' >>> extract_path_info('http://example.com/app', ... 'https://example.com/app/hello', ... collapse_http_schemes=False) is None True Instead of providing a base URL you can also pass a WSGI environment. .. versionadded:: 0.6 :param environ_or_baseurl: a WSGI environment dict, a base URL or base IRI. This is the root of the application. :param path_or_url: an absolute path from the server root, a relative path (in which case it's the path info) or a full URL. Also accepts IRIs and unicode parameters. :param charset: the charset for byte data in URLs :param errors: the error handling on decode :param collapse_http_schemes: if set to `False` the algorithm does not assume that http and https on the same server point to the same resource. """ def _normalize_netloc(scheme, netloc): parts = netloc.split(u'@', 1)[-1].split(u':', 1) if len(parts) == 2: netloc, port = parts if (scheme == u'http' and port == u'80') or \ (scheme == u'https' and port == u'443'): port = None else: netloc = parts[0] port = None if port is not None: netloc += u':' + port return netloc # make sure whatever we are working on is a IRI and parse it path = uri_to_iri(path_or_url, charset, errors) if isinstance(environ_or_baseurl, dict): environ_or_baseurl = get_current_url(environ_or_baseurl, root_only=True) base_iri = uri_to_iri(environ_or_baseurl, charset, errors) base_scheme, base_netloc, base_path = url_parse(base_iri)[:3] cur_scheme, cur_netloc, cur_path, = \ url_parse(url_join(base_iri, path))[:3] # normalize the network location base_netloc = _normalize_netloc(base_scheme, base_netloc) cur_netloc = _normalize_netloc(cur_scheme, cur_netloc) # is that IRI even on a known HTTP scheme? if collapse_http_schemes: for scheme in base_scheme, cur_scheme: if scheme not in (u'http', u'https'): return None else: if not (base_scheme in (u'http', u'https') and base_scheme == cur_scheme): return None # are the netlocs compatible? if base_netloc != cur_netloc: return None # are we below the application path? base_path = base_path.rstrip(u'/') if not cur_path.startswith(base_path): return None return u'/' + cur_path[len(base_path):].lstrip(u'/') class SharedDataMiddleware(object): """A WSGI middleware that provides static content for development environments or simple server setups. Usage is quite simple:: import os from werkzeug.wsgi import SharedDataMiddleware app = SharedDataMiddleware(app, { '/shared': os.path.join(os.path.dirname(__file__), 'shared') }) The contents of the folder ``./shared`` will now be available on ``http://example.com/shared/``. This is pretty useful during development because a standalone media server is not required. One can also mount files on the root folder and still continue to use the application because the shared data middleware forwards all unhandled requests to the application, even if the requests are below one of the shared folders. If `pkg_resources` is available you can also tell the middleware to serve files from package data:: app = SharedDataMiddleware(app, { '/shared': ('myapplication', 'shared_files') }) This will then serve the ``shared_files`` folder in the `myapplication` Python package. The optional `disallow` parameter can be a list of :func:`~fnmatch.fnmatch` rules for files that are not accessible from the web. If `cache` is set to `False` no caching headers are sent. Currently the middleware does not support non ASCII filenames. If the encoding on the file system happens to be the encoding of the URI it may work but this could also be by accident. We strongly suggest using ASCII only file names for static files. The middleware will guess the mimetype using the Python `mimetype` module. If it's unable to figure out the charset it will fall back to `fallback_mimetype`. .. versionchanged:: 0.5 The cache timeout is configurable now. .. versionadded:: 0.6 The `fallback_mimetype` parameter was added. :param app: the application to wrap. If you don't want to wrap an application you can pass it :exc:`NotFound`. :param exports: a dict of exported files and folders. :param disallow: a list of :func:`~fnmatch.fnmatch` rules. :param fallback_mimetype: the fallback mimetype for unknown files. :param cache: enable or disable caching headers. :Param cache_timeout: the cache timeout in seconds for the headers. """ def __init__(self, app, exports, disallow=None, cache=True, cache_timeout=60 * 60 * 12, fallback_mimetype='text/plain'): self.app = app self.exports = {} self.cache = cache self.cache_timeout = cache_timeout for key, value in iteritems(exports): if isinstance(value, tuple): loader = self.get_package_loader(*value) elif isinstance(value, string_types): if os.path.isfile(value): loader = self.get_file_loader(value) else: loader = self.get_directory_loader(value) else: raise TypeError('unknown def %r' % value) self.exports[key] = loader if disallow is not None: from fnmatch import fnmatch self.is_allowed = lambda x: not fnmatch(x, disallow) self.fallback_mimetype = fallback_mimetype def is_allowed(self, filename): """Subclasses can override this method to disallow the access to certain files. However by providing `disallow` in the constructor this method is overwritten. """ return True def _opener(self, filename): return lambda: ( open(filename, 'rb'), datetime.utcfromtimestamp(os.path.getmtime(filename)), int(os.path.getsize(filename)) ) def get_file_loader(self, filename): return lambda x: (os.path.basename(filename), self._opener(filename)) def get_package_loader(self, package, package_path): from pkg_resources import DefaultProvider, ResourceManager, \ get_provider loadtime = datetime.utcnow() provider = get_provider(package) manager = ResourceManager() filesystem_bound = isinstance(provider, DefaultProvider) def loader(path): if path is None: return None, None path = posixpath.join(package_path, path) if not provider.has_resource(path): return None, None basename = posixpath.basename(path) if filesystem_bound: return basename, self._opener( provider.get_resource_filename(manager, path)) return basename, lambda: ( provider.get_resource_stream(manager, path), loadtime, 0 ) return loader def get_directory_loader(self, directory): def loader(path): if path is not None: path = os.path.join(directory, path) else: path = directory if os.path.isfile(path): return os.path.basename(path), self._opener(path) return None, None return loader def generate_etag(self, mtime, file_size, real_filename): if not isinstance(real_filename, bytes): real_filename = real_filename.encode(sys.getfilesystemencoding()) return 'wzsdm-%d-%s-%s' % ( mktime(mtime.timetuple()), file_size, adler32(real_filename) & 0xffffffff ) def __call__(self, environ, start_response): cleaned_path = get_path_info(environ) if PY2: cleaned_path = cleaned_path.encode(sys.getfilesystemencoding()) # sanitize the path for non unix systems cleaned_path = cleaned_path.strip('/') for sep in os.sep, os.altsep: if sep and sep != '/': cleaned_path = cleaned_path.replace(sep, '/') path = '/'.join([''] + [x for x in cleaned_path.split('/') if x and x != '..']) file_loader = None for search_path, loader in iteritems(self.exports): if search_path == path: real_filename, file_loader = loader(None) if file_loader is not None: break if not search_path.endswith('/'): search_path += '/' if path.startswith(search_path): real_filename, file_loader = loader(path[len(search_path):]) if file_loader is not None: break if file_loader is None or not self.is_allowed(real_filename): return self.app(environ, start_response) guessed_type = mimetypes.guess_type(real_filename) mime_type = guessed_type[0] or self.fallback_mimetype f, mtime, file_size = file_loader() headers = [('Date', http_date())] if self.cache: timeout = self.cache_timeout etag = self.generate_etag(mtime, file_size, real_filename) headers += [ ('Etag', '"%s"' % etag), ('Cache-Control', 'max-age=%d, public' % timeout) ] if not is_resource_modified(environ, etag, last_modified=mtime): f.close() start_response('304 Not Modified', headers) return [] headers.append(('Expires', http_date(time() + timeout))) else: headers.append(('Cache-Control', 'public')) headers.extend(( ('Content-Type', mime_type), ('Content-Length', str(file_size)), ('Last-Modified', http_date(mtime)) )) start_response('200 OK', headers) return wrap_file(environ, f) class DispatcherMiddleware(object): """Allows one to mount middlewares or applications in a WSGI application. This is useful if you want to combine multiple WSGI applications:: app = DispatcherMiddleware(app, { '/app2': app2, '/app3': app3 }) """ def __init__(self, app, mounts=None): self.app = app self.mounts = mounts or {} def __call__(self, environ, start_response): script = environ.get('PATH_INFO', '') path_info = '' while '/' in script: if script in self.mounts: app = self.mounts[script] break items = script.split('/') script = '/'.join(items[:-1]) path_info = '/%s%s' % (items[-1], path_info) else: app = self.mounts.get(script, self.app) original_script_name = environ.get('SCRIPT_NAME', '') environ['SCRIPT_NAME'] = original_script_name + script environ['PATH_INFO'] = path_info return app(environ, start_response) @implements_iterator class ClosingIterator(object): """The WSGI specification requires that all middlewares and gateways respect the `close` callback of an iterator. Because it is useful to add another close action to a returned iterator and adding a custom iterator is a boring task this class can be used for that:: return ClosingIterator(app(environ, start_response), [cleanup_session, cleanup_locals]) If there is just one close function it can be passed instead of the list. A closing iterator is not needed if the application uses response objects and finishes the processing if the response is started:: try: return response(environ, start_response) finally: cleanup_session() cleanup_locals() """ def __init__(self, iterable, callbacks=None): iterator = iter(iterable) self._next = partial(next, iterator) if callbacks is None: callbacks = [] elif callable(callbacks): callbacks = [callbacks] else: callbacks = list(callbacks) iterable_close = getattr(iterator, 'close', None) if iterable_close: callbacks.insert(0, iterable_close) self._callbacks = callbacks def __iter__(self): return self def __next__(self): return self._next() def close(self): for callback in self._callbacks: callback() def wrap_file(environ, file, buffer_size=8192): """Wraps a file. This uses the WSGI server's file wrapper if available or otherwise the generic :class:`FileWrapper`. .. versionadded:: 0.5 If the file wrapper from the WSGI server is used it's important to not iterate over it from inside the application but to pass it through unchanged. If you want to pass out a file wrapper inside a response object you have to set :attr:`~BaseResponse.direct_passthrough` to `True`. More information about file wrappers are available in :pep:`333`. :param file: a :class:`file`-like object with a :meth:`~file.read` method. :param buffer_size: number of bytes for one iteration. """ return environ.get('wsgi.file_wrapper', FileWrapper)(file, buffer_size) @implements_iterator class FileWrapper(object): """This class can be used to convert a :class:`file`-like object into an iterable. It yields `buffer_size` blocks until the file is fully read. You should not use this class directly but rather use the :func:`wrap_file` function that uses the WSGI server's file wrapper support if it's available. .. versionadded:: 0.5 If you're using this object together with a :class:`BaseResponse` you have to use the `direct_passthrough` mode. :param file: a :class:`file`-like object with a :meth:`~file.read` method. :param buffer_size: number of bytes for one iteration. """ def __init__(self, file, buffer_size=8192): self.file = file self.buffer_size = buffer_size def close(self): if hasattr(self.file, 'close'): self.file.close() def __iter__(self): return self def __next__(self): data = self.file.read(self.buffer_size) if data: return data raise StopIteration() def _make_chunk_iter(stream, limit, buffer_size): """Helper for the line and chunk iter functions.""" if isinstance(stream, (bytes, bytearray, text_type)): raise TypeError('Passed a string or byte object instead of ' 'true iterator or stream.') if not hasattr(stream, 'read'): for item in stream: if item: yield item return if not isinstance(stream, LimitedStream) and limit is not None: stream = LimitedStream(stream, limit) _read = stream.read while 1: item = _read(buffer_size) if not item: break yield item def make_line_iter(stream, limit=None, buffer_size=10 * 1024): """Safely iterates line-based over an input stream. If the input stream is not a :class:`LimitedStream` the `limit` parameter is mandatory. This uses the stream's :meth:`~file.read` method internally as opposite to the :meth:`~file.readline` method that is unsafe and can only be used in violation of the WSGI specification. The same problem applies to the `__iter__` function of the input stream which calls :meth:`~file.readline` without arguments. If you need line-by-line processing it's strongly recommended to iterate over the input stream using this helper function. .. versionchanged:: 0.8 This function now ensures that the limit was reached. .. versionadded:: 0.9 added support for iterators as input stream. :param stream: the stream or iterate to iterate over. :param limit: the limit in bytes for the stream. (Usually content length. Not necessary if the `stream` is a :class:`LimitedStream`. :param buffer_size: The optional buffer size. """ _iter = _make_chunk_iter(stream, limit, buffer_size) first_item = next(_iter, '') if not first_item: return s = make_literal_wrapper(first_item) empty = s('') cr = s('\r') lf = s('\n') crlf = s('\r\n') _iter = chain((first_item,), _iter) def _iter_basic_lines(): _join = empty.join buffer = [] while 1: new_data = next(_iter, '') if not new_data: break new_buf = [] for item in chain(buffer, new_data.splitlines(True)): new_buf.append(item) if item and item[-1:] in crlf: yield _join(new_buf) new_buf = [] buffer = new_buf if buffer: yield _join(buffer) # This hackery is necessary to merge 'foo\r' and '\n' into one item # of 'foo\r\n' if we were unlucky and we hit a chunk boundary. previous = empty for item in _iter_basic_lines(): if item == lf and previous[-1:] == cr: previous += item item = empty if previous: yield previous previous = item if previous: yield previous def make_chunk_iter(stream, separator, limit=None, buffer_size=10 * 1024): """Works like :func:`make_line_iter` but accepts a separator which divides chunks. If you want newline based processing you should use :func:`make_line_iter` instead as it supports arbitrary newline markers. .. versionadded:: 0.8 .. versionadded:: 0.9 added support for iterators as input stream. :param stream: the stream or iterate to iterate over. :param separator: the separator that divides chunks. :param limit: the limit in bytes for the stream. (Usually content length. Not necessary if the `stream` is otherwise already limited). :param buffer_size: The optional buffer size. """ _iter = _make_chunk_iter(stream, limit, buffer_size) first_item = next(_iter, '') if not first_item: return _iter = chain((first_item,), _iter) if isinstance(first_item, text_type): separator = to_unicode(separator) _split = re.compile(r'(%s)' % re.escape(separator)).split _join = u''.join else: separator = to_bytes(separator) _split = re.compile(b'(' + re.escape(separator) + b')').split _join = b''.join buffer = [] while 1: new_data = next(_iter, '') if not new_data: break chunks = _split(new_data) new_buf = [] for item in chain(buffer, chunks): if item == separator: yield _join(new_buf) new_buf = [] else: new_buf.append(item) buffer = new_buf if buffer: yield _join(buffer) @implements_iterator class LimitedStream(object): """Wraps a stream so that it doesn't read more than n bytes. If the stream is exhausted and the caller tries to get more bytes from it :func:`on_exhausted` is called which by default returns an empty string. The return value of that function is forwarded to the reader function. So if it returns an empty string :meth:`read` will return an empty string as well. The limit however must never be higher than what the stream can output. Otherwise :meth:`readlines` will try to read past the limit. .. admonition:: Note on WSGI compliance calls to :meth:`readline` and :meth:`readlines` are not WSGI compliant because it passes a size argument to the readline methods. Unfortunately the WSGI PEP is not safely implementable without a size argument to :meth:`readline` because there is no EOF marker in the stream. As a result of that the use of :meth:`readline` is discouraged. For the same reason iterating over the :class:`LimitedStream` is not portable. It internally calls :meth:`readline`. We strongly suggest using :meth:`read` only or using the :func:`make_line_iter` which safely iterates line-based over a WSGI input stream. :param stream: the stream to wrap. :param limit: the limit for the stream, must not be longer than what the string can provide if the stream does not end with `EOF` (like `wsgi.input`) """ def __init__(self, stream, limit): self._read = stream.read self._readline = stream.readline self._pos = 0 self.limit = limit def __iter__(self): return self @property def is_exhausted(self): """If the stream is exhausted this attribute is `True`.""" return self._pos >= self.limit def on_exhausted(self): """This is called when the stream tries to read past the limit. The return value of this function is returned from the reading function. """ # Read null bytes from the stream so that we get the # correct end of stream marker. return self._read(0) def on_disconnect(self): """What should happen if a disconnect is detected? The return value of this function is returned from read functions in case the client went away. By default a :exc:`~werkzeug.exceptions.ClientDisconnected` exception is raised. """ from werkzeug.exceptions import ClientDisconnected raise ClientDisconnected() def exhaust(self, chunk_size=1024 * 64): """Exhaust the stream. This consumes all the data left until the limit is reached. :param chunk_size: the size for a chunk. It will read the chunk until the stream is exhausted and throw away the results. """ to_read = self.limit - self._pos chunk = chunk_size while to_read > 0: chunk = min(to_read, chunk) self.read(chunk) to_read -= chunk def read(self, size=None): """Read `size` bytes or if size is not provided everything is read. :param size: the number of bytes read. """ if self._pos >= self.limit: return self.on_exhausted() if size is None or size == -1: # -1 is for consistence with file size = self.limit to_read = min(self.limit - self._pos, size) try: read = self._read(to_read) except (IOError, ValueError): return self.on_disconnect() if to_read and len(read) != to_read: return self.on_disconnect() self._pos += len(read) return read def readline(self, size=None): """Reads one line from the stream.""" if self._pos >= self.limit: return self.on_exhausted() if size is None: size = self.limit - self._pos else: size = min(size, self.limit - self._pos) try: line = self._readline(size) except (ValueError, IOError): return self.on_disconnect() if size and not line: return self.on_disconnect() self._pos += len(line) return line def readlines(self, size=None): """Reads a file into a list of strings. It calls :meth:`readline` until the file is read to the end. It does support the optional `size` argument if the underlaying stream supports it for `readline`. """ last_pos = self._pos result = [] if size is not None: end = min(self.limit, last_pos + size) else: end = self.limit while 1: if size is not None: size -= last_pos - self._pos if self._pos >= end: break result.append(self.readline(size)) if size is not None: last_pos = self._pos return result def tell(self): """Returns the position of the stream. .. versionadded:: 0.9 """ return self._pos def __next__(self): line = self.readline() if not line: raise StopIteration() return line
agpl-3.0
tuxinhang1989/mezzanine
mezzanine/utils/html.py
9
3678
from __future__ import absolute_import, unicode_literals from future.builtins import chr, int, str try: from html.parser import HTMLParser, HTMLParseError from html.entities import name2codepoint except ImportError: # Python 2 from HTMLParser import HTMLParser, HTMLParseError from htmlentitydefs import name2codepoint import re SELF_CLOSING_TAGS = ['br', 'img'] NON_SELF_CLOSING_TAGS = ['script', 'iframe'] ABSOLUTE_URL_TAGS = {"img": "src", "a": "href", "iframe": "src"} def absolute_urls(html): """ Converts relative URLs into absolute URLs. Used for RSS feeds to provide more complete HTML for item descriptions, but could also be used as a general richtext filter. """ from bs4 import BeautifulSoup from mezzanine.core.request import current_request request = current_request() if request is not None: dom = BeautifulSoup(html, "html.parser") for tag, attr in ABSOLUTE_URL_TAGS.items(): for node in dom.findAll(tag): url = node.get(attr, "") if url: node[attr] = request.build_absolute_uri(url) html = str(dom) return html def decode_entities(html): """ Remove HTML entities from a string. Adapted from http://effbot.org/zone/re-sub.htm#unescape-html """ def decode(m): html = m.group(0) if html[:2] == "&#": try: if html[:3] == "&#x": return chr(int(html[3:-1], 16)) else: return chr(int(html[2:-1])) except ValueError: pass else: try: html = chr(name2codepoint[html[1:-1]]) except KeyError: pass return html return re.sub("&#?\w+;", decode, html.replace("&amp;", "&")) def thumbnails(html): """ Given a HTML string, converts paths in img tags to thumbnail paths, using Mezzanine's ``thumbnail`` template tag. Used as one of the default values in the ``RICHTEXT_FILTERS`` setting. """ from django.conf import settings from bs4 import BeautifulSoup from mezzanine.core.templatetags.mezzanine_tags import thumbnail # If MEDIA_URL isn't in the HTML string, there aren't any # images to replace, so bail early. if settings.MEDIA_URL.lower() not in html.lower(): return html dom = BeautifulSoup(html, "html.parser") for img in dom.findAll("img"): src = img.get("src", "") src_in_media = src.lower().startswith(settings.MEDIA_URL.lower()) width = img.get("width") height = img.get("height") if src_in_media and width and height: img["src"] = settings.MEDIA_URL + thumbnail(src, width, height) # BS adds closing br tags, which the browser interprets as br tags. return str(dom).replace("</br>", "") class TagCloser(HTMLParser): """ HTMLParser that closes open tags. Takes a HTML string as its first arg, and populate a ``html`` attribute on the parser with the original HTML arg and any required closing tags. """ def __init__(self, html): HTMLParser.__init__(self) self.html = html self.tags = [] try: self.feed(self.html) except HTMLParseError: pass else: self.html += "".join(["</%s>" % tag for tag in self.tags]) def handle_starttag(self, tag, attrs): if tag not in SELF_CLOSING_TAGS: self.tags.insert(0, tag) def handle_endtag(self, tag): try: self.tags.remove(tag) except ValueError: pass
bsd-2-clause
daviwesley/Empire
lib/modules/situational_awareness/host/computerdetails.py
19
4385
from lib.common import helpers class Module: def __init__(self, mainMenu, params=[]): self.info = { 'Name': 'Get-ComputerDetails', 'Author': ['@JosephBialek'], 'Description': ('Enumerates useful information on the system. By default, all checks are run.'), 'Background' : True, 'OutputExtension' : None, 'NeedsAdmin' : True, 'OpsecSafe' : True, 'MinPSVersion' : '2', 'Comments': [ 'https://github.com/mattifestation/PowerSploit/blob/master/Recon/Get-ComputerDetails.ps1' ] } # any options needed by the module, settable during runtime self.options = { # format: # value_name : {description, required, default_value} 'Agent' : { 'Description' : 'Agent to run module on.', 'Required' : True, 'Value' : '' }, '4648' : { 'Description' : 'Switch. Only return 4648 logon information (RDP to another machine).', 'Required' : False, 'Value' : '' }, '4624' : { 'Description' : 'Switch. Only return 4624 logon information (logons to this machine).', 'Required' : False, 'Value' : '' }, 'AppLocker' : { 'Description' : 'Switch. Only return AppLocker logs.', 'Required' : False, 'Value' : '' }, 'PSScripts' : { 'Description' : 'Switch. Only return PowerShell scripts run from operational log.', 'Required' : False, 'Value' : '' }, 'SavedRDP' : { 'Description' : 'Switch. Only return saved RDP connections.', 'Required' : False, 'Value' : '' } } # save off a copy of the mainMenu object to access external functionality # like listeners/agent handlers/etc. self.mainMenu = mainMenu for param in params: # parameter format is [Name, Value] option, value = param if option in self.options: self.options[option]['Value'] = value def generate(self): # read in the common module source code moduleSource = self.mainMenu.installPath + "/data/module_source/situational_awareness/host/Get-ComputerDetails.ps1" try: f = open(moduleSource, 'r') except: print helpers.color("[!] Could not read module source path at: " + str(moduleSource)) return "" moduleCode = f.read() f.close() script = moduleCode for option,values in self.options.iteritems(): if option.lower() != "agent": if values['Value'] and values['Value'] != '': if option == "4648": script += "$SecurityLog = Get-EventLog -LogName Security;$Filtered4624 = Find-4624Logons $SecurityLog;Write-Output $Filtered4624.Values | Format-List" return script if option == "4624": script += "$SecurityLog = Get-EventLog -LogName Security;$Filtered4648 = Find-4648Logons $SecurityLog;Write-Output $Filtered4648.Values | Format-List" return script if option == "AppLocker": script += "$AppLockerLogs = Find-AppLockerLogs;Write-Output $AppLockerLogs.Values | Format-List" return script if option == "PSLogs": script += "$PSLogs = Find-PSScriptsInPSAppLog;Write-Output $PSLogs.Values | Format-List" return script if option == "SavedRDP": script += "$RdpClientData = Find-RDPClientConnections;Write-Output $RdpClientData.Values | Format-List" return script # if we get to this point, no switched were specified return script + "Get-ComputerDetails -ToString"
bsd-3-clause
mrkulk/text-world
evennia/server/portal/irc.py
2
6997
""" This connects to an IRC network/channel and launches an 'bot' onto it. The bot then pipes what is being said between the IRC channel and one or more Evennia channels. """ import re from twisted.application import internet from twisted.words.protocols import irc from twisted.internet import protocol from evennia.server.session import Session from evennia.utils import logger, utils # IRC colors IRC_BOLD = "\002" IRC_COLOR = "\003" IRC_RESET = "\017" IRC_ITALIC = "\026" IRC_NORMAL = "99" IRC_UNDERLINE = "37" IRC_WHITE = "0" IRC_BLACK = "1" IRC_DBLUE = "2" IRC_DGREEN = "3" IRC_RED = "4" IRC_DRED = "5" IRC_DMAGENTA = "6" IRC_DYELLOW = "7" IRC_YELLOW = "8" IRC_GREEN = "9" IRC_DCYAN = "10" IRC_CYAN = "11" IRC_BLUE = "12" IRC_MAGENTA = "13" IRC_DGREY = "14" IRC_GRAY = "15" # test: # {rred {ggreen {yyellow {bblue {mmagenta {ccyan {wwhite {xdgrey # {Rdred {Gdgreen {Ydyellow {Bdblue {Mdmagenta {Cdcyan {Wlgrey {Xblack # {[rredbg {[ggreenbg {[yyellowbg {[bbluebg {[mmagentabg {[ccyanbg {[wlgreybg {[xblackbg IRC_COLOR_MAP = dict([ (r'{n', IRC_RESET), # reset (r'{/', ""), # line break (r'{-', " "), # tab (r'{_', " "), # space (r'{*', ""), # invert (r'{^', ""), # blinking text (r'{r', IRC_COLOR + IRC_RED), (r'{g', IRC_COLOR + IRC_GREEN), (r'{y', IRC_COLOR + IRC_YELLOW), (r'{b', IRC_COLOR + IRC_BLUE), (r'{m', IRC_COLOR + IRC_MAGENTA), (r'{c', IRC_COLOR + IRC_CYAN), (r'{w', IRC_COLOR + IRC_WHITE), # pure white (r'{x', IRC_COLOR + IRC_DGREY), # dark grey (r'{R', IRC_COLOR + IRC_DRED), (r'{G', IRC_COLOR + IRC_DGREEN), (r'{Y', IRC_COLOR + IRC_DYELLOW), (r'{B', IRC_COLOR + IRC_DBLUE), (r'{M', IRC_COLOR + IRC_DMAGENTA), (r'{C', IRC_COLOR + IRC_DCYAN), (r'{W', IRC_COLOR + IRC_GRAY), # light grey (r'{X', IRC_COLOR + IRC_BLACK), # pure black (r'{[r', IRC_COLOR + IRC_NORMAL + "," + IRC_DRED), (r'{[g', IRC_COLOR + IRC_NORMAL + "," + IRC_DGREEN), (r'{[y', IRC_COLOR + IRC_NORMAL + "," + IRC_DYELLOW), (r'{[b', IRC_COLOR + IRC_NORMAL + "," + IRC_DBLUE), (r'{[m', IRC_COLOR + IRC_NORMAL + "," + IRC_DMAGENTA), (r'{[c', IRC_COLOR + IRC_NORMAL + "," + IRC_DCYAN), (r'{[w', IRC_COLOR + IRC_NORMAL + "," + IRC_GRAY), # light grey background (r'{[x', IRC_COLOR + IRC_NORMAL + "," + IRC_BLACK) # pure black background ]) RE_IRC_COLOR = re.compile(r"|".join([re.escape(key) for key in IRC_COLOR_MAP.keys()]), re.DOTALL) RE_MXP = re.compile(r'\{lc(.*?)\{lt(.*?)\{le', re.DOTALL) RE_ANSI_ESCAPES = re.compile(r"(%s)" % "|".join(("{{", "%%", "\\\\")), re.DOTALL) def sub_irc(ircmatch): return IRC_COLOR_MAP.get(ircmatch.group(), "") def parse_irc_colors(string): """ Parse {-type syntax and replace with IRC color markers """ in_string = utils.to_str(string) parsed_string = "" parts = RE_ANSI_ESCAPES.split(in_string) + [" "] for part, sep in zip(parts[::2], parts[1::2]): pstring = RE_IRC_COLOR.sub(sub_irc, part) parsed_string += "%s%s" % (pstring, sep[0].strip()) # strip mxp parsed_string = RE_MXP.sub(r'\2', parsed_string) return parsed_string # IRC bot class IRCBot(irc.IRCClient, Session): """ An IRC bot that tracks actitivity in a channel as well as sends text to it when prompted """ lineRate = 1 # assigned by factory at creation nickname = None logger = None factory = None channel = None def signedOn(self): """ This is called when we successfully connect to the network. We make sure to now register with the game as a full session. """ self.join(self.channel) self.stopping = False self.factory.bot = self address = "%s@%s" % (self.channel, self.network) self.init_session("ircbot", address, self.factory.sessionhandler) # we link back to our bot and log in self.uid = int(self.factory.uid) self.logged_in = True self.factory.sessionhandler.connect(self) logger.log_infomsg("IRC bot '%s' connected to %s at %s:%s." % (self.nickname, self.channel, self.network, self.port)) def disconnect(self, reason=None): """ Called by sessionhandler to disconnect this protocol """ print "irc disconnect called!" self.sessionhandler.disconnect(self) self.stopping = True self.transport.loseConnection() def privmsg(self, user, channel, msg): "A message was sent to channel" if not msg.startswith('***'): user = user.split('!', 1)[0] self.data_in("bot_data_in %s@%s: %s" % (user, channel, msg)) def action(self, user, channel, msg): "An action was done in channel" if not msg.startswith('**'): user = user.split('!', 1)[0] self.data_in("bot_data_in %s@%s %s" % (user, channel, msg)) def data_in(self, text=None, **kwargs): "Data IRC -> Server" self.sessionhandler.data_in(self, text=text, **kwargs) def data_out(self, text=None, **kwargs): "Data from server-> IRC" if text.startswith("bot_data_out"): text = text.split(" ", 1)[1] text = parse_irc_colors(text) self.say(self.channel, text) class IRCBotFactory(protocol.ReconnectingClientFactory): """ Creates instances of AnnounceBot, connecting with a staggered increase in delay """ # scaling reconnect time initialDelay = 1 factor = 1.5 maxDelay = 60 def __init__(self, sessionhandler, uid=None, botname=None, channel=None, network=None, port=None): "Storing some important protocol properties" self.sessionhandler = sessionhandler self.uid = uid self.nickname = str(botname) self.channel = str(channel) self.network = str(network) self.port = port self.bot = None def buildProtocol(self, addr): "Build the protocol and assign it some properties" protocol = IRCBot() protocol.factory = self protocol.nickname = self.nickname protocol.channel = self.channel protocol.network = self.network protocol.port = self.port return protocol def startedConnecting(self, connector): "Tracks reconnections for debugging" logger.log_infomsg("(re)connecting to %s" % self.channel) def clientConnectionFailed(self, connector, reason): self.retry(connector) def clientConnectionLost(self, connector, reason): if not self.bot.stopping: self.retry(connector) def start(self): "Connect session to sessionhandler" if self.port: service = internet.TCPClient(self.network, int(self.port), self) self.sessionhandler.portal.services.addService(service)
bsd-3-clause
HyperBaton/ansible
lib/ansible/modules/storage/netapp/na_ontap_license.py
41
10704
#!/usr/bin/python # (c) 2018-2019, NetApp, Inc # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import absolute_import, division, print_function __metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ['preview'], 'supported_by': 'certified'} DOCUMENTATION = ''' module: na_ontap_license short_description: NetApp ONTAP protocol and feature licenses extends_documentation_fragment: - netapp.na_ontap version_added: '2.6' author: NetApp Ansible Team (@carchi8py) <ng-ansibleteam@netapp.com> description: - Add or remove licenses on NetApp ONTAP. options: state: description: - Whether the specified license should exist or not. choices: ['present', 'absent'] default: present remove_unused: description: - Remove licenses that have no controller affiliation in the cluster. type: bool remove_expired: description: - Remove licenses that have expired in the cluster. type: bool serial_number: description: Serial number of the node associated with the license. This parameter is used primarily when removing license for a specific service. license_names: description: - List of license-names to delete. suboptions: base: description: - Cluster Base License nfs: description: - NFS License cifs: description: - CIFS License iscsi: description: - iSCSI License fcp: description: - FCP License cdmi: description: - CDMI License snaprestore: description: - SnapRestore License snapmirror: description: - SnapMirror License flexclone: description: - FlexClone License snapvault: description: - SnapVault License snaplock: description: - SnapLock License snapmanagersuite: description: - SnapManagerSuite License snapprotectapps: description: - SnapProtectApp License v_storageattach: description: - Virtual Attached Storage License license_codes: description: - List of license codes to be added. ''' EXAMPLES = """ - name: Add licenses na_ontap_license: state: present hostname: "{{ netapp_hostname }}" username: "{{ netapp_username }}" password: "{{ netapp_password }}" serial_number: ################# license_codes: CODE1,CODE2 - name: Remove licenses na_ontap_license: state: absent hostname: "{{ netapp_hostname }}" username: "{{ netapp_username }}" password: "{{ netapp_password }}" remove_unused: false remove_expired: true serial_number: ################# license_names: nfs,cifs """ RETURN = """ """ import traceback from ansible.module_utils.basic import AnsibleModule from ansible.module_utils._text import to_native import ansible.module_utils.netapp as netapp_utils HAS_NETAPP_LIB = netapp_utils.has_netapp_lib() def local_cmp(a, b): """ compares with only values and not keys, keys should be the same for both dicts :param a: dict 1 :param b: dict 2 :return: difference of values in both dicts """ diff = [key for key in a if a[key] != b[key]] return len(diff) class NetAppOntapLicense(object): '''ONTAP license class''' def __init__(self): self.argument_spec = netapp_utils.na_ontap_host_argument_spec() self.argument_spec.update(dict( state=dict(required=False, choices=[ 'present', 'absent'], default='present'), serial_number=dict(required=False, type='str'), remove_unused=dict(default=None, type='bool'), remove_expired=dict(default=None, type='bool'), license_codes=dict(default=None, type='list'), license_names=dict(default=None, type='list'), )) self.module = AnsibleModule( argument_spec=self.argument_spec, supports_check_mode=False, required_if=[ ('state', 'absent', ['serial_number', 'license_names'])] ) parameters = self.module.params # set up state variables self.state = parameters['state'] self.serial_number = parameters['serial_number'] self.remove_unused = parameters['remove_unused'] self.remove_expired = parameters['remove_expired'] self.license_codes = parameters['license_codes'] self.license_names = parameters['license_names'] if HAS_NETAPP_LIB is False: self.module.fail_json( msg="the python NetApp-Lib module is required") else: self.server = netapp_utils.setup_na_ontap_zapi(module=self.module) def get_licensing_status(self): """ Check licensing status :return: package (key) and licensing status (value) :rtype: dict """ license_status = netapp_utils.zapi.NaElement( 'license-v2-status-list-info') result = None try: result = self.server.invoke_successfully(license_status, enable_tunneling=False) except netapp_utils.zapi.NaApiError as error: self.module.fail_json(msg="Error checking license status: %s" % to_native(error), exception=traceback.format_exc()) return_dictionary = {} license_v2_status = result.get_child_by_name('license-v2-status') if license_v2_status: for license_v2_status_info in license_v2_status.get_children(): package = license_v2_status_info.get_child_content('package') status = license_v2_status_info.get_child_content('method') return_dictionary[package] = status return return_dictionary def remove_licenses(self, package_name): """ Remove requested licenses :param: package_name: Name of the license to be deleted """ license_delete = netapp_utils.zapi.NaElement('license-v2-delete') license_delete.add_new_child('serial-number', self.serial_number) license_delete.add_new_child('package', package_name) try: self.server.invoke_successfully(license_delete, enable_tunneling=False) return True except netapp_utils.zapi.NaApiError as error: # Error 15661 - Object not found if to_native(error.code) == "15661": return False else: self.module.fail_json(msg="Error removing license %s" % to_native(error), exception=traceback.format_exc()) def remove_unused_licenses(self): """ Remove unused licenses """ remove_unused = netapp_utils.zapi.NaElement('license-v2-delete-unused') try: self.server.invoke_successfully(remove_unused, enable_tunneling=False) except netapp_utils.zapi.NaApiError as error: self.module.fail_json(msg="Error removing unused licenses: %s" % to_native(error), exception=traceback.format_exc()) def remove_expired_licenses(self): """ Remove expired licenses """ remove_expired = netapp_utils.zapi.NaElement( 'license-v2-delete-expired') try: self.server.invoke_successfully(remove_expired, enable_tunneling=False) except netapp_utils.zapi.NaApiError as error: self.module.fail_json(msg="Error removing expired licenses: %s" % to_native(error), exception=traceback.format_exc()) def add_licenses(self): """ Add licenses """ license_add = netapp_utils.zapi.NaElement('license-v2-add') codes = netapp_utils.zapi.NaElement('codes') for code in self.license_codes: codes.add_new_child('license-code-v2', str(code.strip().lower())) license_add.add_child_elem(codes) try: self.server.invoke_successfully(license_add, enable_tunneling=False) except netapp_utils.zapi.NaApiError as error: self.module.fail_json(msg="Error adding licenses: %s" % to_native(error), exception=traceback.format_exc()) def apply(self): '''Call add, delete or modify methods''' changed = False create_license = False remove_license = False results = netapp_utils.get_cserver(self.server) cserver = netapp_utils.setup_na_ontap_zapi( module=self.module, vserver=results) netapp_utils.ems_log_event("na_ontap_license", cserver) # Add / Update licenses. license_status = self.get_licensing_status() if self.state == 'absent': # delete changed = True else: # add or update if self.license_codes is not None: create_license = True changed = True if self.remove_unused is not None: remove_license = True changed = True if self.remove_expired is not None: remove_license = True changed = True if changed: if self.state == 'present': # execute create if create_license: self.add_licenses() if self.remove_unused is not None: self.remove_unused_licenses() if self.remove_expired is not None: self.remove_expired_licenses() if create_license or remove_license: new_license_status = self.get_licensing_status() if local_cmp(license_status, new_license_status) == 0: changed = False else: # execute delete license_deleted = False for package in self.license_names: license_deleted |= self.remove_licenses(package) changed = license_deleted self.module.exit_json(changed=changed) def main(): '''Apply license operations''' obj = NetAppOntapLicense() obj.apply() if __name__ == '__main__': main()
gpl-3.0
lordmos/blink
Source/bindings/scripts/unstable/idl_compiler.py
1
5668
#!/usr/bin/python # Copyright (C) 2013 Google Inc. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above # copyright notice, this list of conditions and the following disclaimer # in the documentation and/or other materials provided with the # distribution. # * Neither the name of Google Inc. nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """Compile an .idl file to Blink V8 bindings (.h and .cpp files). FIXME: Not currently used in build. This is a rewrite of the Perl IDL compiler in Python, but is not complete. Once it is complete, we will switch all IDL files over to Python at once. Until then, please work on the Perl IDL compiler. For details, see bug http://crbug.com/239771 """ import optparse import os import pickle import posixpath import shlex import sys import code_generator_v8 import idl_reader module_path, _ = os.path.split(__file__) source_path = os.path.normpath(os.path.join(module_path, os.pardir, os.pardir, os.pardir)) def parse_options(): parser = optparse.OptionParser() parser.add_option('--additional-idl-files') # FIXME: The --dump-json-and-pickle option is only for debugging and will # be removed once we complete migrating all IDL files from the Perl flow to # the Python flow. parser.add_option('--dump-json-and-pickle', action='store_true', default=False) parser.add_option('--idl-attributes-file') parser.add_option('--include', dest='idl_directories', action='append') parser.add_option('--output-directory') parser.add_option('--interface-dependencies-file') parser.add_option('--verbose', action='store_true', default=False) parser.add_option('--write-file-only-if-changed', type='int') # ensure output comes last, so command line easy to parse via regexes parser.disable_interspersed_args() options, args = parser.parse_args() if options.output_directory is None: parser.error('Must specify output directory using --output-directory.') if options.additional_idl_files is None: options.additional_idl_files = [] else: # additional_idl_files is passed as a string with varied (shell-style) # quoting, hence needs parsing. options.additional_idl_files = shlex.split(options.additional_idl_files) if len(args) != 1: parser.error('Must specify exactly 1 input file as argument, but %d given.' % len(args)) options.idl_filename = os.path.realpath(args[0]) return options def get_relative_dir_posix(filename): """Returns directory of a local file relative to Source, in POSIX format.""" relative_path_local = os.path.relpath(filename, source_path) relative_dir_local = os.path.dirname(relative_path_local) return relative_dir_local.replace(os.path.sep, posixpath.sep) def write_json_and_pickle(definitions, interface_name, output_directory): json_string = definitions.to_json() json_basename = interface_name + '.json' json_filename = os.path.join(output_directory, json_basename) with open(json_filename, 'w') as json_file: json_file.write(json_string) pickle_basename = interface_name + '.pkl' pickle_filename = os.path.join(output_directory, pickle_basename) with open(pickle_filename, 'wb') as pickle_file: pickle.dump(definitions, pickle_file) def main(): options = parse_options() idl_filename = options.idl_filename basename = os.path.basename(idl_filename) interface_name, _ = os.path.splitext(basename) output_directory = options.output_directory verbose = options.verbose if verbose: print idl_filename relative_dir_posix = get_relative_dir_posix(idl_filename) reader = idl_reader.IdlReader(options.interface_dependencies_file, options.additional_idl_files, options.idl_attributes_file, output_directory, verbose) definitions = reader.read_idl_definitions(idl_filename) code_generator = code_generator_v8.CodeGeneratorV8(definitions, interface_name, options.output_directory, relative_dir_posix, options.idl_directories, verbose) if not definitions: # We generate dummy .h and .cpp files just to tell build scripts # that outputs have been created. code_generator.write_dummy_header_and_cpp() return if options.dump_json_and_pickle: write_json_and_pickle(definitions, interface_name, output_directory) return code_generator.write_header_and_cpp() if __name__ == '__main__': sys.exit(main())
mit
wyq200704/ardupilot
Tools/LogAnalyzer/LogAnalyzer.py
55
12485
#!/usr/bin/env python # # A module to analyze and identify any common problems which can be determined from log files # # Initial code by Andrew Chapman (amchapman@gmail.com), 16th Jan 2014 # # some logging oddities noticed while doing this, to be followed up on: # - tradheli MOT labels Mot1,Mot2,Mot3,Mot4,GGain # - Pixhawk doesn't output one of the FMT labels... forget which one # - MAG offsets seem to be constant (only seen data on Pixhawk) # - MAG offsets seem to be cast to int before being output? (param is -84.67, logged as -84) # - copter+plane use 'V' in their vehicle type/version/build line, rover uses lower case 'v'. Copter+Rover give a build number, plane does not # - CTUN.ThrOut on copter is 0-1000, on plane+rover it is 0-100 # TODO: add test for noisy baro values # TODO: support loading binary log files (use Tridge's mavlogdump?) import DataflashLog import pprint # temp import imp import glob import inspect import os, sys import argparse import datetime import time from xml.sax.saxutils import escape from VehicleType import VehicleType class TestResult(object): '''all tests return a standardized result type''' class StatusType: # NA means not applicable for this log (e.g. copter tests against a plane log), UNKNOWN means it is missing data required for the test GOOD, FAIL, WARN, UNKNOWN, NA = range(5) status = None statusMessage = "" # can be multi-line class Test(object): '''base class to be inherited by log tests. Each test should be quite granular so we have lots of small tests with clear results''' def __init__(self): self.name = "" self.result = None # will be an instance of TestResult after being run self.execTime = None self.enable = True def run(self, logdata, verbose=False): pass class TestSuite(object): '''registers test classes, loading using a basic plugin architecture, and can run them all in one run() operation''' def __init__(self): self.tests = [] self.logfile = None self.logdata = None # dynamically load in Test subclasses from the 'tests' folder # to prevent one being loaded, move it out of that folder, or set that test's .enable attribute to False dirName = os.path.dirname(os.path.abspath(__file__)) testScripts = glob.glob(dirName + '/tests/*.py') testClasses = [] for script in testScripts: m = imp.load_source("m",script) for name, obj in inspect.getmembers(m, inspect.isclass): if name not in testClasses and inspect.getsourcefile(obj) == script: testClasses.append(name) self.tests.append(obj()) # and here's an example of explicitly loading a Test class if you wanted to do that # m = imp.load_source("m", dirName + '/tests/TestBadParams.py') # self.tests.append(m.TestBadParams()) def run(self, logdata, verbose): '''run all registered tests in a single call, gathering execution timing info''' self.logdata = logdata if 'GPS' not in self.logdata.channels and 'GPS2' in self.logdata.channels: # *cough* self.logdata.channels['GPS'] = self.logdata.channels['GPS2'] self.logfile = logdata.filename for test in self.tests: # run each test in turn, gathering timing info if test.enable: startTime = time.time() test.run(self.logdata, verbose) # RUN THE TEST endTime = time.time() test.execTime = 1000 * (endTime-startTime) def outputPlainText(self, outputStats): '''output test results in plain text''' print 'Dataflash log analysis report for file: ' + self.logfile print 'Log size: %.2fmb (%d lines)' % (self.logdata.filesizeKB / 1024.0, self.logdata.lineCount) print 'Log duration: %s' % str(datetime.timedelta(seconds=self.logdata.durationSecs)) + '\n' if self.logdata.vehicleType == VehicleType.Copter and self.logdata.getCopterType(): print 'Vehicle Type: %s (%s)' % (self.logdata.vehicleTypeString, self.logdata.getCopterType()) else: print 'Vehicle Type: %s' % self.logdata.vehicleTypeString print 'Firmware Version: %s (%s)' % (self.logdata.firmwareVersion, self.logdata.firmwareHash) print 'Hardware: %s' % self.logdata.hardwareType print 'Free RAM: %s' % self.logdata.freeRAM if self.logdata.skippedLines: print "\nWARNING: %d malformed log lines skipped during read" % self.logdata.skippedLines print '\n' print "Test Results:" for test in self.tests: if not test.enable: continue statusMessageFirstLine = test.result.statusMessage.strip('\n\r').split('\n')[0] statusMessageExtra = test.result.statusMessage.strip('\n\r').split('\n')[1:] execTime = "" if outputStats: execTime = " (%6.2fms)" % (test.execTime) if test.result.status == TestResult.StatusType.GOOD: print " %20s: GOOD %-55s%s" % (test.name, statusMessageFirstLine, execTime) elif test.result.status == TestResult.StatusType.FAIL: print " %20s: FAIL %-55s%s [GRAPH]" % (test.name, statusMessageFirstLine, execTime) elif test.result.status == TestResult.StatusType.WARN: print " %20s: WARN %-55s%s [GRAPH]" % (test.name, statusMessageFirstLine, execTime) elif test.result.status == TestResult.StatusType.NA: # skip any that aren't relevant for this vehicle/hardware/etc continue else: print " %20s: UNKNOWN %-55s%s" % (test.name, statusMessageFirstLine, execTime) #if statusMessageExtra: for line in statusMessageExtra: print " %29s %s" % ("",line) print '\n' print 'The Log Analyzer is currently BETA code.\nFor any support or feedback on the log analyzer please email Andrew Chapman (amchapman@gmail.com)' print '\n' def outputXML(self, xmlFile): '''output test results to an XML file''' # open the file for writing xml = None try: if xmlFile == '-': xml = sys.stdout else: xml = open(xmlFile, 'w') except: sys.stderr.write("Error opening output xml file: %s" % xmlFile) sys.exit(1) # output header info print >>xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" print >>xml, "<loganalysis>" print >>xml, "<header>" print >>xml, " <logfile>" + escape(self.logfile) + "</logfile>" print >>xml, " <sizekb>" + escape(`self.logdata.filesizeKB`) + "</sizekb>" print >>xml, " <sizelines>" + escape(`self.logdata.lineCount`) + "</sizelines>" print >>xml, " <duration>" + escape(str(datetime.timedelta(seconds=self.logdata.durationSecs))) + "</duration>" print >>xml, " <vehicletype>" + escape(self.logdata.vehicleTypeString) + "</vehicletype>" if self.logdata.vehicleType == VehicleType.Copter and self.logdata.getCopterType(): print >>xml, " <coptertype>" + escape(self.logdata.getCopterType()) + "</coptertype>" print >>xml, " <firmwareversion>" + escape(self.logdata.firmwareVersion) + "</firmwareversion>" print >>xml, " <firmwarehash>" + escape(self.logdata.firmwareHash) + "</firmwarehash>" print >>xml, " <hardwaretype>" + escape(self.logdata.hardwareType) + "</hardwaretype>" print >>xml, " <freemem>" + escape(`self.logdata.freeRAM`) + "</freemem>" print >>xml, " <skippedlines>" + escape(`self.logdata.skippedLines`) + "</skippedlines>" print >>xml, "</header>" # output parameters print >>xml, "<params>" for param, value in self.logdata.parameters.items(): print >>xml, " <param name=\"%s\" value=\"%s\" />" % (param,escape(`value`)) print >>xml, "</params>" # output test results print >>xml, "<results>" for test in self.tests: if not test.enable: continue print >>xml, " <result>" if test.result.status == TestResult.StatusType.GOOD: print >>xml, " <name>" + escape(test.name) + "</name>" print >>xml, " <status>GOOD</status>" print >>xml, " <message>" + escape(test.result.statusMessage) + "</message>" elif test.result.status == TestResult.StatusType.FAIL: print >>xml, " <name>" + escape(test.name) + "</name>" print >>xml, " <status>FAIL</status>" print >>xml, " <message>" + escape(test.result.statusMessage) + "</message>" print >>xml, " <data>(test data will be embeded here at some point)</data>" elif test.result.status == TestResult.StatusType.WARN: print >>xml, " <name>" + escape(test.name) + "</name>" print >>xml, " <status>WARN</status>" print >>xml, " <message>" + escape(test.result.statusMessage) + "</message>" print >>xml, " <data>(test data will be embeded here at some point)</data>" elif test.result.status == TestResult.StatusType.NA: print >>xml, " <name>" + escape(test.name) + "</name>" print >>xml, " <status>NA</status>" else: print >>xml, " <name>" + escape(test.name) + "</name>" print >>xml, " <status>UNKNOWN</status>" print >>xml, " <message>" + escape(test.result.statusMessage) + "</message>" print >>xml, " </result>" print >>xml, "</results>" print >>xml, "</loganalysis>" xml.close() def main(): dirName = os.path.dirname(os.path.abspath(__file__)) # deal with command line arguments parser = argparse.ArgumentParser(description='Analyze an APM Dataflash log for known issues') parser.add_argument('logfile', type=argparse.FileType('r'), help='path to Dataflash log file (or - for stdin)') parser.add_argument('-f', '--format', metavar='', type=str, action='store', choices=['bin','log','auto'], default='auto', help='log file format: \'bin\',\'log\' or \'auto\'') parser.add_argument('-q', '--quiet', metavar='', action='store_const', const=True, help='quiet mode, do not print results') parser.add_argument('-p', '--profile', metavar='', action='store_const', const=True, help='output performance profiling data') parser.add_argument('-s', '--skip_bad', metavar='', action='store_const', const=True, help='skip over corrupt dataflash lines') parser.add_argument('-e', '--empty', metavar='', action='store_const', const=True, help='run an initial check for an empty log') parser.add_argument('-x', '--xml', type=str, metavar='XML file', nargs='?', const='', default='', help='write output to specified XML file (or - for stdout)') parser.add_argument('-v', '--verbose', metavar='', action='store_const', const=True, help='verbose output') args = parser.parse_args() # load the log startTime = time.time() logdata = DataflashLog.DataflashLog(args.logfile.name, format=args.format, ignoreBadlines=args.skip_bad) # read log endTime = time.time() if args.profile: print "Log file read time: %.2f seconds" % (endTime-startTime) # check for empty log if requested if args.empty: emptyErr = DataflashLog.DataflashLogHelper.isLogEmpty(logdata) if emptyErr: sys.stderr.write("Empty log file: %s, %s" % (logdata.filename, emptyErr)) sys.exit(1) #run the tests, and gather timings testSuite = TestSuite() startTime = time.time() testSuite.run(logdata, args.verbose) # run tests endTime = time.time() if args.profile: print "Test suite run time: %.2f seconds" % (endTime-startTime) # deal with output if not args.quiet: testSuite.outputPlainText(args.profile) if args.xml: testSuite.outputXML(args.xml) if not args.quiet: print "XML output written to file: %s\n" % args.xml if __name__ == "__main__": main()
gpl-3.0
zhlinh/leetcode
0173.Binary Search Tree Iterator/test.py
1
1230
#!/usr/bin/env python # -*- coding: utf-8 -*- from solution import TreeNode from solution import BSTIterator def constructOne(s): s = s.strip() if s == '#': return None else: return TreeNode(int(s)) def createTree(tree): q = [] tree = tree.split(",") root = constructOne(tree[0]); q.append(root); idx = 1; while q: tn = q.pop(0) if not tn: continue if idx == len(tree): break left = constructOne(tree[idx]) tn.left = left q.append(left) idx += 1 if idx == len(tree): break right = constructOne(tree[idx]) idx += 1 tn.right = right q.append(right) return root def printNode(tn, indent): sb = "" for i in range(indent): sb += "\t" sb += str(tn.val) print(sb) def printTree(root, indent): if not root: return printTree(root.right, indent + 1) printNode(root, indent) printTree(root.left, indent + 1) # root = createTree("1, 2, 5, 3, 4, #, 6") root = createTree("4, 3, 5, 2, #, #, 7") i, v = BSTIterator(root), [] while i.hasNext(): v.append(i.next()) for node in v: print(node.val)
apache-2.0
wavelets/silk
django_silky/silk/tests/test_code_gen_curl.py
4
4039
""" Test curl command generation by executing the generated command against a HTTP server that echos various components in the request. """ import json import unittest import subprocess from silk.tests.util import PORT, construct_echo_process # noinspection PyUnresolvedReferences from silk.code_generation.curl import curl_cmd class TestCodeGenerationCurl(unittest.TestCase): httpd_process = construct_echo_process() methods = ['GET', 'POST', 'HEAD', 'PUT', 'PATCH', 'OPTIONS', 'DELETE', 'TRACE', 'CONNECT'] @classmethod def setUpClass(cls): cls.httpd_process.start() @classmethod def tearDownClass(cls): cls.httpd_process.terminate() def _execute(self, path, method, query_params=None, body=None, content_type=None): cmd = curl_cmd('127.0.0.1:%d' % PORT + path, method=method, query_params=query_params, body=body, content_type=content_type) print(cmd) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) stdout, _ = p.communicate() if hasattr(stdout, 'decode'): #py3 raw_response = stdout.decode("utf-8") else: #py2 raw_response = stdout return json.loads(raw_response) def test_query_params(self): def _execute_test(method): response = self._execute('/', method, {'blah': 5}) self.assertDictContainsSubset({ 'path': '/', 'query_params': { 'blah': ['5'] } }, response) map(_execute_test, self.methods) def test_methods(self): def _test_method(method): response = self._execute('/', method) self.assertDictContainsSubset({'path': '/'}, response) map(_test_method, self.methods) def test_json_body(self): def _test_json_body(method): body = { 'x': 5, 'fdf': 10 } response = self._execute('/', method, body=body, content_type='application/json') self.assertDictContainsSubset({ 'path': '/', 'body': json.dumps(body) }, response) map(_test_json_body, self.methods) def test_raw_json_body(self): def _test_json_body(method): body = { 'x': 5, 'fdf': 10 } response = self._execute('/', method, body=json.dumps(body), content_type='application/json') self.assertDictContainsSubset({ 'path': '/', 'body': json.dumps(body) }, response) map(_test_json_body, self.methods) def test_body_no_content_type(self): """should reduce anything with no content_type down to a string""" def _test(method): body = {"random": "body"} response = self._execute('/', method, body=json.dumps(body), content_type='application/json') self.assertDictContainsSubset({ 'path': '/' }, response) response_body = response.get('body') self.assertDictEqual(body, eval(response_body)) print(body) map(_test, self.methods) def test_multipart(self): body = {"multi": "part"} response = self._execute('/', 'POST', body=body, content_type='multipart/form-data') try: body = response['body'] except KeyError: self.fail('Response isnt dict') self.assertIn('form-data', body) self.assertIn('------', body) self.assertIn('part', body) self.assertIn('multi', body) print(response) def test_form_urlencoded(self): body = {"multi": "part"} response = self._execute('/', 'POST', body=body, content_type='application/x-www-form-urlencoded') try: body = response['body'] except KeyError: self.fail('Response isnt dict') print(response)
mit
ClearCorp/odoo-clearcorp
report_xls_template_example/__openerp__.py
3
1648
# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution # Addons modules by CLEARCORP S.A. # Copyright (C) 2009-TODAY CLEARCORP S.A. (<http://clearcorp.co.cr>). # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # ############################################################################## { 'name': 'Report XLS Example', 'version': '1.0', 'category': 'Accounting & Finance', 'sequence': 16, 'summary': 'XLS Invoice Report', 'description': """Example Report Module""", 'author': 'ClearCorp', 'website': 'http://clearcorp.co.cr', 'complexity': 'easy', 'images': [], 'depends': ['report_xls_template'], 'data': [ 'views/account_invoice_report.xml', 'report_xls_example_report.xml', ], 'test': [], 'demo': [], 'installable': True, 'auto_install': False, 'application': False, 'license': 'AGPL-3', }
agpl-3.0
spl0k/supysonic
tests/base/test_cache.py
1
8007
# This file is part of Supysonic. # Supysonic is a Python implementation of the Subsonic server API. # # Copyright (C) 2018 Alban 'spl0k' Féron # 2018-2019 Carey 'pR0Ps' Metcalfe # # Distributed under terms of the GNU AGPLv3 license. import os import unittest import shutil import time import tempfile from supysonic.cache import Cache, CacheMiss, ProtectedError class CacheTestCase(unittest.TestCase): def setUp(self): self.__dir = tempfile.mkdtemp() def tearDown(self): shutil.rmtree(self.__dir) def test_existing_files_order(self): cache = Cache(self.__dir, 30) val = b"0123456789" cache.set("key1", val) cache.set("key2", val) cache.set("key3", val) self.assertEqual(cache.size, 30) # file mtime is accurate to the second time.sleep(1) cache.get_value("key1") cache = Cache(self.__dir, 30, min_time=0) self.assertEqual(cache.size, 30) self.assertTrue(cache.has("key1")) self.assertTrue(cache.has("key2")) self.assertTrue(cache.has("key3")) cache.set("key4", val) self.assertEqual(cache.size, 30) self.assertTrue(cache.has("key1")) self.assertFalse(cache.has("key2")) self.assertTrue(cache.has("key3")) self.assertTrue(cache.has("key4")) def test_missing(self): cache = Cache(self.__dir, 10) self.assertFalse(cache.has("missing")) with self.assertRaises(CacheMiss): cache.get_value("missing") def test_delete_missing(self): cache = Cache(self.__dir, 0, min_time=0) cache.delete("missing1") cache.delete("missing2") def test_store_literal(self): cache = Cache(self.__dir, 10) val = b"0123456789" cache.set("key", val) self.assertEqual(cache.size, 10) self.assertTrue(cache.has("key")) self.assertEqual(cache.get_value("key"), val) def test_store_generated(self): cache = Cache(self.__dir, 10) val = [b"0", b"12", b"345", b"6789"] def gen(): yield from val t = [] for x in cache.set_generated("key", gen): t.append(x) self.assertEqual(cache.size, 0) self.assertFalse(cache.has("key")) self.assertEqual(t, val) self.assertEqual(cache.size, 10) self.assertEqual(cache.get_value("key"), b"".join(val)) def test_store_to_fp(self): cache = Cache(self.__dir, 10) val = b"0123456789" with cache.set_fileobj("key") as fp: fp.write(val) self.assertEqual(cache.size, 0) self.assertEqual(cache.size, 10) self.assertEqual(cache.get_value("key"), val) def test_access_data(self): cache = Cache(self.__dir, 25, min_time=0) val = b"0123456789" cache.set("key", val) self.assertEqual(cache.get_value("key"), val) with cache.get_fileobj("key") as f: self.assertEqual(f.read(), val) with open(cache.get("key"), "rb") as f: self.assertEqual(f.read(), val) def test_accessing_preserves(self): cache = Cache(self.__dir, 25, min_time=0) val = b"0123456789" cache.set("key1", val) cache.set("key2", val) self.assertEqual(cache.size, 20) cache.get_value("key1") cache.set("key3", val) self.assertEqual(cache.size, 20) self.assertTrue(cache.has("key1")) self.assertFalse(cache.has("key2")) self.assertTrue(cache.has("key3")) def test_automatic_delete_oldest(self): cache = Cache(self.__dir, 25, min_time=0) val = b"0123456789" cache.set("key1", val) self.assertTrue(cache.has("key1")) self.assertEqual(cache.size, 10) cache.set("key2", val) self.assertEqual(cache.size, 20) self.assertTrue(cache.has("key1")) self.assertTrue(cache.has("key2")) cache.set("key3", val) self.assertEqual(cache.size, 20) self.assertFalse(cache.has("key1")) self.assertTrue(cache.has("key2")) self.assertTrue(cache.has("key3")) def test_delete(self): cache = Cache(self.__dir, 25, min_time=0) val = b"0123456789" cache.set("key1", val) self.assertTrue(cache.has("key1")) self.assertEqual(cache.size, 10) cache.delete("key1") self.assertFalse(cache.has("key1")) self.assertEqual(cache.size, 0) def test_cleanup_on_error(self): cache = Cache(self.__dir, 10) def gen(): # Cause a TypeError halfway through yield from [b"0", b"12", object(), b"345", b"6789"] with self.assertRaises(TypeError): for x in cache.set_generated("key", gen): pass # Make sure no partial files are left after the error self.assertEqual(list(os.listdir(self.__dir)), list()) def test_parallel_generation(self): cache = Cache(self.__dir, 20) def gen(): yield from [b"0", b"12", b"345", b"6789"] g1 = cache.set_generated("key", gen) g2 = cache.set_generated("key", gen) next(g1) files = os.listdir(self.__dir) self.assertEqual(len(files), 1) for x in files: self.assertTrue(x.endswith(".part")) next(g2) files = os.listdir(self.__dir) self.assertEqual(len(files), 2) for x in files: self.assertTrue(x.endswith(".part")) self.assertEqual(cache.size, 0) for x in g1: pass self.assertEqual(cache.size, 10) self.assertTrue(cache.has("key")) # Replace the file - size should stay the same for x in g2: pass self.assertEqual(cache.size, 10) self.assertTrue(cache.has("key")) # Only a single file self.assertEqual(len(os.listdir(self.__dir)), 1) def test_replace(self): cache = Cache(self.__dir, 20) val_small = b"0" val_big = b"0123456789" cache.set("key", val_small) self.assertEqual(cache.size, 1) cache.set("key", val_big) self.assertEqual(cache.size, 10) cache.set("key", val_small) self.assertEqual(cache.size, 1) def test_no_auto_prune(self): cache = Cache(self.__dir, 10, min_time=0, auto_prune=False) val = b"0123456789" cache.set("key1", val) cache.set("key2", val) cache.set("key3", val) cache.set("key4", val) self.assertEqual(cache.size, 40) cache.prune() self.assertEqual(cache.size, 10) def test_min_time_clear(self): cache = Cache(self.__dir, 40, min_time=1) val = b"0123456789" cache.set("key1", val) cache.set("key2", val) time.sleep(1) cache.set("key3", val) cache.set("key4", val) self.assertEqual(cache.size, 40) cache.clear() self.assertEqual(cache.size, 20) time.sleep(1) cache.clear() self.assertEqual(cache.size, 0) def test_not_expired(self): cache = Cache(self.__dir, 40, min_time=1) val = b"0123456789" cache.set("key1", val) with self.assertRaises(ProtectedError): cache.delete("key1") time.sleep(1) cache.delete("key1") self.assertEqual(cache.size, 0) def test_missing_cache_file(self): cache = Cache(self.__dir, 10, min_time=0) val = b"0123456789" os.remove(cache.set("key", val)) self.assertEqual(cache.size, 10) self.assertFalse(cache.has("key")) self.assertEqual(cache.size, 0) os.remove(cache.set("key", val)) self.assertEqual(cache.size, 10) with self.assertRaises(CacheMiss): cache.get("key") self.assertEqual(cache.size, 0) if __name__ == "__main__": unittest.main()
agpl-3.0
lxml/lxml
src/lxml/tests/test_xslt.py
1
69992
# -*- coding: utf-8 -*- """ Test cases related to XSLT processing """ from __future__ import absolute_import import io import sys import copy import gzip import os.path import unittest import contextlib from textwrap import dedent from tempfile import NamedTemporaryFile, mkdtemp is_python3 = sys.version_info[0] >= 3 try: unicode except NameError: # Python 3 unicode = str try: basestring except NameError: # Python 3 basestring = str from .common_imports import ( etree, BytesIO, HelperTestCase, fileInTestDir, _bytes, make_doctest, skipif ) class ETreeXSLTTestCase(HelperTestCase): """XSLT tests etree""" def test_xslt(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="/a/b/text()" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree) self.assertEqual('''\ <?xml version="1.0"?> <foo>B</foo> ''', str(res)) def test_xslt_elementtree_error(self): self.assertRaises(ValueError, etree.XSLT, etree.ElementTree()) def test_xslt_input_none(self): self.assertRaises(TypeError, etree.XSLT, None) def test_xslt_invalid_stylesheet(self): style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:stylesheet /> </xsl:stylesheet>''') self.assertRaises( etree.XSLTParseError, etree.XSLT, style) def test_xslt_copy(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="/a/b/text()" /></foo> </xsl:template> </xsl:stylesheet>''') transform = etree.XSLT(style) res = transform(tree) self.assertEqual('''\ <?xml version="1.0"?> <foo>B</foo> ''', str(res)) transform_copy = copy.deepcopy(transform) res = transform_copy(tree) self.assertEqual('''\ <?xml version="1.0"?> <foo>B</foo> ''', str(res)) transform = etree.XSLT(style) res = transform(tree) self.assertEqual('''\ <?xml version="1.0"?> <foo>B</foo> ''', str(res)) @contextlib.contextmanager def _xslt_setup( self, encoding='UTF-16', expected_encoding=None, expected='<?xml version="1.0" encoding="%(ENCODING)s"?><foo>\\uF8D2</foo>'): tree = self.parse(_bytes('<a><b>\\uF8D2</b><c>\\uF8D2</c></a>' ).decode("unicode_escape")) style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output encoding="%(ENCODING)s"/> <xsl:template match="/"> <foo><xsl:value-of select="/a/b/text()" /></foo> </xsl:template> </xsl:stylesheet>''' % {'ENCODING': encoding}) st = etree.XSLT(style) res = st(tree) expected = _bytes(dedent(expected).strip()).decode("unicode_escape").replace('\n', '') % { 'ENCODING': expected_encoding or encoding, } data = [res] yield data self.assertEqual(expected, data[0].replace('\n', '')) def test_xslt_utf8(self): with self._xslt_setup(encoding='UTF-8') as res: res[0] = unicode(bytes(res[0]), 'UTF-8') assert 'UTF-8' in res[0] def test_xslt_encoding(self): with self._xslt_setup() as res: res[0] = unicode(bytes(res[0]), 'UTF-16') assert 'UTF-16' in res[0] def test_xslt_encoding_override(self): with self._xslt_setup(encoding='UTF-8', expected_encoding='UTF-16') as res: f = BytesIO() res[0].write(f, encoding='UTF-16') if is_python3: output = str(f.getvalue(), 'UTF-16') else: output = unicode(str(f.getvalue()), 'UTF-16') res[0] = output.replace("'", '"') def test_xslt_write_output_bytesio(self): with self._xslt_setup() as res: f = BytesIO() res[0].write_output(f) res[0] = f.getvalue().decode('UTF-16') def test_xslt_write_output_failure(self): class Writer(object): def write(self, data): raise ValueError("FAILED!") try: with self._xslt_setup() as res: res[0].write_output(Writer()) except ValueError as exc: self.assertTrue("FAILED!" in str(exc), exc) else: self.assertTrue(False, "exception not raised") def test_xslt_write_output_file(self): with self._xslt_setup() as res: f = NamedTemporaryFile(delete=False) try: try: res[0].write_output(f) finally: f.close() with io.open(f.name, encoding='UTF-16') as f: res[0] = f.read() finally: os.unlink(f.name) def test_xslt_write_output_file_path(self): with self._xslt_setup() as res: f = NamedTemporaryFile(delete=False) try: try: res[0].write_output(f.name, compression=9) finally: f.close() with gzip.GzipFile(f.name) as f: res[0] = f.read().decode("UTF-16") finally: os.unlink(f.name) def test_xslt_write_output_file_path_urlescaped(self): # libxml2 should not unescape file paths. with self._xslt_setup() as res: f = NamedTemporaryFile(prefix='tmp%2e', suffix='.xml.gz', delete=False) try: try: res[0].write_output(f.name, compression=3) finally: f.close() with gzip.GzipFile(f.name) as f: res[0] = f.read().decode("UTF-16") finally: os.unlink(f.name) def test_xslt_write_output_file_path_urlescaped_plus(self): with self._xslt_setup() as res: f = NamedTemporaryFile(prefix='p+%2e', suffix='.xml.gz', delete=False) try: try: res[0].write_output(f.name, compression=1) finally: f.close() with gzip.GzipFile(f.name) as f: res[0] = f.read().decode("UTF-16") finally: os.unlink(f.name) def test_xslt_write_output_file_oserror(self): with self._xslt_setup(expected='') as res: tempdir = mkdtemp() try: res[0].write_output(os.path.join(tempdir, 'missing_subdir', 'out.xml')) except IOError: res[0] = '' else: self.fail("IOError not raised") finally: os.rmdir(tempdir) def test_xslt_unicode(self): expected = ''' <?xml version="1.0"?> <foo>\\uF8D2</foo> ''' with self._xslt_setup(expected=expected) as res: res[0] = unicode(res[0]) def test_xslt_unicode_standalone(self): tree = self.parse(_bytes('<a><b>\\uF8D2</b><c>\\uF8D2</c></a>' ).decode("unicode_escape")) style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output encoding="UTF-16" standalone="no"/> <xsl:template match="/"> <foo><xsl:value-of select="/a/b/text()" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree) expected = _bytes('''\ <?xml version="1.0" standalone="no"?> <foo>\\uF8D2</foo> ''').decode("unicode_escape") self.assertEqual(expected, unicode(res)) def test_xslt_input(self): style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="/a/b/text()" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) st = etree.XSLT(style.getroot()) def test_xslt_input_partial_doc(self): style = self.parse('''\ <otherroot> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="/a/b/text()" /></foo> </xsl:template> </xsl:stylesheet> </otherroot>''') self.assertRaises(etree.XSLTParseError, etree.XSLT, style) root_node = style.getroot() self.assertRaises(etree.XSLTParseError, etree.XSLT, root_node) st = etree.XSLT(root_node[0]) def test_xslt_broken(self): style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:foo /> </xsl:stylesheet>''') self.assertRaises(etree.XSLTParseError, etree.XSLT, style) def test_xslt_parsing_error_log(self): tree = self.parse('<a/>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:foo /> </xsl:stylesheet>''') self.assertRaises(etree.XSLTParseError, etree.XSLT, style) exc = None try: etree.XSLT(style) except etree.XSLTParseError as e: exc = e else: self.assertFalse(True, "XSLT processing should have failed but didn't") self.assertTrue(exc is not None) self.assertTrue(len(exc.error_log)) for error in exc.error_log: self.assertTrue(':ERROR:XSLT:' in str(error)) def test_xslt_apply_error_log(self): tree = self.parse('<a/>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="a"> <xsl:copy> <xsl:message terminate="yes">FAIL</xsl:message> </xsl:copy> </xsl:template> </xsl:stylesheet>''') self.assertRaises(etree.XSLTApplyError, etree.XSLT(style), tree) transform = etree.XSLT(style) exc = None try: transform(tree) except etree.XSLTApplyError as e: exc = e else: self.assertFalse(True, "XSLT processing should have failed but didn't") self.assertTrue(exc is not None) self.assertTrue(len(exc.error_log)) self.assertEqual(len(transform.error_log), len(exc.error_log)) for error in exc.error_log: self.assertTrue(':ERROR:XSLT:' in str(error)) for error in transform.error_log: self.assertTrue(':ERROR:XSLT:' in str(error)) def test_xslt_parameters(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <foo><xsl:value-of select="$bar" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree, bar="'Bar'") self.assertEqual('''\ <?xml version="1.0"?> <foo>Bar</foo> ''', str(res)) def test_xslt_string_parameters(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <foo><xsl:value-of select="$bar" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree, bar=etree.XSLT.strparam('''it's me, "Bar"''')) self.assertEqual('''\ <?xml version="1.0"?> <foo>it's me, "Bar"</foo> ''', str(res)) def test_xslt_parameter_invalid(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="bar"/> <xsl:template match="/"> <foo><xsl:value-of select="$bar" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = self.assertRaises(etree.XSLTApplyError, st, tree, bar="<test/>") res = self.assertRaises(etree.XSLTApplyError, st, tree, bar="....") def test_xslt_parameter_missing(self): # apply() without needed parameter will lead to XSLTApplyError tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <foo><xsl:value-of select="$bar" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) # at least libxslt 1.1.28 produces this error, earlier ones (e.g. 1.1.18) might not ... self.assertRaises(etree.XSLTApplyError, st.apply, tree) def test_xslt_multiple_parameters(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="$bar" /></foo> <foo><xsl:value-of select="$baz" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree, bar="'Bar'", baz="'Baz'") self.assertEqual('''\ <?xml version="1.0"?> <foo>Bar</foo><foo>Baz</foo> ''', str(res)) def test_xslt_parameter_xpath(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="$bar" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree, bar="/a/b/text()") self.assertEqual('''\ <?xml version="1.0"?> <foo>B</foo> ''', str(res)) def test_xslt_parameter_xpath_object(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="$bar" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree, bar=etree.XPath("/a/b/text()")) self.assertEqual('''\ <?xml version="1.0"?> <foo>B</foo> ''', str(res)) def test_xslt_default_parameters(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="bar" select="'Default'" /> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="$bar" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree, bar="'Bar'") self.assertEqual('''\ <?xml version="1.0"?> <foo>Bar</foo> ''', str(res)) res = st(tree) self.assertEqual('''\ <?xml version="1.0"?> <foo>Default</foo> ''', str(res)) def test_xslt_html_output(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:strip-space elements="*"/> <xsl:template match="/"> <html><body><xsl:value-of select="/a/b/text()" /></body></html> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree) self.assertEqual('<html><body>B</body></html>', str(res).strip()) def test_xslt_include(self): tree = etree.parse(fileInTestDir('test1.xslt')) st = etree.XSLT(tree) def test_xslt_include_from_filelike(self): f = open(fileInTestDir('test1.xslt'), 'rb') tree = etree.parse(f) f.close() st = etree.XSLT(tree) def test_xslt_multiple_transforms(self): xml = '<a/>' xslt = '''\ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <response>Some text</response> </xsl:template> </xsl:stylesheet> ''' source = self.parse(xml) styledoc = self.parse(xslt) style = etree.XSLT(styledoc) result = style(source) etree.tostring(result.getroot()) source = self.parse(xml) styledoc = self.parse(xslt) style = etree.XSLT(styledoc) result = style(source) etree.tostring(result.getroot()) def test_xslt_repeat_transform(self): xml = '<a/>' xslt = '''\ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <response>Some text</response> </xsl:template> </xsl:stylesheet> ''' source = self.parse(xml) styledoc = self.parse(xslt) transform = etree.XSLT(styledoc) result = transform(source) result = transform(source) etree.tostring(result.getroot()) result = transform(source) etree.tostring(result.getroot()) str(result) result1 = transform(source) result2 = transform(source) self.assertEqual(str(result1), str(result2)) result = transform(source) str(result) def test_xslt_empty(self): # could segfault if result contains "empty document" xml = '<blah/>' xslt = ''' <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/" /> </xsl:stylesheet> ''' source = self.parse(xml) styledoc = self.parse(xslt) style = etree.XSLT(styledoc) result = style(source) self.assertEqual('', str(result)) def test_xslt_message(self): xml = '<blah/>' xslt = ''' <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <xsl:message>TEST TEST TEST</xsl:message> </xsl:template> </xsl:stylesheet> ''' source = self.parse(xml) styledoc = self.parse(xslt) style = etree.XSLT(styledoc) result = style(source) self.assertEqual('', str(result)) self.assertTrue("TEST TEST TEST" in [entry.message for entry in style.error_log]) def test_xslt_message_terminate(self): xml = '<blah/>' xslt = ''' <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <xsl:message terminate="yes">TEST TEST TEST</xsl:message> </xsl:template> </xsl:stylesheet> ''' source = self.parse(xml) styledoc = self.parse(xslt) style = etree.XSLT(styledoc) self.assertRaises(etree.XSLTApplyError, style, source) self.assertTrue("TEST TEST TEST" in [entry.message for entry in style.error_log]) def test_xslt_shortcut(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*" /> <xsl:template match="/"> <doc> <foo><xsl:value-of select="$bar" /></foo> <foo><xsl:value-of select="$baz" /></foo> </doc> </xsl:template> </xsl:stylesheet>''') result = tree.xslt(style, bar="'Bar'", baz="'Baz'") self.assertEqual( _bytes('<doc><foo>Bar</foo><foo>Baz</foo></doc>'), etree.tostring(result.getroot())) def test_multiple_elementrees(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="a"><A><xsl:apply-templates/></A></xsl:template> <xsl:template match="b"><B><xsl:apply-templates/></B></xsl:template> <xsl:template match="c"><C><xsl:apply-templates/></C></xsl:template> </xsl:stylesheet>''') self.assertEqual(self._rootstring(tree), _bytes('<a><b>B</b><c>C</c></a>')) result = tree.xslt(style) self.assertEqual(self._rootstring(tree), _bytes('<a><b>B</b><c>C</c></a>')) self.assertEqual(self._rootstring(result), _bytes('<A><B>B</B><C>C</C></A>')) b_tree = etree.ElementTree(tree.getroot()[0]) self.assertEqual(self._rootstring(b_tree), _bytes('<b>B</b>')) result = b_tree.xslt(style) self.assertEqual(self._rootstring(tree), _bytes('<a><b>B</b><c>C</c></a>')) self.assertEqual(self._rootstring(result), _bytes('<B>B</B>')) c_tree = etree.ElementTree(tree.getroot()[1]) self.assertEqual(self._rootstring(c_tree), _bytes('<c>C</c>')) result = c_tree.xslt(style) self.assertEqual(self._rootstring(tree), _bytes('<a><b>B</b><c>C</c></a>')) self.assertEqual(self._rootstring(result), _bytes('<C>C</C>')) def test_xslt_document_XML(self): # make sure document('') works from parsed strings xslt = etree.XSLT(etree.XML("""\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <test>TEXT<xsl:copy-of select="document('')//test"/></test> </xsl:template> </xsl:stylesheet> """)) result = xslt(etree.XML('<a/>')) root = result.getroot() self.assertEqual(root.tag, 'test') self.assertEqual(root[0].tag, 'test') self.assertEqual(root[0].text, 'TEXT') self.assertEqual(root[0][0].tag, '{http://www.w3.org/1999/XSL/Transform}copy-of') def test_xslt_document_parse(self): # make sure document('') works from loaded files xslt = etree.XSLT(etree.parse(fileInTestDir("test-document.xslt"))) result = xslt(etree.XML('<a/>')) root = result.getroot() self.assertEqual(root.tag, 'test') self.assertEqual(root[0].tag, '{http://www.w3.org/1999/XSL/Transform}stylesheet') def test_xslt_document_elementtree(self): # make sure document('') works from loaded files xslt = etree.XSLT(etree.ElementTree(file=fileInTestDir("test-document.xslt"))) result = xslt(etree.XML('<a/>')) root = result.getroot() self.assertEqual(root.tag, 'test') self.assertEqual(root[0].tag, '{http://www.w3.org/1999/XSL/Transform}stylesheet') def test_xslt_document_error(self): xslt = etree.XSLT(etree.XML("""\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <test>TEXT<xsl:copy-of select="document('uri:__junkfood__is__evil__')//test"/></test> </xsl:template> </xsl:stylesheet> """)) errors = None try: xslt(etree.XML('<a/>')) except etree.XSLTApplyError as exc: errors = exc.error_log else: self.assertFalse(True, "XSLT processing should have failed but didn't") self.assertTrue(len(errors)) for error in errors: if ':ERROR:XSLT:' in str(error): break else: self.assertFalse(True, 'No XSLT errors found in error log:\n%s' % errors) def test_xslt_document_XML_resolver(self): # make sure document('') works when custom resolvers are in use assertEqual = self.assertEqual called = {'count' : 0} class TestResolver(etree.Resolver): def resolve(self, url, id, context): assertEqual(url, 'file://ANYTHING') called['count'] += 1 return self.resolve_string('<CALLED/>', context) parser = etree.XMLParser() parser.resolvers.add(TestResolver()) xslt = etree.XSLT(etree.XML(_bytes("""\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:l="local"> <xsl:template match="/"> <test> <xsl:for-each select="document('')//l:data/l:entry"> <xsl:copy-of select="document('file://ANYTHING')"/> <xsl:copy> <xsl:attribute name="value"> <xsl:value-of select="."/> </xsl:attribute> </xsl:copy> </xsl:for-each> </test> </xsl:template> <l:data> <l:entry>A</l:entry> <l:entry>B</l:entry> </l:data> </xsl:stylesheet> """), parser)) self.assertEqual(called['count'], 0) result = xslt(etree.XML('<a/>')) self.assertEqual(called['count'], 1) root = result.getroot() self.assertEqual(root.tag, 'test') self.assertEqual(len(root), 4) self.assertEqual(root[0].tag, 'CALLED') self.assertEqual(root[1].tag, '{local}entry') self.assertEqual(root[1].text, None) self.assertEqual(root[1].get("value"), 'A') self.assertEqual(root[2].tag, 'CALLED') self.assertEqual(root[3].tag, '{local}entry') self.assertEqual(root[3].text, None) self.assertEqual(root[3].get("value"), 'B') def test_xslt_resolver_url_building(self): assertEqual = self.assertEqual called = {'count' : 0} expected_url = None class TestResolver(etree.Resolver): def resolve(self, url, id, context): assertEqual(url, expected_url) called['count'] += 1 return self.resolve_string('<CALLED/>', context) stylesheet_xml = _bytes("""\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:l="local"> <xsl:template match="/"> <xsl:copy-of select="document('test.xml')"/> </xsl:template> </xsl:stylesheet> """) parser = etree.XMLParser() parser.resolvers.add(TestResolver()) # test without base_url => relative path only expected_url = 'test.xml' xslt = etree.XSLT(etree.XML(stylesheet_xml, parser)) self.assertEqual(called['count'], 0) result = xslt(etree.XML('<a/>')) self.assertEqual(called['count'], 1) # now the same thing with a stylesheet base URL on the filesystem called['count'] = 0 expected_url = 'MY/BASE/test.xml' # seems to be the same on Windows xslt = etree.XSLT(etree.XML( stylesheet_xml, parser, base_url=os.path.join('MY', 'BASE', 'FILE'))) self.assertEqual(called['count'], 0) result = xslt(etree.XML('<a/>')) self.assertEqual(called['count'], 1) # now the same thing with a stylesheet base URL called['count'] = 0 expected_url = 'http://server.com/BASE/DIR/test.xml' xslt = etree.XSLT(etree.XML( stylesheet_xml, parser, base_url='http://server.com/BASE/DIR/FILE')) self.assertEqual(called['count'], 0) result = xslt(etree.XML('<a/>')) self.assertEqual(called['count'], 1) # now the same thing with a stylesheet base file:// URL called['count'] = 0 expected_url = 'file://BASE/DIR/test.xml' xslt = etree.XSLT(etree.XML( stylesheet_xml, parser, base_url='file://BASE/DIR/FILE')) self.assertEqual(called['count'], 0) result = xslt(etree.XML('<a/>')) self.assertEqual(called['count'], 1) def test_xslt_document_parse_allow(self): access_control = etree.XSLTAccessControl(read_file=True) xslt = etree.XSLT(etree.parse(fileInTestDir("test-document.xslt")), access_control=access_control) result = xslt(etree.XML('<a/>')) root = result.getroot() self.assertEqual(root.tag, 'test') self.assertEqual(root[0].tag, '{http://www.w3.org/1999/XSL/Transform}stylesheet') def test_xslt_document_parse_deny(self): access_control = etree.XSLTAccessControl(read_file=False) xslt = etree.XSLT(etree.parse(fileInTestDir("test-document.xslt")), access_control=access_control) self.assertRaises(etree.XSLTApplyError, xslt, etree.XML('<a/>')) def test_xslt_document_parse_deny_all(self): access_control = etree.XSLTAccessControl.DENY_ALL xslt = etree.XSLT(etree.parse(fileInTestDir("test-document.xslt")), access_control=access_control) self.assertRaises(etree.XSLTApplyError, xslt, etree.XML('<a/>')) def test_xslt_access_control_repr(self): access_control = etree.XSLTAccessControl.DENY_ALL self.assertTrue(repr(access_control).startswith(type(access_control).__name__)) self.assertEqual(repr(access_control), repr(access_control)) self.assertNotEqual(repr(etree.XSLTAccessControl.DENY_ALL), repr(etree.XSLTAccessControl.DENY_WRITE)) self.assertNotEqual(repr(etree.XSLTAccessControl.DENY_ALL), repr(etree.XSLTAccessControl())) def test_xslt_move_result(self): root = etree.XML(_bytes('''\ <transform> <widget displayType="fieldset"/> </transform>''')) xslt = etree.XSLT(etree.XML(_bytes('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="no"/> <xsl:template match="/"> <html> <xsl:apply-templates/> </html> </xsl:template> <xsl:template match="widget"> <xsl:element name="{@displayType}"/> </xsl:template> </xsl:stylesheet>'''))) result = xslt(root[0]) root[:] = result.getroot()[:] del root # segfaulted before def test_xslt_pi(self): tree = self.parse('''\ <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="%s"?> <a> <b>B</b> <c>C</c> </a>''' % fileInTestDir("test1.xslt")) style_root = tree.getroot().getprevious().parseXSL().getroot() self.assertEqual("{http://www.w3.org/1999/XSL/Transform}stylesheet", style_root.tag) def test_xslt_pi_embedded_xmlid(self): # test xml:id dictionary lookup mechanism tree = self.parse('''\ <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="#style"?> <a> <b>B</b> <c>C</c> <xsl:stylesheet version="1.0" xml:id="style" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="/a/b/text()" /></foo> </xsl:template> </xsl:stylesheet> </a>''') style_root = tree.getroot().getprevious().parseXSL().getroot() self.assertEqual("{http://www.w3.org/1999/XSL/Transform}stylesheet", style_root.tag) st = etree.XSLT(style_root) res = st(tree) self.assertEqual('''\ <?xml version="1.0"?> <foo>B</foo> ''', str(res)) def test_xslt_pi_embedded_id(self): # test XPath lookup mechanism tree = self.parse('''\ <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="#style"?> <a> <b>B</b> <c>C</c> </a>''') style = self.parse('''\ <xsl:stylesheet version="1.0" xml:id="style" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="/a/b/text()" /></foo> </xsl:template> </xsl:stylesheet> ''') tree.getroot().append(style.getroot()) style_root = tree.getroot().getprevious().parseXSL().getroot() self.assertEqual("{http://www.w3.org/1999/XSL/Transform}stylesheet", style_root.tag) st = etree.XSLT(style_root) res = st(tree) self.assertEqual('''\ <?xml version="1.0"?> <foo>B</foo> ''', str(res)) def test_xslt_pi_get(self): tree = self.parse('''\ <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="TEST"?> <a> <b>B</b> <c>C</c> </a>''') pi = tree.getroot().getprevious() self.assertEqual("TEST", pi.get("href")) def test_xslt_pi_get_all(self): tree = self.parse('''\ <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="TEST"?> <a> <b>B</b> <c>C</c> </a>''') pi = tree.getroot().getprevious() self.assertEqual("TEST", pi.get("href")) self.assertEqual("text/xsl", pi.get("type")) self.assertEqual(None, pi.get("motz")) def test_xslt_pi_get_all_reversed(self): tree = self.parse('''\ <?xml version="1.0"?> <?xml-stylesheet href="TEST" type="text/xsl"?> <a> <b>B</b> <c>C</c> </a>''') pi = tree.getroot().getprevious() self.assertEqual("TEST", pi.get("href")) self.assertEqual("text/xsl", pi.get("type")) self.assertEqual(None, pi.get("motz")) def test_xslt_pi_get_unknown(self): tree = self.parse('''\ <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="TEST"?> <a> <b>B</b> <c>C</c> </a>''') pi = tree.getroot().getprevious() self.assertEqual(None, pi.get("unknownattribute")) def test_xslt_pi_set_replace(self): tree = self.parse('''\ <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="TEST"?> <a> <b>B</b> <c>C</c> </a>''') pi = tree.getroot().getprevious() self.assertEqual("TEST", pi.get("href")) pi.set("href", "TEST123") self.assertEqual("TEST123", pi.get("href")) def test_xslt_pi_set_new(self): tree = self.parse('''\ <?xml version="1.0"?> <?xml-stylesheet type="text/xsl"?> <a> <b>B</b> <c>C</c> </a>''') pi = tree.getroot().getprevious() self.assertEqual(None, pi.get("href")) pi.set("href", "TEST") self.assertEqual("TEST", pi.get("href")) class ETreeEXSLTTestCase(HelperTestCase): """EXSLT tests""" def test_exslt_str(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:str="http://exslt.org/strings" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="str xsl"> <xsl:template match="text()"> <xsl:value-of select="str:align(string(.), '***', 'center')" /> </xsl:template> <xsl:template match="*"> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree) self.assertEqual('''\ <?xml version="1.0"?> <a><b>*B*</b><c>*C*</c></a> ''', str(res)) def test_exslt_str_attribute_replace(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version = "1.0" xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:str="http://exslt.org/strings" extension-element-prefixes="str"> <xsl:template match="/"> <h1 class="{str:replace('abc', 'b', 'x')}">test</h1> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree) self.assertEqual(str(res), '''\ <?xml version="1.0"?> <h1 class="axc">test</h1> ''') def test_exslt_math(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:math="http://exslt.org/math" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="math xsl"> <xsl:template match="*"> <xsl:copy> <xsl:attribute name="pi"> <xsl:value-of select="math:constant('PI', count(*)+2)"/> </xsl:attribute> <xsl:apply-templates/> </xsl:copy> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree) self.assertEqual('''\ <?xml version="1.0"?> <a pi="3.14"><b pi="3">B</b><c pi="3">C</c></a> ''', str(res)) def test_exslt_regexp_test(self): xslt = etree.XSLT(etree.XML(_bytes("""\ <xsl:stylesheet version="1.0" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*"> <test><xsl:copy-of select="*[regexp:test(string(.), '8.')]"/></test> </xsl:template> </xsl:stylesheet> """))) result = xslt(etree.XML(_bytes('<a><b>123</b><b>098</b><b>987</b></a>'))) root = result.getroot() self.assertEqual(root.tag, 'test') self.assertEqual(len(root), 1) self.assertEqual(root[0].tag, 'b') self.assertEqual(root[0].text, '987') def test_exslt_regexp_replace(self): xslt = etree.XSLT(etree.XML("""\ <xsl:stylesheet version="1.0" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*"> <test> <xsl:copy-of select="regexp:replace(string(.), 'd.', '', 'XX')"/> <xsl:text>-</xsl:text> <xsl:copy-of select="regexp:replace(string(.), 'd.', 'gi', 'XX')"/> </test> </xsl:template> </xsl:stylesheet> """)) result = xslt(etree.XML(_bytes('<a>abdCdEeDed</a>'))) root = result.getroot() self.assertEqual(root.tag, 'test') self.assertEqual(len(root), 0) self.assertEqual(root.text, 'abXXdEeDed-abXXXXeXXd') def test_exslt_regexp_match(self): xslt = etree.XSLT(etree.XML("""\ <xsl:stylesheet version="1.0" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*"> <test> <test1><xsl:copy-of select="regexp:match(string(.), 'd.')"/></test1> <test2><xsl:copy-of select="regexp:match(string(.), 'd.', 'g')"/></test2> <test2i><xsl:copy-of select="regexp:match(string(.), 'd.', 'gi')"/></test2i> </test> </xsl:template> </xsl:stylesheet> """)) result = xslt(etree.XML(_bytes('<a>abdCdEeDed</a>'))) root = result.getroot() self.assertEqual(root.tag, 'test') self.assertEqual(len(root), 3) self.assertEqual(len(root[0]), 1) self.assertEqual(root[0][0].tag, 'match') self.assertEqual(root[0][0].text, 'dC') self.assertEqual(len(root[1]), 2) self.assertEqual(root[1][0].tag, 'match') self.assertEqual(root[1][0].text, 'dC') self.assertEqual(root[1][1].tag, 'match') self.assertEqual(root[1][1].text, 'dE') self.assertEqual(len(root[2]), 3) self.assertEqual(root[2][0].tag, 'match') self.assertEqual(root[2][0].text, 'dC') self.assertEqual(root[2][1].tag, 'match') self.assertEqual(root[2][1].text, 'dE') self.assertEqual(root[2][2].tag, 'match') self.assertEqual(root[2][2].text, 'De') def test_exslt_regexp_match_groups(self): xslt = etree.XSLT(etree.XML(_bytes("""\ <xsl:stylesheet version="1.0" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <test> <xsl:for-each select="regexp:match( '123abc567', '([0-9]+)([a-z]+)([0-9]+)' )"> <test1><xsl:value-of select="."/></test1> </xsl:for-each> </test> </xsl:template> </xsl:stylesheet> """))) result = xslt(etree.XML(_bytes('<a/>'))) root = result.getroot() self.assertEqual(root.tag, 'test') self.assertEqual(len(root), 4) self.assertEqual(root[0].text, "123abc567") self.assertEqual(root[1].text, "123") self.assertEqual(root[2].text, "abc") self.assertEqual(root[3].text, "567") def test_exslt_regexp_match1(self): # taken from http://www.exslt.org/regexp/functions/match/index.html xslt = etree.XSLT(etree.XML(_bytes("""\ <xsl:stylesheet version="1.0" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <test> <xsl:for-each select="regexp:match( 'http://www.bayes.co.uk/xml/index.xml?/xml/utils/rechecker.xml', '(\\w+):\\/\\/([^/:]+)(:\\d*)?([^# ]*)')"> <test1><xsl:value-of select="."/></test1> </xsl:for-each> </test> </xsl:template> </xsl:stylesheet> """))) result = xslt(etree.XML(_bytes('<a/>'))) root = result.getroot() self.assertEqual(root.tag, 'test') self.assertEqual(len(root), 5) self.assertEqual( root[0].text, "http://www.bayes.co.uk/xml/index.xml?/xml/utils/rechecker.xml") self.assertEqual( root[1].text, "http") self.assertEqual( root[2].text, "www.bayes.co.uk") self.assertFalse(root[3].text) self.assertEqual( root[4].text, "/xml/index.xml?/xml/utils/rechecker.xml") def test_exslt_regexp_match2(self): # taken from http://www.exslt.org/regexp/functions/match/index.html xslt = etree.XSLT(self.parse("""\ <xsl:stylesheet version="1.0" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <test> <xsl:for-each select="regexp:match( 'This is a test string', '(\\w+)', 'g')"> <test1><xsl:value-of select="."/></test1> </xsl:for-each> </test> </xsl:template> </xsl:stylesheet> """)) result = xslt(etree.XML(_bytes('<a/>'))) root = result.getroot() self.assertEqual(root.tag, 'test') self.assertEqual(len(root), 5) self.assertEqual(root[0].text, "This") self.assertEqual(root[1].text, "is") self.assertEqual(root[2].text, "a") self.assertEqual(root[3].text, "test") self.assertEqual(root[4].text, "string") def _test_exslt_regexp_match3(self): # taken from http://www.exslt.org/regexp/functions/match/index.html # THIS IS NOT SUPPORTED! xslt = etree.XSLT(etree.XML(_bytes("""\ <xsl:stylesheet version="1.0" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <test> <xsl:for-each select="regexp:match( 'This is a test string', '([a-z])+ ', 'g')"> <test1><xsl:value-of select="."/></test1> </xsl:for-each> </test> </xsl:template> </xsl:stylesheet> """))) result = xslt(etree.XML(_bytes('<a/>'))) root = result.getroot() self.assertEqual(root.tag, 'test') self.assertEqual(len(root), 4) self.assertEqual(root[0].text, "his") self.assertEqual(root[1].text, "is") self.assertEqual(root[2].text, "a") self.assertEqual(root[3].text, "test") def _test_exslt_regexp_match4(self): # taken from http://www.exslt.org/regexp/functions/match/index.html # THIS IS NOT SUPPORTED! xslt = etree.XSLT(etree.XML(_bytes("""\ <xsl:stylesheet version="1.0" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <test> <xsl:for-each select="regexp:match( 'This is a test string', '([a-z])+ ', 'gi')"> <test1><xsl:value-of select="."/></test1> </xsl:for-each> </test> </xsl:template> </xsl:stylesheet> """))) result = xslt(etree.XML(_bytes('<a/>'))) root = result.getroot() self.assertEqual(root.tag, 'test') self.assertEqual(len(root), 4) self.assertEqual(root[0].text, "This") self.assertEqual(root[1].text, "is") self.assertEqual(root[2].text, "a") self.assertEqual(root[3].text, "test") class ETreeXSLTExtFuncTestCase(HelperTestCase): """Tests for XPath extension functions in XSLT.""" def test_extensions1(self): tree = self.parse('<a><b>B</b></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" exclude-result-prefixes="myns"> <xsl:template match="a"><A><xsl:value-of select="myns:mytext(b)"/></A></xsl:template> </xsl:stylesheet>''') def mytext(ctxt, values): return 'X' * len(values) result = tree.xslt(style, {('testns', 'mytext') : mytext}) self.assertEqual(self._rootstring(result), _bytes('<A>X</A>')) def test_extensions2(self): tree = self.parse('<a><b>B</b></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" exclude-result-prefixes="myns"> <xsl:template match="a"><A><xsl:value-of select="myns:mytext(b)"/></A></xsl:template> </xsl:stylesheet>''') def mytext(ctxt, values): return 'X' * len(values) namespace = etree.FunctionNamespace('testns') namespace['mytext'] = mytext result = tree.xslt(style) self.assertEqual(self._rootstring(result), _bytes('<A>X</A>')) def test_variable_result_tree_fragment(self): tree = self.parse('<a><b>B</b><b/></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" exclude-result-prefixes="myns"> <xsl:template match="a"> <xsl:variable name="content"> <xsl:apply-templates/> </xsl:variable> <A><xsl:value-of select="myns:mytext($content)"/></A> </xsl:template> <xsl:template match="b"><xsl:copy>BBB</xsl:copy></xsl:template> </xsl:stylesheet>''') def mytext(ctxt, values): for value in values: self.assertTrue(hasattr(value, 'tag'), "%s is not an Element" % type(value)) self.assertEqual(value.tag, 'b') self.assertEqual(value.text, 'BBB') return 'X'.join([el.tag for el in values]) namespace = etree.FunctionNamespace('testns') namespace['mytext'] = mytext result = tree.xslt(style) self.assertEqual(self._rootstring(result), _bytes('<A>bXb</A>')) def test_xpath_on_context_node(self): tree = self.parse('<a><b>B<c/>C</b><b/></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" exclude-result-prefixes="myns"> <xsl:template match="b"> <A><xsl:value-of select="myns:myext()"/></A> </xsl:template> </xsl:stylesheet>''') def extfunc(ctxt): text_content = ctxt.context_node.xpath('text()') return 'x'.join(text_content) namespace = etree.FunctionNamespace('testns') namespace['myext'] = extfunc result = tree.xslt(style) self.assertEqual(self._rootstring(result), _bytes('<A>BxC</A>')) def test_xpath_on_foreign_context_node(self): # LP ticket 1354652 class Resolver(etree.Resolver): def resolve(self, system_url, public_id, context): assert system_url == 'extdoc.xml' return self.resolve_string(b'<a><b>B<c/>C</b><b/></a>', context) parser = etree.XMLParser() parser.resolvers.add(Resolver()) tree = self.parse(b'<a><b/><b/></a>') transform = etree.XSLT(self.parse(b'''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mypre="testns" exclude-result-prefixes="mypre"> <xsl:template match="b"> <B><xsl:value-of select="mypre:myext()"/></B> </xsl:template> <xsl:template match="a"> <A><xsl:apply-templates select="document('extdoc.xml')//b" /></A> </xsl:template> </xsl:stylesheet>''', parser=parser)) def extfunc(ctxt): text_content = ctxt.context_node.xpath('text()') return 'x'.join(text_content) namespace = etree.FunctionNamespace('testns') namespace['myext'] = extfunc result = transform(tree) self.assertEqual(self._rootstring(result), _bytes('<A><B>BxC</B><B/></A>')) class ETreeXSLTExtElementTestCase(HelperTestCase): """Tests for extension elements in XSLT.""" def test_extension_element(self): tree = self.parse('<a><b>B</b></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" extension-element-prefixes="myns" exclude-result-prefixes="myns"> <xsl:template match="a"> <A><myns:myext>b</myns:myext></A> </xsl:template> </xsl:stylesheet>''') class MyExt(etree.XSLTExtension): def execute(self, context, self_node, input_node, output_parent): child = etree.Element(self_node.text) child.text = 'X' output_parent.append(child) extensions = { ('testns', 'myext') : MyExt() } result = tree.xslt(style, extensions=extensions) self.assertEqual(self._rootstring(result), _bytes('<A><b>X</b></A>')) def test_extension_element_doc_context(self): tree = self.parse('<a><b>B</b></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" extension-element-prefixes="myns" exclude-result-prefixes="myns"> <xsl:template match="/"> <A><myns:myext>b</myns:myext></A> </xsl:template> </xsl:stylesheet>''') tags = [] class MyExt(etree.XSLTExtension): def execute(self, context, self_node, input_node, output_parent): tags.append(input_node.tag) extensions = { ('testns', 'myext') : MyExt() } result = tree.xslt(style, extensions=extensions) self.assertEqual(tags, ['a']) def test_extension_element_comment_pi_context(self): tree = self.parse('<?test toast?><a><!--a comment--><?another pi?></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" extension-element-prefixes="myns" exclude-result-prefixes="myns"> <xsl:template match="/"> <ROOT><xsl:apply-templates /></ROOT> </xsl:template> <xsl:template match="comment()"> <A><myns:myext>b</myns:myext></A> </xsl:template> <xsl:template match="processing-instruction()"> <A><myns:myext>b</myns:myext></A> </xsl:template> </xsl:stylesheet>''') text = [] class MyExt(etree.XSLTExtension): def execute(self, context, self_node, input_node, output_parent): text.append(input_node.text) extensions = { ('testns', 'myext') : MyExt() } result = tree.xslt(style, extensions=extensions) self.assertEqual(text, ['toast', 'a comment', 'pi']) def _test_extension_element_attribute_context(self): # currently not supported tree = self.parse('<a test="A"><b attr="B"/></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" extension-element-prefixes="myns" exclude-result-prefixes="myns"> <xsl:template match="@test"> <A><myns:myext>b</myns:myext></A> </xsl:template> <xsl:template match="@attr"> <A><myns:myext>b</myns:myext></A> </xsl:template> </xsl:stylesheet>''') text = [] class MyExt(etree.XSLTExtension): def execute(self, context, self_node, attr_value, output_parent): text.append(attr_value) extensions = { ('testns', 'myext') : MyExt() } result = tree.xslt(style, extensions=extensions) self.assertEqual(text, ['A', 'B']) def test_extension_element_content(self): tree = self.parse('<a><b>B</b></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" extension-element-prefixes="myns"> <xsl:template match="a"> <A><myns:myext><x>X</x><y>Y</y><z/></myns:myext></A> </xsl:template> </xsl:stylesheet>''') class MyExt(etree.XSLTExtension): def execute(self, context, self_node, input_node, output_parent): output_parent.extend(list(self_node)[1:]) extensions = { ('testns', 'myext') : MyExt() } result = tree.xslt(style, extensions=extensions) self.assertEqual(self._rootstring(result), _bytes('<A><y>Y</y><z/></A>')) def test_extension_element_apply_templates(self): tree = self.parse('<a><b>B</b></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" extension-element-prefixes="myns"> <xsl:template match="a"> <A><myns:myext><x>X</x><y>Y</y><z/></myns:myext></A> </xsl:template> <xsl:template match="x" /> <xsl:template match="z">XYZ</xsl:template> </xsl:stylesheet>''') class MyExt(etree.XSLTExtension): def execute(self, context, self_node, input_node, output_parent): for child in self_node: for result in self.apply_templates(context, child): if isinstance(result, basestring): el = etree.Element("T") el.text = result else: el = result output_parent.append(el) extensions = { ('testns', 'myext') : MyExt() } result = tree.xslt(style, extensions=extensions) self.assertEqual(self._rootstring(result), _bytes('<A><T>Y</T><T>XYZ</T></A>')) def test_extension_element_apply_templates_elements_only(self): tree = self.parse('<a><b>B</b></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" extension-element-prefixes="myns"> <xsl:template match="a"> <A><myns:myext><x>X</x><y>Y</y><z/></myns:myext></A> </xsl:template> <xsl:template match="x"><X/></xsl:template> <xsl:template match="z">XYZ</xsl:template> </xsl:stylesheet>''') class MyExt(etree.XSLTExtension): def execute(self, context, self_node, input_node, output_parent): for child in self_node: for result in self.apply_templates(context, child, elements_only=True): assert not isinstance(result, basestring) output_parent.append(result) extensions = { ('testns', 'myext') : MyExt() } result = tree.xslt(style, extensions=extensions) self.assertEqual(self._rootstring(result), _bytes('<A><X/></A>')) def test_extension_element_apply_templates_remove_blank_text(self): tree = self.parse('<a><b>B</b></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" extension-element-prefixes="myns"> <xsl:template match="a"> <A><myns:myext><x>X</x><y>Y</y><z/></myns:myext></A> </xsl:template> <xsl:template match="x"><X/></xsl:template> <xsl:template match="y"><xsl:text> </xsl:text></xsl:template> <xsl:template match="z">XYZ</xsl:template> </xsl:stylesheet>''') class MyExt(etree.XSLTExtension): def execute(self, context, self_node, input_node, output_parent): for child in self_node: for result in self.apply_templates(context, child, remove_blank_text=True): if isinstance(result, basestring): assert result.strip() el = etree.Element("T") el.text = result else: el = result output_parent.append(el) extensions = { ('testns', 'myext') : MyExt() } result = tree.xslt(style, extensions=extensions) self.assertEqual(self._rootstring(result), _bytes('<A><X/><T>XYZ</T></A>')) def test_extension_element_apply_templates_target_node(self): tree = self.parse('<a><b>B</b></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" extension-element-prefixes="myns"> <xsl:template match="a"> <A><myns:myext><x>X</x><y>Y</y><z/></myns:myext></A> </xsl:template> <xsl:template match="x" /> <xsl:template match="z">XYZ</xsl:template> </xsl:stylesheet>''') class MyExt(etree.XSLTExtension): def execute(self, context, self_node, input_node, output_parent): for child in self_node: self.apply_templates(context, child, output_parent) extensions = { ('testns', 'myext') : MyExt() } result = tree.xslt(style, extensions=extensions) self.assertEqual(self._rootstring(result), _bytes('<A>YXYZ</A>')) def test_extension_element_apply_templates_target_node_doc(self): tree = self.parse('<a><b>B</b></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" extension-element-prefixes="myns"> <xsl:template match="a"> <myns:myext><x>X</x><y>Y</y><z/></myns:myext> </xsl:template> <xsl:template match="x"><xsl:processing-instruction name="test">TEST</xsl:processing-instruction></xsl:template> <xsl:template match="y"><Y>XYZ</Y></xsl:template> <xsl:template match="z"><xsl:comment>TEST</xsl:comment></xsl:template> </xsl:stylesheet>''') class MyExt(etree.XSLTExtension): def execute(self, context, self_node, input_node, output_parent): for child in self_node: self.apply_templates(context, child, output_parent) extensions = { ('testns', 'myext') : MyExt() } result = tree.xslt(style, extensions=extensions) self.assertEqual(etree.tostring(result), _bytes('<?test TEST?><Y>XYZ</Y><!--TEST-->')) def test_extension_element_process_children(self): tree = self.parse('<a><b>E</b></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" extension-element-prefixes="myns"> <xsl:template match="a"> <xsl:variable name="testvar">yo</xsl:variable> <A> <myns:myext> <xsl:attribute name="attr"> <xsl:value-of select="$testvar" /> </xsl:attribute> <B> <xsl:choose> <xsl:when test="1 = 2"><C/></xsl:when> <xsl:otherwise><D><xsl:value-of select="b/text()" /></D></xsl:otherwise> </xsl:choose> </B> </myns:myext> </A> </xsl:template> </xsl:stylesheet>''') class MyExt(etree.XSLTExtension): def execute(self, context, self_node, input_node, output_parent): el = etree.Element('MY') self.process_children(context, el) output_parent.append(el) extensions = { ('testns', 'myext') : MyExt() } result = tree.xslt(style, extensions=extensions) self.assertEqual(self._rootstring(result), _bytes('<A><MYattr="yo"><B><D>E</D></B></MY></A>')) def test_extension_element_process_children_to_append_only(self): tree = self.parse('<a/>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" extension-element-prefixes="myns"> <xsl:template match="a"> <myns:myext> <A/> </myns:myext> </xsl:template> </xsl:stylesheet>''') class MyExt(etree.XSLTExtension): def execute(self, context, self_node, input_node, output_parent): self.process_children(context, output_parent) extensions = { ('testns', 'myext') : MyExt() } result = tree.xslt(style, extensions=extensions) self.assertEqual(self._rootstring(result), _bytes('<A/>')) def test_extension_element_process_children_to_read_only_raise(self): tree = self.parse('<a/>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" extension-element-prefixes="myns"> <xsl:template match="a"> <myns:myext> <A/> </myns:myext> </xsl:template> </xsl:stylesheet>''') class MyExt(etree.XSLTExtension): def execute(self, context, self_node, input_node, output_parent): self.process_children(context, self_node) extensions = { ('testns', 'myext') : MyExt() } self.assertRaises(TypeError, tree.xslt, style, extensions=extensions) def test_extension_element_process_children_with_subextension_element(self): tree = self.parse('<a/>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" extension-element-prefixes="myns"> <xsl:template match="a"> <myns:myext> <A><myns:myext><B/></myns:myext></A> </myns:myext> </xsl:template> </xsl:stylesheet>''') class MyExt(etree.XSLTExtension): callback_call_counter = 0 def execute(self, context, self_node, input_node, output_parent): self.callback_call_counter += 1 el = etree.Element('MY', n=str(self.callback_call_counter)) self.process_children(context, el) output_parent.append(el) extensions = { ('testns', 'myext') : MyExt() } result = tree.xslt(style, extensions=extensions) self.assertEqual(self._rootstring(result), _bytes('<MYn="1"><A><MYn="2"><B/></MY></A></MY>')) def test_extension_element_raise(self): tree = self.parse('<a><b>B</b></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="testns" extension-element-prefixes="myns" exclude-result-prefixes="myns"> <xsl:template match="a"> <A><myns:myext>b</myns:myext></A> </xsl:template> </xsl:stylesheet>''') class MyError(Exception): pass class MyExt(etree.XSLTExtension): def execute(self, context, self_node, input_node, output_parent): raise MyError("expected!") extensions = { ('testns', 'myext') : MyExt() } self.assertRaises(MyError, tree.xslt, style, extensions=extensions) # FIXME: DISABLED - implementation seems to be broken # if someone cares enough about this feature, I take pull requests that fix it. def _test_multiple_extension_elements_with_output_parent(self): tree = self.parse("""\ <text> <par>This is <format>arbitrary</format> text in a paragraph</par> </text>""") style = self.parse("""\ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="my" extension-element-prefixes="my" version="1.0"> <xsl:template match="par"> <my:par><xsl:apply-templates /></my:par> </xsl:template> <xsl:template match="format"> <my:format><xsl:apply-templates /></my:format> </xsl:template> </xsl:stylesheet> """) test = self calls = [] class ExtMyPar(etree.XSLTExtension): def execute(self, context, self_node, input_node, output_parent): calls.append('par') p = etree.Element("p") p.attrib["style"] = "color:red" self.process_children(context, p) output_parent.append(p) class ExtMyFormat(etree.XSLTExtension): def execute(self, context, self_node, input_node, output_parent): calls.append('format') content = self.process_children(context) test.assertEqual(1, len(content)) test.assertEqual('arbitrary', content[0]) test.assertEqual('This is ', output_parent.text) output_parent.text += '*-%s-*' % content[0] extensions = {("my", "par"): ExtMyPar(), ("my", "format"): ExtMyFormat()} transform = etree.XSLT(style, extensions=extensions) result = transform(tree) self.assertEqual(['par', 'format'], calls) self.assertEqual( b'<p style="color:red">This is *-arbitrary-* text in a paragraph</p>\n', etree.tostring(result)) def test_extensions_nsmap(self): tree = self.parse("""\ <root> <inner xmlns:sha256="http://www.w3.org/2001/04/xmlenc#sha256"> <data>test</data> </inner> </root> """) style = self.parse("""\ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="extns" extension-element-prefixes="my" version="1.0"> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="data"> <my:show-nsmap/> </xsl:template> </xsl:stylesheet> """) class MyExt(etree.XSLTExtension): def execute(self, context, self_node, input_node, output_parent): output_parent.text = str(input_node.nsmap) extensions = {('extns', 'show-nsmap'): MyExt()} result = tree.xslt(style, extensions=extensions) self.assertEqual(etree.tostring(result, pretty_print=True), b"""\ <root> <inner xmlns:sha256="http://www.w3.org/2001/04/xmlenc#sha256">{'sha256': 'http://www.w3.org/2001/04/xmlenc#sha256'} </inner> </root> """) class Py3XSLTTestCase(HelperTestCase): """XSLT tests for etree under Python 3""" pytestmark = skipif('sys.version_info < (3,0)') def test_xslt_result_bytes(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="/a/b/text()" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree) self.assertEqual(_bytes('''\ <?xml version="1.0"?> <foo>B</foo> '''), bytes(res)) def test_xslt_result_bytearray(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="/a/b/text()" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree) self.assertEqual(_bytes('''\ <?xml version="1.0"?> <foo>B</foo> '''), bytearray(res)) def test_xslt_result_memoryview(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="/a/b/text()" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree) self.assertEqual(_bytes('''\ <?xml version="1.0"?> <foo>B</foo> '''), bytes(memoryview(res))) def test_suite(): suite = unittest.TestSuite() suite.addTests([unittest.makeSuite(ETreeXSLTTestCase)]) suite.addTests([unittest.makeSuite(ETreeEXSLTTestCase)]) suite.addTests([unittest.makeSuite(ETreeXSLTExtFuncTestCase)]) suite.addTests([unittest.makeSuite(ETreeXSLTExtElementTestCase)]) if is_python3: suite.addTests([unittest.makeSuite(Py3XSLTTestCase)]) suite.addTests( [make_doctest('../../../doc/extensions.txt')]) suite.addTests( [make_doctest('../../../doc/xpathxslt.txt')]) return suite if __name__ == '__main__': print('to test use test.py %s' % __file__)
bsd-3-clause
cpn18/track-chart
desktop/gps_smoothing.py
1
1313
import sys import json import math THRESHOLD = 10 data = [] with open(sys.argv[1], "r") as f: used = count = 0 for line in f: if line[0] == "#": continue items = line.split() if items[1] == "TPV": obj = json.loads(" ".join(items[2:-1])) obj['used'] = used obj['count'] = count elif items[1] == "SKY": obj = json.loads(" ".join(items[2:-1])) used = 0 count = len(obj['satellites']) for i in range(0, count): if obj['satellites'][i]['used']: used += 1 continue else: continue if used >= THRESHOLD and 'lon' in obj and 'lat' in obj: data.append(obj) print("Longitude Latitude dx epx dy epy used count") for i in range(1, len(data)): dx = abs((data[i]['lon'] - data[i-1]['lon']) * 111120 * math.cos(math.radians(data[i]['lat']))) dy = abs((data[i]['lat'] - data[i-1]['lat']) * 111128) # degrees to meters try: if dx > 3*data[i]['epx'] or dy > 3*data[i]['epy']: continue print("%f %f %f %f %f %f %d %d" % (data[i]['lon'], data[i]['lat'], dx, data[i]['epx'], dy, data[i]['epy'], data[i]['used'], data[i]['count'])) except KeyError: pass
gpl-3.0
rjschwei/azure-sdk-for-python
unreleased/azure-mgmt-machinelearning/azure/mgmt/machinelearning/models/__init__.py
5
2737
# coding=utf-8 # -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. # # Code generated by Microsoft (R) AutoRest Code Generator. # Changes may cause incorrect behavior and will be lost if the code is # regenerated. # -------------------------------------------------------------------------- from .resource import Resource from .web_service_keys import WebServiceKeys from .realtime_configuration import RealtimeConfiguration from .diagnostics_configuration import DiagnosticsConfiguration from .storage_account import StorageAccount from .machine_learning_workspace import MachineLearningWorkspace from .commitment_plan import CommitmentPlan from .column_specification import ColumnSpecification from .table_specification import TableSpecification from .service_input_output_specification import ServiceInputOutputSpecification from .example_request import ExampleRequest from .asset_location import AssetLocation from .input_port import InputPort from .output_port import OutputPort from .mode_value_info import ModeValueInfo from .module_asset_parameter import ModuleAssetParameter from .asset_item import AssetItem from .web_service_properties import WebServiceProperties from .web_service import WebService from .graph_node import GraphNode from .graph_edge import GraphEdge from .graph_parameter_link import GraphParameterLink from .graph_parameter import GraphParameter from .graph_package import GraphPackage from .web_service_properties_for_graph import WebServicePropertiesForGraph from .web_service_paged import WebServicePaged from .azure_ml_web_services_management_client_enums import ( ProvisioningState, DiagnosticsLevel, ColumnType, ColumnFormat, AssetType, InputPortType, OutputPortType, ParameterType, ) __all__ = [ 'Resource', 'WebServiceKeys', 'RealtimeConfiguration', 'DiagnosticsConfiguration', 'StorageAccount', 'MachineLearningWorkspace', 'CommitmentPlan', 'ColumnSpecification', 'TableSpecification', 'ServiceInputOutputSpecification', 'ExampleRequest', 'AssetLocation', 'InputPort', 'OutputPort', 'ModeValueInfo', 'ModuleAssetParameter', 'AssetItem', 'WebServiceProperties', 'WebService', 'GraphNode', 'GraphEdge', 'GraphParameterLink', 'GraphParameter', 'GraphPackage', 'WebServicePropertiesForGraph', 'WebServicePaged', 'ProvisioningState', 'DiagnosticsLevel', 'ColumnType', 'ColumnFormat', 'AssetType', 'InputPortType', 'OutputPortType', 'ParameterType', ]
mit
ABaldwinHunter/django-clone-classic
tests/template_tests/test_context.py
128
6990
# -*- coding: utf-8 -*- from django.http import HttpRequest from django.template import ( Context, Engine, RequestContext, Template, Variable, VariableDoesNotExist, ) from django.template.context import RenderContext from django.test import RequestFactory, SimpleTestCase class ContextTests(SimpleTestCase): def test_context(self): c = Context({"a": 1, "b": "xyzzy"}) self.assertEqual(c["a"], 1) self.assertEqual(c.push(), {}) c["a"] = 2 self.assertEqual(c["a"], 2) self.assertEqual(c.get("a"), 2) self.assertEqual(c.pop(), {"a": 2}) self.assertEqual(c["a"], 1) self.assertEqual(c.get("foo", 42), 42) def test_push_context_manager(self): c = Context({"a": 1}) with c.push(): c['a'] = 2 self.assertEqual(c['a'], 2) self.assertEqual(c['a'], 1) with c.push(a=3): self.assertEqual(c['a'], 3) self.assertEqual(c['a'], 1) def test_update_context_manager(self): c = Context({"a": 1}) with c.update({}): c['a'] = 2 self.assertEqual(c['a'], 2) self.assertEqual(c['a'], 1) with c.update({'a': 3}): self.assertEqual(c['a'], 3) self.assertEqual(c['a'], 1) def test_push_context_manager_with_context_object(self): c = Context({'a': 1}) with c.push(Context({'a': 3})): self.assertEqual(c['a'], 3) self.assertEqual(c['a'], 1) def test_update_context_manager_with_context_object(self): c = Context({'a': 1}) with c.update(Context({'a': 3})): self.assertEqual(c['a'], 3) self.assertEqual(c['a'], 1) def test_push_proper_layering(self): c = Context({'a': 1}) c.push(Context({'b': 2})) c.push(Context({'c': 3, 'd': {'z': '26'}})) self.assertEqual( c.dicts, [ {'False': False, 'None': None, 'True': True}, {'a': 1}, {'b': 2}, {'c': 3, 'd': {'z': '26'}}, ] ) def test_update_proper_layering(self): c = Context({'a': 1}) c.update(Context({'b': 2})) c.update(Context({'c': 3, 'd': {'z': '26'}})) self.assertEqual( c.dicts, [ {'False': False, 'None': None, 'True': True}, {'a': 1}, {'b': 2}, {'c': 3, 'd': {'z': '26'}}, ] ) def test_setdefault(self): c = Context() x = c.setdefault('x', 42) self.assertEqual(x, 42) self.assertEqual(c['x'], 42) x = c.setdefault('x', 100) self.assertEqual(x, 42) self.assertEqual(c['x'], 42) def test_resolve_on_context_method(self): """ #17778 -- Variable shouldn't resolve RequestContext methods """ empty_context = Context() with self.assertRaises(VariableDoesNotExist): Variable('no_such_variable').resolve(empty_context) with self.assertRaises(VariableDoesNotExist): Variable('new').resolve(empty_context) self.assertEqual( Variable('new').resolve(Context({'new': 'foo'})), 'foo', ) def test_render_context(self): test_context = RenderContext({'fruit': 'papaya'}) # Test that push() limits access to the topmost dict test_context.push() test_context['vegetable'] = 'artichoke' self.assertEqual(list(test_context), ['vegetable']) self.assertNotIn('fruit', test_context) with self.assertRaises(KeyError): test_context['fruit'] self.assertIsNone(test_context.get('fruit')) def test_flatten_context(self): a = Context() a.update({'a': 2}) a.update({'b': 4}) a.update({'c': 8}) self.assertEqual(a.flatten(), { 'False': False, 'None': None, 'True': True, 'a': 2, 'b': 4, 'c': 8 }) def test_flatten_context_with_context(self): """ Context.push() with a Context argument should work. """ a = Context({'a': 2}) a.push(Context({'z': '8'})) self.assertEqual(a.flatten(), { 'False': False, 'None': None, 'True': True, 'a': 2, 'z': '8', }) def test_context_comparable(self): """ #21765 -- equality comparison should work """ test_data = {'x': 'y', 'v': 'z', 'd': {'o': object, 'a': 'b'}} self.assertEqual(Context(test_data), Context(test_data)) a = Context() b = Context() self.assertEqual(a, b) # update only a a.update({'a': 1}) self.assertNotEqual(a, b) # update both to check regression a.update({'c': 3}) b.update({'c': 3}) self.assertNotEqual(a, b) # make contexts equals again b.update({'a': 1}) self.assertEqual(a, b) def test_copy_request_context_twice(self): """ #24273 -- Copy twice shouldn't raise an exception """ RequestContext(HttpRequest()).new().new() class RequestContextTests(SimpleTestCase): def test_include_only(self): """ #15721 -- ``{% include %}`` and ``RequestContext`` should work together. """ engine = Engine(loaders=[ ('django.template.loaders.locmem.Loader', { 'child': '{{ var|default:"none" }}', }), ]) request = RequestFactory().get('/') ctx = RequestContext(request, {'var': 'parent'}) self.assertEqual(engine.from_string('{% include "child" %}').render(ctx), 'parent') self.assertEqual(engine.from_string('{% include "child" only %}').render(ctx), 'none') def test_stack_size(self): """ #7116 -- Optimize RequetsContext construction """ request = RequestFactory().get('/') ctx = RequestContext(request, {}) # The stack should now contain 3 items: # [builtins, supplied context, context processor, empty dict] self.assertEqual(len(ctx.dicts), 4) def test_context_comparable(self): # Create an engine without any context processors. test_data = {'x': 'y', 'v': 'z', 'd': {'o': object, 'a': 'b'}} # test comparing RequestContext to prevent problems if somebody # adds __eq__ in the future request = RequestFactory().get('/') self.assertEqual( RequestContext(request, dict_=test_data), RequestContext(request, dict_=test_data), ) def test_modify_context_and_render(self): template = Template('{{ foo }}') request = RequestFactory().get('/') context = RequestContext(request, {}) context['foo'] = 'foo' self.assertEqual(template.render(context), 'foo')
bsd-3-clause
nopjmp/SickRage
lib/rebulk/processors.py
20
3372
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Processor functions """ from logging import getLogger from .utils import IdentitySet from .rules import Rule, RemoveMatch log = getLogger(__name__).log DEFAULT = '__default__' POST_PROCESS = -2048 PRE_PROCESS = 2048 def _default_conflict_solver(match, conflicting_match): """ Default conflict solver for matches, shorter matches if they conflicts with longer ones :param conflicting_match: :type conflicting_match: :param match: :type match: :return: :rtype: """ if len(conflicting_match.initiator) < len(match.initiator): return conflicting_match elif len(match.initiator) < len(conflicting_match.initiator): return match return None class ConflictSolver(Rule): """ Remove conflicting matches. """ priority = PRE_PROCESS consequence = RemoveMatch @property def default_conflict_solver(self): # pylint:disable=no-self-use """ Default conflict solver to use. """ return _default_conflict_solver def when(self, matches, context): to_remove_matches = IdentitySet() public_matches = [match for match in matches if not match.private] public_matches.sort(key=len) for match in public_matches: conflicting_matches = matches.conflicting(match) if conflicting_matches: # keep the match only if it's the longest conflicting_matches = [conflicting_match for conflicting_match in conflicting_matches if not conflicting_match.private] conflicting_matches.sort(key=len) for conflicting_match in conflicting_matches: conflict_solvers = [(self.default_conflict_solver, False)] if match.conflict_solver: conflict_solvers.append((match.conflict_solver, False)) if conflicting_match.conflict_solver: conflict_solvers.append((conflicting_match.conflict_solver, True)) for conflict_solver, reverse in reversed(conflict_solvers): if reverse: to_remove = conflict_solver(conflicting_match, match) else: to_remove = conflict_solver(match, conflicting_match) if to_remove == DEFAULT: continue if to_remove and to_remove not in to_remove_matches: both_matches = [match, conflicting_match] both_matches.remove(to_remove) to_keep = both_matches[0] if to_keep not in to_remove_matches: log(self.log_level, "Conflicting match %s will be removed in favor of match %s", to_remove, to_keep) to_remove_matches.add(to_remove) break return to_remove_matches class PrivateRemover(Rule): """ Removes private matches rule. """ priority = POST_PROCESS consequence = RemoveMatch def when(self, matches, context): return [match for match in matches if match.private]
gpl-3.0
kgori/treeCl
treeCl/parutils.py
1
9550
from abc import ABCMeta, abstractmethod from .constants import PARALLEL_PROFILE from .utils import setup_progressbar, grouper, flatten_list import logging import multiprocessing import sys logger = logging.getLogger(__name__) __author__ = 'kgori' """ Introduced this workaround for a bug in multiprocessing where errors are thrown for an EINTR interrupt. Workaround taken from http://stackoverflow.com/a/5395277 - but changed because can't subclass from multiprocessing.Queue (it's a factory method) """ import errno def retry_on_eintr(function, *args, **kw): while True: try: return function(*args, **kw) except IOError as e: if e.errno == errno.EINTR: continue else: raise def get_from_queue(queue, block=True, timeout=None): return retry_on_eintr(queue.get, block, timeout) """ End of workaround """ def fun(f, q_in, q_out): while True: (i, x) = get_from_queue(q_in) if i is None: break q_out.put((i, f(*x))) def async_avail(): from IPython import parallel try: client = parallel.Client(PARALLEL_PROFILE) return len(client) > 0 except IOError: return False except Exception: return False def get_client(): from IPython import parallel try: client = parallel.Client(profile=PARALLEL_PROFILE) return client if len(client) > 0 else None except IOError: return None except Exception: return None def tupleise(args): for a in args: if isinstance(a, (tuple, list)): yield a else: yield (a,) def get_njobs(nargs, args): if nargs is not None: njobs = nargs elif isinstance(args, (tuple, list)): njobs = len(args) else: njobs = int(sys.maxsize / 1000000) # sys.maxsize is too large for progressbar to display ETA (datetime issue) return njobs def parallel_map(client, task, args, message, batchsize=1, background=False, nargs=None): """ Helper to map a function over a sequence of inputs, in parallel, with progress meter. :param client: IPython.parallel.Client instance :param task: Function :param args: Must be a list of tuples of arguments that the task function will be mapped onto. If the function takes a single argument, it still must be a 1-tuple. :param message: String for progress bar :param batchsize: Jobs are shipped in batches of this size. Higher numbers mean less network traffic, but longer execution time per job. :return: IPython.parallel.AsyncMapResult """ show_progress = bool(message) njobs = get_njobs(nargs, args) nproc = len(client) logger.debug('parallel_map: len(client) = {}'.format(len(client))) view = client.load_balanced_view() if show_progress: message += ' (IP:{}w:{}b)'.format(nproc, batchsize) pbar = setup_progressbar(message, njobs, simple_progress=True) if not background: pbar.start() map_result = view.map(task, *list(zip(*args)), chunksize=batchsize) if background: return map_result, client while not map_result.ready(): map_result.wait(1) if show_progress: pbar.update(min(njobs, map_result.progress * batchsize)) if show_progress: pbar.finish() return map_result def sequential_map(task, args, message, nargs=None): """ Helper to map a function over a sequence of inputs, sequentially, with progress meter. :param client: IPython.parallel.Client instance :param task: Function :param args: Must be a list of tuples of arguments that the task function will be mapped onto. If the function takes a single argument, it still must be a 1-tuple. :param message: String for progress bar :param batchsize: Jobs are shipped in batches of this size. Higher numbers mean less network traffic, but longer execution time per job. :return: IPython.parallel.AsyncMapResult """ njobs = get_njobs(nargs, args) show_progress = bool(message) if show_progress: pbar = setup_progressbar(message, njobs, simple_progress=True) pbar.start() map_result = [] for (i, arglist) in enumerate(tupleise(args), start=1): map_result.append(task(*arglist)) if show_progress: pbar.update(i) if show_progress: pbar.finish() return map_result def threadpool_map(task, args, message, concurrency, batchsize=1, nargs=None): """ Helper to map a function over a range of inputs, using a threadpool, with a progress meter """ import concurrent.futures njobs = get_njobs(nargs, args) show_progress = bool(message) batches = grouper(batchsize, tupleise(args)) batched_task = lambda batch: [task(*job) for job in batch] if show_progress: message += ' (TP:{}w:{}b)'.format(concurrency, batchsize) pbar = setup_progressbar(message, njobs, simple_progress=True) pbar.start() with concurrent.futures.ThreadPoolExecutor(max_workers=concurrency) as executor: futures = [] completed_count = 0 for batch in batches: futures.append(executor.submit(batched_task, batch)) if show_progress: for i, fut in enumerate(concurrent.futures.as_completed(futures), start=1): completed_count += len(fut.result()) pbar.update(completed_count) else: concurrent.futures.wait(futures) if show_progress: pbar.finish() return flatten_list([fut.result() for fut in futures]) def processpool_map(task, args, message, concurrency, batchsize=1, nargs=None): """ See http://stackoverflow.com/a/16071616 """ njobs = get_njobs(nargs, args) show_progress = bool(message) batches = grouper(batchsize, tupleise(args)) def batched_task(*batch): return [task(*job) for job in batch] if show_progress: message += ' (PP:{}w:{}b)'.format(concurrency, batchsize) pbar = setup_progressbar(message, njobs, simple_progress=True) pbar.start() q_in = multiprocessing.Queue() # Should I limit either queue size? Limiting in-queue q_out = multiprocessing.Queue() # increases time taken to send jobs, makes pbar less useful proc = [multiprocessing.Process(target=fun, args=(batched_task, q_in, q_out)) for _ in range(concurrency)] for p in proc: p.daemon = True p.start() sent = [q_in.put((i, x)) for (i, x) in enumerate(batches)] [q_in.put((None, None)) for _ in range(concurrency)] res = [] completed_count = 0 for _ in range(len(sent)): result = get_from_queue(q_out) res.append(result) completed_count += len(result[1]) if show_progress: pbar.update(completed_count) [p.join() for p in proc] if show_progress: pbar.finish() return flatten_list([x for (i, x) in sorted(res)]) class JobHandler(object): """ Base class to provide uniform interface for all job handlers """ metaclass = ABCMeta @abstractmethod def __call__(self, task, args, message, batchsize): """ If you define a message, then progress will be written to stderr """ pass class SequentialJobHandler(JobHandler): """ Jobs are handled using a simple map """ def __call__(self, task, args, message, batchsize, nargs=None): if batchsize > 1: logger.warn("Setting batchsize > 1 has no effect when using a SequentialJobHandler") return sequential_map(task, args, message, nargs) class ThreadpoolJobHandler(JobHandler): """ Jobs are handled by a threadpool using concurrent.futures """ def __init__(self, concurrency): self.concurrency = concurrency def __call__(self, task, args, message, batchsize, nargs=None): return threadpool_map(task, args, message, self.concurrency, batchsize, nargs) class ProcesspoolJobHandler(JobHandler): """ Jobs are handled by a threadpool using concurrent.futures """ def __init__(self, concurrency): self.concurrency = concurrency def __call__(self, task, args, message, batchsize, nargs=None): return processpool_map(task, args, message, self.concurrency, batchsize, nargs) class IPythonJobHandler(JobHandler): """ Jobs are handled using an IPython.parallel.Client """ def __init__(self, profile=None): """ Initialise the IPythonJobHandler using the given ipython profile. Parameters ---------- profile: string The ipython profile to connect to - this should already be running an ipcluster If the connection fails it raises a RuntimeError """ import IPython.parallel try: self.client=IPython.parallel.Client(profile=profile) logger.debug('__init__: len(client) = {}'.format(len(self.client))) except (IOError, IPython.parallel.TimeoutError): msg = 'Could not obtain an IPython parallel Client using profile "{}"'.format(profile) logger.error(msg) raise RuntimeError(msg) def __call__(self, task, args, message, batchsize): logger.debug('__call__: len(client) = {}'.format(len(self.client))) return list(parallel_map(self.client, task, args, message, batchsize))
mit
anbangr/trusted-juju
juju/unit/tests/test_charm.py
1
5922
from functools import partial import os import shutil from twisted.internet.defer import inlineCallbacks, returnValue, succeed, fail from twisted.web.error import Error from twisted.web.client import downloadPage from juju.charm import get_charm_from_path from juju.charm.bundle import CharmBundle from juju.charm.publisher import CharmPublisher from juju.charm.tests import local_charm_id from juju.charm.tests.test_directory import sample_directory from juju.errors import FileNotFound from juju.lib import under from juju.state.errors import CharmStateNotFound from juju.state.tests.common import StateTestBase from juju.unit.charm import download_charm from juju.lib.mocker import MATCH class CharmPublisherTestBase(StateTestBase): @inlineCallbacks def setUp(self): yield super(CharmPublisherTestBase, self).setUp() yield self.push_default_config() self.provider = self.config.get_default().get_machine_provider() self.storage = self.provider.get_file_storage() @inlineCallbacks def publish_charm(self, charm_path=sample_directory): charm = get_charm_from_path(charm_path) publisher = CharmPublisher(self.client, self.storage) yield publisher.add_charm(local_charm_id(charm), charm) charm_states = yield publisher.publish() returnValue((charm, charm_states[0])) class DownloadTestCase(CharmPublisherTestBase): @inlineCallbacks def test_charm_download_file(self): """Downloading a charm should store the charm locally. """ charm, charm_state = yield self.publish_charm() charm_directory = self.makeDir() # Download the charm yield download_charm( self.client, charm_state.id, charm_directory) # Verify the downloaded copy checksum = charm.get_sha256() charm_id = local_charm_id(charm) charm_key = under.quote("%s:%s" % (charm_id, checksum)) charm_path = os.path.join(charm_directory, charm_key) self.assertTrue(os.path.exists(charm_path)) bundle = CharmBundle(charm_path) self.assertEquals(bundle.get_revision(), charm.get_revision()) self.assertEqual(checksum, bundle.get_sha256()) @inlineCallbacks def test_charm_missing_download_file(self): """Downloading a file that doesn't exist raises FileNotFound. """ charm, charm_state = yield self.publish_charm() charm_directory = self.makeDir() # Delete the file file_path = charm_state.bundle_url[len("file://"):] os.remove(file_path) # Download the charm yield self.assertFailure( download_charm(self.client, charm_state.id, charm_directory), FileNotFound) @inlineCallbacks def test_charm_download_http(self): """Downloading a charm should store the charm locally. """ mock_storage = self.mocker.patch(self.storage) def match_string(expected, value): self.assertTrue(isinstance(value, basestring)) self.assertIn(expected, value) return True mock_storage.get_url(MATCH( partial(match_string, "local_3a_series_2f_dummy-1"))) self.mocker.result("http://example.com/foobar.zip") download_page = self.mocker.replace(downloadPage) download_page( MATCH(partial(match_string, "http://example.com/foobar.zip")), MATCH(partial(match_string, "local_3a_series_2f_dummy-1"))) def bundle_in_place(url, local_path): # must keep ref to charm else temp file goes out of scope. charm = get_charm_from_path(sample_directory) bundle = charm.as_bundle() shutil.copyfile(bundle.path, local_path) self.mocker.call(bundle_in_place) self.mocker.result(succeed(True)) self.mocker.replay() charm, charm_state = yield self.publish_charm() charm_directory = self.makeDir() self.assertEqual( charm_state.bundle_url, "http://example.com/foobar.zip") # Download the charm yield download_charm( self.client, charm_state.id, charm_directory) @inlineCallbacks def test_charm_download_http_error(self): """Errors in donwloading a charm are reported as charm not found. """ def match_string(expected, value): self.assertTrue(isinstance(value, basestring)) self.assertIn(expected, value) return True mock_storage = self.mocker.patch(self.storage) mock_storage.get_url( MATCH(partial(match_string, "local_3a_series_2f_dummy-1"))) remote_url = "http://example.com/foobar.zip" self.mocker.result(remote_url) download_page = self.mocker.replace(downloadPage) download_page( MATCH(partial(match_string, "http://example.com/foobar.zip")), MATCH(partial(match_string, "local_3a_series_2f_dummy-1"))) self.mocker.result(fail(Error("400", "Bad Stuff", ""))) self.mocker.replay() charm, charm_state = yield self.publish_charm() charm_directory = self.makeDir() self.assertEqual(charm_state.bundle_url, remote_url) error = yield self.assertFailure( download_charm(self.client, charm_state.id, charm_directory), FileNotFound) self.assertIn(remote_url, str(error)) @inlineCallbacks def test_charm_download_not_found(self): """An error is raised if trying to download a non existant charm. """ charm_directory = self.makeDir() # Download the charm error = yield self.assertFailure( download_charm( self.client, "local:mickey-21", charm_directory), CharmStateNotFound) self.assertEquals(str(error), "Charm 'local:mickey-21' was not found")
agpl-3.0
zengenti/ansible
lib/ansible/modules/network/nxos/nxos_overlay_global.py
6
8874
#!/usr/bin/python # # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see <http://www.gnu.org/licenses/>. # ANSIBLE_METADATA = {'status': ['preview'], 'supported_by': 'community', 'version': '1.0'} DOCUMENTATION = ''' --- module: nxos_overlay_global extends_documentation_fragment: nxos version_added: "2.2" short_description: Configures anycast gateway MAC of the switch. description: - Configures anycast gateway MAC of the switch. author: Gabriele Gerbino (@GGabriele) notes: - Default restores params default value - Supported MAC address format are "E.E.E", "EE-EE-EE-EE-EE-EE", "EE:EE:EE:EE:EE:EE" and "EEEE.EEEE.EEEE" options: anycast_gateway_mac: description: - Anycast gateway mac of the switch. required: true default: null ''' EXAMPLES = ''' - nxos_overlay_global: anycast_gateway_mac: "b.b.b" username: "{{ un }}" password: "{{ pwd }}" host: "{{ inventory_hostname }}" ''' RETURN = ''' proposed: description: k/v pairs of parameters passed into module returned: verbose mode type: dict sample: {"asn": "65535", "router_id": "1.1.1.1", "vrf": "test"} existing: description: k/v pairs of existing BGP configuration returned: verbose mode type: dict sample: {"asn": "65535", "bestpath_always_compare_med": false, "bestpath_aspath_multipath_relax": false, "bestpath_compare_neighborid": false, "bestpath_compare_routerid": false, "bestpath_cost_community_ignore": false, "bestpath_med_confed": false, "bestpath_med_missing_as_worst": false, "bestpath_med_non_deterministic": false, "cluster_id": "", "confederation_id": "", "confederation_peers": "", "graceful_restart": true, "graceful_restart_helper": false, "graceful_restart_timers_restart": "120", "graceful_restart_timers_stalepath_time": "300", "local_as": "", "log_neighbor_changes": false, "maxas_limit": "", "neighbor_down_fib_accelerate": false, "reconnect_interval": "60", "router_id": "11.11.11.11", "suppress_fib_pending": false, "timer_bestpath_limit": "", "timer_bgp_hold": "180", "timer_bgp_keepalive": "60", "vrf": "test"} end_state: description: k/v pairs of BGP configuration after module execution returned: verbose mode type: dict sample: {"asn": "65535", "bestpath_always_compare_med": false, "bestpath_aspath_multipath_relax": false, "bestpath_compare_neighborid": false, "bestpath_compare_routerid": false, "bestpath_cost_community_ignore": false, "bestpath_med_confed": false, "bestpath_med_missing_as_worst": false, "bestpath_med_non_deterministic": false, "cluster_id": "", "confederation_id": "", "confederation_peers": "", "graceful_restart": true, "graceful_restart_helper": false, "graceful_restart_timers_restart": "120", "graceful_restart_timers_stalepath_time": "300", "local_as": "", "log_neighbor_changes": false, "maxas_limit": "", "neighbor_down_fib_accelerate": false, "reconnect_interval": "60", "router_id": "1.1.1.1", "suppress_fib_pending": false, "timer_bestpath_limit": "", "timer_bgp_hold": "180", "timer_bgp_keepalive": "60", "vrf": "test"} updates: description: commands sent to the device returned: always type: list sample: ["router bgp 65535", "vrf test", "router-id 1.1.1.1"] changed: description: check to see if a change was made on the device returned: always type: boolean sample: true ''' import re from ansible.module_utils.nxos import get_config, load_config from ansible.module_utils.nxos import nxos_argument_spec, check_args from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.netcfg import CustomNetworkConfig PARAM_TO_COMMAND_KEYMAP = { 'anycast_gateway_mac': 'fabric forwarding anycast-gateway-mac', } def invoke(name, *args, **kwargs): func = globals().get(name) if func: return func(*args, **kwargs) def get_value(arg, config, module): REGEX = re.compile(r'(?:{0}\s)(?P<value>.*)$'.format(PARAM_TO_COMMAND_KEYMAP[arg]), re.M) value = '' if PARAM_TO_COMMAND_KEYMAP[arg] in config: value = REGEX.search(config).group('value') return value def get_existing(module, args): existing = {} config = str(get_config(module)) for arg in args: existing[arg] = get_value(arg, config, module) return existing def apply_key_map(key_map, table): new_dict = {} for key, value in table.items(): new_key = key_map.get(key) if new_key: value = table.get(key) if value: new_dict[new_key] = value else: new_dict[new_key] = value return new_dict def get_commands(module, existing, proposed, candidate): commands = list() proposed_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, proposed) existing_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, existing) for key, value in proposed_commands.items(): if value == 'default': existing_value = existing_commands.get(key) if existing_value: commands.append('no {0} {1}'.format(key, existing_value)) else: if 'anycast-gateway-mac' in key: value = normalize_mac(value, module) command = '{0} {1}'.format(key, value) commands.append(command) if commands: candidate.add(commands, parents=[]) def normalize_mac(proposed_mac, module): try: if '-' in proposed_mac: splitted_mac = proposed_mac.split('-') if len(splitted_mac) != 6: raise ValueError for octect in splitted_mac: if len(octect) != 2: raise ValueError elif '.' in proposed_mac: splitted_mac = [] splitted_dot_mac = proposed_mac.split('.') if len(splitted_dot_mac) != 3: raise ValueError for octect in splitted_dot_mac: if len(octect) > 4: raise ValueError else: octect_len = len(octect) padding = 4 - octect_len splitted_mac.append(octect.zfill(padding+1)) elif ':' in proposed_mac: splitted_mac = proposed_mac.split(':') if len(splitted_mac) != 6: raise ValueError for octect in splitted_mac: if len(octect) != 2: raise ValueError else: raise ValueError except ValueError: module.fail_json(msg='Invalid MAC address format', proposed_mac=proposed_mac) joined_mac = ''.join(splitted_mac) mac = [joined_mac[i:i+4] for i in range(0, len(joined_mac), 4)] return '.'.join(mac).upper() def main(): argument_spec = dict( anycast_gateway_mac=dict(required=True, type='str'), m_facts=dict(required=False, default=False, type='bool'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) args = [ 'anycast_gateway_mac' ] existing = invoke('get_existing', module, args) end_state = existing proposed = dict((k, v) for k, v in module.params.items() if v is not None and k in args) result = {} candidate = CustomNetworkConfig(indent=3) invoke('get_commands', module, existing, proposed, candidate) if not module.check_mode: load_config(module, candidate) if module._verbosity > 0: end_state = invoke('get_existing', module, args) result['end_state'] = end_state result['existing'] = existing result['proposed'] = proposed result['warnings'] = True module.exit_json(**result) if __name__ == '__main__': main()
gpl-3.0
jakobworldpeace/scikit-learn
sklearn/linear_model/tests/test_theil_sen.py
55
9939
""" Testing for Theil-Sen module (sklearn.linear_model.theil_sen) """ # Author: Florian Wilhelm <florian.wilhelm@gmail.com> # License: BSD 3 clause from __future__ import division, print_function, absolute_import import os import sys from contextlib import contextmanager import numpy as np from numpy.testing import assert_array_equal, assert_array_less from numpy.testing import assert_array_almost_equal, assert_warns from scipy.linalg import norm from scipy.optimize import fmin_bfgs from sklearn.exceptions import ConvergenceWarning from sklearn.linear_model import LinearRegression, TheilSenRegressor from sklearn.linear_model.theil_sen import _spatial_median, _breakdown_point from sklearn.linear_model.theil_sen import _modified_weiszfeld_step from sklearn.utils.testing import ( assert_almost_equal, assert_greater, assert_less, raises, ) @contextmanager def no_stdout_stderr(): old_stdout = sys.stdout old_stderr = sys.stderr with open(os.devnull, 'w') as devnull: sys.stdout = devnull sys.stderr = devnull yield devnull.flush() sys.stdout = old_stdout sys.stderr = old_stderr def gen_toy_problem_1d(intercept=True): random_state = np.random.RandomState(0) # Linear model y = 3*x + N(2, 0.1**2) w = 3. if intercept: c = 2. n_samples = 50 else: c = 0.1 n_samples = 100 x = random_state.normal(size=n_samples) noise = 0.1 * random_state.normal(size=n_samples) y = w * x + c + noise # Add some outliers if intercept: x[42], y[42] = (-2, 4) x[43], y[43] = (-2.5, 8) x[33], y[33] = (2.5, 1) x[49], y[49] = (2.1, 2) else: x[42], y[42] = (-2, 4) x[43], y[43] = (-2.5, 8) x[53], y[53] = (2.5, 1) x[60], y[60] = (2.1, 2) x[72], y[72] = (1.8, -7) return x[:, np.newaxis], y, w, c def gen_toy_problem_2d(): random_state = np.random.RandomState(0) n_samples = 100 # Linear model y = 5*x_1 + 10*x_2 + N(1, 0.1**2) X = random_state.normal(size=(n_samples, 2)) w = np.array([5., 10.]) c = 1. noise = 0.1 * random_state.normal(size=n_samples) y = np.dot(X, w) + c + noise # Add some outliers n_outliers = n_samples // 10 ix = random_state.randint(0, n_samples, size=n_outliers) y[ix] = 50 * random_state.normal(size=n_outliers) return X, y, w, c def gen_toy_problem_4d(): random_state = np.random.RandomState(0) n_samples = 10000 # Linear model y = 5*x_1 + 10*x_2 + 42*x_3 + 7*x_4 + N(1, 0.1**2) X = random_state.normal(size=(n_samples, 4)) w = np.array([5., 10., 42., 7.]) c = 1. noise = 0.1 * random_state.normal(size=n_samples) y = np.dot(X, w) + c + noise # Add some outliers n_outliers = n_samples // 10 ix = random_state.randint(0, n_samples, size=n_outliers) y[ix] = 50 * random_state.normal(size=n_outliers) return X, y, w, c def test_modweiszfeld_step_1d(): X = np.array([1., 2., 3.]).reshape(3, 1) # Check startvalue is element of X and solution median = 2. new_y = _modified_weiszfeld_step(X, median) assert_array_almost_equal(new_y, median) # Check startvalue is not the solution y = 2.5 new_y = _modified_weiszfeld_step(X, y) assert_array_less(median, new_y) assert_array_less(new_y, y) # Check startvalue is not the solution but element of X y = 3. new_y = _modified_weiszfeld_step(X, y) assert_array_less(median, new_y) assert_array_less(new_y, y) # Check that a single vector is identity X = np.array([1., 2., 3.]).reshape(1, 3) y = X[0, ] new_y = _modified_weiszfeld_step(X, y) assert_array_equal(y, new_y) def test_modweiszfeld_step_2d(): X = np.array([0., 0., 1., 1., 0., 1.]).reshape(3, 2) y = np.array([0.5, 0.5]) # Check first two iterations new_y = _modified_weiszfeld_step(X, y) assert_array_almost_equal(new_y, np.array([1 / 3, 2 / 3])) new_y = _modified_weiszfeld_step(X, new_y) assert_array_almost_equal(new_y, np.array([0.2792408, 0.7207592])) # Check fix point y = np.array([0.21132505, 0.78867497]) new_y = _modified_weiszfeld_step(X, y) assert_array_almost_equal(new_y, y) def test_spatial_median_1d(): X = np.array([1., 2., 3.]).reshape(3, 1) true_median = 2. _, median = _spatial_median(X) assert_array_almost_equal(median, true_median) # Test larger problem and for exact solution in 1d case random_state = np.random.RandomState(0) X = random_state.randint(100, size=(1000, 1)) true_median = np.median(X.ravel()) _, median = _spatial_median(X) assert_array_equal(median, true_median) def test_spatial_median_2d(): X = np.array([0., 0., 1., 1., 0., 1.]).reshape(3, 2) _, median = _spatial_median(X, max_iter=100, tol=1.e-6) def cost_func(y): dists = np.array([norm(x - y) for x in X]) return np.sum(dists) # Check if median is solution of the Fermat-Weber location problem fermat_weber = fmin_bfgs(cost_func, median, disp=False) assert_array_almost_equal(median, fermat_weber) # Check when maximum iteration is exceeded a warning is emitted assert_warns(ConvergenceWarning, _spatial_median, X, max_iter=30, tol=0.) def test_theil_sen_1d(): X, y, w, c = gen_toy_problem_1d() # Check that Least Squares fails lstq = LinearRegression().fit(X, y) assert_greater(np.abs(lstq.coef_ - w), 0.9) # Check that Theil-Sen works theil_sen = TheilSenRegressor(random_state=0).fit(X, y) assert_array_almost_equal(theil_sen.coef_, w, 1) assert_array_almost_equal(theil_sen.intercept_, c, 1) def test_theil_sen_1d_no_intercept(): X, y, w, c = gen_toy_problem_1d(intercept=False) # Check that Least Squares fails lstq = LinearRegression(fit_intercept=False).fit(X, y) assert_greater(np.abs(lstq.coef_ - w - c), 0.5) # Check that Theil-Sen works theil_sen = TheilSenRegressor(fit_intercept=False, random_state=0).fit(X, y) assert_array_almost_equal(theil_sen.coef_, w + c, 1) assert_almost_equal(theil_sen.intercept_, 0.) def test_theil_sen_2d(): X, y, w, c = gen_toy_problem_2d() # Check that Least Squares fails lstq = LinearRegression().fit(X, y) assert_greater(norm(lstq.coef_ - w), 1.0) # Check that Theil-Sen works theil_sen = TheilSenRegressor(max_subpopulation=1e3, random_state=0).fit(X, y) assert_array_almost_equal(theil_sen.coef_, w, 1) assert_array_almost_equal(theil_sen.intercept_, c, 1) def test_calc_breakdown_point(): bp = _breakdown_point(1e10, 2) assert_less(np.abs(bp - 1 + 1 / (np.sqrt(2))), 1.e-6) @raises(ValueError) def test_checksubparams_negative_subpopulation(): X, y, w, c = gen_toy_problem_1d() TheilSenRegressor(max_subpopulation=-1, random_state=0).fit(X, y) @raises(ValueError) def test_checksubparams_too_few_subsamples(): X, y, w, c = gen_toy_problem_1d() TheilSenRegressor(n_subsamples=1, random_state=0).fit(X, y) @raises(ValueError) def test_checksubparams_too_many_subsamples(): X, y, w, c = gen_toy_problem_1d() TheilSenRegressor(n_subsamples=101, random_state=0).fit(X, y) @raises(ValueError) def test_checksubparams_n_subsamples_if_less_samples_than_features(): random_state = np.random.RandomState(0) n_samples, n_features = 10, 20 X = random_state.normal(size=(n_samples, n_features)) y = random_state.normal(size=n_samples) TheilSenRegressor(n_subsamples=9, random_state=0).fit(X, y) def test_subpopulation(): X, y, w, c = gen_toy_problem_4d() theil_sen = TheilSenRegressor(max_subpopulation=250, random_state=0).fit(X, y) assert_array_almost_equal(theil_sen.coef_, w, 1) assert_array_almost_equal(theil_sen.intercept_, c, 1) def test_subsamples(): X, y, w, c = gen_toy_problem_4d() theil_sen = TheilSenRegressor(n_subsamples=X.shape[0], random_state=0).fit(X, y) lstq = LinearRegression().fit(X, y) # Check for exact the same results as Least Squares assert_array_almost_equal(theil_sen.coef_, lstq.coef_, 9) def test_verbosity(): X, y, w, c = gen_toy_problem_1d() # Check that Theil-Sen can be verbose with no_stdout_stderr(): TheilSenRegressor(verbose=True, random_state=0).fit(X, y) TheilSenRegressor(verbose=True, max_subpopulation=10, random_state=0).fit(X, y) def test_theil_sen_parallel(): X, y, w, c = gen_toy_problem_2d() # Check that Least Squares fails lstq = LinearRegression().fit(X, y) assert_greater(norm(lstq.coef_ - w), 1.0) # Check that Theil-Sen works theil_sen = TheilSenRegressor(n_jobs=-1, random_state=0, max_subpopulation=2e3).fit(X, y) assert_array_almost_equal(theil_sen.coef_, w, 1) assert_array_almost_equal(theil_sen.intercept_, c, 1) def test_less_samples_than_features(): random_state = np.random.RandomState(0) n_samples, n_features = 10, 20 X = random_state.normal(size=(n_samples, n_features)) y = random_state.normal(size=n_samples) # Check that Theil-Sen falls back to Least Squares if fit_intercept=False theil_sen = TheilSenRegressor(fit_intercept=False, random_state=0).fit(X, y) lstq = LinearRegression(fit_intercept=False).fit(X, y) assert_array_almost_equal(theil_sen.coef_, lstq.coef_, 12) # Check fit_intercept=True case. This will not be equal to the Least # Squares solution since the intercept is calculated differently. theil_sen = TheilSenRegressor(fit_intercept=True, random_state=0).fit(X, y) y_pred = theil_sen.predict(X) assert_array_almost_equal(y_pred, y, 12)
bsd-3-clause
pbrazdil/phantomjs
src/breakpad/src/third_party/protobuf/protobuf/python/google/protobuf/reflection.py
260
5864
# Protocol Buffers - Google's data interchange format # Copyright 2008 Google Inc. All rights reserved. # http://code.google.com/p/protobuf/ # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above # copyright notice, this list of conditions and the following disclaimer # in the documentation and/or other materials provided with the # distribution. # * Neither the name of Google Inc. nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # This code is meant to work on Python 2.4 and above only. """Contains a metaclass and helper functions used to create protocol message classes from Descriptor objects at runtime. Recall that a metaclass is the "type" of a class. (A class is to a metaclass what an instance is to a class.) In this case, we use the GeneratedProtocolMessageType metaclass to inject all the useful functionality into the classes output by the protocol compiler at compile-time. The upshot of all this is that the real implementation details for ALL pure-Python protocol buffers are *here in this file*. """ __author__ = 'robinson@google.com (Will Robinson)' from google.protobuf.internal import api_implementation from google.protobuf import descriptor as descriptor_mod _FieldDescriptor = descriptor_mod.FieldDescriptor if api_implementation.Type() == 'cpp': from google.protobuf.internal import cpp_message _NewMessage = cpp_message.NewMessage _InitMessage = cpp_message.InitMessage else: from google.protobuf.internal import python_message _NewMessage = python_message.NewMessage _InitMessage = python_message.InitMessage class GeneratedProtocolMessageType(type): """Metaclass for protocol message classes created at runtime from Descriptors. We add implementations for all methods described in the Message class. We also create properties to allow getting/setting all fields in the protocol message. Finally, we create slots to prevent users from accidentally "setting" nonexistent fields in the protocol message, which then wouldn't get serialized / deserialized properly. The protocol compiler currently uses this metaclass to create protocol message classes at runtime. Clients can also manually create their own classes at runtime, as in this example: mydescriptor = Descriptor(.....) class MyProtoClass(Message): __metaclass__ = GeneratedProtocolMessageType DESCRIPTOR = mydescriptor myproto_instance = MyProtoClass() myproto.foo_field = 23 ... """ # Must be consistent with the protocol-compiler code in # proto2/compiler/internal/generator.*. _DESCRIPTOR_KEY = 'DESCRIPTOR' def __new__(cls, name, bases, dictionary): """Custom allocation for runtime-generated class types. We override __new__ because this is apparently the only place where we can meaningfully set __slots__ on the class we're creating(?). (The interplay between metaclasses and slots is not very well-documented). Args: name: Name of the class (ignored, but required by the metaclass protocol). bases: Base classes of the class we're constructing. (Should be message.Message). We ignore this field, but it's required by the metaclass protocol dictionary: The class dictionary of the class we're constructing. dictionary[_DESCRIPTOR_KEY] must contain a Descriptor object describing this protocol message type. Returns: Newly-allocated class. """ descriptor = dictionary[GeneratedProtocolMessageType._DESCRIPTOR_KEY] _NewMessage(descriptor, dictionary) superclass = super(GeneratedProtocolMessageType, cls) new_class = superclass.__new__(cls, name, bases, dictionary) setattr(descriptor, '_concrete_class', new_class) return new_class def __init__(cls, name, bases, dictionary): """Here we perform the majority of our work on the class. We add enum getters, an __init__ method, implementations of all Message methods, and properties for all fields in the protocol type. Args: name: Name of the class (ignored, but required by the metaclass protocol). bases: Base classes of the class we're constructing. (Should be message.Message). We ignore this field, but it's required by the metaclass protocol dictionary: The class dictionary of the class we're constructing. dictionary[_DESCRIPTOR_KEY] must contain a Descriptor object describing this protocol message type. """ descriptor = dictionary[GeneratedProtocolMessageType._DESCRIPTOR_KEY] _InitMessage(descriptor, cls) superclass = super(GeneratedProtocolMessageType, cls) superclass.__init__(name, bases, dictionary)
bsd-3-clause
larsmans/scikit-learn
sklearn/cluster/setup.py
31
1248
# Author: Alexandre Gramfort <alexandre.gramfort@inria.fr> # License: BSD 3 clause import os from os.path import join import numpy from sklearn._build_utils import get_blas_info def configuration(parent_package='', top_path=None): from numpy.distutils.misc_util import Configuration cblas_libs, blas_info = get_blas_info() libraries = [] if os.name == 'posix': cblas_libs.append('m') libraries.append('m') config = Configuration('cluster', parent_package, top_path) config.add_extension('_hierarchical', sources=['_hierarchical.cpp'], language="c++", include_dirs=[numpy.get_include()], libraries=libraries) config.add_extension( '_k_means', libraries=cblas_libs, sources=['_k_means.c'], include_dirs=[join('..', 'src', 'cblas'), numpy.get_include(), blas_info.pop('include_dirs', [])], extra_compile_args=blas_info.pop('extra_compile_args', []), **blas_info ) return config if __name__ == '__main__': from numpy.distutils.core import setup setup(**configuration(top_path='').todict())
bsd-3-clause
bcantoni/ccm
ccmlib/dse_node.py
1
22234
# ccm node from __future__ import absolute_import, with_statement import os import re import shutil import signal import stat import subprocess import time import yaml from six import iteritems, print_ from ccmlib import common, extension, repository from ccmlib.node import (Node, NodeError, ToolError, handle_external_tool_process) class DseNode(Node): """ Provides interactions to a DSE node. """ def __init__(self, name, cluster, auto_bootstrap, thrift_interface, storage_interface, jmx_port, remote_debug_port, initial_token, save=True, binary_interface=None, byteman_port='0', environment_variables=None): super(DseNode, self).__init__(name, cluster, auto_bootstrap, thrift_interface, storage_interface, jmx_port, remote_debug_port, initial_token, save, binary_interface, byteman_port, environment_variables=environment_variables) self.get_cassandra_version() self._dse_config_options = {} if self.cluster.hasOpscenter(): self._copy_agent() def get_install_cassandra_root(self): return os.path.join(self.get_install_dir(), 'resources', 'cassandra') def get_node_cassandra_root(self): return os.path.join(self.get_path(), 'resources', 'cassandra') def get_conf_dir(self): """ Returns the path to the directory where Cassandra config are located """ return os.path.join(self.get_path(), 'resources', 'cassandra', 'conf') def get_tool(self, toolname): return common.join_bin(os.path.join(self.get_install_dir(), 'resources', 'cassandra'), 'bin', toolname) def get_tool_args(self, toolname): return [common.join_bin(os.path.join(self.get_install_dir(), 'resources', 'cassandra'), 'bin', 'dse'), toolname] def get_env(self): (node_ip, _) = self.network_interfaces['binary'] return common.make_dse_env(self.get_install_dir(), self.get_path(), node_ip) def get_cassandra_version(self): return common.get_dse_cassandra_version(self.get_install_dir()) def node_setup(self, version, verbose): dir, v = repository.setup_dse(version, self.cluster.dse_username, self.cluster.dse_password, verbose=verbose) return dir def set_workloads(self, workloads): self.workloads = workloads self._update_config() if 'solr' in self.workloads: self.__generate_server_xml() if 'graph' in self.workloads: (node_ip, _) = self.network_interfaces['binary'] conf_file = os.path.join(self.get_path(), 'resources', 'dse', 'conf', 'dse.yaml') with open(conf_file, 'r') as f: data = yaml.load(f) graph_options = data['graph'] graph_options['gremlin_server']['host'] = node_ip self.set_dse_configuration_options({'graph': graph_options}) self.__update_gremlin_config_yaml() if 'dsefs' in self.workloads: dsefs_options = {'dsefs_options': {'enabled': True, 'work_dir': os.path.join(self.get_path(), 'dsefs'), 'data_directories': [{'dir': os.path.join(self.get_path(), 'dsefs', 'data')}]}} self.set_dse_configuration_options(dsefs_options) if 'spark' in self.workloads: self._update_spark_env() def set_dse_configuration_options(self, values=None): if values is not None: self._dse_config_options = common.merge_configuration(self._dse_config_options, values) self.import_dse_config_files() def watch_log_for_alive(self, nodes, from_mark=None, timeout=720, filename='system.log'): """ Watch the log of this node until it detects that the provided other nodes are marked UP. This method works similarly to watch_log_for_death. We want to provide a higher default timeout when this is called on DSE. """ super(DseNode, self).watch_log_for_alive(nodes, from_mark=from_mark, timeout=timeout, filename=filename) def get_launch_bin(self): cdir = self.get_install_dir() launch_bin = common.join_bin(cdir, 'bin', 'dse') # Copy back the dse scripts since profiling may have modified it the previous time shutil.copy(launch_bin, self.get_bin_dir()) return common.join_bin(self.get_path(), 'bin', 'dse') def add_custom_launch_arguments(self, args): args.append('cassandra') for workload in self.workloads: if 'hadoop' in workload: args.append('-t') if 'solr' in workload: args.append('-s') if 'spark' in workload: args.append('-k') if 'cfs' in workload: args.append('-c') if 'graph' in workload: args.append('-g') def start(self, join_ring=True, no_wait=False, verbose=False, update_pid=True, wait_other_notice=True, replace_token=None, replace_address=None, jvm_args=None, wait_for_binary_proto=False, profile_options=None, use_jna=False, quiet_start=False, allow_root=False, set_migration_task=True): process = super(DseNode, self).start(join_ring, no_wait, verbose, update_pid, wait_other_notice, replace_token, replace_address, jvm_args, wait_for_binary_proto, profile_options, use_jna, quiet_start, allow_root, set_migration_task) if self.cluster.hasOpscenter(): self._start_agent() def _start_agent(self): agent_dir = os.path.join(self.get_path(), 'datastax-agent') if os.path.exists(agent_dir): self._write_agent_address_yaml(agent_dir) self._write_agent_log4j_properties(agent_dir) args = [os.path.join(agent_dir, 'bin', common.platform_binary('datastax-agent'))] subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) def stop(self, wait=True, wait_other_notice=False, signal_event=signal.SIGTERM, **kwargs): if self.cluster.hasOpscenter(): self._stop_agent() return super(DseNode, self).stop(wait=wait, wait_other_notice=wait_other_notice, signal_event=signal_event, **kwargs) def _stop_agent(self): agent_dir = os.path.join(self.get_path(), 'datastax-agent') if os.path.exists(agent_dir): pidfile = os.path.join(agent_dir, 'datastax-agent.pid') if os.path.exists(pidfile): with open(pidfile, 'r') as f: pid = int(f.readline().strip()) f.close() if pid is not None: try: os.kill(pid, signal.SIGKILL) except OSError: pass os.remove(pidfile) def nodetool(self, cmd, username=None, password=None, capture_output=True, wait=True): if password is not None: cmd = '-pw {} '.format(password) + cmd if username is not None: cmd = '-u {} '.format(username) + cmd return super(DseNode, self).nodetool(cmd) def dsetool(self, cmd): env = self.get_env() extension.append_to_client_env(self, env) node_ip, binary_port = self.network_interfaces['binary'] dsetool = common.join_bin(self.get_install_dir(), 'bin', 'dsetool') args = [dsetool, '-h', node_ip, '-j', str(self.jmx_port), '-c', str(binary_port)] args += cmd.split() p = subprocess.Popen(args, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return handle_external_tool_process(p, args) def dse(self, dse_options=None): if dse_options is None: dse_options = [] env = self.get_env() extension.append_to_client_env(self, env) env['JMX_PORT'] = self.jmx_port dse = common.join_bin(self.get_install_dir(), 'bin', 'dse') args = [dse] args += dse_options p = subprocess.Popen(args, env=env) #Don't redirect stdout/stderr, users need to interact with new process return handle_external_tool_process(p, args) def hadoop(self, hadoop_options=None): if hadoop_options is None: hadoop_options = [] env = self.get_env() env['JMX_PORT'] = self.jmx_port dse = common.join_bin(self.get_install_dir(), 'bin', 'dse') args = [dse, 'hadoop'] args += hadoop_options p = subprocess.Popen(args, env=env) #Don't redirect stdout/stderr, users need to interact with new process return handle_external_tool_process(p, args) def hive(self, hive_options=None): if hive_options is None: hive_options = [] env = self.get_env() env['JMX_PORT'] = self.jmx_port dse = common.join_bin(self.get_install_dir(), 'bin', 'dse') args = [dse, 'hive'] args += hive_options p = subprocess.Popen(args, env=env) #Don't redirect stdout/stderr, users need to interact with new process return handle_external_tool_process(p, args) def pig(self, pig_options=None): if pig_options is None: pig_options = [] env = self.get_env() env['JMX_PORT'] = self.jmx_port dse = common.join_bin(self.get_install_dir(), 'bin', 'dse') args = [dse, 'pig'] args += pig_options p = subprocess.Popen(args, env=env) #Don't redirect stdout/stderr, users need to interact with new process return handle_external_tool_process(p, args) def sqoop(self, sqoop_options=None): if sqoop_options is None: sqoop_options = [] env = self.get_env() env['JMX_PORT'] = self.jmx_port dse = common.join_bin(self.get_install_dir(), 'bin', 'dse') args = [dse, 'sqoop'] args += sqoop_options p = subprocess.Popen(args, env=env) #Don't redirect stdout/stderr, users need to interact with new process return handle_external_tool_process(p, args) def spark(self, spark_options=None): if spark_options is None: spark_options = [] env = self.get_env() env['JMX_PORT'] = self.jmx_port dse = common.join_bin(self.get_install_dir(), 'bin', 'dse') args = [dse, 'spark'] args += spark_options p = subprocess.Popen(args, env=env) #Don't redirect stdout/stderr, users need to interact with new process return handle_external_tool_process(p, args) def import_dse_config_files(self): self._update_config() if not os.path.isdir(os.path.join(self.get_path(), 'resources', 'dse', 'conf')): os.makedirs(os.path.join(self.get_path(), 'resources', 'dse', 'conf')) common.copy_directory(os.path.join(self.get_install_dir(), 'resources', 'dse', 'conf'), os.path.join(self.get_path(), 'resources', 'dse', 'conf')) self.__update_yaml() def copy_config_files(self): for product in ['dse', 'cassandra', 'hadoop', 'hadoop2-client', 'sqoop', 'hive', 'tomcat', 'spark', 'shark', 'mahout', 'pig', 'solr', 'graph']: src_conf = os.path.join(self.get_install_dir(), 'resources', product, 'conf') dst_conf = os.path.join(self.get_path(), 'resources', product, 'conf') if not os.path.isdir(src_conf): continue if os.path.isdir(dst_conf): common.rmdirs(dst_conf) shutil.copytree(src_conf, dst_conf) if product == 'solr': src_web = os.path.join(self.get_install_dir(), 'resources', product, 'web') dst_web = os.path.join(self.get_path(), 'resources', product, 'web') if os.path.isdir(dst_web): common.rmdirs(dst_web) shutil.copytree(src_web, dst_web) if product == 'tomcat': src_lib = os.path.join(self.get_install_dir(), 'resources', product, 'lib') dst_lib = os.path.join(self.get_path(), 'resources', product, 'lib') if os.path.isdir(dst_lib): common.rmdirs(dst_lib) if os.path.exists(src_lib): shutil.copytree(src_lib, dst_lib) src_webapps = os.path.join(self.get_install_dir(), 'resources', product, 'webapps') dst_webapps = os.path.join(self.get_path(), 'resources', product, 'webapps') if os.path.isdir(dst_webapps): common.rmdirs(dst_webapps) shutil.copytree(src_webapps, dst_webapps) src_lib = os.path.join(self.get_install_dir(), 'resources', product, 'gremlin-console', 'conf') dst_lib = os.path.join(self.get_path(), 'resources', product, 'gremlin-console', 'conf') if os.path.isdir(dst_lib): common.rmdirs(dst_lib) if os.path.exists(src_lib): shutil.copytree(src_lib, dst_lib) def import_bin_files(self): common.copy_directory(os.path.join(self.get_install_dir(), 'bin'), self.get_bin_dir()) cassandra_bin_dir = os.path.join(self.get_path(), 'resources', 'cassandra', 'bin') shutil.rmtree(cassandra_bin_dir, ignore_errors=True) os.makedirs(cassandra_bin_dir) common.copy_directory(os.path.join(self.get_install_dir(), 'resources', 'cassandra', 'bin'), cassandra_bin_dir) if os.path.exists(os.path.join(self.get_install_dir(), 'resources', 'cassandra', 'tools')): cassandra_tools_dir = os.path.join(self.get_path(), 'resources', 'cassandra', 'tools') shutil.rmtree(cassandra_tools_dir, ignore_errors=True) shutil.copytree(os.path.join(self.get_install_dir(), 'resources', 'cassandra', 'tools'), cassandra_tools_dir) self.export_dse_home_in_dse_env_sh() def export_dse_home_in_dse_env_sh(self): ''' Due to the way CCM lays out files, separating the repository from the node(s) confs, the `dse-env.sh` script of each node needs to have its DSE_HOME var set and exported. Since DSE 4.5.x, the stock `dse-env.sh` file includes a commented-out place to do exactly this, intended for installers. Basically: read in the file, write it back out and add the two lines. 'sstableloader' is an example of a node script that depends on this, when used in a CCM-built cluster. ''' with open(self.get_bin_dir() + "/dse-env.sh", "r") as dse_env_sh: buf = dse_env_sh.readlines() with open(self.get_bin_dir() + "/dse-env.sh", "w") as out_file: for line in buf: out_file.write(line) if line == "# This is here so the installer can force set DSE_HOME\n": out_file.write("DSE_HOME=" + self.get_install_dir() + "\nexport DSE_HOME\n") def _update_log4j(self): super(DseNode, self)._update_log4j() conf_file = os.path.join(self.get_conf_dir(), common.LOG4J_CONF) append_pattern = 'log4j.appender.V.File=' log_file = os.path.join(self.get_path(), 'logs', 'solrvalidation.log') if common.is_win(): log_file = re.sub("\\\\", "/", log_file) common.replace_in_file(conf_file, append_pattern, append_pattern + log_file) append_pattern = 'log4j.appender.A.File=' log_file = os.path.join(self.get_path(), 'logs', 'audit.log') if common.is_win(): log_file = re.sub("\\\\", "/", log_file) common.replace_in_file(conf_file, append_pattern, append_pattern + log_file) append_pattern = 'log4j.appender.B.File=' log_file = os.path.join(self.get_path(), 'logs', 'audit', 'dropped-events.log') if common.is_win(): log_file = re.sub("\\\\", "/", log_file) common.replace_in_file(conf_file, append_pattern, append_pattern + log_file) def __update_yaml(self): conf_file = os.path.join(self.get_path(), 'resources', 'dse', 'conf', 'dse.yaml') with open(conf_file, 'r') as f: data = yaml.load(f) data['system_key_directory'] = os.path.join(self.get_path(), 'keys') # Get a map of combined cluster and node configuration with the node # configuration taking precedence. full_options = common.merge_configuration( self.cluster._dse_config_options, self._dse_config_options, delete_empty=False) # Merge options with original yaml data. data = common.merge_configuration(data, full_options) with open(conf_file, 'w') as f: yaml.safe_dump(data, f, default_flow_style=False) def __generate_server_xml(self): server_xml = os.path.join(self.get_path(), 'resources', 'tomcat', 'conf', 'server.xml') if os.path.isfile(server_xml): os.remove(server_xml) with open(server_xml, 'w+') as f: f.write('<Server port="8005" shutdown="SHUTDOWN">\n') f.write(' <Service name="Solr">\n') f.write(' <Connector port="8983" address="%s" protocol="HTTP/1.1" connectionTimeout="20000" maxThreads = "200" URIEncoding="UTF-8"/>\n' % self.network_interfaces['thrift'][0]) f.write(' <Engine name="Solr" defaultHost="localhost">\n') f.write(' <Host name="localhost" appBase="../solr/web"\n') f.write(' unpackWARs="true" autoDeploy="true"\n') f.write(' xmlValidation="false" xmlNamespaceAware="false">\n') f.write(' </Host>\n') f.write(' </Engine>\n') f.write(' </Service>\n') f.write('</Server>\n') f.close() def __update_gremlin_config_yaml(self): (node_ip, _) = self.network_interfaces['binary'] conf_file = os.path.join(self.get_path(), 'resources', 'graph', 'gremlin-console', 'conf', 'remote.yaml') with open(conf_file, 'r') as f: data = yaml.load(f) data['hosts'] = [node_ip] with open(conf_file, 'w') as f: yaml.safe_dump(data, f, default_flow_style=False) def _get_directories(self): dirs = [] for i in ['data', 'commitlogs', 'saved_caches', 'logs', 'bin', 'keys', 'resources', os.path.join('data', 'hints')]: dirs.append(os.path.join(self.get_path(), i)) return dirs def _copy_agent(self): agent_source = os.path.join(self.get_install_dir(), 'datastax-agent') agent_target = os.path.join(self.get_path(), 'datastax-agent') if os.path.exists(agent_source) and not os.path.exists(agent_target): shutil.copytree(agent_source, agent_target) def _write_agent_address_yaml(self, agent_dir): address_yaml = os.path.join(agent_dir, 'conf', 'address.yaml') if not os.path.exists(address_yaml): with open(address_yaml, 'w+') as f: (ip, port) = self.network_interfaces['thrift'] jmx = self.jmx_port f.write('stomp_interface: 127.0.0.1\n') f.write('local_interface: %s\n' % ip) f.write('agent_rpc_interface: %s\n' % ip) f.write('agent_rpc_broadcast_address: %s\n' % ip) f.write('cassandra_conf: %s\n' % os.path.join(self.get_path(), 'resources', 'cassandra', 'conf', 'cassandra.yaml')) f.write('cassandra_install: %s\n' % self.get_path()) f.write('cassandra_logs: %s\n' % os.path.join(self.get_path(), 'logs')) f.write('thrift_port: %s\n' % port) f.write('jmx_port: %s\n' % jmx) f.close() def _write_agent_log4j_properties(self, agent_dir): log4j_properties = os.path.join(agent_dir, 'conf', 'log4j.properties') with open(log4j_properties, 'w+') as f: f.write('log4j.rootLogger=INFO,R\n') f.write('log4j.logger.org.apache.http=OFF\n') f.write('log4j.logger.org.eclipse.jetty.util.log=WARN,R\n') f.write('log4j.appender.R=org.apache.log4j.RollingFileAppender\n') f.write('log4j.appender.R.maxFileSize=20MB\n') f.write('log4j.appender.R.maxBackupIndex=5\n') f.write('log4j.appender.R.layout=org.apache.log4j.PatternLayout\n') f.write('log4j.appender.R.layout.ConversionPattern=%5p [%t] %d{ISO8601} %m%n\n') f.write('log4j.appender.R.File=./log/agent.log\n') f.close() def _update_spark_env(self): try: node_num = re.search(u'node(\d+)', self.name).group(1) except AttributeError: node_num = 0 conf_file = os.path.join(self.get_path(), 'resources', 'spark', 'conf', 'spark-env.sh') env = self.get_env() content = [] with open(conf_file, 'r') as f: for line in f.readlines(): for spark_var in env.keys(): if line.startswith('export %s=' % spark_var) or line.startswith('export %s=' % spark_var, 2): line = 'export %s=%s\n' % (spark_var, env[spark_var]) break content.append(line) with open(conf_file, 'w') as f: f.writelines(content) # set unique spark.shuffle.service.port for each node; this is only needed for DSE 5.0.x; # starting in 5.1 this setting is no longer needed if self.cluster.version() > '5.0' and self.cluster.version() < '5.1': defaults_file = os.path.join(self.get_path(), 'resources', 'spark', 'conf', 'spark-defaults.conf') with open(defaults_file, 'a') as f: port_num = 7737 + int(node_num) f.write("\nspark.shuffle.service.port %s\n" % port_num) # create Spark working dirs; starting with DSE 5.0.10/5.1.3 these are no longer automatically created for e in ["SPARK_WORKER_DIR", "SPARK_LOCAL_DIRS"]: dir = env[e] if not os.path.exists(dir): os.makedirs(dir)
apache-2.0
Microvellum/Fluid-Designer
win64-vc/2.78/Python/bin/2.78/scripts/addons_contrib/mesh_extra_tools/pkhg_faces.py
1
32230
bl_info = { "name": "PKHG faces", "author": " PKHG ", "version": (0, 0, 5), "blender": (2, 7, 1), "location": "View3D > Tools > PKHG (tab)", "description": "Faces selected will become added faces of different style", "warning": "not yet finished", "wiki_url": "", "category": "Mesh", } import bpy import bmesh from mathutils import Vector, Matrix from bpy.props import BoolProperty, StringProperty, IntProperty, FloatProperty, EnumProperty class AddFaces(bpy.types.Operator): """Get parameters and build object with added faces""" bl_idname = "mesh.add_faces_to_object" bl_label = "new FACES: add" bl_options = {'REGISTER', 'UNDO', 'PRESET'} reverse_faces = BoolProperty(name="reverse_faces", default=False, description="revert the normal of selected faces") name_source_object = StringProperty( name="which MESH", description="lets you chose a mesh", default="Cube") remove_start_faces = BoolProperty(name="remove_start_faces", default=True, description="make a choice, remove or not") base_height = FloatProperty(name="base_height faces", min=-20, soft_max=10, max=20, default=0.2, description="sets general base_height") use_relative_base_height = BoolProperty(name="rel.base_height", default=False, description=" reletive or absolute base_height") relative_base_height = FloatProperty(name="relative_height", min=-5, soft_max=5, max=20, default=0.2, description="PKHG>TODO") relative_width = FloatProperty(name="relative_width", min=-5, soft_max=5, max=20, default=0.2, description="PKHG>TODO") second_height = FloatProperty(name="2. height", min=-5, soft_max=5, max=20, default=0.2, description="2. height for this and that") width = FloatProperty(name="wds.faces", min=-20, max=20, default=0.5, description="sets general width") repeat_extrude = IntProperty(name="repeat", min=1, soft_max=5, max=20, description="for longer base") move_inside = FloatProperty(name="move inside", min=0.0, max=1.0, default=0.5, description="how much move to inside") thickness = FloatProperty(name="thickness", soft_min=0.01, min=0, soft_max=5.0, max=20.0, default=0) depth = FloatProperty(name="depth", min=-5, soft_max=5.0, max=20.0, default=0) collapse_edges = BoolProperty(name="make point", default=False, description="collapse vertices of edges") spike_base_width = FloatProperty(name="spike_base_width", default=0.4, min=-4.0, soft_max=1, max=20, description="base width of a spike") base_height_inset = FloatProperty(name="base_height_inset", default=0.0, min=-5, max=5, description="to elevate/or neg the ...") top_spike = FloatProperty(name="top_spike", default=1.0, min=-10.0, max=10.0, description=" the base_height of a spike") top_extra_height = FloatProperty(name="top_extra_height", default=0.0, min=-10.0, max=10.0, description=" add extra height") step_with_real_spike = BoolProperty(name="step_with_real_spike", default=False, description=" in stepped a real spike") use_relative = BoolProperty(name="use_relative", default=False, description="change size using area, min of max") face_types = EnumProperty( description="different types of faces", default="no", items=[ ('no', 'choose!', 'choose one of the other possibilies'), ('open inset', 'open inset', 'holes'), ('with base', 'with base', 'base and ...'), ('clsd vertical', 'clsd vertical', 'clsd vertical'), ('open vertical', 'open vertical', 'openvertical'), ('spiked', 'spiked', 'spike'), ('stepped', 'stepped', 'stepped'), ('boxed', 'boxed', 'boxed'), ('bar', 'bar', 'bar'), ]) strange_boxed_effect = BoolProperty(name="strange effect", default=False, description="do not show one extrusion") use_boundary = BoolProperty(name="use_boundary", default=True) use_even_offset = BoolProperty(name="even_offset", default=True) use_relative_offset = BoolProperty(name="relativ_offset", default=True) use_edge_rail = BoolProperty(name="edge_rail", default=False) use_outset = BoolProperty(name="outset", default=False) use_select_inset = BoolProperty(name="inset", default=False) use_interpolate = BoolProperty(name="interpolate", default=True) @classmethod def poll(cls, context): result = False active_object = context.active_object if active_object: mesh_objects_name = [el.name for el in bpy.data.objects if el.type == "MESH"] if active_object.name in mesh_objects_name: result = True return result def draw(self, context): # PKHG>INFO Add_Faces_To_Object operator GUI layout = self.layout col = layout.column() col.label(text="ACTIVE object used!") col.prop(self, "face_types") col.prop(self, "use_relative") if self.face_types == "open inset": col.prop(self, "move_inside") col.prop(self, "base_height") elif self.face_types == "with base": col.prop(self, "move_inside") col.prop(self, "base_height") col.prop(self, "second_height") col.prop(self, "width") elif self.face_types == "clsd vertical": col.prop(self, "base_height") elif self.face_types == "open vertical": col.prop(self, "base_height") elif self.face_types == "boxed": col.prop(self, "move_inside") col.prop(self, "base_height") col.prop(self, "top_spike") col.prop(self, "strange_boxed_effect") elif self.face_types == "spiked": col.prop(self, "spike_base_width") col.prop(self, "base_height_inset") col.prop(self, "top_spike") elif self.face_types == "bar": col.prop(self, "spike_base_width") col.prop(self, "top_spike") col.prop(self, "top_extra_height") elif self.face_types == "stepped": col.prop(self, "spike_base_width") col.prop(self, "base_height_inset") col.prop(self, "top_extra_height") col.prop(self, "second_height") col.prop(self, "step_with_real_spike") def execute(self, context): bpy.context.scene.objects.active obj_name = self.name_source_object face_type = self.face_types if face_type == "spiked": Spiked(spike_base_width=self.spike_base_width, base_height_inset=self.base_height_inset, top_spike=self.top_spike, top_relative=self.use_relative) elif face_type == "boxed": startinfo = prepare(self, context, self.remove_start_faces) bm = startinfo['bm'] top = self.top_spike obj = startinfo['obj'] obj_matrix_local = obj.matrix_local distance = None base_heights = None t = self.move_inside areas = startinfo['areas'] base_height = self.base_height if self.use_relative: distance = [min(t * area, 1.0) for i, area in enumerate(areas)] base_heights = [base_height * area for i, area in enumerate(areas)] else: distance = [t] * len(areas) base_heights = [base_height] * len(areas) rings = startinfo['rings'] centers = startinfo['centers'] normals = startinfo['normals'] for i in range(len(rings)): make_one_inset(self, context, bm=bm, ringvectors=rings[i], center=centers[i], normal=normals[i], t=distance[i], base_height=base_heights[i]) bpy.ops.mesh.select_mode(type="EDGE") bpy.ops.mesh.select_more() bpy.ops.mesh.select_more() bpy.ops.object.mode_set(mode='OBJECT') # PKHG>INFO base extrusion done and set to the mesh # PKHG>INFO if the extrusion is NOT done ... it looks straneg soon! if not self.strange_boxed_effect: bpy.ops.object.mode_set(mode='EDIT') obj = context.active_object bm = bmesh.from_edit_mesh(obj.data) bmfaces = [face for face in bm.faces if face.select] res = extrude_faces(self, context, bm=bm, face_l=bmfaces) ring_edges = [face.edges[:] for face in res] bpy.ops.object.mode_set(mode='OBJECT') # PKHG>INFO now the extruded facec have to move in normal direction bpy.ops.object.mode_set(mode='EDIT') obj = bpy.context.scene.objects.active bm = bmesh.from_edit_mesh(obj.data) todo_faces = [face for face in bm.faces if face.select] for face in todo_faces: bmesh.ops.translate(bm, vec=face.normal * top, space=obj_matrix_local, verts=face.verts) bpy.ops.object.mode_set(mode='OBJECT') elif face_type == "stepped": Stepped(spike_base_width=self.spike_base_width, base_height_inset=self.base_height_inset, top_spike=self.second_height, top_extra_height=self.top_extra_height, use_relative_offset=self.use_relative, with_spike=self.step_with_real_spike) elif face_type == "open inset": startinfo = prepare(self, context, self.remove_start_faces) bm = startinfo['bm'] # PKHG>INFO adjust for relative, via areas t = self.move_inside areas = startinfo['areas'] base_height = self.base_height base_heights = None distance = None if self.use_relative: distance = [min(t * area, 1.0) for i, area in enumerate(areas)] base_heights = [base_height * area for i, area in enumerate(areas)] else: distance = [t] * len(areas) base_heights = [base_height] * len(areas) rings = startinfo['rings'] centers = startinfo['centers'] normals = startinfo['normals'] for i in range(len(rings)): make_one_inset(self, context, bm=bm, ringvectors=rings[i], center=centers[i], normal=normals[i], t=distance[i], base_height=base_heights[i]) bpy.ops.object.mode_set(mode='OBJECT') elif face_type == "with base": startinfo = prepare(self, context, self.remove_start_faces) bm = startinfo['bm'] obj = startinfo['obj'] object_matrix = obj.matrix_local # PKHG>INFO for relative (using areas) t = self.move_inside areas = startinfo['areas'] base_height = self.base_height distance = None base_heights = None if self.use_relative: distance = [min(t * area, 1.0) for i, area in enumerate(areas)] base_heights = [base_height * area for i, area in enumerate(areas)] else: distance = [t] * len(areas) base_heights = [base_height] * len(areas) next_rings = [] rings = startinfo['rings'] centers = startinfo['centers'] normals = startinfo['normals'] for i in range(len(rings)): next_rings.append(make_one_inset(self, context, bm=bm, ringvectors=rings[i], center=centers[i], normal=normals[i], t=distance[i], base_height=base_heights[i])) prepare_ring = extrude_edges(self, context, bm=bm, edge_l_l=next_rings) second_height = self.second_height width = self.width vectors = [[ele.verts[:] for ele in edge] for edge in prepare_ring] n_ring_vecs = [] for rings in vectors: v = [] for edgv in rings: v.extend(edgv) # PKHF>INFO no double verts allowed, coming from two adjacents edges! bm.verts.ensure_lookup_table() vv = list(set([ele.index for ele in v])) vvv = [bm.verts[i].co for i in vv] n_ring_vecs.append(vvv) for i, ring in enumerate(n_ring_vecs): make_one_inset(self, context, bm=bm, ringvectors=ring, center=centers[i], normal=normals[i], t=width, base_height=base_heights[i] + second_height) bpy.ops.object.mode_set(mode='OBJECT') else: if face_type == "clsd vertical": obj_name = context.active_object.name ClosedVertical(name=obj_name, base_height=self.base_height, use_relative_base_height=self.use_relative) elif face_type == "open vertical": obj_name = context.active_object.name OpenVertical(name=obj_name, base_height=self.base_height, use_relative_base_height=self.use_relative) elif face_type == "bar": startinfo = prepare(self, context, self.remove_start_faces) result = [] bm = startinfo['bm'] rings = startinfo['rings'] centers = startinfo['centers'] normals = startinfo['normals'] spike_base_width = self.spike_base_width for i, ring in enumerate(rings): result.append(make_one_inset(self, context, bm=bm, ringvectors=ring, center=centers[i], normal=normals[i], t=spike_base_width)) next_ring_edges_list = extrude_edges(self, context, bm=bm, edge_l_l=result) top_spike = self.top_spike fac = top_spike object_matrix = startinfo['obj'].matrix_local for i in range(len(next_ring_edges_list)): translate_ONE_ring(self, context, bm=bm, object_matrix=object_matrix, ring_edges=next_ring_edges_list[i], normal=normals[i], distance=fac) next_ring_edges_list_2 = extrude_edges(self, context, bm=bm, edge_l_l=next_ring_edges_list) top_extra_height = self.top_extra_height for i in range(len(next_ring_edges_list_2)): move_corner_vecs_outside(self, context, bm=bm, edge_list=next_ring_edges_list_2[i], center=centers[i], normal=normals[i], base_height_erlier=fac + top_extra_height, distance=fac) bpy.ops.mesh.select_mode(type="VERT") bpy.ops.mesh.select_more() bpy.ops.object.mode_set(mode='OBJECT') return {'FINISHED'} class ReverseFacesOperator(bpy.types.Operator): """Reverse selected Faces""" bl_idname = "mesh.revers_selected_faces" bl_label = "reverse normal of selected faces1" bl_options = {'REGISTER', 'UNDO', 'PRESET'} reverse_faces = BoolProperty(name="reverse_faces", default=False, description="revert the normal of selected faces") def execute(self, context): name = context.active_object.name ReverseFaces(name=name) return {'FINISHED'} class pkhg_help(bpy.types.Operator): bl_idname = 'help.pkhg' bl_label = '' def draw(self, context): layout = self.layout layout.label('To use:') layout.label('Make a selection or selection of Faces') layout.label('Extrude, rotate extrusions & more') layout.label('Toggle edit mode after use') def invoke(self, context, event): return context.window_manager.invoke_popup(self, width=300) class VIEW3D_Faces_Panel(bpy.types.Panel): bl_label = "Face Extrude" bl_space_type = "VIEW_3D" bl_region_type = "TOOLS" bl_category = 'Tools' bl_options = {'DEFAULT_CLOSED'} @classmethod def poll(cls, context): result = False active_object = context.active_object if active_object: mesh_objects_name = [el.name for el in bpy.data.objects if el.type == "MESH"] if active_object.name in mesh_objects_name: if active_object.mode == "OBJECT": result = True return result def draw(self, context): layout = self.layout layout.operator(AddFaces.bl_idname, "Selected Faces!") layout.label("Use this to Extrude") layout.label("Selected Faces Only") layout.label("---------------------------------------") layout.operator(ReverseFacesOperator.bl_idname, "Reverse faceNormals") layout.label("Only Use This") layout.label("After Mesh Creation") layout.label("To Repair Normals") layout.label("Save File Often") def find_one_ring(sel_vertices): ring0 = sel_vertices.pop(0) to_delete = [] for i, edge in enumerate(sel_vertices): len_nu = len(ring0) if len(ring0 - edge) < len_nu: to_delete.append(i) ring0 = ring0.union(edge) to_delete.reverse() for el in to_delete: sel_vertices.pop(el) return (ring0, sel_vertices) class Stepped: def __init__(self, spike_base_width=0.5, base_height_inset=0.0, top_spike=0.2, top_relative=False, top_extra_height=0, use_relative_offset=False, with_spike=False): obj = bpy.context.active_object bpy.ops.object.mode_set(mode='EDIT') bpy.ops.mesh.inset(use_boundary=True, use_even_offset=True, use_relative_offset=False, use_edge_rail=False, thickness=spike_base_width, depth=0, use_outset=True, use_select_inset=False, use_individual=True, use_interpolate=True) bpy.ops.mesh.inset(use_boundary=True, use_even_offset=True, use_relative_offset=use_relative_offset, use_edge_rail=False, thickness=top_extra_height, depth=base_height_inset, use_outset=True, use_select_inset=False, use_individual=True, use_interpolate=True) bpy.ops.mesh.inset(use_boundary=True, use_even_offset=True, use_relative_offset=use_relative_offset, use_edge_rail=False, thickness=spike_base_width, depth=0, use_outset=True, use_select_inset=False, use_individual=True, use_interpolate=True) bpy.ops.mesh.inset(use_boundary=True, use_even_offset=True, use_relative_offset=False, use_edge_rail=False, thickness=0, depth=top_spike, use_outset=True, use_select_inset=False, use_individual=True, use_interpolate=True) if with_spike: bpy.ops.mesh.merge(type='COLLAPSE') bpy.ops.object.mode_set(mode='OBJECT') class Spiked: def __init__(self, spike_base_width=0.5, base_height_inset=0.0, top_spike=0.2, top_relative=False): obj = bpy.context.active_object bpy.ops.object.mode_set(mode='EDIT') bpy.ops.mesh.inset(use_boundary=True, use_even_offset=True, use_relative_offset=False, use_edge_rail=False, thickness=spike_base_width, depth=base_height_inset, use_outset=True, use_select_inset=False, use_individual=True, use_interpolate=True) bpy.ops.mesh.inset(use_boundary=True, use_even_offset=True, use_relative_offset=top_relative, use_edge_rail=False, thickness=0, depth=top_spike, use_outset=True, use_select_inset=False, use_individual=True, use_interpolate=True) bm = bmesh.from_edit_mesh(obj.data) selected_faces = [face for face in bm.faces if face.select] edges_todo = [] bpy.ops.mesh.merge(type='COLLAPSE') bpy.ops.object.mode_set(mode='OBJECT') class ClosedVertical: def __init__(self, name="Plane", base_height=1, use_relative_base_height=False): obj = bpy.data.objects[name] bm = bmesh.new() bm.from_mesh(obj.data) # PKHG>INFO deselect chosen faces sel = [f for f in bm.faces if f.select] for f in sel: f.select = False res = bmesh.ops.extrude_discrete_faces(bm, faces=sel) # PKHG>INFO select extruded faces for f in res['faces']: f.select = True lood = Vector((0, 0, 1)) # PKHG>INFO adjust extrusion by a vector! test just only lood factor = base_height for face in res['faces']: if use_relative_base_height: area = face.calc_area() factor = area * base_height else: factor = base_height for el in face.verts: tmp = el.co + face.normal * factor el.co = tmp me = bpy.data.meshes[name] bm.to_mesh(me) bm.free() class ReverseFaces: def __init__(self, name="Cube"): obj = bpy.data.objects[name] me = obj.data bpy.ops.object.mode_set(mode='EDIT') bm = bmesh.new() bm.from_mesh(me) bpy.ops.object.mode_set(mode='OBJECT') sel = [f for f in bm.faces if f.select] bmesh.ops.reverse_faces(bm, faces=sel) bm.to_mesh(me) bm.free() class OpenVertical: def __init__(self, name="Plane", base_height=1, use_relative_base_height=False): obj = bpy.data.objects[name] bm = bmesh.new() bm.from_mesh(obj.data) # PKHG>INFO deselect chosen faces sel = [f for f in bm.faces if f.select] for f in sel: f.select = False res = bmesh.ops.extrude_discrete_faces(bm, faces=sel) # PKHG>INFO select extruded faces for f in res['faces']: f.select = True # PKHG>INFO adjust extrusion by a vector! test just only lood factor = base_height for face in res['faces']: if use_relative_base_height: area = face.calc_area() factor = area * base_height else: factor = base_height for el in face.verts: tmp = el.co + face.normal * factor el.co = tmp me = bpy.data.meshes[name] bm.to_mesh(me) bm.free() bpy.ops.object.editmode_toggle() bpy.ops.mesh.delete(type='FACE') bpy.ops.object.editmode_toggle() class StripFaces: def __init__(self, use_boundary=True, use_even_offset=True, use_relative_offset=False, use_edge_rail=True, thickness=0.0, depth=0.0, use_outset=False, use_select_inset=False, use_individual=True, use_interpolate=True): bpy.ops.object.mode_set(mode='EDIT') bpy.ops.mesh.inset(use_boundary=use_boundary, use_even_offset=True, use_relative_offset=False, use_edge_rail=True, thickness=thickness, depth=depth, use_outset=use_outset, use_select_inset=use_select_inset, use_individual=use_individual, use_interpolate=use_interpolate) bpy.ops.object.mode_set(mode='OBJECT') # PKHG>IMFO only 3 parameters inc execution context supported!! if False: bpy.ops.mesh.inset(use_boundary, use_even_offset, use_relative_offset, use_edge_rail, thickness, depth, use_outset, use_select_inset, use_individual, use_interpolate) elif type == 0: bpy.ops.mesh.inset(use_boundary=True, use_even_offset=True, use_relative_offset=False, use_edge_rail=True, thickness=thickness, depth=depth, use_outset=False, use_select_inset=False, use_individual=True, use_interpolate=True) elif type == 1: bpy.ops.mesh.inset(use_boundary=True, use_even_offset=True, use_relative_offset=False, use_edge_rail=True, thickness=thickness, depth=depth, use_outset=False, use_select_inset=False, use_individual=True, use_interpolate=False) bpy.ops.mesh.delete(type='FACE') elif type == 2: bpy.ops.mesh.inset(use_boundary=True, use_even_offset=False, use_relative_offset=True, use_edge_rail=True, thickness=thickness, depth=depth, use_outset=False, use_select_inset=False, use_individual=True, use_interpolate=False) bpy.ops.mesh.delete(type='FACE') elif type == 3: bpy.ops.mesh.inset(use_boundary=True, use_even_offset=False, use_relative_offset=True, use_edge_rail=True, thickness=depth, depth=thickness, use_outset=False, use_select_inset=False, use_individual=True, use_interpolate=True) bpy.ops.mesh.delete(type='FACE') elif type == 4: bpy.ops.mesh.inset(use_boundary=True, use_even_offset=False, use_relative_offset=True, use_edge_rail=True, thickness=thickness, depth=depth, use_outset=True, use_select_inset=False, use_individual=True, use_interpolate=True) bpy.ops.mesh.inset(use_boundary=True, use_even_offset=False, use_relative_offset=True, use_edge_rail=True, thickness=thickness, depth=depth, use_outset=True, use_select_inset=False, use_individual=True, use_interpolate=True) bpy.ops.mesh.delete(type='FACE') bpy.ops.object.mode_set(mode='OBJECT') def prepare(self, context, remove_start_faces=True): """Start for a face selected change of faces select an object of type mesh, with activated severel (all) faces """ obj = bpy.context.scene.objects.active bpy.ops.object.mode_set(mode='OBJECT') selectedpolygons = [el for el in obj.data.polygons if el.select] # PKHG>INFO copies of the vectors are needed, otherwise Blender crashes! centers = [face.center for face in selectedpolygons] centers_copy = [Vector((el[0], el[1], el[2])) for el in centers] normals = [face.normal for face in selectedpolygons] normals_copy = [Vector((el[0], el[1], el[2])) for el in normals] vertindicesofpolgons = [[vert for vert in face.vertices] for face in selectedpolygons] vertVectorsOfSelectedFaces = [[obj.data.vertices[ind].co for ind in vertIndiceofface] for vertIndiceofface in vertindicesofpolgons] vertVectorsOfSelectedFaces_copy = [[Vector((el[0], el[1], el[2])) for el in listofvecs] for listofvecs in vertVectorsOfSelectedFaces] bpy.ops.object.mode_set(mode='EDIT') bm = bmesh.from_edit_mesh(obj.data) selected_bm_faces = [ele for ele in bm.faces if ele.select] selected_edges_per_face_ind = [[ele.index for ele in face.edges] for face in selected_bm_faces] indices = [el.index for el in selectedpolygons] selected_faces_areas = [bm.faces[:][i] for i in indices] tmp_area = [el.calc_area() for el in selected_faces_areas] # PKHG>INFO, selected faces are removed, only their edges are used! if remove_start_faces: bpy.ops.mesh.delete(type='ONLY_FACE') bpy.ops.object.mode_set(mode='OBJECT') obj.data.update() bpy.ops.object.mode_set(mode='EDIT') bm = bmesh.from_edit_mesh(obj.data) bm.verts.ensure_lookup_table() bm.faces.ensure_lookup_table() start_ring_raw = [[bm.verts[ind].index for ind in vertIndiceofface] for vertIndiceofface in vertindicesofpolgons] start_ring = [] for el in start_ring_raw: start_ring.append(set(el)) bm.edges.ensure_lookup_table() bm_selected_edges_l_l = [[bm.edges[i] for i in bm_ind_list] for bm_ind_list in selected_edges_per_face_ind] result = {'obj': obj, 'centers': centers_copy, 'normals': normals_copy, 'rings': vertVectorsOfSelectedFaces_copy, 'bm': bm, 'areas': tmp_area, 'startBMRingVerts': start_ring, 'base_edges': bm_selected_edges_l_l} return result def make_one_inset(self, context, bm=None, ringvectors=None, center=None, normal=None, t=None, base_height=0): """a face will get 'inserted' faces to create (normaly) a hole it t is > 0 and < 1) """ tmp = [] for el in ringvectors: tmp.append((el * (1 - t) + center * t) + normal * base_height) tmp = [bm.verts.new(v) for v in tmp] # the new corner bmvectors # PKHG>INFO so to say sentinells, ot use ONE for ... tmp.append(tmp[0]) vectorsFace_i = [bm.verts.new(v) for v in ringvectors] vectorsFace_i.append(vectorsFace_i[0]) myres = [] for ii in range(len(vectorsFace_i) - 1): # PKHG>INFO next line: sequence important! for added edge bmvecs = [vectorsFace_i[ii], vectorsFace_i[ii + 1], tmp[ii + 1], tmp[ii]] res = bm.faces.new(bmvecs) myres.append(res.edges[2]) myres[-1].select = True # PKHG>INFO to be used later selected! return (myres) def extrude_faces(self, context, bm=None, face_l=None): """ to make a ring extrusion! """ all_results = [] res = bmesh.ops.extrude_discrete_faces(bm, faces=face_l)['faces'] for face in res: face.select = True return res def extrude_edges(self, context, bm=None, edge_l_l=None): """ to make a ring extrusion! """ all_results = [] for edge_l in edge_l_l: for edge in edge_l: edge.select = False res = bmesh.ops.extrude_edge_only(bm, edges=edge_l) tmp = [ele for ele in res['geom'] if isinstance(ele, bmesh.types.BMEdge)] for edge in tmp: edge.select = True all_results.append(tmp) return all_results def translate_ONE_ring(self, context, bm=None, object_matrix=None, ring_edges=None, normal=(0, 0, 1), distance=0.5): """ translate a ring in given (normal?!) direction with given (global) amount """ tmp = [] for edge in ring_edges: tmp.extend(edge.verts[:]) # PKHG>INFO no double vertices allowed by bmesh! tmp = set(tmp) tmp = list(tmp) bmesh.ops.translate(bm, vec=normal * distance, space=object_matrix, verts=tmp) return ring_edges # PKHG>INFO relevant edges will stay selected def move_corner_vecs_outside(self, context, bm=None, edge_list=None, center=None, normal=None, base_height_erlier=0.5, distance=0.5): """ move corners (outside meant mostly) dependent on the parameters """ tmp = [] for edge in edge_list: tmp.extend([ele for ele in edge.verts if isinstance(ele, bmesh.types.BMVert)]) # PKHG>INFO to remove vertices, they are all twices used in the ring! tmp = set(tmp) tmp = list(tmp) for i in range(len(tmp)): vec = tmp[i].co direction = vec + (vec - (normal * base_height_erlier + center)) * distance tmp[i].co = direction def register(): bpy.utils.register_module(__name__) def unregister(): bpy.utils.unregister_module(__name__) if __name__ == "__main__": register()
gpl-3.0
hackersql/sq1map
plugins/dbms/mssqlserver/__init__.py
3
1081
#!/usr/bin/env python """ Copyright (c) 2006-2017 sqlmap developers (http://sqlmap.org/) See the file 'doc/COPYING' for copying permission """ from lib.core.enums import DBMS from lib.core.settings import MSSQL_SYSTEM_DBS from lib.core.unescaper import unescaper from plugins.dbms.mssqlserver.enumeration import Enumeration from plugins.dbms.mssqlserver.filesystem import Filesystem from plugins.dbms.mssqlserver.fingerprint import Fingerprint from plugins.dbms.mssqlserver.syntax import Syntax from plugins.dbms.mssqlserver.takeover import Takeover from plugins.generic.misc import Miscellaneous class MSSQLServerMap(Syntax, Fingerprint, Enumeration, Filesystem, Miscellaneous, Takeover): """ This class defines Microsoft SQL Server methods """ def __init__(self): self.excludeDbsList = MSSQL_SYSTEM_DBS Syntax.__init__(self) Fingerprint.__init__(self) Enumeration.__init__(self) Filesystem.__init__(self) Miscellaneous.__init__(self) Takeover.__init__(self) unescaper[DBMS.MSSQL] = Syntax.escape
gpl-3.0
enriclluelles/ansible-modules-extras
network/openvswitch_bridge.py
8
3925
#!/usr/bin/python #coding: utf-8 -*- # (c) 2013, David Stygstra <david.stygstra@gmail.com> # # This file is part of Ansible # # This module is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This software is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this software. If not, see <http://www.gnu.org/licenses/>. DOCUMENTATION = ''' --- module: openvswitch_bridge version_added: 1.4 author: '"David Stygstra (@stygstra)" <david.stygstra@gmail.com>' short_description: Manage Open vSwitch bridges requirements: [ ovs-vsctl ] description: - Manage Open vSwitch bridges options: bridge: required: true description: - Name of bridge to manage state: required: false default: "present" choices: [ present, absent ] description: - Whether the bridge should exist timeout: required: false default: 5 description: - How long to wait for ovs-vswitchd to respond ''' EXAMPLES = ''' # Create a bridge named br-int - openvswitch_bridge: bridge=br-int state=present ''' class OVSBridge(object): def __init__(self, module): self.module = module self.bridge = module.params['bridge'] self.state = module.params['state'] self.timeout = module.params['timeout'] def _vsctl(self, command): '''Run ovs-vsctl command''' return self.module.run_command(['ovs-vsctl', '-t', str(self.timeout)] + command) def exists(self): '''Check if the bridge already exists''' rc, _, err = self._vsctl(['br-exists', self.bridge]) if rc == 0: # See ovs-vsctl(8) for status codes return True if rc == 2: return False raise Exception(err) def add(self): '''Create the bridge''' rc, _, err = self._vsctl(['add-br', self.bridge]) if rc != 0: raise Exception(err) def delete(self): '''Delete the bridge''' rc, _, err = self._vsctl(['del-br', self.bridge]) if rc != 0: raise Exception(err) def check(self): '''Run check mode''' try: if self.state == 'absent' and self.exists(): changed = True elif self.state == 'present' and not self.exists(): changed = True else: changed = False except Exception, e: self.module.fail_json(msg=str(e)) self.module.exit_json(changed=changed) def run(self): '''Make the necessary changes''' changed = False try: if self.state == 'absent': if self.exists(): self.delete() changed = True elif self.state == 'present': if not self.exists(): self.add() changed = True except Exception, e: self.module.fail_json(msg=str(e)) self.module.exit_json(changed=changed) def main(): module = AnsibleModule( argument_spec={ 'bridge': {'required': True}, 'state': {'default': 'present', 'choices': ['present', 'absent']}, 'timeout': {'default': 5, 'type': 'int'} }, supports_check_mode=True, ) br = OVSBridge(module) if module.check_mode: br.check() else: br.run() # import module snippets from ansible.module_utils.basic import * main()
gpl-3.0
ArcherSys/ArcherSys
Lib/test/test_socket.py
1
585437
<<<<<<< HEAD <<<<<<< HEAD import unittest from test import support import errno import io import itertools import socket import select import tempfile import time import traceback import queue import sys import os import array import platform import contextlib from weakref import proxy import signal import math import pickle import struct try: import multiprocessing except ImportError: multiprocessing = False try: import fcntl except ImportError: fcntl = None HOST = support.HOST MSG = 'Michael Gilfix was here\u1234\r\n'.encode('utf-8') ## test unicode string and carriage return try: import _thread as thread import threading except ImportError: thread = None threading = None try: import _socket except ImportError: _socket = None def _have_socket_can(): """Check whether CAN sockets are supported on this host.""" try: s = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) except (AttributeError, OSError): return False else: s.close() return True def _have_socket_rds(): """Check whether RDS sockets are supported on this host.""" try: s = socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) except (AttributeError, OSError): return False else: s.close() return True HAVE_SOCKET_CAN = _have_socket_can() HAVE_SOCKET_RDS = _have_socket_rds() # Size in bytes of the int type SIZEOF_INT = array.array("i").itemsize class SocketTCPTest(unittest.TestCase): def setUp(self): self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.port = support.bind_port(self.serv) self.serv.listen(1) def tearDown(self): self.serv.close() self.serv = None class SocketUDPTest(unittest.TestCase): def setUp(self): self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.port = support.bind_port(self.serv) def tearDown(self): self.serv.close() self.serv = None class ThreadSafeCleanupTestCase(unittest.TestCase): """Subclass of unittest.TestCase with thread-safe cleanup methods. This subclass protects the addCleanup() and doCleanups() methods with a recursive lock. """ if threading: def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._cleanup_lock = threading.RLock() def addCleanup(self, *args, **kwargs): with self._cleanup_lock: return super().addCleanup(*args, **kwargs) def doCleanups(self, *args, **kwargs): with self._cleanup_lock: return super().doCleanups(*args, **kwargs) class SocketCANTest(unittest.TestCase): """To be able to run this test, a `vcan0` CAN interface can be created with the following commands: # modprobe vcan # ip link add dev vcan0 type vcan # ifconfig vcan0 up """ interface = 'vcan0' bufsize = 128 """The CAN frame structure is defined in <linux/can.h>: struct can_frame { canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ __u8 can_dlc; /* data length code: 0 .. 8 */ __u8 data[8] __attribute__((aligned(8))); }; """ can_frame_fmt = "=IB3x8s" can_frame_size = struct.calcsize(can_frame_fmt) """The Broadcast Management Command frame structure is defined in <linux/can/bcm.h>: struct bcm_msg_head { __u32 opcode; __u32 flags; __u32 count; struct timeval ival1, ival2; canid_t can_id; __u32 nframes; struct can_frame frames[0]; } `bcm_msg_head` must be 8 bytes aligned because of the `frames` member (see `struct can_frame` definition). Must use native not standard types for packing. """ bcm_cmd_msg_fmt = "@3I4l2I" bcm_cmd_msg_fmt += "x" * (struct.calcsize(bcm_cmd_msg_fmt) % 8) def setUp(self): self.s = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) self.addCleanup(self.s.close) try: self.s.bind((self.interface,)) except OSError: self.skipTest('network interface `%s` does not exist' % self.interface) class SocketRDSTest(unittest.TestCase): """To be able to run this test, the `rds` kernel module must be loaded: # modprobe rds """ bufsize = 8192 def setUp(self): self.serv = socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) self.addCleanup(self.serv.close) try: self.port = support.bind_port(self.serv) except OSError: self.skipTest('unable to bind RDS socket') class ThreadableTest: """Threadable Test class The ThreadableTest class makes it easy to create a threaded client/server pair from an existing unit test. To create a new threaded class from an existing unit test, use multiple inheritance: class NewClass (OldClass, ThreadableTest): pass This class defines two new fixture functions with obvious purposes for overriding: clientSetUp () clientTearDown () Any new test functions within the class must then define tests in pairs, where the test name is preceeded with a '_' to indicate the client portion of the test. Ex: def testFoo(self): # Server portion def _testFoo(self): # Client portion Any exceptions raised by the clients during their tests are caught and transferred to the main thread to alert the testing framework. Note, the server setup function cannot call any blocking functions that rely on the client thread during setup, unless serverExplicitReady() is called just before the blocking call (such as in setting up a client/server connection and performing the accept() in setUp(). """ def __init__(self): # Swap the true setup function self.__setUp = self.setUp self.__tearDown = self.tearDown self.setUp = self._setUp self.tearDown = self._tearDown def serverExplicitReady(self): """This method allows the server to explicitly indicate that it wants the client thread to proceed. This is useful if the server is about to execute a blocking routine that is dependent upon the client thread during its setup routine.""" self.server_ready.set() def _setUp(self): self.server_ready = threading.Event() self.client_ready = threading.Event() self.done = threading.Event() self.queue = queue.Queue(1) self.server_crashed = False # Do some munging to start the client test. methodname = self.id() i = methodname.rfind('.') methodname = methodname[i+1:] test_method = getattr(self, '_' + methodname) self.client_thread = thread.start_new_thread( self.clientRun, (test_method,)) try: self.__setUp() except: self.server_crashed = True raise finally: self.server_ready.set() self.client_ready.wait() def _tearDown(self): self.__tearDown() self.done.wait() if self.queue.qsize(): exc = self.queue.get() raise exc def clientRun(self, test_func): self.server_ready.wait() self.clientSetUp() self.client_ready.set() if self.server_crashed: self.clientTearDown() return if not hasattr(test_func, '__call__'): raise TypeError("test_func must be a callable function") try: test_func() except BaseException as e: self.queue.put(e) finally: self.clientTearDown() def clientSetUp(self): raise NotImplementedError("clientSetUp must be implemented.") def clientTearDown(self): self.done.set() thread.exit() class ThreadedTCPSocketTest(SocketTCPTest, ThreadableTest): def __init__(self, methodName='runTest'): SocketTCPTest.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): self.cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) class ThreadedUDPSocketTest(SocketUDPTest, ThreadableTest): def __init__(self, methodName='runTest'): SocketUDPTest.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): self.cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) class ThreadedCANSocketTest(SocketCANTest, ThreadableTest): def __init__(self, methodName='runTest'): SocketCANTest.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): self.cli = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) try: self.cli.bind((self.interface,)) except OSError: # skipTest should not be called here, and will be called in the # server instead pass def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) class ThreadedRDSSocketTest(SocketRDSTest, ThreadableTest): def __init__(self, methodName='runTest'): SocketRDSTest.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): self.cli = socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) try: # RDS sockets must be bound explicitly to send or receive data self.cli.bind((HOST, 0)) self.cli_addr = self.cli.getsockname() except OSError: # skipTest should not be called here, and will be called in the # server instead pass def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) class SocketConnectedTest(ThreadedTCPSocketTest): """Socket tests for client-server connection. self.cli_conn is a client socket connected to the server. The setUp() method guarantees that it is connected to the server. """ def __init__(self, methodName='runTest'): ThreadedTCPSocketTest.__init__(self, methodName=methodName) def setUp(self): ThreadedTCPSocketTest.setUp(self) # Indicate explicitly we're ready for the client thread to # proceed and then perform the blocking call to accept self.serverExplicitReady() conn, addr = self.serv.accept() self.cli_conn = conn def tearDown(self): self.cli_conn.close() self.cli_conn = None ThreadedTCPSocketTest.tearDown(self) def clientSetUp(self): ThreadedTCPSocketTest.clientSetUp(self) self.cli.connect((HOST, self.port)) self.serv_conn = self.cli def clientTearDown(self): self.serv_conn.close() self.serv_conn = None ThreadedTCPSocketTest.clientTearDown(self) class SocketPairTest(unittest.TestCase, ThreadableTest): def __init__(self, methodName='runTest'): unittest.TestCase.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def setUp(self): self.serv, self.cli = socket.socketpair() def tearDown(self): self.serv.close() self.serv = None def clientSetUp(self): pass def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) # The following classes are used by the sendmsg()/recvmsg() tests. # Combining, for instance, ConnectedStreamTestMixin and TCPTestBase # gives a drop-in replacement for SocketConnectedTest, but different # address families can be used, and the attributes serv_addr and # cli_addr will be set to the addresses of the endpoints. class SocketTestBase(unittest.TestCase): """A base class for socket tests. Subclasses must provide methods newSocket() to return a new socket and bindSock(sock) to bind it to an unused address. Creates a socket self.serv and sets self.serv_addr to its address. """ def setUp(self): self.serv = self.newSocket() self.bindServer() def bindServer(self): """Bind server socket and set self.serv_addr to its address.""" self.bindSock(self.serv) self.serv_addr = self.serv.getsockname() def tearDown(self): self.serv.close() self.serv = None class SocketListeningTestMixin(SocketTestBase): """Mixin to listen on the server socket.""" def setUp(self): super().setUp() self.serv.listen(1) class ThreadedSocketTestMixin(ThreadSafeCleanupTestCase, SocketTestBase, ThreadableTest): """Mixin to add client socket and allow client/server tests. Client socket is self.cli and its address is self.cli_addr. See ThreadableTest for usage information. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) ThreadableTest.__init__(self) def clientSetUp(self): self.cli = self.newClientSocket() self.bindClient() def newClientSocket(self): """Return a new socket for use as client.""" return self.newSocket() def bindClient(self): """Bind client socket and set self.cli_addr to its address.""" self.bindSock(self.cli) self.cli_addr = self.cli.getsockname() def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) class ConnectedStreamTestMixin(SocketListeningTestMixin, ThreadedSocketTestMixin): """Mixin to allow client/server stream tests with connected client. Server's socket representing connection to client is self.cli_conn and client's connection to server is self.serv_conn. (Based on SocketConnectedTest.) """ def setUp(self): super().setUp() # Indicate explicitly we're ready for the client thread to # proceed and then perform the blocking call to accept self.serverExplicitReady() conn, addr = self.serv.accept() self.cli_conn = conn def tearDown(self): self.cli_conn.close() self.cli_conn = None super().tearDown() def clientSetUp(self): super().clientSetUp() self.cli.connect(self.serv_addr) self.serv_conn = self.cli def clientTearDown(self): self.serv_conn.close() self.serv_conn = None super().clientTearDown() class UnixSocketTestBase(SocketTestBase): """Base class for Unix-domain socket tests.""" # This class is used for file descriptor passing tests, so we # create the sockets in a private directory so that other users # can't send anything that might be problematic for a privileged # user running the tests. def setUp(self): self.dir_path = tempfile.mkdtemp() self.addCleanup(os.rmdir, self.dir_path) super().setUp() def bindSock(self, sock): path = tempfile.mktemp(dir=self.dir_path) sock.bind(path) self.addCleanup(support.unlink, path) class UnixStreamBase(UnixSocketTestBase): """Base class for Unix-domain SOCK_STREAM tests.""" def newSocket(self): return socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) class InetTestBase(SocketTestBase): """Base class for IPv4 socket tests.""" host = HOST def setUp(self): super().setUp() self.port = self.serv_addr[1] def bindSock(self, sock): support.bind_port(sock, host=self.host) class TCPTestBase(InetTestBase): """Base class for TCP-over-IPv4 tests.""" def newSocket(self): return socket.socket(socket.AF_INET, socket.SOCK_STREAM) class UDPTestBase(InetTestBase): """Base class for UDP-over-IPv4 tests.""" def newSocket(self): return socket.socket(socket.AF_INET, socket.SOCK_DGRAM) class SCTPStreamBase(InetTestBase): """Base class for SCTP tests in one-to-one (SOCK_STREAM) mode.""" def newSocket(self): return socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_SCTP) class Inet6TestBase(InetTestBase): """Base class for IPv6 socket tests.""" host = support.HOSTv6 class UDP6TestBase(Inet6TestBase): """Base class for UDP-over-IPv6 tests.""" def newSocket(self): return socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) # Test-skipping decorators for use with ThreadableTest. def skipWithClientIf(condition, reason): """Skip decorated test if condition is true, add client_skip decorator. If the decorated object is not a class, sets its attribute "client_skip" to a decorator which will return an empty function if the test is to be skipped, or the original function if it is not. This can be used to avoid running the client part of a skipped test when using ThreadableTest. """ def client_pass(*args, **kwargs): pass def skipdec(obj): retval = unittest.skip(reason)(obj) if not isinstance(obj, type): retval.client_skip = lambda f: client_pass return retval def noskipdec(obj): if not (isinstance(obj, type) or hasattr(obj, "client_skip")): obj.client_skip = lambda f: f return obj return skipdec if condition else noskipdec def requireAttrs(obj, *attributes): """Skip decorated test if obj is missing any of the given attributes. Sets client_skip attribute as skipWithClientIf() does. """ missing = [name for name in attributes if not hasattr(obj, name)] return skipWithClientIf( missing, "don't have " + ", ".join(name for name in missing)) def requireSocket(*args): """Skip decorated test if a socket cannot be created with given arguments. When an argument is given as a string, will use the value of that attribute of the socket module, or skip the test if it doesn't exist. Sets client_skip attribute as skipWithClientIf() does. """ err = None missing = [obj for obj in args if isinstance(obj, str) and not hasattr(socket, obj)] if missing: err = "don't have " + ", ".join(name for name in missing) else: callargs = [getattr(socket, obj) if isinstance(obj, str) else obj for obj in args] try: s = socket.socket(*callargs) except OSError as e: # XXX: check errno? err = str(e) else: s.close() return skipWithClientIf( err is not None, "can't create socket({0}): {1}".format( ", ".join(str(o) for o in args), err)) ####################################################################### ## Begin Tests class GeneralModuleTests(unittest.TestCase): def test_SocketType_is_socketobject(self): import _socket self.assertTrue(socket.SocketType is _socket.socket) s = socket.socket() self.assertIsInstance(s, socket.SocketType) s.close() def test_repr(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) with s: self.assertIn('fd=%i' % s.fileno(), repr(s)) self.assertIn('family=%s' % socket.AF_INET, repr(s)) self.assertIn('type=%s' % socket.SOCK_STREAM, repr(s)) self.assertIn('proto=0', repr(s)) self.assertNotIn('raddr', repr(s)) s.bind(('127.0.0.1', 0)) self.assertIn('laddr', repr(s)) self.assertIn(str(s.getsockname()), repr(s)) self.assertIn('[closed]', repr(s)) self.assertNotIn('laddr', repr(s)) @unittest.skipUnless(_socket is not None, 'need _socket module') def test_csocket_repr(self): s = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM) try: expected = ('<socket object, fd=%s, family=%s, type=%s, proto=%s>' % (s.fileno(), s.family, s.type, s.proto)) self.assertEqual(repr(s), expected) finally: s.close() expected = ('<socket object, fd=-1, family=%s, type=%s, proto=%s>' % (s.family, s.type, s.proto)) self.assertEqual(repr(s), expected) def test_weakref(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) p = proxy(s) self.assertEqual(p.fileno(), s.fileno()) s.close() s = None try: p.fileno() except ReferenceError: pass else: self.fail('Socket proxy still exists') def testSocketError(self): # Testing socket module exceptions msg = "Error raising socket exception (%s)." with self.assertRaises(OSError, msg=msg % 'OSError'): raise OSError with self.assertRaises(OSError, msg=msg % 'socket.herror'): raise socket.herror with self.assertRaises(OSError, msg=msg % 'socket.gaierror'): raise socket.gaierror def testSendtoErrors(self): # Testing that sendto doens't masks failures. See #10169. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.addCleanup(s.close) s.bind(('', 0)) sockname = s.getsockname() # 2 args with self.assertRaises(TypeError) as cm: s.sendto('\u2620', sockname) self.assertEqual(str(cm.exception), "'str' does not support the buffer interface") with self.assertRaises(TypeError) as cm: s.sendto(5j, sockname) self.assertEqual(str(cm.exception), "'complex' does not support the buffer interface") with self.assertRaises(TypeError) as cm: s.sendto(b'foo', None) self.assertIn('not NoneType',str(cm.exception)) # 3 args with self.assertRaises(TypeError) as cm: s.sendto('\u2620', 0, sockname) self.assertEqual(str(cm.exception), "'str' does not support the buffer interface") with self.assertRaises(TypeError) as cm: s.sendto(5j, 0, sockname) self.assertEqual(str(cm.exception), "'complex' does not support the buffer interface") with self.assertRaises(TypeError) as cm: s.sendto(b'foo', 0, None) self.assertIn('not NoneType', str(cm.exception)) with self.assertRaises(TypeError) as cm: s.sendto(b'foo', 'bar', sockname) self.assertIn('an integer is required', str(cm.exception)) with self.assertRaises(TypeError) as cm: s.sendto(b'foo', None, None) self.assertIn('an integer is required', str(cm.exception)) # wrong number of args with self.assertRaises(TypeError) as cm: s.sendto(b'foo') self.assertIn('(1 given)', str(cm.exception)) with self.assertRaises(TypeError) as cm: s.sendto(b'foo', 0, sockname, 4) self.assertIn('(4 given)', str(cm.exception)) def testCrucialConstants(self): # Testing for mission critical constants socket.AF_INET socket.SOCK_STREAM socket.SOCK_DGRAM socket.SOCK_RAW socket.SOCK_RDM socket.SOCK_SEQPACKET socket.SOL_SOCKET socket.SO_REUSEADDR def testHostnameRes(self): # Testing hostname resolution mechanisms hostname = socket.gethostname() try: ip = socket.gethostbyname(hostname) except OSError: # Probably name lookup wasn't set up right; skip this test self.skipTest('name lookup failure') self.assertTrue(ip.find('.') >= 0, "Error resolving host to ip.") try: hname, aliases, ipaddrs = socket.gethostbyaddr(ip) except OSError: # Probably a similar problem as above; skip this test self.skipTest('name lookup failure') all_host_names = [hostname, hname] + aliases fqhn = socket.getfqdn(ip) if not fqhn in all_host_names: self.fail("Error testing host resolution mechanisms. (fqdn: %s, all: %s)" % (fqhn, repr(all_host_names))) def test_host_resolution(self): for addr in ['0.1.1.~1', '1+.1.1.1', '::1q', '::1::2', '1:1:1:1:1:1:1:1:1']: self.assertRaises(OSError, socket.gethostbyname, addr) self.assertRaises(OSError, socket.gethostbyaddr, addr) for addr in [support.HOST, '10.0.0.1', '255.255.255.255']: self.assertEqual(socket.gethostbyname(addr), addr) # we don't test support.HOSTv6 because there's a chance it doesn't have # a matching name entry (e.g. 'ip6-localhost') for host in [support.HOST]: self.assertIn(host, socket.gethostbyaddr(host)[2]) @unittest.skipUnless(hasattr(socket, 'sethostname'), "test needs socket.sethostname()") @unittest.skipUnless(hasattr(socket, 'gethostname'), "test needs socket.gethostname()") def test_sethostname(self): oldhn = socket.gethostname() try: socket.sethostname('new') except OSError as e: if e.errno == errno.EPERM: self.skipTest("test should be run as root") else: raise try: # running test as root! self.assertEqual(socket.gethostname(), 'new') # Should work with bytes objects too socket.sethostname(b'bar') self.assertEqual(socket.gethostname(), 'bar') finally: socket.sethostname(oldhn) @unittest.skipUnless(hasattr(socket, 'if_nameindex'), 'socket.if_nameindex() not available.') def testInterfaceNameIndex(self): interfaces = socket.if_nameindex() for index, name in interfaces: self.assertIsInstance(index, int) self.assertIsInstance(name, str) # interface indices are non-zero integers self.assertGreater(index, 0) _index = socket.if_nametoindex(name) self.assertIsInstance(_index, int) self.assertEqual(index, _index) _name = socket.if_indextoname(index) self.assertIsInstance(_name, str) self.assertEqual(name, _name) @unittest.skipUnless(hasattr(socket, 'if_nameindex'), 'socket.if_nameindex() not available.') def testInvalidInterfaceNameIndex(self): # test nonexistent interface index/name self.assertRaises(OSError, socket.if_indextoname, 0) self.assertRaises(OSError, socket.if_nametoindex, '_DEADBEEF') # test with invalid values self.assertRaises(TypeError, socket.if_nametoindex, 0) self.assertRaises(TypeError, socket.if_indextoname, '_DEADBEEF') @unittest.skipUnless(hasattr(sys, 'getrefcount'), 'test needs sys.getrefcount()') def testRefCountGetNameInfo(self): # Testing reference count for getnameinfo try: # On some versions, this loses a reference orig = sys.getrefcount(__name__) socket.getnameinfo(__name__,0) except TypeError: if sys.getrefcount(__name__) != orig: self.fail("socket.getnameinfo loses a reference") def testInterpreterCrash(self): # Making sure getnameinfo doesn't crash the interpreter try: # On some versions, this crashes the interpreter. socket.getnameinfo(('x', 0, 0, 0), 0) except OSError: pass def testNtoH(self): # This just checks that htons etc. are their own inverse, # when looking at the lower 16 or 32 bits. sizes = {socket.htonl: 32, socket.ntohl: 32, socket.htons: 16, socket.ntohs: 16} for func, size in sizes.items(): mask = (1<<size) - 1 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): self.assertEqual(i & mask, func(func(i&mask)) & mask) swapped = func(mask) self.assertEqual(swapped & mask, mask) self.assertRaises(OverflowError, func, 1<<34) def testNtoHErrors(self): good_values = [ 1, 2, 3, 1, 2, 3 ] bad_values = [ -1, -2, -3, -1, -2, -3 ] for k in good_values: socket.ntohl(k) socket.ntohs(k) socket.htonl(k) socket.htons(k) for k in bad_values: self.assertRaises(OverflowError, socket.ntohl, k) self.assertRaises(OverflowError, socket.ntohs, k) self.assertRaises(OverflowError, socket.htonl, k) self.assertRaises(OverflowError, socket.htons, k) def testGetServBy(self): eq = self.assertEqual # Find one service that exists, then check all the related interfaces. # I've ordered this by protocols that have both a tcp and udp # protocol, at least for modern Linuxes. if (sys.platform.startswith(('freebsd', 'netbsd', 'gnukfreebsd')) or sys.platform in ('linux', 'darwin')): # avoid the 'echo' service on this platform, as there is an # assumption breaking non-standard port/protocol entry services = ('daytime', 'qotd', 'domain') else: services = ('echo', 'daytime', 'domain') for service in services: try: port = socket.getservbyname(service, 'tcp') break except OSError: pass else: raise OSError # Try same call with optional protocol omitted port2 = socket.getservbyname(service) eq(port, port2) # Try udp, but don't barf if it doesn't exist try: udpport = socket.getservbyname(service, 'udp') except OSError: udpport = None else: eq(udpport, port) # Now make sure the lookup by port returns the same service name eq(socket.getservbyport(port2), service) eq(socket.getservbyport(port, 'tcp'), service) if udpport is not None: eq(socket.getservbyport(udpport, 'udp'), service) # Make sure getservbyport does not accept out of range ports. self.assertRaises(OverflowError, socket.getservbyport, -1) self.assertRaises(OverflowError, socket.getservbyport, 65536) def testDefaultTimeout(self): # Testing default timeout # The default timeout should initially be None self.assertEqual(socket.getdefaulttimeout(), None) s = socket.socket() self.assertEqual(s.gettimeout(), None) s.close() # Set the default timeout to 10, and see if it propagates socket.setdefaulttimeout(10) self.assertEqual(socket.getdefaulttimeout(), 10) s = socket.socket() self.assertEqual(s.gettimeout(), 10) s.close() # Reset the default timeout to None, and see if it propagates socket.setdefaulttimeout(None) self.assertEqual(socket.getdefaulttimeout(), None) s = socket.socket() self.assertEqual(s.gettimeout(), None) s.close() # Check that setting it to an invalid value raises ValueError self.assertRaises(ValueError, socket.setdefaulttimeout, -1) # Check that setting it to an invalid type raises TypeError self.assertRaises(TypeError, socket.setdefaulttimeout, "spam") @unittest.skipUnless(hasattr(socket, 'inet_aton'), 'test needs socket.inet_aton()') def testIPv4_inet_aton_fourbytes(self): # Test that issue1008086 and issue767150 are fixed. # It must return 4 bytes. self.assertEqual(b'\x00'*4, socket.inet_aton('0.0.0.0')) self.assertEqual(b'\xff'*4, socket.inet_aton('255.255.255.255')) @unittest.skipUnless(hasattr(socket, 'inet_pton'), 'test needs socket.inet_pton()') def testIPv4toString(self): from socket import inet_aton as f, inet_pton, AF_INET g = lambda a: inet_pton(AF_INET, a) assertInvalid = lambda func,a: self.assertRaises( (OSError, ValueError), func, a ) self.assertEqual(b'\x00\x00\x00\x00', f('0.0.0.0')) self.assertEqual(b'\xff\x00\xff\x00', f('255.0.255.0')) self.assertEqual(b'\xaa\xaa\xaa\xaa', f('170.170.170.170')) self.assertEqual(b'\x01\x02\x03\x04', f('1.2.3.4')) self.assertEqual(b'\xff\xff\xff\xff', f('255.255.255.255')) assertInvalid(f, '0.0.0.') assertInvalid(f, '300.0.0.0') assertInvalid(f, 'a.0.0.0') assertInvalid(f, '1.2.3.4.5') assertInvalid(f, '::1') self.assertEqual(b'\x00\x00\x00\x00', g('0.0.0.0')) self.assertEqual(b'\xff\x00\xff\x00', g('255.0.255.0')) self.assertEqual(b'\xaa\xaa\xaa\xaa', g('170.170.170.170')) self.assertEqual(b'\xff\xff\xff\xff', g('255.255.255.255')) assertInvalid(g, '0.0.0.') assertInvalid(g, '300.0.0.0') assertInvalid(g, 'a.0.0.0') assertInvalid(g, '1.2.3.4.5') assertInvalid(g, '::1') @unittest.skipUnless(hasattr(socket, 'inet_pton'), 'test needs socket.inet_pton()') def testIPv6toString(self): try: from socket import inet_pton, AF_INET6, has_ipv6 if not has_ipv6: self.skipTest('IPv6 not available') except ImportError: self.skipTest('could not import needed symbols from socket') if sys.platform == "win32": try: inet_pton(AF_INET6, '::') except OSError as e: if e.winerror == 10022: self.skipTest('IPv6 might not be supported') f = lambda a: inet_pton(AF_INET6, a) assertInvalid = lambda a: self.assertRaises( (OSError, ValueError), f, a ) self.assertEqual(b'\x00' * 16, f('::')) self.assertEqual(b'\x00' * 16, f('0::0')) self.assertEqual(b'\x00\x01' + b'\x00' * 14, f('1::')) self.assertEqual( b'\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae', f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae') ) self.assertEqual( b'\xad\x42\x0a\xbc' + b'\x00' * 4 + b'\x01\x27\x00\x00\x02\x54\x00\x02', f('ad42:abc::127:0:254:2') ) self.assertEqual(b'\x00\x12\x00\x0a' + b'\x00' * 12, f('12:a::')) assertInvalid('0x20::') assertInvalid(':::') assertInvalid('::0::') assertInvalid('1::abc::') assertInvalid('1::abc::def') assertInvalid('1:2:3:4:5:6:') assertInvalid('1:2:3:4:5:6') assertInvalid('1:2:3:4:5:6:7:8:') assertInvalid('1:2:3:4:5:6:7:8:0') self.assertEqual(b'\x00' * 12 + b'\xfe\x2a\x17\x40', f('::254.42.23.64') ) self.assertEqual( b'\x00\x42' + b'\x00' * 8 + b'\xa2\x9b\xfe\x2a\x17\x40', f('42::a29b:254.42.23.64') ) self.assertEqual( b'\x00\x42\xa8\xb9\x00\x00\x00\x02\xff\xff\xa2\x9b\xfe\x2a\x17\x40', f('42:a8b9:0:2:ffff:a29b:254.42.23.64') ) assertInvalid('255.254.253.252') assertInvalid('1::260.2.3.0') assertInvalid('1::0.be.e.0') assertInvalid('1:2:3:4:5:6:7:1.2.3.4') assertInvalid('::1.2.3.4:0') assertInvalid('0.100.200.0:3:4:5:6:7:8') @unittest.skipUnless(hasattr(socket, 'inet_ntop'), 'test needs socket.inet_ntop()') def testStringToIPv4(self): from socket import inet_ntoa as f, inet_ntop, AF_INET g = lambda a: inet_ntop(AF_INET, a) assertInvalid = lambda func,a: self.assertRaises( (OSError, ValueError), func, a ) self.assertEqual('1.0.1.0', f(b'\x01\x00\x01\x00')) self.assertEqual('170.85.170.85', f(b'\xaa\x55\xaa\x55')) self.assertEqual('255.255.255.255', f(b'\xff\xff\xff\xff')) self.assertEqual('1.2.3.4', f(b'\x01\x02\x03\x04')) assertInvalid(f, b'\x00' * 3) assertInvalid(f, b'\x00' * 5) assertInvalid(f, b'\x00' * 16) self.assertEqual('1.0.1.0', g(b'\x01\x00\x01\x00')) self.assertEqual('170.85.170.85', g(b'\xaa\x55\xaa\x55')) self.assertEqual('255.255.255.255', g(b'\xff\xff\xff\xff')) assertInvalid(g, b'\x00' * 3) assertInvalid(g, b'\x00' * 5) assertInvalid(g, b'\x00' * 16) @unittest.skipUnless(hasattr(socket, 'inet_ntop'), 'test needs socket.inet_ntop()') def testStringToIPv6(self): try: from socket import inet_ntop, AF_INET6, has_ipv6 if not has_ipv6: self.skipTest('IPv6 not available') except ImportError: self.skipTest('could not import needed symbols from socket') if sys.platform == "win32": try: inet_ntop(AF_INET6, b'\x00' * 16) except OSError as e: if e.winerror == 10022: self.skipTest('IPv6 might not be supported') f = lambda a: inet_ntop(AF_INET6, a) assertInvalid = lambda a: self.assertRaises( (OSError, ValueError), f, a ) self.assertEqual('::', f(b'\x00' * 16)) self.assertEqual('::1', f(b'\x00' * 15 + b'\x01')) self.assertEqual( 'aef:b01:506:1001:ffff:9997:55:170', f(b'\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70') ) assertInvalid(b'\x12' * 15) assertInvalid(b'\x12' * 17) assertInvalid(b'\x12' * 4) # XXX The following don't test module-level functionality... def testSockName(self): # Testing getsockname() port = support.find_unused_port() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(sock.close) sock.bind(("0.0.0.0", port)) name = sock.getsockname() # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate # it reasonable to get the host's addr in addition to 0.0.0.0. # At least for eCos. This is required for the S/390 to pass. try: my_ip_addr = socket.gethostbyname(socket.gethostname()) except OSError: # Probably name lookup wasn't set up right; skip this test self.skipTest('name lookup failure') self.assertIn(name[0], ("0.0.0.0", my_ip_addr), '%s invalid' % name[0]) self.assertEqual(name[1], port) def testGetSockOpt(self): # Testing getsockopt() # We know a socket should start without reuse==0 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(sock.close) reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) self.assertFalse(reuse != 0, "initial mode is reuse") def testSetSockOpt(self): # Testing setsockopt() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(sock.close) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) self.assertFalse(reuse == 0, "failed to set reuse mode") def testSendAfterClose(self): # testing send() after close() with timeout sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(1) sock.close() self.assertRaises(OSError, sock.send, b"spam") def testNewAttributes(self): # testing .family, .type and .protocol sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.assertEqual(sock.family, socket.AF_INET) if hasattr(socket, 'SOCK_CLOEXEC'): self.assertIn(sock.type, (socket.SOCK_STREAM | socket.SOCK_CLOEXEC, socket.SOCK_STREAM)) else: self.assertEqual(sock.type, socket.SOCK_STREAM) self.assertEqual(sock.proto, 0) sock.close() def test_getsockaddrarg(self): sock = socket.socket() self.addCleanup(sock.close) port = support.find_unused_port() big_port = port + 65536 neg_port = port - 65536 self.assertRaises(OverflowError, sock.bind, (HOST, big_port)) self.assertRaises(OverflowError, sock.bind, (HOST, neg_port)) # Since find_unused_port() is inherently subject to race conditions, we # call it a couple times if necessary. for i in itertools.count(): port = support.find_unused_port() try: sock.bind((HOST, port)) except OSError as e: if e.errno != errno.EADDRINUSE or i == 5: raise else: break @unittest.skipUnless(os.name == "nt", "Windows specific") def test_sock_ioctl(self): self.assertTrue(hasattr(socket.socket, 'ioctl')) self.assertTrue(hasattr(socket, 'SIO_RCVALL')) self.assertTrue(hasattr(socket, 'RCVALL_ON')) self.assertTrue(hasattr(socket, 'RCVALL_OFF')) self.assertTrue(hasattr(socket, 'SIO_KEEPALIVE_VALS')) s = socket.socket() self.addCleanup(s.close) self.assertRaises(ValueError, s.ioctl, -1, None) s.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 100, 100)) def testGetaddrinfo(self): try: socket.getaddrinfo('localhost', 80) except socket.gaierror as err: if err.errno == socket.EAI_SERVICE: # see http://bugs.python.org/issue1282647 self.skipTest("buggy libc version") raise # len of every sequence is supposed to be == 5 for info in socket.getaddrinfo(HOST, None): self.assertEqual(len(info), 5) # host can be a domain name, a string representation of an # IPv4/v6 address or None socket.getaddrinfo('localhost', 80) socket.getaddrinfo('127.0.0.1', 80) socket.getaddrinfo(None, 80) if support.IPV6_ENABLED: socket.getaddrinfo('::1', 80) # port can be a string service name such as "http", a numeric # port number or None socket.getaddrinfo(HOST, "http") socket.getaddrinfo(HOST, 80) socket.getaddrinfo(HOST, None) # test family and socktype filters infos = socket.getaddrinfo(HOST, 80, socket.AF_INET, socket.SOCK_STREAM) for family, type, _, _, _ in infos: self.assertEqual(family, socket.AF_INET) self.assertEqual(str(family), 'AddressFamily.AF_INET') self.assertEqual(type, socket.SOCK_STREAM) self.assertEqual(str(type), 'SocketKind.SOCK_STREAM') infos = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM) for _, socktype, _, _, _ in infos: self.assertEqual(socktype, socket.SOCK_STREAM) # test proto and flags arguments socket.getaddrinfo(HOST, None, 0, 0, socket.SOL_TCP) socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE) # a server willing to support both IPv4 and IPv6 will # usually do this socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE) # test keyword arguments a = socket.getaddrinfo(HOST, None) b = socket.getaddrinfo(host=HOST, port=None) self.assertEqual(a, b) a = socket.getaddrinfo(HOST, None, socket.AF_INET) b = socket.getaddrinfo(HOST, None, family=socket.AF_INET) self.assertEqual(a, b) a = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM) b = socket.getaddrinfo(HOST, None, type=socket.SOCK_STREAM) self.assertEqual(a, b) a = socket.getaddrinfo(HOST, None, 0, 0, socket.SOL_TCP) b = socket.getaddrinfo(HOST, None, proto=socket.SOL_TCP) self.assertEqual(a, b) a = socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE) b = socket.getaddrinfo(HOST, None, flags=socket.AI_PASSIVE) self.assertEqual(a, b) a = socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE) b = socket.getaddrinfo(host=None, port=0, family=socket.AF_UNSPEC, type=socket.SOCK_STREAM, proto=0, flags=socket.AI_PASSIVE) self.assertEqual(a, b) # Issue #6697. self.assertRaises(UnicodeEncodeError, socket.getaddrinfo, 'localhost', '\uD800') # Issue 17269: test workaround for OS X platform bug segfault if hasattr(socket, 'AI_NUMERICSERV'): try: # The arguments here are undefined and the call may succeed # or fail. All we care here is that it doesn't segfault. socket.getaddrinfo("localhost", None, 0, 0, 0, socket.AI_NUMERICSERV) except socket.gaierror: pass def test_getnameinfo(self): # only IP addresses are allowed self.assertRaises(OSError, socket.getnameinfo, ('mail.python.org',0), 0) @unittest.skipUnless(support.is_resource_enabled('network'), 'network is not enabled') def test_idna(self): # Check for internet access before running test (issue #12804). try: socket.gethostbyname('python.org') except socket.gaierror as e: if e.errno == socket.EAI_NODATA: self.skipTest('internet access required for this test') # these should all be successful domain = 'испытание.pythontest.net' socket.gethostbyname(domain) socket.gethostbyname_ex(domain) socket.getaddrinfo(domain,0,socket.AF_UNSPEC,socket.SOCK_STREAM) # this may not work if the forward lookup choses the IPv6 address, as that doesn't # have a reverse entry yet # socket.gethostbyaddr('испытание.python.org') def check_sendall_interrupted(self, with_timeout): # socketpair() is not stricly required, but it makes things easier. if not hasattr(signal, 'alarm') or not hasattr(socket, 'socketpair'): self.skipTest("signal.alarm and socket.socketpair required for this test") # Our signal handlers clobber the C errno by calling a math function # with an invalid domain value. def ok_handler(*args): self.assertRaises(ValueError, math.acosh, 0) def raising_handler(*args): self.assertRaises(ValueError, math.acosh, 0) 1 // 0 c, s = socket.socketpair() old_alarm = signal.signal(signal.SIGALRM, raising_handler) try: if with_timeout: # Just above the one second minimum for signal.alarm c.settimeout(1.5) with self.assertRaises(ZeroDivisionError): signal.alarm(1) c.sendall(b"x" * support.SOCK_MAX_SIZE) if with_timeout: signal.signal(signal.SIGALRM, ok_handler) signal.alarm(1) self.assertRaises(socket.timeout, c.sendall, b"x" * support.SOCK_MAX_SIZE) finally: signal.alarm(0) signal.signal(signal.SIGALRM, old_alarm) c.close() s.close() def test_sendall_interrupted(self): self.check_sendall_interrupted(False) def test_sendall_interrupted_with_timeout(self): self.check_sendall_interrupted(True) def test_dealloc_warn(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) r = repr(sock) with self.assertWarns(ResourceWarning) as cm: sock = None support.gc_collect() self.assertIn(r, str(cm.warning.args[0])) # An open socket file object gets dereferenced after the socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) f = sock.makefile('rb') r = repr(sock) sock = None support.gc_collect() with self.assertWarns(ResourceWarning): f = None support.gc_collect() def test_name_closed_socketio(self): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: fp = sock.makefile("rb") fp.close() self.assertEqual(repr(fp), "<_io.BufferedReader name=-1>") def test_unusable_closed_socketio(self): with socket.socket() as sock: fp = sock.makefile("rb", buffering=0) self.assertTrue(fp.readable()) self.assertFalse(fp.writable()) self.assertFalse(fp.seekable()) fp.close() self.assertRaises(ValueError, fp.readable) self.assertRaises(ValueError, fp.writable) self.assertRaises(ValueError, fp.seekable) def test_pickle(self): sock = socket.socket() with sock: for protocol in range(pickle.HIGHEST_PROTOCOL + 1): self.assertRaises(TypeError, pickle.dumps, sock, protocol) def test_listen_backlog(self): for backlog in 0, -1: srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) srv.bind((HOST, 0)) srv.listen(backlog) srv.close() @support.cpython_only def test_listen_backlog_overflow(self): # Issue 15989 import _testcapi srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) srv.bind((HOST, 0)) self.assertRaises(OverflowError, srv.listen, _testcapi.INT_MAX + 1) srv.close() @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') def test_flowinfo(self): self.assertRaises(OverflowError, socket.getnameinfo, (support.HOSTv6, 0, 0xffffffff), 0) with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s: self.assertRaises(OverflowError, s.bind, (support.HOSTv6, 0, -10)) def test_str_for_enums(self): # Make sure that the AF_* and SOCK_* constants have enum-like string # reprs. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: self.assertEqual(str(s.family), 'AddressFamily.AF_INET') self.assertEqual(str(s.type), 'SocketKind.SOCK_STREAM') @unittest.skipIf(os.name == 'nt', 'Will not work on Windows') def test_uknown_socket_family_repr(self): # Test that when created with a family that's not one of the known # AF_*/SOCK_* constants, socket.family just returns the number. # # To do this we fool socket.socket into believing it already has an # open fd because on this path it doesn't actually verify the family and # type and populates the socket object. # # On Windows this trick won't work, so the test is skipped. fd, _ = tempfile.mkstemp() with socket.socket(family=42424, type=13331, fileno=fd) as s: self.assertEqual(s.family, 42424) self.assertEqual(s.type, 13331) @unittest.skipUnless(HAVE_SOCKET_CAN, 'SocketCan required for this test.') class BasicCANTest(unittest.TestCase): def testCrucialConstants(self): socket.AF_CAN socket.PF_CAN socket.CAN_RAW @unittest.skipUnless(hasattr(socket, "CAN_BCM"), 'socket.CAN_BCM required for this test.') def testBCMConstants(self): socket.CAN_BCM # opcodes socket.CAN_BCM_TX_SETUP # create (cyclic) transmission task socket.CAN_BCM_TX_DELETE # remove (cyclic) transmission task socket.CAN_BCM_TX_READ # read properties of (cyclic) transmission task socket.CAN_BCM_TX_SEND # send one CAN frame socket.CAN_BCM_RX_SETUP # create RX content filter subscription socket.CAN_BCM_RX_DELETE # remove RX content filter subscription socket.CAN_BCM_RX_READ # read properties of RX content filter subscription socket.CAN_BCM_TX_STATUS # reply to TX_READ request socket.CAN_BCM_TX_EXPIRED # notification on performed transmissions (count=0) socket.CAN_BCM_RX_STATUS # reply to RX_READ request socket.CAN_BCM_RX_TIMEOUT # cyclic message is absent socket.CAN_BCM_RX_CHANGED # updated CAN frame (detected content change) def testCreateSocket(self): with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: pass @unittest.skipUnless(hasattr(socket, "CAN_BCM"), 'socket.CAN_BCM required for this test.') def testCreateBCMSocket(self): with socket.socket(socket.PF_CAN, socket.SOCK_DGRAM, socket.CAN_BCM) as s: pass def testBindAny(self): with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: s.bind(('', )) def testTooLongInterfaceName(self): # most systems limit IFNAMSIZ to 16, take 1024 to be sure with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: self.assertRaisesRegex(OSError, 'interface name too long', s.bind, ('x' * 1024,)) @unittest.skipUnless(hasattr(socket, "CAN_RAW_LOOPBACK"), 'socket.CAN_RAW_LOOPBACK required for this test.') def testLoopback(self): with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: for loopback in (0, 1): s.setsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_LOOPBACK, loopback) self.assertEqual(loopback, s.getsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_LOOPBACK)) @unittest.skipUnless(hasattr(socket, "CAN_RAW_FILTER"), 'socket.CAN_RAW_FILTER required for this test.') def testFilter(self): can_id, can_mask = 0x200, 0x700 can_filter = struct.pack("=II", can_id, can_mask) with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: s.setsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_FILTER, can_filter) self.assertEqual(can_filter, s.getsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_FILTER, 8)) @unittest.skipUnless(HAVE_SOCKET_CAN, 'SocketCan required for this test.') @unittest.skipUnless(thread, 'Threading required for this test.') class CANTest(ThreadedCANSocketTest): def __init__(self, methodName='runTest'): ThreadedCANSocketTest.__init__(self, methodName=methodName) @classmethod def build_can_frame(cls, can_id, data): """Build a CAN frame.""" can_dlc = len(data) data = data.ljust(8, b'\x00') return struct.pack(cls.can_frame_fmt, can_id, can_dlc, data) @classmethod def dissect_can_frame(cls, frame): """Dissect a CAN frame.""" can_id, can_dlc, data = struct.unpack(cls.can_frame_fmt, frame) return (can_id, can_dlc, data[:can_dlc]) def testSendFrame(self): cf, addr = self.s.recvfrom(self.bufsize) self.assertEqual(self.cf, cf) self.assertEqual(addr[0], self.interface) self.assertEqual(addr[1], socket.AF_CAN) def _testSendFrame(self): self.cf = self.build_can_frame(0x00, b'\x01\x02\x03\x04\x05') self.cli.send(self.cf) def testSendMaxFrame(self): cf, addr = self.s.recvfrom(self.bufsize) self.assertEqual(self.cf, cf) def _testSendMaxFrame(self): self.cf = self.build_can_frame(0x00, b'\x07' * 8) self.cli.send(self.cf) def testSendMultiFrames(self): cf, addr = self.s.recvfrom(self.bufsize) self.assertEqual(self.cf1, cf) cf, addr = self.s.recvfrom(self.bufsize) self.assertEqual(self.cf2, cf) def _testSendMultiFrames(self): self.cf1 = self.build_can_frame(0x07, b'\x44\x33\x22\x11') self.cli.send(self.cf1) self.cf2 = self.build_can_frame(0x12, b'\x99\x22\x33') self.cli.send(self.cf2) @unittest.skipUnless(hasattr(socket, "CAN_BCM"), 'socket.CAN_BCM required for this test.') def _testBCM(self): cf, addr = self.cli.recvfrom(self.bufsize) self.assertEqual(self.cf, cf) can_id, can_dlc, data = self.dissect_can_frame(cf) self.assertEqual(self.can_id, can_id) self.assertEqual(self.data, data) @unittest.skipUnless(hasattr(socket, "CAN_BCM"), 'socket.CAN_BCM required for this test.') def testBCM(self): bcm = socket.socket(socket.PF_CAN, socket.SOCK_DGRAM, socket.CAN_BCM) self.addCleanup(bcm.close) bcm.connect((self.interface,)) self.can_id = 0x123 self.data = bytes([0xc0, 0xff, 0xee]) self.cf = self.build_can_frame(self.can_id, self.data) opcode = socket.CAN_BCM_TX_SEND flags = 0 count = 0 ival1_seconds = ival1_usec = ival2_seconds = ival2_usec = 0 bcm_can_id = 0x0222 nframes = 1 assert len(self.cf) == 16 header = struct.pack(self.bcm_cmd_msg_fmt, opcode, flags, count, ival1_seconds, ival1_usec, ival2_seconds, ival2_usec, bcm_can_id, nframes, ) header_plus_frame = header + self.cf bytes_sent = bcm.send(header_plus_frame) self.assertEqual(bytes_sent, len(header_plus_frame)) @unittest.skipUnless(HAVE_SOCKET_RDS, 'RDS sockets required for this test.') class BasicRDSTest(unittest.TestCase): def testCrucialConstants(self): socket.AF_RDS socket.PF_RDS def testCreateSocket(self): with socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) as s: pass def testSocketBufferSize(self): bufsize = 16384 with socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) as s: s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, bufsize) s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, bufsize) @unittest.skipUnless(HAVE_SOCKET_RDS, 'RDS sockets required for this test.') @unittest.skipUnless(thread, 'Threading required for this test.') class RDSTest(ThreadedRDSSocketTest): def __init__(self, methodName='runTest'): ThreadedRDSSocketTest.__init__(self, methodName=methodName) def setUp(self): super().setUp() self.evt = threading.Event() def testSendAndRecv(self): data, addr = self.serv.recvfrom(self.bufsize) self.assertEqual(self.data, data) self.assertEqual(self.cli_addr, addr) def _testSendAndRecv(self): self.data = b'spam' self.cli.sendto(self.data, 0, (HOST, self.port)) def testPeek(self): data, addr = self.serv.recvfrom(self.bufsize, socket.MSG_PEEK) self.assertEqual(self.data, data) data, addr = self.serv.recvfrom(self.bufsize) self.assertEqual(self.data, data) def _testPeek(self): self.data = b'spam' self.cli.sendto(self.data, 0, (HOST, self.port)) @requireAttrs(socket.socket, 'recvmsg') def testSendAndRecvMsg(self): data, ancdata, msg_flags, addr = self.serv.recvmsg(self.bufsize) self.assertEqual(self.data, data) @requireAttrs(socket.socket, 'sendmsg') def _testSendAndRecvMsg(self): self.data = b'hello ' * 10 self.cli.sendmsg([self.data], (), 0, (HOST, self.port)) def testSendAndRecvMulti(self): data, addr = self.serv.recvfrom(self.bufsize) self.assertEqual(self.data1, data) data, addr = self.serv.recvfrom(self.bufsize) self.assertEqual(self.data2, data) def _testSendAndRecvMulti(self): self.data1 = b'bacon' self.cli.sendto(self.data1, 0, (HOST, self.port)) self.data2 = b'egg' self.cli.sendto(self.data2, 0, (HOST, self.port)) def testSelect(self): r, w, x = select.select([self.serv], [], [], 3.0) self.assertIn(self.serv, r) data, addr = self.serv.recvfrom(self.bufsize) self.assertEqual(self.data, data) def _testSelect(self): self.data = b'select' self.cli.sendto(self.data, 0, (HOST, self.port)) def testCongestion(self): # wait until the sender is done self.evt.wait() def _testCongestion(self): # test the behavior in case of congestion self.data = b'fill' self.cli.setblocking(False) try: # try to lower the receiver's socket buffer size self.cli.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 16384) except OSError: pass with self.assertRaises(OSError) as cm: try: # fill the receiver's socket buffer while True: self.cli.sendto(self.data, 0, (HOST, self.port)) finally: # signal the receiver we're done self.evt.set() # sendto() should have failed with ENOBUFS self.assertEqual(cm.exception.errno, errno.ENOBUFS) # and we should have received a congestion notification through poll r, w, x = select.select([self.serv], [], [], 3.0) self.assertIn(self.serv, r) @unittest.skipUnless(thread, 'Threading required for this test.') class BasicTCPTest(SocketConnectedTest): def __init__(self, methodName='runTest'): SocketConnectedTest.__init__(self, methodName=methodName) def testRecv(self): # Testing large receive over TCP msg = self.cli_conn.recv(1024) self.assertEqual(msg, MSG) def _testRecv(self): self.serv_conn.send(MSG) def testOverFlowRecv(self): # Testing receive in chunks over TCP seg1 = self.cli_conn.recv(len(MSG) - 3) seg2 = self.cli_conn.recv(1024) msg = seg1 + seg2 self.assertEqual(msg, MSG) def _testOverFlowRecv(self): self.serv_conn.send(MSG) def testRecvFrom(self): # Testing large recvfrom() over TCP msg, addr = self.cli_conn.recvfrom(1024) self.assertEqual(msg, MSG) def _testRecvFrom(self): self.serv_conn.send(MSG) def testOverFlowRecvFrom(self): # Testing recvfrom() in chunks over TCP seg1, addr = self.cli_conn.recvfrom(len(MSG)-3) seg2, addr = self.cli_conn.recvfrom(1024) msg = seg1 + seg2 self.assertEqual(msg, MSG) def _testOverFlowRecvFrom(self): self.serv_conn.send(MSG) def testSendAll(self): # Testing sendall() with a 2048 byte string over TCP msg = b'' while 1: read = self.cli_conn.recv(1024) if not read: break msg += read self.assertEqual(msg, b'f' * 2048) def _testSendAll(self): big_chunk = b'f' * 2048 self.serv_conn.sendall(big_chunk) def testFromFd(self): # Testing fromfd() fd = self.cli_conn.fileno() sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(sock.close) self.assertIsInstance(sock, socket.socket) msg = sock.recv(1024) self.assertEqual(msg, MSG) def _testFromFd(self): self.serv_conn.send(MSG) def testDup(self): # Testing dup() sock = self.cli_conn.dup() self.addCleanup(sock.close) msg = sock.recv(1024) self.assertEqual(msg, MSG) def _testDup(self): self.serv_conn.send(MSG) def testShutdown(self): # Testing shutdown() msg = self.cli_conn.recv(1024) self.assertEqual(msg, MSG) # wait for _testShutdown to finish: on OS X, when the server # closes the connection the client also becomes disconnected, # and the client's shutdown call will fail. (Issue #4397.) self.done.wait() def _testShutdown(self): self.serv_conn.send(MSG) self.serv_conn.shutdown(2) testShutdown_overflow = support.cpython_only(testShutdown) @support.cpython_only def _testShutdown_overflow(self): import _testcapi self.serv_conn.send(MSG) # Issue 15989 self.assertRaises(OverflowError, self.serv_conn.shutdown, _testcapi.INT_MAX + 1) self.assertRaises(OverflowError, self.serv_conn.shutdown, 2 + (_testcapi.UINT_MAX + 1)) self.serv_conn.shutdown(2) def testDetach(self): # Testing detach() fileno = self.cli_conn.fileno() f = self.cli_conn.detach() self.assertEqual(f, fileno) # cli_conn cannot be used anymore... self.assertTrue(self.cli_conn._closed) self.assertRaises(OSError, self.cli_conn.recv, 1024) self.cli_conn.close() # ...but we can create another socket using the (still open) # file descriptor sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, fileno=f) self.addCleanup(sock.close) msg = sock.recv(1024) self.assertEqual(msg, MSG) def _testDetach(self): self.serv_conn.send(MSG) @unittest.skipUnless(thread, 'Threading required for this test.') class BasicUDPTest(ThreadedUDPSocketTest): def __init__(self, methodName='runTest'): ThreadedUDPSocketTest.__init__(self, methodName=methodName) def testSendtoAndRecv(self): # Testing sendto() and Recv() over UDP msg = self.serv.recv(len(MSG)) self.assertEqual(msg, MSG) def _testSendtoAndRecv(self): self.cli.sendto(MSG, 0, (HOST, self.port)) def testRecvFrom(self): # Testing recvfrom() over UDP msg, addr = self.serv.recvfrom(len(MSG)) self.assertEqual(msg, MSG) def _testRecvFrom(self): self.cli.sendto(MSG, 0, (HOST, self.port)) def testRecvFromNegative(self): # Negative lengths passed to recvfrom should give ValueError. self.assertRaises(ValueError, self.serv.recvfrom, -1) def _testRecvFromNegative(self): self.cli.sendto(MSG, 0, (HOST, self.port)) # Tests for the sendmsg()/recvmsg() interface. Where possible, the # same test code is used with different families and types of socket # (e.g. stream, datagram), and tests using recvmsg() are repeated # using recvmsg_into(). # # The generic test classes such as SendmsgTests and # RecvmsgGenericTests inherit from SendrecvmsgBase and expect to be # supplied with sockets cli_sock and serv_sock representing the # client's and the server's end of the connection respectively, and # attributes cli_addr and serv_addr holding their (numeric where # appropriate) addresses. # # The final concrete test classes combine these with subclasses of # SocketTestBase which set up client and server sockets of a specific # type, and with subclasses of SendrecvmsgBase such as # SendrecvmsgDgramBase and SendrecvmsgConnectedBase which map these # sockets to cli_sock and serv_sock and override the methods and # attributes of SendrecvmsgBase to fill in destination addresses if # needed when sending, check for specific flags in msg_flags, etc. # # RecvmsgIntoMixin provides a version of doRecvmsg() implemented using # recvmsg_into(). # XXX: like the other datagram (UDP) tests in this module, the code # here assumes that datagram delivery on the local machine will be # reliable. class SendrecvmsgBase(ThreadSafeCleanupTestCase): # Base class for sendmsg()/recvmsg() tests. # Time in seconds to wait before considering a test failed, or # None for no timeout. Not all tests actually set a timeout. fail_timeout = 3.0 def setUp(self): self.misc_event = threading.Event() super().setUp() def sendToServer(self, msg): # Send msg to the server. return self.cli_sock.send(msg) # Tuple of alternative default arguments for sendmsg() when called # via sendmsgToServer() (e.g. to include a destination address). sendmsg_to_server_defaults = () def sendmsgToServer(self, *args): # Call sendmsg() on self.cli_sock with the given arguments, # filling in any arguments which are not supplied with the # corresponding items of self.sendmsg_to_server_defaults, if # any. return self.cli_sock.sendmsg( *(args + self.sendmsg_to_server_defaults[len(args):])) def doRecvmsg(self, sock, bufsize, *args): # Call recvmsg() on sock with given arguments and return its # result. Should be used for tests which can use either # recvmsg() or recvmsg_into() - RecvmsgIntoMixin overrides # this method with one which emulates it using recvmsg_into(), # thus allowing the same test to be used for both methods. result = sock.recvmsg(bufsize, *args) self.registerRecvmsgResult(result) return result def registerRecvmsgResult(self, result): # Called by doRecvmsg() with the return value of recvmsg() or # recvmsg_into(). Can be overridden to arrange cleanup based # on the returned ancillary data, for instance. pass def checkRecvmsgAddress(self, addr1, addr2): # Called to compare the received address with the address of # the peer. self.assertEqual(addr1, addr2) # Flags that are normally unset in msg_flags msg_flags_common_unset = 0 for name in ("MSG_CTRUNC", "MSG_OOB"): msg_flags_common_unset |= getattr(socket, name, 0) # Flags that are normally set msg_flags_common_set = 0 # Flags set when a complete record has been received (e.g. MSG_EOR # for SCTP) msg_flags_eor_indicator = 0 # Flags set when a complete record has not been received # (e.g. MSG_TRUNC for datagram sockets) msg_flags_non_eor_indicator = 0 def checkFlags(self, flags, eor=None, checkset=0, checkunset=0, ignore=0): # Method to check the value of msg_flags returned by recvmsg[_into](). # # Checks that all bits in msg_flags_common_set attribute are # set in "flags" and all bits in msg_flags_common_unset are # unset. # # The "eor" argument specifies whether the flags should # indicate that a full record (or datagram) has been received. # If "eor" is None, no checks are done; otherwise, checks # that: # # * if "eor" is true, all bits in msg_flags_eor_indicator are # set and all bits in msg_flags_non_eor_indicator are unset # # * if "eor" is false, all bits in msg_flags_non_eor_indicator # are set and all bits in msg_flags_eor_indicator are unset # # If "checkset" and/or "checkunset" are supplied, they require # the given bits to be set or unset respectively, overriding # what the attributes require for those bits. # # If any bits are set in "ignore", they will not be checked, # regardless of the other inputs. # # Will raise Exception if the inputs require a bit to be both # set and unset, and it is not ignored. defaultset = self.msg_flags_common_set defaultunset = self.msg_flags_common_unset if eor: defaultset |= self.msg_flags_eor_indicator defaultunset |= self.msg_flags_non_eor_indicator elif eor is not None: defaultset |= self.msg_flags_non_eor_indicator defaultunset |= self.msg_flags_eor_indicator # Function arguments override defaults defaultset &= ~checkunset defaultunset &= ~checkset # Merge arguments with remaining defaults, and check for conflicts checkset |= defaultset checkunset |= defaultunset inboth = checkset & checkunset & ~ignore if inboth: raise Exception("contradictory set, unset requirements for flags " "{0:#x}".format(inboth)) # Compare with given msg_flags value mask = (checkset | checkunset) & ~ignore self.assertEqual(flags & mask, checkset & mask) class RecvmsgIntoMixin(SendrecvmsgBase): # Mixin to implement doRecvmsg() using recvmsg_into(). def doRecvmsg(self, sock, bufsize, *args): buf = bytearray(bufsize) result = sock.recvmsg_into([buf], *args) self.registerRecvmsgResult(result) self.assertGreaterEqual(result[0], 0) self.assertLessEqual(result[0], bufsize) return (bytes(buf[:result[0]]),) + result[1:] class SendrecvmsgDgramFlagsBase(SendrecvmsgBase): # Defines flags to be checked in msg_flags for datagram sockets. @property def msg_flags_non_eor_indicator(self): return super().msg_flags_non_eor_indicator | socket.MSG_TRUNC class SendrecvmsgSCTPFlagsBase(SendrecvmsgBase): # Defines flags to be checked in msg_flags for SCTP sockets. @property def msg_flags_eor_indicator(self): return super().msg_flags_eor_indicator | socket.MSG_EOR class SendrecvmsgConnectionlessBase(SendrecvmsgBase): # Base class for tests on connectionless-mode sockets. Users must # supply sockets on attributes cli and serv to be mapped to # cli_sock and serv_sock respectively. @property def serv_sock(self): return self.serv @property def cli_sock(self): return self.cli @property def sendmsg_to_server_defaults(self): return ([], [], 0, self.serv_addr) def sendToServer(self, msg): return self.cli_sock.sendto(msg, self.serv_addr) class SendrecvmsgConnectedBase(SendrecvmsgBase): # Base class for tests on connected sockets. Users must supply # sockets on attributes serv_conn and cli_conn (representing the # connections *to* the server and the client), to be mapped to # cli_sock and serv_sock respectively. @property def serv_sock(self): return self.cli_conn @property def cli_sock(self): return self.serv_conn def checkRecvmsgAddress(self, addr1, addr2): # Address is currently "unspecified" for a connected socket, # so we don't examine it pass class SendrecvmsgServerTimeoutBase(SendrecvmsgBase): # Base class to set a timeout on server's socket. def setUp(self): super().setUp() self.serv_sock.settimeout(self.fail_timeout) class SendmsgTests(SendrecvmsgServerTimeoutBase): # Tests for sendmsg() which can use any socket type and do not # involve recvmsg() or recvmsg_into(). def testSendmsg(self): # Send a simple message with sendmsg(). self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) def _testSendmsg(self): self.assertEqual(self.sendmsgToServer([MSG]), len(MSG)) def testSendmsgDataGenerator(self): # Send from buffer obtained from a generator (not a sequence). self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) def _testSendmsgDataGenerator(self): self.assertEqual(self.sendmsgToServer((o for o in [MSG])), len(MSG)) def testSendmsgAncillaryGenerator(self): # Gather (empty) ancillary data from a generator. self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) def _testSendmsgAncillaryGenerator(self): self.assertEqual(self.sendmsgToServer([MSG], (o for o in [])), len(MSG)) def testSendmsgArray(self): # Send data from an array instead of the usual bytes object. self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) def _testSendmsgArray(self): self.assertEqual(self.sendmsgToServer([array.array("B", MSG)]), len(MSG)) def testSendmsgGather(self): # Send message data from more than one buffer (gather write). self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) def _testSendmsgGather(self): self.assertEqual(self.sendmsgToServer([MSG[:3], MSG[3:]]), len(MSG)) def testSendmsgBadArgs(self): # Check that sendmsg() rejects invalid arguments. self.assertEqual(self.serv_sock.recv(1000), b"done") def _testSendmsgBadArgs(self): self.assertRaises(TypeError, self.cli_sock.sendmsg) self.assertRaises(TypeError, self.sendmsgToServer, b"not in an iterable") self.assertRaises(TypeError, self.sendmsgToServer, object()) self.assertRaises(TypeError, self.sendmsgToServer, [object()]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG, object()]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], object()) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [], object()) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [], 0, object()) self.sendToServer(b"done") def testSendmsgBadCmsg(self): # Check that invalid ancillary data items are rejected. self.assertEqual(self.serv_sock.recv(1000), b"done") def _testSendmsgBadCmsg(self): self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [object()]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [(object(), 0, b"data")]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [(0, object(), b"data")]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [(0, 0, object())]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [(0, 0)]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [(0, 0, b"data", 42)]) self.sendToServer(b"done") @requireAttrs(socket, "CMSG_SPACE") def testSendmsgBadMultiCmsg(self): # Check that invalid ancillary data items are rejected when # more than one item is present. self.assertEqual(self.serv_sock.recv(1000), b"done") @testSendmsgBadMultiCmsg.client_skip def _testSendmsgBadMultiCmsg(self): self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [0, 0, b""]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [(0, 0, b""), object()]) self.sendToServer(b"done") def testSendmsgExcessCmsgReject(self): # Check that sendmsg() rejects excess ancillary data items # when the number that can be sent is limited. self.assertEqual(self.serv_sock.recv(1000), b"done") def _testSendmsgExcessCmsgReject(self): if not hasattr(socket, "CMSG_SPACE"): # Can only send one item with self.assertRaises(OSError) as cm: self.sendmsgToServer([MSG], [(0, 0, b""), (0, 0, b"")]) self.assertIsNone(cm.exception.errno) self.sendToServer(b"done") def testSendmsgAfterClose(self): # Check that sendmsg() fails on a closed socket. pass def _testSendmsgAfterClose(self): self.cli_sock.close() self.assertRaises(OSError, self.sendmsgToServer, [MSG]) class SendmsgStreamTests(SendmsgTests): # Tests for sendmsg() which require a stream socket and do not # involve recvmsg() or recvmsg_into(). def testSendmsgExplicitNoneAddr(self): # Check that peer address can be specified as None. self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) def _testSendmsgExplicitNoneAddr(self): self.assertEqual(self.sendmsgToServer([MSG], [], 0, None), len(MSG)) def testSendmsgTimeout(self): # Check that timeout works with sendmsg(). self.assertEqual(self.serv_sock.recv(512), b"a"*512) self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) def _testSendmsgTimeout(self): try: self.cli_sock.settimeout(0.03) with self.assertRaises(socket.timeout): while True: self.sendmsgToServer([b"a"*512]) finally: self.misc_event.set() # XXX: would be nice to have more tests for sendmsg flags argument. # Linux supports MSG_DONTWAIT when sending, but in general, it # only works when receiving. Could add other platforms if they # support it too. @skipWithClientIf(sys.platform not in {"linux"}, "MSG_DONTWAIT not known to work on this platform when " "sending") def testSendmsgDontWait(self): # Check that MSG_DONTWAIT in flags causes non-blocking behaviour. self.assertEqual(self.serv_sock.recv(512), b"a"*512) self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) @testSendmsgDontWait.client_skip def _testSendmsgDontWait(self): try: with self.assertRaises(OSError) as cm: while True: self.sendmsgToServer([b"a"*512], [], socket.MSG_DONTWAIT) self.assertIn(cm.exception.errno, (errno.EAGAIN, errno.EWOULDBLOCK)) finally: self.misc_event.set() class SendmsgConnectionlessTests(SendmsgTests): # Tests for sendmsg() which require a connectionless-mode # (e.g. datagram) socket, and do not involve recvmsg() or # recvmsg_into(). def testSendmsgNoDestAddr(self): # Check that sendmsg() fails when no destination address is # given for unconnected socket. pass def _testSendmsgNoDestAddr(self): self.assertRaises(OSError, self.cli_sock.sendmsg, [MSG]) self.assertRaises(OSError, self.cli_sock.sendmsg, [MSG], [], 0, None) class RecvmsgGenericTests(SendrecvmsgBase): # Tests for recvmsg() which can also be emulated using # recvmsg_into(), and can use any socket type. def testRecvmsg(self): # Receive a simple message with recvmsg[_into](). msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG)) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsg(self): self.sendToServer(MSG) def testRecvmsgExplicitDefaults(self): # Test recvmsg[_into]() with default arguments provided explicitly. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 0, 0) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgExplicitDefaults(self): self.sendToServer(MSG) def testRecvmsgShorter(self): # Receive a message smaller than buffer. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG) + 42) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgShorter(self): self.sendToServer(MSG) # FreeBSD < 8 doesn't always set the MSG_TRUNC flag when a truncated # datagram is received (issue #13001). @support.requires_freebsd_version(8) def testRecvmsgTrunc(self): # Receive part of message, check for truncation indicators. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG) - 3) self.assertEqual(msg, MSG[:-3]) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=False) @support.requires_freebsd_version(8) def _testRecvmsgTrunc(self): self.sendToServer(MSG) def testRecvmsgShortAncillaryBuf(self): # Test ancillary data buffer too small to hold any ancillary data. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 1) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgShortAncillaryBuf(self): self.sendToServer(MSG) def testRecvmsgLongAncillaryBuf(self): # Test large ancillary data buffer. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 10240) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgLongAncillaryBuf(self): self.sendToServer(MSG) def testRecvmsgAfterClose(self): # Check that recvmsg[_into]() fails on a closed socket. self.serv_sock.close() self.assertRaises(OSError, self.doRecvmsg, self.serv_sock, 1024) def _testRecvmsgAfterClose(self): pass def testRecvmsgTimeout(self): # Check that timeout works. try: self.serv_sock.settimeout(0.03) self.assertRaises(socket.timeout, self.doRecvmsg, self.serv_sock, len(MSG)) finally: self.misc_event.set() def _testRecvmsgTimeout(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) @requireAttrs(socket, "MSG_PEEK") def testRecvmsgPeek(self): # Check that MSG_PEEK in flags enables examination of pending # data without consuming it. # Receive part of data with MSG_PEEK. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG) - 3, 0, socket.MSG_PEEK) self.assertEqual(msg, MSG[:-3]) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) # Ignoring MSG_TRUNC here (so this test is the same for stream # and datagram sockets). Some wording in POSIX seems to # suggest that it needn't be set when peeking, but that may # just be a slip. self.checkFlags(flags, eor=False, ignore=getattr(socket, "MSG_TRUNC", 0)) # Receive all data with MSG_PEEK. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 0, socket.MSG_PEEK) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) # Check that the same data can still be received normally. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG)) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) @testRecvmsgPeek.client_skip def _testRecvmsgPeek(self): self.sendToServer(MSG) @requireAttrs(socket.socket, "sendmsg") def testRecvmsgFromSendmsg(self): # Test receiving with recvmsg[_into]() when message is sent # using sendmsg(). self.serv_sock.settimeout(self.fail_timeout) msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG)) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) @testRecvmsgFromSendmsg.client_skip def _testRecvmsgFromSendmsg(self): self.assertEqual(self.sendmsgToServer([MSG[:3], MSG[3:]]), len(MSG)) class RecvmsgGenericStreamTests(RecvmsgGenericTests): # Tests which require a stream socket and can use either recvmsg() # or recvmsg_into(). def testRecvmsgEOF(self): # Receive end-of-stream indicator (b"", peer socket closed). msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, 1024) self.assertEqual(msg, b"") self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=None) # Might not have end-of-record marker def _testRecvmsgEOF(self): self.cli_sock.close() def testRecvmsgOverflow(self): # Receive a message in more than one chunk. seg1, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG) - 3) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=False) seg2, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, 1024) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) msg = seg1 + seg2 self.assertEqual(msg, MSG) def _testRecvmsgOverflow(self): self.sendToServer(MSG) class RecvmsgTests(RecvmsgGenericTests): # Tests for recvmsg() which can use any socket type. def testRecvmsgBadArgs(self): # Check that recvmsg() rejects invalid arguments. self.assertRaises(TypeError, self.serv_sock.recvmsg) self.assertRaises(ValueError, self.serv_sock.recvmsg, -1, 0, 0) self.assertRaises(ValueError, self.serv_sock.recvmsg, len(MSG), -1, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg, [bytearray(10)], 0, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg, object(), 0, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg, len(MSG), object(), 0) self.assertRaises(TypeError, self.serv_sock.recvmsg, len(MSG), 0, object()) msg, ancdata, flags, addr = self.serv_sock.recvmsg(len(MSG), 0, 0) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgBadArgs(self): self.sendToServer(MSG) class RecvmsgIntoTests(RecvmsgIntoMixin, RecvmsgGenericTests): # Tests for recvmsg_into() which can use any socket type. def testRecvmsgIntoBadArgs(self): # Check that recvmsg_into() rejects invalid arguments. buf = bytearray(len(MSG)) self.assertRaises(TypeError, self.serv_sock.recvmsg_into) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, len(MSG), 0, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, buf, 0, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, [object()], 0, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, [b"I'm not writable"], 0, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, [buf, object()], 0, 0) self.assertRaises(ValueError, self.serv_sock.recvmsg_into, [buf], -1, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, [buf], object(), 0) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, [buf], 0, object()) nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into([buf], 0, 0) self.assertEqual(nbytes, len(MSG)) self.assertEqual(buf, bytearray(MSG)) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgIntoBadArgs(self): self.sendToServer(MSG) def testRecvmsgIntoGenerator(self): # Receive into buffer obtained from a generator (not a sequence). buf = bytearray(len(MSG)) nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into( (o for o in [buf])) self.assertEqual(nbytes, len(MSG)) self.assertEqual(buf, bytearray(MSG)) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgIntoGenerator(self): self.sendToServer(MSG) def testRecvmsgIntoArray(self): # Receive into an array rather than the usual bytearray. buf = array.array("B", [0] * len(MSG)) nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into([buf]) self.assertEqual(nbytes, len(MSG)) self.assertEqual(buf.tobytes(), MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgIntoArray(self): self.sendToServer(MSG) def testRecvmsgIntoScatter(self): # Receive into multiple buffers (scatter write). b1 = bytearray(b"----") b2 = bytearray(b"0123456789") b3 = bytearray(b"--------------") nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into( [b1, memoryview(b2)[2:9], b3]) self.assertEqual(nbytes, len(b"Mary had a little lamb")) self.assertEqual(b1, bytearray(b"Mary")) self.assertEqual(b2, bytearray(b"01 had a 9")) self.assertEqual(b3, bytearray(b"little lamb---")) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgIntoScatter(self): self.sendToServer(b"Mary had a little lamb") class CmsgMacroTests(unittest.TestCase): # Test the functions CMSG_LEN() and CMSG_SPACE(). Tests # assumptions used by sendmsg() and recvmsg[_into](), which share # code with these functions. # Match the definition in socketmodule.c try: import _testcapi except ImportError: socklen_t_limit = 0x7fffffff else: socklen_t_limit = min(0x7fffffff, _testcapi.INT_MAX) @requireAttrs(socket, "CMSG_LEN") def testCMSG_LEN(self): # Test CMSG_LEN() with various valid and invalid values, # checking the assumptions used by recvmsg() and sendmsg(). toobig = self.socklen_t_limit - socket.CMSG_LEN(0) + 1 values = list(range(257)) + list(range(toobig - 257, toobig)) # struct cmsghdr has at least three members, two of which are ints self.assertGreater(socket.CMSG_LEN(0), array.array("i").itemsize * 2) for n in values: ret = socket.CMSG_LEN(n) # This is how recvmsg() calculates the data size self.assertEqual(ret - socket.CMSG_LEN(0), n) self.assertLessEqual(ret, self.socklen_t_limit) self.assertRaises(OverflowError, socket.CMSG_LEN, -1) # sendmsg() shares code with these functions, and requires # that it reject values over the limit. self.assertRaises(OverflowError, socket.CMSG_LEN, toobig) self.assertRaises(OverflowError, socket.CMSG_LEN, sys.maxsize) @requireAttrs(socket, "CMSG_SPACE") def testCMSG_SPACE(self): # Test CMSG_SPACE() with various valid and invalid values, # checking the assumptions used by sendmsg(). toobig = self.socklen_t_limit - socket.CMSG_SPACE(1) + 1 values = list(range(257)) + list(range(toobig - 257, toobig)) last = socket.CMSG_SPACE(0) # struct cmsghdr has at least three members, two of which are ints self.assertGreater(last, array.array("i").itemsize * 2) for n in values: ret = socket.CMSG_SPACE(n) self.assertGreaterEqual(ret, last) self.assertGreaterEqual(ret, socket.CMSG_LEN(n)) self.assertGreaterEqual(ret, n + socket.CMSG_LEN(0)) self.assertLessEqual(ret, self.socklen_t_limit) last = ret self.assertRaises(OverflowError, socket.CMSG_SPACE, -1) # sendmsg() shares code with these functions, and requires # that it reject values over the limit. self.assertRaises(OverflowError, socket.CMSG_SPACE, toobig) self.assertRaises(OverflowError, socket.CMSG_SPACE, sys.maxsize) class SCMRightsTest(SendrecvmsgServerTimeoutBase): # Tests for file descriptor passing on Unix-domain sockets. # Invalid file descriptor value that's unlikely to evaluate to a # real FD even if one of its bytes is replaced with a different # value (which shouldn't actually happen). badfd = -0x5555 def newFDs(self, n): # Return a list of n file descriptors for newly-created files # containing their list indices as ASCII numbers. fds = [] for i in range(n): fd, path = tempfile.mkstemp() self.addCleanup(os.unlink, path) self.addCleanup(os.close, fd) os.write(fd, str(i).encode()) fds.append(fd) return fds def checkFDs(self, fds): # Check that the file descriptors in the given list contain # their correct list indices as ASCII numbers. for n, fd in enumerate(fds): os.lseek(fd, 0, os.SEEK_SET) self.assertEqual(os.read(fd, 1024), str(n).encode()) def registerRecvmsgResult(self, result): self.addCleanup(self.closeRecvmsgFDs, result) def closeRecvmsgFDs(self, recvmsg_result): # Close all file descriptors specified in the ancillary data # of the given return value from recvmsg() or recvmsg_into(). for cmsg_level, cmsg_type, cmsg_data in recvmsg_result[1]: if (cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS): fds = array.array("i") fds.frombytes(cmsg_data[: len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) for fd in fds: os.close(fd) def createAndSendFDs(self, n): # Send n new file descriptors created by newFDs() to the # server, with the constant MSG as the non-ancillary data. self.assertEqual( self.sendmsgToServer([MSG], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", self.newFDs(n)))]), len(MSG)) def checkRecvmsgFDs(self, numfds, result, maxcmsgs=1, ignoreflags=0): # Check that constant MSG was received with numfds file # descriptors in a maximum of maxcmsgs control messages (which # must contain only complete integers). By default, check # that MSG_CTRUNC is unset, but ignore any flags in # ignoreflags. msg, ancdata, flags, addr = result self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkunset=socket.MSG_CTRUNC, ignore=ignoreflags) self.assertIsInstance(ancdata, list) self.assertLessEqual(len(ancdata), maxcmsgs) fds = array.array("i") for item in ancdata: self.assertIsInstance(item, tuple) cmsg_level, cmsg_type, cmsg_data = item self.assertEqual(cmsg_level, socket.SOL_SOCKET) self.assertEqual(cmsg_type, socket.SCM_RIGHTS) self.assertIsInstance(cmsg_data, bytes) self.assertEqual(len(cmsg_data) % SIZEOF_INT, 0) fds.frombytes(cmsg_data) self.assertEqual(len(fds), numfds) self.checkFDs(fds) def testFDPassSimple(self): # Pass a single FD (array read from bytes object). self.checkRecvmsgFDs(1, self.doRecvmsg(self.serv_sock, len(MSG), 10240)) def _testFDPassSimple(self): self.assertEqual( self.sendmsgToServer( [MSG], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", self.newFDs(1)).tobytes())]), len(MSG)) def testMultipleFDPass(self): # Pass multiple FDs in a single array. self.checkRecvmsgFDs(4, self.doRecvmsg(self.serv_sock, len(MSG), 10240)) def _testMultipleFDPass(self): self.createAndSendFDs(4) @requireAttrs(socket, "CMSG_SPACE") def testFDPassCMSG_SPACE(self): # Test using CMSG_SPACE() to calculate ancillary buffer size. self.checkRecvmsgFDs( 4, self.doRecvmsg(self.serv_sock, len(MSG), socket.CMSG_SPACE(4 * SIZEOF_INT))) @testFDPassCMSG_SPACE.client_skip def _testFDPassCMSG_SPACE(self): self.createAndSendFDs(4) def testFDPassCMSG_LEN(self): # Test using CMSG_LEN() to calculate ancillary buffer size. self.checkRecvmsgFDs(1, self.doRecvmsg(self.serv_sock, len(MSG), socket.CMSG_LEN(4 * SIZEOF_INT)), # RFC 3542 says implementations may set # MSG_CTRUNC if there isn't enough space # for trailing padding. ignoreflags=socket.MSG_CTRUNC) def _testFDPassCMSG_LEN(self): self.createAndSendFDs(1) @unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958") @requireAttrs(socket, "CMSG_SPACE") def testFDPassSeparate(self): # Pass two FDs in two separate arrays. Arrays may be combined # into a single control message by the OS. self.checkRecvmsgFDs(2, self.doRecvmsg(self.serv_sock, len(MSG), 10240), maxcmsgs=2) @testFDPassSeparate.client_skip @unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958") def _testFDPassSeparate(self): fd0, fd1 = self.newFDs(2) self.assertEqual( self.sendmsgToServer([MSG], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [fd0])), (socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [fd1]))]), len(MSG)) @unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958") @requireAttrs(socket, "CMSG_SPACE") def testFDPassSeparateMinSpace(self): # Pass two FDs in two separate arrays, receiving them into the # minimum space for two arrays. self.checkRecvmsgFDs(2, self.doRecvmsg(self.serv_sock, len(MSG), socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(SIZEOF_INT)), maxcmsgs=2, ignoreflags=socket.MSG_CTRUNC) @testFDPassSeparateMinSpace.client_skip @unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958") def _testFDPassSeparateMinSpace(self): fd0, fd1 = self.newFDs(2) self.assertEqual( self.sendmsgToServer([MSG], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [fd0])), (socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [fd1]))]), len(MSG)) def sendAncillaryIfPossible(self, msg, ancdata): # Try to send msg and ancdata to server, but if the system # call fails, just send msg with no ancillary data. try: nbytes = self.sendmsgToServer([msg], ancdata) except OSError as e: # Check that it was the system call that failed self.assertIsInstance(e.errno, int) nbytes = self.sendmsgToServer([msg]) self.assertEqual(nbytes, len(msg)) def testFDPassEmpty(self): # Try to pass an empty FD array. Can receive either no array # or an empty array. self.checkRecvmsgFDs(0, self.doRecvmsg(self.serv_sock, len(MSG), 10240), ignoreflags=socket.MSG_CTRUNC) def _testFDPassEmpty(self): self.sendAncillaryIfPossible(MSG, [(socket.SOL_SOCKET, socket.SCM_RIGHTS, b"")]) def testFDPassPartialInt(self): # Try to pass a truncated FD array. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 10240) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, ignore=socket.MSG_CTRUNC) self.assertLessEqual(len(ancdata), 1) for cmsg_level, cmsg_type, cmsg_data in ancdata: self.assertEqual(cmsg_level, socket.SOL_SOCKET) self.assertEqual(cmsg_type, socket.SCM_RIGHTS) self.assertLess(len(cmsg_data), SIZEOF_INT) def _testFDPassPartialInt(self): self.sendAncillaryIfPossible( MSG, [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [self.badfd]).tobytes()[:-1])]) @requireAttrs(socket, "CMSG_SPACE") def testFDPassPartialIntInMiddle(self): # Try to pass two FD arrays, the first of which is truncated. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 10240) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, ignore=socket.MSG_CTRUNC) self.assertLessEqual(len(ancdata), 2) fds = array.array("i") # Arrays may have been combined in a single control message for cmsg_level, cmsg_type, cmsg_data in ancdata: self.assertEqual(cmsg_level, socket.SOL_SOCKET) self.assertEqual(cmsg_type, socket.SCM_RIGHTS) fds.frombytes(cmsg_data[: len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) self.assertLessEqual(len(fds), 2) self.checkFDs(fds) @testFDPassPartialIntInMiddle.client_skip def _testFDPassPartialIntInMiddle(self): fd0, fd1 = self.newFDs(2) self.sendAncillaryIfPossible( MSG, [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [fd0, self.badfd]).tobytes()[:-1]), (socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [fd1]))]) def checkTruncatedHeader(self, result, ignoreflags=0): # Check that no ancillary data items are returned when data is # truncated inside the cmsghdr structure. msg, ancdata, flags, addr = result self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC, ignore=ignoreflags) def testCmsgTruncNoBufSize(self): # Check that no ancillary data is received when no buffer size # is specified. self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG)), # BSD seems to set MSG_CTRUNC only # if an item has been partially # received. ignoreflags=socket.MSG_CTRUNC) def _testCmsgTruncNoBufSize(self): self.createAndSendFDs(1) def testCmsgTrunc0(self): # Check that no ancillary data is received when buffer size is 0. self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), 0), ignoreflags=socket.MSG_CTRUNC) def _testCmsgTrunc0(self): self.createAndSendFDs(1) # Check that no ancillary data is returned for various non-zero # (but still too small) buffer sizes. def testCmsgTrunc1(self): self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), 1)) def _testCmsgTrunc1(self): self.createAndSendFDs(1) def testCmsgTrunc2Int(self): # The cmsghdr structure has at least three members, two of # which are ints, so we still shouldn't see any ancillary # data. self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), SIZEOF_INT * 2)) def _testCmsgTrunc2Int(self): self.createAndSendFDs(1) def testCmsgTruncLen0Minus1(self): self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), socket.CMSG_LEN(0) - 1)) def _testCmsgTruncLen0Minus1(self): self.createAndSendFDs(1) # The following tests try to truncate the control message in the # middle of the FD array. def checkTruncatedArray(self, ancbuf, maxdata, mindata=0): # Check that file descriptor data is truncated to between # mindata and maxdata bytes when received with buffer size # ancbuf, and that any complete file descriptor numbers are # valid. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), ancbuf) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC) if mindata == 0 and ancdata == []: return self.assertEqual(len(ancdata), 1) cmsg_level, cmsg_type, cmsg_data = ancdata[0] self.assertEqual(cmsg_level, socket.SOL_SOCKET) self.assertEqual(cmsg_type, socket.SCM_RIGHTS) self.assertGreaterEqual(len(cmsg_data), mindata) self.assertLessEqual(len(cmsg_data), maxdata) fds = array.array("i") fds.frombytes(cmsg_data[: len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) self.checkFDs(fds) def testCmsgTruncLen0(self): self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(0), maxdata=0) def _testCmsgTruncLen0(self): self.createAndSendFDs(1) def testCmsgTruncLen0Plus1(self): self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(0) + 1, maxdata=1) def _testCmsgTruncLen0Plus1(self): self.createAndSendFDs(2) def testCmsgTruncLen1(self): self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(SIZEOF_INT), maxdata=SIZEOF_INT) def _testCmsgTruncLen1(self): self.createAndSendFDs(2) def testCmsgTruncLen2Minus1(self): self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(2 * SIZEOF_INT) - 1, maxdata=(2 * SIZEOF_INT) - 1) def _testCmsgTruncLen2Minus1(self): self.createAndSendFDs(2) class RFC3542AncillaryTest(SendrecvmsgServerTimeoutBase): # Test sendmsg() and recvmsg[_into]() using the ancillary data # features of the RFC 3542 Advanced Sockets API for IPv6. # Currently we can only handle certain data items (e.g. traffic # class, hop limit, MTU discovery and fragmentation settings) # without resorting to unportable means such as the struct module, # but the tests here are aimed at testing the ancillary data # handling in sendmsg() and recvmsg() rather than the IPv6 API # itself. # Test value to use when setting hop limit of packet hop_limit = 2 # Test value to use when setting traffic class of packet. # -1 means "use kernel default". traffic_class = -1 def ancillaryMapping(self, ancdata): # Given ancillary data list ancdata, return a mapping from # pairs (cmsg_level, cmsg_type) to corresponding cmsg_data. # Check that no (level, type) pair appears more than once. d = {} for cmsg_level, cmsg_type, cmsg_data in ancdata: self.assertNotIn((cmsg_level, cmsg_type), d) d[(cmsg_level, cmsg_type)] = cmsg_data return d def checkHopLimit(self, ancbufsize, maxhop=255, ignoreflags=0): # Receive hop limit into ancbufsize bytes of ancillary data # space. Check that data is MSG, ancillary data is not # truncated (but ignore any flags in ignoreflags), and hop # limit is between 0 and maxhop inclusive. self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.misc_event.set() msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), ancbufsize) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkunset=socket.MSG_CTRUNC, ignore=ignoreflags) self.assertEqual(len(ancdata), 1) self.assertIsInstance(ancdata[0], tuple) cmsg_level, cmsg_type, cmsg_data = ancdata[0] self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) self.assertEqual(cmsg_type, socket.IPV6_HOPLIMIT) self.assertIsInstance(cmsg_data, bytes) self.assertEqual(len(cmsg_data), SIZEOF_INT) a = array.array("i") a.frombytes(cmsg_data) self.assertGreaterEqual(a[0], 0) self.assertLessEqual(a[0], maxhop) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testRecvHopLimit(self): # Test receiving the packet hop limit as ancillary data. self.checkHopLimit(ancbufsize=10240) @testRecvHopLimit.client_skip def _testRecvHopLimit(self): # Need to wait until server has asked to receive ancillary # data, as implementations are not required to buffer it # otherwise. self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testRecvHopLimitCMSG_SPACE(self): # Test receiving hop limit, using CMSG_SPACE to calculate buffer size. self.checkHopLimit(ancbufsize=socket.CMSG_SPACE(SIZEOF_INT)) @testRecvHopLimitCMSG_SPACE.client_skip def _testRecvHopLimitCMSG_SPACE(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) # Could test receiving into buffer sized using CMSG_LEN, but RFC # 3542 says portable applications must provide space for trailing # padding. Implementations may set MSG_CTRUNC if there isn't # enough space for the padding. @requireAttrs(socket.socket, "sendmsg") @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testSetHopLimit(self): # Test setting hop limit on outgoing packet and receiving it # at the other end. self.checkHopLimit(ancbufsize=10240, maxhop=self.hop_limit) @testSetHopLimit.client_skip def _testSetHopLimit(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.assertEqual( self.sendmsgToServer([MSG], [(socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT, array.array("i", [self.hop_limit]))]), len(MSG)) def checkTrafficClassAndHopLimit(self, ancbufsize, maxhop=255, ignoreflags=0): # Receive traffic class and hop limit into ancbufsize bytes of # ancillary data space. Check that data is MSG, ancillary # data is not truncated (but ignore any flags in ignoreflags), # and traffic class and hop limit are in range (hop limit no # more than maxhop). self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVTCLASS, 1) self.misc_event.set() msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), ancbufsize) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkunset=socket.MSG_CTRUNC, ignore=ignoreflags) self.assertEqual(len(ancdata), 2) ancmap = self.ancillaryMapping(ancdata) tcdata = ancmap[(socket.IPPROTO_IPV6, socket.IPV6_TCLASS)] self.assertEqual(len(tcdata), SIZEOF_INT) a = array.array("i") a.frombytes(tcdata) self.assertGreaterEqual(a[0], 0) self.assertLessEqual(a[0], 255) hldata = ancmap[(socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT)] self.assertEqual(len(hldata), SIZEOF_INT) a = array.array("i") a.frombytes(hldata) self.assertGreaterEqual(a[0], 0) self.assertLessEqual(a[0], maxhop) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testRecvTrafficClassAndHopLimit(self): # Test receiving traffic class and hop limit as ancillary data. self.checkTrafficClassAndHopLimit(ancbufsize=10240) @testRecvTrafficClassAndHopLimit.client_skip def _testRecvTrafficClassAndHopLimit(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testRecvTrafficClassAndHopLimitCMSG_SPACE(self): # Test receiving traffic class and hop limit, using # CMSG_SPACE() to calculate buffer size. self.checkTrafficClassAndHopLimit( ancbufsize=socket.CMSG_SPACE(SIZEOF_INT) * 2) @testRecvTrafficClassAndHopLimitCMSG_SPACE.client_skip def _testRecvTrafficClassAndHopLimitCMSG_SPACE(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket.socket, "sendmsg") @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testSetTrafficClassAndHopLimit(self): # Test setting traffic class and hop limit on outgoing packet, # and receiving them at the other end. self.checkTrafficClassAndHopLimit(ancbufsize=10240, maxhop=self.hop_limit) @testSetTrafficClassAndHopLimit.client_skip def _testSetTrafficClassAndHopLimit(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.assertEqual( self.sendmsgToServer([MSG], [(socket.IPPROTO_IPV6, socket.IPV6_TCLASS, array.array("i", [self.traffic_class])), (socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT, array.array("i", [self.hop_limit]))]), len(MSG)) @requireAttrs(socket.socket, "sendmsg") @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testOddCmsgSize(self): # Try to send ancillary data with first item one byte too # long. Fall back to sending with correct size if this fails, # and check that second item was handled correctly. self.checkTrafficClassAndHopLimit(ancbufsize=10240, maxhop=self.hop_limit) @testOddCmsgSize.client_skip def _testOddCmsgSize(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) try: nbytes = self.sendmsgToServer( [MSG], [(socket.IPPROTO_IPV6, socket.IPV6_TCLASS, array.array("i", [self.traffic_class]).tobytes() + b"\x00"), (socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT, array.array("i", [self.hop_limit]))]) except OSError as e: self.assertIsInstance(e.errno, int) nbytes = self.sendmsgToServer( [MSG], [(socket.IPPROTO_IPV6, socket.IPV6_TCLASS, array.array("i", [self.traffic_class])), (socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT, array.array("i", [self.hop_limit]))]) self.assertEqual(nbytes, len(MSG)) # Tests for proper handling of truncated ancillary data def checkHopLimitTruncatedHeader(self, ancbufsize, ignoreflags=0): # Receive hop limit into ancbufsize bytes of ancillary data # space, which should be too small to contain the ancillary # data header (if ancbufsize is None, pass no second argument # to recvmsg()). Check that data is MSG, MSG_CTRUNC is set # (unless included in ignoreflags), and no ancillary data is # returned. self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.misc_event.set() args = () if ancbufsize is None else (ancbufsize,) msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), *args) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC, ignore=ignoreflags) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testCmsgTruncNoBufSize(self): # Check that no ancillary data is received when no ancillary # buffer size is provided. self.checkHopLimitTruncatedHeader(ancbufsize=None, # BSD seems to set # MSG_CTRUNC only if an item # has been partially # received. ignoreflags=socket.MSG_CTRUNC) @testCmsgTruncNoBufSize.client_skip def _testCmsgTruncNoBufSize(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testSingleCmsgTrunc0(self): # Check that no ancillary data is received when ancillary # buffer size is zero. self.checkHopLimitTruncatedHeader(ancbufsize=0, ignoreflags=socket.MSG_CTRUNC) @testSingleCmsgTrunc0.client_skip def _testSingleCmsgTrunc0(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) # Check that no ancillary data is returned for various non-zero # (but still too small) buffer sizes. @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testSingleCmsgTrunc1(self): self.checkHopLimitTruncatedHeader(ancbufsize=1) @testSingleCmsgTrunc1.client_skip def _testSingleCmsgTrunc1(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testSingleCmsgTrunc2Int(self): self.checkHopLimitTruncatedHeader(ancbufsize=2 * SIZEOF_INT) @testSingleCmsgTrunc2Int.client_skip def _testSingleCmsgTrunc2Int(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testSingleCmsgTruncLen0Minus1(self): self.checkHopLimitTruncatedHeader(ancbufsize=socket.CMSG_LEN(0) - 1) @testSingleCmsgTruncLen0Minus1.client_skip def _testSingleCmsgTruncLen0Minus1(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testSingleCmsgTruncInData(self): # Test truncation of a control message inside its associated # data. The message may be returned with its data truncated, # or not returned at all. self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.misc_event.set() msg, ancdata, flags, addr = self.doRecvmsg( self.serv_sock, len(MSG), socket.CMSG_LEN(SIZEOF_INT) - 1) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC) self.assertLessEqual(len(ancdata), 1) if ancdata: cmsg_level, cmsg_type, cmsg_data = ancdata[0] self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) self.assertEqual(cmsg_type, socket.IPV6_HOPLIMIT) self.assertLess(len(cmsg_data), SIZEOF_INT) @testSingleCmsgTruncInData.client_skip def _testSingleCmsgTruncInData(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) def checkTruncatedSecondHeader(self, ancbufsize, ignoreflags=0): # Receive traffic class and hop limit into ancbufsize bytes of # ancillary data space, which should be large enough to # contain the first item, but too small to contain the header # of the second. Check that data is MSG, MSG_CTRUNC is set # (unless included in ignoreflags), and only one ancillary # data item is returned. self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVTCLASS, 1) self.misc_event.set() msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), ancbufsize) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC, ignore=ignoreflags) self.assertEqual(len(ancdata), 1) cmsg_level, cmsg_type, cmsg_data = ancdata[0] self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) self.assertIn(cmsg_type, {socket.IPV6_TCLASS, socket.IPV6_HOPLIMIT}) self.assertEqual(len(cmsg_data), SIZEOF_INT) a = array.array("i") a.frombytes(cmsg_data) self.assertGreaterEqual(a[0], 0) self.assertLessEqual(a[0], 255) # Try the above test with various buffer sizes. @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testSecondCmsgTrunc0(self): self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT), ignoreflags=socket.MSG_CTRUNC) @testSecondCmsgTrunc0.client_skip def _testSecondCmsgTrunc0(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testSecondCmsgTrunc1(self): self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT) + 1) @testSecondCmsgTrunc1.client_skip def _testSecondCmsgTrunc1(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testSecondCmsgTrunc2Int(self): self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT) + 2 * SIZEOF_INT) @testSecondCmsgTrunc2Int.client_skip def _testSecondCmsgTrunc2Int(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testSecondCmsgTruncLen0Minus1(self): self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(0) - 1) @testSecondCmsgTruncLen0Minus1.client_skip def _testSecondCmsgTruncLen0Minus1(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testSecomdCmsgTruncInData(self): # Test truncation of the second of two control messages inside # its associated data. self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVTCLASS, 1) self.misc_event.set() msg, ancdata, flags, addr = self.doRecvmsg( self.serv_sock, len(MSG), socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(SIZEOF_INT) - 1) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC) cmsg_types = {socket.IPV6_TCLASS, socket.IPV6_HOPLIMIT} cmsg_level, cmsg_type, cmsg_data = ancdata.pop(0) self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) cmsg_types.remove(cmsg_type) self.assertEqual(len(cmsg_data), SIZEOF_INT) a = array.array("i") a.frombytes(cmsg_data) self.assertGreaterEqual(a[0], 0) self.assertLessEqual(a[0], 255) if ancdata: cmsg_level, cmsg_type, cmsg_data = ancdata.pop(0) self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) cmsg_types.remove(cmsg_type) self.assertLess(len(cmsg_data), SIZEOF_INT) self.assertEqual(ancdata, []) @testSecomdCmsgTruncInData.client_skip def _testSecomdCmsgTruncInData(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) # Derive concrete test classes for different socket types. class SendrecvmsgUDPTestBase(SendrecvmsgDgramFlagsBase, SendrecvmsgConnectionlessBase, ThreadedSocketTestMixin, UDPTestBase): pass @requireAttrs(socket.socket, "sendmsg") @unittest.skipUnless(thread, 'Threading required for this test.') class SendmsgUDPTest(SendmsgConnectionlessTests, SendrecvmsgUDPTestBase): pass @requireAttrs(socket.socket, "recvmsg") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgUDPTest(RecvmsgTests, SendrecvmsgUDPTestBase): pass @requireAttrs(socket.socket, "recvmsg_into") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoUDPTest(RecvmsgIntoTests, SendrecvmsgUDPTestBase): pass class SendrecvmsgUDP6TestBase(SendrecvmsgDgramFlagsBase, SendrecvmsgConnectionlessBase, ThreadedSocketTestMixin, UDP6TestBase): def checkRecvmsgAddress(self, addr1, addr2): # Called to compare the received address with the address of # the peer, ignoring scope ID self.assertEqual(addr1[:-1], addr2[:-1]) @requireAttrs(socket.socket, "sendmsg") @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') @requireSocket("AF_INET6", "SOCK_DGRAM") @unittest.skipUnless(thread, 'Threading required for this test.') class SendmsgUDP6Test(SendmsgConnectionlessTests, SendrecvmsgUDP6TestBase): pass @requireAttrs(socket.socket, "recvmsg") @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') @requireSocket("AF_INET6", "SOCK_DGRAM") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgUDP6Test(RecvmsgTests, SendrecvmsgUDP6TestBase): pass @requireAttrs(socket.socket, "recvmsg_into") @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') @requireSocket("AF_INET6", "SOCK_DGRAM") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoUDP6Test(RecvmsgIntoTests, SendrecvmsgUDP6TestBase): pass @requireAttrs(socket.socket, "recvmsg") @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') @requireAttrs(socket, "IPPROTO_IPV6") @requireSocket("AF_INET6", "SOCK_DGRAM") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgRFC3542AncillaryUDP6Test(RFC3542AncillaryTest, SendrecvmsgUDP6TestBase): pass @requireAttrs(socket.socket, "recvmsg_into") @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') @requireAttrs(socket, "IPPROTO_IPV6") @requireSocket("AF_INET6", "SOCK_DGRAM") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoRFC3542AncillaryUDP6Test(RecvmsgIntoMixin, RFC3542AncillaryTest, SendrecvmsgUDP6TestBase): pass class SendrecvmsgTCPTestBase(SendrecvmsgConnectedBase, ConnectedStreamTestMixin, TCPTestBase): pass @requireAttrs(socket.socket, "sendmsg") @unittest.skipUnless(thread, 'Threading required for this test.') class SendmsgTCPTest(SendmsgStreamTests, SendrecvmsgTCPTestBase): pass @requireAttrs(socket.socket, "recvmsg") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgTCPTest(RecvmsgTests, RecvmsgGenericStreamTests, SendrecvmsgTCPTestBase): pass @requireAttrs(socket.socket, "recvmsg_into") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoTCPTest(RecvmsgIntoTests, RecvmsgGenericStreamTests, SendrecvmsgTCPTestBase): pass class SendrecvmsgSCTPStreamTestBase(SendrecvmsgSCTPFlagsBase, SendrecvmsgConnectedBase, ConnectedStreamTestMixin, SCTPStreamBase): pass @requireAttrs(socket.socket, "sendmsg") @requireSocket("AF_INET", "SOCK_STREAM", "IPPROTO_SCTP") @unittest.skipUnless(thread, 'Threading required for this test.') class SendmsgSCTPStreamTest(SendmsgStreamTests, SendrecvmsgSCTPStreamTestBase): pass @requireAttrs(socket.socket, "recvmsg") @requireSocket("AF_INET", "SOCK_STREAM", "IPPROTO_SCTP") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgSCTPStreamTest(RecvmsgTests, RecvmsgGenericStreamTests, SendrecvmsgSCTPStreamTestBase): def testRecvmsgEOF(self): try: super(RecvmsgSCTPStreamTest, self).testRecvmsgEOF() except OSError as e: if e.errno != errno.ENOTCONN: raise self.skipTest("sporadic ENOTCONN (kernel issue?) - see issue #13876") @requireAttrs(socket.socket, "recvmsg_into") @requireSocket("AF_INET", "SOCK_STREAM", "IPPROTO_SCTP") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoSCTPStreamTest(RecvmsgIntoTests, RecvmsgGenericStreamTests, SendrecvmsgSCTPStreamTestBase): def testRecvmsgEOF(self): try: super(RecvmsgIntoSCTPStreamTest, self).testRecvmsgEOF() except OSError as e: if e.errno != errno.ENOTCONN: raise self.skipTest("sporadic ENOTCONN (kernel issue?) - see issue #13876") class SendrecvmsgUnixStreamTestBase(SendrecvmsgConnectedBase, ConnectedStreamTestMixin, UnixStreamBase): pass @requireAttrs(socket.socket, "sendmsg") @requireAttrs(socket, "AF_UNIX") @unittest.skipUnless(thread, 'Threading required for this test.') class SendmsgUnixStreamTest(SendmsgStreamTests, SendrecvmsgUnixStreamTestBase): pass @requireAttrs(socket.socket, "recvmsg") @requireAttrs(socket, "AF_UNIX") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgUnixStreamTest(RecvmsgTests, RecvmsgGenericStreamTests, SendrecvmsgUnixStreamTestBase): pass @requireAttrs(socket.socket, "recvmsg_into") @requireAttrs(socket, "AF_UNIX") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoUnixStreamTest(RecvmsgIntoTests, RecvmsgGenericStreamTests, SendrecvmsgUnixStreamTestBase): pass @requireAttrs(socket.socket, "sendmsg", "recvmsg") @requireAttrs(socket, "AF_UNIX", "SOL_SOCKET", "SCM_RIGHTS") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgSCMRightsStreamTest(SCMRightsTest, SendrecvmsgUnixStreamTestBase): pass @requireAttrs(socket.socket, "sendmsg", "recvmsg_into") @requireAttrs(socket, "AF_UNIX", "SOL_SOCKET", "SCM_RIGHTS") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoSCMRightsStreamTest(RecvmsgIntoMixin, SCMRightsTest, SendrecvmsgUnixStreamTestBase): pass # Test interrupting the interruptible send/receive methods with a # signal when a timeout is set. These tests avoid having multiple # threads alive during the test so that the OS cannot deliver the # signal to the wrong one. class InterruptedTimeoutBase(unittest.TestCase): # Base class for interrupted send/receive tests. Installs an # empty handler for SIGALRM and removes it on teardown, along with # any scheduled alarms. def setUp(self): super().setUp() orig_alrm_handler = signal.signal(signal.SIGALRM, lambda signum, frame: None) self.addCleanup(signal.signal, signal.SIGALRM, orig_alrm_handler) self.addCleanup(self.setAlarm, 0) # Timeout for socket operations timeout = 4.0 # Provide setAlarm() method to schedule delivery of SIGALRM after # given number of seconds, or cancel it if zero, and an # appropriate time value to use. Use setitimer() if available. if hasattr(signal, "setitimer"): alarm_time = 0.05 def setAlarm(self, seconds): signal.setitimer(signal.ITIMER_REAL, seconds) else: # Old systems may deliver the alarm up to one second early alarm_time = 2 def setAlarm(self, seconds): signal.alarm(seconds) # Require siginterrupt() in order to ensure that system calls are # interrupted by default. @requireAttrs(signal, "siginterrupt") @unittest.skipUnless(hasattr(signal, "alarm") or hasattr(signal, "setitimer"), "Don't have signal.alarm or signal.setitimer") class InterruptedRecvTimeoutTest(InterruptedTimeoutBase, UDPTestBase): # Test interrupting the recv*() methods with signals when a # timeout is set. def setUp(self): super().setUp() self.serv.settimeout(self.timeout) def checkInterruptedRecv(self, func, *args, **kwargs): # Check that func(*args, **kwargs) raises OSError with an # errno of EINTR when interrupted by a signal. self.setAlarm(self.alarm_time) with self.assertRaises(OSError) as cm: func(*args, **kwargs) self.assertNotIsInstance(cm.exception, socket.timeout) self.assertEqual(cm.exception.errno, errno.EINTR) def testInterruptedRecvTimeout(self): self.checkInterruptedRecv(self.serv.recv, 1024) def testInterruptedRecvIntoTimeout(self): self.checkInterruptedRecv(self.serv.recv_into, bytearray(1024)) def testInterruptedRecvfromTimeout(self): self.checkInterruptedRecv(self.serv.recvfrom, 1024) def testInterruptedRecvfromIntoTimeout(self): self.checkInterruptedRecv(self.serv.recvfrom_into, bytearray(1024)) @requireAttrs(socket.socket, "recvmsg") def testInterruptedRecvmsgTimeout(self): self.checkInterruptedRecv(self.serv.recvmsg, 1024) @requireAttrs(socket.socket, "recvmsg_into") def testInterruptedRecvmsgIntoTimeout(self): self.checkInterruptedRecv(self.serv.recvmsg_into, [bytearray(1024)]) # Require siginterrupt() in order to ensure that system calls are # interrupted by default. @requireAttrs(signal, "siginterrupt") @unittest.skipUnless(hasattr(signal, "alarm") or hasattr(signal, "setitimer"), "Don't have signal.alarm or signal.setitimer") @unittest.skipUnless(thread, 'Threading required for this test.') class InterruptedSendTimeoutTest(InterruptedTimeoutBase, ThreadSafeCleanupTestCase, SocketListeningTestMixin, TCPTestBase): # Test interrupting the interruptible send*() methods with signals # when a timeout is set. def setUp(self): super().setUp() self.serv_conn = self.newSocket() self.addCleanup(self.serv_conn.close) # Use a thread to complete the connection, but wait for it to # terminate before running the test, so that there is only one # thread to accept the signal. cli_thread = threading.Thread(target=self.doConnect) cli_thread.start() self.cli_conn, addr = self.serv.accept() self.addCleanup(self.cli_conn.close) cli_thread.join() self.serv_conn.settimeout(self.timeout) def doConnect(self): self.serv_conn.connect(self.serv_addr) def checkInterruptedSend(self, func, *args, **kwargs): # Check that func(*args, **kwargs), run in a loop, raises # OSError with an errno of EINTR when interrupted by a # signal. with self.assertRaises(OSError) as cm: while True: self.setAlarm(self.alarm_time) func(*args, **kwargs) self.assertNotIsInstance(cm.exception, socket.timeout) self.assertEqual(cm.exception.errno, errno.EINTR) # Issue #12958: The following tests have problems on OS X prior to 10.7 @support.requires_mac_ver(10, 7) def testInterruptedSendTimeout(self): self.checkInterruptedSend(self.serv_conn.send, b"a"*512) @support.requires_mac_ver(10, 7) def testInterruptedSendtoTimeout(self): # Passing an actual address here as Python's wrapper for # sendto() doesn't allow passing a zero-length one; POSIX # requires that the address is ignored since the socket is # connection-mode, however. self.checkInterruptedSend(self.serv_conn.sendto, b"a"*512, self.serv_addr) @support.requires_mac_ver(10, 7) @requireAttrs(socket.socket, "sendmsg") def testInterruptedSendmsgTimeout(self): self.checkInterruptedSend(self.serv_conn.sendmsg, [b"a"*512]) @unittest.skipUnless(thread, 'Threading required for this test.') class TCPCloserTest(ThreadedTCPSocketTest): def testClose(self): conn, addr = self.serv.accept() conn.close() sd = self.cli read, write, err = select.select([sd], [], [], 1.0) self.assertEqual(read, [sd]) self.assertEqual(sd.recv(1), b'') # Calling close() many times should be safe. conn.close() conn.close() def _testClose(self): self.cli.connect((HOST, self.port)) time.sleep(1.0) @unittest.skipUnless(hasattr(socket, 'socketpair'), 'test needs socket.socketpair()') @unittest.skipUnless(thread, 'Threading required for this test.') class BasicSocketPairTest(SocketPairTest): def __init__(self, methodName='runTest'): SocketPairTest.__init__(self, methodName=methodName) def _check_defaults(self, sock): self.assertIsInstance(sock, socket.socket) if hasattr(socket, 'AF_UNIX'): self.assertEqual(sock.family, socket.AF_UNIX) else: self.assertEqual(sock.family, socket.AF_INET) self.assertEqual(sock.type, socket.SOCK_STREAM) self.assertEqual(sock.proto, 0) def _testDefaults(self): self._check_defaults(self.cli) def testDefaults(self): self._check_defaults(self.serv) def testRecv(self): msg = self.serv.recv(1024) self.assertEqual(msg, MSG) def _testRecv(self): self.cli.send(MSG) def testSend(self): self.serv.send(MSG) def _testSend(self): msg = self.cli.recv(1024) self.assertEqual(msg, MSG) @unittest.skipUnless(thread, 'Threading required for this test.') class NonBlockingTCPTests(ThreadedTCPSocketTest): def __init__(self, methodName='runTest'): ThreadedTCPSocketTest.__init__(self, methodName=methodName) def testSetBlocking(self): # Testing whether set blocking works self.serv.setblocking(True) self.assertIsNone(self.serv.gettimeout()) self.serv.setblocking(False) self.assertEqual(self.serv.gettimeout(), 0.0) start = time.time() try: self.serv.accept() except OSError: pass end = time.time() self.assertTrue((end - start) < 1.0, "Error setting non-blocking mode.") def _testSetBlocking(self): pass @support.cpython_only def testSetBlocking_overflow(self): # Issue 15989 import _testcapi if _testcapi.UINT_MAX >= _testcapi.ULONG_MAX: self.skipTest('needs UINT_MAX < ULONG_MAX') self.serv.setblocking(False) self.assertEqual(self.serv.gettimeout(), 0.0) self.serv.setblocking(_testcapi.UINT_MAX + 1) self.assertIsNone(self.serv.gettimeout()) _testSetBlocking_overflow = support.cpython_only(_testSetBlocking) @unittest.skipUnless(hasattr(socket, 'SOCK_NONBLOCK'), 'test needs socket.SOCK_NONBLOCK') @support.requires_linux_version(2, 6, 28) def testInitNonBlocking(self): # reinit server socket self.serv.close() self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM | socket.SOCK_NONBLOCK) self.port = support.bind_port(self.serv) self.serv.listen(1) # actual testing start = time.time() try: self.serv.accept() except OSError: pass end = time.time() self.assertTrue((end - start) < 1.0, "Error creating with non-blocking mode.") def _testInitNonBlocking(self): pass def testInheritFlags(self): # Issue #7995: when calling accept() on a listening socket with a # timeout, the resulting socket should not be non-blocking. self.serv.settimeout(10) try: conn, addr = self.serv.accept() message = conn.recv(len(MSG)) finally: conn.close() self.serv.settimeout(None) def _testInheritFlags(self): time.sleep(0.1) self.cli.connect((HOST, self.port)) time.sleep(0.5) self.cli.send(MSG) def testAccept(self): # Testing non-blocking accept self.serv.setblocking(0) try: conn, addr = self.serv.accept() except OSError: pass else: self.fail("Error trying to do non-blocking accept.") read, write, err = select.select([self.serv], [], []) if self.serv in read: conn, addr = self.serv.accept() conn.close() else: self.fail("Error trying to do accept after select.") def _testAccept(self): time.sleep(0.1) self.cli.connect((HOST, self.port)) def testConnect(self): # Testing non-blocking connect conn, addr = self.serv.accept() conn.close() def _testConnect(self): self.cli.settimeout(10) self.cli.connect((HOST, self.port)) def testRecv(self): # Testing non-blocking recv conn, addr = self.serv.accept() conn.setblocking(0) try: msg = conn.recv(len(MSG)) except OSError: pass else: self.fail("Error trying to do non-blocking recv.") read, write, err = select.select([conn], [], []) if conn in read: msg = conn.recv(len(MSG)) conn.close() self.assertEqual(msg, MSG) else: self.fail("Error during select call to non-blocking socket.") def _testRecv(self): self.cli.connect((HOST, self.port)) time.sleep(0.1) self.cli.send(MSG) @unittest.skipUnless(thread, 'Threading required for this test.') class FileObjectClassTestCase(SocketConnectedTest): """Unit tests for the object returned by socket.makefile() self.read_file is the io object returned by makefile() on the client connection. You can read from this file to get output from the server. self.write_file is the io object returned by makefile() on the server connection. You can write to this file to send output to the client. """ bufsize = -1 # Use default buffer size encoding = 'utf-8' errors = 'strict' newline = None read_mode = 'rb' read_msg = MSG write_mode = 'wb' write_msg = MSG def __init__(self, methodName='runTest'): SocketConnectedTest.__init__(self, methodName=methodName) def setUp(self): self.evt1, self.evt2, self.serv_finished, self.cli_finished = [ threading.Event() for i in range(4)] SocketConnectedTest.setUp(self) self.read_file = self.cli_conn.makefile( self.read_mode, self.bufsize, encoding = self.encoding, errors = self.errors, newline = self.newline) def tearDown(self): self.serv_finished.set() self.read_file.close() self.assertTrue(self.read_file.closed) self.read_file = None SocketConnectedTest.tearDown(self) def clientSetUp(self): SocketConnectedTest.clientSetUp(self) self.write_file = self.serv_conn.makefile( self.write_mode, self.bufsize, encoding = self.encoding, errors = self.errors, newline = self.newline) def clientTearDown(self): self.cli_finished.set() self.write_file.close() self.assertTrue(self.write_file.closed) self.write_file = None SocketConnectedTest.clientTearDown(self) def testReadAfterTimeout(self): # Issue #7322: A file object must disallow further reads # after a timeout has occurred. self.cli_conn.settimeout(1) self.read_file.read(3) # First read raises a timeout self.assertRaises(socket.timeout, self.read_file.read, 1) # Second read is disallowed with self.assertRaises(OSError) as ctx: self.read_file.read(1) self.assertIn("cannot read from timed out object", str(ctx.exception)) def _testReadAfterTimeout(self): self.write_file.write(self.write_msg[0:3]) self.write_file.flush() self.serv_finished.wait() def testSmallRead(self): # Performing small file read test first_seg = self.read_file.read(len(self.read_msg)-3) second_seg = self.read_file.read(3) msg = first_seg + second_seg self.assertEqual(msg, self.read_msg) def _testSmallRead(self): self.write_file.write(self.write_msg) self.write_file.flush() def testFullRead(self): # read until EOF msg = self.read_file.read() self.assertEqual(msg, self.read_msg) def _testFullRead(self): self.write_file.write(self.write_msg) self.write_file.close() def testUnbufferedRead(self): # Performing unbuffered file read test buf = type(self.read_msg)() while 1: char = self.read_file.read(1) if not char: break buf += char self.assertEqual(buf, self.read_msg) def _testUnbufferedRead(self): self.write_file.write(self.write_msg) self.write_file.flush() def testReadline(self): # Performing file readline test line = self.read_file.readline() self.assertEqual(line, self.read_msg) def _testReadline(self): self.write_file.write(self.write_msg) self.write_file.flush() def testCloseAfterMakefile(self): # The file returned by makefile should keep the socket open. self.cli_conn.close() # read until EOF msg = self.read_file.read() self.assertEqual(msg, self.read_msg) def _testCloseAfterMakefile(self): self.write_file.write(self.write_msg) self.write_file.flush() def testMakefileAfterMakefileClose(self): self.read_file.close() msg = self.cli_conn.recv(len(MSG)) if isinstance(self.read_msg, str): msg = msg.decode() self.assertEqual(msg, self.read_msg) def _testMakefileAfterMakefileClose(self): self.write_file.write(self.write_msg) self.write_file.flush() def testClosedAttr(self): self.assertTrue(not self.read_file.closed) def _testClosedAttr(self): self.assertTrue(not self.write_file.closed) def testAttributes(self): self.assertEqual(self.read_file.mode, self.read_mode) self.assertEqual(self.read_file.name, self.cli_conn.fileno()) def _testAttributes(self): self.assertEqual(self.write_file.mode, self.write_mode) self.assertEqual(self.write_file.name, self.serv_conn.fileno()) def testRealClose(self): self.read_file.close() self.assertRaises(ValueError, self.read_file.fileno) self.cli_conn.close() self.assertRaises(OSError, self.cli_conn.getsockname) def _testRealClose(self): pass class FileObjectInterruptedTestCase(unittest.TestCase): """Test that the file object correctly handles EINTR internally.""" class MockSocket(object): def __init__(self, recv_funcs=()): # A generator that returns callables that we'll call for each # call to recv(). self._recv_step = iter(recv_funcs) def recv_into(self, buffer): data = next(self._recv_step)() assert len(buffer) >= len(data) buffer[:len(data)] = data return len(data) def _decref_socketios(self): pass def _textiowrap_for_test(self, buffering=-1): raw = socket.SocketIO(self, "r") if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: return raw buffer = io.BufferedReader(raw, buffering) text = io.TextIOWrapper(buffer, None, None) text.mode = "rb" return text @staticmethod def _raise_eintr(): raise OSError(errno.EINTR, "interrupted") def _textiowrap_mock_socket(self, mock, buffering=-1): raw = socket.SocketIO(mock, "r") if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: return raw buffer = io.BufferedReader(raw, buffering) text = io.TextIOWrapper(buffer, None, None) text.mode = "rb" return text def _test_readline(self, size=-1, buffering=-1): mock_sock = self.MockSocket(recv_funcs=[ lambda : b"This is the first line\nAnd the sec", self._raise_eintr, lambda : b"ond line is here\n", lambda : b"", lambda : b"", # XXX(gps): io library does an extra EOF read ]) fo = mock_sock._textiowrap_for_test(buffering=buffering) self.assertEqual(fo.readline(size), "This is the first line\n") self.assertEqual(fo.readline(size), "And the second line is here\n") def _test_read(self, size=-1, buffering=-1): mock_sock = self.MockSocket(recv_funcs=[ lambda : b"This is the first line\nAnd the sec", self._raise_eintr, lambda : b"ond line is here\n", lambda : b"", lambda : b"", # XXX(gps): io library does an extra EOF read ]) expecting = (b"This is the first line\n" b"And the second line is here\n") fo = mock_sock._textiowrap_for_test(buffering=buffering) if buffering == 0: data = b'' else: data = '' expecting = expecting.decode('utf-8') while len(data) != len(expecting): part = fo.read(size) if not part: break data += part self.assertEqual(data, expecting) def test_default(self): self._test_readline() self._test_readline(size=100) self._test_read() self._test_read(size=100) def test_with_1k_buffer(self): self._test_readline(buffering=1024) self._test_readline(size=100, buffering=1024) self._test_read(buffering=1024) self._test_read(size=100, buffering=1024) def _test_readline_no_buffer(self, size=-1): mock_sock = self.MockSocket(recv_funcs=[ lambda : b"a", lambda : b"\n", lambda : b"B", self._raise_eintr, lambda : b"b", lambda : b"", ]) fo = mock_sock._textiowrap_for_test(buffering=0) self.assertEqual(fo.readline(size), b"a\n") self.assertEqual(fo.readline(size), b"Bb") def test_no_buffer(self): self._test_readline_no_buffer() self._test_readline_no_buffer(size=4) self._test_read(buffering=0) self._test_read(size=100, buffering=0) class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase): """Repeat the tests from FileObjectClassTestCase with bufsize==0. In this case (and in this case only), it should be possible to create a file object, read a line from it, create another file object, read another line from it, without loss of data in the first file object's buffer. Note that http.client relies on this when reading multiple requests from the same socket.""" bufsize = 0 # Use unbuffered mode def testUnbufferedReadline(self): # Read a line, create a new file object, read another line with it line = self.read_file.readline() # first line self.assertEqual(line, b"A. " + self.write_msg) # first line self.read_file = self.cli_conn.makefile('rb', 0) line = self.read_file.readline() # second line self.assertEqual(line, b"B. " + self.write_msg) # second line def _testUnbufferedReadline(self): self.write_file.write(b"A. " + self.write_msg) self.write_file.write(b"B. " + self.write_msg) self.write_file.flush() def testMakefileClose(self): # The file returned by makefile should keep the socket open... self.cli_conn.close() msg = self.cli_conn.recv(1024) self.assertEqual(msg, self.read_msg) # ...until the file is itself closed self.read_file.close() self.assertRaises(OSError, self.cli_conn.recv, 1024) def _testMakefileClose(self): self.write_file.write(self.write_msg) self.write_file.flush() def testMakefileCloseSocketDestroy(self): refcount_before = sys.getrefcount(self.cli_conn) self.read_file.close() refcount_after = sys.getrefcount(self.cli_conn) self.assertEqual(refcount_before - 1, refcount_after) def _testMakefileCloseSocketDestroy(self): pass # Non-blocking ops # NOTE: to set `read_file` as non-blocking, we must call # `cli_conn.setblocking` and vice-versa (see setUp / clientSetUp). def testSmallReadNonBlocking(self): self.cli_conn.setblocking(False) self.assertEqual(self.read_file.readinto(bytearray(10)), None) self.assertEqual(self.read_file.read(len(self.read_msg) - 3), None) self.evt1.set() self.evt2.wait(1.0) first_seg = self.read_file.read(len(self.read_msg) - 3) if first_seg is None: # Data not arrived (can happen under Windows), wait a bit time.sleep(0.5) first_seg = self.read_file.read(len(self.read_msg) - 3) buf = bytearray(10) n = self.read_file.readinto(buf) self.assertEqual(n, 3) msg = first_seg + buf[:n] self.assertEqual(msg, self.read_msg) self.assertEqual(self.read_file.readinto(bytearray(16)), None) self.assertEqual(self.read_file.read(1), None) def _testSmallReadNonBlocking(self): self.evt1.wait(1.0) self.write_file.write(self.write_msg) self.write_file.flush() self.evt2.set() # Avoid cloding the socket before the server test has finished, # otherwise system recv() will return 0 instead of EWOULDBLOCK. self.serv_finished.wait(5.0) def testWriteNonBlocking(self): self.cli_finished.wait(5.0) # The client thread can't skip directly - the SkipTest exception # would appear as a failure. if self.serv_skipped: self.skipTest(self.serv_skipped) def _testWriteNonBlocking(self): self.serv_skipped = None self.serv_conn.setblocking(False) # Try to saturate the socket buffer pipe with repeated large writes. BIG = b"x" * support.SOCK_MAX_SIZE LIMIT = 10 # The first write() succeeds since a chunk of data can be buffered n = self.write_file.write(BIG) self.assertGreater(n, 0) for i in range(LIMIT): n = self.write_file.write(BIG) if n is None: # Succeeded break self.assertGreater(n, 0) else: # Let us know that this test didn't manage to establish # the expected conditions. This is not a failure in itself but, # if it happens repeatedly, the test should be fixed. self.serv_skipped = "failed to saturate the socket buffer" class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase): bufsize = 1 # Default-buffered for reading; line-buffered for writing class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase): bufsize = 2 # Exercise the buffering code class UnicodeReadFileObjectClassTestCase(FileObjectClassTestCase): """Tests for socket.makefile() in text mode (rather than binary)""" read_mode = 'r' read_msg = MSG.decode('utf-8') write_mode = 'wb' write_msg = MSG newline = '' class UnicodeWriteFileObjectClassTestCase(FileObjectClassTestCase): """Tests for socket.makefile() in text mode (rather than binary)""" read_mode = 'rb' read_msg = MSG write_mode = 'w' write_msg = MSG.decode('utf-8') newline = '' class UnicodeReadWriteFileObjectClassTestCase(FileObjectClassTestCase): """Tests for socket.makefile() in text mode (rather than binary)""" read_mode = 'r' read_msg = MSG.decode('utf-8') write_mode = 'w' write_msg = MSG.decode('utf-8') newline = '' class NetworkConnectionTest(object): """Prove network connection.""" def clientSetUp(self): # We're inherited below by BasicTCPTest2, which also inherits # BasicTCPTest, which defines self.port referenced below. self.cli = socket.create_connection((HOST, self.port)) self.serv_conn = self.cli class BasicTCPTest2(NetworkConnectionTest, BasicTCPTest): """Tests that NetworkConnection does not break existing TCP functionality. """ class NetworkConnectionNoServer(unittest.TestCase): class MockSocket(socket.socket): def connect(self, *args): raise socket.timeout('timed out') @contextlib.contextmanager def mocked_socket_module(self): """Return a socket which times out on connect""" old_socket = socket.socket socket.socket = self.MockSocket try: yield finally: socket.socket = old_socket def test_connect(self): port = support.find_unused_port() cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(cli.close) with self.assertRaises(OSError) as cm: cli.connect((HOST, port)) self.assertEqual(cm.exception.errno, errno.ECONNREFUSED) def test_create_connection(self): # Issue #9792: errors raised by create_connection() should have # a proper errno attribute. port = support.find_unused_port() with self.assertRaises(OSError) as cm: socket.create_connection((HOST, port)) # Issue #16257: create_connection() calls getaddrinfo() against # 'localhost'. This may result in an IPV6 addr being returned # as well as an IPV4 one: # >>> socket.getaddrinfo('localhost', port, 0, SOCK_STREAM) # >>> [(2, 2, 0, '', ('127.0.0.1', 41230)), # (26, 2, 0, '', ('::1', 41230, 0, 0))] # # create_connection() enumerates through all the addresses returned # and if it doesn't successfully bind to any of them, it propagates # the last exception it encountered. # # On Solaris, ENETUNREACH is returned in this circumstance instead # of ECONNREFUSED. So, if that errno exists, add it to our list of # expected errnos. expected_errnos = [ errno.ECONNREFUSED, ] if hasattr(errno, 'ENETUNREACH'): expected_errnos.append(errno.ENETUNREACH) self.assertIn(cm.exception.errno, expected_errnos) def test_create_connection_timeout(self): # Issue #9792: create_connection() should not recast timeout errors # as generic socket errors. with self.mocked_socket_module(): with self.assertRaises(socket.timeout): socket.create_connection((HOST, 1234)) @unittest.skipUnless(thread, 'Threading required for this test.') class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest): def __init__(self, methodName='runTest'): SocketTCPTest.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): self.source_port = support.find_unused_port() def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) def _justAccept(self): conn, addr = self.serv.accept() conn.close() testFamily = _justAccept def _testFamily(self): self.cli = socket.create_connection((HOST, self.port), timeout=30) self.addCleanup(self.cli.close) self.assertEqual(self.cli.family, 2) testSourceAddress = _justAccept def _testSourceAddress(self): self.cli = socket.create_connection((HOST, self.port), timeout=30, source_address=('', self.source_port)) self.addCleanup(self.cli.close) self.assertEqual(self.cli.getsockname()[1], self.source_port) # The port number being used is sufficient to show that the bind() # call happened. testTimeoutDefault = _justAccept def _testTimeoutDefault(self): # passing no explicit timeout uses socket's global default self.assertTrue(socket.getdefaulttimeout() is None) socket.setdefaulttimeout(42) try: self.cli = socket.create_connection((HOST, self.port)) self.addCleanup(self.cli.close) finally: socket.setdefaulttimeout(None) self.assertEqual(self.cli.gettimeout(), 42) testTimeoutNone = _justAccept def _testTimeoutNone(self): # None timeout means the same as sock.settimeout(None) self.assertTrue(socket.getdefaulttimeout() is None) socket.setdefaulttimeout(30) try: self.cli = socket.create_connection((HOST, self.port), timeout=None) self.addCleanup(self.cli.close) finally: socket.setdefaulttimeout(None) self.assertEqual(self.cli.gettimeout(), None) testTimeoutValueNamed = _justAccept def _testTimeoutValueNamed(self): self.cli = socket.create_connection((HOST, self.port), timeout=30) self.assertEqual(self.cli.gettimeout(), 30) testTimeoutValueNonamed = _justAccept def _testTimeoutValueNonamed(self): self.cli = socket.create_connection((HOST, self.port), 30) self.addCleanup(self.cli.close) self.assertEqual(self.cli.gettimeout(), 30) @unittest.skipUnless(thread, 'Threading required for this test.') class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest): def __init__(self, methodName='runTest'): SocketTCPTest.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): pass def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) def testInsideTimeout(self): conn, addr = self.serv.accept() self.addCleanup(conn.close) time.sleep(3) conn.send(b"done!") testOutsideTimeout = testInsideTimeout def _testInsideTimeout(self): self.cli = sock = socket.create_connection((HOST, self.port)) data = sock.recv(5) self.assertEqual(data, b"done!") def _testOutsideTimeout(self): self.cli = sock = socket.create_connection((HOST, self.port), timeout=1) self.assertRaises(socket.timeout, lambda: sock.recv(5)) class TCPTimeoutTest(SocketTCPTest): def testTCPTimeout(self): def raise_timeout(*args, **kwargs): self.serv.settimeout(1.0) self.serv.accept() self.assertRaises(socket.timeout, raise_timeout, "Error generating a timeout exception (TCP)") def testTimeoutZero(self): ok = False try: self.serv.settimeout(0.0) foo = self.serv.accept() except socket.timeout: self.fail("caught timeout instead of error (TCP)") except OSError: ok = True except: self.fail("caught unexpected exception (TCP)") if not ok: self.fail("accept() returned success when we did not expect it") @unittest.skipUnless(hasattr(signal, 'alarm'), 'test needs signal.alarm()') def testInterruptedTimeout(self): # XXX I don't know how to do this test on MSWindows or any other # plaform that doesn't support signal.alarm() or os.kill(), though # the bug should have existed on all platforms. self.serv.settimeout(5.0) # must be longer than alarm class Alarm(Exception): pass def alarm_handler(signal, frame): raise Alarm old_alarm = signal.signal(signal.SIGALRM, alarm_handler) try: signal.alarm(2) # POSIX allows alarm to be up to 1 second early try: foo = self.serv.accept() except socket.timeout: self.fail("caught timeout instead of Alarm") except Alarm: pass except: self.fail("caught other exception instead of Alarm:" " %s(%s):\n%s" % (sys.exc_info()[:2] + (traceback.format_exc(),))) else: self.fail("nothing caught") finally: signal.alarm(0) # shut off alarm except Alarm: self.fail("got Alarm in wrong place") finally: # no alarm can be pending. Safe to restore old handler. signal.signal(signal.SIGALRM, old_alarm) class UDPTimeoutTest(SocketUDPTest): def testUDPTimeout(self): def raise_timeout(*args, **kwargs): self.serv.settimeout(1.0) self.serv.recv(1024) self.assertRaises(socket.timeout, raise_timeout, "Error generating a timeout exception (UDP)") def testTimeoutZero(self): ok = False try: self.serv.settimeout(0.0) foo = self.serv.recv(1024) except socket.timeout: self.fail("caught timeout instead of error (UDP)") except OSError: ok = True except: self.fail("caught unexpected exception (UDP)") if not ok: self.fail("recv() returned success when we did not expect it") class TestExceptions(unittest.TestCase): def testExceptionTree(self): self.assertTrue(issubclass(OSError, Exception)) self.assertTrue(issubclass(socket.herror, OSError)) self.assertTrue(issubclass(socket.gaierror, OSError)) self.assertTrue(issubclass(socket.timeout, OSError)) @unittest.skipUnless(sys.platform == 'linux', 'Linux specific test') class TestLinuxAbstractNamespace(unittest.TestCase): UNIX_PATH_MAX = 108 def testLinuxAbstractNamespace(self): address = b"\x00python-test-hello\x00\xff" with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s1: s1.bind(address) s1.listen(1) with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s2: s2.connect(s1.getsockname()) with s1.accept()[0] as s3: self.assertEqual(s1.getsockname(), address) self.assertEqual(s2.getpeername(), address) def testMaxName(self): address = b"\x00" + b"h" * (self.UNIX_PATH_MAX - 1) with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: s.bind(address) self.assertEqual(s.getsockname(), address) def testNameOverflow(self): address = "\x00" + "h" * self.UNIX_PATH_MAX with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: self.assertRaises(OSError, s.bind, address) def testStrName(self): # Check that an abstract name can be passed as a string. s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: s.bind("\x00python\x00test\x00") self.assertEqual(s.getsockname(), b"\x00python\x00test\x00") finally: s.close() @unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'test needs socket.AF_UNIX') class TestUnixDomain(unittest.TestCase): def setUp(self): self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) def tearDown(self): self.sock.close() def encoded(self, path): # Return the given path encoded in the file system encoding, # or skip the test if this is not possible. try: return os.fsencode(path) except UnicodeEncodeError: self.skipTest( "Pathname {0!a} cannot be represented in file " "system encoding {1!r}".format( path, sys.getfilesystemencoding())) def bind(self, sock, path): # Bind the socket try: sock.bind(path) except OSError as e: if str(e) == "AF_UNIX path too long": self.skipTest( "Pathname {0!a} is too long to serve as a AF_UNIX path" .format(path)) else: raise def testStrAddr(self): # Test binding to and retrieving a normal string pathname. path = os.path.abspath(support.TESTFN) self.bind(self.sock, path) self.addCleanup(support.unlink, path) self.assertEqual(self.sock.getsockname(), path) def testBytesAddr(self): # Test binding to a bytes pathname. path = os.path.abspath(support.TESTFN) self.bind(self.sock, self.encoded(path)) self.addCleanup(support.unlink, path) self.assertEqual(self.sock.getsockname(), path) def testSurrogateescapeBind(self): # Test binding to a valid non-ASCII pathname, with the # non-ASCII bytes supplied using surrogateescape encoding. path = os.path.abspath(support.TESTFN_UNICODE) b = self.encoded(path) self.bind(self.sock, b.decode("ascii", "surrogateescape")) self.addCleanup(support.unlink, path) self.assertEqual(self.sock.getsockname(), path) def testUnencodableAddr(self): # Test binding to a pathname that cannot be encoded in the # file system encoding. if support.TESTFN_UNENCODABLE is None: self.skipTest("No unencodable filename available") path = os.path.abspath(support.TESTFN_UNENCODABLE) self.bind(self.sock, path) self.addCleanup(support.unlink, path) self.assertEqual(self.sock.getsockname(), path) @unittest.skipUnless(thread, 'Threading required for this test.') class BufferIOTest(SocketConnectedTest): """ Test the buffer versions of socket.recv() and socket.send(). """ def __init__(self, methodName='runTest'): SocketConnectedTest.__init__(self, methodName=methodName) def testRecvIntoArray(self): buf = bytearray(1024) nbytes = self.cli_conn.recv_into(buf) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] self.assertEqual(msg, MSG) def _testRecvIntoArray(self): buf = bytes(MSG) self.serv_conn.send(buf) def testRecvIntoBytearray(self): buf = bytearray(1024) nbytes = self.cli_conn.recv_into(buf) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] self.assertEqual(msg, MSG) _testRecvIntoBytearray = _testRecvIntoArray def testRecvIntoMemoryview(self): buf = bytearray(1024) nbytes = self.cli_conn.recv_into(memoryview(buf)) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] self.assertEqual(msg, MSG) _testRecvIntoMemoryview = _testRecvIntoArray def testRecvFromIntoArray(self): buf = bytearray(1024) nbytes, addr = self.cli_conn.recvfrom_into(buf) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] self.assertEqual(msg, MSG) def _testRecvFromIntoArray(self): buf = bytes(MSG) self.serv_conn.send(buf) def testRecvFromIntoBytearray(self): buf = bytearray(1024) nbytes, addr = self.cli_conn.recvfrom_into(buf) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] self.assertEqual(msg, MSG) _testRecvFromIntoBytearray = _testRecvFromIntoArray def testRecvFromIntoMemoryview(self): buf = bytearray(1024) nbytes, addr = self.cli_conn.recvfrom_into(memoryview(buf)) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] self.assertEqual(msg, MSG) _testRecvFromIntoMemoryview = _testRecvFromIntoArray def testRecvFromIntoSmallBuffer(self): # See issue #20246. buf = bytearray(8) self.assertRaises(ValueError, self.cli_conn.recvfrom_into, buf, 1024) def _testRecvFromIntoSmallBuffer(self): self.serv_conn.send(MSG) def testRecvFromIntoEmptyBuffer(self): buf = bytearray() self.cli_conn.recvfrom_into(buf) self.cli_conn.recvfrom_into(buf, 0) _testRecvFromIntoEmptyBuffer = _testRecvFromIntoArray TIPC_STYPE = 2000 TIPC_LOWER = 200 TIPC_UPPER = 210 def isTipcAvailable(): """Check if the TIPC module is loaded The TIPC module is not loaded automatically on Ubuntu and probably other Linux distros. """ if not hasattr(socket, "AF_TIPC"): return False if not os.path.isfile("/proc/modules"): return False with open("/proc/modules") as f: for line in f: if line.startswith("tipc "): return True return False @unittest.skipUnless(isTipcAvailable(), "TIPC module is not loaded, please 'sudo modprobe tipc'") class TIPCTest(unittest.TestCase): def testRDM(self): srv = socket.socket(socket.AF_TIPC, socket.SOCK_RDM) cli = socket.socket(socket.AF_TIPC, socket.SOCK_RDM) self.addCleanup(srv.close) self.addCleanup(cli.close) srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE, TIPC_LOWER, TIPC_UPPER) srv.bind(srvaddr) sendaddr = (socket.TIPC_ADDR_NAME, TIPC_STYPE, TIPC_LOWER + int((TIPC_UPPER - TIPC_LOWER) / 2), 0) cli.sendto(MSG, sendaddr) msg, recvaddr = srv.recvfrom(1024) self.assertEqual(cli.getsockname(), recvaddr) self.assertEqual(msg, MSG) @unittest.skipUnless(isTipcAvailable(), "TIPC module is not loaded, please 'sudo modprobe tipc'") class TIPCThreadableTest(unittest.TestCase, ThreadableTest): def __init__(self, methodName = 'runTest'): unittest.TestCase.__init__(self, methodName = methodName) ThreadableTest.__init__(self) def setUp(self): self.srv = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM) self.addCleanup(self.srv.close) self.srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE, TIPC_LOWER, TIPC_UPPER) self.srv.bind(srvaddr) self.srv.listen(5) self.serverExplicitReady() self.conn, self.connaddr = self.srv.accept() self.addCleanup(self.conn.close) def clientSetUp(self): # The is a hittable race between serverExplicitReady() and the # accept() call; sleep a little while to avoid it, otherwise # we could get an exception time.sleep(0.1) self.cli = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM) self.addCleanup(self.cli.close) addr = (socket.TIPC_ADDR_NAME, TIPC_STYPE, TIPC_LOWER + int((TIPC_UPPER - TIPC_LOWER) / 2), 0) self.cli.connect(addr) self.cliaddr = self.cli.getsockname() def testStream(self): msg = self.conn.recv(1024) self.assertEqual(msg, MSG) self.assertEqual(self.cliaddr, self.connaddr) def _testStream(self): self.cli.send(MSG) self.cli.close() @unittest.skipUnless(thread, 'Threading required for this test.') class ContextManagersTest(ThreadedTCPSocketTest): def _testSocketClass(self): # base test with socket.socket() as sock: self.assertFalse(sock._closed) self.assertTrue(sock._closed) # close inside with block with socket.socket() as sock: sock.close() self.assertTrue(sock._closed) # exception inside with block with socket.socket() as sock: self.assertRaises(OSError, sock.sendall, b'foo') self.assertTrue(sock._closed) def testCreateConnectionBase(self): conn, addr = self.serv.accept() self.addCleanup(conn.close) data = conn.recv(1024) conn.sendall(data) def _testCreateConnectionBase(self): address = self.serv.getsockname() with socket.create_connection(address) as sock: self.assertFalse(sock._closed) sock.sendall(b'foo') self.assertEqual(sock.recv(1024), b'foo') self.assertTrue(sock._closed) def testCreateConnectionClose(self): conn, addr = self.serv.accept() self.addCleanup(conn.close) data = conn.recv(1024) conn.sendall(data) def _testCreateConnectionClose(self): address = self.serv.getsockname() with socket.create_connection(address) as sock: sock.close() self.assertTrue(sock._closed) self.assertRaises(OSError, sock.sendall, b'foo') class InheritanceTest(unittest.TestCase): @unittest.skipUnless(hasattr(socket, "SOCK_CLOEXEC"), "SOCK_CLOEXEC not defined") @support.requires_linux_version(2, 6, 28) def test_SOCK_CLOEXEC(self): with socket.socket(socket.AF_INET, socket.SOCK_STREAM | socket.SOCK_CLOEXEC) as s: self.assertTrue(s.type & socket.SOCK_CLOEXEC) self.assertFalse(s.get_inheritable()) def test_default_inheritable(self): sock = socket.socket() with sock: self.assertEqual(sock.get_inheritable(), False) def test_dup(self): sock = socket.socket() with sock: newsock = sock.dup() sock.close() with newsock: self.assertEqual(newsock.get_inheritable(), False) def test_set_inheritable(self): sock = socket.socket() with sock: sock.set_inheritable(True) self.assertEqual(sock.get_inheritable(), True) sock.set_inheritable(False) self.assertEqual(sock.get_inheritable(), False) @unittest.skipIf(fcntl is None, "need fcntl") def test_get_inheritable_cloexec(self): sock = socket.socket() with sock: fd = sock.fileno() self.assertEqual(sock.get_inheritable(), False) # clear FD_CLOEXEC flag flags = fcntl.fcntl(fd, fcntl.F_GETFD) flags &= ~fcntl.FD_CLOEXEC fcntl.fcntl(fd, fcntl.F_SETFD, flags) self.assertEqual(sock.get_inheritable(), True) @unittest.skipIf(fcntl is None, "need fcntl") def test_set_inheritable_cloexec(self): sock = socket.socket() with sock: fd = sock.fileno() self.assertEqual(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC, fcntl.FD_CLOEXEC) sock.set_inheritable(True) self.assertEqual(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC, 0) @unittest.skipUnless(hasattr(socket, "socketpair"), "need socket.socketpair()") def test_socketpair(self): s1, s2 = socket.socketpair() self.addCleanup(s1.close) self.addCleanup(s2.close) self.assertEqual(s1.get_inheritable(), False) self.assertEqual(s2.get_inheritable(), False) @unittest.skipUnless(hasattr(socket, "SOCK_NONBLOCK"), "SOCK_NONBLOCK not defined") class NonblockConstantTest(unittest.TestCase): def checkNonblock(self, s, nonblock=True, timeout=0.0): if nonblock: self.assertTrue(s.type & socket.SOCK_NONBLOCK) self.assertEqual(s.gettimeout(), timeout) else: self.assertFalse(s.type & socket.SOCK_NONBLOCK) self.assertEqual(s.gettimeout(), None) @support.requires_linux_version(2, 6, 28) def test_SOCK_NONBLOCK(self): # a lot of it seems silly and redundant, but I wanted to test that # changing back and forth worked ok with socket.socket(socket.AF_INET, socket.SOCK_STREAM | socket.SOCK_NONBLOCK) as s: self.checkNonblock(s) s.setblocking(1) self.checkNonblock(s, False) s.setblocking(0) self.checkNonblock(s) s.settimeout(None) self.checkNonblock(s, False) s.settimeout(2.0) self.checkNonblock(s, timeout=2.0) s.setblocking(1) self.checkNonblock(s, False) # defaulttimeout t = socket.getdefaulttimeout() socket.setdefaulttimeout(0.0) with socket.socket() as s: self.checkNonblock(s) socket.setdefaulttimeout(None) with socket.socket() as s: self.checkNonblock(s, False) socket.setdefaulttimeout(2.0) with socket.socket() as s: self.checkNonblock(s, timeout=2.0) socket.setdefaulttimeout(None) with socket.socket() as s: self.checkNonblock(s, False) socket.setdefaulttimeout(t) @unittest.skipUnless(os.name == "nt", "Windows specific") @unittest.skipUnless(multiprocessing, "need multiprocessing") class TestSocketSharing(SocketTCPTest): # This must be classmethod and not staticmethod or multiprocessing # won't be able to bootstrap it. @classmethod def remoteProcessServer(cls, q): # Recreate socket from shared data sdata = q.get() message = q.get() s = socket.fromshare(sdata) s2, c = s.accept() # Send the message s2.sendall(message) s2.close() s.close() def testShare(self): # Transfer the listening server socket to another process # and service it from there. # Create process: q = multiprocessing.Queue() p = multiprocessing.Process(target=self.remoteProcessServer, args=(q,)) p.start() # Get the shared socket data data = self.serv.share(p.pid) # Pass the shared socket to the other process addr = self.serv.getsockname() self.serv.close() q.put(data) # The data that the server will send us message = b"slapmahfro" q.put(message) # Connect s = socket.create_connection(addr) # listen for the data m = [] while True: data = s.recv(100) if not data: break m.append(data) s.close() received = b"".join(m) self.assertEqual(received, message) p.join() def testShareLength(self): data = self.serv.share(os.getpid()) self.assertRaises(ValueError, socket.fromshare, data[:-1]) self.assertRaises(ValueError, socket.fromshare, data+b"foo") def compareSockets(self, org, other): # socket sharing is expected to work only for blocking socket # since the internal python timout value isn't transfered. self.assertEqual(org.gettimeout(), None) self.assertEqual(org.gettimeout(), other.gettimeout()) self.assertEqual(org.family, other.family) self.assertEqual(org.type, other.type) # If the user specified "0" for proto, then # internally windows will have picked the correct value. # Python introspection on the socket however will still return # 0. For the shared socket, the python value is recreated # from the actual value, so it may not compare correctly. if org.proto != 0: self.assertEqual(org.proto, other.proto) def testShareLocal(self): data = self.serv.share(os.getpid()) s = socket.fromshare(data) try: self.compareSockets(self.serv, s) finally: s.close() def testTypes(self): families = [socket.AF_INET, socket.AF_INET6] types = [socket.SOCK_STREAM, socket.SOCK_DGRAM] for f in families: for t in types: try: source = socket.socket(f, t) except OSError: continue # This combination is not supported try: data = source.share(os.getpid()) shared = socket.fromshare(data) try: self.compareSockets(source, shared) finally: shared.close() finally: source.close() def test_main(): tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest, TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, UDPTimeoutTest ] tests.extend([ NonBlockingTCPTests, FileObjectClassTestCase, FileObjectInterruptedTestCase, UnbufferedFileObjectClassTestCase, LineBufferedFileObjectClassTestCase, SmallBufferedFileObjectClassTestCase, UnicodeReadFileObjectClassTestCase, UnicodeWriteFileObjectClassTestCase, UnicodeReadWriteFileObjectClassTestCase, NetworkConnectionNoServer, NetworkConnectionAttributesTest, NetworkConnectionBehaviourTest, ContextManagersTest, InheritanceTest, NonblockConstantTest ]) tests.append(BasicSocketPairTest) tests.append(TestUnixDomain) tests.append(TestLinuxAbstractNamespace) tests.extend([TIPCTest, TIPCThreadableTest]) tests.extend([BasicCANTest, CANTest]) tests.extend([BasicRDSTest, RDSTest]) tests.extend([ CmsgMacroTests, SendmsgUDPTest, RecvmsgUDPTest, RecvmsgIntoUDPTest, SendmsgUDP6Test, RecvmsgUDP6Test, RecvmsgRFC3542AncillaryUDP6Test, RecvmsgIntoRFC3542AncillaryUDP6Test, RecvmsgIntoUDP6Test, SendmsgTCPTest, RecvmsgTCPTest, RecvmsgIntoTCPTest, SendmsgSCTPStreamTest, RecvmsgSCTPStreamTest, RecvmsgIntoSCTPStreamTest, SendmsgUnixStreamTest, RecvmsgUnixStreamTest, RecvmsgIntoUnixStreamTest, RecvmsgSCMRightsStreamTest, RecvmsgIntoSCMRightsStreamTest, # These are slow when setitimer() is not available InterruptedRecvTimeoutTest, InterruptedSendTimeoutTest, TestSocketSharing, ]) thread_info = support.threading_setup() support.run_unittest(*tests) support.threading_cleanup(*thread_info) if __name__ == "__main__": test_main() ======= import unittest from test import support import errno import io import itertools import socket import select import tempfile import time import traceback import queue import sys import os import array import platform import contextlib from weakref import proxy import signal import math import pickle import struct try: import multiprocessing except ImportError: multiprocessing = False try: import fcntl except ImportError: fcntl = None HOST = support.HOST MSG = 'Michael Gilfix was here\u1234\r\n'.encode('utf-8') ## test unicode string and carriage return try: import _thread as thread import threading except ImportError: thread = None threading = None try: import _socket except ImportError: _socket = None def _have_socket_can(): """Check whether CAN sockets are supported on this host.""" try: s = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) except (AttributeError, OSError): return False else: s.close() return True def _have_socket_rds(): """Check whether RDS sockets are supported on this host.""" try: s = socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) except (AttributeError, OSError): return False else: s.close() return True HAVE_SOCKET_CAN = _have_socket_can() HAVE_SOCKET_RDS = _have_socket_rds() # Size in bytes of the int type SIZEOF_INT = array.array("i").itemsize class SocketTCPTest(unittest.TestCase): def setUp(self): self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.port = support.bind_port(self.serv) self.serv.listen(1) def tearDown(self): self.serv.close() self.serv = None class SocketUDPTest(unittest.TestCase): def setUp(self): self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.port = support.bind_port(self.serv) def tearDown(self): self.serv.close() self.serv = None class ThreadSafeCleanupTestCase(unittest.TestCase): """Subclass of unittest.TestCase with thread-safe cleanup methods. This subclass protects the addCleanup() and doCleanups() methods with a recursive lock. """ if threading: def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._cleanup_lock = threading.RLock() def addCleanup(self, *args, **kwargs): with self._cleanup_lock: return super().addCleanup(*args, **kwargs) def doCleanups(self, *args, **kwargs): with self._cleanup_lock: return super().doCleanups(*args, **kwargs) class SocketCANTest(unittest.TestCase): """To be able to run this test, a `vcan0` CAN interface can be created with the following commands: # modprobe vcan # ip link add dev vcan0 type vcan # ifconfig vcan0 up """ interface = 'vcan0' bufsize = 128 """The CAN frame structure is defined in <linux/can.h>: struct can_frame { canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ __u8 can_dlc; /* data length code: 0 .. 8 */ __u8 data[8] __attribute__((aligned(8))); }; """ can_frame_fmt = "=IB3x8s" can_frame_size = struct.calcsize(can_frame_fmt) """The Broadcast Management Command frame structure is defined in <linux/can/bcm.h>: struct bcm_msg_head { __u32 opcode; __u32 flags; __u32 count; struct timeval ival1, ival2; canid_t can_id; __u32 nframes; struct can_frame frames[0]; } `bcm_msg_head` must be 8 bytes aligned because of the `frames` member (see `struct can_frame` definition). Must use native not standard types for packing. """ bcm_cmd_msg_fmt = "@3I4l2I" bcm_cmd_msg_fmt += "x" * (struct.calcsize(bcm_cmd_msg_fmt) % 8) def setUp(self): self.s = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) self.addCleanup(self.s.close) try: self.s.bind((self.interface,)) except OSError: self.skipTest('network interface `%s` does not exist' % self.interface) class SocketRDSTest(unittest.TestCase): """To be able to run this test, the `rds` kernel module must be loaded: # modprobe rds """ bufsize = 8192 def setUp(self): self.serv = socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) self.addCleanup(self.serv.close) try: self.port = support.bind_port(self.serv) except OSError: self.skipTest('unable to bind RDS socket') class ThreadableTest: """Threadable Test class The ThreadableTest class makes it easy to create a threaded client/server pair from an existing unit test. To create a new threaded class from an existing unit test, use multiple inheritance: class NewClass (OldClass, ThreadableTest): pass This class defines two new fixture functions with obvious purposes for overriding: clientSetUp () clientTearDown () Any new test functions within the class must then define tests in pairs, where the test name is preceeded with a '_' to indicate the client portion of the test. Ex: def testFoo(self): # Server portion def _testFoo(self): # Client portion Any exceptions raised by the clients during their tests are caught and transferred to the main thread to alert the testing framework. Note, the server setup function cannot call any blocking functions that rely on the client thread during setup, unless serverExplicitReady() is called just before the blocking call (such as in setting up a client/server connection and performing the accept() in setUp(). """ def __init__(self): # Swap the true setup function self.__setUp = self.setUp self.__tearDown = self.tearDown self.setUp = self._setUp self.tearDown = self._tearDown def serverExplicitReady(self): """This method allows the server to explicitly indicate that it wants the client thread to proceed. This is useful if the server is about to execute a blocking routine that is dependent upon the client thread during its setup routine.""" self.server_ready.set() def _setUp(self): self.server_ready = threading.Event() self.client_ready = threading.Event() self.done = threading.Event() self.queue = queue.Queue(1) self.server_crashed = False # Do some munging to start the client test. methodname = self.id() i = methodname.rfind('.') methodname = methodname[i+1:] test_method = getattr(self, '_' + methodname) self.client_thread = thread.start_new_thread( self.clientRun, (test_method,)) try: self.__setUp() except: self.server_crashed = True raise finally: self.server_ready.set() self.client_ready.wait() def _tearDown(self): self.__tearDown() self.done.wait() if self.queue.qsize(): exc = self.queue.get() raise exc def clientRun(self, test_func): self.server_ready.wait() self.clientSetUp() self.client_ready.set() if self.server_crashed: self.clientTearDown() return if not hasattr(test_func, '__call__'): raise TypeError("test_func must be a callable function") try: test_func() except BaseException as e: self.queue.put(e) finally: self.clientTearDown() def clientSetUp(self): raise NotImplementedError("clientSetUp must be implemented.") def clientTearDown(self): self.done.set() thread.exit() class ThreadedTCPSocketTest(SocketTCPTest, ThreadableTest): def __init__(self, methodName='runTest'): SocketTCPTest.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): self.cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) class ThreadedUDPSocketTest(SocketUDPTest, ThreadableTest): def __init__(self, methodName='runTest'): SocketUDPTest.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): self.cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) class ThreadedCANSocketTest(SocketCANTest, ThreadableTest): def __init__(self, methodName='runTest'): SocketCANTest.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): self.cli = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) try: self.cli.bind((self.interface,)) except OSError: # skipTest should not be called here, and will be called in the # server instead pass def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) class ThreadedRDSSocketTest(SocketRDSTest, ThreadableTest): def __init__(self, methodName='runTest'): SocketRDSTest.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): self.cli = socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) try: # RDS sockets must be bound explicitly to send or receive data self.cli.bind((HOST, 0)) self.cli_addr = self.cli.getsockname() except OSError: # skipTest should not be called here, and will be called in the # server instead pass def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) class SocketConnectedTest(ThreadedTCPSocketTest): """Socket tests for client-server connection. self.cli_conn is a client socket connected to the server. The setUp() method guarantees that it is connected to the server. """ def __init__(self, methodName='runTest'): ThreadedTCPSocketTest.__init__(self, methodName=methodName) def setUp(self): ThreadedTCPSocketTest.setUp(self) # Indicate explicitly we're ready for the client thread to # proceed and then perform the blocking call to accept self.serverExplicitReady() conn, addr = self.serv.accept() self.cli_conn = conn def tearDown(self): self.cli_conn.close() self.cli_conn = None ThreadedTCPSocketTest.tearDown(self) def clientSetUp(self): ThreadedTCPSocketTest.clientSetUp(self) self.cli.connect((HOST, self.port)) self.serv_conn = self.cli def clientTearDown(self): self.serv_conn.close() self.serv_conn = None ThreadedTCPSocketTest.clientTearDown(self) class SocketPairTest(unittest.TestCase, ThreadableTest): def __init__(self, methodName='runTest'): unittest.TestCase.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def setUp(self): self.serv, self.cli = socket.socketpair() def tearDown(self): self.serv.close() self.serv = None def clientSetUp(self): pass def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) # The following classes are used by the sendmsg()/recvmsg() tests. # Combining, for instance, ConnectedStreamTestMixin and TCPTestBase # gives a drop-in replacement for SocketConnectedTest, but different # address families can be used, and the attributes serv_addr and # cli_addr will be set to the addresses of the endpoints. class SocketTestBase(unittest.TestCase): """A base class for socket tests. Subclasses must provide methods newSocket() to return a new socket and bindSock(sock) to bind it to an unused address. Creates a socket self.serv and sets self.serv_addr to its address. """ def setUp(self): self.serv = self.newSocket() self.bindServer() def bindServer(self): """Bind server socket and set self.serv_addr to its address.""" self.bindSock(self.serv) self.serv_addr = self.serv.getsockname() def tearDown(self): self.serv.close() self.serv = None class SocketListeningTestMixin(SocketTestBase): """Mixin to listen on the server socket.""" def setUp(self): super().setUp() self.serv.listen(1) class ThreadedSocketTestMixin(ThreadSafeCleanupTestCase, SocketTestBase, ThreadableTest): """Mixin to add client socket and allow client/server tests. Client socket is self.cli and its address is self.cli_addr. See ThreadableTest for usage information. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) ThreadableTest.__init__(self) def clientSetUp(self): self.cli = self.newClientSocket() self.bindClient() def newClientSocket(self): """Return a new socket for use as client.""" return self.newSocket() def bindClient(self): """Bind client socket and set self.cli_addr to its address.""" self.bindSock(self.cli) self.cli_addr = self.cli.getsockname() def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) class ConnectedStreamTestMixin(SocketListeningTestMixin, ThreadedSocketTestMixin): """Mixin to allow client/server stream tests with connected client. Server's socket representing connection to client is self.cli_conn and client's connection to server is self.serv_conn. (Based on SocketConnectedTest.) """ def setUp(self): super().setUp() # Indicate explicitly we're ready for the client thread to # proceed and then perform the blocking call to accept self.serverExplicitReady() conn, addr = self.serv.accept() self.cli_conn = conn def tearDown(self): self.cli_conn.close() self.cli_conn = None super().tearDown() def clientSetUp(self): super().clientSetUp() self.cli.connect(self.serv_addr) self.serv_conn = self.cli def clientTearDown(self): self.serv_conn.close() self.serv_conn = None super().clientTearDown() class UnixSocketTestBase(SocketTestBase): """Base class for Unix-domain socket tests.""" # This class is used for file descriptor passing tests, so we # create the sockets in a private directory so that other users # can't send anything that might be problematic for a privileged # user running the tests. def setUp(self): self.dir_path = tempfile.mkdtemp() self.addCleanup(os.rmdir, self.dir_path) super().setUp() def bindSock(self, sock): path = tempfile.mktemp(dir=self.dir_path) sock.bind(path) self.addCleanup(support.unlink, path) class UnixStreamBase(UnixSocketTestBase): """Base class for Unix-domain SOCK_STREAM tests.""" def newSocket(self): return socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) class InetTestBase(SocketTestBase): """Base class for IPv4 socket tests.""" host = HOST def setUp(self): super().setUp() self.port = self.serv_addr[1] def bindSock(self, sock): support.bind_port(sock, host=self.host) class TCPTestBase(InetTestBase): """Base class for TCP-over-IPv4 tests.""" def newSocket(self): return socket.socket(socket.AF_INET, socket.SOCK_STREAM) class UDPTestBase(InetTestBase): """Base class for UDP-over-IPv4 tests.""" def newSocket(self): return socket.socket(socket.AF_INET, socket.SOCK_DGRAM) class SCTPStreamBase(InetTestBase): """Base class for SCTP tests in one-to-one (SOCK_STREAM) mode.""" def newSocket(self): return socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_SCTP) class Inet6TestBase(InetTestBase): """Base class for IPv6 socket tests.""" host = support.HOSTv6 class UDP6TestBase(Inet6TestBase): """Base class for UDP-over-IPv6 tests.""" def newSocket(self): return socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) # Test-skipping decorators for use with ThreadableTest. def skipWithClientIf(condition, reason): """Skip decorated test if condition is true, add client_skip decorator. If the decorated object is not a class, sets its attribute "client_skip" to a decorator which will return an empty function if the test is to be skipped, or the original function if it is not. This can be used to avoid running the client part of a skipped test when using ThreadableTest. """ def client_pass(*args, **kwargs): pass def skipdec(obj): retval = unittest.skip(reason)(obj) if not isinstance(obj, type): retval.client_skip = lambda f: client_pass return retval def noskipdec(obj): if not (isinstance(obj, type) or hasattr(obj, "client_skip")): obj.client_skip = lambda f: f return obj return skipdec if condition else noskipdec def requireAttrs(obj, *attributes): """Skip decorated test if obj is missing any of the given attributes. Sets client_skip attribute as skipWithClientIf() does. """ missing = [name for name in attributes if not hasattr(obj, name)] return skipWithClientIf( missing, "don't have " + ", ".join(name for name in missing)) def requireSocket(*args): """Skip decorated test if a socket cannot be created with given arguments. When an argument is given as a string, will use the value of that attribute of the socket module, or skip the test if it doesn't exist. Sets client_skip attribute as skipWithClientIf() does. """ err = None missing = [obj for obj in args if isinstance(obj, str) and not hasattr(socket, obj)] if missing: err = "don't have " + ", ".join(name for name in missing) else: callargs = [getattr(socket, obj) if isinstance(obj, str) else obj for obj in args] try: s = socket.socket(*callargs) except OSError as e: # XXX: check errno? err = str(e) else: s.close() return skipWithClientIf( err is not None, "can't create socket({0}): {1}".format( ", ".join(str(o) for o in args), err)) ####################################################################### ## Begin Tests class GeneralModuleTests(unittest.TestCase): def test_SocketType_is_socketobject(self): import _socket self.assertTrue(socket.SocketType is _socket.socket) s = socket.socket() self.assertIsInstance(s, socket.SocketType) s.close() def test_repr(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) with s: self.assertIn('fd=%i' % s.fileno(), repr(s)) self.assertIn('family=%s' % socket.AF_INET, repr(s)) self.assertIn('type=%s' % socket.SOCK_STREAM, repr(s)) self.assertIn('proto=0', repr(s)) self.assertNotIn('raddr', repr(s)) s.bind(('127.0.0.1', 0)) self.assertIn('laddr', repr(s)) self.assertIn(str(s.getsockname()), repr(s)) self.assertIn('[closed]', repr(s)) self.assertNotIn('laddr', repr(s)) @unittest.skipUnless(_socket is not None, 'need _socket module') def test_csocket_repr(self): s = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM) try: expected = ('<socket object, fd=%s, family=%s, type=%s, proto=%s>' % (s.fileno(), s.family, s.type, s.proto)) self.assertEqual(repr(s), expected) finally: s.close() expected = ('<socket object, fd=-1, family=%s, type=%s, proto=%s>' % (s.family, s.type, s.proto)) self.assertEqual(repr(s), expected) def test_weakref(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) p = proxy(s) self.assertEqual(p.fileno(), s.fileno()) s.close() s = None try: p.fileno() except ReferenceError: pass else: self.fail('Socket proxy still exists') def testSocketError(self): # Testing socket module exceptions msg = "Error raising socket exception (%s)." with self.assertRaises(OSError, msg=msg % 'OSError'): raise OSError with self.assertRaises(OSError, msg=msg % 'socket.herror'): raise socket.herror with self.assertRaises(OSError, msg=msg % 'socket.gaierror'): raise socket.gaierror def testSendtoErrors(self): # Testing that sendto doens't masks failures. See #10169. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.addCleanup(s.close) s.bind(('', 0)) sockname = s.getsockname() # 2 args with self.assertRaises(TypeError) as cm: s.sendto('\u2620', sockname) self.assertEqual(str(cm.exception), "'str' does not support the buffer interface") with self.assertRaises(TypeError) as cm: s.sendto(5j, sockname) self.assertEqual(str(cm.exception), "'complex' does not support the buffer interface") with self.assertRaises(TypeError) as cm: s.sendto(b'foo', None) self.assertIn('not NoneType',str(cm.exception)) # 3 args with self.assertRaises(TypeError) as cm: s.sendto('\u2620', 0, sockname) self.assertEqual(str(cm.exception), "'str' does not support the buffer interface") with self.assertRaises(TypeError) as cm: s.sendto(5j, 0, sockname) self.assertEqual(str(cm.exception), "'complex' does not support the buffer interface") with self.assertRaises(TypeError) as cm: s.sendto(b'foo', 0, None) self.assertIn('not NoneType', str(cm.exception)) with self.assertRaises(TypeError) as cm: s.sendto(b'foo', 'bar', sockname) self.assertIn('an integer is required', str(cm.exception)) with self.assertRaises(TypeError) as cm: s.sendto(b'foo', None, None) self.assertIn('an integer is required', str(cm.exception)) # wrong number of args with self.assertRaises(TypeError) as cm: s.sendto(b'foo') self.assertIn('(1 given)', str(cm.exception)) with self.assertRaises(TypeError) as cm: s.sendto(b'foo', 0, sockname, 4) self.assertIn('(4 given)', str(cm.exception)) def testCrucialConstants(self): # Testing for mission critical constants socket.AF_INET socket.SOCK_STREAM socket.SOCK_DGRAM socket.SOCK_RAW socket.SOCK_RDM socket.SOCK_SEQPACKET socket.SOL_SOCKET socket.SO_REUSEADDR def testHostnameRes(self): # Testing hostname resolution mechanisms hostname = socket.gethostname() try: ip = socket.gethostbyname(hostname) except OSError: # Probably name lookup wasn't set up right; skip this test self.skipTest('name lookup failure') self.assertTrue(ip.find('.') >= 0, "Error resolving host to ip.") try: hname, aliases, ipaddrs = socket.gethostbyaddr(ip) except OSError: # Probably a similar problem as above; skip this test self.skipTest('name lookup failure') all_host_names = [hostname, hname] + aliases fqhn = socket.getfqdn(ip) if not fqhn in all_host_names: self.fail("Error testing host resolution mechanisms. (fqdn: %s, all: %s)" % (fqhn, repr(all_host_names))) def test_host_resolution(self): for addr in ['0.1.1.~1', '1+.1.1.1', '::1q', '::1::2', '1:1:1:1:1:1:1:1:1']: self.assertRaises(OSError, socket.gethostbyname, addr) self.assertRaises(OSError, socket.gethostbyaddr, addr) for addr in [support.HOST, '10.0.0.1', '255.255.255.255']: self.assertEqual(socket.gethostbyname(addr), addr) # we don't test support.HOSTv6 because there's a chance it doesn't have # a matching name entry (e.g. 'ip6-localhost') for host in [support.HOST]: self.assertIn(host, socket.gethostbyaddr(host)[2]) @unittest.skipUnless(hasattr(socket, 'sethostname'), "test needs socket.sethostname()") @unittest.skipUnless(hasattr(socket, 'gethostname'), "test needs socket.gethostname()") def test_sethostname(self): oldhn = socket.gethostname() try: socket.sethostname('new') except OSError as e: if e.errno == errno.EPERM: self.skipTest("test should be run as root") else: raise try: # running test as root! self.assertEqual(socket.gethostname(), 'new') # Should work with bytes objects too socket.sethostname(b'bar') self.assertEqual(socket.gethostname(), 'bar') finally: socket.sethostname(oldhn) @unittest.skipUnless(hasattr(socket, 'if_nameindex'), 'socket.if_nameindex() not available.') def testInterfaceNameIndex(self): interfaces = socket.if_nameindex() for index, name in interfaces: self.assertIsInstance(index, int) self.assertIsInstance(name, str) # interface indices are non-zero integers self.assertGreater(index, 0) _index = socket.if_nametoindex(name) self.assertIsInstance(_index, int) self.assertEqual(index, _index) _name = socket.if_indextoname(index) self.assertIsInstance(_name, str) self.assertEqual(name, _name) @unittest.skipUnless(hasattr(socket, 'if_nameindex'), 'socket.if_nameindex() not available.') def testInvalidInterfaceNameIndex(self): # test nonexistent interface index/name self.assertRaises(OSError, socket.if_indextoname, 0) self.assertRaises(OSError, socket.if_nametoindex, '_DEADBEEF') # test with invalid values self.assertRaises(TypeError, socket.if_nametoindex, 0) self.assertRaises(TypeError, socket.if_indextoname, '_DEADBEEF') @unittest.skipUnless(hasattr(sys, 'getrefcount'), 'test needs sys.getrefcount()') def testRefCountGetNameInfo(self): # Testing reference count for getnameinfo try: # On some versions, this loses a reference orig = sys.getrefcount(__name__) socket.getnameinfo(__name__,0) except TypeError: if sys.getrefcount(__name__) != orig: self.fail("socket.getnameinfo loses a reference") def testInterpreterCrash(self): # Making sure getnameinfo doesn't crash the interpreter try: # On some versions, this crashes the interpreter. socket.getnameinfo(('x', 0, 0, 0), 0) except OSError: pass def testNtoH(self): # This just checks that htons etc. are their own inverse, # when looking at the lower 16 or 32 bits. sizes = {socket.htonl: 32, socket.ntohl: 32, socket.htons: 16, socket.ntohs: 16} for func, size in sizes.items(): mask = (1<<size) - 1 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): self.assertEqual(i & mask, func(func(i&mask)) & mask) swapped = func(mask) self.assertEqual(swapped & mask, mask) self.assertRaises(OverflowError, func, 1<<34) def testNtoHErrors(self): good_values = [ 1, 2, 3, 1, 2, 3 ] bad_values = [ -1, -2, -3, -1, -2, -3 ] for k in good_values: socket.ntohl(k) socket.ntohs(k) socket.htonl(k) socket.htons(k) for k in bad_values: self.assertRaises(OverflowError, socket.ntohl, k) self.assertRaises(OverflowError, socket.ntohs, k) self.assertRaises(OverflowError, socket.htonl, k) self.assertRaises(OverflowError, socket.htons, k) def testGetServBy(self): eq = self.assertEqual # Find one service that exists, then check all the related interfaces. # I've ordered this by protocols that have both a tcp and udp # protocol, at least for modern Linuxes. if (sys.platform.startswith(('freebsd', 'netbsd', 'gnukfreebsd')) or sys.platform in ('linux', 'darwin')): # avoid the 'echo' service on this platform, as there is an # assumption breaking non-standard port/protocol entry services = ('daytime', 'qotd', 'domain') else: services = ('echo', 'daytime', 'domain') for service in services: try: port = socket.getservbyname(service, 'tcp') break except OSError: pass else: raise OSError # Try same call with optional protocol omitted port2 = socket.getservbyname(service) eq(port, port2) # Try udp, but don't barf if it doesn't exist try: udpport = socket.getservbyname(service, 'udp') except OSError: udpport = None else: eq(udpport, port) # Now make sure the lookup by port returns the same service name eq(socket.getservbyport(port2), service) eq(socket.getservbyport(port, 'tcp'), service) if udpport is not None: eq(socket.getservbyport(udpport, 'udp'), service) # Make sure getservbyport does not accept out of range ports. self.assertRaises(OverflowError, socket.getservbyport, -1) self.assertRaises(OverflowError, socket.getservbyport, 65536) def testDefaultTimeout(self): # Testing default timeout # The default timeout should initially be None self.assertEqual(socket.getdefaulttimeout(), None) s = socket.socket() self.assertEqual(s.gettimeout(), None) s.close() # Set the default timeout to 10, and see if it propagates socket.setdefaulttimeout(10) self.assertEqual(socket.getdefaulttimeout(), 10) s = socket.socket() self.assertEqual(s.gettimeout(), 10) s.close() # Reset the default timeout to None, and see if it propagates socket.setdefaulttimeout(None) self.assertEqual(socket.getdefaulttimeout(), None) s = socket.socket() self.assertEqual(s.gettimeout(), None) s.close() # Check that setting it to an invalid value raises ValueError self.assertRaises(ValueError, socket.setdefaulttimeout, -1) # Check that setting it to an invalid type raises TypeError self.assertRaises(TypeError, socket.setdefaulttimeout, "spam") @unittest.skipUnless(hasattr(socket, 'inet_aton'), 'test needs socket.inet_aton()') def testIPv4_inet_aton_fourbytes(self): # Test that issue1008086 and issue767150 are fixed. # It must return 4 bytes. self.assertEqual(b'\x00'*4, socket.inet_aton('0.0.0.0')) self.assertEqual(b'\xff'*4, socket.inet_aton('255.255.255.255')) @unittest.skipUnless(hasattr(socket, 'inet_pton'), 'test needs socket.inet_pton()') def testIPv4toString(self): from socket import inet_aton as f, inet_pton, AF_INET g = lambda a: inet_pton(AF_INET, a) assertInvalid = lambda func,a: self.assertRaises( (OSError, ValueError), func, a ) self.assertEqual(b'\x00\x00\x00\x00', f('0.0.0.0')) self.assertEqual(b'\xff\x00\xff\x00', f('255.0.255.0')) self.assertEqual(b'\xaa\xaa\xaa\xaa', f('170.170.170.170')) self.assertEqual(b'\x01\x02\x03\x04', f('1.2.3.4')) self.assertEqual(b'\xff\xff\xff\xff', f('255.255.255.255')) assertInvalid(f, '0.0.0.') assertInvalid(f, '300.0.0.0') assertInvalid(f, 'a.0.0.0') assertInvalid(f, '1.2.3.4.5') assertInvalid(f, '::1') self.assertEqual(b'\x00\x00\x00\x00', g('0.0.0.0')) self.assertEqual(b'\xff\x00\xff\x00', g('255.0.255.0')) self.assertEqual(b'\xaa\xaa\xaa\xaa', g('170.170.170.170')) self.assertEqual(b'\xff\xff\xff\xff', g('255.255.255.255')) assertInvalid(g, '0.0.0.') assertInvalid(g, '300.0.0.0') assertInvalid(g, 'a.0.0.0') assertInvalid(g, '1.2.3.4.5') assertInvalid(g, '::1') @unittest.skipUnless(hasattr(socket, 'inet_pton'), 'test needs socket.inet_pton()') def testIPv6toString(self): try: from socket import inet_pton, AF_INET6, has_ipv6 if not has_ipv6: self.skipTest('IPv6 not available') except ImportError: self.skipTest('could not import needed symbols from socket') if sys.platform == "win32": try: inet_pton(AF_INET6, '::') except OSError as e: if e.winerror == 10022: self.skipTest('IPv6 might not be supported') f = lambda a: inet_pton(AF_INET6, a) assertInvalid = lambda a: self.assertRaises( (OSError, ValueError), f, a ) self.assertEqual(b'\x00' * 16, f('::')) self.assertEqual(b'\x00' * 16, f('0::0')) self.assertEqual(b'\x00\x01' + b'\x00' * 14, f('1::')) self.assertEqual( b'\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae', f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae') ) self.assertEqual( b'\xad\x42\x0a\xbc' + b'\x00' * 4 + b'\x01\x27\x00\x00\x02\x54\x00\x02', f('ad42:abc::127:0:254:2') ) self.assertEqual(b'\x00\x12\x00\x0a' + b'\x00' * 12, f('12:a::')) assertInvalid('0x20::') assertInvalid(':::') assertInvalid('::0::') assertInvalid('1::abc::') assertInvalid('1::abc::def') assertInvalid('1:2:3:4:5:6:') assertInvalid('1:2:3:4:5:6') assertInvalid('1:2:3:4:5:6:7:8:') assertInvalid('1:2:3:4:5:6:7:8:0') self.assertEqual(b'\x00' * 12 + b'\xfe\x2a\x17\x40', f('::254.42.23.64') ) self.assertEqual( b'\x00\x42' + b'\x00' * 8 + b'\xa2\x9b\xfe\x2a\x17\x40', f('42::a29b:254.42.23.64') ) self.assertEqual( b'\x00\x42\xa8\xb9\x00\x00\x00\x02\xff\xff\xa2\x9b\xfe\x2a\x17\x40', f('42:a8b9:0:2:ffff:a29b:254.42.23.64') ) assertInvalid('255.254.253.252') assertInvalid('1::260.2.3.0') assertInvalid('1::0.be.e.0') assertInvalid('1:2:3:4:5:6:7:1.2.3.4') assertInvalid('::1.2.3.4:0') assertInvalid('0.100.200.0:3:4:5:6:7:8') @unittest.skipUnless(hasattr(socket, 'inet_ntop'), 'test needs socket.inet_ntop()') def testStringToIPv4(self): from socket import inet_ntoa as f, inet_ntop, AF_INET g = lambda a: inet_ntop(AF_INET, a) assertInvalid = lambda func,a: self.assertRaises( (OSError, ValueError), func, a ) self.assertEqual('1.0.1.0', f(b'\x01\x00\x01\x00')) self.assertEqual('170.85.170.85', f(b'\xaa\x55\xaa\x55')) self.assertEqual('255.255.255.255', f(b'\xff\xff\xff\xff')) self.assertEqual('1.2.3.4', f(b'\x01\x02\x03\x04')) assertInvalid(f, b'\x00' * 3) assertInvalid(f, b'\x00' * 5) assertInvalid(f, b'\x00' * 16) self.assertEqual('1.0.1.0', g(b'\x01\x00\x01\x00')) self.assertEqual('170.85.170.85', g(b'\xaa\x55\xaa\x55')) self.assertEqual('255.255.255.255', g(b'\xff\xff\xff\xff')) assertInvalid(g, b'\x00' * 3) assertInvalid(g, b'\x00' * 5) assertInvalid(g, b'\x00' * 16) @unittest.skipUnless(hasattr(socket, 'inet_ntop'), 'test needs socket.inet_ntop()') def testStringToIPv6(self): try: from socket import inet_ntop, AF_INET6, has_ipv6 if not has_ipv6: self.skipTest('IPv6 not available') except ImportError: self.skipTest('could not import needed symbols from socket') if sys.platform == "win32": try: inet_ntop(AF_INET6, b'\x00' * 16) except OSError as e: if e.winerror == 10022: self.skipTest('IPv6 might not be supported') f = lambda a: inet_ntop(AF_INET6, a) assertInvalid = lambda a: self.assertRaises( (OSError, ValueError), f, a ) self.assertEqual('::', f(b'\x00' * 16)) self.assertEqual('::1', f(b'\x00' * 15 + b'\x01')) self.assertEqual( 'aef:b01:506:1001:ffff:9997:55:170', f(b'\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70') ) assertInvalid(b'\x12' * 15) assertInvalid(b'\x12' * 17) assertInvalid(b'\x12' * 4) # XXX The following don't test module-level functionality... def testSockName(self): # Testing getsockname() port = support.find_unused_port() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(sock.close) sock.bind(("0.0.0.0", port)) name = sock.getsockname() # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate # it reasonable to get the host's addr in addition to 0.0.0.0. # At least for eCos. This is required for the S/390 to pass. try: my_ip_addr = socket.gethostbyname(socket.gethostname()) except OSError: # Probably name lookup wasn't set up right; skip this test self.skipTest('name lookup failure') self.assertIn(name[0], ("0.0.0.0", my_ip_addr), '%s invalid' % name[0]) self.assertEqual(name[1], port) def testGetSockOpt(self): # Testing getsockopt() # We know a socket should start without reuse==0 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(sock.close) reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) self.assertFalse(reuse != 0, "initial mode is reuse") def testSetSockOpt(self): # Testing setsockopt() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(sock.close) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) self.assertFalse(reuse == 0, "failed to set reuse mode") def testSendAfterClose(self): # testing send() after close() with timeout sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(1) sock.close() self.assertRaises(OSError, sock.send, b"spam") def testNewAttributes(self): # testing .family, .type and .protocol sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.assertEqual(sock.family, socket.AF_INET) if hasattr(socket, 'SOCK_CLOEXEC'): self.assertIn(sock.type, (socket.SOCK_STREAM | socket.SOCK_CLOEXEC, socket.SOCK_STREAM)) else: self.assertEqual(sock.type, socket.SOCK_STREAM) self.assertEqual(sock.proto, 0) sock.close() def test_getsockaddrarg(self): sock = socket.socket() self.addCleanup(sock.close) port = support.find_unused_port() big_port = port + 65536 neg_port = port - 65536 self.assertRaises(OverflowError, sock.bind, (HOST, big_port)) self.assertRaises(OverflowError, sock.bind, (HOST, neg_port)) # Since find_unused_port() is inherently subject to race conditions, we # call it a couple times if necessary. for i in itertools.count(): port = support.find_unused_port() try: sock.bind((HOST, port)) except OSError as e: if e.errno != errno.EADDRINUSE or i == 5: raise else: break @unittest.skipUnless(os.name == "nt", "Windows specific") def test_sock_ioctl(self): self.assertTrue(hasattr(socket.socket, 'ioctl')) self.assertTrue(hasattr(socket, 'SIO_RCVALL')) self.assertTrue(hasattr(socket, 'RCVALL_ON')) self.assertTrue(hasattr(socket, 'RCVALL_OFF')) self.assertTrue(hasattr(socket, 'SIO_KEEPALIVE_VALS')) s = socket.socket() self.addCleanup(s.close) self.assertRaises(ValueError, s.ioctl, -1, None) s.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 100, 100)) def testGetaddrinfo(self): try: socket.getaddrinfo('localhost', 80) except socket.gaierror as err: if err.errno == socket.EAI_SERVICE: # see http://bugs.python.org/issue1282647 self.skipTest("buggy libc version") raise # len of every sequence is supposed to be == 5 for info in socket.getaddrinfo(HOST, None): self.assertEqual(len(info), 5) # host can be a domain name, a string representation of an # IPv4/v6 address or None socket.getaddrinfo('localhost', 80) socket.getaddrinfo('127.0.0.1', 80) socket.getaddrinfo(None, 80) if support.IPV6_ENABLED: socket.getaddrinfo('::1', 80) # port can be a string service name such as "http", a numeric # port number or None socket.getaddrinfo(HOST, "http") socket.getaddrinfo(HOST, 80) socket.getaddrinfo(HOST, None) # test family and socktype filters infos = socket.getaddrinfo(HOST, 80, socket.AF_INET, socket.SOCK_STREAM) for family, type, _, _, _ in infos: self.assertEqual(family, socket.AF_INET) self.assertEqual(str(family), 'AddressFamily.AF_INET') self.assertEqual(type, socket.SOCK_STREAM) self.assertEqual(str(type), 'SocketKind.SOCK_STREAM') infos = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM) for _, socktype, _, _, _ in infos: self.assertEqual(socktype, socket.SOCK_STREAM) # test proto and flags arguments socket.getaddrinfo(HOST, None, 0, 0, socket.SOL_TCP) socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE) # a server willing to support both IPv4 and IPv6 will # usually do this socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE) # test keyword arguments a = socket.getaddrinfo(HOST, None) b = socket.getaddrinfo(host=HOST, port=None) self.assertEqual(a, b) a = socket.getaddrinfo(HOST, None, socket.AF_INET) b = socket.getaddrinfo(HOST, None, family=socket.AF_INET) self.assertEqual(a, b) a = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM) b = socket.getaddrinfo(HOST, None, type=socket.SOCK_STREAM) self.assertEqual(a, b) a = socket.getaddrinfo(HOST, None, 0, 0, socket.SOL_TCP) b = socket.getaddrinfo(HOST, None, proto=socket.SOL_TCP) self.assertEqual(a, b) a = socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE) b = socket.getaddrinfo(HOST, None, flags=socket.AI_PASSIVE) self.assertEqual(a, b) a = socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE) b = socket.getaddrinfo(host=None, port=0, family=socket.AF_UNSPEC, type=socket.SOCK_STREAM, proto=0, flags=socket.AI_PASSIVE) self.assertEqual(a, b) # Issue #6697. self.assertRaises(UnicodeEncodeError, socket.getaddrinfo, 'localhost', '\uD800') # Issue 17269: test workaround for OS X platform bug segfault if hasattr(socket, 'AI_NUMERICSERV'): try: # The arguments here are undefined and the call may succeed # or fail. All we care here is that it doesn't segfault. socket.getaddrinfo("localhost", None, 0, 0, 0, socket.AI_NUMERICSERV) except socket.gaierror: pass def test_getnameinfo(self): # only IP addresses are allowed self.assertRaises(OSError, socket.getnameinfo, ('mail.python.org',0), 0) @unittest.skipUnless(support.is_resource_enabled('network'), 'network is not enabled') def test_idna(self): # Check for internet access before running test (issue #12804). try: socket.gethostbyname('python.org') except socket.gaierror as e: if e.errno == socket.EAI_NODATA: self.skipTest('internet access required for this test') # these should all be successful domain = 'испытание.pythontest.net' socket.gethostbyname(domain) socket.gethostbyname_ex(domain) socket.getaddrinfo(domain,0,socket.AF_UNSPEC,socket.SOCK_STREAM) # this may not work if the forward lookup choses the IPv6 address, as that doesn't # have a reverse entry yet # socket.gethostbyaddr('испытание.python.org') def check_sendall_interrupted(self, with_timeout): # socketpair() is not stricly required, but it makes things easier. if not hasattr(signal, 'alarm') or not hasattr(socket, 'socketpair'): self.skipTest("signal.alarm and socket.socketpair required for this test") # Our signal handlers clobber the C errno by calling a math function # with an invalid domain value. def ok_handler(*args): self.assertRaises(ValueError, math.acosh, 0) def raising_handler(*args): self.assertRaises(ValueError, math.acosh, 0) 1 // 0 c, s = socket.socketpair() old_alarm = signal.signal(signal.SIGALRM, raising_handler) try: if with_timeout: # Just above the one second minimum for signal.alarm c.settimeout(1.5) with self.assertRaises(ZeroDivisionError): signal.alarm(1) c.sendall(b"x" * support.SOCK_MAX_SIZE) if with_timeout: signal.signal(signal.SIGALRM, ok_handler) signal.alarm(1) self.assertRaises(socket.timeout, c.sendall, b"x" * support.SOCK_MAX_SIZE) finally: signal.alarm(0) signal.signal(signal.SIGALRM, old_alarm) c.close() s.close() def test_sendall_interrupted(self): self.check_sendall_interrupted(False) def test_sendall_interrupted_with_timeout(self): self.check_sendall_interrupted(True) def test_dealloc_warn(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) r = repr(sock) with self.assertWarns(ResourceWarning) as cm: sock = None support.gc_collect() self.assertIn(r, str(cm.warning.args[0])) # An open socket file object gets dereferenced after the socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) f = sock.makefile('rb') r = repr(sock) sock = None support.gc_collect() with self.assertWarns(ResourceWarning): f = None support.gc_collect() def test_name_closed_socketio(self): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: fp = sock.makefile("rb") fp.close() self.assertEqual(repr(fp), "<_io.BufferedReader name=-1>") def test_unusable_closed_socketio(self): with socket.socket() as sock: fp = sock.makefile("rb", buffering=0) self.assertTrue(fp.readable()) self.assertFalse(fp.writable()) self.assertFalse(fp.seekable()) fp.close() self.assertRaises(ValueError, fp.readable) self.assertRaises(ValueError, fp.writable) self.assertRaises(ValueError, fp.seekable) def test_pickle(self): sock = socket.socket() with sock: for protocol in range(pickle.HIGHEST_PROTOCOL + 1): self.assertRaises(TypeError, pickle.dumps, sock, protocol) def test_listen_backlog(self): for backlog in 0, -1: srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) srv.bind((HOST, 0)) srv.listen(backlog) srv.close() @support.cpython_only def test_listen_backlog_overflow(self): # Issue 15989 import _testcapi srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) srv.bind((HOST, 0)) self.assertRaises(OverflowError, srv.listen, _testcapi.INT_MAX + 1) srv.close() @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') def test_flowinfo(self): self.assertRaises(OverflowError, socket.getnameinfo, (support.HOSTv6, 0, 0xffffffff), 0) with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s: self.assertRaises(OverflowError, s.bind, (support.HOSTv6, 0, -10)) def test_str_for_enums(self): # Make sure that the AF_* and SOCK_* constants have enum-like string # reprs. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: self.assertEqual(str(s.family), 'AddressFamily.AF_INET') self.assertEqual(str(s.type), 'SocketKind.SOCK_STREAM') @unittest.skipIf(os.name == 'nt', 'Will not work on Windows') def test_uknown_socket_family_repr(self): # Test that when created with a family that's not one of the known # AF_*/SOCK_* constants, socket.family just returns the number. # # To do this we fool socket.socket into believing it already has an # open fd because on this path it doesn't actually verify the family and # type and populates the socket object. # # On Windows this trick won't work, so the test is skipped. fd, _ = tempfile.mkstemp() with socket.socket(family=42424, type=13331, fileno=fd) as s: self.assertEqual(s.family, 42424) self.assertEqual(s.type, 13331) @unittest.skipUnless(HAVE_SOCKET_CAN, 'SocketCan required for this test.') class BasicCANTest(unittest.TestCase): def testCrucialConstants(self): socket.AF_CAN socket.PF_CAN socket.CAN_RAW @unittest.skipUnless(hasattr(socket, "CAN_BCM"), 'socket.CAN_BCM required for this test.') def testBCMConstants(self): socket.CAN_BCM # opcodes socket.CAN_BCM_TX_SETUP # create (cyclic) transmission task socket.CAN_BCM_TX_DELETE # remove (cyclic) transmission task socket.CAN_BCM_TX_READ # read properties of (cyclic) transmission task socket.CAN_BCM_TX_SEND # send one CAN frame socket.CAN_BCM_RX_SETUP # create RX content filter subscription socket.CAN_BCM_RX_DELETE # remove RX content filter subscription socket.CAN_BCM_RX_READ # read properties of RX content filter subscription socket.CAN_BCM_TX_STATUS # reply to TX_READ request socket.CAN_BCM_TX_EXPIRED # notification on performed transmissions (count=0) socket.CAN_BCM_RX_STATUS # reply to RX_READ request socket.CAN_BCM_RX_TIMEOUT # cyclic message is absent socket.CAN_BCM_RX_CHANGED # updated CAN frame (detected content change) def testCreateSocket(self): with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: pass @unittest.skipUnless(hasattr(socket, "CAN_BCM"), 'socket.CAN_BCM required for this test.') def testCreateBCMSocket(self): with socket.socket(socket.PF_CAN, socket.SOCK_DGRAM, socket.CAN_BCM) as s: pass def testBindAny(self): with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: s.bind(('', )) def testTooLongInterfaceName(self): # most systems limit IFNAMSIZ to 16, take 1024 to be sure with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: self.assertRaisesRegex(OSError, 'interface name too long', s.bind, ('x' * 1024,)) @unittest.skipUnless(hasattr(socket, "CAN_RAW_LOOPBACK"), 'socket.CAN_RAW_LOOPBACK required for this test.') def testLoopback(self): with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: for loopback in (0, 1): s.setsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_LOOPBACK, loopback) self.assertEqual(loopback, s.getsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_LOOPBACK)) @unittest.skipUnless(hasattr(socket, "CAN_RAW_FILTER"), 'socket.CAN_RAW_FILTER required for this test.') def testFilter(self): can_id, can_mask = 0x200, 0x700 can_filter = struct.pack("=II", can_id, can_mask) with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: s.setsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_FILTER, can_filter) self.assertEqual(can_filter, s.getsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_FILTER, 8)) @unittest.skipUnless(HAVE_SOCKET_CAN, 'SocketCan required for this test.') @unittest.skipUnless(thread, 'Threading required for this test.') class CANTest(ThreadedCANSocketTest): def __init__(self, methodName='runTest'): ThreadedCANSocketTest.__init__(self, methodName=methodName) @classmethod def build_can_frame(cls, can_id, data): """Build a CAN frame.""" can_dlc = len(data) data = data.ljust(8, b'\x00') return struct.pack(cls.can_frame_fmt, can_id, can_dlc, data) @classmethod def dissect_can_frame(cls, frame): """Dissect a CAN frame.""" can_id, can_dlc, data = struct.unpack(cls.can_frame_fmt, frame) return (can_id, can_dlc, data[:can_dlc]) def testSendFrame(self): cf, addr = self.s.recvfrom(self.bufsize) self.assertEqual(self.cf, cf) self.assertEqual(addr[0], self.interface) self.assertEqual(addr[1], socket.AF_CAN) def _testSendFrame(self): self.cf = self.build_can_frame(0x00, b'\x01\x02\x03\x04\x05') self.cli.send(self.cf) def testSendMaxFrame(self): cf, addr = self.s.recvfrom(self.bufsize) self.assertEqual(self.cf, cf) def _testSendMaxFrame(self): self.cf = self.build_can_frame(0x00, b'\x07' * 8) self.cli.send(self.cf) def testSendMultiFrames(self): cf, addr = self.s.recvfrom(self.bufsize) self.assertEqual(self.cf1, cf) cf, addr = self.s.recvfrom(self.bufsize) self.assertEqual(self.cf2, cf) def _testSendMultiFrames(self): self.cf1 = self.build_can_frame(0x07, b'\x44\x33\x22\x11') self.cli.send(self.cf1) self.cf2 = self.build_can_frame(0x12, b'\x99\x22\x33') self.cli.send(self.cf2) @unittest.skipUnless(hasattr(socket, "CAN_BCM"), 'socket.CAN_BCM required for this test.') def _testBCM(self): cf, addr = self.cli.recvfrom(self.bufsize) self.assertEqual(self.cf, cf) can_id, can_dlc, data = self.dissect_can_frame(cf) self.assertEqual(self.can_id, can_id) self.assertEqual(self.data, data) @unittest.skipUnless(hasattr(socket, "CAN_BCM"), 'socket.CAN_BCM required for this test.') def testBCM(self): bcm = socket.socket(socket.PF_CAN, socket.SOCK_DGRAM, socket.CAN_BCM) self.addCleanup(bcm.close) bcm.connect((self.interface,)) self.can_id = 0x123 self.data = bytes([0xc0, 0xff, 0xee]) self.cf = self.build_can_frame(self.can_id, self.data) opcode = socket.CAN_BCM_TX_SEND flags = 0 count = 0 ival1_seconds = ival1_usec = ival2_seconds = ival2_usec = 0 bcm_can_id = 0x0222 nframes = 1 assert len(self.cf) == 16 header = struct.pack(self.bcm_cmd_msg_fmt, opcode, flags, count, ival1_seconds, ival1_usec, ival2_seconds, ival2_usec, bcm_can_id, nframes, ) header_plus_frame = header + self.cf bytes_sent = bcm.send(header_plus_frame) self.assertEqual(bytes_sent, len(header_plus_frame)) @unittest.skipUnless(HAVE_SOCKET_RDS, 'RDS sockets required for this test.') class BasicRDSTest(unittest.TestCase): def testCrucialConstants(self): socket.AF_RDS socket.PF_RDS def testCreateSocket(self): with socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) as s: pass def testSocketBufferSize(self): bufsize = 16384 with socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) as s: s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, bufsize) s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, bufsize) @unittest.skipUnless(HAVE_SOCKET_RDS, 'RDS sockets required for this test.') @unittest.skipUnless(thread, 'Threading required for this test.') class RDSTest(ThreadedRDSSocketTest): def __init__(self, methodName='runTest'): ThreadedRDSSocketTest.__init__(self, methodName=methodName) def setUp(self): super().setUp() self.evt = threading.Event() def testSendAndRecv(self): data, addr = self.serv.recvfrom(self.bufsize) self.assertEqual(self.data, data) self.assertEqual(self.cli_addr, addr) def _testSendAndRecv(self): self.data = b'spam' self.cli.sendto(self.data, 0, (HOST, self.port)) def testPeek(self): data, addr = self.serv.recvfrom(self.bufsize, socket.MSG_PEEK) self.assertEqual(self.data, data) data, addr = self.serv.recvfrom(self.bufsize) self.assertEqual(self.data, data) def _testPeek(self): self.data = b'spam' self.cli.sendto(self.data, 0, (HOST, self.port)) @requireAttrs(socket.socket, 'recvmsg') def testSendAndRecvMsg(self): data, ancdata, msg_flags, addr = self.serv.recvmsg(self.bufsize) self.assertEqual(self.data, data) @requireAttrs(socket.socket, 'sendmsg') def _testSendAndRecvMsg(self): self.data = b'hello ' * 10 self.cli.sendmsg([self.data], (), 0, (HOST, self.port)) def testSendAndRecvMulti(self): data, addr = self.serv.recvfrom(self.bufsize) self.assertEqual(self.data1, data) data, addr = self.serv.recvfrom(self.bufsize) self.assertEqual(self.data2, data) def _testSendAndRecvMulti(self): self.data1 = b'bacon' self.cli.sendto(self.data1, 0, (HOST, self.port)) self.data2 = b'egg' self.cli.sendto(self.data2, 0, (HOST, self.port)) def testSelect(self): r, w, x = select.select([self.serv], [], [], 3.0) self.assertIn(self.serv, r) data, addr = self.serv.recvfrom(self.bufsize) self.assertEqual(self.data, data) def _testSelect(self): self.data = b'select' self.cli.sendto(self.data, 0, (HOST, self.port)) def testCongestion(self): # wait until the sender is done self.evt.wait() def _testCongestion(self): # test the behavior in case of congestion self.data = b'fill' self.cli.setblocking(False) try: # try to lower the receiver's socket buffer size self.cli.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 16384) except OSError: pass with self.assertRaises(OSError) as cm: try: # fill the receiver's socket buffer while True: self.cli.sendto(self.data, 0, (HOST, self.port)) finally: # signal the receiver we're done self.evt.set() # sendto() should have failed with ENOBUFS self.assertEqual(cm.exception.errno, errno.ENOBUFS) # and we should have received a congestion notification through poll r, w, x = select.select([self.serv], [], [], 3.0) self.assertIn(self.serv, r) @unittest.skipUnless(thread, 'Threading required for this test.') class BasicTCPTest(SocketConnectedTest): def __init__(self, methodName='runTest'): SocketConnectedTest.__init__(self, methodName=methodName) def testRecv(self): # Testing large receive over TCP msg = self.cli_conn.recv(1024) self.assertEqual(msg, MSG) def _testRecv(self): self.serv_conn.send(MSG) def testOverFlowRecv(self): # Testing receive in chunks over TCP seg1 = self.cli_conn.recv(len(MSG) - 3) seg2 = self.cli_conn.recv(1024) msg = seg1 + seg2 self.assertEqual(msg, MSG) def _testOverFlowRecv(self): self.serv_conn.send(MSG) def testRecvFrom(self): # Testing large recvfrom() over TCP msg, addr = self.cli_conn.recvfrom(1024) self.assertEqual(msg, MSG) def _testRecvFrom(self): self.serv_conn.send(MSG) def testOverFlowRecvFrom(self): # Testing recvfrom() in chunks over TCP seg1, addr = self.cli_conn.recvfrom(len(MSG)-3) seg2, addr = self.cli_conn.recvfrom(1024) msg = seg1 + seg2 self.assertEqual(msg, MSG) def _testOverFlowRecvFrom(self): self.serv_conn.send(MSG) def testSendAll(self): # Testing sendall() with a 2048 byte string over TCP msg = b'' while 1: read = self.cli_conn.recv(1024) if not read: break msg += read self.assertEqual(msg, b'f' * 2048) def _testSendAll(self): big_chunk = b'f' * 2048 self.serv_conn.sendall(big_chunk) def testFromFd(self): # Testing fromfd() fd = self.cli_conn.fileno() sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(sock.close) self.assertIsInstance(sock, socket.socket) msg = sock.recv(1024) self.assertEqual(msg, MSG) def _testFromFd(self): self.serv_conn.send(MSG) def testDup(self): # Testing dup() sock = self.cli_conn.dup() self.addCleanup(sock.close) msg = sock.recv(1024) self.assertEqual(msg, MSG) def _testDup(self): self.serv_conn.send(MSG) def testShutdown(self): # Testing shutdown() msg = self.cli_conn.recv(1024) self.assertEqual(msg, MSG) # wait for _testShutdown to finish: on OS X, when the server # closes the connection the client also becomes disconnected, # and the client's shutdown call will fail. (Issue #4397.) self.done.wait() def _testShutdown(self): self.serv_conn.send(MSG) self.serv_conn.shutdown(2) testShutdown_overflow = support.cpython_only(testShutdown) @support.cpython_only def _testShutdown_overflow(self): import _testcapi self.serv_conn.send(MSG) # Issue 15989 self.assertRaises(OverflowError, self.serv_conn.shutdown, _testcapi.INT_MAX + 1) self.assertRaises(OverflowError, self.serv_conn.shutdown, 2 + (_testcapi.UINT_MAX + 1)) self.serv_conn.shutdown(2) def testDetach(self): # Testing detach() fileno = self.cli_conn.fileno() f = self.cli_conn.detach() self.assertEqual(f, fileno) # cli_conn cannot be used anymore... self.assertTrue(self.cli_conn._closed) self.assertRaises(OSError, self.cli_conn.recv, 1024) self.cli_conn.close() # ...but we can create another socket using the (still open) # file descriptor sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, fileno=f) self.addCleanup(sock.close) msg = sock.recv(1024) self.assertEqual(msg, MSG) def _testDetach(self): self.serv_conn.send(MSG) @unittest.skipUnless(thread, 'Threading required for this test.') class BasicUDPTest(ThreadedUDPSocketTest): def __init__(self, methodName='runTest'): ThreadedUDPSocketTest.__init__(self, methodName=methodName) def testSendtoAndRecv(self): # Testing sendto() and Recv() over UDP msg = self.serv.recv(len(MSG)) self.assertEqual(msg, MSG) def _testSendtoAndRecv(self): self.cli.sendto(MSG, 0, (HOST, self.port)) def testRecvFrom(self): # Testing recvfrom() over UDP msg, addr = self.serv.recvfrom(len(MSG)) self.assertEqual(msg, MSG) def _testRecvFrom(self): self.cli.sendto(MSG, 0, (HOST, self.port)) def testRecvFromNegative(self): # Negative lengths passed to recvfrom should give ValueError. self.assertRaises(ValueError, self.serv.recvfrom, -1) def _testRecvFromNegative(self): self.cli.sendto(MSG, 0, (HOST, self.port)) # Tests for the sendmsg()/recvmsg() interface. Where possible, the # same test code is used with different families and types of socket # (e.g. stream, datagram), and tests using recvmsg() are repeated # using recvmsg_into(). # # The generic test classes such as SendmsgTests and # RecvmsgGenericTests inherit from SendrecvmsgBase and expect to be # supplied with sockets cli_sock and serv_sock representing the # client's and the server's end of the connection respectively, and # attributes cli_addr and serv_addr holding their (numeric where # appropriate) addresses. # # The final concrete test classes combine these with subclasses of # SocketTestBase which set up client and server sockets of a specific # type, and with subclasses of SendrecvmsgBase such as # SendrecvmsgDgramBase and SendrecvmsgConnectedBase which map these # sockets to cli_sock and serv_sock and override the methods and # attributes of SendrecvmsgBase to fill in destination addresses if # needed when sending, check for specific flags in msg_flags, etc. # # RecvmsgIntoMixin provides a version of doRecvmsg() implemented using # recvmsg_into(). # XXX: like the other datagram (UDP) tests in this module, the code # here assumes that datagram delivery on the local machine will be # reliable. class SendrecvmsgBase(ThreadSafeCleanupTestCase): # Base class for sendmsg()/recvmsg() tests. # Time in seconds to wait before considering a test failed, or # None for no timeout. Not all tests actually set a timeout. fail_timeout = 3.0 def setUp(self): self.misc_event = threading.Event() super().setUp() def sendToServer(self, msg): # Send msg to the server. return self.cli_sock.send(msg) # Tuple of alternative default arguments for sendmsg() when called # via sendmsgToServer() (e.g. to include a destination address). sendmsg_to_server_defaults = () def sendmsgToServer(self, *args): # Call sendmsg() on self.cli_sock with the given arguments, # filling in any arguments which are not supplied with the # corresponding items of self.sendmsg_to_server_defaults, if # any. return self.cli_sock.sendmsg( *(args + self.sendmsg_to_server_defaults[len(args):])) def doRecvmsg(self, sock, bufsize, *args): # Call recvmsg() on sock with given arguments and return its # result. Should be used for tests which can use either # recvmsg() or recvmsg_into() - RecvmsgIntoMixin overrides # this method with one which emulates it using recvmsg_into(), # thus allowing the same test to be used for both methods. result = sock.recvmsg(bufsize, *args) self.registerRecvmsgResult(result) return result def registerRecvmsgResult(self, result): # Called by doRecvmsg() with the return value of recvmsg() or # recvmsg_into(). Can be overridden to arrange cleanup based # on the returned ancillary data, for instance. pass def checkRecvmsgAddress(self, addr1, addr2): # Called to compare the received address with the address of # the peer. self.assertEqual(addr1, addr2) # Flags that are normally unset in msg_flags msg_flags_common_unset = 0 for name in ("MSG_CTRUNC", "MSG_OOB"): msg_flags_common_unset |= getattr(socket, name, 0) # Flags that are normally set msg_flags_common_set = 0 # Flags set when a complete record has been received (e.g. MSG_EOR # for SCTP) msg_flags_eor_indicator = 0 # Flags set when a complete record has not been received # (e.g. MSG_TRUNC for datagram sockets) msg_flags_non_eor_indicator = 0 def checkFlags(self, flags, eor=None, checkset=0, checkunset=0, ignore=0): # Method to check the value of msg_flags returned by recvmsg[_into](). # # Checks that all bits in msg_flags_common_set attribute are # set in "flags" and all bits in msg_flags_common_unset are # unset. # # The "eor" argument specifies whether the flags should # indicate that a full record (or datagram) has been received. # If "eor" is None, no checks are done; otherwise, checks # that: # # * if "eor" is true, all bits in msg_flags_eor_indicator are # set and all bits in msg_flags_non_eor_indicator are unset # # * if "eor" is false, all bits in msg_flags_non_eor_indicator # are set and all bits in msg_flags_eor_indicator are unset # # If "checkset" and/or "checkunset" are supplied, they require # the given bits to be set or unset respectively, overriding # what the attributes require for those bits. # # If any bits are set in "ignore", they will not be checked, # regardless of the other inputs. # # Will raise Exception if the inputs require a bit to be both # set and unset, and it is not ignored. defaultset = self.msg_flags_common_set defaultunset = self.msg_flags_common_unset if eor: defaultset |= self.msg_flags_eor_indicator defaultunset |= self.msg_flags_non_eor_indicator elif eor is not None: defaultset |= self.msg_flags_non_eor_indicator defaultunset |= self.msg_flags_eor_indicator # Function arguments override defaults defaultset &= ~checkunset defaultunset &= ~checkset # Merge arguments with remaining defaults, and check for conflicts checkset |= defaultset checkunset |= defaultunset inboth = checkset & checkunset & ~ignore if inboth: raise Exception("contradictory set, unset requirements for flags " "{0:#x}".format(inboth)) # Compare with given msg_flags value mask = (checkset | checkunset) & ~ignore self.assertEqual(flags & mask, checkset & mask) class RecvmsgIntoMixin(SendrecvmsgBase): # Mixin to implement doRecvmsg() using recvmsg_into(). def doRecvmsg(self, sock, bufsize, *args): buf = bytearray(bufsize) result = sock.recvmsg_into([buf], *args) self.registerRecvmsgResult(result) self.assertGreaterEqual(result[0], 0) self.assertLessEqual(result[0], bufsize) return (bytes(buf[:result[0]]),) + result[1:] class SendrecvmsgDgramFlagsBase(SendrecvmsgBase): # Defines flags to be checked in msg_flags for datagram sockets. @property def msg_flags_non_eor_indicator(self): return super().msg_flags_non_eor_indicator | socket.MSG_TRUNC class SendrecvmsgSCTPFlagsBase(SendrecvmsgBase): # Defines flags to be checked in msg_flags for SCTP sockets. @property def msg_flags_eor_indicator(self): return super().msg_flags_eor_indicator | socket.MSG_EOR class SendrecvmsgConnectionlessBase(SendrecvmsgBase): # Base class for tests on connectionless-mode sockets. Users must # supply sockets on attributes cli and serv to be mapped to # cli_sock and serv_sock respectively. @property def serv_sock(self): return self.serv @property def cli_sock(self): return self.cli @property def sendmsg_to_server_defaults(self): return ([], [], 0, self.serv_addr) def sendToServer(self, msg): return self.cli_sock.sendto(msg, self.serv_addr) class SendrecvmsgConnectedBase(SendrecvmsgBase): # Base class for tests on connected sockets. Users must supply # sockets on attributes serv_conn and cli_conn (representing the # connections *to* the server and the client), to be mapped to # cli_sock and serv_sock respectively. @property def serv_sock(self): return self.cli_conn @property def cli_sock(self): return self.serv_conn def checkRecvmsgAddress(self, addr1, addr2): # Address is currently "unspecified" for a connected socket, # so we don't examine it pass class SendrecvmsgServerTimeoutBase(SendrecvmsgBase): # Base class to set a timeout on server's socket. def setUp(self): super().setUp() self.serv_sock.settimeout(self.fail_timeout) class SendmsgTests(SendrecvmsgServerTimeoutBase): # Tests for sendmsg() which can use any socket type and do not # involve recvmsg() or recvmsg_into(). def testSendmsg(self): # Send a simple message with sendmsg(). self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) def _testSendmsg(self): self.assertEqual(self.sendmsgToServer([MSG]), len(MSG)) def testSendmsgDataGenerator(self): # Send from buffer obtained from a generator (not a sequence). self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) def _testSendmsgDataGenerator(self): self.assertEqual(self.sendmsgToServer((o for o in [MSG])), len(MSG)) def testSendmsgAncillaryGenerator(self): # Gather (empty) ancillary data from a generator. self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) def _testSendmsgAncillaryGenerator(self): self.assertEqual(self.sendmsgToServer([MSG], (o for o in [])), len(MSG)) def testSendmsgArray(self): # Send data from an array instead of the usual bytes object. self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) def _testSendmsgArray(self): self.assertEqual(self.sendmsgToServer([array.array("B", MSG)]), len(MSG)) def testSendmsgGather(self): # Send message data from more than one buffer (gather write). self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) def _testSendmsgGather(self): self.assertEqual(self.sendmsgToServer([MSG[:3], MSG[3:]]), len(MSG)) def testSendmsgBadArgs(self): # Check that sendmsg() rejects invalid arguments. self.assertEqual(self.serv_sock.recv(1000), b"done") def _testSendmsgBadArgs(self): self.assertRaises(TypeError, self.cli_sock.sendmsg) self.assertRaises(TypeError, self.sendmsgToServer, b"not in an iterable") self.assertRaises(TypeError, self.sendmsgToServer, object()) self.assertRaises(TypeError, self.sendmsgToServer, [object()]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG, object()]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], object()) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [], object()) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [], 0, object()) self.sendToServer(b"done") def testSendmsgBadCmsg(self): # Check that invalid ancillary data items are rejected. self.assertEqual(self.serv_sock.recv(1000), b"done") def _testSendmsgBadCmsg(self): self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [object()]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [(object(), 0, b"data")]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [(0, object(), b"data")]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [(0, 0, object())]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [(0, 0)]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [(0, 0, b"data", 42)]) self.sendToServer(b"done") @requireAttrs(socket, "CMSG_SPACE") def testSendmsgBadMultiCmsg(self): # Check that invalid ancillary data items are rejected when # more than one item is present. self.assertEqual(self.serv_sock.recv(1000), b"done") @testSendmsgBadMultiCmsg.client_skip def _testSendmsgBadMultiCmsg(self): self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [0, 0, b""]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [(0, 0, b""), object()]) self.sendToServer(b"done") def testSendmsgExcessCmsgReject(self): # Check that sendmsg() rejects excess ancillary data items # when the number that can be sent is limited. self.assertEqual(self.serv_sock.recv(1000), b"done") def _testSendmsgExcessCmsgReject(self): if not hasattr(socket, "CMSG_SPACE"): # Can only send one item with self.assertRaises(OSError) as cm: self.sendmsgToServer([MSG], [(0, 0, b""), (0, 0, b"")]) self.assertIsNone(cm.exception.errno) self.sendToServer(b"done") def testSendmsgAfterClose(self): # Check that sendmsg() fails on a closed socket. pass def _testSendmsgAfterClose(self): self.cli_sock.close() self.assertRaises(OSError, self.sendmsgToServer, [MSG]) class SendmsgStreamTests(SendmsgTests): # Tests for sendmsg() which require a stream socket and do not # involve recvmsg() or recvmsg_into(). def testSendmsgExplicitNoneAddr(self): # Check that peer address can be specified as None. self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) def _testSendmsgExplicitNoneAddr(self): self.assertEqual(self.sendmsgToServer([MSG], [], 0, None), len(MSG)) def testSendmsgTimeout(self): # Check that timeout works with sendmsg(). self.assertEqual(self.serv_sock.recv(512), b"a"*512) self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) def _testSendmsgTimeout(self): try: self.cli_sock.settimeout(0.03) with self.assertRaises(socket.timeout): while True: self.sendmsgToServer([b"a"*512]) finally: self.misc_event.set() # XXX: would be nice to have more tests for sendmsg flags argument. # Linux supports MSG_DONTWAIT when sending, but in general, it # only works when receiving. Could add other platforms if they # support it too. @skipWithClientIf(sys.platform not in {"linux"}, "MSG_DONTWAIT not known to work on this platform when " "sending") def testSendmsgDontWait(self): # Check that MSG_DONTWAIT in flags causes non-blocking behaviour. self.assertEqual(self.serv_sock.recv(512), b"a"*512) self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) @testSendmsgDontWait.client_skip def _testSendmsgDontWait(self): try: with self.assertRaises(OSError) as cm: while True: self.sendmsgToServer([b"a"*512], [], socket.MSG_DONTWAIT) self.assertIn(cm.exception.errno, (errno.EAGAIN, errno.EWOULDBLOCK)) finally: self.misc_event.set() class SendmsgConnectionlessTests(SendmsgTests): # Tests for sendmsg() which require a connectionless-mode # (e.g. datagram) socket, and do not involve recvmsg() or # recvmsg_into(). def testSendmsgNoDestAddr(self): # Check that sendmsg() fails when no destination address is # given for unconnected socket. pass def _testSendmsgNoDestAddr(self): self.assertRaises(OSError, self.cli_sock.sendmsg, [MSG]) self.assertRaises(OSError, self.cli_sock.sendmsg, [MSG], [], 0, None) class RecvmsgGenericTests(SendrecvmsgBase): # Tests for recvmsg() which can also be emulated using # recvmsg_into(), and can use any socket type. def testRecvmsg(self): # Receive a simple message with recvmsg[_into](). msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG)) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsg(self): self.sendToServer(MSG) def testRecvmsgExplicitDefaults(self): # Test recvmsg[_into]() with default arguments provided explicitly. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 0, 0) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgExplicitDefaults(self): self.sendToServer(MSG) def testRecvmsgShorter(self): # Receive a message smaller than buffer. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG) + 42) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgShorter(self): self.sendToServer(MSG) # FreeBSD < 8 doesn't always set the MSG_TRUNC flag when a truncated # datagram is received (issue #13001). @support.requires_freebsd_version(8) def testRecvmsgTrunc(self): # Receive part of message, check for truncation indicators. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG) - 3) self.assertEqual(msg, MSG[:-3]) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=False) @support.requires_freebsd_version(8) def _testRecvmsgTrunc(self): self.sendToServer(MSG) def testRecvmsgShortAncillaryBuf(self): # Test ancillary data buffer too small to hold any ancillary data. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 1) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgShortAncillaryBuf(self): self.sendToServer(MSG) def testRecvmsgLongAncillaryBuf(self): # Test large ancillary data buffer. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 10240) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgLongAncillaryBuf(self): self.sendToServer(MSG) def testRecvmsgAfterClose(self): # Check that recvmsg[_into]() fails on a closed socket. self.serv_sock.close() self.assertRaises(OSError, self.doRecvmsg, self.serv_sock, 1024) def _testRecvmsgAfterClose(self): pass def testRecvmsgTimeout(self): # Check that timeout works. try: self.serv_sock.settimeout(0.03) self.assertRaises(socket.timeout, self.doRecvmsg, self.serv_sock, len(MSG)) finally: self.misc_event.set() def _testRecvmsgTimeout(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) @requireAttrs(socket, "MSG_PEEK") def testRecvmsgPeek(self): # Check that MSG_PEEK in flags enables examination of pending # data without consuming it. # Receive part of data with MSG_PEEK. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG) - 3, 0, socket.MSG_PEEK) self.assertEqual(msg, MSG[:-3]) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) # Ignoring MSG_TRUNC here (so this test is the same for stream # and datagram sockets). Some wording in POSIX seems to # suggest that it needn't be set when peeking, but that may # just be a slip. self.checkFlags(flags, eor=False, ignore=getattr(socket, "MSG_TRUNC", 0)) # Receive all data with MSG_PEEK. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 0, socket.MSG_PEEK) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) # Check that the same data can still be received normally. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG)) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) @testRecvmsgPeek.client_skip def _testRecvmsgPeek(self): self.sendToServer(MSG) @requireAttrs(socket.socket, "sendmsg") def testRecvmsgFromSendmsg(self): # Test receiving with recvmsg[_into]() when message is sent # using sendmsg(). self.serv_sock.settimeout(self.fail_timeout) msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG)) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) @testRecvmsgFromSendmsg.client_skip def _testRecvmsgFromSendmsg(self): self.assertEqual(self.sendmsgToServer([MSG[:3], MSG[3:]]), len(MSG)) class RecvmsgGenericStreamTests(RecvmsgGenericTests): # Tests which require a stream socket and can use either recvmsg() # or recvmsg_into(). def testRecvmsgEOF(self): # Receive end-of-stream indicator (b"", peer socket closed). msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, 1024) self.assertEqual(msg, b"") self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=None) # Might not have end-of-record marker def _testRecvmsgEOF(self): self.cli_sock.close() def testRecvmsgOverflow(self): # Receive a message in more than one chunk. seg1, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG) - 3) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=False) seg2, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, 1024) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) msg = seg1 + seg2 self.assertEqual(msg, MSG) def _testRecvmsgOverflow(self): self.sendToServer(MSG) class RecvmsgTests(RecvmsgGenericTests): # Tests for recvmsg() which can use any socket type. def testRecvmsgBadArgs(self): # Check that recvmsg() rejects invalid arguments. self.assertRaises(TypeError, self.serv_sock.recvmsg) self.assertRaises(ValueError, self.serv_sock.recvmsg, -1, 0, 0) self.assertRaises(ValueError, self.serv_sock.recvmsg, len(MSG), -1, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg, [bytearray(10)], 0, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg, object(), 0, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg, len(MSG), object(), 0) self.assertRaises(TypeError, self.serv_sock.recvmsg, len(MSG), 0, object()) msg, ancdata, flags, addr = self.serv_sock.recvmsg(len(MSG), 0, 0) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgBadArgs(self): self.sendToServer(MSG) class RecvmsgIntoTests(RecvmsgIntoMixin, RecvmsgGenericTests): # Tests for recvmsg_into() which can use any socket type. def testRecvmsgIntoBadArgs(self): # Check that recvmsg_into() rejects invalid arguments. buf = bytearray(len(MSG)) self.assertRaises(TypeError, self.serv_sock.recvmsg_into) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, len(MSG), 0, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, buf, 0, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, [object()], 0, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, [b"I'm not writable"], 0, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, [buf, object()], 0, 0) self.assertRaises(ValueError, self.serv_sock.recvmsg_into, [buf], -1, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, [buf], object(), 0) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, [buf], 0, object()) nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into([buf], 0, 0) self.assertEqual(nbytes, len(MSG)) self.assertEqual(buf, bytearray(MSG)) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgIntoBadArgs(self): self.sendToServer(MSG) def testRecvmsgIntoGenerator(self): # Receive into buffer obtained from a generator (not a sequence). buf = bytearray(len(MSG)) nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into( (o for o in [buf])) self.assertEqual(nbytes, len(MSG)) self.assertEqual(buf, bytearray(MSG)) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgIntoGenerator(self): self.sendToServer(MSG) def testRecvmsgIntoArray(self): # Receive into an array rather than the usual bytearray. buf = array.array("B", [0] * len(MSG)) nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into([buf]) self.assertEqual(nbytes, len(MSG)) self.assertEqual(buf.tobytes(), MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgIntoArray(self): self.sendToServer(MSG) def testRecvmsgIntoScatter(self): # Receive into multiple buffers (scatter write). b1 = bytearray(b"----") b2 = bytearray(b"0123456789") b3 = bytearray(b"--------------") nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into( [b1, memoryview(b2)[2:9], b3]) self.assertEqual(nbytes, len(b"Mary had a little lamb")) self.assertEqual(b1, bytearray(b"Mary")) self.assertEqual(b2, bytearray(b"01 had a 9")) self.assertEqual(b3, bytearray(b"little lamb---")) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgIntoScatter(self): self.sendToServer(b"Mary had a little lamb") class CmsgMacroTests(unittest.TestCase): # Test the functions CMSG_LEN() and CMSG_SPACE(). Tests # assumptions used by sendmsg() and recvmsg[_into](), which share # code with these functions. # Match the definition in socketmodule.c try: import _testcapi except ImportError: socklen_t_limit = 0x7fffffff else: socklen_t_limit = min(0x7fffffff, _testcapi.INT_MAX) @requireAttrs(socket, "CMSG_LEN") def testCMSG_LEN(self): # Test CMSG_LEN() with various valid and invalid values, # checking the assumptions used by recvmsg() and sendmsg(). toobig = self.socklen_t_limit - socket.CMSG_LEN(0) + 1 values = list(range(257)) + list(range(toobig - 257, toobig)) # struct cmsghdr has at least three members, two of which are ints self.assertGreater(socket.CMSG_LEN(0), array.array("i").itemsize * 2) for n in values: ret = socket.CMSG_LEN(n) # This is how recvmsg() calculates the data size self.assertEqual(ret - socket.CMSG_LEN(0), n) self.assertLessEqual(ret, self.socklen_t_limit) self.assertRaises(OverflowError, socket.CMSG_LEN, -1) # sendmsg() shares code with these functions, and requires # that it reject values over the limit. self.assertRaises(OverflowError, socket.CMSG_LEN, toobig) self.assertRaises(OverflowError, socket.CMSG_LEN, sys.maxsize) @requireAttrs(socket, "CMSG_SPACE") def testCMSG_SPACE(self): # Test CMSG_SPACE() with various valid and invalid values, # checking the assumptions used by sendmsg(). toobig = self.socklen_t_limit - socket.CMSG_SPACE(1) + 1 values = list(range(257)) + list(range(toobig - 257, toobig)) last = socket.CMSG_SPACE(0) # struct cmsghdr has at least three members, two of which are ints self.assertGreater(last, array.array("i").itemsize * 2) for n in values: ret = socket.CMSG_SPACE(n) self.assertGreaterEqual(ret, last) self.assertGreaterEqual(ret, socket.CMSG_LEN(n)) self.assertGreaterEqual(ret, n + socket.CMSG_LEN(0)) self.assertLessEqual(ret, self.socklen_t_limit) last = ret self.assertRaises(OverflowError, socket.CMSG_SPACE, -1) # sendmsg() shares code with these functions, and requires # that it reject values over the limit. self.assertRaises(OverflowError, socket.CMSG_SPACE, toobig) self.assertRaises(OverflowError, socket.CMSG_SPACE, sys.maxsize) class SCMRightsTest(SendrecvmsgServerTimeoutBase): # Tests for file descriptor passing on Unix-domain sockets. # Invalid file descriptor value that's unlikely to evaluate to a # real FD even if one of its bytes is replaced with a different # value (which shouldn't actually happen). badfd = -0x5555 def newFDs(self, n): # Return a list of n file descriptors for newly-created files # containing their list indices as ASCII numbers. fds = [] for i in range(n): fd, path = tempfile.mkstemp() self.addCleanup(os.unlink, path) self.addCleanup(os.close, fd) os.write(fd, str(i).encode()) fds.append(fd) return fds def checkFDs(self, fds): # Check that the file descriptors in the given list contain # their correct list indices as ASCII numbers. for n, fd in enumerate(fds): os.lseek(fd, 0, os.SEEK_SET) self.assertEqual(os.read(fd, 1024), str(n).encode()) def registerRecvmsgResult(self, result): self.addCleanup(self.closeRecvmsgFDs, result) def closeRecvmsgFDs(self, recvmsg_result): # Close all file descriptors specified in the ancillary data # of the given return value from recvmsg() or recvmsg_into(). for cmsg_level, cmsg_type, cmsg_data in recvmsg_result[1]: if (cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS): fds = array.array("i") fds.frombytes(cmsg_data[: len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) for fd in fds: os.close(fd) def createAndSendFDs(self, n): # Send n new file descriptors created by newFDs() to the # server, with the constant MSG as the non-ancillary data. self.assertEqual( self.sendmsgToServer([MSG], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", self.newFDs(n)))]), len(MSG)) def checkRecvmsgFDs(self, numfds, result, maxcmsgs=1, ignoreflags=0): # Check that constant MSG was received with numfds file # descriptors in a maximum of maxcmsgs control messages (which # must contain only complete integers). By default, check # that MSG_CTRUNC is unset, but ignore any flags in # ignoreflags. msg, ancdata, flags, addr = result self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkunset=socket.MSG_CTRUNC, ignore=ignoreflags) self.assertIsInstance(ancdata, list) self.assertLessEqual(len(ancdata), maxcmsgs) fds = array.array("i") for item in ancdata: self.assertIsInstance(item, tuple) cmsg_level, cmsg_type, cmsg_data = item self.assertEqual(cmsg_level, socket.SOL_SOCKET) self.assertEqual(cmsg_type, socket.SCM_RIGHTS) self.assertIsInstance(cmsg_data, bytes) self.assertEqual(len(cmsg_data) % SIZEOF_INT, 0) fds.frombytes(cmsg_data) self.assertEqual(len(fds), numfds) self.checkFDs(fds) def testFDPassSimple(self): # Pass a single FD (array read from bytes object). self.checkRecvmsgFDs(1, self.doRecvmsg(self.serv_sock, len(MSG), 10240)) def _testFDPassSimple(self): self.assertEqual( self.sendmsgToServer( [MSG], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", self.newFDs(1)).tobytes())]), len(MSG)) def testMultipleFDPass(self): # Pass multiple FDs in a single array. self.checkRecvmsgFDs(4, self.doRecvmsg(self.serv_sock, len(MSG), 10240)) def _testMultipleFDPass(self): self.createAndSendFDs(4) @requireAttrs(socket, "CMSG_SPACE") def testFDPassCMSG_SPACE(self): # Test using CMSG_SPACE() to calculate ancillary buffer size. self.checkRecvmsgFDs( 4, self.doRecvmsg(self.serv_sock, len(MSG), socket.CMSG_SPACE(4 * SIZEOF_INT))) @testFDPassCMSG_SPACE.client_skip def _testFDPassCMSG_SPACE(self): self.createAndSendFDs(4) def testFDPassCMSG_LEN(self): # Test using CMSG_LEN() to calculate ancillary buffer size. self.checkRecvmsgFDs(1, self.doRecvmsg(self.serv_sock, len(MSG), socket.CMSG_LEN(4 * SIZEOF_INT)), # RFC 3542 says implementations may set # MSG_CTRUNC if there isn't enough space # for trailing padding. ignoreflags=socket.MSG_CTRUNC) def _testFDPassCMSG_LEN(self): self.createAndSendFDs(1) @unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958") @requireAttrs(socket, "CMSG_SPACE") def testFDPassSeparate(self): # Pass two FDs in two separate arrays. Arrays may be combined # into a single control message by the OS. self.checkRecvmsgFDs(2, self.doRecvmsg(self.serv_sock, len(MSG), 10240), maxcmsgs=2) @testFDPassSeparate.client_skip @unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958") def _testFDPassSeparate(self): fd0, fd1 = self.newFDs(2) self.assertEqual( self.sendmsgToServer([MSG], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [fd0])), (socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [fd1]))]), len(MSG)) @unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958") @requireAttrs(socket, "CMSG_SPACE") def testFDPassSeparateMinSpace(self): # Pass two FDs in two separate arrays, receiving them into the # minimum space for two arrays. self.checkRecvmsgFDs(2, self.doRecvmsg(self.serv_sock, len(MSG), socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(SIZEOF_INT)), maxcmsgs=2, ignoreflags=socket.MSG_CTRUNC) @testFDPassSeparateMinSpace.client_skip @unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958") def _testFDPassSeparateMinSpace(self): fd0, fd1 = self.newFDs(2) self.assertEqual( self.sendmsgToServer([MSG], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [fd0])), (socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [fd1]))]), len(MSG)) def sendAncillaryIfPossible(self, msg, ancdata): # Try to send msg and ancdata to server, but if the system # call fails, just send msg with no ancillary data. try: nbytes = self.sendmsgToServer([msg], ancdata) except OSError as e: # Check that it was the system call that failed self.assertIsInstance(e.errno, int) nbytes = self.sendmsgToServer([msg]) self.assertEqual(nbytes, len(msg)) def testFDPassEmpty(self): # Try to pass an empty FD array. Can receive either no array # or an empty array. self.checkRecvmsgFDs(0, self.doRecvmsg(self.serv_sock, len(MSG), 10240), ignoreflags=socket.MSG_CTRUNC) def _testFDPassEmpty(self): self.sendAncillaryIfPossible(MSG, [(socket.SOL_SOCKET, socket.SCM_RIGHTS, b"")]) def testFDPassPartialInt(self): # Try to pass a truncated FD array. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 10240) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, ignore=socket.MSG_CTRUNC) self.assertLessEqual(len(ancdata), 1) for cmsg_level, cmsg_type, cmsg_data in ancdata: self.assertEqual(cmsg_level, socket.SOL_SOCKET) self.assertEqual(cmsg_type, socket.SCM_RIGHTS) self.assertLess(len(cmsg_data), SIZEOF_INT) def _testFDPassPartialInt(self): self.sendAncillaryIfPossible( MSG, [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [self.badfd]).tobytes()[:-1])]) @requireAttrs(socket, "CMSG_SPACE") def testFDPassPartialIntInMiddle(self): # Try to pass two FD arrays, the first of which is truncated. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 10240) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, ignore=socket.MSG_CTRUNC) self.assertLessEqual(len(ancdata), 2) fds = array.array("i") # Arrays may have been combined in a single control message for cmsg_level, cmsg_type, cmsg_data in ancdata: self.assertEqual(cmsg_level, socket.SOL_SOCKET) self.assertEqual(cmsg_type, socket.SCM_RIGHTS) fds.frombytes(cmsg_data[: len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) self.assertLessEqual(len(fds), 2) self.checkFDs(fds) @testFDPassPartialIntInMiddle.client_skip def _testFDPassPartialIntInMiddle(self): fd0, fd1 = self.newFDs(2) self.sendAncillaryIfPossible( MSG, [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [fd0, self.badfd]).tobytes()[:-1]), (socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [fd1]))]) def checkTruncatedHeader(self, result, ignoreflags=0): # Check that no ancillary data items are returned when data is # truncated inside the cmsghdr structure. msg, ancdata, flags, addr = result self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC, ignore=ignoreflags) def testCmsgTruncNoBufSize(self): # Check that no ancillary data is received when no buffer size # is specified. self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG)), # BSD seems to set MSG_CTRUNC only # if an item has been partially # received. ignoreflags=socket.MSG_CTRUNC) def _testCmsgTruncNoBufSize(self): self.createAndSendFDs(1) def testCmsgTrunc0(self): # Check that no ancillary data is received when buffer size is 0. self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), 0), ignoreflags=socket.MSG_CTRUNC) def _testCmsgTrunc0(self): self.createAndSendFDs(1) # Check that no ancillary data is returned for various non-zero # (but still too small) buffer sizes. def testCmsgTrunc1(self): self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), 1)) def _testCmsgTrunc1(self): self.createAndSendFDs(1) def testCmsgTrunc2Int(self): # The cmsghdr structure has at least three members, two of # which are ints, so we still shouldn't see any ancillary # data. self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), SIZEOF_INT * 2)) def _testCmsgTrunc2Int(self): self.createAndSendFDs(1) def testCmsgTruncLen0Minus1(self): self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), socket.CMSG_LEN(0) - 1)) def _testCmsgTruncLen0Minus1(self): self.createAndSendFDs(1) # The following tests try to truncate the control message in the # middle of the FD array. def checkTruncatedArray(self, ancbuf, maxdata, mindata=0): # Check that file descriptor data is truncated to between # mindata and maxdata bytes when received with buffer size # ancbuf, and that any complete file descriptor numbers are # valid. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), ancbuf) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC) if mindata == 0 and ancdata == []: return self.assertEqual(len(ancdata), 1) cmsg_level, cmsg_type, cmsg_data = ancdata[0] self.assertEqual(cmsg_level, socket.SOL_SOCKET) self.assertEqual(cmsg_type, socket.SCM_RIGHTS) self.assertGreaterEqual(len(cmsg_data), mindata) self.assertLessEqual(len(cmsg_data), maxdata) fds = array.array("i") fds.frombytes(cmsg_data[: len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) self.checkFDs(fds) def testCmsgTruncLen0(self): self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(0), maxdata=0) def _testCmsgTruncLen0(self): self.createAndSendFDs(1) def testCmsgTruncLen0Plus1(self): self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(0) + 1, maxdata=1) def _testCmsgTruncLen0Plus1(self): self.createAndSendFDs(2) def testCmsgTruncLen1(self): self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(SIZEOF_INT), maxdata=SIZEOF_INT) def _testCmsgTruncLen1(self): self.createAndSendFDs(2) def testCmsgTruncLen2Minus1(self): self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(2 * SIZEOF_INT) - 1, maxdata=(2 * SIZEOF_INT) - 1) def _testCmsgTruncLen2Minus1(self): self.createAndSendFDs(2) class RFC3542AncillaryTest(SendrecvmsgServerTimeoutBase): # Test sendmsg() and recvmsg[_into]() using the ancillary data # features of the RFC 3542 Advanced Sockets API for IPv6. # Currently we can only handle certain data items (e.g. traffic # class, hop limit, MTU discovery and fragmentation settings) # without resorting to unportable means such as the struct module, # but the tests here are aimed at testing the ancillary data # handling in sendmsg() and recvmsg() rather than the IPv6 API # itself. # Test value to use when setting hop limit of packet hop_limit = 2 # Test value to use when setting traffic class of packet. # -1 means "use kernel default". traffic_class = -1 def ancillaryMapping(self, ancdata): # Given ancillary data list ancdata, return a mapping from # pairs (cmsg_level, cmsg_type) to corresponding cmsg_data. # Check that no (level, type) pair appears more than once. d = {} for cmsg_level, cmsg_type, cmsg_data in ancdata: self.assertNotIn((cmsg_level, cmsg_type), d) d[(cmsg_level, cmsg_type)] = cmsg_data return d def checkHopLimit(self, ancbufsize, maxhop=255, ignoreflags=0): # Receive hop limit into ancbufsize bytes of ancillary data # space. Check that data is MSG, ancillary data is not # truncated (but ignore any flags in ignoreflags), and hop # limit is between 0 and maxhop inclusive. self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.misc_event.set() msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), ancbufsize) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkunset=socket.MSG_CTRUNC, ignore=ignoreflags) self.assertEqual(len(ancdata), 1) self.assertIsInstance(ancdata[0], tuple) cmsg_level, cmsg_type, cmsg_data = ancdata[0] self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) self.assertEqual(cmsg_type, socket.IPV6_HOPLIMIT) self.assertIsInstance(cmsg_data, bytes) self.assertEqual(len(cmsg_data), SIZEOF_INT) a = array.array("i") a.frombytes(cmsg_data) self.assertGreaterEqual(a[0], 0) self.assertLessEqual(a[0], maxhop) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testRecvHopLimit(self): # Test receiving the packet hop limit as ancillary data. self.checkHopLimit(ancbufsize=10240) @testRecvHopLimit.client_skip def _testRecvHopLimit(self): # Need to wait until server has asked to receive ancillary # data, as implementations are not required to buffer it # otherwise. self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testRecvHopLimitCMSG_SPACE(self): # Test receiving hop limit, using CMSG_SPACE to calculate buffer size. self.checkHopLimit(ancbufsize=socket.CMSG_SPACE(SIZEOF_INT)) @testRecvHopLimitCMSG_SPACE.client_skip def _testRecvHopLimitCMSG_SPACE(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) # Could test receiving into buffer sized using CMSG_LEN, but RFC # 3542 says portable applications must provide space for trailing # padding. Implementations may set MSG_CTRUNC if there isn't # enough space for the padding. @requireAttrs(socket.socket, "sendmsg") @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testSetHopLimit(self): # Test setting hop limit on outgoing packet and receiving it # at the other end. self.checkHopLimit(ancbufsize=10240, maxhop=self.hop_limit) @testSetHopLimit.client_skip def _testSetHopLimit(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.assertEqual( self.sendmsgToServer([MSG], [(socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT, array.array("i", [self.hop_limit]))]), len(MSG)) def checkTrafficClassAndHopLimit(self, ancbufsize, maxhop=255, ignoreflags=0): # Receive traffic class and hop limit into ancbufsize bytes of # ancillary data space. Check that data is MSG, ancillary # data is not truncated (but ignore any flags in ignoreflags), # and traffic class and hop limit are in range (hop limit no # more than maxhop). self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVTCLASS, 1) self.misc_event.set() msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), ancbufsize) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkunset=socket.MSG_CTRUNC, ignore=ignoreflags) self.assertEqual(len(ancdata), 2) ancmap = self.ancillaryMapping(ancdata) tcdata = ancmap[(socket.IPPROTO_IPV6, socket.IPV6_TCLASS)] self.assertEqual(len(tcdata), SIZEOF_INT) a = array.array("i") a.frombytes(tcdata) self.assertGreaterEqual(a[0], 0) self.assertLessEqual(a[0], 255) hldata = ancmap[(socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT)] self.assertEqual(len(hldata), SIZEOF_INT) a = array.array("i") a.frombytes(hldata) self.assertGreaterEqual(a[0], 0) self.assertLessEqual(a[0], maxhop) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testRecvTrafficClassAndHopLimit(self): # Test receiving traffic class and hop limit as ancillary data. self.checkTrafficClassAndHopLimit(ancbufsize=10240) @testRecvTrafficClassAndHopLimit.client_skip def _testRecvTrafficClassAndHopLimit(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testRecvTrafficClassAndHopLimitCMSG_SPACE(self): # Test receiving traffic class and hop limit, using # CMSG_SPACE() to calculate buffer size. self.checkTrafficClassAndHopLimit( ancbufsize=socket.CMSG_SPACE(SIZEOF_INT) * 2) @testRecvTrafficClassAndHopLimitCMSG_SPACE.client_skip def _testRecvTrafficClassAndHopLimitCMSG_SPACE(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket.socket, "sendmsg") @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testSetTrafficClassAndHopLimit(self): # Test setting traffic class and hop limit on outgoing packet, # and receiving them at the other end. self.checkTrafficClassAndHopLimit(ancbufsize=10240, maxhop=self.hop_limit) @testSetTrafficClassAndHopLimit.client_skip def _testSetTrafficClassAndHopLimit(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.assertEqual( self.sendmsgToServer([MSG], [(socket.IPPROTO_IPV6, socket.IPV6_TCLASS, array.array("i", [self.traffic_class])), (socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT, array.array("i", [self.hop_limit]))]), len(MSG)) @requireAttrs(socket.socket, "sendmsg") @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testOddCmsgSize(self): # Try to send ancillary data with first item one byte too # long. Fall back to sending with correct size if this fails, # and check that second item was handled correctly. self.checkTrafficClassAndHopLimit(ancbufsize=10240, maxhop=self.hop_limit) @testOddCmsgSize.client_skip def _testOddCmsgSize(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) try: nbytes = self.sendmsgToServer( [MSG], [(socket.IPPROTO_IPV6, socket.IPV6_TCLASS, array.array("i", [self.traffic_class]).tobytes() + b"\x00"), (socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT, array.array("i", [self.hop_limit]))]) except OSError as e: self.assertIsInstance(e.errno, int) nbytes = self.sendmsgToServer( [MSG], [(socket.IPPROTO_IPV6, socket.IPV6_TCLASS, array.array("i", [self.traffic_class])), (socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT, array.array("i", [self.hop_limit]))]) self.assertEqual(nbytes, len(MSG)) # Tests for proper handling of truncated ancillary data def checkHopLimitTruncatedHeader(self, ancbufsize, ignoreflags=0): # Receive hop limit into ancbufsize bytes of ancillary data # space, which should be too small to contain the ancillary # data header (if ancbufsize is None, pass no second argument # to recvmsg()). Check that data is MSG, MSG_CTRUNC is set # (unless included in ignoreflags), and no ancillary data is # returned. self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.misc_event.set() args = () if ancbufsize is None else (ancbufsize,) msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), *args) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC, ignore=ignoreflags) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testCmsgTruncNoBufSize(self): # Check that no ancillary data is received when no ancillary # buffer size is provided. self.checkHopLimitTruncatedHeader(ancbufsize=None, # BSD seems to set # MSG_CTRUNC only if an item # has been partially # received. ignoreflags=socket.MSG_CTRUNC) @testCmsgTruncNoBufSize.client_skip def _testCmsgTruncNoBufSize(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testSingleCmsgTrunc0(self): # Check that no ancillary data is received when ancillary # buffer size is zero. self.checkHopLimitTruncatedHeader(ancbufsize=0, ignoreflags=socket.MSG_CTRUNC) @testSingleCmsgTrunc0.client_skip def _testSingleCmsgTrunc0(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) # Check that no ancillary data is returned for various non-zero # (but still too small) buffer sizes. @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testSingleCmsgTrunc1(self): self.checkHopLimitTruncatedHeader(ancbufsize=1) @testSingleCmsgTrunc1.client_skip def _testSingleCmsgTrunc1(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testSingleCmsgTrunc2Int(self): self.checkHopLimitTruncatedHeader(ancbufsize=2 * SIZEOF_INT) @testSingleCmsgTrunc2Int.client_skip def _testSingleCmsgTrunc2Int(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testSingleCmsgTruncLen0Minus1(self): self.checkHopLimitTruncatedHeader(ancbufsize=socket.CMSG_LEN(0) - 1) @testSingleCmsgTruncLen0Minus1.client_skip def _testSingleCmsgTruncLen0Minus1(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testSingleCmsgTruncInData(self): # Test truncation of a control message inside its associated # data. The message may be returned with its data truncated, # or not returned at all. self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.misc_event.set() msg, ancdata, flags, addr = self.doRecvmsg( self.serv_sock, len(MSG), socket.CMSG_LEN(SIZEOF_INT) - 1) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC) self.assertLessEqual(len(ancdata), 1) if ancdata: cmsg_level, cmsg_type, cmsg_data = ancdata[0] self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) self.assertEqual(cmsg_type, socket.IPV6_HOPLIMIT) self.assertLess(len(cmsg_data), SIZEOF_INT) @testSingleCmsgTruncInData.client_skip def _testSingleCmsgTruncInData(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) def checkTruncatedSecondHeader(self, ancbufsize, ignoreflags=0): # Receive traffic class and hop limit into ancbufsize bytes of # ancillary data space, which should be large enough to # contain the first item, but too small to contain the header # of the second. Check that data is MSG, MSG_CTRUNC is set # (unless included in ignoreflags), and only one ancillary # data item is returned. self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVTCLASS, 1) self.misc_event.set() msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), ancbufsize) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC, ignore=ignoreflags) self.assertEqual(len(ancdata), 1) cmsg_level, cmsg_type, cmsg_data = ancdata[0] self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) self.assertIn(cmsg_type, {socket.IPV6_TCLASS, socket.IPV6_HOPLIMIT}) self.assertEqual(len(cmsg_data), SIZEOF_INT) a = array.array("i") a.frombytes(cmsg_data) self.assertGreaterEqual(a[0], 0) self.assertLessEqual(a[0], 255) # Try the above test with various buffer sizes. @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testSecondCmsgTrunc0(self): self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT), ignoreflags=socket.MSG_CTRUNC) @testSecondCmsgTrunc0.client_skip def _testSecondCmsgTrunc0(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testSecondCmsgTrunc1(self): self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT) + 1) @testSecondCmsgTrunc1.client_skip def _testSecondCmsgTrunc1(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testSecondCmsgTrunc2Int(self): self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT) + 2 * SIZEOF_INT) @testSecondCmsgTrunc2Int.client_skip def _testSecondCmsgTrunc2Int(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testSecondCmsgTruncLen0Minus1(self): self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(0) - 1) @testSecondCmsgTruncLen0Minus1.client_skip def _testSecondCmsgTruncLen0Minus1(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testSecomdCmsgTruncInData(self): # Test truncation of the second of two control messages inside # its associated data. self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVTCLASS, 1) self.misc_event.set() msg, ancdata, flags, addr = self.doRecvmsg( self.serv_sock, len(MSG), socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(SIZEOF_INT) - 1) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC) cmsg_types = {socket.IPV6_TCLASS, socket.IPV6_HOPLIMIT} cmsg_level, cmsg_type, cmsg_data = ancdata.pop(0) self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) cmsg_types.remove(cmsg_type) self.assertEqual(len(cmsg_data), SIZEOF_INT) a = array.array("i") a.frombytes(cmsg_data) self.assertGreaterEqual(a[0], 0) self.assertLessEqual(a[0], 255) if ancdata: cmsg_level, cmsg_type, cmsg_data = ancdata.pop(0) self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) cmsg_types.remove(cmsg_type) self.assertLess(len(cmsg_data), SIZEOF_INT) self.assertEqual(ancdata, []) @testSecomdCmsgTruncInData.client_skip def _testSecomdCmsgTruncInData(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) # Derive concrete test classes for different socket types. class SendrecvmsgUDPTestBase(SendrecvmsgDgramFlagsBase, SendrecvmsgConnectionlessBase, ThreadedSocketTestMixin, UDPTestBase): pass @requireAttrs(socket.socket, "sendmsg") @unittest.skipUnless(thread, 'Threading required for this test.') class SendmsgUDPTest(SendmsgConnectionlessTests, SendrecvmsgUDPTestBase): pass @requireAttrs(socket.socket, "recvmsg") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgUDPTest(RecvmsgTests, SendrecvmsgUDPTestBase): pass @requireAttrs(socket.socket, "recvmsg_into") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoUDPTest(RecvmsgIntoTests, SendrecvmsgUDPTestBase): pass class SendrecvmsgUDP6TestBase(SendrecvmsgDgramFlagsBase, SendrecvmsgConnectionlessBase, ThreadedSocketTestMixin, UDP6TestBase): def checkRecvmsgAddress(self, addr1, addr2): # Called to compare the received address with the address of # the peer, ignoring scope ID self.assertEqual(addr1[:-1], addr2[:-1]) @requireAttrs(socket.socket, "sendmsg") @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') @requireSocket("AF_INET6", "SOCK_DGRAM") @unittest.skipUnless(thread, 'Threading required for this test.') class SendmsgUDP6Test(SendmsgConnectionlessTests, SendrecvmsgUDP6TestBase): pass @requireAttrs(socket.socket, "recvmsg") @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') @requireSocket("AF_INET6", "SOCK_DGRAM") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgUDP6Test(RecvmsgTests, SendrecvmsgUDP6TestBase): pass @requireAttrs(socket.socket, "recvmsg_into") @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') @requireSocket("AF_INET6", "SOCK_DGRAM") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoUDP6Test(RecvmsgIntoTests, SendrecvmsgUDP6TestBase): pass @requireAttrs(socket.socket, "recvmsg") @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') @requireAttrs(socket, "IPPROTO_IPV6") @requireSocket("AF_INET6", "SOCK_DGRAM") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgRFC3542AncillaryUDP6Test(RFC3542AncillaryTest, SendrecvmsgUDP6TestBase): pass @requireAttrs(socket.socket, "recvmsg_into") @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') @requireAttrs(socket, "IPPROTO_IPV6") @requireSocket("AF_INET6", "SOCK_DGRAM") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoRFC3542AncillaryUDP6Test(RecvmsgIntoMixin, RFC3542AncillaryTest, SendrecvmsgUDP6TestBase): pass class SendrecvmsgTCPTestBase(SendrecvmsgConnectedBase, ConnectedStreamTestMixin, TCPTestBase): pass @requireAttrs(socket.socket, "sendmsg") @unittest.skipUnless(thread, 'Threading required for this test.') class SendmsgTCPTest(SendmsgStreamTests, SendrecvmsgTCPTestBase): pass @requireAttrs(socket.socket, "recvmsg") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgTCPTest(RecvmsgTests, RecvmsgGenericStreamTests, SendrecvmsgTCPTestBase): pass @requireAttrs(socket.socket, "recvmsg_into") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoTCPTest(RecvmsgIntoTests, RecvmsgGenericStreamTests, SendrecvmsgTCPTestBase): pass class SendrecvmsgSCTPStreamTestBase(SendrecvmsgSCTPFlagsBase, SendrecvmsgConnectedBase, ConnectedStreamTestMixin, SCTPStreamBase): pass @requireAttrs(socket.socket, "sendmsg") @requireSocket("AF_INET", "SOCK_STREAM", "IPPROTO_SCTP") @unittest.skipUnless(thread, 'Threading required for this test.') class SendmsgSCTPStreamTest(SendmsgStreamTests, SendrecvmsgSCTPStreamTestBase): pass @requireAttrs(socket.socket, "recvmsg") @requireSocket("AF_INET", "SOCK_STREAM", "IPPROTO_SCTP") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgSCTPStreamTest(RecvmsgTests, RecvmsgGenericStreamTests, SendrecvmsgSCTPStreamTestBase): def testRecvmsgEOF(self): try: super(RecvmsgSCTPStreamTest, self).testRecvmsgEOF() except OSError as e: if e.errno != errno.ENOTCONN: raise self.skipTest("sporadic ENOTCONN (kernel issue?) - see issue #13876") @requireAttrs(socket.socket, "recvmsg_into") @requireSocket("AF_INET", "SOCK_STREAM", "IPPROTO_SCTP") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoSCTPStreamTest(RecvmsgIntoTests, RecvmsgGenericStreamTests, SendrecvmsgSCTPStreamTestBase): def testRecvmsgEOF(self): try: super(RecvmsgIntoSCTPStreamTest, self).testRecvmsgEOF() except OSError as e: if e.errno != errno.ENOTCONN: raise self.skipTest("sporadic ENOTCONN (kernel issue?) - see issue #13876") class SendrecvmsgUnixStreamTestBase(SendrecvmsgConnectedBase, ConnectedStreamTestMixin, UnixStreamBase): pass @requireAttrs(socket.socket, "sendmsg") @requireAttrs(socket, "AF_UNIX") @unittest.skipUnless(thread, 'Threading required for this test.') class SendmsgUnixStreamTest(SendmsgStreamTests, SendrecvmsgUnixStreamTestBase): pass @requireAttrs(socket.socket, "recvmsg") @requireAttrs(socket, "AF_UNIX") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgUnixStreamTest(RecvmsgTests, RecvmsgGenericStreamTests, SendrecvmsgUnixStreamTestBase): pass @requireAttrs(socket.socket, "recvmsg_into") @requireAttrs(socket, "AF_UNIX") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoUnixStreamTest(RecvmsgIntoTests, RecvmsgGenericStreamTests, SendrecvmsgUnixStreamTestBase): pass @requireAttrs(socket.socket, "sendmsg", "recvmsg") @requireAttrs(socket, "AF_UNIX", "SOL_SOCKET", "SCM_RIGHTS") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgSCMRightsStreamTest(SCMRightsTest, SendrecvmsgUnixStreamTestBase): pass @requireAttrs(socket.socket, "sendmsg", "recvmsg_into") @requireAttrs(socket, "AF_UNIX", "SOL_SOCKET", "SCM_RIGHTS") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoSCMRightsStreamTest(RecvmsgIntoMixin, SCMRightsTest, SendrecvmsgUnixStreamTestBase): pass # Test interrupting the interruptible send/receive methods with a # signal when a timeout is set. These tests avoid having multiple # threads alive during the test so that the OS cannot deliver the # signal to the wrong one. class InterruptedTimeoutBase(unittest.TestCase): # Base class for interrupted send/receive tests. Installs an # empty handler for SIGALRM and removes it on teardown, along with # any scheduled alarms. def setUp(self): super().setUp() orig_alrm_handler = signal.signal(signal.SIGALRM, lambda signum, frame: None) self.addCleanup(signal.signal, signal.SIGALRM, orig_alrm_handler) self.addCleanup(self.setAlarm, 0) # Timeout for socket operations timeout = 4.0 # Provide setAlarm() method to schedule delivery of SIGALRM after # given number of seconds, or cancel it if zero, and an # appropriate time value to use. Use setitimer() if available. if hasattr(signal, "setitimer"): alarm_time = 0.05 def setAlarm(self, seconds): signal.setitimer(signal.ITIMER_REAL, seconds) else: # Old systems may deliver the alarm up to one second early alarm_time = 2 def setAlarm(self, seconds): signal.alarm(seconds) # Require siginterrupt() in order to ensure that system calls are # interrupted by default. @requireAttrs(signal, "siginterrupt") @unittest.skipUnless(hasattr(signal, "alarm") or hasattr(signal, "setitimer"), "Don't have signal.alarm or signal.setitimer") class InterruptedRecvTimeoutTest(InterruptedTimeoutBase, UDPTestBase): # Test interrupting the recv*() methods with signals when a # timeout is set. def setUp(self): super().setUp() self.serv.settimeout(self.timeout) def checkInterruptedRecv(self, func, *args, **kwargs): # Check that func(*args, **kwargs) raises OSError with an # errno of EINTR when interrupted by a signal. self.setAlarm(self.alarm_time) with self.assertRaises(OSError) as cm: func(*args, **kwargs) self.assertNotIsInstance(cm.exception, socket.timeout) self.assertEqual(cm.exception.errno, errno.EINTR) def testInterruptedRecvTimeout(self): self.checkInterruptedRecv(self.serv.recv, 1024) def testInterruptedRecvIntoTimeout(self): self.checkInterruptedRecv(self.serv.recv_into, bytearray(1024)) def testInterruptedRecvfromTimeout(self): self.checkInterruptedRecv(self.serv.recvfrom, 1024) def testInterruptedRecvfromIntoTimeout(self): self.checkInterruptedRecv(self.serv.recvfrom_into, bytearray(1024)) @requireAttrs(socket.socket, "recvmsg") def testInterruptedRecvmsgTimeout(self): self.checkInterruptedRecv(self.serv.recvmsg, 1024) @requireAttrs(socket.socket, "recvmsg_into") def testInterruptedRecvmsgIntoTimeout(self): self.checkInterruptedRecv(self.serv.recvmsg_into, [bytearray(1024)]) # Require siginterrupt() in order to ensure that system calls are # interrupted by default. @requireAttrs(signal, "siginterrupt") @unittest.skipUnless(hasattr(signal, "alarm") or hasattr(signal, "setitimer"), "Don't have signal.alarm or signal.setitimer") @unittest.skipUnless(thread, 'Threading required for this test.') class InterruptedSendTimeoutTest(InterruptedTimeoutBase, ThreadSafeCleanupTestCase, SocketListeningTestMixin, TCPTestBase): # Test interrupting the interruptible send*() methods with signals # when a timeout is set. def setUp(self): super().setUp() self.serv_conn = self.newSocket() self.addCleanup(self.serv_conn.close) # Use a thread to complete the connection, but wait for it to # terminate before running the test, so that there is only one # thread to accept the signal. cli_thread = threading.Thread(target=self.doConnect) cli_thread.start() self.cli_conn, addr = self.serv.accept() self.addCleanup(self.cli_conn.close) cli_thread.join() self.serv_conn.settimeout(self.timeout) def doConnect(self): self.serv_conn.connect(self.serv_addr) def checkInterruptedSend(self, func, *args, **kwargs): # Check that func(*args, **kwargs), run in a loop, raises # OSError with an errno of EINTR when interrupted by a # signal. with self.assertRaises(OSError) as cm: while True: self.setAlarm(self.alarm_time) func(*args, **kwargs) self.assertNotIsInstance(cm.exception, socket.timeout) self.assertEqual(cm.exception.errno, errno.EINTR) # Issue #12958: The following tests have problems on OS X prior to 10.7 @support.requires_mac_ver(10, 7) def testInterruptedSendTimeout(self): self.checkInterruptedSend(self.serv_conn.send, b"a"*512) @support.requires_mac_ver(10, 7) def testInterruptedSendtoTimeout(self): # Passing an actual address here as Python's wrapper for # sendto() doesn't allow passing a zero-length one; POSIX # requires that the address is ignored since the socket is # connection-mode, however. self.checkInterruptedSend(self.serv_conn.sendto, b"a"*512, self.serv_addr) @support.requires_mac_ver(10, 7) @requireAttrs(socket.socket, "sendmsg") def testInterruptedSendmsgTimeout(self): self.checkInterruptedSend(self.serv_conn.sendmsg, [b"a"*512]) @unittest.skipUnless(thread, 'Threading required for this test.') class TCPCloserTest(ThreadedTCPSocketTest): def testClose(self): conn, addr = self.serv.accept() conn.close() sd = self.cli read, write, err = select.select([sd], [], [], 1.0) self.assertEqual(read, [sd]) self.assertEqual(sd.recv(1), b'') # Calling close() many times should be safe. conn.close() conn.close() def _testClose(self): self.cli.connect((HOST, self.port)) time.sleep(1.0) @unittest.skipUnless(hasattr(socket, 'socketpair'), 'test needs socket.socketpair()') @unittest.skipUnless(thread, 'Threading required for this test.') class BasicSocketPairTest(SocketPairTest): def __init__(self, methodName='runTest'): SocketPairTest.__init__(self, methodName=methodName) def _check_defaults(self, sock): self.assertIsInstance(sock, socket.socket) if hasattr(socket, 'AF_UNIX'): self.assertEqual(sock.family, socket.AF_UNIX) else: self.assertEqual(sock.family, socket.AF_INET) self.assertEqual(sock.type, socket.SOCK_STREAM) self.assertEqual(sock.proto, 0) def _testDefaults(self): self._check_defaults(self.cli) def testDefaults(self): self._check_defaults(self.serv) def testRecv(self): msg = self.serv.recv(1024) self.assertEqual(msg, MSG) def _testRecv(self): self.cli.send(MSG) def testSend(self): self.serv.send(MSG) def _testSend(self): msg = self.cli.recv(1024) self.assertEqual(msg, MSG) @unittest.skipUnless(thread, 'Threading required for this test.') class NonBlockingTCPTests(ThreadedTCPSocketTest): def __init__(self, methodName='runTest'): ThreadedTCPSocketTest.__init__(self, methodName=methodName) def testSetBlocking(self): # Testing whether set blocking works self.serv.setblocking(True) self.assertIsNone(self.serv.gettimeout()) self.serv.setblocking(False) self.assertEqual(self.serv.gettimeout(), 0.0) start = time.time() try: self.serv.accept() except OSError: pass end = time.time() self.assertTrue((end - start) < 1.0, "Error setting non-blocking mode.") def _testSetBlocking(self): pass @support.cpython_only def testSetBlocking_overflow(self): # Issue 15989 import _testcapi if _testcapi.UINT_MAX >= _testcapi.ULONG_MAX: self.skipTest('needs UINT_MAX < ULONG_MAX') self.serv.setblocking(False) self.assertEqual(self.serv.gettimeout(), 0.0) self.serv.setblocking(_testcapi.UINT_MAX + 1) self.assertIsNone(self.serv.gettimeout()) _testSetBlocking_overflow = support.cpython_only(_testSetBlocking) @unittest.skipUnless(hasattr(socket, 'SOCK_NONBLOCK'), 'test needs socket.SOCK_NONBLOCK') @support.requires_linux_version(2, 6, 28) def testInitNonBlocking(self): # reinit server socket self.serv.close() self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM | socket.SOCK_NONBLOCK) self.port = support.bind_port(self.serv) self.serv.listen(1) # actual testing start = time.time() try: self.serv.accept() except OSError: pass end = time.time() self.assertTrue((end - start) < 1.0, "Error creating with non-blocking mode.") def _testInitNonBlocking(self): pass def testInheritFlags(self): # Issue #7995: when calling accept() on a listening socket with a # timeout, the resulting socket should not be non-blocking. self.serv.settimeout(10) try: conn, addr = self.serv.accept() message = conn.recv(len(MSG)) finally: conn.close() self.serv.settimeout(None) def _testInheritFlags(self): time.sleep(0.1) self.cli.connect((HOST, self.port)) time.sleep(0.5) self.cli.send(MSG) def testAccept(self): # Testing non-blocking accept self.serv.setblocking(0) try: conn, addr = self.serv.accept() except OSError: pass else: self.fail("Error trying to do non-blocking accept.") read, write, err = select.select([self.serv], [], []) if self.serv in read: conn, addr = self.serv.accept() conn.close() else: self.fail("Error trying to do accept after select.") def _testAccept(self): time.sleep(0.1) self.cli.connect((HOST, self.port)) def testConnect(self): # Testing non-blocking connect conn, addr = self.serv.accept() conn.close() def _testConnect(self): self.cli.settimeout(10) self.cli.connect((HOST, self.port)) def testRecv(self): # Testing non-blocking recv conn, addr = self.serv.accept() conn.setblocking(0) try: msg = conn.recv(len(MSG)) except OSError: pass else: self.fail("Error trying to do non-blocking recv.") read, write, err = select.select([conn], [], []) if conn in read: msg = conn.recv(len(MSG)) conn.close() self.assertEqual(msg, MSG) else: self.fail("Error during select call to non-blocking socket.") def _testRecv(self): self.cli.connect((HOST, self.port)) time.sleep(0.1) self.cli.send(MSG) @unittest.skipUnless(thread, 'Threading required for this test.') class FileObjectClassTestCase(SocketConnectedTest): """Unit tests for the object returned by socket.makefile() self.read_file is the io object returned by makefile() on the client connection. You can read from this file to get output from the server. self.write_file is the io object returned by makefile() on the server connection. You can write to this file to send output to the client. """ bufsize = -1 # Use default buffer size encoding = 'utf-8' errors = 'strict' newline = None read_mode = 'rb' read_msg = MSG write_mode = 'wb' write_msg = MSG def __init__(self, methodName='runTest'): SocketConnectedTest.__init__(self, methodName=methodName) def setUp(self): self.evt1, self.evt2, self.serv_finished, self.cli_finished = [ threading.Event() for i in range(4)] SocketConnectedTest.setUp(self) self.read_file = self.cli_conn.makefile( self.read_mode, self.bufsize, encoding = self.encoding, errors = self.errors, newline = self.newline) def tearDown(self): self.serv_finished.set() self.read_file.close() self.assertTrue(self.read_file.closed) self.read_file = None SocketConnectedTest.tearDown(self) def clientSetUp(self): SocketConnectedTest.clientSetUp(self) self.write_file = self.serv_conn.makefile( self.write_mode, self.bufsize, encoding = self.encoding, errors = self.errors, newline = self.newline) def clientTearDown(self): self.cli_finished.set() self.write_file.close() self.assertTrue(self.write_file.closed) self.write_file = None SocketConnectedTest.clientTearDown(self) def testReadAfterTimeout(self): # Issue #7322: A file object must disallow further reads # after a timeout has occurred. self.cli_conn.settimeout(1) self.read_file.read(3) # First read raises a timeout self.assertRaises(socket.timeout, self.read_file.read, 1) # Second read is disallowed with self.assertRaises(OSError) as ctx: self.read_file.read(1) self.assertIn("cannot read from timed out object", str(ctx.exception)) def _testReadAfterTimeout(self): self.write_file.write(self.write_msg[0:3]) self.write_file.flush() self.serv_finished.wait() def testSmallRead(self): # Performing small file read test first_seg = self.read_file.read(len(self.read_msg)-3) second_seg = self.read_file.read(3) msg = first_seg + second_seg self.assertEqual(msg, self.read_msg) def _testSmallRead(self): self.write_file.write(self.write_msg) self.write_file.flush() def testFullRead(self): # read until EOF msg = self.read_file.read() self.assertEqual(msg, self.read_msg) def _testFullRead(self): self.write_file.write(self.write_msg) self.write_file.close() def testUnbufferedRead(self): # Performing unbuffered file read test buf = type(self.read_msg)() while 1: char = self.read_file.read(1) if not char: break buf += char self.assertEqual(buf, self.read_msg) def _testUnbufferedRead(self): self.write_file.write(self.write_msg) self.write_file.flush() def testReadline(self): # Performing file readline test line = self.read_file.readline() self.assertEqual(line, self.read_msg) def _testReadline(self): self.write_file.write(self.write_msg) self.write_file.flush() def testCloseAfterMakefile(self): # The file returned by makefile should keep the socket open. self.cli_conn.close() # read until EOF msg = self.read_file.read() self.assertEqual(msg, self.read_msg) def _testCloseAfterMakefile(self): self.write_file.write(self.write_msg) self.write_file.flush() def testMakefileAfterMakefileClose(self): self.read_file.close() msg = self.cli_conn.recv(len(MSG)) if isinstance(self.read_msg, str): msg = msg.decode() self.assertEqual(msg, self.read_msg) def _testMakefileAfterMakefileClose(self): self.write_file.write(self.write_msg) self.write_file.flush() def testClosedAttr(self): self.assertTrue(not self.read_file.closed) def _testClosedAttr(self): self.assertTrue(not self.write_file.closed) def testAttributes(self): self.assertEqual(self.read_file.mode, self.read_mode) self.assertEqual(self.read_file.name, self.cli_conn.fileno()) def _testAttributes(self): self.assertEqual(self.write_file.mode, self.write_mode) self.assertEqual(self.write_file.name, self.serv_conn.fileno()) def testRealClose(self): self.read_file.close() self.assertRaises(ValueError, self.read_file.fileno) self.cli_conn.close() self.assertRaises(OSError, self.cli_conn.getsockname) def _testRealClose(self): pass class FileObjectInterruptedTestCase(unittest.TestCase): """Test that the file object correctly handles EINTR internally.""" class MockSocket(object): def __init__(self, recv_funcs=()): # A generator that returns callables that we'll call for each # call to recv(). self._recv_step = iter(recv_funcs) def recv_into(self, buffer): data = next(self._recv_step)() assert len(buffer) >= len(data) buffer[:len(data)] = data return len(data) def _decref_socketios(self): pass def _textiowrap_for_test(self, buffering=-1): raw = socket.SocketIO(self, "r") if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: return raw buffer = io.BufferedReader(raw, buffering) text = io.TextIOWrapper(buffer, None, None) text.mode = "rb" return text @staticmethod def _raise_eintr(): raise OSError(errno.EINTR, "interrupted") def _textiowrap_mock_socket(self, mock, buffering=-1): raw = socket.SocketIO(mock, "r") if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: return raw buffer = io.BufferedReader(raw, buffering) text = io.TextIOWrapper(buffer, None, None) text.mode = "rb" return text def _test_readline(self, size=-1, buffering=-1): mock_sock = self.MockSocket(recv_funcs=[ lambda : b"This is the first line\nAnd the sec", self._raise_eintr, lambda : b"ond line is here\n", lambda : b"", lambda : b"", # XXX(gps): io library does an extra EOF read ]) fo = mock_sock._textiowrap_for_test(buffering=buffering) self.assertEqual(fo.readline(size), "This is the first line\n") self.assertEqual(fo.readline(size), "And the second line is here\n") def _test_read(self, size=-1, buffering=-1): mock_sock = self.MockSocket(recv_funcs=[ lambda : b"This is the first line\nAnd the sec", self._raise_eintr, lambda : b"ond line is here\n", lambda : b"", lambda : b"", # XXX(gps): io library does an extra EOF read ]) expecting = (b"This is the first line\n" b"And the second line is here\n") fo = mock_sock._textiowrap_for_test(buffering=buffering) if buffering == 0: data = b'' else: data = '' expecting = expecting.decode('utf-8') while len(data) != len(expecting): part = fo.read(size) if not part: break data += part self.assertEqual(data, expecting) def test_default(self): self._test_readline() self._test_readline(size=100) self._test_read() self._test_read(size=100) def test_with_1k_buffer(self): self._test_readline(buffering=1024) self._test_readline(size=100, buffering=1024) self._test_read(buffering=1024) self._test_read(size=100, buffering=1024) def _test_readline_no_buffer(self, size=-1): mock_sock = self.MockSocket(recv_funcs=[ lambda : b"a", lambda : b"\n", lambda : b"B", self._raise_eintr, lambda : b"b", lambda : b"", ]) fo = mock_sock._textiowrap_for_test(buffering=0) self.assertEqual(fo.readline(size), b"a\n") self.assertEqual(fo.readline(size), b"Bb") def test_no_buffer(self): self._test_readline_no_buffer() self._test_readline_no_buffer(size=4) self._test_read(buffering=0) self._test_read(size=100, buffering=0) class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase): """Repeat the tests from FileObjectClassTestCase with bufsize==0. In this case (and in this case only), it should be possible to create a file object, read a line from it, create another file object, read another line from it, without loss of data in the first file object's buffer. Note that http.client relies on this when reading multiple requests from the same socket.""" bufsize = 0 # Use unbuffered mode def testUnbufferedReadline(self): # Read a line, create a new file object, read another line with it line = self.read_file.readline() # first line self.assertEqual(line, b"A. " + self.write_msg) # first line self.read_file = self.cli_conn.makefile('rb', 0) line = self.read_file.readline() # second line self.assertEqual(line, b"B. " + self.write_msg) # second line def _testUnbufferedReadline(self): self.write_file.write(b"A. " + self.write_msg) self.write_file.write(b"B. " + self.write_msg) self.write_file.flush() def testMakefileClose(self): # The file returned by makefile should keep the socket open... self.cli_conn.close() msg = self.cli_conn.recv(1024) self.assertEqual(msg, self.read_msg) # ...until the file is itself closed self.read_file.close() self.assertRaises(OSError, self.cli_conn.recv, 1024) def _testMakefileClose(self): self.write_file.write(self.write_msg) self.write_file.flush() def testMakefileCloseSocketDestroy(self): refcount_before = sys.getrefcount(self.cli_conn) self.read_file.close() refcount_after = sys.getrefcount(self.cli_conn) self.assertEqual(refcount_before - 1, refcount_after) def _testMakefileCloseSocketDestroy(self): pass # Non-blocking ops # NOTE: to set `read_file` as non-blocking, we must call # `cli_conn.setblocking` and vice-versa (see setUp / clientSetUp). def testSmallReadNonBlocking(self): self.cli_conn.setblocking(False) self.assertEqual(self.read_file.readinto(bytearray(10)), None) self.assertEqual(self.read_file.read(len(self.read_msg) - 3), None) self.evt1.set() self.evt2.wait(1.0) first_seg = self.read_file.read(len(self.read_msg) - 3) if first_seg is None: # Data not arrived (can happen under Windows), wait a bit time.sleep(0.5) first_seg = self.read_file.read(len(self.read_msg) - 3) buf = bytearray(10) n = self.read_file.readinto(buf) self.assertEqual(n, 3) msg = first_seg + buf[:n] self.assertEqual(msg, self.read_msg) self.assertEqual(self.read_file.readinto(bytearray(16)), None) self.assertEqual(self.read_file.read(1), None) def _testSmallReadNonBlocking(self): self.evt1.wait(1.0) self.write_file.write(self.write_msg) self.write_file.flush() self.evt2.set() # Avoid cloding the socket before the server test has finished, # otherwise system recv() will return 0 instead of EWOULDBLOCK. self.serv_finished.wait(5.0) def testWriteNonBlocking(self): self.cli_finished.wait(5.0) # The client thread can't skip directly - the SkipTest exception # would appear as a failure. if self.serv_skipped: self.skipTest(self.serv_skipped) def _testWriteNonBlocking(self): self.serv_skipped = None self.serv_conn.setblocking(False) # Try to saturate the socket buffer pipe with repeated large writes. BIG = b"x" * support.SOCK_MAX_SIZE LIMIT = 10 # The first write() succeeds since a chunk of data can be buffered n = self.write_file.write(BIG) self.assertGreater(n, 0) for i in range(LIMIT): n = self.write_file.write(BIG) if n is None: # Succeeded break self.assertGreater(n, 0) else: # Let us know that this test didn't manage to establish # the expected conditions. This is not a failure in itself but, # if it happens repeatedly, the test should be fixed. self.serv_skipped = "failed to saturate the socket buffer" class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase): bufsize = 1 # Default-buffered for reading; line-buffered for writing class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase): bufsize = 2 # Exercise the buffering code class UnicodeReadFileObjectClassTestCase(FileObjectClassTestCase): """Tests for socket.makefile() in text mode (rather than binary)""" read_mode = 'r' read_msg = MSG.decode('utf-8') write_mode = 'wb' write_msg = MSG newline = '' class UnicodeWriteFileObjectClassTestCase(FileObjectClassTestCase): """Tests for socket.makefile() in text mode (rather than binary)""" read_mode = 'rb' read_msg = MSG write_mode = 'w' write_msg = MSG.decode('utf-8') newline = '' class UnicodeReadWriteFileObjectClassTestCase(FileObjectClassTestCase): """Tests for socket.makefile() in text mode (rather than binary)""" read_mode = 'r' read_msg = MSG.decode('utf-8') write_mode = 'w' write_msg = MSG.decode('utf-8') newline = '' class NetworkConnectionTest(object): """Prove network connection.""" def clientSetUp(self): # We're inherited below by BasicTCPTest2, which also inherits # BasicTCPTest, which defines self.port referenced below. self.cli = socket.create_connection((HOST, self.port)) self.serv_conn = self.cli class BasicTCPTest2(NetworkConnectionTest, BasicTCPTest): """Tests that NetworkConnection does not break existing TCP functionality. """ class NetworkConnectionNoServer(unittest.TestCase): class MockSocket(socket.socket): def connect(self, *args): raise socket.timeout('timed out') @contextlib.contextmanager def mocked_socket_module(self): """Return a socket which times out on connect""" old_socket = socket.socket socket.socket = self.MockSocket try: yield finally: socket.socket = old_socket def test_connect(self): port = support.find_unused_port() cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(cli.close) with self.assertRaises(OSError) as cm: cli.connect((HOST, port)) self.assertEqual(cm.exception.errno, errno.ECONNREFUSED) def test_create_connection(self): # Issue #9792: errors raised by create_connection() should have # a proper errno attribute. port = support.find_unused_port() with self.assertRaises(OSError) as cm: socket.create_connection((HOST, port)) # Issue #16257: create_connection() calls getaddrinfo() against # 'localhost'. This may result in an IPV6 addr being returned # as well as an IPV4 one: # >>> socket.getaddrinfo('localhost', port, 0, SOCK_STREAM) # >>> [(2, 2, 0, '', ('127.0.0.1', 41230)), # (26, 2, 0, '', ('::1', 41230, 0, 0))] # # create_connection() enumerates through all the addresses returned # and if it doesn't successfully bind to any of them, it propagates # the last exception it encountered. # # On Solaris, ENETUNREACH is returned in this circumstance instead # of ECONNREFUSED. So, if that errno exists, add it to our list of # expected errnos. expected_errnos = [ errno.ECONNREFUSED, ] if hasattr(errno, 'ENETUNREACH'): expected_errnos.append(errno.ENETUNREACH) self.assertIn(cm.exception.errno, expected_errnos) def test_create_connection_timeout(self): # Issue #9792: create_connection() should not recast timeout errors # as generic socket errors. with self.mocked_socket_module(): with self.assertRaises(socket.timeout): socket.create_connection((HOST, 1234)) @unittest.skipUnless(thread, 'Threading required for this test.') class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest): def __init__(self, methodName='runTest'): SocketTCPTest.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): self.source_port = support.find_unused_port() def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) def _justAccept(self): conn, addr = self.serv.accept() conn.close() testFamily = _justAccept def _testFamily(self): self.cli = socket.create_connection((HOST, self.port), timeout=30) self.addCleanup(self.cli.close) self.assertEqual(self.cli.family, 2) testSourceAddress = _justAccept def _testSourceAddress(self): self.cli = socket.create_connection((HOST, self.port), timeout=30, source_address=('', self.source_port)) self.addCleanup(self.cli.close) self.assertEqual(self.cli.getsockname()[1], self.source_port) # The port number being used is sufficient to show that the bind() # call happened. testTimeoutDefault = _justAccept def _testTimeoutDefault(self): # passing no explicit timeout uses socket's global default self.assertTrue(socket.getdefaulttimeout() is None) socket.setdefaulttimeout(42) try: self.cli = socket.create_connection((HOST, self.port)) self.addCleanup(self.cli.close) finally: socket.setdefaulttimeout(None) self.assertEqual(self.cli.gettimeout(), 42) testTimeoutNone = _justAccept def _testTimeoutNone(self): # None timeout means the same as sock.settimeout(None) self.assertTrue(socket.getdefaulttimeout() is None) socket.setdefaulttimeout(30) try: self.cli = socket.create_connection((HOST, self.port), timeout=None) self.addCleanup(self.cli.close) finally: socket.setdefaulttimeout(None) self.assertEqual(self.cli.gettimeout(), None) testTimeoutValueNamed = _justAccept def _testTimeoutValueNamed(self): self.cli = socket.create_connection((HOST, self.port), timeout=30) self.assertEqual(self.cli.gettimeout(), 30) testTimeoutValueNonamed = _justAccept def _testTimeoutValueNonamed(self): self.cli = socket.create_connection((HOST, self.port), 30) self.addCleanup(self.cli.close) self.assertEqual(self.cli.gettimeout(), 30) @unittest.skipUnless(thread, 'Threading required for this test.') class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest): def __init__(self, methodName='runTest'): SocketTCPTest.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): pass def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) def testInsideTimeout(self): conn, addr = self.serv.accept() self.addCleanup(conn.close) time.sleep(3) conn.send(b"done!") testOutsideTimeout = testInsideTimeout def _testInsideTimeout(self): self.cli = sock = socket.create_connection((HOST, self.port)) data = sock.recv(5) self.assertEqual(data, b"done!") def _testOutsideTimeout(self): self.cli = sock = socket.create_connection((HOST, self.port), timeout=1) self.assertRaises(socket.timeout, lambda: sock.recv(5)) class TCPTimeoutTest(SocketTCPTest): def testTCPTimeout(self): def raise_timeout(*args, **kwargs): self.serv.settimeout(1.0) self.serv.accept() self.assertRaises(socket.timeout, raise_timeout, "Error generating a timeout exception (TCP)") def testTimeoutZero(self): ok = False try: self.serv.settimeout(0.0) foo = self.serv.accept() except socket.timeout: self.fail("caught timeout instead of error (TCP)") except OSError: ok = True except: self.fail("caught unexpected exception (TCP)") if not ok: self.fail("accept() returned success when we did not expect it") @unittest.skipUnless(hasattr(signal, 'alarm'), 'test needs signal.alarm()') def testInterruptedTimeout(self): # XXX I don't know how to do this test on MSWindows or any other # plaform that doesn't support signal.alarm() or os.kill(), though # the bug should have existed on all platforms. self.serv.settimeout(5.0) # must be longer than alarm class Alarm(Exception): pass def alarm_handler(signal, frame): raise Alarm old_alarm = signal.signal(signal.SIGALRM, alarm_handler) try: signal.alarm(2) # POSIX allows alarm to be up to 1 second early try: foo = self.serv.accept() except socket.timeout: self.fail("caught timeout instead of Alarm") except Alarm: pass except: self.fail("caught other exception instead of Alarm:" " %s(%s):\n%s" % (sys.exc_info()[:2] + (traceback.format_exc(),))) else: self.fail("nothing caught") finally: signal.alarm(0) # shut off alarm except Alarm: self.fail("got Alarm in wrong place") finally: # no alarm can be pending. Safe to restore old handler. signal.signal(signal.SIGALRM, old_alarm) class UDPTimeoutTest(SocketUDPTest): def testUDPTimeout(self): def raise_timeout(*args, **kwargs): self.serv.settimeout(1.0) self.serv.recv(1024) self.assertRaises(socket.timeout, raise_timeout, "Error generating a timeout exception (UDP)") def testTimeoutZero(self): ok = False try: self.serv.settimeout(0.0) foo = self.serv.recv(1024) except socket.timeout: self.fail("caught timeout instead of error (UDP)") except OSError: ok = True except: self.fail("caught unexpected exception (UDP)") if not ok: self.fail("recv() returned success when we did not expect it") class TestExceptions(unittest.TestCase): def testExceptionTree(self): self.assertTrue(issubclass(OSError, Exception)) self.assertTrue(issubclass(socket.herror, OSError)) self.assertTrue(issubclass(socket.gaierror, OSError)) self.assertTrue(issubclass(socket.timeout, OSError)) @unittest.skipUnless(sys.platform == 'linux', 'Linux specific test') class TestLinuxAbstractNamespace(unittest.TestCase): UNIX_PATH_MAX = 108 def testLinuxAbstractNamespace(self): address = b"\x00python-test-hello\x00\xff" with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s1: s1.bind(address) s1.listen(1) with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s2: s2.connect(s1.getsockname()) with s1.accept()[0] as s3: self.assertEqual(s1.getsockname(), address) self.assertEqual(s2.getpeername(), address) def testMaxName(self): address = b"\x00" + b"h" * (self.UNIX_PATH_MAX - 1) with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: s.bind(address) self.assertEqual(s.getsockname(), address) def testNameOverflow(self): address = "\x00" + "h" * self.UNIX_PATH_MAX with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: self.assertRaises(OSError, s.bind, address) def testStrName(self): # Check that an abstract name can be passed as a string. s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: s.bind("\x00python\x00test\x00") self.assertEqual(s.getsockname(), b"\x00python\x00test\x00") finally: s.close() @unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'test needs socket.AF_UNIX') class TestUnixDomain(unittest.TestCase): def setUp(self): self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) def tearDown(self): self.sock.close() def encoded(self, path): # Return the given path encoded in the file system encoding, # or skip the test if this is not possible. try: return os.fsencode(path) except UnicodeEncodeError: self.skipTest( "Pathname {0!a} cannot be represented in file " "system encoding {1!r}".format( path, sys.getfilesystemencoding())) def bind(self, sock, path): # Bind the socket try: sock.bind(path) except OSError as e: if str(e) == "AF_UNIX path too long": self.skipTest( "Pathname {0!a} is too long to serve as a AF_UNIX path" .format(path)) else: raise def testStrAddr(self): # Test binding to and retrieving a normal string pathname. path = os.path.abspath(support.TESTFN) self.bind(self.sock, path) self.addCleanup(support.unlink, path) self.assertEqual(self.sock.getsockname(), path) def testBytesAddr(self): # Test binding to a bytes pathname. path = os.path.abspath(support.TESTFN) self.bind(self.sock, self.encoded(path)) self.addCleanup(support.unlink, path) self.assertEqual(self.sock.getsockname(), path) def testSurrogateescapeBind(self): # Test binding to a valid non-ASCII pathname, with the # non-ASCII bytes supplied using surrogateescape encoding. path = os.path.abspath(support.TESTFN_UNICODE) b = self.encoded(path) self.bind(self.sock, b.decode("ascii", "surrogateescape")) self.addCleanup(support.unlink, path) self.assertEqual(self.sock.getsockname(), path) def testUnencodableAddr(self): # Test binding to a pathname that cannot be encoded in the # file system encoding. if support.TESTFN_UNENCODABLE is None: self.skipTest("No unencodable filename available") path = os.path.abspath(support.TESTFN_UNENCODABLE) self.bind(self.sock, path) self.addCleanup(support.unlink, path) self.assertEqual(self.sock.getsockname(), path) @unittest.skipUnless(thread, 'Threading required for this test.') class BufferIOTest(SocketConnectedTest): """ Test the buffer versions of socket.recv() and socket.send(). """ def __init__(self, methodName='runTest'): SocketConnectedTest.__init__(self, methodName=methodName) def testRecvIntoArray(self): buf = bytearray(1024) nbytes = self.cli_conn.recv_into(buf) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] self.assertEqual(msg, MSG) def _testRecvIntoArray(self): buf = bytes(MSG) self.serv_conn.send(buf) def testRecvIntoBytearray(self): buf = bytearray(1024) nbytes = self.cli_conn.recv_into(buf) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] self.assertEqual(msg, MSG) _testRecvIntoBytearray = _testRecvIntoArray def testRecvIntoMemoryview(self): buf = bytearray(1024) nbytes = self.cli_conn.recv_into(memoryview(buf)) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] self.assertEqual(msg, MSG) _testRecvIntoMemoryview = _testRecvIntoArray def testRecvFromIntoArray(self): buf = bytearray(1024) nbytes, addr = self.cli_conn.recvfrom_into(buf) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] self.assertEqual(msg, MSG) def _testRecvFromIntoArray(self): buf = bytes(MSG) self.serv_conn.send(buf) def testRecvFromIntoBytearray(self): buf = bytearray(1024) nbytes, addr = self.cli_conn.recvfrom_into(buf) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] self.assertEqual(msg, MSG) _testRecvFromIntoBytearray = _testRecvFromIntoArray def testRecvFromIntoMemoryview(self): buf = bytearray(1024) nbytes, addr = self.cli_conn.recvfrom_into(memoryview(buf)) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] self.assertEqual(msg, MSG) _testRecvFromIntoMemoryview = _testRecvFromIntoArray def testRecvFromIntoSmallBuffer(self): # See issue #20246. buf = bytearray(8) self.assertRaises(ValueError, self.cli_conn.recvfrom_into, buf, 1024) def _testRecvFromIntoSmallBuffer(self): self.serv_conn.send(MSG) def testRecvFromIntoEmptyBuffer(self): buf = bytearray() self.cli_conn.recvfrom_into(buf) self.cli_conn.recvfrom_into(buf, 0) _testRecvFromIntoEmptyBuffer = _testRecvFromIntoArray TIPC_STYPE = 2000 TIPC_LOWER = 200 TIPC_UPPER = 210 def isTipcAvailable(): """Check if the TIPC module is loaded The TIPC module is not loaded automatically on Ubuntu and probably other Linux distros. """ if not hasattr(socket, "AF_TIPC"): return False if not os.path.isfile("/proc/modules"): return False with open("/proc/modules") as f: for line in f: if line.startswith("tipc "): return True return False @unittest.skipUnless(isTipcAvailable(), "TIPC module is not loaded, please 'sudo modprobe tipc'") class TIPCTest(unittest.TestCase): def testRDM(self): srv = socket.socket(socket.AF_TIPC, socket.SOCK_RDM) cli = socket.socket(socket.AF_TIPC, socket.SOCK_RDM) self.addCleanup(srv.close) self.addCleanup(cli.close) srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE, TIPC_LOWER, TIPC_UPPER) srv.bind(srvaddr) sendaddr = (socket.TIPC_ADDR_NAME, TIPC_STYPE, TIPC_LOWER + int((TIPC_UPPER - TIPC_LOWER) / 2), 0) cli.sendto(MSG, sendaddr) msg, recvaddr = srv.recvfrom(1024) self.assertEqual(cli.getsockname(), recvaddr) self.assertEqual(msg, MSG) @unittest.skipUnless(isTipcAvailable(), "TIPC module is not loaded, please 'sudo modprobe tipc'") class TIPCThreadableTest(unittest.TestCase, ThreadableTest): def __init__(self, methodName = 'runTest'): unittest.TestCase.__init__(self, methodName = methodName) ThreadableTest.__init__(self) def setUp(self): self.srv = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM) self.addCleanup(self.srv.close) self.srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE, TIPC_LOWER, TIPC_UPPER) self.srv.bind(srvaddr) self.srv.listen(5) self.serverExplicitReady() self.conn, self.connaddr = self.srv.accept() self.addCleanup(self.conn.close) def clientSetUp(self): # The is a hittable race between serverExplicitReady() and the # accept() call; sleep a little while to avoid it, otherwise # we could get an exception time.sleep(0.1) self.cli = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM) self.addCleanup(self.cli.close) addr = (socket.TIPC_ADDR_NAME, TIPC_STYPE, TIPC_LOWER + int((TIPC_UPPER - TIPC_LOWER) / 2), 0) self.cli.connect(addr) self.cliaddr = self.cli.getsockname() def testStream(self): msg = self.conn.recv(1024) self.assertEqual(msg, MSG) self.assertEqual(self.cliaddr, self.connaddr) def _testStream(self): self.cli.send(MSG) self.cli.close() @unittest.skipUnless(thread, 'Threading required for this test.') class ContextManagersTest(ThreadedTCPSocketTest): def _testSocketClass(self): # base test with socket.socket() as sock: self.assertFalse(sock._closed) self.assertTrue(sock._closed) # close inside with block with socket.socket() as sock: sock.close() self.assertTrue(sock._closed) # exception inside with block with socket.socket() as sock: self.assertRaises(OSError, sock.sendall, b'foo') self.assertTrue(sock._closed) def testCreateConnectionBase(self): conn, addr = self.serv.accept() self.addCleanup(conn.close) data = conn.recv(1024) conn.sendall(data) def _testCreateConnectionBase(self): address = self.serv.getsockname() with socket.create_connection(address) as sock: self.assertFalse(sock._closed) sock.sendall(b'foo') self.assertEqual(sock.recv(1024), b'foo') self.assertTrue(sock._closed) def testCreateConnectionClose(self): conn, addr = self.serv.accept() self.addCleanup(conn.close) data = conn.recv(1024) conn.sendall(data) def _testCreateConnectionClose(self): address = self.serv.getsockname() with socket.create_connection(address) as sock: sock.close() self.assertTrue(sock._closed) self.assertRaises(OSError, sock.sendall, b'foo') class InheritanceTest(unittest.TestCase): @unittest.skipUnless(hasattr(socket, "SOCK_CLOEXEC"), "SOCK_CLOEXEC not defined") @support.requires_linux_version(2, 6, 28) def test_SOCK_CLOEXEC(self): with socket.socket(socket.AF_INET, socket.SOCK_STREAM | socket.SOCK_CLOEXEC) as s: self.assertTrue(s.type & socket.SOCK_CLOEXEC) self.assertFalse(s.get_inheritable()) def test_default_inheritable(self): sock = socket.socket() with sock: self.assertEqual(sock.get_inheritable(), False) def test_dup(self): sock = socket.socket() with sock: newsock = sock.dup() sock.close() with newsock: self.assertEqual(newsock.get_inheritable(), False) def test_set_inheritable(self): sock = socket.socket() with sock: sock.set_inheritable(True) self.assertEqual(sock.get_inheritable(), True) sock.set_inheritable(False) self.assertEqual(sock.get_inheritable(), False) @unittest.skipIf(fcntl is None, "need fcntl") def test_get_inheritable_cloexec(self): sock = socket.socket() with sock: fd = sock.fileno() self.assertEqual(sock.get_inheritable(), False) # clear FD_CLOEXEC flag flags = fcntl.fcntl(fd, fcntl.F_GETFD) flags &= ~fcntl.FD_CLOEXEC fcntl.fcntl(fd, fcntl.F_SETFD, flags) self.assertEqual(sock.get_inheritable(), True) @unittest.skipIf(fcntl is None, "need fcntl") def test_set_inheritable_cloexec(self): sock = socket.socket() with sock: fd = sock.fileno() self.assertEqual(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC, fcntl.FD_CLOEXEC) sock.set_inheritable(True) self.assertEqual(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC, 0) @unittest.skipUnless(hasattr(socket, "socketpair"), "need socket.socketpair()") def test_socketpair(self): s1, s2 = socket.socketpair() self.addCleanup(s1.close) self.addCleanup(s2.close) self.assertEqual(s1.get_inheritable(), False) self.assertEqual(s2.get_inheritable(), False) @unittest.skipUnless(hasattr(socket, "SOCK_NONBLOCK"), "SOCK_NONBLOCK not defined") class NonblockConstantTest(unittest.TestCase): def checkNonblock(self, s, nonblock=True, timeout=0.0): if nonblock: self.assertTrue(s.type & socket.SOCK_NONBLOCK) self.assertEqual(s.gettimeout(), timeout) else: self.assertFalse(s.type & socket.SOCK_NONBLOCK) self.assertEqual(s.gettimeout(), None) @support.requires_linux_version(2, 6, 28) def test_SOCK_NONBLOCK(self): # a lot of it seems silly and redundant, but I wanted to test that # changing back and forth worked ok with socket.socket(socket.AF_INET, socket.SOCK_STREAM | socket.SOCK_NONBLOCK) as s: self.checkNonblock(s) s.setblocking(1) self.checkNonblock(s, False) s.setblocking(0) self.checkNonblock(s) s.settimeout(None) self.checkNonblock(s, False) s.settimeout(2.0) self.checkNonblock(s, timeout=2.0) s.setblocking(1) self.checkNonblock(s, False) # defaulttimeout t = socket.getdefaulttimeout() socket.setdefaulttimeout(0.0) with socket.socket() as s: self.checkNonblock(s) socket.setdefaulttimeout(None) with socket.socket() as s: self.checkNonblock(s, False) socket.setdefaulttimeout(2.0) with socket.socket() as s: self.checkNonblock(s, timeout=2.0) socket.setdefaulttimeout(None) with socket.socket() as s: self.checkNonblock(s, False) socket.setdefaulttimeout(t) @unittest.skipUnless(os.name == "nt", "Windows specific") @unittest.skipUnless(multiprocessing, "need multiprocessing") class TestSocketSharing(SocketTCPTest): # This must be classmethod and not staticmethod or multiprocessing # won't be able to bootstrap it. @classmethod def remoteProcessServer(cls, q): # Recreate socket from shared data sdata = q.get() message = q.get() s = socket.fromshare(sdata) s2, c = s.accept() # Send the message s2.sendall(message) s2.close() s.close() def testShare(self): # Transfer the listening server socket to another process # and service it from there. # Create process: q = multiprocessing.Queue() p = multiprocessing.Process(target=self.remoteProcessServer, args=(q,)) p.start() # Get the shared socket data data = self.serv.share(p.pid) # Pass the shared socket to the other process addr = self.serv.getsockname() self.serv.close() q.put(data) # The data that the server will send us message = b"slapmahfro" q.put(message) # Connect s = socket.create_connection(addr) # listen for the data m = [] while True: data = s.recv(100) if not data: break m.append(data) s.close() received = b"".join(m) self.assertEqual(received, message) p.join() def testShareLength(self): data = self.serv.share(os.getpid()) self.assertRaises(ValueError, socket.fromshare, data[:-1]) self.assertRaises(ValueError, socket.fromshare, data+b"foo") def compareSockets(self, org, other): # socket sharing is expected to work only for blocking socket # since the internal python timout value isn't transfered. self.assertEqual(org.gettimeout(), None) self.assertEqual(org.gettimeout(), other.gettimeout()) self.assertEqual(org.family, other.family) self.assertEqual(org.type, other.type) # If the user specified "0" for proto, then # internally windows will have picked the correct value. # Python introspection on the socket however will still return # 0. For the shared socket, the python value is recreated # from the actual value, so it may not compare correctly. if org.proto != 0: self.assertEqual(org.proto, other.proto) def testShareLocal(self): data = self.serv.share(os.getpid()) s = socket.fromshare(data) try: self.compareSockets(self.serv, s) finally: s.close() def testTypes(self): families = [socket.AF_INET, socket.AF_INET6] types = [socket.SOCK_STREAM, socket.SOCK_DGRAM] for f in families: for t in types: try: source = socket.socket(f, t) except OSError: continue # This combination is not supported try: data = source.share(os.getpid()) shared = socket.fromshare(data) try: self.compareSockets(source, shared) finally: shared.close() finally: source.close() def test_main(): tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest, TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, UDPTimeoutTest ] tests.extend([ NonBlockingTCPTests, FileObjectClassTestCase, FileObjectInterruptedTestCase, UnbufferedFileObjectClassTestCase, LineBufferedFileObjectClassTestCase, SmallBufferedFileObjectClassTestCase, UnicodeReadFileObjectClassTestCase, UnicodeWriteFileObjectClassTestCase, UnicodeReadWriteFileObjectClassTestCase, NetworkConnectionNoServer, NetworkConnectionAttributesTest, NetworkConnectionBehaviourTest, ContextManagersTest, InheritanceTest, NonblockConstantTest ]) tests.append(BasicSocketPairTest) tests.append(TestUnixDomain) tests.append(TestLinuxAbstractNamespace) tests.extend([TIPCTest, TIPCThreadableTest]) tests.extend([BasicCANTest, CANTest]) tests.extend([BasicRDSTest, RDSTest]) tests.extend([ CmsgMacroTests, SendmsgUDPTest, RecvmsgUDPTest, RecvmsgIntoUDPTest, SendmsgUDP6Test, RecvmsgUDP6Test, RecvmsgRFC3542AncillaryUDP6Test, RecvmsgIntoRFC3542AncillaryUDP6Test, RecvmsgIntoUDP6Test, SendmsgTCPTest, RecvmsgTCPTest, RecvmsgIntoTCPTest, SendmsgSCTPStreamTest, RecvmsgSCTPStreamTest, RecvmsgIntoSCTPStreamTest, SendmsgUnixStreamTest, RecvmsgUnixStreamTest, RecvmsgIntoUnixStreamTest, RecvmsgSCMRightsStreamTest, RecvmsgIntoSCMRightsStreamTest, # These are slow when setitimer() is not available InterruptedRecvTimeoutTest, InterruptedSendTimeoutTest, TestSocketSharing, ]) thread_info = support.threading_setup() support.run_unittest(*tests) support.threading_cleanup(*thread_info) if __name__ == "__main__": test_main() >>>>>>> b875702c9c06ab5012e52ff4337439b03918f453 ======= import unittest from test import support import errno import io import itertools import socket import select import tempfile import time import traceback import queue import sys import os import array import platform import contextlib from weakref import proxy import signal import math import pickle import struct try: import multiprocessing except ImportError: multiprocessing = False try: import fcntl except ImportError: fcntl = None HOST = support.HOST MSG = 'Michael Gilfix was here\u1234\r\n'.encode('utf-8') ## test unicode string and carriage return try: import _thread as thread import threading except ImportError: thread = None threading = None try: import _socket except ImportError: _socket = None def _have_socket_can(): """Check whether CAN sockets are supported on this host.""" try: s = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) except (AttributeError, OSError): return False else: s.close() return True def _have_socket_rds(): """Check whether RDS sockets are supported on this host.""" try: s = socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) except (AttributeError, OSError): return False else: s.close() return True HAVE_SOCKET_CAN = _have_socket_can() HAVE_SOCKET_RDS = _have_socket_rds() # Size in bytes of the int type SIZEOF_INT = array.array("i").itemsize class SocketTCPTest(unittest.TestCase): def setUp(self): self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.port = support.bind_port(self.serv) self.serv.listen(1) def tearDown(self): self.serv.close() self.serv = None class SocketUDPTest(unittest.TestCase): def setUp(self): self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.port = support.bind_port(self.serv) def tearDown(self): self.serv.close() self.serv = None class ThreadSafeCleanupTestCase(unittest.TestCase): """Subclass of unittest.TestCase with thread-safe cleanup methods. This subclass protects the addCleanup() and doCleanups() methods with a recursive lock. """ if threading: def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._cleanup_lock = threading.RLock() def addCleanup(self, *args, **kwargs): with self._cleanup_lock: return super().addCleanup(*args, **kwargs) def doCleanups(self, *args, **kwargs): with self._cleanup_lock: return super().doCleanups(*args, **kwargs) class SocketCANTest(unittest.TestCase): """To be able to run this test, a `vcan0` CAN interface can be created with the following commands: # modprobe vcan # ip link add dev vcan0 type vcan # ifconfig vcan0 up """ interface = 'vcan0' bufsize = 128 """The CAN frame structure is defined in <linux/can.h>: struct can_frame { canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ __u8 can_dlc; /* data length code: 0 .. 8 */ __u8 data[8] __attribute__((aligned(8))); }; """ can_frame_fmt = "=IB3x8s" can_frame_size = struct.calcsize(can_frame_fmt) """The Broadcast Management Command frame structure is defined in <linux/can/bcm.h>: struct bcm_msg_head { __u32 opcode; __u32 flags; __u32 count; struct timeval ival1, ival2; canid_t can_id; __u32 nframes; struct can_frame frames[0]; } `bcm_msg_head` must be 8 bytes aligned because of the `frames` member (see `struct can_frame` definition). Must use native not standard types for packing. """ bcm_cmd_msg_fmt = "@3I4l2I" bcm_cmd_msg_fmt += "x" * (struct.calcsize(bcm_cmd_msg_fmt) % 8) def setUp(self): self.s = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) self.addCleanup(self.s.close) try: self.s.bind((self.interface,)) except OSError: self.skipTest('network interface `%s` does not exist' % self.interface) class SocketRDSTest(unittest.TestCase): """To be able to run this test, the `rds` kernel module must be loaded: # modprobe rds """ bufsize = 8192 def setUp(self): self.serv = socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) self.addCleanup(self.serv.close) try: self.port = support.bind_port(self.serv) except OSError: self.skipTest('unable to bind RDS socket') class ThreadableTest: """Threadable Test class The ThreadableTest class makes it easy to create a threaded client/server pair from an existing unit test. To create a new threaded class from an existing unit test, use multiple inheritance: class NewClass (OldClass, ThreadableTest): pass This class defines two new fixture functions with obvious purposes for overriding: clientSetUp () clientTearDown () Any new test functions within the class must then define tests in pairs, where the test name is preceeded with a '_' to indicate the client portion of the test. Ex: def testFoo(self): # Server portion def _testFoo(self): # Client portion Any exceptions raised by the clients during their tests are caught and transferred to the main thread to alert the testing framework. Note, the server setup function cannot call any blocking functions that rely on the client thread during setup, unless serverExplicitReady() is called just before the blocking call (such as in setting up a client/server connection and performing the accept() in setUp(). """ def __init__(self): # Swap the true setup function self.__setUp = self.setUp self.__tearDown = self.tearDown self.setUp = self._setUp self.tearDown = self._tearDown def serverExplicitReady(self): """This method allows the server to explicitly indicate that it wants the client thread to proceed. This is useful if the server is about to execute a blocking routine that is dependent upon the client thread during its setup routine.""" self.server_ready.set() def _setUp(self): self.server_ready = threading.Event() self.client_ready = threading.Event() self.done = threading.Event() self.queue = queue.Queue(1) self.server_crashed = False # Do some munging to start the client test. methodname = self.id() i = methodname.rfind('.') methodname = methodname[i+1:] test_method = getattr(self, '_' + methodname) self.client_thread = thread.start_new_thread( self.clientRun, (test_method,)) try: self.__setUp() except: self.server_crashed = True raise finally: self.server_ready.set() self.client_ready.wait() def _tearDown(self): self.__tearDown() self.done.wait() if self.queue.qsize(): exc = self.queue.get() raise exc def clientRun(self, test_func): self.server_ready.wait() self.clientSetUp() self.client_ready.set() if self.server_crashed: self.clientTearDown() return if not hasattr(test_func, '__call__'): raise TypeError("test_func must be a callable function") try: test_func() except BaseException as e: self.queue.put(e) finally: self.clientTearDown() def clientSetUp(self): raise NotImplementedError("clientSetUp must be implemented.") def clientTearDown(self): self.done.set() thread.exit() class ThreadedTCPSocketTest(SocketTCPTest, ThreadableTest): def __init__(self, methodName='runTest'): SocketTCPTest.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): self.cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) class ThreadedUDPSocketTest(SocketUDPTest, ThreadableTest): def __init__(self, methodName='runTest'): SocketUDPTest.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): self.cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) class ThreadedCANSocketTest(SocketCANTest, ThreadableTest): def __init__(self, methodName='runTest'): SocketCANTest.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): self.cli = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) try: self.cli.bind((self.interface,)) except OSError: # skipTest should not be called here, and will be called in the # server instead pass def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) class ThreadedRDSSocketTest(SocketRDSTest, ThreadableTest): def __init__(self, methodName='runTest'): SocketRDSTest.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): self.cli = socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) try: # RDS sockets must be bound explicitly to send or receive data self.cli.bind((HOST, 0)) self.cli_addr = self.cli.getsockname() except OSError: # skipTest should not be called here, and will be called in the # server instead pass def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) class SocketConnectedTest(ThreadedTCPSocketTest): """Socket tests for client-server connection. self.cli_conn is a client socket connected to the server. The setUp() method guarantees that it is connected to the server. """ def __init__(self, methodName='runTest'): ThreadedTCPSocketTest.__init__(self, methodName=methodName) def setUp(self): ThreadedTCPSocketTest.setUp(self) # Indicate explicitly we're ready for the client thread to # proceed and then perform the blocking call to accept self.serverExplicitReady() conn, addr = self.serv.accept() self.cli_conn = conn def tearDown(self): self.cli_conn.close() self.cli_conn = None ThreadedTCPSocketTest.tearDown(self) def clientSetUp(self): ThreadedTCPSocketTest.clientSetUp(self) self.cli.connect((HOST, self.port)) self.serv_conn = self.cli def clientTearDown(self): self.serv_conn.close() self.serv_conn = None ThreadedTCPSocketTest.clientTearDown(self) class SocketPairTest(unittest.TestCase, ThreadableTest): def __init__(self, methodName='runTest'): unittest.TestCase.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def setUp(self): self.serv, self.cli = socket.socketpair() def tearDown(self): self.serv.close() self.serv = None def clientSetUp(self): pass def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) # The following classes are used by the sendmsg()/recvmsg() tests. # Combining, for instance, ConnectedStreamTestMixin and TCPTestBase # gives a drop-in replacement for SocketConnectedTest, but different # address families can be used, and the attributes serv_addr and # cli_addr will be set to the addresses of the endpoints. class SocketTestBase(unittest.TestCase): """A base class for socket tests. Subclasses must provide methods newSocket() to return a new socket and bindSock(sock) to bind it to an unused address. Creates a socket self.serv and sets self.serv_addr to its address. """ def setUp(self): self.serv = self.newSocket() self.bindServer() def bindServer(self): """Bind server socket and set self.serv_addr to its address.""" self.bindSock(self.serv) self.serv_addr = self.serv.getsockname() def tearDown(self): self.serv.close() self.serv = None class SocketListeningTestMixin(SocketTestBase): """Mixin to listen on the server socket.""" def setUp(self): super().setUp() self.serv.listen(1) class ThreadedSocketTestMixin(ThreadSafeCleanupTestCase, SocketTestBase, ThreadableTest): """Mixin to add client socket and allow client/server tests. Client socket is self.cli and its address is self.cli_addr. See ThreadableTest for usage information. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) ThreadableTest.__init__(self) def clientSetUp(self): self.cli = self.newClientSocket() self.bindClient() def newClientSocket(self): """Return a new socket for use as client.""" return self.newSocket() def bindClient(self): """Bind client socket and set self.cli_addr to its address.""" self.bindSock(self.cli) self.cli_addr = self.cli.getsockname() def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) class ConnectedStreamTestMixin(SocketListeningTestMixin, ThreadedSocketTestMixin): """Mixin to allow client/server stream tests with connected client. Server's socket representing connection to client is self.cli_conn and client's connection to server is self.serv_conn. (Based on SocketConnectedTest.) """ def setUp(self): super().setUp() # Indicate explicitly we're ready for the client thread to # proceed and then perform the blocking call to accept self.serverExplicitReady() conn, addr = self.serv.accept() self.cli_conn = conn def tearDown(self): self.cli_conn.close() self.cli_conn = None super().tearDown() def clientSetUp(self): super().clientSetUp() self.cli.connect(self.serv_addr) self.serv_conn = self.cli def clientTearDown(self): self.serv_conn.close() self.serv_conn = None super().clientTearDown() class UnixSocketTestBase(SocketTestBase): """Base class for Unix-domain socket tests.""" # This class is used for file descriptor passing tests, so we # create the sockets in a private directory so that other users # can't send anything that might be problematic for a privileged # user running the tests. def setUp(self): self.dir_path = tempfile.mkdtemp() self.addCleanup(os.rmdir, self.dir_path) super().setUp() def bindSock(self, sock): path = tempfile.mktemp(dir=self.dir_path) sock.bind(path) self.addCleanup(support.unlink, path) class UnixStreamBase(UnixSocketTestBase): """Base class for Unix-domain SOCK_STREAM tests.""" def newSocket(self): return socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) class InetTestBase(SocketTestBase): """Base class for IPv4 socket tests.""" host = HOST def setUp(self): super().setUp() self.port = self.serv_addr[1] def bindSock(self, sock): support.bind_port(sock, host=self.host) class TCPTestBase(InetTestBase): """Base class for TCP-over-IPv4 tests.""" def newSocket(self): return socket.socket(socket.AF_INET, socket.SOCK_STREAM) class UDPTestBase(InetTestBase): """Base class for UDP-over-IPv4 tests.""" def newSocket(self): return socket.socket(socket.AF_INET, socket.SOCK_DGRAM) class SCTPStreamBase(InetTestBase): """Base class for SCTP tests in one-to-one (SOCK_STREAM) mode.""" def newSocket(self): return socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_SCTP) class Inet6TestBase(InetTestBase): """Base class for IPv6 socket tests.""" host = support.HOSTv6 class UDP6TestBase(Inet6TestBase): """Base class for UDP-over-IPv6 tests.""" def newSocket(self): return socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) # Test-skipping decorators for use with ThreadableTest. def skipWithClientIf(condition, reason): """Skip decorated test if condition is true, add client_skip decorator. If the decorated object is not a class, sets its attribute "client_skip" to a decorator which will return an empty function if the test is to be skipped, or the original function if it is not. This can be used to avoid running the client part of a skipped test when using ThreadableTest. """ def client_pass(*args, **kwargs): pass def skipdec(obj): retval = unittest.skip(reason)(obj) if not isinstance(obj, type): retval.client_skip = lambda f: client_pass return retval def noskipdec(obj): if not (isinstance(obj, type) or hasattr(obj, "client_skip")): obj.client_skip = lambda f: f return obj return skipdec if condition else noskipdec def requireAttrs(obj, *attributes): """Skip decorated test if obj is missing any of the given attributes. Sets client_skip attribute as skipWithClientIf() does. """ missing = [name for name in attributes if not hasattr(obj, name)] return skipWithClientIf( missing, "don't have " + ", ".join(name for name in missing)) def requireSocket(*args): """Skip decorated test if a socket cannot be created with given arguments. When an argument is given as a string, will use the value of that attribute of the socket module, or skip the test if it doesn't exist. Sets client_skip attribute as skipWithClientIf() does. """ err = None missing = [obj for obj in args if isinstance(obj, str) and not hasattr(socket, obj)] if missing: err = "don't have " + ", ".join(name for name in missing) else: callargs = [getattr(socket, obj) if isinstance(obj, str) else obj for obj in args] try: s = socket.socket(*callargs) except OSError as e: # XXX: check errno? err = str(e) else: s.close() return skipWithClientIf( err is not None, "can't create socket({0}): {1}".format( ", ".join(str(o) for o in args), err)) ####################################################################### ## Begin Tests class GeneralModuleTests(unittest.TestCase): def test_SocketType_is_socketobject(self): import _socket self.assertTrue(socket.SocketType is _socket.socket) s = socket.socket() self.assertIsInstance(s, socket.SocketType) s.close() def test_repr(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) with s: self.assertIn('fd=%i' % s.fileno(), repr(s)) self.assertIn('family=%s' % socket.AF_INET, repr(s)) self.assertIn('type=%s' % socket.SOCK_STREAM, repr(s)) self.assertIn('proto=0', repr(s)) self.assertNotIn('raddr', repr(s)) s.bind(('127.0.0.1', 0)) self.assertIn('laddr', repr(s)) self.assertIn(str(s.getsockname()), repr(s)) self.assertIn('[closed]', repr(s)) self.assertNotIn('laddr', repr(s)) @unittest.skipUnless(_socket is not None, 'need _socket module') def test_csocket_repr(self): s = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM) try: expected = ('<socket object, fd=%s, family=%s, type=%s, proto=%s>' % (s.fileno(), s.family, s.type, s.proto)) self.assertEqual(repr(s), expected) finally: s.close() expected = ('<socket object, fd=-1, family=%s, type=%s, proto=%s>' % (s.family, s.type, s.proto)) self.assertEqual(repr(s), expected) def test_weakref(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) p = proxy(s) self.assertEqual(p.fileno(), s.fileno()) s.close() s = None try: p.fileno() except ReferenceError: pass else: self.fail('Socket proxy still exists') def testSocketError(self): # Testing socket module exceptions msg = "Error raising socket exception (%s)." with self.assertRaises(OSError, msg=msg % 'OSError'): raise OSError with self.assertRaises(OSError, msg=msg % 'socket.herror'): raise socket.herror with self.assertRaises(OSError, msg=msg % 'socket.gaierror'): raise socket.gaierror def testSendtoErrors(self): # Testing that sendto doens't masks failures. See #10169. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.addCleanup(s.close) s.bind(('', 0)) sockname = s.getsockname() # 2 args with self.assertRaises(TypeError) as cm: s.sendto('\u2620', sockname) self.assertEqual(str(cm.exception), "'str' does not support the buffer interface") with self.assertRaises(TypeError) as cm: s.sendto(5j, sockname) self.assertEqual(str(cm.exception), "'complex' does not support the buffer interface") with self.assertRaises(TypeError) as cm: s.sendto(b'foo', None) self.assertIn('not NoneType',str(cm.exception)) # 3 args with self.assertRaises(TypeError) as cm: s.sendto('\u2620', 0, sockname) self.assertEqual(str(cm.exception), "'str' does not support the buffer interface") with self.assertRaises(TypeError) as cm: s.sendto(5j, 0, sockname) self.assertEqual(str(cm.exception), "'complex' does not support the buffer interface") with self.assertRaises(TypeError) as cm: s.sendto(b'foo', 0, None) self.assertIn('not NoneType', str(cm.exception)) with self.assertRaises(TypeError) as cm: s.sendto(b'foo', 'bar', sockname) self.assertIn('an integer is required', str(cm.exception)) with self.assertRaises(TypeError) as cm: s.sendto(b'foo', None, None) self.assertIn('an integer is required', str(cm.exception)) # wrong number of args with self.assertRaises(TypeError) as cm: s.sendto(b'foo') self.assertIn('(1 given)', str(cm.exception)) with self.assertRaises(TypeError) as cm: s.sendto(b'foo', 0, sockname, 4) self.assertIn('(4 given)', str(cm.exception)) def testCrucialConstants(self): # Testing for mission critical constants socket.AF_INET socket.SOCK_STREAM socket.SOCK_DGRAM socket.SOCK_RAW socket.SOCK_RDM socket.SOCK_SEQPACKET socket.SOL_SOCKET socket.SO_REUSEADDR def testHostnameRes(self): # Testing hostname resolution mechanisms hostname = socket.gethostname() try: ip = socket.gethostbyname(hostname) except OSError: # Probably name lookup wasn't set up right; skip this test self.skipTest('name lookup failure') self.assertTrue(ip.find('.') >= 0, "Error resolving host to ip.") try: hname, aliases, ipaddrs = socket.gethostbyaddr(ip) except OSError: # Probably a similar problem as above; skip this test self.skipTest('name lookup failure') all_host_names = [hostname, hname] + aliases fqhn = socket.getfqdn(ip) if not fqhn in all_host_names: self.fail("Error testing host resolution mechanisms. (fqdn: %s, all: %s)" % (fqhn, repr(all_host_names))) def test_host_resolution(self): for addr in ['0.1.1.~1', '1+.1.1.1', '::1q', '::1::2', '1:1:1:1:1:1:1:1:1']: self.assertRaises(OSError, socket.gethostbyname, addr) self.assertRaises(OSError, socket.gethostbyaddr, addr) for addr in [support.HOST, '10.0.0.1', '255.255.255.255']: self.assertEqual(socket.gethostbyname(addr), addr) # we don't test support.HOSTv6 because there's a chance it doesn't have # a matching name entry (e.g. 'ip6-localhost') for host in [support.HOST]: self.assertIn(host, socket.gethostbyaddr(host)[2]) @unittest.skipUnless(hasattr(socket, 'sethostname'), "test needs socket.sethostname()") @unittest.skipUnless(hasattr(socket, 'gethostname'), "test needs socket.gethostname()") def test_sethostname(self): oldhn = socket.gethostname() try: socket.sethostname('new') except OSError as e: if e.errno == errno.EPERM: self.skipTest("test should be run as root") else: raise try: # running test as root! self.assertEqual(socket.gethostname(), 'new') # Should work with bytes objects too socket.sethostname(b'bar') self.assertEqual(socket.gethostname(), 'bar') finally: socket.sethostname(oldhn) @unittest.skipUnless(hasattr(socket, 'if_nameindex'), 'socket.if_nameindex() not available.') def testInterfaceNameIndex(self): interfaces = socket.if_nameindex() for index, name in interfaces: self.assertIsInstance(index, int) self.assertIsInstance(name, str) # interface indices are non-zero integers self.assertGreater(index, 0) _index = socket.if_nametoindex(name) self.assertIsInstance(_index, int) self.assertEqual(index, _index) _name = socket.if_indextoname(index) self.assertIsInstance(_name, str) self.assertEqual(name, _name) @unittest.skipUnless(hasattr(socket, 'if_nameindex'), 'socket.if_nameindex() not available.') def testInvalidInterfaceNameIndex(self): # test nonexistent interface index/name self.assertRaises(OSError, socket.if_indextoname, 0) self.assertRaises(OSError, socket.if_nametoindex, '_DEADBEEF') # test with invalid values self.assertRaises(TypeError, socket.if_nametoindex, 0) self.assertRaises(TypeError, socket.if_indextoname, '_DEADBEEF') @unittest.skipUnless(hasattr(sys, 'getrefcount'), 'test needs sys.getrefcount()') def testRefCountGetNameInfo(self): # Testing reference count for getnameinfo try: # On some versions, this loses a reference orig = sys.getrefcount(__name__) socket.getnameinfo(__name__,0) except TypeError: if sys.getrefcount(__name__) != orig: self.fail("socket.getnameinfo loses a reference") def testInterpreterCrash(self): # Making sure getnameinfo doesn't crash the interpreter try: # On some versions, this crashes the interpreter. socket.getnameinfo(('x', 0, 0, 0), 0) except OSError: pass def testNtoH(self): # This just checks that htons etc. are their own inverse, # when looking at the lower 16 or 32 bits. sizes = {socket.htonl: 32, socket.ntohl: 32, socket.htons: 16, socket.ntohs: 16} for func, size in sizes.items(): mask = (1<<size) - 1 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): self.assertEqual(i & mask, func(func(i&mask)) & mask) swapped = func(mask) self.assertEqual(swapped & mask, mask) self.assertRaises(OverflowError, func, 1<<34) def testNtoHErrors(self): good_values = [ 1, 2, 3, 1, 2, 3 ] bad_values = [ -1, -2, -3, -1, -2, -3 ] for k in good_values: socket.ntohl(k) socket.ntohs(k) socket.htonl(k) socket.htons(k) for k in bad_values: self.assertRaises(OverflowError, socket.ntohl, k) self.assertRaises(OverflowError, socket.ntohs, k) self.assertRaises(OverflowError, socket.htonl, k) self.assertRaises(OverflowError, socket.htons, k) def testGetServBy(self): eq = self.assertEqual # Find one service that exists, then check all the related interfaces. # I've ordered this by protocols that have both a tcp and udp # protocol, at least for modern Linuxes. if (sys.platform.startswith(('freebsd', 'netbsd', 'gnukfreebsd')) or sys.platform in ('linux', 'darwin')): # avoid the 'echo' service on this platform, as there is an # assumption breaking non-standard port/protocol entry services = ('daytime', 'qotd', 'domain') else: services = ('echo', 'daytime', 'domain') for service in services: try: port = socket.getservbyname(service, 'tcp') break except OSError: pass else: raise OSError # Try same call with optional protocol omitted port2 = socket.getservbyname(service) eq(port, port2) # Try udp, but don't barf if it doesn't exist try: udpport = socket.getservbyname(service, 'udp') except OSError: udpport = None else: eq(udpport, port) # Now make sure the lookup by port returns the same service name eq(socket.getservbyport(port2), service) eq(socket.getservbyport(port, 'tcp'), service) if udpport is not None: eq(socket.getservbyport(udpport, 'udp'), service) # Make sure getservbyport does not accept out of range ports. self.assertRaises(OverflowError, socket.getservbyport, -1) self.assertRaises(OverflowError, socket.getservbyport, 65536) def testDefaultTimeout(self): # Testing default timeout # The default timeout should initially be None self.assertEqual(socket.getdefaulttimeout(), None) s = socket.socket() self.assertEqual(s.gettimeout(), None) s.close() # Set the default timeout to 10, and see if it propagates socket.setdefaulttimeout(10) self.assertEqual(socket.getdefaulttimeout(), 10) s = socket.socket() self.assertEqual(s.gettimeout(), 10) s.close() # Reset the default timeout to None, and see if it propagates socket.setdefaulttimeout(None) self.assertEqual(socket.getdefaulttimeout(), None) s = socket.socket() self.assertEqual(s.gettimeout(), None) s.close() # Check that setting it to an invalid value raises ValueError self.assertRaises(ValueError, socket.setdefaulttimeout, -1) # Check that setting it to an invalid type raises TypeError self.assertRaises(TypeError, socket.setdefaulttimeout, "spam") @unittest.skipUnless(hasattr(socket, 'inet_aton'), 'test needs socket.inet_aton()') def testIPv4_inet_aton_fourbytes(self): # Test that issue1008086 and issue767150 are fixed. # It must return 4 bytes. self.assertEqual(b'\x00'*4, socket.inet_aton('0.0.0.0')) self.assertEqual(b'\xff'*4, socket.inet_aton('255.255.255.255')) @unittest.skipUnless(hasattr(socket, 'inet_pton'), 'test needs socket.inet_pton()') def testIPv4toString(self): from socket import inet_aton as f, inet_pton, AF_INET g = lambda a: inet_pton(AF_INET, a) assertInvalid = lambda func,a: self.assertRaises( (OSError, ValueError), func, a ) self.assertEqual(b'\x00\x00\x00\x00', f('0.0.0.0')) self.assertEqual(b'\xff\x00\xff\x00', f('255.0.255.0')) self.assertEqual(b'\xaa\xaa\xaa\xaa', f('170.170.170.170')) self.assertEqual(b'\x01\x02\x03\x04', f('1.2.3.4')) self.assertEqual(b'\xff\xff\xff\xff', f('255.255.255.255')) assertInvalid(f, '0.0.0.') assertInvalid(f, '300.0.0.0') assertInvalid(f, 'a.0.0.0') assertInvalid(f, '1.2.3.4.5') assertInvalid(f, '::1') self.assertEqual(b'\x00\x00\x00\x00', g('0.0.0.0')) self.assertEqual(b'\xff\x00\xff\x00', g('255.0.255.0')) self.assertEqual(b'\xaa\xaa\xaa\xaa', g('170.170.170.170')) self.assertEqual(b'\xff\xff\xff\xff', g('255.255.255.255')) assertInvalid(g, '0.0.0.') assertInvalid(g, '300.0.0.0') assertInvalid(g, 'a.0.0.0') assertInvalid(g, '1.2.3.4.5') assertInvalid(g, '::1') @unittest.skipUnless(hasattr(socket, 'inet_pton'), 'test needs socket.inet_pton()') def testIPv6toString(self): try: from socket import inet_pton, AF_INET6, has_ipv6 if not has_ipv6: self.skipTest('IPv6 not available') except ImportError: self.skipTest('could not import needed symbols from socket') if sys.platform == "win32": try: inet_pton(AF_INET6, '::') except OSError as e: if e.winerror == 10022: self.skipTest('IPv6 might not be supported') f = lambda a: inet_pton(AF_INET6, a) assertInvalid = lambda a: self.assertRaises( (OSError, ValueError), f, a ) self.assertEqual(b'\x00' * 16, f('::')) self.assertEqual(b'\x00' * 16, f('0::0')) self.assertEqual(b'\x00\x01' + b'\x00' * 14, f('1::')) self.assertEqual( b'\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae', f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae') ) self.assertEqual( b'\xad\x42\x0a\xbc' + b'\x00' * 4 + b'\x01\x27\x00\x00\x02\x54\x00\x02', f('ad42:abc::127:0:254:2') ) self.assertEqual(b'\x00\x12\x00\x0a' + b'\x00' * 12, f('12:a::')) assertInvalid('0x20::') assertInvalid(':::') assertInvalid('::0::') assertInvalid('1::abc::') assertInvalid('1::abc::def') assertInvalid('1:2:3:4:5:6:') assertInvalid('1:2:3:4:5:6') assertInvalid('1:2:3:4:5:6:7:8:') assertInvalid('1:2:3:4:5:6:7:8:0') self.assertEqual(b'\x00' * 12 + b'\xfe\x2a\x17\x40', f('::254.42.23.64') ) self.assertEqual( b'\x00\x42' + b'\x00' * 8 + b'\xa2\x9b\xfe\x2a\x17\x40', f('42::a29b:254.42.23.64') ) self.assertEqual( b'\x00\x42\xa8\xb9\x00\x00\x00\x02\xff\xff\xa2\x9b\xfe\x2a\x17\x40', f('42:a8b9:0:2:ffff:a29b:254.42.23.64') ) assertInvalid('255.254.253.252') assertInvalid('1::260.2.3.0') assertInvalid('1::0.be.e.0') assertInvalid('1:2:3:4:5:6:7:1.2.3.4') assertInvalid('::1.2.3.4:0') assertInvalid('0.100.200.0:3:4:5:6:7:8') @unittest.skipUnless(hasattr(socket, 'inet_ntop'), 'test needs socket.inet_ntop()') def testStringToIPv4(self): from socket import inet_ntoa as f, inet_ntop, AF_INET g = lambda a: inet_ntop(AF_INET, a) assertInvalid = lambda func,a: self.assertRaises( (OSError, ValueError), func, a ) self.assertEqual('1.0.1.0', f(b'\x01\x00\x01\x00')) self.assertEqual('170.85.170.85', f(b'\xaa\x55\xaa\x55')) self.assertEqual('255.255.255.255', f(b'\xff\xff\xff\xff')) self.assertEqual('1.2.3.4', f(b'\x01\x02\x03\x04')) assertInvalid(f, b'\x00' * 3) assertInvalid(f, b'\x00' * 5) assertInvalid(f, b'\x00' * 16) self.assertEqual('1.0.1.0', g(b'\x01\x00\x01\x00')) self.assertEqual('170.85.170.85', g(b'\xaa\x55\xaa\x55')) self.assertEqual('255.255.255.255', g(b'\xff\xff\xff\xff')) assertInvalid(g, b'\x00' * 3) assertInvalid(g, b'\x00' * 5) assertInvalid(g, b'\x00' * 16) @unittest.skipUnless(hasattr(socket, 'inet_ntop'), 'test needs socket.inet_ntop()') def testStringToIPv6(self): try: from socket import inet_ntop, AF_INET6, has_ipv6 if not has_ipv6: self.skipTest('IPv6 not available') except ImportError: self.skipTest('could not import needed symbols from socket') if sys.platform == "win32": try: inet_ntop(AF_INET6, b'\x00' * 16) except OSError as e: if e.winerror == 10022: self.skipTest('IPv6 might not be supported') f = lambda a: inet_ntop(AF_INET6, a) assertInvalid = lambda a: self.assertRaises( (OSError, ValueError), f, a ) self.assertEqual('::', f(b'\x00' * 16)) self.assertEqual('::1', f(b'\x00' * 15 + b'\x01')) self.assertEqual( 'aef:b01:506:1001:ffff:9997:55:170', f(b'\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70') ) assertInvalid(b'\x12' * 15) assertInvalid(b'\x12' * 17) assertInvalid(b'\x12' * 4) # XXX The following don't test module-level functionality... def testSockName(self): # Testing getsockname() port = support.find_unused_port() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(sock.close) sock.bind(("0.0.0.0", port)) name = sock.getsockname() # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate # it reasonable to get the host's addr in addition to 0.0.0.0. # At least for eCos. This is required for the S/390 to pass. try: my_ip_addr = socket.gethostbyname(socket.gethostname()) except OSError: # Probably name lookup wasn't set up right; skip this test self.skipTest('name lookup failure') self.assertIn(name[0], ("0.0.0.0", my_ip_addr), '%s invalid' % name[0]) self.assertEqual(name[1], port) def testGetSockOpt(self): # Testing getsockopt() # We know a socket should start without reuse==0 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(sock.close) reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) self.assertFalse(reuse != 0, "initial mode is reuse") def testSetSockOpt(self): # Testing setsockopt() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(sock.close) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) self.assertFalse(reuse == 0, "failed to set reuse mode") def testSendAfterClose(self): # testing send() after close() with timeout sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(1) sock.close() self.assertRaises(OSError, sock.send, b"spam") def testNewAttributes(self): # testing .family, .type and .protocol sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.assertEqual(sock.family, socket.AF_INET) if hasattr(socket, 'SOCK_CLOEXEC'): self.assertIn(sock.type, (socket.SOCK_STREAM | socket.SOCK_CLOEXEC, socket.SOCK_STREAM)) else: self.assertEqual(sock.type, socket.SOCK_STREAM) self.assertEqual(sock.proto, 0) sock.close() def test_getsockaddrarg(self): sock = socket.socket() self.addCleanup(sock.close) port = support.find_unused_port() big_port = port + 65536 neg_port = port - 65536 self.assertRaises(OverflowError, sock.bind, (HOST, big_port)) self.assertRaises(OverflowError, sock.bind, (HOST, neg_port)) # Since find_unused_port() is inherently subject to race conditions, we # call it a couple times if necessary. for i in itertools.count(): port = support.find_unused_port() try: sock.bind((HOST, port)) except OSError as e: if e.errno != errno.EADDRINUSE or i == 5: raise else: break @unittest.skipUnless(os.name == "nt", "Windows specific") def test_sock_ioctl(self): self.assertTrue(hasattr(socket.socket, 'ioctl')) self.assertTrue(hasattr(socket, 'SIO_RCVALL')) self.assertTrue(hasattr(socket, 'RCVALL_ON')) self.assertTrue(hasattr(socket, 'RCVALL_OFF')) self.assertTrue(hasattr(socket, 'SIO_KEEPALIVE_VALS')) s = socket.socket() self.addCleanup(s.close) self.assertRaises(ValueError, s.ioctl, -1, None) s.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 100, 100)) def testGetaddrinfo(self): try: socket.getaddrinfo('localhost', 80) except socket.gaierror as err: if err.errno == socket.EAI_SERVICE: # see http://bugs.python.org/issue1282647 self.skipTest("buggy libc version") raise # len of every sequence is supposed to be == 5 for info in socket.getaddrinfo(HOST, None): self.assertEqual(len(info), 5) # host can be a domain name, a string representation of an # IPv4/v6 address or None socket.getaddrinfo('localhost', 80) socket.getaddrinfo('127.0.0.1', 80) socket.getaddrinfo(None, 80) if support.IPV6_ENABLED: socket.getaddrinfo('::1', 80) # port can be a string service name such as "http", a numeric # port number or None socket.getaddrinfo(HOST, "http") socket.getaddrinfo(HOST, 80) socket.getaddrinfo(HOST, None) # test family and socktype filters infos = socket.getaddrinfo(HOST, 80, socket.AF_INET, socket.SOCK_STREAM) for family, type, _, _, _ in infos: self.assertEqual(family, socket.AF_INET) self.assertEqual(str(family), 'AddressFamily.AF_INET') self.assertEqual(type, socket.SOCK_STREAM) self.assertEqual(str(type), 'SocketKind.SOCK_STREAM') infos = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM) for _, socktype, _, _, _ in infos: self.assertEqual(socktype, socket.SOCK_STREAM) # test proto and flags arguments socket.getaddrinfo(HOST, None, 0, 0, socket.SOL_TCP) socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE) # a server willing to support both IPv4 and IPv6 will # usually do this socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE) # test keyword arguments a = socket.getaddrinfo(HOST, None) b = socket.getaddrinfo(host=HOST, port=None) self.assertEqual(a, b) a = socket.getaddrinfo(HOST, None, socket.AF_INET) b = socket.getaddrinfo(HOST, None, family=socket.AF_INET) self.assertEqual(a, b) a = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM) b = socket.getaddrinfo(HOST, None, type=socket.SOCK_STREAM) self.assertEqual(a, b) a = socket.getaddrinfo(HOST, None, 0, 0, socket.SOL_TCP) b = socket.getaddrinfo(HOST, None, proto=socket.SOL_TCP) self.assertEqual(a, b) a = socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE) b = socket.getaddrinfo(HOST, None, flags=socket.AI_PASSIVE) self.assertEqual(a, b) a = socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE) b = socket.getaddrinfo(host=None, port=0, family=socket.AF_UNSPEC, type=socket.SOCK_STREAM, proto=0, flags=socket.AI_PASSIVE) self.assertEqual(a, b) # Issue #6697. self.assertRaises(UnicodeEncodeError, socket.getaddrinfo, 'localhost', '\uD800') # Issue 17269: test workaround for OS X platform bug segfault if hasattr(socket, 'AI_NUMERICSERV'): try: # The arguments here are undefined and the call may succeed # or fail. All we care here is that it doesn't segfault. socket.getaddrinfo("localhost", None, 0, 0, 0, socket.AI_NUMERICSERV) except socket.gaierror: pass def test_getnameinfo(self): # only IP addresses are allowed self.assertRaises(OSError, socket.getnameinfo, ('mail.python.org',0), 0) @unittest.skipUnless(support.is_resource_enabled('network'), 'network is not enabled') def test_idna(self): # Check for internet access before running test (issue #12804). try: socket.gethostbyname('python.org') except socket.gaierror as e: if e.errno == socket.EAI_NODATA: self.skipTest('internet access required for this test') # these should all be successful domain = 'испытание.pythontest.net' socket.gethostbyname(domain) socket.gethostbyname_ex(domain) socket.getaddrinfo(domain,0,socket.AF_UNSPEC,socket.SOCK_STREAM) # this may not work if the forward lookup choses the IPv6 address, as that doesn't # have a reverse entry yet # socket.gethostbyaddr('испытание.python.org') def check_sendall_interrupted(self, with_timeout): # socketpair() is not stricly required, but it makes things easier. if not hasattr(signal, 'alarm') or not hasattr(socket, 'socketpair'): self.skipTest("signal.alarm and socket.socketpair required for this test") # Our signal handlers clobber the C errno by calling a math function # with an invalid domain value. def ok_handler(*args): self.assertRaises(ValueError, math.acosh, 0) def raising_handler(*args): self.assertRaises(ValueError, math.acosh, 0) 1 // 0 c, s = socket.socketpair() old_alarm = signal.signal(signal.SIGALRM, raising_handler) try: if with_timeout: # Just above the one second minimum for signal.alarm c.settimeout(1.5) with self.assertRaises(ZeroDivisionError): signal.alarm(1) c.sendall(b"x" * support.SOCK_MAX_SIZE) if with_timeout: signal.signal(signal.SIGALRM, ok_handler) signal.alarm(1) self.assertRaises(socket.timeout, c.sendall, b"x" * support.SOCK_MAX_SIZE) finally: signal.alarm(0) signal.signal(signal.SIGALRM, old_alarm) c.close() s.close() def test_sendall_interrupted(self): self.check_sendall_interrupted(False) def test_sendall_interrupted_with_timeout(self): self.check_sendall_interrupted(True) def test_dealloc_warn(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) r = repr(sock) with self.assertWarns(ResourceWarning) as cm: sock = None support.gc_collect() self.assertIn(r, str(cm.warning.args[0])) # An open socket file object gets dereferenced after the socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) f = sock.makefile('rb') r = repr(sock) sock = None support.gc_collect() with self.assertWarns(ResourceWarning): f = None support.gc_collect() def test_name_closed_socketio(self): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: fp = sock.makefile("rb") fp.close() self.assertEqual(repr(fp), "<_io.BufferedReader name=-1>") def test_unusable_closed_socketio(self): with socket.socket() as sock: fp = sock.makefile("rb", buffering=0) self.assertTrue(fp.readable()) self.assertFalse(fp.writable()) self.assertFalse(fp.seekable()) fp.close() self.assertRaises(ValueError, fp.readable) self.assertRaises(ValueError, fp.writable) self.assertRaises(ValueError, fp.seekable) def test_pickle(self): sock = socket.socket() with sock: for protocol in range(pickle.HIGHEST_PROTOCOL + 1): self.assertRaises(TypeError, pickle.dumps, sock, protocol) def test_listen_backlog(self): for backlog in 0, -1: srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) srv.bind((HOST, 0)) srv.listen(backlog) srv.close() @support.cpython_only def test_listen_backlog_overflow(self): # Issue 15989 import _testcapi srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) srv.bind((HOST, 0)) self.assertRaises(OverflowError, srv.listen, _testcapi.INT_MAX + 1) srv.close() @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') def test_flowinfo(self): self.assertRaises(OverflowError, socket.getnameinfo, (support.HOSTv6, 0, 0xffffffff), 0) with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s: self.assertRaises(OverflowError, s.bind, (support.HOSTv6, 0, -10)) def test_str_for_enums(self): # Make sure that the AF_* and SOCK_* constants have enum-like string # reprs. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: self.assertEqual(str(s.family), 'AddressFamily.AF_INET') self.assertEqual(str(s.type), 'SocketKind.SOCK_STREAM') @unittest.skipIf(os.name == 'nt', 'Will not work on Windows') def test_uknown_socket_family_repr(self): # Test that when created with a family that's not one of the known # AF_*/SOCK_* constants, socket.family just returns the number. # # To do this we fool socket.socket into believing it already has an # open fd because on this path it doesn't actually verify the family and # type and populates the socket object. # # On Windows this trick won't work, so the test is skipped. fd, _ = tempfile.mkstemp() with socket.socket(family=42424, type=13331, fileno=fd) as s: self.assertEqual(s.family, 42424) self.assertEqual(s.type, 13331) @unittest.skipUnless(HAVE_SOCKET_CAN, 'SocketCan required for this test.') class BasicCANTest(unittest.TestCase): def testCrucialConstants(self): socket.AF_CAN socket.PF_CAN socket.CAN_RAW @unittest.skipUnless(hasattr(socket, "CAN_BCM"), 'socket.CAN_BCM required for this test.') def testBCMConstants(self): socket.CAN_BCM # opcodes socket.CAN_BCM_TX_SETUP # create (cyclic) transmission task socket.CAN_BCM_TX_DELETE # remove (cyclic) transmission task socket.CAN_BCM_TX_READ # read properties of (cyclic) transmission task socket.CAN_BCM_TX_SEND # send one CAN frame socket.CAN_BCM_RX_SETUP # create RX content filter subscription socket.CAN_BCM_RX_DELETE # remove RX content filter subscription socket.CAN_BCM_RX_READ # read properties of RX content filter subscription socket.CAN_BCM_TX_STATUS # reply to TX_READ request socket.CAN_BCM_TX_EXPIRED # notification on performed transmissions (count=0) socket.CAN_BCM_RX_STATUS # reply to RX_READ request socket.CAN_BCM_RX_TIMEOUT # cyclic message is absent socket.CAN_BCM_RX_CHANGED # updated CAN frame (detected content change) def testCreateSocket(self): with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: pass @unittest.skipUnless(hasattr(socket, "CAN_BCM"), 'socket.CAN_BCM required for this test.') def testCreateBCMSocket(self): with socket.socket(socket.PF_CAN, socket.SOCK_DGRAM, socket.CAN_BCM) as s: pass def testBindAny(self): with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: s.bind(('', )) def testTooLongInterfaceName(self): # most systems limit IFNAMSIZ to 16, take 1024 to be sure with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: self.assertRaisesRegex(OSError, 'interface name too long', s.bind, ('x' * 1024,)) @unittest.skipUnless(hasattr(socket, "CAN_RAW_LOOPBACK"), 'socket.CAN_RAW_LOOPBACK required for this test.') def testLoopback(self): with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: for loopback in (0, 1): s.setsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_LOOPBACK, loopback) self.assertEqual(loopback, s.getsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_LOOPBACK)) @unittest.skipUnless(hasattr(socket, "CAN_RAW_FILTER"), 'socket.CAN_RAW_FILTER required for this test.') def testFilter(self): can_id, can_mask = 0x200, 0x700 can_filter = struct.pack("=II", can_id, can_mask) with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: s.setsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_FILTER, can_filter) self.assertEqual(can_filter, s.getsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_FILTER, 8)) @unittest.skipUnless(HAVE_SOCKET_CAN, 'SocketCan required for this test.') @unittest.skipUnless(thread, 'Threading required for this test.') class CANTest(ThreadedCANSocketTest): def __init__(self, methodName='runTest'): ThreadedCANSocketTest.__init__(self, methodName=methodName) @classmethod def build_can_frame(cls, can_id, data): """Build a CAN frame.""" can_dlc = len(data) data = data.ljust(8, b'\x00') return struct.pack(cls.can_frame_fmt, can_id, can_dlc, data) @classmethod def dissect_can_frame(cls, frame): """Dissect a CAN frame.""" can_id, can_dlc, data = struct.unpack(cls.can_frame_fmt, frame) return (can_id, can_dlc, data[:can_dlc]) def testSendFrame(self): cf, addr = self.s.recvfrom(self.bufsize) self.assertEqual(self.cf, cf) self.assertEqual(addr[0], self.interface) self.assertEqual(addr[1], socket.AF_CAN) def _testSendFrame(self): self.cf = self.build_can_frame(0x00, b'\x01\x02\x03\x04\x05') self.cli.send(self.cf) def testSendMaxFrame(self): cf, addr = self.s.recvfrom(self.bufsize) self.assertEqual(self.cf, cf) def _testSendMaxFrame(self): self.cf = self.build_can_frame(0x00, b'\x07' * 8) self.cli.send(self.cf) def testSendMultiFrames(self): cf, addr = self.s.recvfrom(self.bufsize) self.assertEqual(self.cf1, cf) cf, addr = self.s.recvfrom(self.bufsize) self.assertEqual(self.cf2, cf) def _testSendMultiFrames(self): self.cf1 = self.build_can_frame(0x07, b'\x44\x33\x22\x11') self.cli.send(self.cf1) self.cf2 = self.build_can_frame(0x12, b'\x99\x22\x33') self.cli.send(self.cf2) @unittest.skipUnless(hasattr(socket, "CAN_BCM"), 'socket.CAN_BCM required for this test.') def _testBCM(self): cf, addr = self.cli.recvfrom(self.bufsize) self.assertEqual(self.cf, cf) can_id, can_dlc, data = self.dissect_can_frame(cf) self.assertEqual(self.can_id, can_id) self.assertEqual(self.data, data) @unittest.skipUnless(hasattr(socket, "CAN_BCM"), 'socket.CAN_BCM required for this test.') def testBCM(self): bcm = socket.socket(socket.PF_CAN, socket.SOCK_DGRAM, socket.CAN_BCM) self.addCleanup(bcm.close) bcm.connect((self.interface,)) self.can_id = 0x123 self.data = bytes([0xc0, 0xff, 0xee]) self.cf = self.build_can_frame(self.can_id, self.data) opcode = socket.CAN_BCM_TX_SEND flags = 0 count = 0 ival1_seconds = ival1_usec = ival2_seconds = ival2_usec = 0 bcm_can_id = 0x0222 nframes = 1 assert len(self.cf) == 16 header = struct.pack(self.bcm_cmd_msg_fmt, opcode, flags, count, ival1_seconds, ival1_usec, ival2_seconds, ival2_usec, bcm_can_id, nframes, ) header_plus_frame = header + self.cf bytes_sent = bcm.send(header_plus_frame) self.assertEqual(bytes_sent, len(header_plus_frame)) @unittest.skipUnless(HAVE_SOCKET_RDS, 'RDS sockets required for this test.') class BasicRDSTest(unittest.TestCase): def testCrucialConstants(self): socket.AF_RDS socket.PF_RDS def testCreateSocket(self): with socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) as s: pass def testSocketBufferSize(self): bufsize = 16384 with socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) as s: s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, bufsize) s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, bufsize) @unittest.skipUnless(HAVE_SOCKET_RDS, 'RDS sockets required for this test.') @unittest.skipUnless(thread, 'Threading required for this test.') class RDSTest(ThreadedRDSSocketTest): def __init__(self, methodName='runTest'): ThreadedRDSSocketTest.__init__(self, methodName=methodName) def setUp(self): super().setUp() self.evt = threading.Event() def testSendAndRecv(self): data, addr = self.serv.recvfrom(self.bufsize) self.assertEqual(self.data, data) self.assertEqual(self.cli_addr, addr) def _testSendAndRecv(self): self.data = b'spam' self.cli.sendto(self.data, 0, (HOST, self.port)) def testPeek(self): data, addr = self.serv.recvfrom(self.bufsize, socket.MSG_PEEK) self.assertEqual(self.data, data) data, addr = self.serv.recvfrom(self.bufsize) self.assertEqual(self.data, data) def _testPeek(self): self.data = b'spam' self.cli.sendto(self.data, 0, (HOST, self.port)) @requireAttrs(socket.socket, 'recvmsg') def testSendAndRecvMsg(self): data, ancdata, msg_flags, addr = self.serv.recvmsg(self.bufsize) self.assertEqual(self.data, data) @requireAttrs(socket.socket, 'sendmsg') def _testSendAndRecvMsg(self): self.data = b'hello ' * 10 self.cli.sendmsg([self.data], (), 0, (HOST, self.port)) def testSendAndRecvMulti(self): data, addr = self.serv.recvfrom(self.bufsize) self.assertEqual(self.data1, data) data, addr = self.serv.recvfrom(self.bufsize) self.assertEqual(self.data2, data) def _testSendAndRecvMulti(self): self.data1 = b'bacon' self.cli.sendto(self.data1, 0, (HOST, self.port)) self.data2 = b'egg' self.cli.sendto(self.data2, 0, (HOST, self.port)) def testSelect(self): r, w, x = select.select([self.serv], [], [], 3.0) self.assertIn(self.serv, r) data, addr = self.serv.recvfrom(self.bufsize) self.assertEqual(self.data, data) def _testSelect(self): self.data = b'select' self.cli.sendto(self.data, 0, (HOST, self.port)) def testCongestion(self): # wait until the sender is done self.evt.wait() def _testCongestion(self): # test the behavior in case of congestion self.data = b'fill' self.cli.setblocking(False) try: # try to lower the receiver's socket buffer size self.cli.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 16384) except OSError: pass with self.assertRaises(OSError) as cm: try: # fill the receiver's socket buffer while True: self.cli.sendto(self.data, 0, (HOST, self.port)) finally: # signal the receiver we're done self.evt.set() # sendto() should have failed with ENOBUFS self.assertEqual(cm.exception.errno, errno.ENOBUFS) # and we should have received a congestion notification through poll r, w, x = select.select([self.serv], [], [], 3.0) self.assertIn(self.serv, r) @unittest.skipUnless(thread, 'Threading required for this test.') class BasicTCPTest(SocketConnectedTest): def __init__(self, methodName='runTest'): SocketConnectedTest.__init__(self, methodName=methodName) def testRecv(self): # Testing large receive over TCP msg = self.cli_conn.recv(1024) self.assertEqual(msg, MSG) def _testRecv(self): self.serv_conn.send(MSG) def testOverFlowRecv(self): # Testing receive in chunks over TCP seg1 = self.cli_conn.recv(len(MSG) - 3) seg2 = self.cli_conn.recv(1024) msg = seg1 + seg2 self.assertEqual(msg, MSG) def _testOverFlowRecv(self): self.serv_conn.send(MSG) def testRecvFrom(self): # Testing large recvfrom() over TCP msg, addr = self.cli_conn.recvfrom(1024) self.assertEqual(msg, MSG) def _testRecvFrom(self): self.serv_conn.send(MSG) def testOverFlowRecvFrom(self): # Testing recvfrom() in chunks over TCP seg1, addr = self.cli_conn.recvfrom(len(MSG)-3) seg2, addr = self.cli_conn.recvfrom(1024) msg = seg1 + seg2 self.assertEqual(msg, MSG) def _testOverFlowRecvFrom(self): self.serv_conn.send(MSG) def testSendAll(self): # Testing sendall() with a 2048 byte string over TCP msg = b'' while 1: read = self.cli_conn.recv(1024) if not read: break msg += read self.assertEqual(msg, b'f' * 2048) def _testSendAll(self): big_chunk = b'f' * 2048 self.serv_conn.sendall(big_chunk) def testFromFd(self): # Testing fromfd() fd = self.cli_conn.fileno() sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(sock.close) self.assertIsInstance(sock, socket.socket) msg = sock.recv(1024) self.assertEqual(msg, MSG) def _testFromFd(self): self.serv_conn.send(MSG) def testDup(self): # Testing dup() sock = self.cli_conn.dup() self.addCleanup(sock.close) msg = sock.recv(1024) self.assertEqual(msg, MSG) def _testDup(self): self.serv_conn.send(MSG) def testShutdown(self): # Testing shutdown() msg = self.cli_conn.recv(1024) self.assertEqual(msg, MSG) # wait for _testShutdown to finish: on OS X, when the server # closes the connection the client also becomes disconnected, # and the client's shutdown call will fail. (Issue #4397.) self.done.wait() def _testShutdown(self): self.serv_conn.send(MSG) self.serv_conn.shutdown(2) testShutdown_overflow = support.cpython_only(testShutdown) @support.cpython_only def _testShutdown_overflow(self): import _testcapi self.serv_conn.send(MSG) # Issue 15989 self.assertRaises(OverflowError, self.serv_conn.shutdown, _testcapi.INT_MAX + 1) self.assertRaises(OverflowError, self.serv_conn.shutdown, 2 + (_testcapi.UINT_MAX + 1)) self.serv_conn.shutdown(2) def testDetach(self): # Testing detach() fileno = self.cli_conn.fileno() f = self.cli_conn.detach() self.assertEqual(f, fileno) # cli_conn cannot be used anymore... self.assertTrue(self.cli_conn._closed) self.assertRaises(OSError, self.cli_conn.recv, 1024) self.cli_conn.close() # ...but we can create another socket using the (still open) # file descriptor sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, fileno=f) self.addCleanup(sock.close) msg = sock.recv(1024) self.assertEqual(msg, MSG) def _testDetach(self): self.serv_conn.send(MSG) @unittest.skipUnless(thread, 'Threading required for this test.') class BasicUDPTest(ThreadedUDPSocketTest): def __init__(self, methodName='runTest'): ThreadedUDPSocketTest.__init__(self, methodName=methodName) def testSendtoAndRecv(self): # Testing sendto() and Recv() over UDP msg = self.serv.recv(len(MSG)) self.assertEqual(msg, MSG) def _testSendtoAndRecv(self): self.cli.sendto(MSG, 0, (HOST, self.port)) def testRecvFrom(self): # Testing recvfrom() over UDP msg, addr = self.serv.recvfrom(len(MSG)) self.assertEqual(msg, MSG) def _testRecvFrom(self): self.cli.sendto(MSG, 0, (HOST, self.port)) def testRecvFromNegative(self): # Negative lengths passed to recvfrom should give ValueError. self.assertRaises(ValueError, self.serv.recvfrom, -1) def _testRecvFromNegative(self): self.cli.sendto(MSG, 0, (HOST, self.port)) # Tests for the sendmsg()/recvmsg() interface. Where possible, the # same test code is used with different families and types of socket # (e.g. stream, datagram), and tests using recvmsg() are repeated # using recvmsg_into(). # # The generic test classes such as SendmsgTests and # RecvmsgGenericTests inherit from SendrecvmsgBase and expect to be # supplied with sockets cli_sock and serv_sock representing the # client's and the server's end of the connection respectively, and # attributes cli_addr and serv_addr holding their (numeric where # appropriate) addresses. # # The final concrete test classes combine these with subclasses of # SocketTestBase which set up client and server sockets of a specific # type, and with subclasses of SendrecvmsgBase such as # SendrecvmsgDgramBase and SendrecvmsgConnectedBase which map these # sockets to cli_sock and serv_sock and override the methods and # attributes of SendrecvmsgBase to fill in destination addresses if # needed when sending, check for specific flags in msg_flags, etc. # # RecvmsgIntoMixin provides a version of doRecvmsg() implemented using # recvmsg_into(). # XXX: like the other datagram (UDP) tests in this module, the code # here assumes that datagram delivery on the local machine will be # reliable. class SendrecvmsgBase(ThreadSafeCleanupTestCase): # Base class for sendmsg()/recvmsg() tests. # Time in seconds to wait before considering a test failed, or # None for no timeout. Not all tests actually set a timeout. fail_timeout = 3.0 def setUp(self): self.misc_event = threading.Event() super().setUp() def sendToServer(self, msg): # Send msg to the server. return self.cli_sock.send(msg) # Tuple of alternative default arguments for sendmsg() when called # via sendmsgToServer() (e.g. to include a destination address). sendmsg_to_server_defaults = () def sendmsgToServer(self, *args): # Call sendmsg() on self.cli_sock with the given arguments, # filling in any arguments which are not supplied with the # corresponding items of self.sendmsg_to_server_defaults, if # any. return self.cli_sock.sendmsg( *(args + self.sendmsg_to_server_defaults[len(args):])) def doRecvmsg(self, sock, bufsize, *args): # Call recvmsg() on sock with given arguments and return its # result. Should be used for tests which can use either # recvmsg() or recvmsg_into() - RecvmsgIntoMixin overrides # this method with one which emulates it using recvmsg_into(), # thus allowing the same test to be used for both methods. result = sock.recvmsg(bufsize, *args) self.registerRecvmsgResult(result) return result def registerRecvmsgResult(self, result): # Called by doRecvmsg() with the return value of recvmsg() or # recvmsg_into(). Can be overridden to arrange cleanup based # on the returned ancillary data, for instance. pass def checkRecvmsgAddress(self, addr1, addr2): # Called to compare the received address with the address of # the peer. self.assertEqual(addr1, addr2) # Flags that are normally unset in msg_flags msg_flags_common_unset = 0 for name in ("MSG_CTRUNC", "MSG_OOB"): msg_flags_common_unset |= getattr(socket, name, 0) # Flags that are normally set msg_flags_common_set = 0 # Flags set when a complete record has been received (e.g. MSG_EOR # for SCTP) msg_flags_eor_indicator = 0 # Flags set when a complete record has not been received # (e.g. MSG_TRUNC for datagram sockets) msg_flags_non_eor_indicator = 0 def checkFlags(self, flags, eor=None, checkset=0, checkunset=0, ignore=0): # Method to check the value of msg_flags returned by recvmsg[_into](). # # Checks that all bits in msg_flags_common_set attribute are # set in "flags" and all bits in msg_flags_common_unset are # unset. # # The "eor" argument specifies whether the flags should # indicate that a full record (or datagram) has been received. # If "eor" is None, no checks are done; otherwise, checks # that: # # * if "eor" is true, all bits in msg_flags_eor_indicator are # set and all bits in msg_flags_non_eor_indicator are unset # # * if "eor" is false, all bits in msg_flags_non_eor_indicator # are set and all bits in msg_flags_eor_indicator are unset # # If "checkset" and/or "checkunset" are supplied, they require # the given bits to be set or unset respectively, overriding # what the attributes require for those bits. # # If any bits are set in "ignore", they will not be checked, # regardless of the other inputs. # # Will raise Exception if the inputs require a bit to be both # set and unset, and it is not ignored. defaultset = self.msg_flags_common_set defaultunset = self.msg_flags_common_unset if eor: defaultset |= self.msg_flags_eor_indicator defaultunset |= self.msg_flags_non_eor_indicator elif eor is not None: defaultset |= self.msg_flags_non_eor_indicator defaultunset |= self.msg_flags_eor_indicator # Function arguments override defaults defaultset &= ~checkunset defaultunset &= ~checkset # Merge arguments with remaining defaults, and check for conflicts checkset |= defaultset checkunset |= defaultunset inboth = checkset & checkunset & ~ignore if inboth: raise Exception("contradictory set, unset requirements for flags " "{0:#x}".format(inboth)) # Compare with given msg_flags value mask = (checkset | checkunset) & ~ignore self.assertEqual(flags & mask, checkset & mask) class RecvmsgIntoMixin(SendrecvmsgBase): # Mixin to implement doRecvmsg() using recvmsg_into(). def doRecvmsg(self, sock, bufsize, *args): buf = bytearray(bufsize) result = sock.recvmsg_into([buf], *args) self.registerRecvmsgResult(result) self.assertGreaterEqual(result[0], 0) self.assertLessEqual(result[0], bufsize) return (bytes(buf[:result[0]]),) + result[1:] class SendrecvmsgDgramFlagsBase(SendrecvmsgBase): # Defines flags to be checked in msg_flags for datagram sockets. @property def msg_flags_non_eor_indicator(self): return super().msg_flags_non_eor_indicator | socket.MSG_TRUNC class SendrecvmsgSCTPFlagsBase(SendrecvmsgBase): # Defines flags to be checked in msg_flags for SCTP sockets. @property def msg_flags_eor_indicator(self): return super().msg_flags_eor_indicator | socket.MSG_EOR class SendrecvmsgConnectionlessBase(SendrecvmsgBase): # Base class for tests on connectionless-mode sockets. Users must # supply sockets on attributes cli and serv to be mapped to # cli_sock and serv_sock respectively. @property def serv_sock(self): return self.serv @property def cli_sock(self): return self.cli @property def sendmsg_to_server_defaults(self): return ([], [], 0, self.serv_addr) def sendToServer(self, msg): return self.cli_sock.sendto(msg, self.serv_addr) class SendrecvmsgConnectedBase(SendrecvmsgBase): # Base class for tests on connected sockets. Users must supply # sockets on attributes serv_conn and cli_conn (representing the # connections *to* the server and the client), to be mapped to # cli_sock and serv_sock respectively. @property def serv_sock(self): return self.cli_conn @property def cli_sock(self): return self.serv_conn def checkRecvmsgAddress(self, addr1, addr2): # Address is currently "unspecified" for a connected socket, # so we don't examine it pass class SendrecvmsgServerTimeoutBase(SendrecvmsgBase): # Base class to set a timeout on server's socket. def setUp(self): super().setUp() self.serv_sock.settimeout(self.fail_timeout) class SendmsgTests(SendrecvmsgServerTimeoutBase): # Tests for sendmsg() which can use any socket type and do not # involve recvmsg() or recvmsg_into(). def testSendmsg(self): # Send a simple message with sendmsg(). self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) def _testSendmsg(self): self.assertEqual(self.sendmsgToServer([MSG]), len(MSG)) def testSendmsgDataGenerator(self): # Send from buffer obtained from a generator (not a sequence). self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) def _testSendmsgDataGenerator(self): self.assertEqual(self.sendmsgToServer((o for o in [MSG])), len(MSG)) def testSendmsgAncillaryGenerator(self): # Gather (empty) ancillary data from a generator. self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) def _testSendmsgAncillaryGenerator(self): self.assertEqual(self.sendmsgToServer([MSG], (o for o in [])), len(MSG)) def testSendmsgArray(self): # Send data from an array instead of the usual bytes object. self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) def _testSendmsgArray(self): self.assertEqual(self.sendmsgToServer([array.array("B", MSG)]), len(MSG)) def testSendmsgGather(self): # Send message data from more than one buffer (gather write). self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) def _testSendmsgGather(self): self.assertEqual(self.sendmsgToServer([MSG[:3], MSG[3:]]), len(MSG)) def testSendmsgBadArgs(self): # Check that sendmsg() rejects invalid arguments. self.assertEqual(self.serv_sock.recv(1000), b"done") def _testSendmsgBadArgs(self): self.assertRaises(TypeError, self.cli_sock.sendmsg) self.assertRaises(TypeError, self.sendmsgToServer, b"not in an iterable") self.assertRaises(TypeError, self.sendmsgToServer, object()) self.assertRaises(TypeError, self.sendmsgToServer, [object()]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG, object()]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], object()) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [], object()) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [], 0, object()) self.sendToServer(b"done") def testSendmsgBadCmsg(self): # Check that invalid ancillary data items are rejected. self.assertEqual(self.serv_sock.recv(1000), b"done") def _testSendmsgBadCmsg(self): self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [object()]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [(object(), 0, b"data")]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [(0, object(), b"data")]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [(0, 0, object())]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [(0, 0)]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [(0, 0, b"data", 42)]) self.sendToServer(b"done") @requireAttrs(socket, "CMSG_SPACE") def testSendmsgBadMultiCmsg(self): # Check that invalid ancillary data items are rejected when # more than one item is present. self.assertEqual(self.serv_sock.recv(1000), b"done") @testSendmsgBadMultiCmsg.client_skip def _testSendmsgBadMultiCmsg(self): self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [0, 0, b""]) self.assertRaises(TypeError, self.sendmsgToServer, [MSG], [(0, 0, b""), object()]) self.sendToServer(b"done") def testSendmsgExcessCmsgReject(self): # Check that sendmsg() rejects excess ancillary data items # when the number that can be sent is limited. self.assertEqual(self.serv_sock.recv(1000), b"done") def _testSendmsgExcessCmsgReject(self): if not hasattr(socket, "CMSG_SPACE"): # Can only send one item with self.assertRaises(OSError) as cm: self.sendmsgToServer([MSG], [(0, 0, b""), (0, 0, b"")]) self.assertIsNone(cm.exception.errno) self.sendToServer(b"done") def testSendmsgAfterClose(self): # Check that sendmsg() fails on a closed socket. pass def _testSendmsgAfterClose(self): self.cli_sock.close() self.assertRaises(OSError, self.sendmsgToServer, [MSG]) class SendmsgStreamTests(SendmsgTests): # Tests for sendmsg() which require a stream socket and do not # involve recvmsg() or recvmsg_into(). def testSendmsgExplicitNoneAddr(self): # Check that peer address can be specified as None. self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) def _testSendmsgExplicitNoneAddr(self): self.assertEqual(self.sendmsgToServer([MSG], [], 0, None), len(MSG)) def testSendmsgTimeout(self): # Check that timeout works with sendmsg(). self.assertEqual(self.serv_sock.recv(512), b"a"*512) self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) def _testSendmsgTimeout(self): try: self.cli_sock.settimeout(0.03) with self.assertRaises(socket.timeout): while True: self.sendmsgToServer([b"a"*512]) finally: self.misc_event.set() # XXX: would be nice to have more tests for sendmsg flags argument. # Linux supports MSG_DONTWAIT when sending, but in general, it # only works when receiving. Could add other platforms if they # support it too. @skipWithClientIf(sys.platform not in {"linux"}, "MSG_DONTWAIT not known to work on this platform when " "sending") def testSendmsgDontWait(self): # Check that MSG_DONTWAIT in flags causes non-blocking behaviour. self.assertEqual(self.serv_sock.recv(512), b"a"*512) self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) @testSendmsgDontWait.client_skip def _testSendmsgDontWait(self): try: with self.assertRaises(OSError) as cm: while True: self.sendmsgToServer([b"a"*512], [], socket.MSG_DONTWAIT) self.assertIn(cm.exception.errno, (errno.EAGAIN, errno.EWOULDBLOCK)) finally: self.misc_event.set() class SendmsgConnectionlessTests(SendmsgTests): # Tests for sendmsg() which require a connectionless-mode # (e.g. datagram) socket, and do not involve recvmsg() or # recvmsg_into(). def testSendmsgNoDestAddr(self): # Check that sendmsg() fails when no destination address is # given for unconnected socket. pass def _testSendmsgNoDestAddr(self): self.assertRaises(OSError, self.cli_sock.sendmsg, [MSG]) self.assertRaises(OSError, self.cli_sock.sendmsg, [MSG], [], 0, None) class RecvmsgGenericTests(SendrecvmsgBase): # Tests for recvmsg() which can also be emulated using # recvmsg_into(), and can use any socket type. def testRecvmsg(self): # Receive a simple message with recvmsg[_into](). msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG)) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsg(self): self.sendToServer(MSG) def testRecvmsgExplicitDefaults(self): # Test recvmsg[_into]() with default arguments provided explicitly. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 0, 0) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgExplicitDefaults(self): self.sendToServer(MSG) def testRecvmsgShorter(self): # Receive a message smaller than buffer. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG) + 42) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgShorter(self): self.sendToServer(MSG) # FreeBSD < 8 doesn't always set the MSG_TRUNC flag when a truncated # datagram is received (issue #13001). @support.requires_freebsd_version(8) def testRecvmsgTrunc(self): # Receive part of message, check for truncation indicators. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG) - 3) self.assertEqual(msg, MSG[:-3]) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=False) @support.requires_freebsd_version(8) def _testRecvmsgTrunc(self): self.sendToServer(MSG) def testRecvmsgShortAncillaryBuf(self): # Test ancillary data buffer too small to hold any ancillary data. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 1) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgShortAncillaryBuf(self): self.sendToServer(MSG) def testRecvmsgLongAncillaryBuf(self): # Test large ancillary data buffer. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 10240) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgLongAncillaryBuf(self): self.sendToServer(MSG) def testRecvmsgAfterClose(self): # Check that recvmsg[_into]() fails on a closed socket. self.serv_sock.close() self.assertRaises(OSError, self.doRecvmsg, self.serv_sock, 1024) def _testRecvmsgAfterClose(self): pass def testRecvmsgTimeout(self): # Check that timeout works. try: self.serv_sock.settimeout(0.03) self.assertRaises(socket.timeout, self.doRecvmsg, self.serv_sock, len(MSG)) finally: self.misc_event.set() def _testRecvmsgTimeout(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) @requireAttrs(socket, "MSG_PEEK") def testRecvmsgPeek(self): # Check that MSG_PEEK in flags enables examination of pending # data without consuming it. # Receive part of data with MSG_PEEK. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG) - 3, 0, socket.MSG_PEEK) self.assertEqual(msg, MSG[:-3]) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) # Ignoring MSG_TRUNC here (so this test is the same for stream # and datagram sockets). Some wording in POSIX seems to # suggest that it needn't be set when peeking, but that may # just be a slip. self.checkFlags(flags, eor=False, ignore=getattr(socket, "MSG_TRUNC", 0)) # Receive all data with MSG_PEEK. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 0, socket.MSG_PEEK) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) # Check that the same data can still be received normally. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG)) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) @testRecvmsgPeek.client_skip def _testRecvmsgPeek(self): self.sendToServer(MSG) @requireAttrs(socket.socket, "sendmsg") def testRecvmsgFromSendmsg(self): # Test receiving with recvmsg[_into]() when message is sent # using sendmsg(). self.serv_sock.settimeout(self.fail_timeout) msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG)) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) @testRecvmsgFromSendmsg.client_skip def _testRecvmsgFromSendmsg(self): self.assertEqual(self.sendmsgToServer([MSG[:3], MSG[3:]]), len(MSG)) class RecvmsgGenericStreamTests(RecvmsgGenericTests): # Tests which require a stream socket and can use either recvmsg() # or recvmsg_into(). def testRecvmsgEOF(self): # Receive end-of-stream indicator (b"", peer socket closed). msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, 1024) self.assertEqual(msg, b"") self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=None) # Might not have end-of-record marker def _testRecvmsgEOF(self): self.cli_sock.close() def testRecvmsgOverflow(self): # Receive a message in more than one chunk. seg1, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG) - 3) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=False) seg2, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, 1024) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) msg = seg1 + seg2 self.assertEqual(msg, MSG) def _testRecvmsgOverflow(self): self.sendToServer(MSG) class RecvmsgTests(RecvmsgGenericTests): # Tests for recvmsg() which can use any socket type. def testRecvmsgBadArgs(self): # Check that recvmsg() rejects invalid arguments. self.assertRaises(TypeError, self.serv_sock.recvmsg) self.assertRaises(ValueError, self.serv_sock.recvmsg, -1, 0, 0) self.assertRaises(ValueError, self.serv_sock.recvmsg, len(MSG), -1, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg, [bytearray(10)], 0, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg, object(), 0, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg, len(MSG), object(), 0) self.assertRaises(TypeError, self.serv_sock.recvmsg, len(MSG), 0, object()) msg, ancdata, flags, addr = self.serv_sock.recvmsg(len(MSG), 0, 0) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgBadArgs(self): self.sendToServer(MSG) class RecvmsgIntoTests(RecvmsgIntoMixin, RecvmsgGenericTests): # Tests for recvmsg_into() which can use any socket type. def testRecvmsgIntoBadArgs(self): # Check that recvmsg_into() rejects invalid arguments. buf = bytearray(len(MSG)) self.assertRaises(TypeError, self.serv_sock.recvmsg_into) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, len(MSG), 0, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, buf, 0, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, [object()], 0, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, [b"I'm not writable"], 0, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, [buf, object()], 0, 0) self.assertRaises(ValueError, self.serv_sock.recvmsg_into, [buf], -1, 0) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, [buf], object(), 0) self.assertRaises(TypeError, self.serv_sock.recvmsg_into, [buf], 0, object()) nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into([buf], 0, 0) self.assertEqual(nbytes, len(MSG)) self.assertEqual(buf, bytearray(MSG)) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgIntoBadArgs(self): self.sendToServer(MSG) def testRecvmsgIntoGenerator(self): # Receive into buffer obtained from a generator (not a sequence). buf = bytearray(len(MSG)) nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into( (o for o in [buf])) self.assertEqual(nbytes, len(MSG)) self.assertEqual(buf, bytearray(MSG)) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgIntoGenerator(self): self.sendToServer(MSG) def testRecvmsgIntoArray(self): # Receive into an array rather than the usual bytearray. buf = array.array("B", [0] * len(MSG)) nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into([buf]) self.assertEqual(nbytes, len(MSG)) self.assertEqual(buf.tobytes(), MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgIntoArray(self): self.sendToServer(MSG) def testRecvmsgIntoScatter(self): # Receive into multiple buffers (scatter write). b1 = bytearray(b"----") b2 = bytearray(b"0123456789") b3 = bytearray(b"--------------") nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into( [b1, memoryview(b2)[2:9], b3]) self.assertEqual(nbytes, len(b"Mary had a little lamb")) self.assertEqual(b1, bytearray(b"Mary")) self.assertEqual(b2, bytearray(b"01 had a 9")) self.assertEqual(b3, bytearray(b"little lamb---")) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) def _testRecvmsgIntoScatter(self): self.sendToServer(b"Mary had a little lamb") class CmsgMacroTests(unittest.TestCase): # Test the functions CMSG_LEN() and CMSG_SPACE(). Tests # assumptions used by sendmsg() and recvmsg[_into](), which share # code with these functions. # Match the definition in socketmodule.c try: import _testcapi except ImportError: socklen_t_limit = 0x7fffffff else: socklen_t_limit = min(0x7fffffff, _testcapi.INT_MAX) @requireAttrs(socket, "CMSG_LEN") def testCMSG_LEN(self): # Test CMSG_LEN() with various valid and invalid values, # checking the assumptions used by recvmsg() and sendmsg(). toobig = self.socklen_t_limit - socket.CMSG_LEN(0) + 1 values = list(range(257)) + list(range(toobig - 257, toobig)) # struct cmsghdr has at least three members, two of which are ints self.assertGreater(socket.CMSG_LEN(0), array.array("i").itemsize * 2) for n in values: ret = socket.CMSG_LEN(n) # This is how recvmsg() calculates the data size self.assertEqual(ret - socket.CMSG_LEN(0), n) self.assertLessEqual(ret, self.socklen_t_limit) self.assertRaises(OverflowError, socket.CMSG_LEN, -1) # sendmsg() shares code with these functions, and requires # that it reject values over the limit. self.assertRaises(OverflowError, socket.CMSG_LEN, toobig) self.assertRaises(OverflowError, socket.CMSG_LEN, sys.maxsize) @requireAttrs(socket, "CMSG_SPACE") def testCMSG_SPACE(self): # Test CMSG_SPACE() with various valid and invalid values, # checking the assumptions used by sendmsg(). toobig = self.socklen_t_limit - socket.CMSG_SPACE(1) + 1 values = list(range(257)) + list(range(toobig - 257, toobig)) last = socket.CMSG_SPACE(0) # struct cmsghdr has at least three members, two of which are ints self.assertGreater(last, array.array("i").itemsize * 2) for n in values: ret = socket.CMSG_SPACE(n) self.assertGreaterEqual(ret, last) self.assertGreaterEqual(ret, socket.CMSG_LEN(n)) self.assertGreaterEqual(ret, n + socket.CMSG_LEN(0)) self.assertLessEqual(ret, self.socklen_t_limit) last = ret self.assertRaises(OverflowError, socket.CMSG_SPACE, -1) # sendmsg() shares code with these functions, and requires # that it reject values over the limit. self.assertRaises(OverflowError, socket.CMSG_SPACE, toobig) self.assertRaises(OverflowError, socket.CMSG_SPACE, sys.maxsize) class SCMRightsTest(SendrecvmsgServerTimeoutBase): # Tests for file descriptor passing on Unix-domain sockets. # Invalid file descriptor value that's unlikely to evaluate to a # real FD even if one of its bytes is replaced with a different # value (which shouldn't actually happen). badfd = -0x5555 def newFDs(self, n): # Return a list of n file descriptors for newly-created files # containing their list indices as ASCII numbers. fds = [] for i in range(n): fd, path = tempfile.mkstemp() self.addCleanup(os.unlink, path) self.addCleanup(os.close, fd) os.write(fd, str(i).encode()) fds.append(fd) return fds def checkFDs(self, fds): # Check that the file descriptors in the given list contain # their correct list indices as ASCII numbers. for n, fd in enumerate(fds): os.lseek(fd, 0, os.SEEK_SET) self.assertEqual(os.read(fd, 1024), str(n).encode()) def registerRecvmsgResult(self, result): self.addCleanup(self.closeRecvmsgFDs, result) def closeRecvmsgFDs(self, recvmsg_result): # Close all file descriptors specified in the ancillary data # of the given return value from recvmsg() or recvmsg_into(). for cmsg_level, cmsg_type, cmsg_data in recvmsg_result[1]: if (cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS): fds = array.array("i") fds.frombytes(cmsg_data[: len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) for fd in fds: os.close(fd) def createAndSendFDs(self, n): # Send n new file descriptors created by newFDs() to the # server, with the constant MSG as the non-ancillary data. self.assertEqual( self.sendmsgToServer([MSG], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", self.newFDs(n)))]), len(MSG)) def checkRecvmsgFDs(self, numfds, result, maxcmsgs=1, ignoreflags=0): # Check that constant MSG was received with numfds file # descriptors in a maximum of maxcmsgs control messages (which # must contain only complete integers). By default, check # that MSG_CTRUNC is unset, but ignore any flags in # ignoreflags. msg, ancdata, flags, addr = result self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkunset=socket.MSG_CTRUNC, ignore=ignoreflags) self.assertIsInstance(ancdata, list) self.assertLessEqual(len(ancdata), maxcmsgs) fds = array.array("i") for item in ancdata: self.assertIsInstance(item, tuple) cmsg_level, cmsg_type, cmsg_data = item self.assertEqual(cmsg_level, socket.SOL_SOCKET) self.assertEqual(cmsg_type, socket.SCM_RIGHTS) self.assertIsInstance(cmsg_data, bytes) self.assertEqual(len(cmsg_data) % SIZEOF_INT, 0) fds.frombytes(cmsg_data) self.assertEqual(len(fds), numfds) self.checkFDs(fds) def testFDPassSimple(self): # Pass a single FD (array read from bytes object). self.checkRecvmsgFDs(1, self.doRecvmsg(self.serv_sock, len(MSG), 10240)) def _testFDPassSimple(self): self.assertEqual( self.sendmsgToServer( [MSG], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", self.newFDs(1)).tobytes())]), len(MSG)) def testMultipleFDPass(self): # Pass multiple FDs in a single array. self.checkRecvmsgFDs(4, self.doRecvmsg(self.serv_sock, len(MSG), 10240)) def _testMultipleFDPass(self): self.createAndSendFDs(4) @requireAttrs(socket, "CMSG_SPACE") def testFDPassCMSG_SPACE(self): # Test using CMSG_SPACE() to calculate ancillary buffer size. self.checkRecvmsgFDs( 4, self.doRecvmsg(self.serv_sock, len(MSG), socket.CMSG_SPACE(4 * SIZEOF_INT))) @testFDPassCMSG_SPACE.client_skip def _testFDPassCMSG_SPACE(self): self.createAndSendFDs(4) def testFDPassCMSG_LEN(self): # Test using CMSG_LEN() to calculate ancillary buffer size. self.checkRecvmsgFDs(1, self.doRecvmsg(self.serv_sock, len(MSG), socket.CMSG_LEN(4 * SIZEOF_INT)), # RFC 3542 says implementations may set # MSG_CTRUNC if there isn't enough space # for trailing padding. ignoreflags=socket.MSG_CTRUNC) def _testFDPassCMSG_LEN(self): self.createAndSendFDs(1) @unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958") @requireAttrs(socket, "CMSG_SPACE") def testFDPassSeparate(self): # Pass two FDs in two separate arrays. Arrays may be combined # into a single control message by the OS. self.checkRecvmsgFDs(2, self.doRecvmsg(self.serv_sock, len(MSG), 10240), maxcmsgs=2) @testFDPassSeparate.client_skip @unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958") def _testFDPassSeparate(self): fd0, fd1 = self.newFDs(2) self.assertEqual( self.sendmsgToServer([MSG], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [fd0])), (socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [fd1]))]), len(MSG)) @unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958") @requireAttrs(socket, "CMSG_SPACE") def testFDPassSeparateMinSpace(self): # Pass two FDs in two separate arrays, receiving them into the # minimum space for two arrays. self.checkRecvmsgFDs(2, self.doRecvmsg(self.serv_sock, len(MSG), socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(SIZEOF_INT)), maxcmsgs=2, ignoreflags=socket.MSG_CTRUNC) @testFDPassSeparateMinSpace.client_skip @unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958") def _testFDPassSeparateMinSpace(self): fd0, fd1 = self.newFDs(2) self.assertEqual( self.sendmsgToServer([MSG], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [fd0])), (socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [fd1]))]), len(MSG)) def sendAncillaryIfPossible(self, msg, ancdata): # Try to send msg and ancdata to server, but if the system # call fails, just send msg with no ancillary data. try: nbytes = self.sendmsgToServer([msg], ancdata) except OSError as e: # Check that it was the system call that failed self.assertIsInstance(e.errno, int) nbytes = self.sendmsgToServer([msg]) self.assertEqual(nbytes, len(msg)) def testFDPassEmpty(self): # Try to pass an empty FD array. Can receive either no array # or an empty array. self.checkRecvmsgFDs(0, self.doRecvmsg(self.serv_sock, len(MSG), 10240), ignoreflags=socket.MSG_CTRUNC) def _testFDPassEmpty(self): self.sendAncillaryIfPossible(MSG, [(socket.SOL_SOCKET, socket.SCM_RIGHTS, b"")]) def testFDPassPartialInt(self): # Try to pass a truncated FD array. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 10240) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, ignore=socket.MSG_CTRUNC) self.assertLessEqual(len(ancdata), 1) for cmsg_level, cmsg_type, cmsg_data in ancdata: self.assertEqual(cmsg_level, socket.SOL_SOCKET) self.assertEqual(cmsg_type, socket.SCM_RIGHTS) self.assertLess(len(cmsg_data), SIZEOF_INT) def _testFDPassPartialInt(self): self.sendAncillaryIfPossible( MSG, [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [self.badfd]).tobytes()[:-1])]) @requireAttrs(socket, "CMSG_SPACE") def testFDPassPartialIntInMiddle(self): # Try to pass two FD arrays, the first of which is truncated. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 10240) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, ignore=socket.MSG_CTRUNC) self.assertLessEqual(len(ancdata), 2) fds = array.array("i") # Arrays may have been combined in a single control message for cmsg_level, cmsg_type, cmsg_data in ancdata: self.assertEqual(cmsg_level, socket.SOL_SOCKET) self.assertEqual(cmsg_type, socket.SCM_RIGHTS) fds.frombytes(cmsg_data[: len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) self.assertLessEqual(len(fds), 2) self.checkFDs(fds) @testFDPassPartialIntInMiddle.client_skip def _testFDPassPartialIntInMiddle(self): fd0, fd1 = self.newFDs(2) self.sendAncillaryIfPossible( MSG, [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [fd0, self.badfd]).tobytes()[:-1]), (socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", [fd1]))]) def checkTruncatedHeader(self, result, ignoreflags=0): # Check that no ancillary data items are returned when data is # truncated inside the cmsghdr structure. msg, ancdata, flags, addr = result self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC, ignore=ignoreflags) def testCmsgTruncNoBufSize(self): # Check that no ancillary data is received when no buffer size # is specified. self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG)), # BSD seems to set MSG_CTRUNC only # if an item has been partially # received. ignoreflags=socket.MSG_CTRUNC) def _testCmsgTruncNoBufSize(self): self.createAndSendFDs(1) def testCmsgTrunc0(self): # Check that no ancillary data is received when buffer size is 0. self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), 0), ignoreflags=socket.MSG_CTRUNC) def _testCmsgTrunc0(self): self.createAndSendFDs(1) # Check that no ancillary data is returned for various non-zero # (but still too small) buffer sizes. def testCmsgTrunc1(self): self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), 1)) def _testCmsgTrunc1(self): self.createAndSendFDs(1) def testCmsgTrunc2Int(self): # The cmsghdr structure has at least three members, two of # which are ints, so we still shouldn't see any ancillary # data. self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), SIZEOF_INT * 2)) def _testCmsgTrunc2Int(self): self.createAndSendFDs(1) def testCmsgTruncLen0Minus1(self): self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), socket.CMSG_LEN(0) - 1)) def _testCmsgTruncLen0Minus1(self): self.createAndSendFDs(1) # The following tests try to truncate the control message in the # middle of the FD array. def checkTruncatedArray(self, ancbuf, maxdata, mindata=0): # Check that file descriptor data is truncated to between # mindata and maxdata bytes when received with buffer size # ancbuf, and that any complete file descriptor numbers are # valid. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), ancbuf) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC) if mindata == 0 and ancdata == []: return self.assertEqual(len(ancdata), 1) cmsg_level, cmsg_type, cmsg_data = ancdata[0] self.assertEqual(cmsg_level, socket.SOL_SOCKET) self.assertEqual(cmsg_type, socket.SCM_RIGHTS) self.assertGreaterEqual(len(cmsg_data), mindata) self.assertLessEqual(len(cmsg_data), maxdata) fds = array.array("i") fds.frombytes(cmsg_data[: len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) self.checkFDs(fds) def testCmsgTruncLen0(self): self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(0), maxdata=0) def _testCmsgTruncLen0(self): self.createAndSendFDs(1) def testCmsgTruncLen0Plus1(self): self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(0) + 1, maxdata=1) def _testCmsgTruncLen0Plus1(self): self.createAndSendFDs(2) def testCmsgTruncLen1(self): self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(SIZEOF_INT), maxdata=SIZEOF_INT) def _testCmsgTruncLen1(self): self.createAndSendFDs(2) def testCmsgTruncLen2Minus1(self): self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(2 * SIZEOF_INT) - 1, maxdata=(2 * SIZEOF_INT) - 1) def _testCmsgTruncLen2Minus1(self): self.createAndSendFDs(2) class RFC3542AncillaryTest(SendrecvmsgServerTimeoutBase): # Test sendmsg() and recvmsg[_into]() using the ancillary data # features of the RFC 3542 Advanced Sockets API for IPv6. # Currently we can only handle certain data items (e.g. traffic # class, hop limit, MTU discovery and fragmentation settings) # without resorting to unportable means such as the struct module, # but the tests here are aimed at testing the ancillary data # handling in sendmsg() and recvmsg() rather than the IPv6 API # itself. # Test value to use when setting hop limit of packet hop_limit = 2 # Test value to use when setting traffic class of packet. # -1 means "use kernel default". traffic_class = -1 def ancillaryMapping(self, ancdata): # Given ancillary data list ancdata, return a mapping from # pairs (cmsg_level, cmsg_type) to corresponding cmsg_data. # Check that no (level, type) pair appears more than once. d = {} for cmsg_level, cmsg_type, cmsg_data in ancdata: self.assertNotIn((cmsg_level, cmsg_type), d) d[(cmsg_level, cmsg_type)] = cmsg_data return d def checkHopLimit(self, ancbufsize, maxhop=255, ignoreflags=0): # Receive hop limit into ancbufsize bytes of ancillary data # space. Check that data is MSG, ancillary data is not # truncated (but ignore any flags in ignoreflags), and hop # limit is between 0 and maxhop inclusive. self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.misc_event.set() msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), ancbufsize) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkunset=socket.MSG_CTRUNC, ignore=ignoreflags) self.assertEqual(len(ancdata), 1) self.assertIsInstance(ancdata[0], tuple) cmsg_level, cmsg_type, cmsg_data = ancdata[0] self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) self.assertEqual(cmsg_type, socket.IPV6_HOPLIMIT) self.assertIsInstance(cmsg_data, bytes) self.assertEqual(len(cmsg_data), SIZEOF_INT) a = array.array("i") a.frombytes(cmsg_data) self.assertGreaterEqual(a[0], 0) self.assertLessEqual(a[0], maxhop) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testRecvHopLimit(self): # Test receiving the packet hop limit as ancillary data. self.checkHopLimit(ancbufsize=10240) @testRecvHopLimit.client_skip def _testRecvHopLimit(self): # Need to wait until server has asked to receive ancillary # data, as implementations are not required to buffer it # otherwise. self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testRecvHopLimitCMSG_SPACE(self): # Test receiving hop limit, using CMSG_SPACE to calculate buffer size. self.checkHopLimit(ancbufsize=socket.CMSG_SPACE(SIZEOF_INT)) @testRecvHopLimitCMSG_SPACE.client_skip def _testRecvHopLimitCMSG_SPACE(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) # Could test receiving into buffer sized using CMSG_LEN, but RFC # 3542 says portable applications must provide space for trailing # padding. Implementations may set MSG_CTRUNC if there isn't # enough space for the padding. @requireAttrs(socket.socket, "sendmsg") @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testSetHopLimit(self): # Test setting hop limit on outgoing packet and receiving it # at the other end. self.checkHopLimit(ancbufsize=10240, maxhop=self.hop_limit) @testSetHopLimit.client_skip def _testSetHopLimit(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.assertEqual( self.sendmsgToServer([MSG], [(socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT, array.array("i", [self.hop_limit]))]), len(MSG)) def checkTrafficClassAndHopLimit(self, ancbufsize, maxhop=255, ignoreflags=0): # Receive traffic class and hop limit into ancbufsize bytes of # ancillary data space. Check that data is MSG, ancillary # data is not truncated (but ignore any flags in ignoreflags), # and traffic class and hop limit are in range (hop limit no # more than maxhop). self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVTCLASS, 1) self.misc_event.set() msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), ancbufsize) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkunset=socket.MSG_CTRUNC, ignore=ignoreflags) self.assertEqual(len(ancdata), 2) ancmap = self.ancillaryMapping(ancdata) tcdata = ancmap[(socket.IPPROTO_IPV6, socket.IPV6_TCLASS)] self.assertEqual(len(tcdata), SIZEOF_INT) a = array.array("i") a.frombytes(tcdata) self.assertGreaterEqual(a[0], 0) self.assertLessEqual(a[0], 255) hldata = ancmap[(socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT)] self.assertEqual(len(hldata), SIZEOF_INT) a = array.array("i") a.frombytes(hldata) self.assertGreaterEqual(a[0], 0) self.assertLessEqual(a[0], maxhop) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testRecvTrafficClassAndHopLimit(self): # Test receiving traffic class and hop limit as ancillary data. self.checkTrafficClassAndHopLimit(ancbufsize=10240) @testRecvTrafficClassAndHopLimit.client_skip def _testRecvTrafficClassAndHopLimit(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testRecvTrafficClassAndHopLimitCMSG_SPACE(self): # Test receiving traffic class and hop limit, using # CMSG_SPACE() to calculate buffer size. self.checkTrafficClassAndHopLimit( ancbufsize=socket.CMSG_SPACE(SIZEOF_INT) * 2) @testRecvTrafficClassAndHopLimitCMSG_SPACE.client_skip def _testRecvTrafficClassAndHopLimitCMSG_SPACE(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket.socket, "sendmsg") @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testSetTrafficClassAndHopLimit(self): # Test setting traffic class and hop limit on outgoing packet, # and receiving them at the other end. self.checkTrafficClassAndHopLimit(ancbufsize=10240, maxhop=self.hop_limit) @testSetTrafficClassAndHopLimit.client_skip def _testSetTrafficClassAndHopLimit(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.assertEqual( self.sendmsgToServer([MSG], [(socket.IPPROTO_IPV6, socket.IPV6_TCLASS, array.array("i", [self.traffic_class])), (socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT, array.array("i", [self.hop_limit]))]), len(MSG)) @requireAttrs(socket.socket, "sendmsg") @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testOddCmsgSize(self): # Try to send ancillary data with first item one byte too # long. Fall back to sending with correct size if this fails, # and check that second item was handled correctly. self.checkTrafficClassAndHopLimit(ancbufsize=10240, maxhop=self.hop_limit) @testOddCmsgSize.client_skip def _testOddCmsgSize(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) try: nbytes = self.sendmsgToServer( [MSG], [(socket.IPPROTO_IPV6, socket.IPV6_TCLASS, array.array("i", [self.traffic_class]).tobytes() + b"\x00"), (socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT, array.array("i", [self.hop_limit]))]) except OSError as e: self.assertIsInstance(e.errno, int) nbytes = self.sendmsgToServer( [MSG], [(socket.IPPROTO_IPV6, socket.IPV6_TCLASS, array.array("i", [self.traffic_class])), (socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT, array.array("i", [self.hop_limit]))]) self.assertEqual(nbytes, len(MSG)) # Tests for proper handling of truncated ancillary data def checkHopLimitTruncatedHeader(self, ancbufsize, ignoreflags=0): # Receive hop limit into ancbufsize bytes of ancillary data # space, which should be too small to contain the ancillary # data header (if ancbufsize is None, pass no second argument # to recvmsg()). Check that data is MSG, MSG_CTRUNC is set # (unless included in ignoreflags), and no ancillary data is # returned. self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.misc_event.set() args = () if ancbufsize is None else (ancbufsize,) msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), *args) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC, ignore=ignoreflags) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testCmsgTruncNoBufSize(self): # Check that no ancillary data is received when no ancillary # buffer size is provided. self.checkHopLimitTruncatedHeader(ancbufsize=None, # BSD seems to set # MSG_CTRUNC only if an item # has been partially # received. ignoreflags=socket.MSG_CTRUNC) @testCmsgTruncNoBufSize.client_skip def _testCmsgTruncNoBufSize(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testSingleCmsgTrunc0(self): # Check that no ancillary data is received when ancillary # buffer size is zero. self.checkHopLimitTruncatedHeader(ancbufsize=0, ignoreflags=socket.MSG_CTRUNC) @testSingleCmsgTrunc0.client_skip def _testSingleCmsgTrunc0(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) # Check that no ancillary data is returned for various non-zero # (but still too small) buffer sizes. @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testSingleCmsgTrunc1(self): self.checkHopLimitTruncatedHeader(ancbufsize=1) @testSingleCmsgTrunc1.client_skip def _testSingleCmsgTrunc1(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testSingleCmsgTrunc2Int(self): self.checkHopLimitTruncatedHeader(ancbufsize=2 * SIZEOF_INT) @testSingleCmsgTrunc2Int.client_skip def _testSingleCmsgTrunc2Int(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testSingleCmsgTruncLen0Minus1(self): self.checkHopLimitTruncatedHeader(ancbufsize=socket.CMSG_LEN(0) - 1) @testSingleCmsgTruncLen0Minus1.client_skip def _testSingleCmsgTruncLen0Minus1(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") def testSingleCmsgTruncInData(self): # Test truncation of a control message inside its associated # data. The message may be returned with its data truncated, # or not returned at all. self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.misc_event.set() msg, ancdata, flags, addr = self.doRecvmsg( self.serv_sock, len(MSG), socket.CMSG_LEN(SIZEOF_INT) - 1) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC) self.assertLessEqual(len(ancdata), 1) if ancdata: cmsg_level, cmsg_type, cmsg_data = ancdata[0] self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) self.assertEqual(cmsg_type, socket.IPV6_HOPLIMIT) self.assertLess(len(cmsg_data), SIZEOF_INT) @testSingleCmsgTruncInData.client_skip def _testSingleCmsgTruncInData(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) def checkTruncatedSecondHeader(self, ancbufsize, ignoreflags=0): # Receive traffic class and hop limit into ancbufsize bytes of # ancillary data space, which should be large enough to # contain the first item, but too small to contain the header # of the second. Check that data is MSG, MSG_CTRUNC is set # (unless included in ignoreflags), and only one ancillary # data item is returned. self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVTCLASS, 1) self.misc_event.set() msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), ancbufsize) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC, ignore=ignoreflags) self.assertEqual(len(ancdata), 1) cmsg_level, cmsg_type, cmsg_data = ancdata[0] self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) self.assertIn(cmsg_type, {socket.IPV6_TCLASS, socket.IPV6_HOPLIMIT}) self.assertEqual(len(cmsg_data), SIZEOF_INT) a = array.array("i") a.frombytes(cmsg_data) self.assertGreaterEqual(a[0], 0) self.assertLessEqual(a[0], 255) # Try the above test with various buffer sizes. @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testSecondCmsgTrunc0(self): self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT), ignoreflags=socket.MSG_CTRUNC) @testSecondCmsgTrunc0.client_skip def _testSecondCmsgTrunc0(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testSecondCmsgTrunc1(self): self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT) + 1) @testSecondCmsgTrunc1.client_skip def _testSecondCmsgTrunc1(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testSecondCmsgTrunc2Int(self): self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT) + 2 * SIZEOF_INT) @testSecondCmsgTrunc2Int.client_skip def _testSecondCmsgTrunc2Int(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testSecondCmsgTruncLen0Minus1(self): self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(0) - 1) @testSecondCmsgTruncLen0Minus1.client_skip def _testSecondCmsgTruncLen0Minus1(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", "IPV6_RECVTCLASS", "IPV6_TCLASS") def testSecomdCmsgTruncInData(self): # Test truncation of the second of two control messages inside # its associated data. self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVTCLASS, 1) self.misc_event.set() msg, ancdata, flags, addr = self.doRecvmsg( self.serv_sock, len(MSG), socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(SIZEOF_INT) - 1) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC) cmsg_types = {socket.IPV6_TCLASS, socket.IPV6_HOPLIMIT} cmsg_level, cmsg_type, cmsg_data = ancdata.pop(0) self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) cmsg_types.remove(cmsg_type) self.assertEqual(len(cmsg_data), SIZEOF_INT) a = array.array("i") a.frombytes(cmsg_data) self.assertGreaterEqual(a[0], 0) self.assertLessEqual(a[0], 255) if ancdata: cmsg_level, cmsg_type, cmsg_data = ancdata.pop(0) self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) cmsg_types.remove(cmsg_type) self.assertLess(len(cmsg_data), SIZEOF_INT) self.assertEqual(ancdata, []) @testSecomdCmsgTruncInData.client_skip def _testSecomdCmsgTruncInData(self): self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) self.sendToServer(MSG) # Derive concrete test classes for different socket types. class SendrecvmsgUDPTestBase(SendrecvmsgDgramFlagsBase, SendrecvmsgConnectionlessBase, ThreadedSocketTestMixin, UDPTestBase): pass @requireAttrs(socket.socket, "sendmsg") @unittest.skipUnless(thread, 'Threading required for this test.') class SendmsgUDPTest(SendmsgConnectionlessTests, SendrecvmsgUDPTestBase): pass @requireAttrs(socket.socket, "recvmsg") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgUDPTest(RecvmsgTests, SendrecvmsgUDPTestBase): pass @requireAttrs(socket.socket, "recvmsg_into") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoUDPTest(RecvmsgIntoTests, SendrecvmsgUDPTestBase): pass class SendrecvmsgUDP6TestBase(SendrecvmsgDgramFlagsBase, SendrecvmsgConnectionlessBase, ThreadedSocketTestMixin, UDP6TestBase): def checkRecvmsgAddress(self, addr1, addr2): # Called to compare the received address with the address of # the peer, ignoring scope ID self.assertEqual(addr1[:-1], addr2[:-1]) @requireAttrs(socket.socket, "sendmsg") @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') @requireSocket("AF_INET6", "SOCK_DGRAM") @unittest.skipUnless(thread, 'Threading required for this test.') class SendmsgUDP6Test(SendmsgConnectionlessTests, SendrecvmsgUDP6TestBase): pass @requireAttrs(socket.socket, "recvmsg") @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') @requireSocket("AF_INET6", "SOCK_DGRAM") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgUDP6Test(RecvmsgTests, SendrecvmsgUDP6TestBase): pass @requireAttrs(socket.socket, "recvmsg_into") @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') @requireSocket("AF_INET6", "SOCK_DGRAM") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoUDP6Test(RecvmsgIntoTests, SendrecvmsgUDP6TestBase): pass @requireAttrs(socket.socket, "recvmsg") @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') @requireAttrs(socket, "IPPROTO_IPV6") @requireSocket("AF_INET6", "SOCK_DGRAM") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgRFC3542AncillaryUDP6Test(RFC3542AncillaryTest, SendrecvmsgUDP6TestBase): pass @requireAttrs(socket.socket, "recvmsg_into") @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') @requireAttrs(socket, "IPPROTO_IPV6") @requireSocket("AF_INET6", "SOCK_DGRAM") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoRFC3542AncillaryUDP6Test(RecvmsgIntoMixin, RFC3542AncillaryTest, SendrecvmsgUDP6TestBase): pass class SendrecvmsgTCPTestBase(SendrecvmsgConnectedBase, ConnectedStreamTestMixin, TCPTestBase): pass @requireAttrs(socket.socket, "sendmsg") @unittest.skipUnless(thread, 'Threading required for this test.') class SendmsgTCPTest(SendmsgStreamTests, SendrecvmsgTCPTestBase): pass @requireAttrs(socket.socket, "recvmsg") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgTCPTest(RecvmsgTests, RecvmsgGenericStreamTests, SendrecvmsgTCPTestBase): pass @requireAttrs(socket.socket, "recvmsg_into") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoTCPTest(RecvmsgIntoTests, RecvmsgGenericStreamTests, SendrecvmsgTCPTestBase): pass class SendrecvmsgSCTPStreamTestBase(SendrecvmsgSCTPFlagsBase, SendrecvmsgConnectedBase, ConnectedStreamTestMixin, SCTPStreamBase): pass @requireAttrs(socket.socket, "sendmsg") @requireSocket("AF_INET", "SOCK_STREAM", "IPPROTO_SCTP") @unittest.skipUnless(thread, 'Threading required for this test.') class SendmsgSCTPStreamTest(SendmsgStreamTests, SendrecvmsgSCTPStreamTestBase): pass @requireAttrs(socket.socket, "recvmsg") @requireSocket("AF_INET", "SOCK_STREAM", "IPPROTO_SCTP") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgSCTPStreamTest(RecvmsgTests, RecvmsgGenericStreamTests, SendrecvmsgSCTPStreamTestBase): def testRecvmsgEOF(self): try: super(RecvmsgSCTPStreamTest, self).testRecvmsgEOF() except OSError as e: if e.errno != errno.ENOTCONN: raise self.skipTest("sporadic ENOTCONN (kernel issue?) - see issue #13876") @requireAttrs(socket.socket, "recvmsg_into") @requireSocket("AF_INET", "SOCK_STREAM", "IPPROTO_SCTP") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoSCTPStreamTest(RecvmsgIntoTests, RecvmsgGenericStreamTests, SendrecvmsgSCTPStreamTestBase): def testRecvmsgEOF(self): try: super(RecvmsgIntoSCTPStreamTest, self).testRecvmsgEOF() except OSError as e: if e.errno != errno.ENOTCONN: raise self.skipTest("sporadic ENOTCONN (kernel issue?) - see issue #13876") class SendrecvmsgUnixStreamTestBase(SendrecvmsgConnectedBase, ConnectedStreamTestMixin, UnixStreamBase): pass @requireAttrs(socket.socket, "sendmsg") @requireAttrs(socket, "AF_UNIX") @unittest.skipUnless(thread, 'Threading required for this test.') class SendmsgUnixStreamTest(SendmsgStreamTests, SendrecvmsgUnixStreamTestBase): pass @requireAttrs(socket.socket, "recvmsg") @requireAttrs(socket, "AF_UNIX") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgUnixStreamTest(RecvmsgTests, RecvmsgGenericStreamTests, SendrecvmsgUnixStreamTestBase): pass @requireAttrs(socket.socket, "recvmsg_into") @requireAttrs(socket, "AF_UNIX") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoUnixStreamTest(RecvmsgIntoTests, RecvmsgGenericStreamTests, SendrecvmsgUnixStreamTestBase): pass @requireAttrs(socket.socket, "sendmsg", "recvmsg") @requireAttrs(socket, "AF_UNIX", "SOL_SOCKET", "SCM_RIGHTS") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgSCMRightsStreamTest(SCMRightsTest, SendrecvmsgUnixStreamTestBase): pass @requireAttrs(socket.socket, "sendmsg", "recvmsg_into") @requireAttrs(socket, "AF_UNIX", "SOL_SOCKET", "SCM_RIGHTS") @unittest.skipUnless(thread, 'Threading required for this test.') class RecvmsgIntoSCMRightsStreamTest(RecvmsgIntoMixin, SCMRightsTest, SendrecvmsgUnixStreamTestBase): pass # Test interrupting the interruptible send/receive methods with a # signal when a timeout is set. These tests avoid having multiple # threads alive during the test so that the OS cannot deliver the # signal to the wrong one. class InterruptedTimeoutBase(unittest.TestCase): # Base class for interrupted send/receive tests. Installs an # empty handler for SIGALRM and removes it on teardown, along with # any scheduled alarms. def setUp(self): super().setUp() orig_alrm_handler = signal.signal(signal.SIGALRM, lambda signum, frame: None) self.addCleanup(signal.signal, signal.SIGALRM, orig_alrm_handler) self.addCleanup(self.setAlarm, 0) # Timeout for socket operations timeout = 4.0 # Provide setAlarm() method to schedule delivery of SIGALRM after # given number of seconds, or cancel it if zero, and an # appropriate time value to use. Use setitimer() if available. if hasattr(signal, "setitimer"): alarm_time = 0.05 def setAlarm(self, seconds): signal.setitimer(signal.ITIMER_REAL, seconds) else: # Old systems may deliver the alarm up to one second early alarm_time = 2 def setAlarm(self, seconds): signal.alarm(seconds) # Require siginterrupt() in order to ensure that system calls are # interrupted by default. @requireAttrs(signal, "siginterrupt") @unittest.skipUnless(hasattr(signal, "alarm") or hasattr(signal, "setitimer"), "Don't have signal.alarm or signal.setitimer") class InterruptedRecvTimeoutTest(InterruptedTimeoutBase, UDPTestBase): # Test interrupting the recv*() methods with signals when a # timeout is set. def setUp(self): super().setUp() self.serv.settimeout(self.timeout) def checkInterruptedRecv(self, func, *args, **kwargs): # Check that func(*args, **kwargs) raises OSError with an # errno of EINTR when interrupted by a signal. self.setAlarm(self.alarm_time) with self.assertRaises(OSError) as cm: func(*args, **kwargs) self.assertNotIsInstance(cm.exception, socket.timeout) self.assertEqual(cm.exception.errno, errno.EINTR) def testInterruptedRecvTimeout(self): self.checkInterruptedRecv(self.serv.recv, 1024) def testInterruptedRecvIntoTimeout(self): self.checkInterruptedRecv(self.serv.recv_into, bytearray(1024)) def testInterruptedRecvfromTimeout(self): self.checkInterruptedRecv(self.serv.recvfrom, 1024) def testInterruptedRecvfromIntoTimeout(self): self.checkInterruptedRecv(self.serv.recvfrom_into, bytearray(1024)) @requireAttrs(socket.socket, "recvmsg") def testInterruptedRecvmsgTimeout(self): self.checkInterruptedRecv(self.serv.recvmsg, 1024) @requireAttrs(socket.socket, "recvmsg_into") def testInterruptedRecvmsgIntoTimeout(self): self.checkInterruptedRecv(self.serv.recvmsg_into, [bytearray(1024)]) # Require siginterrupt() in order to ensure that system calls are # interrupted by default. @requireAttrs(signal, "siginterrupt") @unittest.skipUnless(hasattr(signal, "alarm") or hasattr(signal, "setitimer"), "Don't have signal.alarm or signal.setitimer") @unittest.skipUnless(thread, 'Threading required for this test.') class InterruptedSendTimeoutTest(InterruptedTimeoutBase, ThreadSafeCleanupTestCase, SocketListeningTestMixin, TCPTestBase): # Test interrupting the interruptible send*() methods with signals # when a timeout is set. def setUp(self): super().setUp() self.serv_conn = self.newSocket() self.addCleanup(self.serv_conn.close) # Use a thread to complete the connection, but wait for it to # terminate before running the test, so that there is only one # thread to accept the signal. cli_thread = threading.Thread(target=self.doConnect) cli_thread.start() self.cli_conn, addr = self.serv.accept() self.addCleanup(self.cli_conn.close) cli_thread.join() self.serv_conn.settimeout(self.timeout) def doConnect(self): self.serv_conn.connect(self.serv_addr) def checkInterruptedSend(self, func, *args, **kwargs): # Check that func(*args, **kwargs), run in a loop, raises # OSError with an errno of EINTR when interrupted by a # signal. with self.assertRaises(OSError) as cm: while True: self.setAlarm(self.alarm_time) func(*args, **kwargs) self.assertNotIsInstance(cm.exception, socket.timeout) self.assertEqual(cm.exception.errno, errno.EINTR) # Issue #12958: The following tests have problems on OS X prior to 10.7 @support.requires_mac_ver(10, 7) def testInterruptedSendTimeout(self): self.checkInterruptedSend(self.serv_conn.send, b"a"*512) @support.requires_mac_ver(10, 7) def testInterruptedSendtoTimeout(self): # Passing an actual address here as Python's wrapper for # sendto() doesn't allow passing a zero-length one; POSIX # requires that the address is ignored since the socket is # connection-mode, however. self.checkInterruptedSend(self.serv_conn.sendto, b"a"*512, self.serv_addr) @support.requires_mac_ver(10, 7) @requireAttrs(socket.socket, "sendmsg") def testInterruptedSendmsgTimeout(self): self.checkInterruptedSend(self.serv_conn.sendmsg, [b"a"*512]) @unittest.skipUnless(thread, 'Threading required for this test.') class TCPCloserTest(ThreadedTCPSocketTest): def testClose(self): conn, addr = self.serv.accept() conn.close() sd = self.cli read, write, err = select.select([sd], [], [], 1.0) self.assertEqual(read, [sd]) self.assertEqual(sd.recv(1), b'') # Calling close() many times should be safe. conn.close() conn.close() def _testClose(self): self.cli.connect((HOST, self.port)) time.sleep(1.0) @unittest.skipUnless(hasattr(socket, 'socketpair'), 'test needs socket.socketpair()') @unittest.skipUnless(thread, 'Threading required for this test.') class BasicSocketPairTest(SocketPairTest): def __init__(self, methodName='runTest'): SocketPairTest.__init__(self, methodName=methodName) def _check_defaults(self, sock): self.assertIsInstance(sock, socket.socket) if hasattr(socket, 'AF_UNIX'): self.assertEqual(sock.family, socket.AF_UNIX) else: self.assertEqual(sock.family, socket.AF_INET) self.assertEqual(sock.type, socket.SOCK_STREAM) self.assertEqual(sock.proto, 0) def _testDefaults(self): self._check_defaults(self.cli) def testDefaults(self): self._check_defaults(self.serv) def testRecv(self): msg = self.serv.recv(1024) self.assertEqual(msg, MSG) def _testRecv(self): self.cli.send(MSG) def testSend(self): self.serv.send(MSG) def _testSend(self): msg = self.cli.recv(1024) self.assertEqual(msg, MSG) @unittest.skipUnless(thread, 'Threading required for this test.') class NonBlockingTCPTests(ThreadedTCPSocketTest): def __init__(self, methodName='runTest'): ThreadedTCPSocketTest.__init__(self, methodName=methodName) def testSetBlocking(self): # Testing whether set blocking works self.serv.setblocking(True) self.assertIsNone(self.serv.gettimeout()) self.serv.setblocking(False) self.assertEqual(self.serv.gettimeout(), 0.0) start = time.time() try: self.serv.accept() except OSError: pass end = time.time() self.assertTrue((end - start) < 1.0, "Error setting non-blocking mode.") def _testSetBlocking(self): pass @support.cpython_only def testSetBlocking_overflow(self): # Issue 15989 import _testcapi if _testcapi.UINT_MAX >= _testcapi.ULONG_MAX: self.skipTest('needs UINT_MAX < ULONG_MAX') self.serv.setblocking(False) self.assertEqual(self.serv.gettimeout(), 0.0) self.serv.setblocking(_testcapi.UINT_MAX + 1) self.assertIsNone(self.serv.gettimeout()) _testSetBlocking_overflow = support.cpython_only(_testSetBlocking) @unittest.skipUnless(hasattr(socket, 'SOCK_NONBLOCK'), 'test needs socket.SOCK_NONBLOCK') @support.requires_linux_version(2, 6, 28) def testInitNonBlocking(self): # reinit server socket self.serv.close() self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM | socket.SOCK_NONBLOCK) self.port = support.bind_port(self.serv) self.serv.listen(1) # actual testing start = time.time() try: self.serv.accept() except OSError: pass end = time.time() self.assertTrue((end - start) < 1.0, "Error creating with non-blocking mode.") def _testInitNonBlocking(self): pass def testInheritFlags(self): # Issue #7995: when calling accept() on a listening socket with a # timeout, the resulting socket should not be non-blocking. self.serv.settimeout(10) try: conn, addr = self.serv.accept() message = conn.recv(len(MSG)) finally: conn.close() self.serv.settimeout(None) def _testInheritFlags(self): time.sleep(0.1) self.cli.connect((HOST, self.port)) time.sleep(0.5) self.cli.send(MSG) def testAccept(self): # Testing non-blocking accept self.serv.setblocking(0) try: conn, addr = self.serv.accept() except OSError: pass else: self.fail("Error trying to do non-blocking accept.") read, write, err = select.select([self.serv], [], []) if self.serv in read: conn, addr = self.serv.accept() conn.close() else: self.fail("Error trying to do accept after select.") def _testAccept(self): time.sleep(0.1) self.cli.connect((HOST, self.port)) def testConnect(self): # Testing non-blocking connect conn, addr = self.serv.accept() conn.close() def _testConnect(self): self.cli.settimeout(10) self.cli.connect((HOST, self.port)) def testRecv(self): # Testing non-blocking recv conn, addr = self.serv.accept() conn.setblocking(0) try: msg = conn.recv(len(MSG)) except OSError: pass else: self.fail("Error trying to do non-blocking recv.") read, write, err = select.select([conn], [], []) if conn in read: msg = conn.recv(len(MSG)) conn.close() self.assertEqual(msg, MSG) else: self.fail("Error during select call to non-blocking socket.") def _testRecv(self): self.cli.connect((HOST, self.port)) time.sleep(0.1) self.cli.send(MSG) @unittest.skipUnless(thread, 'Threading required for this test.') class FileObjectClassTestCase(SocketConnectedTest): """Unit tests for the object returned by socket.makefile() self.read_file is the io object returned by makefile() on the client connection. You can read from this file to get output from the server. self.write_file is the io object returned by makefile() on the server connection. You can write to this file to send output to the client. """ bufsize = -1 # Use default buffer size encoding = 'utf-8' errors = 'strict' newline = None read_mode = 'rb' read_msg = MSG write_mode = 'wb' write_msg = MSG def __init__(self, methodName='runTest'): SocketConnectedTest.__init__(self, methodName=methodName) def setUp(self): self.evt1, self.evt2, self.serv_finished, self.cli_finished = [ threading.Event() for i in range(4)] SocketConnectedTest.setUp(self) self.read_file = self.cli_conn.makefile( self.read_mode, self.bufsize, encoding = self.encoding, errors = self.errors, newline = self.newline) def tearDown(self): self.serv_finished.set() self.read_file.close() self.assertTrue(self.read_file.closed) self.read_file = None SocketConnectedTest.tearDown(self) def clientSetUp(self): SocketConnectedTest.clientSetUp(self) self.write_file = self.serv_conn.makefile( self.write_mode, self.bufsize, encoding = self.encoding, errors = self.errors, newline = self.newline) def clientTearDown(self): self.cli_finished.set() self.write_file.close() self.assertTrue(self.write_file.closed) self.write_file = None SocketConnectedTest.clientTearDown(self) def testReadAfterTimeout(self): # Issue #7322: A file object must disallow further reads # after a timeout has occurred. self.cli_conn.settimeout(1) self.read_file.read(3) # First read raises a timeout self.assertRaises(socket.timeout, self.read_file.read, 1) # Second read is disallowed with self.assertRaises(OSError) as ctx: self.read_file.read(1) self.assertIn("cannot read from timed out object", str(ctx.exception)) def _testReadAfterTimeout(self): self.write_file.write(self.write_msg[0:3]) self.write_file.flush() self.serv_finished.wait() def testSmallRead(self): # Performing small file read test first_seg = self.read_file.read(len(self.read_msg)-3) second_seg = self.read_file.read(3) msg = first_seg + second_seg self.assertEqual(msg, self.read_msg) def _testSmallRead(self): self.write_file.write(self.write_msg) self.write_file.flush() def testFullRead(self): # read until EOF msg = self.read_file.read() self.assertEqual(msg, self.read_msg) def _testFullRead(self): self.write_file.write(self.write_msg) self.write_file.close() def testUnbufferedRead(self): # Performing unbuffered file read test buf = type(self.read_msg)() while 1: char = self.read_file.read(1) if not char: break buf += char self.assertEqual(buf, self.read_msg) def _testUnbufferedRead(self): self.write_file.write(self.write_msg) self.write_file.flush() def testReadline(self): # Performing file readline test line = self.read_file.readline() self.assertEqual(line, self.read_msg) def _testReadline(self): self.write_file.write(self.write_msg) self.write_file.flush() def testCloseAfterMakefile(self): # The file returned by makefile should keep the socket open. self.cli_conn.close() # read until EOF msg = self.read_file.read() self.assertEqual(msg, self.read_msg) def _testCloseAfterMakefile(self): self.write_file.write(self.write_msg) self.write_file.flush() def testMakefileAfterMakefileClose(self): self.read_file.close() msg = self.cli_conn.recv(len(MSG)) if isinstance(self.read_msg, str): msg = msg.decode() self.assertEqual(msg, self.read_msg) def _testMakefileAfterMakefileClose(self): self.write_file.write(self.write_msg) self.write_file.flush() def testClosedAttr(self): self.assertTrue(not self.read_file.closed) def _testClosedAttr(self): self.assertTrue(not self.write_file.closed) def testAttributes(self): self.assertEqual(self.read_file.mode, self.read_mode) self.assertEqual(self.read_file.name, self.cli_conn.fileno()) def _testAttributes(self): self.assertEqual(self.write_file.mode, self.write_mode) self.assertEqual(self.write_file.name, self.serv_conn.fileno()) def testRealClose(self): self.read_file.close() self.assertRaises(ValueError, self.read_file.fileno) self.cli_conn.close() self.assertRaises(OSError, self.cli_conn.getsockname) def _testRealClose(self): pass class FileObjectInterruptedTestCase(unittest.TestCase): """Test that the file object correctly handles EINTR internally.""" class MockSocket(object): def __init__(self, recv_funcs=()): # A generator that returns callables that we'll call for each # call to recv(). self._recv_step = iter(recv_funcs) def recv_into(self, buffer): data = next(self._recv_step)() assert len(buffer) >= len(data) buffer[:len(data)] = data return len(data) def _decref_socketios(self): pass def _textiowrap_for_test(self, buffering=-1): raw = socket.SocketIO(self, "r") if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: return raw buffer = io.BufferedReader(raw, buffering) text = io.TextIOWrapper(buffer, None, None) text.mode = "rb" return text @staticmethod def _raise_eintr(): raise OSError(errno.EINTR, "interrupted") def _textiowrap_mock_socket(self, mock, buffering=-1): raw = socket.SocketIO(mock, "r") if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: return raw buffer = io.BufferedReader(raw, buffering) text = io.TextIOWrapper(buffer, None, None) text.mode = "rb" return text def _test_readline(self, size=-1, buffering=-1): mock_sock = self.MockSocket(recv_funcs=[ lambda : b"This is the first line\nAnd the sec", self._raise_eintr, lambda : b"ond line is here\n", lambda : b"", lambda : b"", # XXX(gps): io library does an extra EOF read ]) fo = mock_sock._textiowrap_for_test(buffering=buffering) self.assertEqual(fo.readline(size), "This is the first line\n") self.assertEqual(fo.readline(size), "And the second line is here\n") def _test_read(self, size=-1, buffering=-1): mock_sock = self.MockSocket(recv_funcs=[ lambda : b"This is the first line\nAnd the sec", self._raise_eintr, lambda : b"ond line is here\n", lambda : b"", lambda : b"", # XXX(gps): io library does an extra EOF read ]) expecting = (b"This is the first line\n" b"And the second line is here\n") fo = mock_sock._textiowrap_for_test(buffering=buffering) if buffering == 0: data = b'' else: data = '' expecting = expecting.decode('utf-8') while len(data) != len(expecting): part = fo.read(size) if not part: break data += part self.assertEqual(data, expecting) def test_default(self): self._test_readline() self._test_readline(size=100) self._test_read() self._test_read(size=100) def test_with_1k_buffer(self): self._test_readline(buffering=1024) self._test_readline(size=100, buffering=1024) self._test_read(buffering=1024) self._test_read(size=100, buffering=1024) def _test_readline_no_buffer(self, size=-1): mock_sock = self.MockSocket(recv_funcs=[ lambda : b"a", lambda : b"\n", lambda : b"B", self._raise_eintr, lambda : b"b", lambda : b"", ]) fo = mock_sock._textiowrap_for_test(buffering=0) self.assertEqual(fo.readline(size), b"a\n") self.assertEqual(fo.readline(size), b"Bb") def test_no_buffer(self): self._test_readline_no_buffer() self._test_readline_no_buffer(size=4) self._test_read(buffering=0) self._test_read(size=100, buffering=0) class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase): """Repeat the tests from FileObjectClassTestCase with bufsize==0. In this case (and in this case only), it should be possible to create a file object, read a line from it, create another file object, read another line from it, without loss of data in the first file object's buffer. Note that http.client relies on this when reading multiple requests from the same socket.""" bufsize = 0 # Use unbuffered mode def testUnbufferedReadline(self): # Read a line, create a new file object, read another line with it line = self.read_file.readline() # first line self.assertEqual(line, b"A. " + self.write_msg) # first line self.read_file = self.cli_conn.makefile('rb', 0) line = self.read_file.readline() # second line self.assertEqual(line, b"B. " + self.write_msg) # second line def _testUnbufferedReadline(self): self.write_file.write(b"A. " + self.write_msg) self.write_file.write(b"B. " + self.write_msg) self.write_file.flush() def testMakefileClose(self): # The file returned by makefile should keep the socket open... self.cli_conn.close() msg = self.cli_conn.recv(1024) self.assertEqual(msg, self.read_msg) # ...until the file is itself closed self.read_file.close() self.assertRaises(OSError, self.cli_conn.recv, 1024) def _testMakefileClose(self): self.write_file.write(self.write_msg) self.write_file.flush() def testMakefileCloseSocketDestroy(self): refcount_before = sys.getrefcount(self.cli_conn) self.read_file.close() refcount_after = sys.getrefcount(self.cli_conn) self.assertEqual(refcount_before - 1, refcount_after) def _testMakefileCloseSocketDestroy(self): pass # Non-blocking ops # NOTE: to set `read_file` as non-blocking, we must call # `cli_conn.setblocking` and vice-versa (see setUp / clientSetUp). def testSmallReadNonBlocking(self): self.cli_conn.setblocking(False) self.assertEqual(self.read_file.readinto(bytearray(10)), None) self.assertEqual(self.read_file.read(len(self.read_msg) - 3), None) self.evt1.set() self.evt2.wait(1.0) first_seg = self.read_file.read(len(self.read_msg) - 3) if first_seg is None: # Data not arrived (can happen under Windows), wait a bit time.sleep(0.5) first_seg = self.read_file.read(len(self.read_msg) - 3) buf = bytearray(10) n = self.read_file.readinto(buf) self.assertEqual(n, 3) msg = first_seg + buf[:n] self.assertEqual(msg, self.read_msg) self.assertEqual(self.read_file.readinto(bytearray(16)), None) self.assertEqual(self.read_file.read(1), None) def _testSmallReadNonBlocking(self): self.evt1.wait(1.0) self.write_file.write(self.write_msg) self.write_file.flush() self.evt2.set() # Avoid cloding the socket before the server test has finished, # otherwise system recv() will return 0 instead of EWOULDBLOCK. self.serv_finished.wait(5.0) def testWriteNonBlocking(self): self.cli_finished.wait(5.0) # The client thread can't skip directly - the SkipTest exception # would appear as a failure. if self.serv_skipped: self.skipTest(self.serv_skipped) def _testWriteNonBlocking(self): self.serv_skipped = None self.serv_conn.setblocking(False) # Try to saturate the socket buffer pipe with repeated large writes. BIG = b"x" * support.SOCK_MAX_SIZE LIMIT = 10 # The first write() succeeds since a chunk of data can be buffered n = self.write_file.write(BIG) self.assertGreater(n, 0) for i in range(LIMIT): n = self.write_file.write(BIG) if n is None: # Succeeded break self.assertGreater(n, 0) else: # Let us know that this test didn't manage to establish # the expected conditions. This is not a failure in itself but, # if it happens repeatedly, the test should be fixed. self.serv_skipped = "failed to saturate the socket buffer" class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase): bufsize = 1 # Default-buffered for reading; line-buffered for writing class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase): bufsize = 2 # Exercise the buffering code class UnicodeReadFileObjectClassTestCase(FileObjectClassTestCase): """Tests for socket.makefile() in text mode (rather than binary)""" read_mode = 'r' read_msg = MSG.decode('utf-8') write_mode = 'wb' write_msg = MSG newline = '' class UnicodeWriteFileObjectClassTestCase(FileObjectClassTestCase): """Tests for socket.makefile() in text mode (rather than binary)""" read_mode = 'rb' read_msg = MSG write_mode = 'w' write_msg = MSG.decode('utf-8') newline = '' class UnicodeReadWriteFileObjectClassTestCase(FileObjectClassTestCase): """Tests for socket.makefile() in text mode (rather than binary)""" read_mode = 'r' read_msg = MSG.decode('utf-8') write_mode = 'w' write_msg = MSG.decode('utf-8') newline = '' class NetworkConnectionTest(object): """Prove network connection.""" def clientSetUp(self): # We're inherited below by BasicTCPTest2, which also inherits # BasicTCPTest, which defines self.port referenced below. self.cli = socket.create_connection((HOST, self.port)) self.serv_conn = self.cli class BasicTCPTest2(NetworkConnectionTest, BasicTCPTest): """Tests that NetworkConnection does not break existing TCP functionality. """ class NetworkConnectionNoServer(unittest.TestCase): class MockSocket(socket.socket): def connect(self, *args): raise socket.timeout('timed out') @contextlib.contextmanager def mocked_socket_module(self): """Return a socket which times out on connect""" old_socket = socket.socket socket.socket = self.MockSocket try: yield finally: socket.socket = old_socket def test_connect(self): port = support.find_unused_port() cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(cli.close) with self.assertRaises(OSError) as cm: cli.connect((HOST, port)) self.assertEqual(cm.exception.errno, errno.ECONNREFUSED) def test_create_connection(self): # Issue #9792: errors raised by create_connection() should have # a proper errno attribute. port = support.find_unused_port() with self.assertRaises(OSError) as cm: socket.create_connection((HOST, port)) # Issue #16257: create_connection() calls getaddrinfo() against # 'localhost'. This may result in an IPV6 addr being returned # as well as an IPV4 one: # >>> socket.getaddrinfo('localhost', port, 0, SOCK_STREAM) # >>> [(2, 2, 0, '', ('127.0.0.1', 41230)), # (26, 2, 0, '', ('::1', 41230, 0, 0))] # # create_connection() enumerates through all the addresses returned # and if it doesn't successfully bind to any of them, it propagates # the last exception it encountered. # # On Solaris, ENETUNREACH is returned in this circumstance instead # of ECONNREFUSED. So, if that errno exists, add it to our list of # expected errnos. expected_errnos = [ errno.ECONNREFUSED, ] if hasattr(errno, 'ENETUNREACH'): expected_errnos.append(errno.ENETUNREACH) self.assertIn(cm.exception.errno, expected_errnos) def test_create_connection_timeout(self): # Issue #9792: create_connection() should not recast timeout errors # as generic socket errors. with self.mocked_socket_module(): with self.assertRaises(socket.timeout): socket.create_connection((HOST, 1234)) @unittest.skipUnless(thread, 'Threading required for this test.') class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest): def __init__(self, methodName='runTest'): SocketTCPTest.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): self.source_port = support.find_unused_port() def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) def _justAccept(self): conn, addr = self.serv.accept() conn.close() testFamily = _justAccept def _testFamily(self): self.cli = socket.create_connection((HOST, self.port), timeout=30) self.addCleanup(self.cli.close) self.assertEqual(self.cli.family, 2) testSourceAddress = _justAccept def _testSourceAddress(self): self.cli = socket.create_connection((HOST, self.port), timeout=30, source_address=('', self.source_port)) self.addCleanup(self.cli.close) self.assertEqual(self.cli.getsockname()[1], self.source_port) # The port number being used is sufficient to show that the bind() # call happened. testTimeoutDefault = _justAccept def _testTimeoutDefault(self): # passing no explicit timeout uses socket's global default self.assertTrue(socket.getdefaulttimeout() is None) socket.setdefaulttimeout(42) try: self.cli = socket.create_connection((HOST, self.port)) self.addCleanup(self.cli.close) finally: socket.setdefaulttimeout(None) self.assertEqual(self.cli.gettimeout(), 42) testTimeoutNone = _justAccept def _testTimeoutNone(self): # None timeout means the same as sock.settimeout(None) self.assertTrue(socket.getdefaulttimeout() is None) socket.setdefaulttimeout(30) try: self.cli = socket.create_connection((HOST, self.port), timeout=None) self.addCleanup(self.cli.close) finally: socket.setdefaulttimeout(None) self.assertEqual(self.cli.gettimeout(), None) testTimeoutValueNamed = _justAccept def _testTimeoutValueNamed(self): self.cli = socket.create_connection((HOST, self.port), timeout=30) self.assertEqual(self.cli.gettimeout(), 30) testTimeoutValueNonamed = _justAccept def _testTimeoutValueNonamed(self): self.cli = socket.create_connection((HOST, self.port), 30) self.addCleanup(self.cli.close) self.assertEqual(self.cli.gettimeout(), 30) @unittest.skipUnless(thread, 'Threading required for this test.') class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest): def __init__(self, methodName='runTest'): SocketTCPTest.__init__(self, methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): pass def clientTearDown(self): self.cli.close() self.cli = None ThreadableTest.clientTearDown(self) def testInsideTimeout(self): conn, addr = self.serv.accept() self.addCleanup(conn.close) time.sleep(3) conn.send(b"done!") testOutsideTimeout = testInsideTimeout def _testInsideTimeout(self): self.cli = sock = socket.create_connection((HOST, self.port)) data = sock.recv(5) self.assertEqual(data, b"done!") def _testOutsideTimeout(self): self.cli = sock = socket.create_connection((HOST, self.port), timeout=1) self.assertRaises(socket.timeout, lambda: sock.recv(5)) class TCPTimeoutTest(SocketTCPTest): def testTCPTimeout(self): def raise_timeout(*args, **kwargs): self.serv.settimeout(1.0) self.serv.accept() self.assertRaises(socket.timeout, raise_timeout, "Error generating a timeout exception (TCP)") def testTimeoutZero(self): ok = False try: self.serv.settimeout(0.0) foo = self.serv.accept() except socket.timeout: self.fail("caught timeout instead of error (TCP)") except OSError: ok = True except: self.fail("caught unexpected exception (TCP)") if not ok: self.fail("accept() returned success when we did not expect it") @unittest.skipUnless(hasattr(signal, 'alarm'), 'test needs signal.alarm()') def testInterruptedTimeout(self): # XXX I don't know how to do this test on MSWindows or any other # plaform that doesn't support signal.alarm() or os.kill(), though # the bug should have existed on all platforms. self.serv.settimeout(5.0) # must be longer than alarm class Alarm(Exception): pass def alarm_handler(signal, frame): raise Alarm old_alarm = signal.signal(signal.SIGALRM, alarm_handler) try: signal.alarm(2) # POSIX allows alarm to be up to 1 second early try: foo = self.serv.accept() except socket.timeout: self.fail("caught timeout instead of Alarm") except Alarm: pass except: self.fail("caught other exception instead of Alarm:" " %s(%s):\n%s" % (sys.exc_info()[:2] + (traceback.format_exc(),))) else: self.fail("nothing caught") finally: signal.alarm(0) # shut off alarm except Alarm: self.fail("got Alarm in wrong place") finally: # no alarm can be pending. Safe to restore old handler. signal.signal(signal.SIGALRM, old_alarm) class UDPTimeoutTest(SocketUDPTest): def testUDPTimeout(self): def raise_timeout(*args, **kwargs): self.serv.settimeout(1.0) self.serv.recv(1024) self.assertRaises(socket.timeout, raise_timeout, "Error generating a timeout exception (UDP)") def testTimeoutZero(self): ok = False try: self.serv.settimeout(0.0) foo = self.serv.recv(1024) except socket.timeout: self.fail("caught timeout instead of error (UDP)") except OSError: ok = True except: self.fail("caught unexpected exception (UDP)") if not ok: self.fail("recv() returned success when we did not expect it") class TestExceptions(unittest.TestCase): def testExceptionTree(self): self.assertTrue(issubclass(OSError, Exception)) self.assertTrue(issubclass(socket.herror, OSError)) self.assertTrue(issubclass(socket.gaierror, OSError)) self.assertTrue(issubclass(socket.timeout, OSError)) @unittest.skipUnless(sys.platform == 'linux', 'Linux specific test') class TestLinuxAbstractNamespace(unittest.TestCase): UNIX_PATH_MAX = 108 def testLinuxAbstractNamespace(self): address = b"\x00python-test-hello\x00\xff" with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s1: s1.bind(address) s1.listen(1) with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s2: s2.connect(s1.getsockname()) with s1.accept()[0] as s3: self.assertEqual(s1.getsockname(), address) self.assertEqual(s2.getpeername(), address) def testMaxName(self): address = b"\x00" + b"h" * (self.UNIX_PATH_MAX - 1) with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: s.bind(address) self.assertEqual(s.getsockname(), address) def testNameOverflow(self): address = "\x00" + "h" * self.UNIX_PATH_MAX with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: self.assertRaises(OSError, s.bind, address) def testStrName(self): # Check that an abstract name can be passed as a string. s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: s.bind("\x00python\x00test\x00") self.assertEqual(s.getsockname(), b"\x00python\x00test\x00") finally: s.close() @unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'test needs socket.AF_UNIX') class TestUnixDomain(unittest.TestCase): def setUp(self): self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) def tearDown(self): self.sock.close() def encoded(self, path): # Return the given path encoded in the file system encoding, # or skip the test if this is not possible. try: return os.fsencode(path) except UnicodeEncodeError: self.skipTest( "Pathname {0!a} cannot be represented in file " "system encoding {1!r}".format( path, sys.getfilesystemencoding())) def bind(self, sock, path): # Bind the socket try: sock.bind(path) except OSError as e: if str(e) == "AF_UNIX path too long": self.skipTest( "Pathname {0!a} is too long to serve as a AF_UNIX path" .format(path)) else: raise def testStrAddr(self): # Test binding to and retrieving a normal string pathname. path = os.path.abspath(support.TESTFN) self.bind(self.sock, path) self.addCleanup(support.unlink, path) self.assertEqual(self.sock.getsockname(), path) def testBytesAddr(self): # Test binding to a bytes pathname. path = os.path.abspath(support.TESTFN) self.bind(self.sock, self.encoded(path)) self.addCleanup(support.unlink, path) self.assertEqual(self.sock.getsockname(), path) def testSurrogateescapeBind(self): # Test binding to a valid non-ASCII pathname, with the # non-ASCII bytes supplied using surrogateescape encoding. path = os.path.abspath(support.TESTFN_UNICODE) b = self.encoded(path) self.bind(self.sock, b.decode("ascii", "surrogateescape")) self.addCleanup(support.unlink, path) self.assertEqual(self.sock.getsockname(), path) def testUnencodableAddr(self): # Test binding to a pathname that cannot be encoded in the # file system encoding. if support.TESTFN_UNENCODABLE is None: self.skipTest("No unencodable filename available") path = os.path.abspath(support.TESTFN_UNENCODABLE) self.bind(self.sock, path) self.addCleanup(support.unlink, path) self.assertEqual(self.sock.getsockname(), path) @unittest.skipUnless(thread, 'Threading required for this test.') class BufferIOTest(SocketConnectedTest): """ Test the buffer versions of socket.recv() and socket.send(). """ def __init__(self, methodName='runTest'): SocketConnectedTest.__init__(self, methodName=methodName) def testRecvIntoArray(self): buf = bytearray(1024) nbytes = self.cli_conn.recv_into(buf) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] self.assertEqual(msg, MSG) def _testRecvIntoArray(self): buf = bytes(MSG) self.serv_conn.send(buf) def testRecvIntoBytearray(self): buf = bytearray(1024) nbytes = self.cli_conn.recv_into(buf) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] self.assertEqual(msg, MSG) _testRecvIntoBytearray = _testRecvIntoArray def testRecvIntoMemoryview(self): buf = bytearray(1024) nbytes = self.cli_conn.recv_into(memoryview(buf)) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] self.assertEqual(msg, MSG) _testRecvIntoMemoryview = _testRecvIntoArray def testRecvFromIntoArray(self): buf = bytearray(1024) nbytes, addr = self.cli_conn.recvfrom_into(buf) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] self.assertEqual(msg, MSG) def _testRecvFromIntoArray(self): buf = bytes(MSG) self.serv_conn.send(buf) def testRecvFromIntoBytearray(self): buf = bytearray(1024) nbytes, addr = self.cli_conn.recvfrom_into(buf) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] self.assertEqual(msg, MSG) _testRecvFromIntoBytearray = _testRecvFromIntoArray def testRecvFromIntoMemoryview(self): buf = bytearray(1024) nbytes, addr = self.cli_conn.recvfrom_into(memoryview(buf)) self.assertEqual(nbytes, len(MSG)) msg = buf[:len(MSG)] self.assertEqual(msg, MSG) _testRecvFromIntoMemoryview = _testRecvFromIntoArray def testRecvFromIntoSmallBuffer(self): # See issue #20246. buf = bytearray(8) self.assertRaises(ValueError, self.cli_conn.recvfrom_into, buf, 1024) def _testRecvFromIntoSmallBuffer(self): self.serv_conn.send(MSG) def testRecvFromIntoEmptyBuffer(self): buf = bytearray() self.cli_conn.recvfrom_into(buf) self.cli_conn.recvfrom_into(buf, 0) _testRecvFromIntoEmptyBuffer = _testRecvFromIntoArray TIPC_STYPE = 2000 TIPC_LOWER = 200 TIPC_UPPER = 210 def isTipcAvailable(): """Check if the TIPC module is loaded The TIPC module is not loaded automatically on Ubuntu and probably other Linux distros. """ if not hasattr(socket, "AF_TIPC"): return False if not os.path.isfile("/proc/modules"): return False with open("/proc/modules") as f: for line in f: if line.startswith("tipc "): return True return False @unittest.skipUnless(isTipcAvailable(), "TIPC module is not loaded, please 'sudo modprobe tipc'") class TIPCTest(unittest.TestCase): def testRDM(self): srv = socket.socket(socket.AF_TIPC, socket.SOCK_RDM) cli = socket.socket(socket.AF_TIPC, socket.SOCK_RDM) self.addCleanup(srv.close) self.addCleanup(cli.close) srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE, TIPC_LOWER, TIPC_UPPER) srv.bind(srvaddr) sendaddr = (socket.TIPC_ADDR_NAME, TIPC_STYPE, TIPC_LOWER + int((TIPC_UPPER - TIPC_LOWER) / 2), 0) cli.sendto(MSG, sendaddr) msg, recvaddr = srv.recvfrom(1024) self.assertEqual(cli.getsockname(), recvaddr) self.assertEqual(msg, MSG) @unittest.skipUnless(isTipcAvailable(), "TIPC module is not loaded, please 'sudo modprobe tipc'") class TIPCThreadableTest(unittest.TestCase, ThreadableTest): def __init__(self, methodName = 'runTest'): unittest.TestCase.__init__(self, methodName = methodName) ThreadableTest.__init__(self) def setUp(self): self.srv = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM) self.addCleanup(self.srv.close) self.srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE, TIPC_LOWER, TIPC_UPPER) self.srv.bind(srvaddr) self.srv.listen(5) self.serverExplicitReady() self.conn, self.connaddr = self.srv.accept() self.addCleanup(self.conn.close) def clientSetUp(self): # The is a hittable race between serverExplicitReady() and the # accept() call; sleep a little while to avoid it, otherwise # we could get an exception time.sleep(0.1) self.cli = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM) self.addCleanup(self.cli.close) addr = (socket.TIPC_ADDR_NAME, TIPC_STYPE, TIPC_LOWER + int((TIPC_UPPER - TIPC_LOWER) / 2), 0) self.cli.connect(addr) self.cliaddr = self.cli.getsockname() def testStream(self): msg = self.conn.recv(1024) self.assertEqual(msg, MSG) self.assertEqual(self.cliaddr, self.connaddr) def _testStream(self): self.cli.send(MSG) self.cli.close() @unittest.skipUnless(thread, 'Threading required for this test.') class ContextManagersTest(ThreadedTCPSocketTest): def _testSocketClass(self): # base test with socket.socket() as sock: self.assertFalse(sock._closed) self.assertTrue(sock._closed) # close inside with block with socket.socket() as sock: sock.close() self.assertTrue(sock._closed) # exception inside with block with socket.socket() as sock: self.assertRaises(OSError, sock.sendall, b'foo') self.assertTrue(sock._closed) def testCreateConnectionBase(self): conn, addr = self.serv.accept() self.addCleanup(conn.close) data = conn.recv(1024) conn.sendall(data) def _testCreateConnectionBase(self): address = self.serv.getsockname() with socket.create_connection(address) as sock: self.assertFalse(sock._closed) sock.sendall(b'foo') self.assertEqual(sock.recv(1024), b'foo') self.assertTrue(sock._closed) def testCreateConnectionClose(self): conn, addr = self.serv.accept() self.addCleanup(conn.close) data = conn.recv(1024) conn.sendall(data) def _testCreateConnectionClose(self): address = self.serv.getsockname() with socket.create_connection(address) as sock: sock.close() self.assertTrue(sock._closed) self.assertRaises(OSError, sock.sendall, b'foo') class InheritanceTest(unittest.TestCase): @unittest.skipUnless(hasattr(socket, "SOCK_CLOEXEC"), "SOCK_CLOEXEC not defined") @support.requires_linux_version(2, 6, 28) def test_SOCK_CLOEXEC(self): with socket.socket(socket.AF_INET, socket.SOCK_STREAM | socket.SOCK_CLOEXEC) as s: self.assertTrue(s.type & socket.SOCK_CLOEXEC) self.assertFalse(s.get_inheritable()) def test_default_inheritable(self): sock = socket.socket() with sock: self.assertEqual(sock.get_inheritable(), False) def test_dup(self): sock = socket.socket() with sock: newsock = sock.dup() sock.close() with newsock: self.assertEqual(newsock.get_inheritable(), False) def test_set_inheritable(self): sock = socket.socket() with sock: sock.set_inheritable(True) self.assertEqual(sock.get_inheritable(), True) sock.set_inheritable(False) self.assertEqual(sock.get_inheritable(), False) @unittest.skipIf(fcntl is None, "need fcntl") def test_get_inheritable_cloexec(self): sock = socket.socket() with sock: fd = sock.fileno() self.assertEqual(sock.get_inheritable(), False) # clear FD_CLOEXEC flag flags = fcntl.fcntl(fd, fcntl.F_GETFD) flags &= ~fcntl.FD_CLOEXEC fcntl.fcntl(fd, fcntl.F_SETFD, flags) self.assertEqual(sock.get_inheritable(), True) @unittest.skipIf(fcntl is None, "need fcntl") def test_set_inheritable_cloexec(self): sock = socket.socket() with sock: fd = sock.fileno() self.assertEqual(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC, fcntl.FD_CLOEXEC) sock.set_inheritable(True) self.assertEqual(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC, 0) @unittest.skipUnless(hasattr(socket, "socketpair"), "need socket.socketpair()") def test_socketpair(self): s1, s2 = socket.socketpair() self.addCleanup(s1.close) self.addCleanup(s2.close) self.assertEqual(s1.get_inheritable(), False) self.assertEqual(s2.get_inheritable(), False) @unittest.skipUnless(hasattr(socket, "SOCK_NONBLOCK"), "SOCK_NONBLOCK not defined") class NonblockConstantTest(unittest.TestCase): def checkNonblock(self, s, nonblock=True, timeout=0.0): if nonblock: self.assertTrue(s.type & socket.SOCK_NONBLOCK) self.assertEqual(s.gettimeout(), timeout) else: self.assertFalse(s.type & socket.SOCK_NONBLOCK) self.assertEqual(s.gettimeout(), None) @support.requires_linux_version(2, 6, 28) def test_SOCK_NONBLOCK(self): # a lot of it seems silly and redundant, but I wanted to test that # changing back and forth worked ok with socket.socket(socket.AF_INET, socket.SOCK_STREAM | socket.SOCK_NONBLOCK) as s: self.checkNonblock(s) s.setblocking(1) self.checkNonblock(s, False) s.setblocking(0) self.checkNonblock(s) s.settimeout(None) self.checkNonblock(s, False) s.settimeout(2.0) self.checkNonblock(s, timeout=2.0) s.setblocking(1) self.checkNonblock(s, False) # defaulttimeout t = socket.getdefaulttimeout() socket.setdefaulttimeout(0.0) with socket.socket() as s: self.checkNonblock(s) socket.setdefaulttimeout(None) with socket.socket() as s: self.checkNonblock(s, False) socket.setdefaulttimeout(2.0) with socket.socket() as s: self.checkNonblock(s, timeout=2.0) socket.setdefaulttimeout(None) with socket.socket() as s: self.checkNonblock(s, False) socket.setdefaulttimeout(t) @unittest.skipUnless(os.name == "nt", "Windows specific") @unittest.skipUnless(multiprocessing, "need multiprocessing") class TestSocketSharing(SocketTCPTest): # This must be classmethod and not staticmethod or multiprocessing # won't be able to bootstrap it. @classmethod def remoteProcessServer(cls, q): # Recreate socket from shared data sdata = q.get() message = q.get() s = socket.fromshare(sdata) s2, c = s.accept() # Send the message s2.sendall(message) s2.close() s.close() def testShare(self): # Transfer the listening server socket to another process # and service it from there. # Create process: q = multiprocessing.Queue() p = multiprocessing.Process(target=self.remoteProcessServer, args=(q,)) p.start() # Get the shared socket data data = self.serv.share(p.pid) # Pass the shared socket to the other process addr = self.serv.getsockname() self.serv.close() q.put(data) # The data that the server will send us message = b"slapmahfro" q.put(message) # Connect s = socket.create_connection(addr) # listen for the data m = [] while True: data = s.recv(100) if not data: break m.append(data) s.close() received = b"".join(m) self.assertEqual(received, message) p.join() def testShareLength(self): data = self.serv.share(os.getpid()) self.assertRaises(ValueError, socket.fromshare, data[:-1]) self.assertRaises(ValueError, socket.fromshare, data+b"foo") def compareSockets(self, org, other): # socket sharing is expected to work only for blocking socket # since the internal python timout value isn't transfered. self.assertEqual(org.gettimeout(), None) self.assertEqual(org.gettimeout(), other.gettimeout()) self.assertEqual(org.family, other.family) self.assertEqual(org.type, other.type) # If the user specified "0" for proto, then # internally windows will have picked the correct value. # Python introspection on the socket however will still return # 0. For the shared socket, the python value is recreated # from the actual value, so it may not compare correctly. if org.proto != 0: self.assertEqual(org.proto, other.proto) def testShareLocal(self): data = self.serv.share(os.getpid()) s = socket.fromshare(data) try: self.compareSockets(self.serv, s) finally: s.close() def testTypes(self): families = [socket.AF_INET, socket.AF_INET6] types = [socket.SOCK_STREAM, socket.SOCK_DGRAM] for f in families: for t in types: try: source = socket.socket(f, t) except OSError: continue # This combination is not supported try: data = source.share(os.getpid()) shared = socket.fromshare(data) try: self.compareSockets(source, shared) finally: shared.close() finally: source.close() def test_main(): tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest, TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, UDPTimeoutTest ] tests.extend([ NonBlockingTCPTests, FileObjectClassTestCase, FileObjectInterruptedTestCase, UnbufferedFileObjectClassTestCase, LineBufferedFileObjectClassTestCase, SmallBufferedFileObjectClassTestCase, UnicodeReadFileObjectClassTestCase, UnicodeWriteFileObjectClassTestCase, UnicodeReadWriteFileObjectClassTestCase, NetworkConnectionNoServer, NetworkConnectionAttributesTest, NetworkConnectionBehaviourTest, ContextManagersTest, InheritanceTest, NonblockConstantTest ]) tests.append(BasicSocketPairTest) tests.append(TestUnixDomain) tests.append(TestLinuxAbstractNamespace) tests.extend([TIPCTest, TIPCThreadableTest]) tests.extend([BasicCANTest, CANTest]) tests.extend([BasicRDSTest, RDSTest]) tests.extend([ CmsgMacroTests, SendmsgUDPTest, RecvmsgUDPTest, RecvmsgIntoUDPTest, SendmsgUDP6Test, RecvmsgUDP6Test, RecvmsgRFC3542AncillaryUDP6Test, RecvmsgIntoRFC3542AncillaryUDP6Test, RecvmsgIntoUDP6Test, SendmsgTCPTest, RecvmsgTCPTest, RecvmsgIntoTCPTest, SendmsgSCTPStreamTest, RecvmsgSCTPStreamTest, RecvmsgIntoSCTPStreamTest, SendmsgUnixStreamTest, RecvmsgUnixStreamTest, RecvmsgIntoUnixStreamTest, RecvmsgSCMRightsStreamTest, RecvmsgIntoSCMRightsStreamTest, # These are slow when setitimer() is not available InterruptedRecvTimeoutTest, InterruptedSendTimeoutTest, TestSocketSharing, ]) thread_info = support.threading_setup() support.run_unittest(*tests) support.threading_cleanup(*thread_info) if __name__ == "__main__": test_main() >>>>>>> b875702c9c06ab5012e52ff4337439b03918f453
mit
choderalab/openmoltools
examples/test_example.py
7
1421
import simtk.unit as u from simtk.openmm import app import simtk.openmm as mm from openmoltools import gafftools, system_checker ligand_name = "sustiva" ligand_path = "./chemicals/%s/" % ligand_name temperature = 300 * u.kelvin friction = 0.3 / u.picosecond timestep = 0.1 * u.femtosecond prmtop = app.AmberPrmtopFile("%s/%s.prmtop" % (ligand_path, ligand_name)) inpcrt = app.AmberInpcrdFile("%s/%s.inpcrd" % (ligand_path, ligand_name)) system_prm = prmtop.createSystem(nonbondedMethod=app.NoCutoff, nonbondedCutoff=1.0*u.nanometers, constraints=None) mol2 = gafftools.Mol2Parser("%s/%s.mol2" % (ligand_path, ligand_name)) top, xyz = mol2.to_openmm() forcefield = app.ForceField("%s/%s.xml" % (ligand_path, ligand_name)) system_xml = forcefield.createSystem(top, nonbondedMethod=app.NoCutoff, nonbondedCutoff=1.0*u.nanometers, constraints=None) integrator_xml = mm.LangevinIntegrator(temperature, friction, timestep) simulation_xml = app.Simulation(top, system_xml, integrator_xml) simulation_xml.context.setPositions(xyz) integrator_prm = mm.LangevinIntegrator(temperature, friction, timestep) simulation_prm = app.Simulation(prmtop.topology, system_prm, integrator_prm) simulation_prm.context.setPositions(xyz) checker = system_checker.SystemChecker(simulation_xml, simulation_prm) checker.check_force_parameters() energy0, energy1 = checker.check_energies() abs((energy0 - energy1) / u.kilojoules_per_mole)
mit
laissezfarrell/rl-bitcurator-scripts
python/accession-reporter.py
1
3754
#!/usr/bin/env python3 #Script (in progress) to report high-level folder information for offices transferring records to the University Archives. #Dependencies: argparse, pathlib, python3.6 or above, csv, datetime #Assumptions: # 1. import argparse, csv, datetime from pathlib import Path, PurePath from datetime import datetime def walkPath(): startingPath = Path(inputDir) #uncomment when ready for arguments #csvOut = Path('/home/bcadmin/Desktop/accession-report-test.csv') #startingPath = Path('/home/bcadmin/Desktop/test-data/objects') #comment when ready for arguments spChild = [x for x in startingPath.iterdir() if x.is_dir()] #create a list of the children directories in startingPath. with open (csvOut, 'w') as m: writer = csv.writer(m) writer.writerow(['path','foldersize '+ labelUnits,'Earliest Timestamp','Latest Timestamp']) for i in spChild: operatingDirectory = Path(i) print("the next directory to process is ",operatingDirectory)#sanity check fileList = list(operatingDirectory.glob('**/*')) #fileList = [x for x in operatingDirectory.iterdir() if x.is_file()] #print(fileList) #sanity check folderSize = 0 fModTime = datetime.now() oldestTime = fModTime newestTime = datetime.strptime("Jan 01 1950", "%b %d %Y") for f in fileList: fSizeBytes = (Path.stat(f).st_size / repUnits) folderSize = folderSize + fSizeBytes fModTime = datetime.fromtimestamp(Path.stat(f).st_mtime) if fModTime >= oldestTime: pass else: oldestTime = fModTime if fModTime <= newestTime: pass else: newestTime = fModTime #print(folderSize) #print(oldestTime) #print(newestTime) writer.writerow([i,folderSize,oldestTime,newestTime]) #end of day May 15: above function calculates the size of the files in a folder, as well as the most recent and oldest date modified. Next steps: 1) add arguments back in and test function. Mostly compied/pasted writer stuff from another script, so potentially doesn't work yet. # Main body to accept arguments and call the three functions. parser = argparse.ArgumentParser() parser.add_argument("output", help="Path to and filename for the CSV to create.") parser.add_argument("input", help="Path to input directory.") parser.add_argument("-u", "--units", type=str, choices=["b", "kb", "mb", "gb"], help="Unit of measurement for reporting aggregate size") args = parser.parse_args() if args.output: csvOut = args.output if args.input: inputDir = args.input if args.units == "kb": repUnits = 1024 labelUnits = "(kilobytes)" print("Reporting sizes in kilobytes") elif args.units == "mb": repUnits = 1024*1024 labelUnits = "(megabytes)" print("Reporting sizes in megabytes") elif args.units =="gb": repUnits = 1024*1024*1024 labelUnits = "(gigabytes)" print("Reporting sizes in gigabytes") elif args.units == "b": repUnits = 1 labelUnits = "(bytes)" print("Reporting sizes in bytes, the purest way to report sizes.") else: repUnits = 1 labelUnits = "(bytes)" print("Your inattentiveness leads Self to default to reporting size in bytes, the purest yet least human readable way to report sizes. Ha ha, puny human. Bow before Self, the mighty computer. 01000100 01000101 01010011 01010100 01010010 01001111 01011001 00100000 01000001 01001100 01001100 00100000 01001000 01010101 01001101 01000001 01001110 01010011 00101110") walkPath()
gpl-3.0
Azure/azure-sdk-for-python
sdk/network/azure-mgmt-network/azure/mgmt/network/v2018_01_01/operations/_subnets_operations.py
1
21762
# coding=utf-8 # -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # Code generated by Microsoft (R) AutoRest Code Generator. # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- from typing import TYPE_CHECKING import warnings from azure.core.exceptions import ClientAuthenticationError, HttpResponseError, ResourceExistsError, ResourceNotFoundError, map_error from azure.core.paging import ItemPaged from azure.core.pipeline import PipelineResponse from azure.core.pipeline.transport import HttpRequest, HttpResponse from azure.core.polling import LROPoller, NoPolling, PollingMethod from azure.mgmt.core.exceptions import ARMErrorFormat from azure.mgmt.core.polling.arm_polling import ARMPolling from .. import models as _models if TYPE_CHECKING: # pylint: disable=unused-import,ungrouped-imports from typing import Any, Callable, Dict, Generic, Iterable, Optional, TypeVar, Union T = TypeVar('T') ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]] class SubnetsOperations(object): """SubnetsOperations operations. You should not instantiate this class directly. Instead, you should create a Client instance that instantiates it for you and attaches it as an attribute. :ivar models: Alias to model classes used in this operation group. :type models: ~azure.mgmt.network.v2018_01_01.models :param client: Client for service requests. :param config: Configuration of service client. :param serializer: An object model serializer. :param deserializer: An object model deserializer. """ models = _models def __init__(self, client, config, serializer, deserializer): self._client = client self._serialize = serializer self._deserialize = deserializer self._config = config def _delete_initial( self, resource_group_name, # type: str virtual_network_name, # type: str subnet_name, # type: str **kwargs # type: Any ): # type: (...) -> None cls = kwargs.pop('cls', None) # type: ClsType[None] error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) api_version = "2018-01-01" # Construct URL url = self._delete_initial.metadata['url'] # type: ignore path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'virtualNetworkName': self._serialize.url("virtual_network_name", virtual_network_name, 'str'), 'subnetName': self._serialize.url("subnet_name", subnet_name, 'str'), 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} # type: Dict[str, Any] query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') # Construct headers header_parameters = {} # type: Dict[str, Any] request = self._client.delete(url, query_parameters, header_parameters) pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200, 202, 204]: map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response, error_format=ARMErrorFormat) if cls: return cls(pipeline_response, None, {}) _delete_initial.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}/subnets/{subnetName}'} # type: ignore def begin_delete( self, resource_group_name, # type: str virtual_network_name, # type: str subnet_name, # type: str **kwargs # type: Any ): # type: (...) -> LROPoller[None] """Deletes the specified subnet. :param resource_group_name: The name of the resource group. :type resource_group_name: str :param virtual_network_name: The name of the virtual network. :type virtual_network_name: str :param subnet_name: The name of the subnet. :type subnet_name: str :keyword callable cls: A custom type or function that will be passed the direct response :keyword str continuation_token: A continuation token to restart a poller from a saved state. :keyword polling: Pass in True if you'd like the ARMPolling polling method, False for no polling, or your own initialized polling object for a personal polling strategy. :paramtype polling: bool or ~azure.core.polling.PollingMethod :keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present. :return: An instance of LROPoller that returns either None or the result of cls(response) :rtype: ~azure.core.polling.LROPoller[None] :raises ~azure.core.exceptions.HttpResponseError: """ polling = kwargs.pop('polling', True) # type: Union[bool, PollingMethod] cls = kwargs.pop('cls', None) # type: ClsType[None] lro_delay = kwargs.pop( 'polling_interval', self._config.polling_interval ) cont_token = kwargs.pop('continuation_token', None) # type: Optional[str] if cont_token is None: raw_result = self._delete_initial( resource_group_name=resource_group_name, virtual_network_name=virtual_network_name, subnet_name=subnet_name, cls=lambda x,y,z: x, **kwargs ) kwargs.pop('error_map', None) kwargs.pop('content_type', None) def get_long_running_output(pipeline_response): if cls: return cls(pipeline_response, None, {}) path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'virtualNetworkName': self._serialize.url("virtual_network_name", virtual_network_name, 'str'), 'subnetName': self._serialize.url("subnet_name", subnet_name, 'str'), 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } if polling is True: polling_method = ARMPolling(lro_delay, path_format_arguments=path_format_arguments, **kwargs) elif polling is False: polling_method = NoPolling() else: polling_method = polling if cont_token: return LROPoller.from_continuation_token( polling_method=polling_method, continuation_token=cont_token, client=self._client, deserialization_callback=get_long_running_output ) else: return LROPoller(self._client, raw_result, get_long_running_output, polling_method) begin_delete.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}/subnets/{subnetName}'} # type: ignore def get( self, resource_group_name, # type: str virtual_network_name, # type: str subnet_name, # type: str expand=None, # type: Optional[str] **kwargs # type: Any ): # type: (...) -> "_models.Subnet" """Gets the specified subnet by virtual network and resource group. :param resource_group_name: The name of the resource group. :type resource_group_name: str :param virtual_network_name: The name of the virtual network. :type virtual_network_name: str :param subnet_name: The name of the subnet. :type subnet_name: str :param expand: Expands referenced resources. :type expand: str :keyword callable cls: A custom type or function that will be passed the direct response :return: Subnet, or the result of cls(response) :rtype: ~azure.mgmt.network.v2018_01_01.models.Subnet :raises: ~azure.core.exceptions.HttpResponseError """ cls = kwargs.pop('cls', None) # type: ClsType["_models.Subnet"] error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) api_version = "2018-01-01" accept = "application/json, text/json" # Construct URL url = self.get.metadata['url'] # type: ignore path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'virtualNetworkName': self._serialize.url("virtual_network_name", virtual_network_name, 'str'), 'subnetName': self._serialize.url("subnet_name", subnet_name, 'str'), 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} # type: Dict[str, Any] query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') if expand is not None: query_parameters['$expand'] = self._serialize.query("expand", expand, 'str') # Construct headers header_parameters = {} # type: Dict[str, Any] header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') request = self._client.get(url, query_parameters, header_parameters) pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200]: map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response, error_format=ARMErrorFormat) deserialized = self._deserialize('Subnet', pipeline_response) if cls: return cls(pipeline_response, deserialized, {}) return deserialized get.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}/subnets/{subnetName}'} # type: ignore def _create_or_update_initial( self, resource_group_name, # type: str virtual_network_name, # type: str subnet_name, # type: str subnet_parameters, # type: "_models.Subnet" **kwargs # type: Any ): # type: (...) -> "_models.Subnet" cls = kwargs.pop('cls', None) # type: ClsType["_models.Subnet"] error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) api_version = "2018-01-01" content_type = kwargs.pop("content_type", "application/json") accept = "application/json, text/json" # Construct URL url = self._create_or_update_initial.metadata['url'] # type: ignore path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'virtualNetworkName': self._serialize.url("virtual_network_name", virtual_network_name, 'str'), 'subnetName': self._serialize.url("subnet_name", subnet_name, 'str'), 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} # type: Dict[str, Any] query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') # Construct headers header_parameters = {} # type: Dict[str, Any] header_parameters['Content-Type'] = self._serialize.header("content_type", content_type, 'str') header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') body_content_kwargs = {} # type: Dict[str, Any] body_content = self._serialize.body(subnet_parameters, 'Subnet') body_content_kwargs['content'] = body_content request = self._client.put(url, query_parameters, header_parameters, **body_content_kwargs) pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200, 201]: map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response, error_format=ARMErrorFormat) if response.status_code == 200: deserialized = self._deserialize('Subnet', pipeline_response) if response.status_code == 201: deserialized = self._deserialize('Subnet', pipeline_response) if cls: return cls(pipeline_response, deserialized, {}) return deserialized _create_or_update_initial.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}/subnets/{subnetName}'} # type: ignore def begin_create_or_update( self, resource_group_name, # type: str virtual_network_name, # type: str subnet_name, # type: str subnet_parameters, # type: "_models.Subnet" **kwargs # type: Any ): # type: (...) -> LROPoller["_models.Subnet"] """Creates or updates a subnet in the specified virtual network. :param resource_group_name: The name of the resource group. :type resource_group_name: str :param virtual_network_name: The name of the virtual network. :type virtual_network_name: str :param subnet_name: The name of the subnet. :type subnet_name: str :param subnet_parameters: Parameters supplied to the create or update subnet operation. :type subnet_parameters: ~azure.mgmt.network.v2018_01_01.models.Subnet :keyword callable cls: A custom type or function that will be passed the direct response :keyword str continuation_token: A continuation token to restart a poller from a saved state. :keyword polling: Pass in True if you'd like the ARMPolling polling method, False for no polling, or your own initialized polling object for a personal polling strategy. :paramtype polling: bool or ~azure.core.polling.PollingMethod :keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present. :return: An instance of LROPoller that returns either Subnet or the result of cls(response) :rtype: ~azure.core.polling.LROPoller[~azure.mgmt.network.v2018_01_01.models.Subnet] :raises ~azure.core.exceptions.HttpResponseError: """ polling = kwargs.pop('polling', True) # type: Union[bool, PollingMethod] cls = kwargs.pop('cls', None) # type: ClsType["_models.Subnet"] lro_delay = kwargs.pop( 'polling_interval', self._config.polling_interval ) cont_token = kwargs.pop('continuation_token', None) # type: Optional[str] if cont_token is None: raw_result = self._create_or_update_initial( resource_group_name=resource_group_name, virtual_network_name=virtual_network_name, subnet_name=subnet_name, subnet_parameters=subnet_parameters, cls=lambda x,y,z: x, **kwargs ) kwargs.pop('error_map', None) kwargs.pop('content_type', None) def get_long_running_output(pipeline_response): deserialized = self._deserialize('Subnet', pipeline_response) if cls: return cls(pipeline_response, deserialized, {}) return deserialized path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'virtualNetworkName': self._serialize.url("virtual_network_name", virtual_network_name, 'str'), 'subnetName': self._serialize.url("subnet_name", subnet_name, 'str'), 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } if polling is True: polling_method = ARMPolling(lro_delay, path_format_arguments=path_format_arguments, **kwargs) elif polling is False: polling_method = NoPolling() else: polling_method = polling if cont_token: return LROPoller.from_continuation_token( polling_method=polling_method, continuation_token=cont_token, client=self._client, deserialization_callback=get_long_running_output ) else: return LROPoller(self._client, raw_result, get_long_running_output, polling_method) begin_create_or_update.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}/subnets/{subnetName}'} # type: ignore def list( self, resource_group_name, # type: str virtual_network_name, # type: str **kwargs # type: Any ): # type: (...) -> Iterable["_models.SubnetListResult"] """Gets all subnets in a virtual network. :param resource_group_name: The name of the resource group. :type resource_group_name: str :param virtual_network_name: The name of the virtual network. :type virtual_network_name: str :keyword callable cls: A custom type or function that will be passed the direct response :return: An iterator like instance of either SubnetListResult or the result of cls(response) :rtype: ~azure.core.paging.ItemPaged[~azure.mgmt.network.v2018_01_01.models.SubnetListResult] :raises: ~azure.core.exceptions.HttpResponseError """ cls = kwargs.pop('cls', None) # type: ClsType["_models.SubnetListResult"] error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) api_version = "2018-01-01" accept = "application/json, text/json" def prepare_request(next_link=None): # Construct headers header_parameters = {} # type: Dict[str, Any] header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') if not next_link: # Construct URL url = self.list.metadata['url'] # type: ignore path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'virtualNetworkName': self._serialize.url("virtual_network_name", virtual_network_name, 'str'), 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} # type: Dict[str, Any] query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') request = self._client.get(url, query_parameters, header_parameters) else: url = next_link query_parameters = {} # type: Dict[str, Any] request = self._client.get(url, query_parameters, header_parameters) return request def extract_data(pipeline_response): deserialized = self._deserialize('SubnetListResult', pipeline_response) list_of_elem = deserialized.value if cls: list_of_elem = cls(list_of_elem) return deserialized.next_link or None, iter(list_of_elem) def get_next(next_link=None): request = prepare_request(next_link) pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200]: map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response, error_format=ARMErrorFormat) return pipeline_response return ItemPaged( get_next, extract_data ) list.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}/subnets'} # type: ignore
mit
alirizakeles/zato
code/zato-server/src/zato/server/service/internal/security/openstack.py
1
6547
# -*- coding: utf-8 -*- """ Copyright (C) 2013 Dariusz Suchojad <dsuch at zato.io> Licensed under LGPLv3, see LICENSE.txt for terms and conditions. """ from __future__ import absolute_import, division, print_function, unicode_literals # stdlib from contextlib import closing from traceback import format_exc from uuid import uuid4 # Zato from zato.common import SEC_DEF_TYPE from zato.common.broker_message import SECURITY from zato.common.odb.model import Cluster, OpenStackSecurity from zato.common.odb.query import openstack_security_list from zato.server.service.internal import AdminService, AdminSIO, ChangePasswordBase, GetListAdminSIO class GetList(AdminService): """ Returns a list of OpenStack definitions available. """ _filter_by = OpenStackSecurity.name, class SimpleIO(GetListAdminSIO): request_elem = 'zato_security_openstack_get_list_request' response_elem = 'zato_security_openstack_get_list_response' input_required = ('cluster_id',) output_required = ('id', 'name', 'is_active', 'username') def get_data(self, session): return self._search(openstack_security_list, session, self.request.input.cluster_id, False) def handle(self): with closing(self.odb.session()) as session: self.response.payload[:] = self.get_data(session) class Create(AdminService): """ Creates a new OpenStack definition. """ class SimpleIO(AdminSIO): request_elem = 'zato_security_openstack_create_request' response_elem = 'zato_security_openstack_create_response' input_required = ('cluster_id', 'name', 'is_active', 'username') output_required = ('id', 'name') def handle(self): input = self.request.input input.password = uuid4().hex with closing(self.odb.session()) as session: try: cluster = session.query(Cluster).filter_by(id=input.cluster_id).first() # Let's see if we already have a definition of that name before committing # any stuff into the database. existing_one = session.query(OpenStackSecurity).\ filter(Cluster.id==input.cluster_id).\ filter(OpenStackSecurity.name==input.name).first() if existing_one: raise Exception('OpenStack definition [{0}] already exists on this cluster'.format(input.name)) auth = OpenStackSecurity(None, input.name, input.is_active, input.username, input.password, cluster) session.add(auth) session.commit() except Exception, e: msg = 'Could not create an OpenStack definition, e:[{e}]'.format(e=format_exc(e)) self.logger.error(msg) session.rollback() raise else: input.action = SECURITY.OPENSTACK_CREATE.value input.sec_type = SEC_DEF_TYPE.OPENSTACK self.broker_client.publish(input) self.response.payload.id = auth.id self.response.payload.name = auth.name class Edit(AdminService): """ Updates an OpenStack definition. """ class SimpleIO(AdminSIO): request_elem = 'zato_security_openstack_edit_request' response_elem = 'zato_security_openstack_edit_response' input_required = ('id', 'cluster_id', 'name', 'is_active', 'username') output_required = ('id', 'name') def handle(self): input = self.request.input with closing(self.odb.session()) as session: try: existing_one = session.query(OpenStackSecurity).\ filter(Cluster.id==input.cluster_id).\ filter(OpenStackSecurity.name==input.name).\ filter(OpenStackSecurity.id!=input.id).\ first() if existing_one: raise Exception('OpenStack definition [{0}] already exists on this cluster'.format(input.name)) definition = session.query(OpenStackSecurity).filter_by(id=input.id).one() old_name = definition.name definition.name = input.name definition.is_active = input.is_active definition.username = input.username session.add(definition) session.commit() except Exception, e: msg = 'Could not update the OpenStack definition, e:[{e}]'.format(e=format_exc(e)) self.logger.error(msg) session.rollback() raise else: input.action = SECURITY.OPENSTACK_EDIT.value input.old_name = old_name input.sec_type = SEC_DEF_TYPE.OPENSTACK self.broker_client.publish(input) self.response.payload.id = definition.id self.response.payload.name = definition.name class ChangePassword(ChangePasswordBase): """ Changes the password of an OpenStack definition. """ password_required = False class SimpleIO(ChangePasswordBase.SimpleIO): request_elem = 'zato_security_openstack_change_password_request' response_elem = 'zato_security_openstack_change_password_response' def handle(self): def _auth(instance, password): instance.password = password return self._handle(OpenStackSecurity, _auth, SECURITY.OPENSTACK_CHANGE_PASSWORD.value) class Delete(AdminService): """ Deletes an OpenStack definition. """ class SimpleIO(AdminSIO): request_elem = 'zato_security_openstack_delete_request' response_elem = 'zato_security_openstack_delete_response' input_required = ('id',) def handle(self): with closing(self.odb.session()) as session: try: auth = session.query(OpenStackSecurity).\ filter(OpenStackSecurity.id==self.request.input.id).\ one() session.delete(auth) session.commit() except Exception, e: msg = 'Could not delete the OpenStack definition, e:[{e}]'.format(e=format_exc(e)) self.logger.error(msg) session.rollback() raise else: self.request.input.action = SECURITY.OPENSTACK_DELETE.value self.request.input.name = auth.name self.broker_client.publish(self.request.input)
gpl-3.0
Juniper/contrail-dev-neutron
neutron/services/vpn/service_drivers/ipsec.py
8
5879
# vim: tabstop=10 shiftwidth=4 softtabstop=4 # # Copyright 2013, Nachi Ueno, NTT I3, Inc. # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import netaddr from neutron.common import rpc as n_rpc from neutron.openstack.common import log as logging from neutron.openstack.common import rpc from neutron.services.vpn.common import topics from neutron.services.vpn import service_drivers LOG = logging.getLogger(__name__) IPSEC = 'ipsec' BASE_IPSEC_VERSION = '1.0' class IPsecVpnDriverCallBack(object): """Callback for IPSecVpnDriver rpc.""" # history # 1.0 Initial version RPC_API_VERSION = BASE_IPSEC_VERSION def __init__(self, driver): self.driver = driver def create_rpc_dispatcher(self): return n_rpc.PluginRpcDispatcher([self]) def get_vpn_services_on_host(self, context, host=None): """Returns the vpnservices on the host.""" plugin = self.driver.service_plugin vpnservices = plugin._get_agent_hosting_vpn_services( context, host) return [self.driver._make_vpnservice_dict(vpnservice) for vpnservice in vpnservices] def update_status(self, context, status): """Update status of vpnservices.""" plugin = self.driver.service_plugin plugin.update_status_by_agent(context, status) class IPsecVpnAgentApi(service_drivers.BaseIPsecVpnAgentApi): """Agent RPC API for IPsecVPNAgent.""" RPC_API_VERSION = BASE_IPSEC_VERSION def __init__(self, topic, default_version): super(IPsecVpnAgentApi, self).__init__( topics.IPSEC_AGENT_TOPIC, topic, default_version) class IPsecVPNDriver(service_drivers.VpnDriver): """VPN Service Driver class for IPsec.""" def __init__(self, service_plugin): super(IPsecVPNDriver, self).__init__(service_plugin) self.callbacks = IPsecVpnDriverCallBack(self) self.conn = rpc.create_connection(new=True) self.conn.create_consumer( topics.IPSEC_DRIVER_TOPIC, self.callbacks.create_rpc_dispatcher(), fanout=False) self.conn.consume_in_thread() self.agent_rpc = IPsecVpnAgentApi( topics.IPSEC_AGENT_TOPIC, BASE_IPSEC_VERSION) @property def service_type(self): return IPSEC def create_ipsec_site_connection(self, context, ipsec_site_connection): vpnservice = self.service_plugin._get_vpnservice( context, ipsec_site_connection['vpnservice_id']) self.agent_rpc.vpnservice_updated(context, vpnservice['router_id']) def update_ipsec_site_connection( self, context, old_ipsec_site_connection, ipsec_site_connection): vpnservice = self.service_plugin._get_vpnservice( context, ipsec_site_connection['vpnservice_id']) self.agent_rpc.vpnservice_updated(context, vpnservice['router_id']) def delete_ipsec_site_connection(self, context, ipsec_site_connection): vpnservice = self.service_plugin._get_vpnservice( context, ipsec_site_connection['vpnservice_id']) self.agent_rpc.vpnservice_updated(context, vpnservice['router_id']) def create_ikepolicy(self, context, ikepolicy): pass def delete_ikepolicy(self, context, ikepolicy): pass def update_ikepolicy(self, context, old_ikepolicy, ikepolicy): pass def create_ipsecpolicy(self, context, ipsecpolicy): pass def delete_ipsecpolicy(self, context, ipsecpolicy): pass def update_ipsecpolicy(self, context, old_ipsec_policy, ipsecpolicy): pass def create_vpnservice(self, context, vpnservice): pass def update_vpnservice(self, context, old_vpnservice, vpnservice): self.agent_rpc.vpnservice_updated(context, vpnservice['router_id']) def delete_vpnservice(self, context, vpnservice): self.agent_rpc.vpnservice_updated(context, vpnservice['router_id']) def _make_vpnservice_dict(self, vpnservice): """Convert vpnservice information for vpn agent. also converting parameter name for vpn agent driver """ vpnservice_dict = dict(vpnservice) vpnservice_dict['ipsec_site_connections'] = [] vpnservice_dict['subnet'] = dict( vpnservice.subnet) vpnservice_dict['external_ip'] = vpnservice.router.gw_port[ 'fixed_ips'][0]['ip_address'] for ipsec_site_connection in vpnservice.ipsec_site_connections: ipsec_site_connection_dict = dict(ipsec_site_connection) try: netaddr.IPAddress(ipsec_site_connection['peer_id']) except netaddr.core.AddrFormatError: ipsec_site_connection['peer_id'] = ( '@' + ipsec_site_connection['peer_id']) ipsec_site_connection_dict['ikepolicy'] = dict( ipsec_site_connection.ikepolicy) ipsec_site_connection_dict['ipsecpolicy'] = dict( ipsec_site_connection.ipsecpolicy) vpnservice_dict['ipsec_site_connections'].append( ipsec_site_connection_dict) peer_cidrs = [ peer_cidr.cidr for peer_cidr in ipsec_site_connection.peer_cidrs] ipsec_site_connection_dict['peer_cidrs'] = peer_cidrs return vpnservice_dict
apache-2.0
10239847509238470925387z/tmp123
app.py
1
2195
#!/usr/bin/env python import urllib import json import os import constants import accounts from flask import Flask from flask import request from flask import make_response # Flask app should start in global layout app = Flask(__name__) PERSON = constants.TEST_1 @app.route('/webhook', methods=['POST']) def webhook(): req = request.get_json(silent=True, force=True) print("Request:") print(json.dumps(req, indent=4)) res = makeWebhookResult(req) res = json.dumps(res, indent=4) print(res) r = make_response(res) r.headers['Content-Type'] = 'application/json' return r def makeWebhookResult(req): if req.get("result").get("action") != "account-balance": return constants.ERR_DICT(req.get("result").get("action")) result = req.get("result") parameters = result.get("parameters") acct = parameters.get("account-type") acct = acct.strip() if acct=='401k': acct='WI' qual = parameters.get("qualifier") speech = str(req.get("result").get("action")) if acct: if acct in constants.ACCT_TYPES: speech = "The value of your {ACCT_TYPE} accounts is {VALU} dollars.".format(VALU=accounts.get_balance(PERSON, acct), ACCT_TYPE=acct) else: speech = "You don't have any accounts of that type. The total value of your other accounts is {VALU} dollars.".format( VALU=accounts.get_balance(PERSON)) elif qual: speech = "The total value of your accounts is {VALU} dollars.".format(VALU=accounts.get_balance(PERSON)) else: speech = "The total value of your accounts is {VALU} dollars.".format(VALU=accounts.get_balance(PERSON)) # speech = "The cost of shipping to " + zone + " is " + str(cost[zone]) + " euros." print("Response:") print(speech) speech += "\nAnything else I can help you with today?" return { "speech": speech, "displayText": speech, #"data": {}, # "contextOut": [], "source": "home" } if __name__ == '__main__': port = int(os.getenv('PORT', 5000)) print "Starting app on port %d" % port app.run(debug=True, port=port, host='0.0.0.0')
apache-2.0
rezvorck/android_kernel_s450m_4g_mm
tools/perf/scripts/python/sched-migration.py
1910
11965
#!/usr/bin/python # # Cpu task migration overview toy # # Copyright (C) 2010 Frederic Weisbecker <fweisbec@gmail.com> # # perf script event handlers have been generated by perf script -g python # # This software is distributed under the terms of the GNU General # Public License ("GPL") version 2 as published by the Free Software # Foundation. import os import sys from collections import defaultdict from UserList import UserList sys.path.append(os.environ['PERF_EXEC_PATH'] + \ '/scripts/python/Perf-Trace-Util/lib/Perf/Trace') sys.path.append('scripts/python/Perf-Trace-Util/lib/Perf/Trace') from perf_trace_context import * from Core import * from SchedGui import * threads = { 0 : "idle"} def thread_name(pid): return "%s:%d" % (threads[pid], pid) class RunqueueEventUnknown: @staticmethod def color(): return None def __repr__(self): return "unknown" class RunqueueEventSleep: @staticmethod def color(): return (0, 0, 0xff) def __init__(self, sleeper): self.sleeper = sleeper def __repr__(self): return "%s gone to sleep" % thread_name(self.sleeper) class RunqueueEventWakeup: @staticmethod def color(): return (0xff, 0xff, 0) def __init__(self, wakee): self.wakee = wakee def __repr__(self): return "%s woke up" % thread_name(self.wakee) class RunqueueEventFork: @staticmethod def color(): return (0, 0xff, 0) def __init__(self, child): self.child = child def __repr__(self): return "new forked task %s" % thread_name(self.child) class RunqueueMigrateIn: @staticmethod def color(): return (0, 0xf0, 0xff) def __init__(self, new): self.new = new def __repr__(self): return "task migrated in %s" % thread_name(self.new) class RunqueueMigrateOut: @staticmethod def color(): return (0xff, 0, 0xff) def __init__(self, old): self.old = old def __repr__(self): return "task migrated out %s" % thread_name(self.old) class RunqueueSnapshot: def __init__(self, tasks = [0], event = RunqueueEventUnknown()): self.tasks = tuple(tasks) self.event = event def sched_switch(self, prev, prev_state, next): event = RunqueueEventUnknown() if taskState(prev_state) == "R" and next in self.tasks \ and prev in self.tasks: return self if taskState(prev_state) != "R": event = RunqueueEventSleep(prev) next_tasks = list(self.tasks[:]) if prev in self.tasks: if taskState(prev_state) != "R": next_tasks.remove(prev) elif taskState(prev_state) == "R": next_tasks.append(prev) if next not in next_tasks: next_tasks.append(next) return RunqueueSnapshot(next_tasks, event) def migrate_out(self, old): if old not in self.tasks: return self next_tasks = [task for task in self.tasks if task != old] return RunqueueSnapshot(next_tasks, RunqueueMigrateOut(old)) def __migrate_in(self, new, event): if new in self.tasks: self.event = event return self next_tasks = self.tasks[:] + tuple([new]) return RunqueueSnapshot(next_tasks, event) def migrate_in(self, new): return self.__migrate_in(new, RunqueueMigrateIn(new)) def wake_up(self, new): return self.__migrate_in(new, RunqueueEventWakeup(new)) def wake_up_new(self, new): return self.__migrate_in(new, RunqueueEventFork(new)) def load(self): """ Provide the number of tasks on the runqueue. Don't count idle""" return len(self.tasks) - 1 def __repr__(self): ret = self.tasks.__repr__() ret += self.origin_tostring() return ret class TimeSlice: def __init__(self, start, prev): self.start = start self.prev = prev self.end = start # cpus that triggered the event self.event_cpus = [] if prev is not None: self.total_load = prev.total_load self.rqs = prev.rqs.copy() else: self.rqs = defaultdict(RunqueueSnapshot) self.total_load = 0 def __update_total_load(self, old_rq, new_rq): diff = new_rq.load() - old_rq.load() self.total_load += diff def sched_switch(self, ts_list, prev, prev_state, next, cpu): old_rq = self.prev.rqs[cpu] new_rq = old_rq.sched_switch(prev, prev_state, next) if old_rq is new_rq: return self.rqs[cpu] = new_rq self.__update_total_load(old_rq, new_rq) ts_list.append(self) self.event_cpus = [cpu] def migrate(self, ts_list, new, old_cpu, new_cpu): if old_cpu == new_cpu: return old_rq = self.prev.rqs[old_cpu] out_rq = old_rq.migrate_out(new) self.rqs[old_cpu] = out_rq self.__update_total_load(old_rq, out_rq) new_rq = self.prev.rqs[new_cpu] in_rq = new_rq.migrate_in(new) self.rqs[new_cpu] = in_rq self.__update_total_load(new_rq, in_rq) ts_list.append(self) if old_rq is not out_rq: self.event_cpus.append(old_cpu) self.event_cpus.append(new_cpu) def wake_up(self, ts_list, pid, cpu, fork): old_rq = self.prev.rqs[cpu] if fork: new_rq = old_rq.wake_up_new(pid) else: new_rq = old_rq.wake_up(pid) if new_rq is old_rq: return self.rqs[cpu] = new_rq self.__update_total_load(old_rq, new_rq) ts_list.append(self) self.event_cpus = [cpu] def next(self, t): self.end = t return TimeSlice(t, self) class TimeSliceList(UserList): def __init__(self, arg = []): self.data = arg def get_time_slice(self, ts): if len(self.data) == 0: slice = TimeSlice(ts, TimeSlice(-1, None)) else: slice = self.data[-1].next(ts) return slice def find_time_slice(self, ts): start = 0 end = len(self.data) found = -1 searching = True while searching: if start == end or start == end - 1: searching = False i = (end + start) / 2 if self.data[i].start <= ts and self.data[i].end >= ts: found = i end = i continue if self.data[i].end < ts: start = i elif self.data[i].start > ts: end = i return found def set_root_win(self, win): self.root_win = win def mouse_down(self, cpu, t): idx = self.find_time_slice(t) if idx == -1: return ts = self[idx] rq = ts.rqs[cpu] raw = "CPU: %d\n" % cpu raw += "Last event : %s\n" % rq.event.__repr__() raw += "Timestamp : %d.%06d\n" % (ts.start / (10 ** 9), (ts.start % (10 ** 9)) / 1000) raw += "Duration : %6d us\n" % ((ts.end - ts.start) / (10 ** 6)) raw += "Load = %d\n" % rq.load() for t in rq.tasks: raw += "%s \n" % thread_name(t) self.root_win.update_summary(raw) def update_rectangle_cpu(self, slice, cpu): rq = slice.rqs[cpu] if slice.total_load != 0: load_rate = rq.load() / float(slice.total_load) else: load_rate = 0 red_power = int(0xff - (0xff * load_rate)) color = (0xff, red_power, red_power) top_color = None if cpu in slice.event_cpus: top_color = rq.event.color() self.root_win.paint_rectangle_zone(cpu, color, top_color, slice.start, slice.end) def fill_zone(self, start, end): i = self.find_time_slice(start) if i == -1: return for i in xrange(i, len(self.data)): timeslice = self.data[i] if timeslice.start > end: return for cpu in timeslice.rqs: self.update_rectangle_cpu(timeslice, cpu) def interval(self): if len(self.data) == 0: return (0, 0) return (self.data[0].start, self.data[-1].end) def nr_rectangles(self): last_ts = self.data[-1] max_cpu = 0 for cpu in last_ts.rqs: if cpu > max_cpu: max_cpu = cpu return max_cpu class SchedEventProxy: def __init__(self): self.current_tsk = defaultdict(lambda : -1) self.timeslices = TimeSliceList() def sched_switch(self, headers, prev_comm, prev_pid, prev_prio, prev_state, next_comm, next_pid, next_prio): """ Ensure the task we sched out this cpu is really the one we logged. Otherwise we may have missed traces """ on_cpu_task = self.current_tsk[headers.cpu] if on_cpu_task != -1 and on_cpu_task != prev_pid: print "Sched switch event rejected ts: %s cpu: %d prev: %s(%d) next: %s(%d)" % \ (headers.ts_format(), headers.cpu, prev_comm, prev_pid, next_comm, next_pid) threads[prev_pid] = prev_comm threads[next_pid] = next_comm self.current_tsk[headers.cpu] = next_pid ts = self.timeslices.get_time_slice(headers.ts()) ts.sched_switch(self.timeslices, prev_pid, prev_state, next_pid, headers.cpu) def migrate(self, headers, pid, prio, orig_cpu, dest_cpu): ts = self.timeslices.get_time_slice(headers.ts()) ts.migrate(self.timeslices, pid, orig_cpu, dest_cpu) def wake_up(self, headers, comm, pid, success, target_cpu, fork): if success == 0: return ts = self.timeslices.get_time_slice(headers.ts()) ts.wake_up(self.timeslices, pid, target_cpu, fork) def trace_begin(): global parser parser = SchedEventProxy() def trace_end(): app = wx.App(False) timeslices = parser.timeslices frame = RootFrame(timeslices, "Migration") app.MainLoop() def sched__sched_stat_runtime(event_name, context, common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain, comm, pid, runtime, vruntime): pass def sched__sched_stat_iowait(event_name, context, common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain, comm, pid, delay): pass def sched__sched_stat_sleep(event_name, context, common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain, comm, pid, delay): pass def sched__sched_stat_wait(event_name, context, common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain, comm, pid, delay): pass def sched__sched_process_fork(event_name, context, common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain, parent_comm, parent_pid, child_comm, child_pid): pass def sched__sched_process_wait(event_name, context, common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain, comm, pid, prio): pass def sched__sched_process_exit(event_name, context, common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain, comm, pid, prio): pass def sched__sched_process_free(event_name, context, common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain, comm, pid, prio): pass def sched__sched_migrate_task(event_name, context, common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain, comm, pid, prio, orig_cpu, dest_cpu): headers = EventHeaders(common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain) parser.migrate(headers, pid, prio, orig_cpu, dest_cpu) def sched__sched_switch(event_name, context, common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain, prev_comm, prev_pid, prev_prio, prev_state, next_comm, next_pid, next_prio): headers = EventHeaders(common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain) parser.sched_switch(headers, prev_comm, prev_pid, prev_prio, prev_state, next_comm, next_pid, next_prio) def sched__sched_wakeup_new(event_name, context, common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain, comm, pid, prio, success, target_cpu): headers = EventHeaders(common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain) parser.wake_up(headers, comm, pid, success, target_cpu, 1) def sched__sched_wakeup(event_name, context, common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain, comm, pid, prio, success, target_cpu): headers = EventHeaders(common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain) parser.wake_up(headers, comm, pid, success, target_cpu, 0) def sched__sched_wait_task(event_name, context, common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain, comm, pid, prio): pass def sched__sched_kthread_stop_ret(event_name, context, common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain, ret): pass def sched__sched_kthread_stop(event_name, context, common_cpu, common_secs, common_nsecs, common_pid, common_comm, common_callchain, comm, pid): pass def trace_unhandled(event_name, context, event_fields_dict): pass
gpl-2.0
cbrewster/servo
tests/wpt/web-platform-tests/tools/third_party/py/testing/process/test_forkedfunc.py
55
4612
import pytest import py, sys, os pytestmark = py.test.mark.skipif("not hasattr(os, 'fork')") def test_waitfinish_removes_tempdir(): ff = py.process.ForkedFunc(boxf1) assert ff.tempdir.check() ff.waitfinish() assert not ff.tempdir.check() def test_tempdir_gets_gc_collected(monkeypatch): monkeypatch.setattr(os, 'fork', lambda: os.getpid()) ff = py.process.ForkedFunc(boxf1) assert ff.tempdir.check() ff.__del__() assert not ff.tempdir.check() def test_basic_forkedfunc(): result = py.process.ForkedFunc(boxf1).waitfinish() assert result.out == "some out\n" assert result.err == "some err\n" assert result.exitstatus == 0 assert result.signal == 0 assert result.retval == 1 def test_exitstatus(): def func(): os._exit(4) result = py.process.ForkedFunc(func).waitfinish() assert result.exitstatus == 4 assert result.signal == 0 assert not result.out assert not result.err def test_execption_in_func(): def fun(): raise ValueError(42) ff = py.process.ForkedFunc(fun) result = ff.waitfinish() assert result.exitstatus == ff.EXITSTATUS_EXCEPTION assert result.err.find("ValueError: 42") != -1 assert result.signal == 0 assert not result.retval def test_forkedfunc_on_fds(): result = py.process.ForkedFunc(boxf2).waitfinish() assert result.out == "someout" assert result.err == "someerr" assert result.exitstatus == 0 assert result.signal == 0 assert result.retval == 2 def test_forkedfunc_on_fds_output(): result = py.process.ForkedFunc(boxf3).waitfinish() assert result.signal == 11 assert result.out == "s" def test_forkedfunc_on_stdout(): def boxf3(): import sys sys.stdout.write("hello\n") os.kill(os.getpid(), 11) result = py.process.ForkedFunc(boxf3).waitfinish() assert result.signal == 11 assert result.out == "hello\n" def test_forkedfunc_signal(): result = py.process.ForkedFunc(boxseg).waitfinish() assert result.retval is None assert result.signal == 11 def test_forkedfunc_huge_data(): result = py.process.ForkedFunc(boxhuge).waitfinish() assert result.out assert result.exitstatus == 0 assert result.signal == 0 assert result.retval == 3 def test_box_seq(): # we run many boxes with huge data, just one after another for i in range(50): result = py.process.ForkedFunc(boxhuge).waitfinish() assert result.out assert result.exitstatus == 0 assert result.signal == 0 assert result.retval == 3 def test_box_in_a_box(): def boxfun(): result = py.process.ForkedFunc(boxf2).waitfinish() print (result.out) sys.stderr.write(result.err + "\n") return result.retval result = py.process.ForkedFunc(boxfun).waitfinish() assert result.out == "someout\n" assert result.err == "someerr\n" assert result.exitstatus == 0 assert result.signal == 0 assert result.retval == 2 def test_kill_func_forked(): class A: pass info = A() import time def box_fun(): time.sleep(10) # we don't want to last forever here ff = py.process.ForkedFunc(box_fun) os.kill(ff.pid, 15) result = ff.waitfinish() assert result.signal == 15 def test_hooks(monkeypatch): def _boxed(): return 1 def _on_start(): sys.stdout.write("some out\n") sys.stdout.flush() def _on_exit(): sys.stderr.write("some err\n") sys.stderr.flush() result = py.process.ForkedFunc(_boxed, child_on_start=_on_start, child_on_exit=_on_exit).waitfinish() assert result.out == "some out\n" assert result.err == "some err\n" assert result.exitstatus == 0 assert result.signal == 0 assert result.retval == 1 # ====================================================================== # examples # ====================================================================== # def boxf1(): sys.stdout.write("some out\n") sys.stderr.write("some err\n") return 1 def boxf2(): os.write(1, "someout".encode('ascii')) os.write(2, "someerr".encode('ascii')) return 2 def boxf3(): os.write(1, "s".encode('ascii')) os.kill(os.getpid(), 11) def boxseg(): os.kill(os.getpid(), 11) def boxhuge(): s = " ".encode('ascii') os.write(1, s * 10000) os.write(2, s * 10000) os.write(1, s * 10000) os.write(1, s * 10000) os.write(2, s * 10000) os.write(2, s * 10000) os.write(1, s * 10000) return 3
mpl-2.0
ckuethe/gnuradio
gr-atsc/python/atsc/qa_atsc.py
55
7019
#!/usr/bin/env python # # Copyright 2004,2006,2007,2013 Free Software Foundation, Inc. # # This file is part of GNU Radio # # GNU Radio is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3, or (at your option) # any later version. # # GNU Radio is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with GNU Radio; see the file COPYING. If not, write to # the Free Software Foundation, Inc., 51 Franklin Street, # Boston, MA 02110-1301, USA. # import sys from gnuradio import gr, gr_unittest, atsc, blocks from gnuradio.atsc import atsc_utils class memoize(object): def __init__(self, thunk): self.thunk = thunk self.cached = False self.value = None def __call__(self): if self.cached: return self.value self.value = self.thunk() self.cached = True return self.value """ Make a fake transport stream that's big enough for our purposes. We generate 8 full fields. This is relatively expensive. It takes about 2 seconds to execute. """ make_transport_stream = \ memoize(lambda : tuple(atsc_utils.make_fake_transport_stream_packet(8 * atsc.ATSC_DSEGS_PER_FIELD))) def pad_transport_stream(src): """ An MPEG transport stream packet is 188 bytes long. Internally we use a packet that is 256 bytes long to help with buffer alignment. This function adds the appropriate trailing padding to convert each packet from 188 to 256 bytes. """ return atsc_utils.pad_stream(src, atsc.sizeof_atsc_mpeg_packet, atsc.sizeof_atsc_mpeg_packet_pad) def depad_transport_stream(src): """ An MPEG transport stream packet is 188 bytes long. Internally we use a packet that is 256 bytes long to help with buffer alignment. This function removes the trailing padding to convert each packet from 256 back to 188 bytes. """ return atsc_utils.depad_stream(src, atsc.sizeof_atsc_mpeg_packet, atsc.sizeof_atsc_mpeg_packet_pad) class vector_source_ts(gr.hier_block2): """ MPEG Transport stream source for testing. """ def __init__(self, ts): """ Pad tranport stream packets to 256 bytes and reformat appropriately. Args: ts: MPEG transport stream. (sequence of ints in [0,255]; len(ts) % 188 == 0) """ src = blocks.vector_source_b(pad_transport_stream(ts)) s2v = blocks.stream_to_vector(gr.sizeof_char, atsc.sizeof_atsc_mpeg_packet) gr.hier_block2.__init__(self, "vector_source_ts", gr.io_signature(0, 0, 0), s2v.output_signature()) self.connect(src, s2v, self) class vector_sink_ts(gr.hier_block2): """ MPEG Transport stream sink for testing. """ def __init__(self): """ """ v2s = blocks.vector_to_stream(gr.sizeof_char, atsc.sizeof_atsc_mpeg_packet) self.sink = blocks.vector_sink_b() gr.hier_block2.__init__(self, "vector_sink_ts", v2s.input_signature(), gr.io_signature(0, 0, 0)) self.connect(self, v2s, self.sink) def data(self): """ Extracts tranport stream from sink and returns it to python. Depads tranport stream packets from 256 back to 188 bytes. @rtype: tuple of ints in [0,255]; len(result) % 188 == 0 """ return tuple(depad_transport_stream(self.sink.data())) class qa_atsc(gr_unittest.TestCase): def setUp(self): self.tb = gr.top_block() def tearDown(self): self.tb = None # The tests are run in alphabetical order def test_loopback_000(self): """ Loopback randomizer to derandomizer """ src_data = make_transport_stream() expected_result = src_data src = vector_source_ts(src_data) rand = atsc.randomizer() derand = atsc.derandomizer() dst = vector_sink_ts() self.tb.connect(src, rand) self.tb.connect(rand, derand) self.tb.connect(derand, dst) self.tb.run() result_data = dst.data () self.assertEqual (expected_result, result_data) def est_loopback_001(self): """ Loopback randomizer/rs_encoder to rs_decoder/derandomizer """ src_data = make_transport_stream() expected_result = src_data src = vector_source_ts(src_data) rand = atsc.randomizer() rs_enc = atsc.rs_encoder() rs_dec = atsc.rs_decoder() derand = atsc.derandomizer() dst = vector_sink_ts() self.tb.connect(src, rand, rs_enc, rs_dec, derand, dst) self.tb.run () result_data = dst.data () self.assertEqual (expected_result, result_data) def est_loopback_002(self): """ Loopback randomizer/rs_encoder/interleaver to deinterleaver/rs_decoder/derandomizer """ src_data = make_transport_stream() interleaver_delay = 52 expected_result = src_data[0:len(src_data)-(interleaver_delay*atsc.ATSC_MPEG_PKT_LENGTH)] src = vector_source_ts(src_data) rand = atsc.randomizer() rs_enc = atsc.rs_encoder() inter = atsc.interleaver() deinter = atsc.deinterleaver() rs_dec = atsc.rs_decoder() derand = atsc.derandomizer() dst = vector_sink_ts() self.tb.connect(src, rand, rs_enc, inter, deinter, rs_dec, derand, dst) self.tb.run () result_data = dst.data () result_data = result_data[(interleaver_delay*atsc.ATSC_MPEG_PKT_LENGTH):len(result_data)] self.assertEqual (expected_result, result_data) def est_loopback_003(self): """ Loopback randomizer/rs_encoder/interleaver/trellis_encoder via ds_to_softds to viterbi_decoder/deinterleaver/rs_decoder/derandomizer """ src_data = make_transport_stream() interleaver_delay = 52 viterbi_delay = 12 expected_result = src_data[0:len(src_data)-((interleaver_delay+viterbi_delay)*atsc.ATSC_MPEG_PKT_LENGTH)] src = vector_source_ts(src_data) rand = atsc.randomizer() rs_enc = atsc.rs_encoder() inter = atsc.interleaver() trellis = atsc.trellis_encoder() softds = atsc.ds_to_softds() viterbi = atsc.viterbi_decoder() deinter = atsc.deinterleaver() rs_dec = atsc.rs_decoder() derand = atsc.derandomizer() dst = vector_sink_ts() self.tb.connect(src, rand, rs_enc, inter, trellis, softds, viterbi, deinter, rs_dec, derand, dst) self.tb.run () result_data = dst.data ()[((interleaver_delay+viterbi_delay)*atsc.ATSC_MPEG_PKT_LENGTH):len(dst.data())] self.assertEqual (expected_result, result_data) if __name__ == '__main__': gr_unittest.main()
gpl-3.0
the3dsean/cryboards
maintenance/language/zhtable/Makefile.py
37
14682
#!/usr/bin/env python # -*- coding: utf-8 -*- # @author Philip import tarfile as tf import zipfile as zf import os, re, shutil, sys, platform pyversion = platform.python_version() islinux = platform.system().lower() == 'linux' if pyversion[:3] in ['2.6', '2.7']: import urllib as urllib_request import codecs open = codecs.open _unichr = unichr if sys.maxunicode < 0x10000: def unichr(i): if i < 0x10000: return _unichr(i) else: return _unichr( 0xD7C0 + ( i>>10 ) ) + _unichr( 0xDC00 + ( i & 0x3FF ) ) elif pyversion[:2] == '3.': import urllib.request as urllib_request unichr = chr def unichr2( *args ): return [unichr( int( i.split('<')[0][2:], 16 ) ) for i in args] def unichr3( *args ): return [unichr( int( i[2:7], 16 ) ) for i in args if i[2:7]] # DEFINE UNIHAN_VER = '6.3.0' SF_MIRROR = 'dfn' SCIM_TABLES_VER = '0.5.13' SCIM_PINYIN_VER = '0.5.92' LIBTABE_VER = '0.2.3' # END OF DEFINE def download( url, dest ): if os.path.isfile( dest ): print( 'File %s is up to date.' % dest ) return global islinux if islinux: # we use wget instead urlretrieve under Linux, # because wget could display details like download progress os.system( 'wget %s -O %s' % ( url, dest ) ) else: print( 'Downloading from [%s] ...' % url ) urllib_request.urlretrieve( url, dest ) print( 'Download complete.\n' ) return def uncompress( fp, member, encoding = 'U8' ): name = member.rsplit( '/', 1 )[-1] print( 'Extracting %s ...' % name ) fp.extract( member ) shutil.move( member, name ) if '/' in member: shutil.rmtree( member.split( '/', 1 )[0] ) if pyversion[:1] in ['2']: fc = open( name, 'rb', encoding, 'ignore' ) else: fc = open( name, 'r', encoding = encoding, errors = 'ignore' ) return fc unzip = lambda path, member, encoding = 'U8': \ uncompress( zf.ZipFile( path ), member, encoding ) untargz = lambda path, member, encoding = 'U8': \ uncompress( tf.open( path, 'r:gz' ), member, encoding ) def parserCore( fp, pos, beginmark = None, endmark = None ): if beginmark and endmark: start = False else: start = True mlist = set() for line in fp: if beginmark and line.startswith( beginmark ): start = True continue elif endmark and line.startswith( endmark ): break if start and not line.startswith( '#' ): elems = line.split() if len( elems ) < 2: continue elif len( elems[0] ) > 1 and \ len( elems[pos] ) > 1: # words only mlist.add( elems[pos] ) return mlist def tablesParser( path, name ): """ Read file from scim-tables and parse it. """ global SCIM_TABLES_VER src = 'scim-tables-%s/tables/zh/%s' % ( SCIM_TABLES_VER, name ) fp = untargz( path, src, 'U8' ) return parserCore( fp, 1, 'BEGIN_TABLE', 'END_TABLE' ) ezbigParser = lambda path: tablesParser( path, 'EZ-Big.txt.in' ) wubiParser = lambda path: tablesParser( path, 'Wubi.txt.in' ) zrmParser = lambda path: tablesParser( path, 'Ziranma.txt.in' ) def phraseParser( path ): """ Read phrase_lib.txt and parse it. """ global SCIM_PINYIN_VER src = 'scim-pinyin-%s/data/phrase_lib.txt' % SCIM_PINYIN_VER dst = 'phrase_lib.txt' fp = untargz( path, src, 'U8' ) return parserCore( fp, 0 ) def tsiParser( path ): """ Read tsi.src and parse it. """ src = 'libtabe/tsi-src/tsi.src' dst = 'tsi.src' fp = untargz( path, src, 'big5hkscs' ) return parserCore( fp, 0 ) def unihanParser( path ): """ Read Unihan_Variants.txt and parse it. """ fp = unzip( path, 'Unihan_Variants.txt', 'U8' ) t2s = dict() s2t = dict() for line in fp: if line.startswith( '#' ): continue else: elems = line.split() if len( elems ) < 3: continue type = elems.pop( 1 ) elems = unichr2( *elems ) if type == 'kTraditionalVariant': s2t[elems[0]] = elems[1:] elif type == 'kSimplifiedVariant': t2s[elems[0]] = elems[1:] fp.close() return ( t2s, s2t ) def applyExcludes( mlist, path ): """ Apply exclude rules from path to mlist. """ if pyversion[:1] in ['2']: excludes = open( path, 'rb', 'U8' ).read().split() else: excludes = open( path, 'r', encoding = 'U8' ).read().split() excludes = [word.split( '#' )[0].strip() for word in excludes] excludes = '|'.join( excludes ) excptn = re.compile( '.*(?:%s).*' % excludes ) diff = [mword for mword in mlist if excptn.search( mword )] mlist.difference_update( diff ) return mlist def charManualTable( path ): fp = open( path, 'r', encoding = 'U8' ) for line in fp: elems = line.split( '#' )[0].split( '|' ) elems = unichr3( *elems ) if len( elems ) > 1: yield elems[0], elems[1:] def toManyRules( src_table ): tomany = set() if pyversion[:1] in ['2']: for ( f, t ) in src_table.iteritems(): for i in range( 1, len( t ) ): tomany.add( t[i] ) else: for ( f, t ) in src_table.items(): for i in range( 1, len( t ) ): tomany.add( t[i] ) return tomany def removeRules( path, table ): fp = open( path, 'r', encoding = 'U8' ) texc = list() for line in fp: elems = line.split( '=>' ) f = t = elems[0].strip() if len( elems ) == 2: t = elems[1].strip() f = f.strip('"').strip("'") t = t.strip('"').strip("'") if f: try: table.pop( f ) except: pass if t: texc.append( t ) texcptn = re.compile( '^(?:%s)$' % '|'.join( texc ) ) if pyversion[:1] in ['2']: for (tmp_f, tmp_t) in table.copy().iteritems(): if texcptn.match( tmp_t ): table.pop( tmp_f ) else: for (tmp_f, tmp_t) in table.copy().items(): if texcptn.match( tmp_t ): table.pop( tmp_f ) return table def customRules( path ): fp = open( path, 'r', encoding = 'U8' ) ret = dict() for line in fp: line = line.rstrip( '\r\n' ) if '#' in line: line = line.split( '#' )[0].rstrip() elems = line.split( '\t' ) if len( elems ) > 1: ret[elems[0]] = elems[1] return ret def dictToSortedList( src_table, pos ): return sorted( src_table.items(), key = lambda m: ( m[pos], m[1 - pos] ) ) def translate( text, conv_table ): i = 0 while i < len( text ): for j in range( len( text ) - i, 0, -1 ): f = text[i:][:j] t = conv_table.get( f ) if t: text = text[:i] + t + text[i:][j:] i += len(t) - 1 break i += 1 return text def manualWordsTable( path, conv_table, reconv_table ): fp = open( path, 'r', encoding = 'U8' ) reconv_table = {} wordlist = [line.split( '#' )[0].strip() for line in fp] wordlist = list( set( wordlist ) ) wordlist.sort( key = lambda w: ( len(w), w ), reverse = True ) while wordlist: word = wordlist.pop() new_word = translate( word, conv_table ) rcv_word = translate( word, reconv_table ) if word != rcv_word: reconv_table[word] = word reconv_table[new_word] = word return reconv_table def defaultWordsTable( src_wordlist, src_tomany, char_conv_table, char_reconv_table ): wordlist = list( src_wordlist ) wordlist.sort( key = lambda w: ( len(w), w ), reverse = True ) word_conv_table = {} word_reconv_table = {} conv_table = char_conv_table.copy() reconv_table = char_reconv_table.copy() tomanyptn = re.compile( '(?:%s)' % '|'.join( src_tomany ) ) while wordlist: conv_table.update( word_conv_table ) reconv_table.update( word_reconv_table ) word = wordlist.pop() new_word_len = word_len = len( word ) while new_word_len == word_len: add = False test_word = translate( word, reconv_table ) new_word = translate( word, conv_table ) if not reconv_table.get( new_word ) \ and ( test_word != word \ or ( tomanyptn.search( word ) \ and word != translate( new_word, reconv_table ) ) ): word_conv_table[word] = new_word word_reconv_table[new_word] = word try: word = wordlist.pop() except IndexError: break new_word_len = len(word) return word_reconv_table def PHPArray( table ): lines = ['\'%s\' => \'%s\',' % (f, t) for (f, t) in table if f and t] return '\n'.join(lines) def main(): #Get Unihan.zip: url = 'http://www.unicode.org/Public/%s/ucd/Unihan.zip' % UNIHAN_VER han_dest = 'Unihan-%s.zip' % UNIHAN_VER download( url, han_dest ) # Get scim-tables-$(SCIM_TABLES_VER).tar.gz: url = 'http://%s.dl.sourceforge.net/sourceforge/scim/scim-tables-%s.tar.gz' % ( SF_MIRROR, SCIM_TABLES_VER ) tbe_dest = 'scim-tables-%s.tar.gz' % SCIM_TABLES_VER download( url, tbe_dest ) # Get scim-pinyin-$(SCIM_PINYIN_VER).tar.gz: url = 'http://%s.dl.sourceforge.net/sourceforge/scim/scim-pinyin-%s.tar.gz' % ( SF_MIRROR, SCIM_PINYIN_VER ) pyn_dest = 'scim-pinyin-%s.tar.gz' % SCIM_PINYIN_VER download( url, pyn_dest ) # Get libtabe-$(LIBTABE_VER).tgz: url = 'http://%s.dl.sourceforge.net/sourceforge/libtabe/libtabe-%s.tgz' % ( SF_MIRROR, LIBTABE_VER ) lbt_dest = 'libtabe-%s.tgz' % LIBTABE_VER download( url, lbt_dest ) # Unihan.txt ( t2s_1tomany, s2t_1tomany ) = unihanParser( han_dest ) t2s_1tomany.update( charManualTable( 'symme_supp.manual' ) ) t2s_1tomany.update( charManualTable( 'trad2simp.manual' ) ) s2t_1tomany.update( ( t[0], [f] ) for ( f, t ) in charManualTable( 'symme_supp.manual' ) ) s2t_1tomany.update( charManualTable( 'simp2trad.manual' ) ) if pyversion[:1] in ['2']: t2s_1to1 = dict( [( f, t[0] ) for ( f, t ) in t2s_1tomany.iteritems()] ) s2t_1to1 = dict( [( f, t[0] ) for ( f, t ) in s2t_1tomany.iteritems()] ) else: t2s_1to1 = dict( [( f, t[0] ) for ( f, t ) in t2s_1tomany.items()] ) s2t_1to1 = dict( [( f, t[0] ) for ( f, t ) in s2t_1tomany.items()] ) s_tomany = toManyRules( t2s_1tomany ) t_tomany = toManyRules( s2t_1tomany ) # noconvert rules t2s_1to1 = removeRules( 'trad2simp_noconvert.manual', t2s_1to1 ) s2t_1to1 = removeRules( 'simp2trad_noconvert.manual', s2t_1to1 ) # the supper set for word to word conversion t2s_1to1_supp = t2s_1to1.copy() s2t_1to1_supp = s2t_1to1.copy() t2s_1to1_supp.update( customRules( 'trad2simp_supp_set.manual' ) ) s2t_1to1_supp.update( customRules( 'simp2trad_supp_set.manual' ) ) # word to word manual rules t2s_word2word_manual = manualWordsTable( 'simpphrases.manual', s2t_1to1_supp, t2s_1to1_supp ) t2s_word2word_manual.update( customRules( 'toSimp.manual' ) ) s2t_word2word_manual = manualWordsTable( 'tradphrases.manual', t2s_1to1_supp, s2t_1to1_supp ) s2t_word2word_manual.update( customRules( 'toTrad.manual' ) ) # word to word rules from input methods t_wordlist = set() s_wordlist = set() t_wordlist.update( ezbigParser( tbe_dest ), tsiParser( lbt_dest ) ) s_wordlist.update( wubiParser( tbe_dest ), zrmParser( tbe_dest ), phraseParser( pyn_dest ) ) # exclude s_wordlist = applyExcludes( s_wordlist, 'simpphrases_exclude.manual' ) t_wordlist = applyExcludes( t_wordlist, 'tradphrases_exclude.manual' ) s2t_supp = s2t_1to1_supp.copy() s2t_supp.update( s2t_word2word_manual ) t2s_supp = t2s_1to1_supp.copy() t2s_supp.update( t2s_word2word_manual ) # parse list to dict t2s_word2word = defaultWordsTable( s_wordlist, s_tomany, s2t_1to1_supp, t2s_supp ) t2s_word2word.update( t2s_word2word_manual ) s2t_word2word = defaultWordsTable( t_wordlist, t_tomany, t2s_1to1_supp, s2t_supp ) s2t_word2word.update( s2t_word2word_manual ) # Final tables # sorted list toHans if pyversion[:1] in ['2']: t2s_1to1 = dict( [( f, t ) for ( f, t ) in t2s_1to1.iteritems() if f != t] ) else: t2s_1to1 = dict( [( f, t ) for ( f, t ) in t2s_1to1.items() if f != t] ) toHans = dictToSortedList( t2s_1to1, 0 ) + dictToSortedList( t2s_word2word, 1 ) # sorted list toHant if pyversion[:1] in ['2']: s2t_1to1 = dict( [( f, t ) for ( f, t ) in s2t_1to1.iteritems() if f != t] ) else: s2t_1to1 = dict( [( f, t ) for ( f, t ) in s2t_1to1.items() if f != t] ) toHant = dictToSortedList( s2t_1to1, 0 ) + dictToSortedList( s2t_word2word, 1 ) # sorted list toCN toCN = dictToSortedList( customRules( 'toCN.manual' ), 1 ) # sorted list toHK toHK = dictToSortedList( customRules( 'toHK.manual' ), 1 ) # sorted list toTW toTW = dictToSortedList( customRules( 'toTW.manual' ), 1 ) # Get PHP Array php = '''<?php /** * Simplified / Traditional Chinese conversion tables * * Automatically generated using code and data in maintenance/language/zhtable/ * Do not modify directly! * * @file */ $zh2Hant = array(\n''' php += PHPArray( toHant ) \ + '\n);\n\n$zh2Hans = array(\n' \ + PHPArray( toHans ) \ + '\n);\n\n$zh2TW = array(\n' \ + PHPArray( toTW ) \ + '\n);\n\n$zh2HK = array(\n' \ + PHPArray( toHK ) \ + '\n);\n\n$zh2CN = array(\n' \ + PHPArray( toCN ) \ + '\n);\n' if pyversion[:1] in ['2']: f = open( os.path.join( '..', '..', '..', 'includes', 'ZhConversion.php' ), 'wb', encoding = 'utf8' ) else: f = open( os.path.join( '..', '..', '..', 'includes', 'ZhConversion.php' ), 'w', buffering = 4096, encoding = 'utf8' ) print ('Writing ZhConversion.php ... ') f.write( php ) f.close() # Remove temporary files print ('Deleting temporary files ... ') os.remove('EZ-Big.txt.in') os.remove('phrase_lib.txt') os.remove('tsi.src') os.remove('Unihan_Variants.txt') os.remove('Wubi.txt.in') os.remove('Ziranma.txt.in') if __name__ == '__main__': main()
gpl-2.0
Lind-Project/native_client
src/trusted/validator_arm/validation-report.py
1
3575
#!/usr/bin/python2 # # Copyright (c) 2012 The Native Client Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. # import sys import textwrap from subprocess import Popen, PIPE _OBJDUMP = 'arm-linux-gnueabi-objdump' def _objdump(binary, vaddr, ctx_before, ctx_after): args = [ _OBJDUMP, '-d', '-G', binary, '--start-address=0x%08X' % (vaddr - (4 * ctx_before)), '--stop-address=0x%08X' % (vaddr + 4 + (4 * ctx_after))] highlight = ctx_before lines = 0 for line in Popen(args, stdout=PIPE).stdout.read().split('\n'): if line.startswith(' '): if highlight == 0: print '--> ', line else: print ' ', line highlight -= 1 lines += 1 if not lines: print ' (not found)' def _problem_info(code): return { 'kProblemUnsafe': ['Instruction is unsafe', 0, 0], 'kProblemBranchSplitsPattern': ['The destination of this branch is ' 'part of an instruction sequence that must be executed in full, ' 'or is inline data', 0, 0], 'kProblemPatternCrossesBundle': ['This instruction is part of a ' 'sequence that must execute in full, but it spans a bundle edge ' '-- so an indirect branch may target it', 1, 1], 'kProblemBranchInvalidDest': ['This branch targets a location that is ' 'outside of the application\'s executable code, and is not a valid ' 'trampoline entry point', 0, 0], 'kProblemUnsafeLoadStore': ['This store instruction is not preceded by ' 'a valid address mask instruction', 1, 0], 'kProblemUnsafeBranch': ['This indirect branch instruction is not ' 'preceded by a valid address mask instruction', 1, 0], 'kProblemUnsafeDataWrite': ['This instruction affects a register that ' 'must contain a valid data-region address, but is not followed by ' 'a valid address mask instruction', 0, 1], 'kProblemReadOnlyRegister': ['This instruction changes the contents of ' 'a read-only register', 0, 0], 'kProblemMisalignedCall': ['This linking branch instruction is not in ' 'the last slot of its bundle, so when its LR result is masked, the ' 'caller will not return to the next instruction', 0, 0], }[code] def _safety_msg(val): return { 0: 'UNKNOWN', # Should not appear 1: 'is undefined', 2: 'has unpredictable effects', 3: 'is deprecated', 4: 'is forbidden', 5: 'uses forbidden operands', }[val] def _explain_problem(binary, vaddr, safety, code, ref_vaddr): msg, ctx_before, ctx_after = _problem_info(code) if safety == 6: msg = "At %08X: %s:" % (vaddr, msg) else: msg = ("At %08X: %s (%s):" % (vaddr, msg, _safety_msg(safety))) print '\n'.join(textwrap.wrap(msg, 70, subsequent_indent=' ')) _objdump(binary, vaddr, ctx_before, ctx_after) if ref_vaddr: print "Destination address %08X:" % ref_vaddr _objdump(binary, ref_vaddr, 1, 1) def _parse_report(line): vaddr_hex, safety, code, ref_vaddr_hex = line.split() return (int(vaddr_hex, 16), int(safety), code, int(ref_vaddr_hex, 16)) for line in sys.stdin: if line.startswith('ncval: '): line = line[7:].strip() _explain_problem(sys.argv[1], *_parse_report(line))
bsd-3-clause
core-code/LibVT
Dependencies/Core3D/Preprocessing/generateOctreeFromObj.py
1
10849
#!/usr/bin/env python # # generateOctreeFromObj.py # Core3D # # Created by Julian Mayer on 16.11.07. # Copyright (c) 2010 A. Julian Mayer # # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitationthe rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import sys, bz2 from struct import * from vecmath import * TEXTURING = 1 GENTEX = 0 MAX_FLOAT = 1e+308 MIN_FLOAT = -1e308 MAX_USHORT = 0xFFFF MAX_FACES_PER_TREELEAF = 1000 MAX_RECURSION_DEPTH = 10 SCALE = 1.0 vertices = [] faces = [] normals = [] texcoords = [] def faceContent(f, i): if i == 0: if f.count("/") == 0: return f else: return f[:f.find("/")] elif i == 1: if f.count("/") == 0 or f.count("//") == 1: return 0 else: if f.count("/") == 2: return f[f.find("/")+1:f.rfind("/")] else: return f[f.find("/")+1:] else: if f.count("/") != 2: return 0 else: return f[f.rfind("/")+1:] def calculateAABB(faces): mi = [MAX_FLOAT, MAX_FLOAT,MAX_FLOAT] ma = [MIN_FLOAT, MIN_FLOAT, MIN_FLOAT] for face in faces: for i in range(3): for v in range(3): ma[i] = max(ma[i], vertices[face[v]][i]) mi[i] = min(mi[i], vertices[face[v]][i]) return mi,ma def classifyVertex(vertex, splitpoint): #TODO: do splitting or other funny things if vertex[0] > splitpoint[0] and vertex[1] > splitpoint[1] and vertex[2] > splitpoint[2]: return 0 if vertex[0] <= splitpoint[0] and vertex[1] > splitpoint[1] and vertex[2] > splitpoint[2]: return 1 if vertex[0] > splitpoint[0] and vertex[1] > splitpoint[1] and vertex[2] <= splitpoint[2]: return 2 if vertex[0] > splitpoint[0] and vertex[1] <= splitpoint[1] and vertex[2] > splitpoint[2]: return 3 if vertex[0] <= splitpoint[0] and vertex[1] > splitpoint[1] and vertex[2] <= splitpoint[2]: return 4 if vertex[0] > splitpoint[0] and vertex[1] <= splitpoint[1] and vertex[2] <= splitpoint[2]: return 5 if vertex[0] <= splitpoint[0] and vertex[1] <= splitpoint[1] and vertex[2] > splitpoint[2]: return 6 if vertex[0] <= splitpoint[0] and vertex[1] <= splitpoint[1] and vertex[2] <= splitpoint[2]:return 7 def classifyFace(face, splitpoint): return max(classifyVertex(vertices[face[0]], splitpoint), classifyVertex(vertices[face[1]], splitpoint), classifyVertex(vertices[face[2]], splitpoint)) #TODO: random instead of max? def buildOctree(faces, offset, level): mi,ma = calculateAABB(faces) ournum = buildOctree.counter buildOctree.counter += 1 childoffset = offset if len(faces) > MAX_FACES_PER_TREELEAF and level < MAX_RECURSION_DEPTH: splitpoint = [mi[0] + (ma[0] - mi[0])/2, mi[1] + (ma[1] - mi[1])/2, mi[2] + (ma[2] - mi[2])/2] newfaces = [[],[],[],[],[],[],[],[]] newnodes = [] childnums = [] for face in faces: x = classifyFace(face, splitpoint) newfaces[x].append(face) for newface in newfaces: a,b = buildOctree(newface, childoffset, level+1) childoffset += len(newface) childnums.append(a) newnodes.extend(b) faces[:] = newfaces[0]+newfaces[1]+newfaces[2]+newfaces[3]+newfaces[4]+newfaces[5]+newfaces[6]+newfaces[7] newnodes.insert(0, [offset, len(faces), mi[0], mi[1], mi[2], ma[0] - mi[0], ma[1] - mi[1], ma[2] - mi[2], childnums[0], childnums[1], childnums[2], childnums[3], childnums[4], childnums[5], childnums[6], childnums[7]]) return ournum, newnodes else: return ournum, [[offset, len(faces), mi[0], mi[1], mi[2], ma[0] - mi[0], ma[1] - mi[1], ma[2] - mi[2], MAX_USHORT, MAX_USHORT, MAX_USHORT, MAX_USHORT, MAX_USHORT, MAX_USHORT, MAX_USHORT, MAX_USHORT]] try: if (len(sys.argv)) == 1: raise Exception('input', 'error') f = open(sys.argv[len(sys.argv) - 1], 'r') of = 0 for i in range(1, len(sys.argv) - 1): if sys.argv[i].startswith("-s="): SCALE = float(sys.argv[i][3:]) elif sys.argv[i].startswith("-t"): TEXTURING = 0 elif sys.argv[i].startswith("-g="): GENTEX = int(sys.argv[i][3:]); TEXTURING = 0 elif sys.argv[i].startswith("-m="): MAX_FACES_PER_TREELEAF = int(sys.argv[i][3:]) elif sys.argv[i].startswith("-r="): MAX_RECURSION_DEPTH = int(sys.argv[i][3:]) elif sys.argv[i].startswith("-o="): of = open(sys.argv[i][3:], 'w') else: raise Exception('input', 'error') if of == 0: of = open(sys.argv[len(sys.argv) - 1][:sys.argv[len(sys.argv) - 1].rfind(".")] + ".octree.bz2", 'w') except: print """Usage: generateOctreeFromObj [options] obj_file Options: -t Ignore texture coordinates, produce an untextured Octree -s=<scale> Scale all coordinates by <scale> -m=<max_faces> Limit faces per leafnode to <max_faces> (Default: 1000) -r=<max_recursion> Limit tree depth to <max_recursion> (Default: 10) -o=<octree_file> Place the output octree into <octree_file> -g=<0,1,2,3,4> Texture coordinate generation: 0 = off, 1 = on, 2 = swap X, 3 = swap Y, 4 = swap XY""" sys.exit() print "Info: Reading the OBJ-file" lines = f.readlines() for line in lines: i = line.strip().split(" ")[0] c = line[2:].strip().split(" ") if i == "v": vertices.append([float(c[0]) * SCALE, float(c[1]) * SCALE, float(c[2]) * SCALE]) elif i == "vn": normals.append(normalize([float(c[0]), float(c[1]), float(c[2])])) elif i == "vt": texcoords.append([float(c[0]), float(c[1]), 0.0]) #TODO: if we discard W anyway we shouldnt store it elif i == "f": if (len(c) > 4): print "Error: faces with more than 4 edges not supported" sys.exit() elif (len(c) == 4): #triangulate faces.append([int(faceContent(c[0],0))-1, int(faceContent(c[1],0))-1, int(faceContent(c[2],0))-1, int(faceContent(c[0],2))-1, int(faceContent(c[1],2))-1, int(faceContent(c[2],2))-1, int(faceContent(c[0],1))-1, int(faceContent(c[1],1))-1, int(faceContent(c[2],1))-1]) faces.append([int(faceContent(c[0],0))-1, int(faceContent(c[2],0))-1, int(faceContent(c[3],0))-1, int(faceContent(c[0],2))-1, int(faceContent(c[2],2))-1, int(faceContent(c[3],2))-1, int(faceContent(c[0],1))-1, int(faceContent(c[2],1))-1, int(faceContent(c[3],1))-1]) else: faces.append([int(faceContent(c[0],0))-1, int(faceContent(c[1],0))-1, int(faceContent(c[2],0))-1, int(faceContent(c[0],2))-1, int(faceContent(c[1],2))-1, int(faceContent(c[2],2))-1, int(faceContent(c[0],1))-1, int(faceContent(c[1],1))-1, int(faceContent(c[2],1))-1]) print "Info: Building the Octree" buildOctree.counter = 0 a,nodes = buildOctree(faces, 0, 0) if len(nodes) > MAX_USHORT: print "Error: too many octree nodes generated, increase MAX_FACES_PER_TREELEAF" sys.exit() print "Info: Unifying and Uniquing Vertices, Normals and Texcoords" normalwarning = 0 newvertices = [] newvertices_dict = {} #it's perhaps not the most intuitive way to have the newvertices stored twice, but it prevents a quadratic runtime for face in faces: for i in range(3): if face[i+3] == -1: normalwarning += 1 normals.append(normalize(crossProduct(substract(vertices[face[0]],vertices[face[1]]), substract(vertices[face[2]],vertices[face[0]])))) face[i+3] = len(normals)-1 if TEXTURING and face[i+6] == -1: print "Warning: some face without a texcoord detected, turning texturing off" TEXTURING = 0 for i in range(3): if len(vertices[face[i]]) == 3: vertices[face[i]].extend(normals[face[i+3]]) if TEXTURING: vertices[face[i]].extend(texcoords[face[i+6]]) elif vertices[face[i]][3:6] != normals[face[i+3]] or (TEXTURING and vertices[face[i]][6:] != texcoords[face[i+6]]): #if this vertex has a different normal/texcoord we have to duplicate it because opengl has only one index list sf = face[i] if TEXTURING: key = vertices[face[i]][0], vertices[face[i]][1], vertices[face[i]][2], normals[face[i+3]][0], normals[face[i+3]][1], normals[face[i+3]][2], texcoords[face[i+6]][0], texcoords[face[i+6]][1], texcoords[face[i+6]][2] else: key = vertices[face[i]][0], vertices[face[i]][1], vertices[face[i]][2], normals[face[i+3]][0], normals[face[i+3]][1], normals[face[i+3]][2] if newvertices_dict.has_key(key): face[i] = len(vertices)+newvertices_dict[key] if sf == face[i]: #or create duplicate newvertices.append(list(key)) newvertices_dict[key] = len(newvertices)-1 face[i] = len(vertices)+len(newvertices)-1 #don't forget to update the index to the duplicated vertex+normal vertices.extend(newvertices) if normalwarning: print "Warning: some face without a normal detected, calculating it (x" + str(normalwarning) +")" print "Info: Writing the resulting Octree-file" dummywarning = 0 out = pack('III', 0xDEADBEEF if (TEXTURING or GENTEX) else 0x6D616C62, len(nodes), len(vertices)) for node in nodes: out += pack('IIffffffHHHHHHHH', node[0], node[1], node[2], node[3], node[4], node[5], node[6], node[7], node[8], node[9], node[10], node[11], node[12], node[13], node[14], node[15]) for vert in vertices: try: if TEXTURING: out += pack('ffffffff', vert[0], vert[1], vert[2], vert[3], vert[4], vert[5], vert[6], vert[7]) elif GENTEX: xcoord = (vert[0] - nodes[0][2]) / nodes[0][5] ycoord = (vert[2] - nodes[0][4]) / nodes[0][7] out += pack('ffffffff', vert[0], vert[1], vert[2], vert[3], vert[4], vert[5], (1.0 - xcoord) if (GENTEX == 2 or GENTEX == 4) else xcoord, (1.0 - ycoord) if (GENTEX == 3 or GENTEX == 4) else ycoord) else: out += pack('ffffff', vert[0], vert[1], vert[2], vert[3], vert[4], vert[5]) #the vertex includes the normal now, if not the vertex is unreferenced and this throws except: dummywarning += 1 if TEXTURING: out += pack('ffffffff', 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0) else: out += pack('ffffff', 0.0, 0.0, 0.0, 0.0, 0.0, 0.0) if (len(vertices) <= MAX_USHORT): type = 'HHH' else: type = 'III' for face in faces: out += pack(type, face[0], face[1], face[2]) of.write(bz2.compress(out)) if dummywarning: print "Warning: unreferenced vertex detected, writing dummy vertex (x" + str(dummywarning) +")" print "\nSUCCESS:\n\nnodes:\t\t", len(nodes), "\nvertices:\t", len(vertices), "\t( duplicatesWithDifferentNormalsOrTexcoords:", len(newvertices), ")", "\nfaces:\t\t", len(faces), "\n"
mit
nlhepler/pysam
save/pysam_bench.py
9
1328
'''benchmark pysam BAM/SAM access with the samtools commandline tools. samtools functions are called via the pysam interface to avoid the over-head of starting additional processes. ''' import pysam import timeit iterations = 10 def runBenchmark( test, pysam_way, samtools_way = None): print test print timeit.repeat( pysam_way, number = iterations, setup="from __main__ import pysam" ) if samtools_way: print timeit.repeat( samtools_way, number = iterations, setup="from __main__ import pysam" ) runBenchmark( "Samfile.fetch", ''' f = pysam.Samfile( "ex1.bam", "rb" ) results = list(f.fetch()) ''', ''' f = pysam.view( "ex1.bam" ) ''' ) runBenchmark( "Samfile.pileup", ''' f = pysam.Samfile( "ex1.bam", "rb" ) results = list(f.pileup()) ''', ''' f = pysam.pileup( "ex1.bam" ) ''') runBenchmark( "Samfile.pileup with coverage retrieval", ''' f = pysam.Samfile( "ex1.bam", "rb" ) results = [ x.n for x in f.pileup() ] ''' ) runBenchmark( "Samfile.pileup with full retrieval", ''' f = pysam.Samfile( "ex1.bam", "rb" ) results = [ x.pileups for x in f.pileup() ] ''' ) runBenchmark( "Samfile.pileup - many references", ''' f = pysam.Samfile( "manyrefs.bam", "rb" ) results = [ x.pileups for x in f.pileup() ] ''', ''' f = pysam.pileup( "manyrefs.bam" ) ''' )
mit
BhallaLab/moose-core
tests/core/test_function_example.py
2
3483
# Modified from function.py --- import numpy as np import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import moose simtime = 1.0 def test_example(): moose.Neutral('/model') function = moose.Function('/model/function') function.c['c0'] = 1.0 function.c['c1'] = 2.0 #function.x.num = 1 function.expr = 'c0 * exp(c1*x0) * cos(y0) + sin(t)' # mode 0 - evaluate function value, derivative and rate # mode 1 - just evaluate function value, # mode 2 - evaluate derivative, # mode 3 - evaluate rate function.mode = 0 function.independent = 'y0' nsteps = 1000 xarr = np.linspace(0.0, 1.0, nsteps) # Stimulus tables allow you to store sequences of numbers which # are delivered via the 'output' message at each time step. This # is a placeholder and in real scenario you will be using any # sourceFinfo that sends out a double value. input_x = moose.StimulusTable('/xtab') input_x.vector = xarr input_x.startTime = 0.0 input_x.stepPosition = xarr[0] input_x.stopTime = simtime moose.connect(input_x, 'output', function.x[0], 'input') yarr = np.linspace(-np.pi, np.pi, nsteps) input_y = moose.StimulusTable('/ytab') input_y.vector = yarr input_y.startTime = 0.0 input_y.stepPosition = yarr[0] input_y.stopTime = simtime moose.connect(function, 'requestOut', input_y, 'getOutputValue') # data recording result = moose.Table('/ztab') moose.connect(result, 'requestOut', function, 'getValue') derivative = moose.Table('/zprime') moose.connect(derivative, 'requestOut', function, 'getDerivative') rate = moose.Table('/dz_by_dt') moose.connect(rate, 'requestOut', function, 'getRate') x_rec = moose.Table('/xrec') moose.connect(x_rec, 'requestOut', input_x, 'getOutputValue') y_rec = moose.Table('/yrec') moose.connect(y_rec, 'requestOut', input_y, 'getOutputValue') dt = simtime/nsteps for ii in range(32): moose.setClock(ii, dt) moose.reinit() moose.start(simtime) # Uncomment the following lines and the import matplotlib.pyplot as plt on top # of this file to display the plot. plt.subplot(3,1,1) plt.plot(x_rec.vector, result.vector, 'r-', label='z = {}'.format(function.expr)) z = function.c['c0'] * np.exp(function.c['c1'] * xarr) * np.cos(yarr) + np.sin(np.arange(len(xarr)) * dt) plt.plot(xarr, z, 'b--', label='numpy computed') plt.xlabel('x') plt.ylabel('z') plt.legend() plt.subplot(3,1,2) plt.plot(y_rec.vector, derivative.vector, 'r-', label='dz/dy0') # derivatives computed by putting x values in the analytical formula dzdy = function.c['c0'] * np.exp(function.c['c1'] * xarr) * (- np.sin(yarr)) plt.plot(yarr, dzdy, 'b--', label='numpy computed') plt.xlabel('y') plt.ylabel('dz/dy') plt.legend() plt.subplot(3,1,3) # *** BEWARE *** The first two entries are spurious. Entry 0 is # *** from reinit sending out the defaults. Entry 2 is because # *** there is no lastValue for computing real forward difference. plt.plot(np.arange(2, len(rate.vector), 1) * dt, rate.vector[2:], 'r-', label='dz/dt') dzdt = np.diff(z)/dt plt.plot(np.arange(0, len(dzdt), 1.0) * dt, dzdt, 'b--', label='numpy computed') plt.xlabel('t') plt.ylabel('dz/dt') plt.legend() plt.tight_layout() plt.savefig(__file__+'.png') if __name__ == '__main__': test_example()
gpl-3.0
AbhiAgarwal/blaze
blaze/compute/air/environment.py
4
1108
# -*- coding: utf-8 -*- """ AIR compilation environment. """ from __future__ import print_function, division, absolute_import # Any state that should persist between passes end up in the environment, and # should be documented here air_env = { # blaze expression graph #'expr_graph': None, # global execution strategy specified by the user # TODO: Not sure this is useful? 'strategy': None, # strategy determined for each Op: { Op : strategy } # For instance different sub-expressions may be execution in different # environments 'strategies': None, # Runtime input arguments 'runtime.args': None, # Set by partitioning pass, indicates for each Op and strategy which # overload should be used. { (Op, strategy) : Overload } 'kernel.overloads': None, # Implementation for each op: { Op: Overload } # This is set by assemblage.py #'kernel.impls': None, } def fresh_env(expr, strategy): """ Allocate a new environment. """ env = dict(air_env) env['strategy'] = strategy return env
bsd-3-clause
Effective-Quadratures/Effective-Quadratures
equadratures/scalers.py
1
7167
""" Classes to scale data. Some of these classes are called internally by other modules, but they can also be used independently as a pre-processing stage. Scalers can fit to one set of data, and used to transform other data sets with the same number of dimensions. Examples -------- Fitting scaler implicitly during transform >>> # Define some 1D sample data >>> X = np.random.RandomState(0).normal(2,0.5,200) >>> (X.mean(),X.std()) >>> (2.0354552465705806, 0.5107113843479977) >>> >>> # Scale to zero mean and unit variance >>> X = eq.scalers.scaler_meanvar().transform(X) >>> (X.mean(),X.std()) >>> (2.886579864025407e-17, 1.0) Using the same scaling to transform train and test data >>> # Define some 5D example data >>> X = np.random.RandomState(0).uniform(-10,10,size=(50,5)) >>> y = X[:,0]**2 - X[:,4] >>> # Split into train/test >>> X_train, X_test,y_train,y_test = eq.datasets.train_test_split(X,y,train=0.7,random_seed=0) >>> (X_train.min(),X_train.max()) >>> (-9.906090476149059, 9.767476761184525) >>> >>> # Define a scaler and fit to training split >>> scaler = eq.scalers.scaler_minmax() >>> scaler.fit(X_train) >>> >>> # Transform train and test data with same scaler >>> X_train = scaler.transform(X_train) >>> X_test = scaler.transform(X_test) >>> (X_train.min(),X_train.max()) >>> (-1.0, 1.0) >>> >>> # Finally, e.g. of transforming data back again >>> X_train = scaler.untransform(X_train) >>> (X_train.min(),X_train.max()) >>> (-9.906090476149059, 9.767476761184525) """ import numpy as np class scaler_minmax(object): """ Scale the data to have a min/max of -1 to 1. """ def __init__(self): self.fitted = False def fit(self,X): """ Fit scaler to data. Parameters ---------- X : numpy.ndarray Array with shape (number_of_samples, number_of_dimensions) containing data to fit scaler to. """ if X.ndim == 1: X = X.reshape(-1,1) self.Xmin = np.min(X,axis=0) self.Xmax = np.max(X,axis=0) self.fitted = True def transform(self,X): """ Transforms data. Calls :meth:`~equadratures.scalers.scaler_minmax.fit` fit internally if scaler not already fitted. Parameters ---------- X : numpy.ndarray Array with shape (number_of_samples, number_of_dimensions) containing data to transform. Returns ------- numpy.ndarray Array with shape (number_of_samples, number_of_dimensions) containing transformed data. """ if X.ndim == 1: X = X.reshape(-1,1) if not self.fitted: self.fit(X) Xtrans = 2.0 * ( (X[:,:]-self.Xmin)/(self.Xmax - self.Xmin) ) - 1.0 return Xtrans def untransform(self,X): """ Untransforms data. Parameters ---------- X : numpy.ndarray Array with shape (number_of_samples, number_of_dimensions) containing data to untransform. Returns ------- numpy.ndarray Array with shape (number_of_samples, number_of_dimensions) containing untransformed data. Raises ------ Exception scaler has not been fitted """ if X.ndim == 1: X = X.reshape(-1,1) if not self.fitted: raise Exception('scaler has not been fitted') Xuntrans = 0.5*(X[:,:]+1)*(self.Xmax - self.Xmin) + self.Xmin return Xuntrans class scaler_meanvar(object): """ Scale the data to have a mean of 0 and variance of 1. """ def __init__(self): self.fitted = False def fit(self,X): """ Fit scaler to data. Parameters ---------- X : numpy.ndarray Array with shape (number_of_samples, number_of_dimensions) containing data to fit scaler to. """ if X.ndim == 1: X = X.reshape(-1,1) self.Xmean = np.mean(X,axis=0) self.Xstd = np.std(X,axis=0) self.fitted = True def transform(self,X): """ Transforms data. Calls :meth:`~equadratures.scalers.scaler_meanvar.fit` fit internally if scaler not already fitted. Parameters ---------- X : numpy.ndarray Array with shape (number_of_samples, number_of_dimensions) containing data to transform. Returns ------- numpy.ndarray Array with shape (number_of_samples, number_of_dimensions) containing transformed data. """ if X.ndim == 1: X = X.reshape(-1,1) if not self.fitted: self.fit(X) eps = np.finfo(np.float64).tiny Xtrans = (X[:,:]-self.Xmean)/(self.Xstd+eps) return Xtrans def untransform(self,X): """ Untransforms data. Parameters ---------- X : numpy.ndarray Array with shape (number_of_samples, number_of_dimensions) containing data to untransform. Returns ------- numpy.ndarray Array with shape (number_of_samples, number_of_dimensions) containing untransformed data. Raises ------ Exception scaler has not been fitted """ if X.ndim == 1: X = X.reshape(-1,1) if not self.fitted: raise Exception('scaler has not been fitted') eps = np.finfo(np.float64).tiny Xuntrans = X[:,:]*(self.Xstd+eps) + self.Xmean return Xuntrans class scaler_custom(object): """ Scale the data by the provided offset and divisor. Parameters ---------- offset : float, numpy.ndarray Offset to subtract from data. Either a float, or array with shape (number_of_samples, number_of_dimensions). div : float, numpy.ndarray Divisor to divide data with. Either a float, or array with shape (number_of_samples, number_of_dimensions). """ def __init__(self, offset, div): self.offset = offset self.div = div self.fitted = True def transform(self,X): """ Transforms data. Parameters ---------- X : numpy.ndarray Array with shape (number_of_samples, number_of_dimensions) containing data to transform. Returns ------- numpy.ndarray Array with shape (number_of_samples, number_of_dimensions) containing transformed data. """ if X.ndim == 1: X = X.reshape(-1,1) eps = np.finfo(np.float64).tiny Xtrans = (X - self.offset)/(self.div + eps) return Xtrans def untransform(self,X): """ Untransforms data. Parameters ---------- X : numpy.ndarray Array with shape (number_of_samples, number_of_dimensions) containing data to untransform. Returns ------- numpy.ndarray Array with shape (number_of_samples, number_of_dimensions) containing untransformed data. """ if X.ndim == 1: X = X.reshape(-1,1) eps = np.finfo(np.float64).tiny Xuntrans = X * (self.div + eps) + self.offset return Xuntrans
lgpl-2.1
goodwinnk/intellij-community
python/helpers/py3only/docutils/parsers/rst/languages/zh_tw.py
52
5165
# -*- coding: utf-8 -*- # $Id: zh_tw.py 7119 2011-09-02 13:00:23Z milde $ # Author: David Goodger <goodger@python.org> # Copyright: This module has been placed in the public domain. # New language mappings are welcome. Before doing a new translation, please # read <http://docutils.sf.net/docs/howto/i18n.html>. Two files must be # translated for each language: one in docutils/languages, the other in # docutils/parsers/rst/languages. """ Traditional Chinese language mappings for language-dependent features of reStructuredText. """ __docformat__ = 'reStructuredText' directives = { # language-dependent: fixed 'attention (translation required)': 'attention', 'caution (translation required)': 'caution', 'code (translation required)': 'code', 'danger (translation required)': 'danger', 'error (translation required)': 'error', 'hint (translation required)': 'hint', 'important (translation required)': 'important', 'note (translation required)': 'note', 'tip (translation required)': 'tip', 'warning (translation required)': 'warning', 'admonition (translation required)': 'admonition', 'sidebar (translation required)': 'sidebar', 'topic (translation required)': 'topic', 'line-block (translation required)': 'line-block', 'parsed-literal (translation required)': 'parsed-literal', 'rubric (translation required)': 'rubric', 'epigraph (translation required)': 'epigraph', 'highlights (translation required)': 'highlights', 'pull-quote (translation required)': 'pull-quote', 'compound (translation required)': 'compound', 'container (translation required)': 'container', #'questions (translation required)': 'questions', 'table (translation required)': 'table', 'csv-table (translation required)': 'csv-table', 'list-table (translation required)': 'list-table', #'qa (translation required)': 'questions', #'faq (translation required)': 'questions', 'meta (translation required)': 'meta', 'math (translation required)': 'math', #'imagemap (translation required)': 'imagemap', 'image (translation required)': 'image', 'figure (translation required)': 'figure', 'include (translation required)': 'include', 'raw (translation required)': 'raw', 'replace (translation required)': 'replace', 'unicode (translation required)': 'unicode', '日期': 'date', 'class (translation required)': 'class', 'role (translation required)': 'role', 'default-role (translation required)': 'default-role', 'title (translation required)': 'title', 'contents (translation required)': 'contents', 'sectnum (translation required)': 'sectnum', 'section-numbering (translation required)': 'sectnum', 'header (translation required)': 'header', 'footer (translation required)': 'footer', #'footnotes (translation required)': 'footnotes', #'citations (translation required)': 'citations', 'target-notes (translation required)': 'target-notes', 'restructuredtext-test-directive': 'restructuredtext-test-directive'} """Traditional Chinese name to registered (in directives/__init__.py) directive name mapping.""" roles = { # language-dependent: fixed 'abbreviation (translation required)': 'abbreviation', 'ab (translation required)': 'abbreviation', 'acronym (translation required)': 'acronym', 'ac (translation required)': 'acronym', 'code (translation required)': 'code', 'index (translation required)': 'index', 'i (translation required)': 'index', 'subscript (translation required)': 'subscript', 'sub (translation required)': 'subscript', 'superscript (translation required)': 'superscript', 'sup (translation required)': 'superscript', 'title-reference (translation required)': 'title-reference', 'title (translation required)': 'title-reference', 't (translation required)': 'title-reference', 'pep-reference (translation required)': 'pep-reference', 'pep (translation required)': 'pep-reference', 'rfc-reference (translation required)': 'rfc-reference', 'rfc (translation required)': 'rfc-reference', 'emphasis (translation required)': 'emphasis', 'strong (translation required)': 'strong', 'literal (translation required)': 'literal', 'math (translation required)': 'math', 'named-reference (translation required)': 'named-reference', 'anonymous-reference (translation required)': 'anonymous-reference', 'footnote-reference (translation required)': 'footnote-reference', 'citation-reference (translation required)': 'citation-reference', 'substitution-reference (translation required)': 'substitution-reference', 'target (translation required)': 'target', 'uri-reference (translation required)': 'uri-reference', 'uri (translation required)': 'uri-reference', 'url (translation required)': 'uri-reference', 'raw (translation required)': 'raw',} """Mapping of Traditional Chinese role names to canonical role names for interpreted text."""
apache-2.0
igor-toga/local-snat
neutron/cmd/eventlet/server/__init__.py
9
1058
# Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. from oslo_config import cfg from neutron import server from neutron.server import rpc_eventlet from neutron.server import wsgi_eventlet from neutron.server import wsgi_pecan def main(): server.boot_server(_main_neutron_server) def _main_neutron_server(): if cfg.CONF.web_framework == 'legacy': wsgi_eventlet.eventlet_wsgi_server() else: wsgi_pecan.pecan_wsgi_server() def main_rpc_eventlet(): server.boot_server(rpc_eventlet.eventlet_rpc_server)
apache-2.0
maheshraju-Huawei/actn
tools/build/uploadToS3.py
20
2690
#!/usr/bin/env python """ Upload a file to S3 """ from sys import argv, stdout from time import time from os.path import basename from optparse import OptionParser from boto.s3.key import Key from boto.s3.connection import S3Connection def uploadFile( filename, dest=None, bucket=None, overwrite=False ): "Upload a file to a bucket" if not bucket: bucket = 'onos' if not dest: key = basename( filename ) else: key = dest + basename( filename ) #FIXME add the / print '* Uploading', filename, 'to bucket', bucket, 'as', key stdout.flush() start = time() def callback( transmitted, size ): "Progress callback for set_contents_from_filename" elapsed = time() - start percent = 100.0 * transmitted / size kbps = .001 * transmitted / elapsed print ( '\r%d bytes transmitted of %d (%.2f%%),' ' %.2f KB/sec ' % ( transmitted, size, percent, kbps ) ), stdout.flush() conn = S3Connection() bucket = conn.get_bucket( bucket ) k = Key( bucket ) k.key = key if overwrite or not k.exists(): k.set_contents_from_filename( filename, cb=callback, num_cb=100 ) print elapsed = time() - start print "* elapsed time: %.2f seconds" % elapsed else: print 'file', basename( filename ), 'already exists in', bucket.name def testAccess( bucket=None ): "Verify access to a bucket" if not bucket: bucket = 'onos' conn = S3Connection() bucket = conn.get_bucket( bucket ) print bucket.get_acl() if __name__ == '__main__': usage = "Usage: %prog [options] <file to upload>" parser = OptionParser(usage=usage) parser.add_option("-b", "--bucket", dest="bucket", help="Bucket on S3") parser.add_option("-d", "--dest", dest="dest", help="Destination path in bucket") parser.add_option("-k", "--key", dest="awsKey", help="Bucket on S3") parser.add_option("-s", "--secret", dest="awsSecret", help="Bucket on S3") parser.add_option("-f", "--force", dest="overwrite", help="Overwrite existing file") parser.add_option("-v", "--verify", dest="verify", action="store_true", help="Verify access to a bucket") (options, args) = parser.parse_args() if options.verify: testAccess(options.bucket) exit() if len( args ) == 0: parser.error("missing filenames") for file in args: uploadFile( file, options.dest, options.bucket, options.overwrite ) #FIXME key and secret are unused
apache-2.0
cloudconductor/cloud_conductor_gui
gui_app/views/applicationDeployViews.py
1
8093
# -*- coding: utf-8 -*- from django.shortcuts import render, redirect, render_to_response import ast from ..forms import selecttForm from ..forms import applicationForm from ..utils import ApplicationUtil from ..utils import ApplicationHistoryUtil from ..utils import EnvironmentUtil from ..utils import StringUtil from ..utils.PathUtil import Path from ..utils.PathUtil import Html from ..enum.FunctionCode import FuncCode from ..enum.ApplicationType import ApplicaionType from ..enum.ProtocolType import ProtocolType from ..utils import SessionUtil from ..utils import SystemUtil from ..logs import log def applicationSelect(request): try: session = request.session code = FuncCode.appDep_application.value token = session.get('auth_token') project_id = session.get('project_id') application = '' list = '' list = ApplicationUtil.get_application_version( code, token, project_id) if request.method == "GET": application = request.session.get('w_app_select') return render(request, Html.appdeploy_applicationSelect, {'list': list, 'application': application, 'message': ''}) elif request.method == "POST": param = request.POST # -- Session add application = selectPut(param) form = selecttForm(application) if not form.is_valid(): return render(request, Html.appdeploy_applicationSelect, {'list': list, 'application': application, 'form': form, 'message': ''}) request.session['w_app_select'] = application return redirect(Path.appdeploy_environmentSelect) except Exception as ex: log.error(FuncCode.appDep_application.value, None, ex) return render(request, Html.appdeploy_applicationSelect, {'list': list, 'application': application, 'message': str(ex)}) def applicationCreate(request): code = FuncCode.applicationCreate.value apptype = list(ApplicaionType) protocol = list(ProtocolType) systems = None try: if not SessionUtil.check_login(request): return redirect(Path.logout) if not SessionUtil.check_permission(request, 'application', 'create'): return render_to_response(Html.error_403) token = request.session['auth_token'] project_id = request.session['project_id'] systems = SystemUtil.get_system_list2(code, token, project_id) if request.method == "GET": return render(request, Html.appdeploy_applicationCreate, {'app': '', 'history': '', 'apptype': apptype, 'protocol': protocol, 'message': '', 'systems': systems, 'save': True}) else: # -- Get a value from a form p = request.POST cpPost = p.copy() # -- Validate check form = applicationForm(p) form.full_clean() if not form.is_valid(): return render(request, Html.appdeploy_applicationCreate, {'app': cpPost, 'history': cpPost, 'apptype': apptype, 'protocol': protocol, 'form': form, 'message': '', 'systems': systems, 'save': True}) # -- 1.Create a application, api call app = ApplicationUtil.create_application(code, token, form.data) # -- 2.Create a applicationhistory, api call ApplicationHistoryUtil.create_history( code, token, app.get('id'), form.data) request.session['w_app_select'] = {"id": app.get("id"), "name": app.get("name")} return redirect(Path.appdeploy_environmentSelect) except Exception as ex: log.error(FuncCode.applicationCreate.value, None, ex) return render(request, Html.appdeploy_applicationCreate, {'app': request.POST, 'history': request.POST, 'apptype': apptype, 'protocol': protocol, 'form': '', 'systems': systems, 'message': str(ex), 'save': True}) def environmentSelect(request): list = '' try: code = FuncCode.appDep_environment.value session = request.session environment = session.get('w_env_select') token = session['auth_token'] project_id = session['project_id'] app = ApplicationUtil.get_application_detail( code, token, session.get('w_app_select').get('id')) list = EnvironmentUtil.get_environment_list_system_id( code, token, project_id, app.get("system_id")) if request.method == "GET": return render(request, Html.appdeploy_environmentSelect, {"list": list, 'environment': environment, 'message': ''}) elif request.method == "POST": param = request.POST environment = selectPut(param) form = selecttForm(environment) if not form.is_valid(): return render(request, Html.appdeploy_environmentSelect, {"list": list, 'environment': environment, 'form': form, 'message': ''}) request.session['w_env_select'] = environment return redirect(Path.appdeploy_confirm) except Exception as ex: log.error(FuncCode.appDep_environment.value, None, ex) return render(request, Html.appdeploy_environmentSelect, {"list": '', 'environment': '', 'message': str(ex)}) def confirm(request): try: code = FuncCode.appDep_confirm.value session = request.session app_session = session.get('w_app_select') env_session = session.get('w_env_select') if request.method == "GET": return render(request, Html.appdeploy_confirm, {'application': app_session, 'environment': env_session, 'message': ''}) elif request.method == "POST": session = request.session code = FuncCode.newapp_confirm.value token = session.get('auth_token') env_id = env_session.get('id') app_id = app_session.get('id') # -- application deploy ApplicationUtil.deploy_application(code, token, env_id, app_id) # -- session delete sessionDelete(session) return redirect(Path.top) except Exception as ex: log.error(FuncCode.newapp_confirm.value, None, ex) session = request.session return render(request, Html.appdeploy_confirm, {"application": session.get('application'), 'environment': session.get('environment'), 'message': str(ex)}) def selectPut(req): if StringUtil.isEmpty(req): return None select_param = req.get('id', None) if StringUtil.isNotEmpty(select_param): select_param = ast.literal_eval(select_param) param = { 'id': str(select_param.get('id')), 'name': select_param.get('name'), } return param else: return select_param def putBlueprint(param): blueprint = param.get('blueprint', None) if not (blueprint is None) and not (blueprint == ''): blueprint = ast.literal_eval(blueprint) param['blueprint_id'] = blueprint.get('id') param['version'] = blueprint.get('version') return param def sessionDelete(session): if 'w_env_select' in session: del session['w_env_select'] if 'w_app_select' in session: del session['w_app_select']
apache-2.0
igorborojevic/incubator-zeppelin
dev/test_zeppelin_pr.py
20
3840
#!/usr/bin/python # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # This utility creates a local branch from specified pullrequest, to help the test and review # You'll need to run this utility from master branch with command # # dev/test_zeppelin_pr.py [#PR] # # then pr[#PR] branch will be created. # import sys, os, subprocess import json, urllib if len(sys.argv) == 1: print "usage) " + sys.argv[0] + " [#PR]" print " eg) " + sys.argv[0] + " 122" sys.exit(1) pr=sys.argv[1] githubApi="https://api.github.com/repos/apache/incubator-zeppelin" prInfo = json.load(urllib.urlopen(githubApi + "/pulls/" + pr)) if "message" in prInfo and prInfo["message"] == "Not Found": sys.stderr.write("PullRequest #" + pr + " not found\n") sys.exit(1) prUser=prInfo['user']['login'] prRepoUrl=prInfo['head']['repo']['clone_url'] prBranch=prInfo['head']['label'].replace(":", "/") # create local branch exitCode = os.system("git checkout -b pr" + pr) if exitCode != 0: sys.exit(1) # add remote repository and fetch exitCode = os.system("git remote remove " + prUser) exitCode = os.system("git remote add " + prUser + " " + prRepoUrl) if exitCode != 0: sys.stderr.write("Can not add remote repository.\n") sys.exit(1) exitCode = os.system("git fetch " + prUser) if exitCode != 0: sys.stderr.write("Can't fetch remote repository.\n") sys.exit(1) currentBranch = subprocess.check_output(["git rev-parse --abbrev-ref HEAD"], shell=True).rstrip() print "Merge branch " + prBranch + " into " + currentBranch rev = subprocess.check_output(["git rev-parse " + prBranch], shell=True).rstrip() prAuthor = subprocess.check_output(["git --no-pager show -s --format='%an <%ae>' " + rev], shell=True).rstrip() prAuthorDate = subprocess.check_output(["git --no-pager show -s --format='%ad' " + rev], shell=True).rstrip() prTitle = prInfo['title'] prBody = prInfo['body'] commitList = subprocess.check_output(["git log --pretty=format:'%h' " + currentBranch + ".." + prBranch], shell=True).rstrip() authorList = [] for commitHash in commitList.split("\n"): a = subprocess.check_output(["git show -s --pretty=format:'%an <%ae>' "+commitHash], shell=True).rstrip() if a not in authorList: authorList.append(a) commitMsg = prTitle + "\n" if prBody : commitMsg += prBody + "\n\n" for author in authorList: commitMsg += "Author: " + author+"\n" commitMsg += "\n" commitMsg += "Closes #" + pr + " from " + prBranch + " and squashes the following commits:\n\n" commitMsg += subprocess.check_output(["git log --pretty=format:'%h [%an] %s' " + currentBranch + ".." + prBranch], shell=True).rstrip() exitCode = os.system("git merge --no-commit --squash " + prBranch) if exitCode != 0: sys.stderr.write("Can not merge\n") sys.exit(1) exitCode = os.system('git commit -a --author "' + prAuthor + '" --date "' + prAuthorDate + '" -m"' + commitMsg.encode('utf-8') + '"') if exitCode != 0: sys.stderr.write("Commit failed\n") sys.exit(1) os.system("git remote remove " + prUser) print "Branch " + prBranch + " is merged into " + currentBranch
apache-2.0
robhudson/django
django/contrib/postgres/fields/ranges.py
124
5609
import json from psycopg2.extras import DateRange, DateTimeTZRange, NumericRange, Range from django.contrib.postgres import forms, lookups from django.db import models from django.utils import six from .utils import AttributeSetter __all__ = [ 'RangeField', 'IntegerRangeField', 'BigIntegerRangeField', 'FloatRangeField', 'DateTimeRangeField', 'DateRangeField', ] class RangeField(models.Field): empty_strings_allowed = False def get_prep_value(self, value): if value is None: return None elif isinstance(value, Range): return value elif isinstance(value, (list, tuple)): return self.range_type(value[0], value[1]) return value def to_python(self, value): if isinstance(value, six.string_types): # Assume we're deserializing vals = json.loads(value) for end in ('lower', 'upper'): if end in vals: vals[end] = self.base_field.to_python(vals[end]) value = self.range_type(**vals) elif isinstance(value, (list, tuple)): value = self.range_type(value[0], value[1]) return value def set_attributes_from_name(self, name): super(RangeField, self).set_attributes_from_name(name) self.base_field.set_attributes_from_name(name) def value_to_string(self, obj): value = self.value_from_object(obj) if value is None: return None if value.isempty: return json.dumps({"empty": True}) base_field = self.base_field result = {"bounds": value._bounds} for end in ('lower', 'upper'): obj = AttributeSetter(base_field.attname, getattr(value, end)) result[end] = base_field.value_to_string(obj) return json.dumps(result) def formfield(self, **kwargs): kwargs.setdefault('form_class', self.form_field) return super(RangeField, self).formfield(**kwargs) class IntegerRangeField(RangeField): base_field = models.IntegerField() range_type = NumericRange form_field = forms.IntegerRangeField def db_type(self, connection): return 'int4range' class BigIntegerRangeField(RangeField): base_field = models.BigIntegerField() range_type = NumericRange form_field = forms.IntegerRangeField def db_type(self, connection): return 'int8range' class FloatRangeField(RangeField): base_field = models.FloatField() range_type = NumericRange form_field = forms.FloatRangeField def db_type(self, connection): return 'numrange' class DateTimeRangeField(RangeField): base_field = models.DateTimeField() range_type = DateTimeTZRange form_field = forms.DateTimeRangeField def db_type(self, connection): return 'tstzrange' class DateRangeField(RangeField): base_field = models.DateField() range_type = DateRange form_field = forms.DateRangeField def db_type(self, connection): return 'daterange' RangeField.register_lookup(lookups.DataContains) RangeField.register_lookup(lookups.ContainedBy) RangeField.register_lookup(lookups.Overlap) class RangeContainedBy(models.Lookup): lookup_name = 'contained_by' type_mapping = { 'integer': 'int4range', 'bigint': 'int8range', 'double precision': 'numrange', 'date': 'daterange', 'timestamp with time zone': 'tstzrange', } def as_sql(self, qn, connection): field = self.lhs.output_field if isinstance(field, models.FloatField): sql = '%s::numeric <@ %s::{}'.format(self.type_mapping[field.db_type(connection)]) else: sql = '%s <@ %s::{}'.format(self.type_mapping[field.db_type(connection)]) lhs, lhs_params = self.process_lhs(qn, connection) rhs, rhs_params = self.process_rhs(qn, connection) params = lhs_params + rhs_params return sql % (lhs, rhs), params def get_prep_lookup(self): return RangeField().get_prep_lookup(self.lookup_name, self.rhs) models.DateField.register_lookup(RangeContainedBy) models.DateTimeField.register_lookup(RangeContainedBy) models.IntegerField.register_lookup(RangeContainedBy) models.BigIntegerField.register_lookup(RangeContainedBy) models.FloatField.register_lookup(RangeContainedBy) @RangeField.register_lookup class FullyLessThan(lookups.PostgresSimpleLookup): lookup_name = 'fully_lt' operator = '<<' @RangeField.register_lookup class FullGreaterThan(lookups.PostgresSimpleLookup): lookup_name = 'fully_gt' operator = '>>' @RangeField.register_lookup class NotLessThan(lookups.PostgresSimpleLookup): lookup_name = 'not_lt' operator = '&>' @RangeField.register_lookup class NotGreaterThan(lookups.PostgresSimpleLookup): lookup_name = 'not_gt' operator = '&<' @RangeField.register_lookup class AdjacentToLookup(lookups.PostgresSimpleLookup): lookup_name = 'adjacent_to' operator = '-|-' @RangeField.register_lookup class RangeStartsWith(models.Transform): lookup_name = 'startswith' function = 'lower' @property def output_field(self): return self.lhs.output_field.base_field @RangeField.register_lookup class RangeEndsWith(models.Transform): lookup_name = 'endswith' function = 'upper' @property def output_field(self): return self.lhs.output_field.base_field @RangeField.register_lookup class IsEmpty(models.Transform): lookup_name = 'isempty' function = 'isempty' output_field = models.BooleanField()
bsd-3-clause
hackerkid/zulip
zerver/openapi/python_examples.py
1
46543
# Zulip's OpenAPI-based API documentation system is documented at # https://zulip.readthedocs.io/en/latest/documentation/api.html # # This file defines the Python code examples that appears in Zulip's # REST API documentation, and also contains a system for running the # example code as part of the `tools/test-api` test suite. # # The actual documentation appears within these blocks: # # {code_example|start} # Code here # # {code_example|end} # # Whereas the surrounding code is test setup logic. import json import os import sys from functools import wraps from typing import Any, Callable, Dict, Iterable, List, Optional, Set, TypeVar, cast from zulip import Client from zerver.lib import mdiff from zerver.models import get_realm, get_user from zerver.openapi.openapi import validate_against_openapi_schema ZULIP_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) TEST_FUNCTIONS: Dict[str, Callable[..., object]] = {} REGISTERED_TEST_FUNCTIONS: Set[str] = set() CALLED_TEST_FUNCTIONS: Set[str] = set() FuncT = TypeVar("FuncT", bound=Callable[..., object]) def openapi_test_function(endpoint: str) -> Callable[[FuncT], FuncT]: """This decorator is used to register an OpenAPI test function with its endpoint. Example usage: @openapi_test_function("/messages/render:post") def ... """ def wrapper(test_func: FuncT) -> FuncT: @wraps(test_func) def _record_calls_wrapper(*args: object, **kwargs: object) -> object: CALLED_TEST_FUNCTIONS.add(test_func.__name__) return test_func(*args, **kwargs) REGISTERED_TEST_FUNCTIONS.add(test_func.__name__) TEST_FUNCTIONS[endpoint] = _record_calls_wrapper return cast(FuncT, _record_calls_wrapper) # https://github.com/python/mypy/issues/1927 return wrapper def ensure_users(ids_list: List[int], user_names: List[str]) -> None: # Ensure that the list of user ids (ids_list) # matches the users we want to refer to (user_names). realm = get_realm("zulip") user_ids = [get_user(name + "@zulip.com", realm).id for name in user_names] assert ids_list == user_ids @openapi_test_function("/users/me/subscriptions:post") def add_subscriptions(client: Client) -> None: # {code_example|start} # Subscribe to the stream "new stream" result = client.add_subscriptions( streams=[ { "name": "new stream", "description": "New stream for testing", }, ], ) # {code_example|end} validate_against_openapi_schema(result, "/users/me/subscriptions", "post", "200_0") # {code_example|start} # To subscribe other users to a stream, you may pass # the `principals` argument, like so: user_id = 26 result = client.add_subscriptions( streams=[ {"name": "new stream", "description": "New stream for testing"}, ], principals=[user_id], ) # {code_example|end} assert result["result"] == "success" assert "newbie@zulip.com" in result["subscribed"] def test_add_subscriptions_already_subscribed(client: Client) -> None: result = client.add_subscriptions( streams=[ {"name": "new stream", "description": "New stream for testing"}, ], principals=["newbie@zulip.com"], ) validate_against_openapi_schema(result, "/users/me/subscriptions", "post", "200_1") def test_authorization_errors_fatal(client: Client, nonadmin_client: Client) -> None: client.add_subscriptions( streams=[ {"name": "private_stream"}, ], ) stream_id = client.get_stream_id("private_stream")["stream_id"] client.call_endpoint( f"streams/{stream_id}", method="PATCH", request={"is_private": True}, ) result = nonadmin_client.add_subscriptions( streams=[ {"name": "private_stream"}, ], authorization_errors_fatal=False, ) validate_against_openapi_schema(result, "/users/me/subscriptions", "post", "400_0") result = nonadmin_client.add_subscriptions( streams=[ {"name": "private_stream"}, ], authorization_errors_fatal=True, ) validate_against_openapi_schema(result, "/users/me/subscriptions", "post", "400_1") @openapi_test_function("/users/{user_id_or_email}/presence:get") def get_user_presence(client: Client) -> None: # {code_example|start} # Get presence information for "iago@zulip.com" result = client.get_user_presence("iago@zulip.com") # {code_example|end} validate_against_openapi_schema(result, "/users/{user_id_or_email}/presence", "get", "200") @openapi_test_function("/users/me/presence:post") def update_presence(client: Client) -> None: request = { "status": "active", "ping_only": False, "new_user_input": False, } result = client.update_presence(request) assert result["result"] == "success" @openapi_test_function("/users:post") def create_user(client: Client) -> None: # {code_example|start} # Create a user request = { "email": "newbie@zulip.com", "password": "temp", "full_name": "New User", } result = client.create_user(request) # {code_example|end} validate_against_openapi_schema(result, "/users", "post", "200") # Test "Email already used error" result = client.create_user(request) validate_against_openapi_schema(result, "/users", "post", "400") @openapi_test_function("/users:get") def get_members(client: Client) -> None: # {code_example|start} # Get all users in the realm result = client.get_members() # {code_example|end} validate_against_openapi_schema(result, "/users", "get", "200") members = [m for m in result["members"] if m["email"] == "newbie@zulip.com"] assert len(members) == 1 newbie = members[0] assert not newbie["is_admin"] assert newbie["full_name"] == "New User" # {code_example|start} # You may pass the `client_gravatar` query parameter as follows: result = client.get_members({"client_gravatar": True}) # {code_example|end} validate_against_openapi_schema(result, "/users", "get", "200") assert result["members"][0]["avatar_url"] is None # {code_example|start} # You may pass the `include_custom_profile_fields` query parameter as follows: result = client.get_members({"include_custom_profile_fields": True}) # {code_example|end} validate_against_openapi_schema(result, "/users", "get", "200") for member in result["members"]: if member["is_bot"]: assert member.get("profile_data", None) is None else: assert member.get("profile_data", None) is not None @openapi_test_function("/users/{email}:get") def get_user_by_email(client: Client) -> None: # {code_example|start} # Fetch details on a user given a user ID email = "iago@zulip.com" result = client.call_endpoint( url=f"/users/{email}", method="GET", ) # {code_example|end} validate_against_openapi_schema(result, "/users/{email}", "get", "200") @openapi_test_function("/users/{user_id}:get") def get_single_user(client: Client) -> None: # {code_example|start} # Fetch details on a user given a user ID user_id = 8 result = client.get_user_by_id(user_id) # {code_example|end} validate_against_openapi_schema(result, "/users/{user_id}", "get", "200") # {code_example|start} # If you'd like data on custom profile fields, you can request them as follows: result = client.get_user_by_id(user_id, include_custom_profile_fields=True) # {code_example|end} validate_against_openapi_schema(result, "/users/{user_id}", "get", "200") @openapi_test_function("/users/{user_id}:delete") def deactivate_user(client: Client) -> None: # {code_example|start} # Deactivate a user user_id = 8 result = client.deactivate_user_by_id(user_id) # {code_example|end} validate_against_openapi_schema(result, "/users/{user_id}", "delete", "200") @openapi_test_function("/users/{user_id}/reactivate:post") def reactivate_user(client: Client) -> None: # {code_example|start} # Reactivate a user user_id = 8 result = client.reactivate_user_by_id(user_id) # {code_example|end} validate_against_openapi_schema(result, "/users/{user_id}/reactivate", "post", "200") @openapi_test_function("/users/{user_id}:patch") def update_user(client: Client) -> None: # {code_example|start} # Change a user's full name. user_id = 10 result = client.update_user_by_id(user_id, full_name="New Name") # {code_example|end} validate_against_openapi_schema(result, "/users/{user_id}", "patch", "200") # {code_example|start} # Change value of the custom profile field with ID 9. user_id = 8 result = client.update_user_by_id(user_id, profile_data=[{"id": 9, "value": "some data"}]) # {code_example|end} validate_against_openapi_schema(result, "/users/{user_id}", "patch", "400") @openapi_test_function("/users/{user_id}/subscriptions/{stream_id}:get") def get_subscription_status(client: Client) -> None: # {code_example|start} # Check whether a user is a subscriber to a given stream. user_id = 7 stream_id = 1 result = client.call_endpoint( url=f"/users/{user_id}/subscriptions/{stream_id}", method="GET", ) # {code_example|end} validate_against_openapi_schema( result, "/users/{user_id}/subscriptions/{stream_id}", "get", "200" ) @openapi_test_function("/realm/linkifiers:get") def get_realm_linkifiers(client: Client) -> None: # {code_example|start} # Fetch all the filters in this organization result = client.call_endpoint( url="/realm/linkifiers", method="GET", ) # {code_example|end} validate_against_openapi_schema(result, "/realm/linkifiers", "get", "200") @openapi_test_function("/realm/profile_fields:get") def get_realm_profile_fields(client: Client) -> None: # {code_example|start} # Fetch all the custom profile fields in the user's organization. result = client.call_endpoint( url="/realm/profile_fields", method="GET", ) # {code_example|end} validate_against_openapi_schema(result, "/realm/profile_fields", "get", "200") @openapi_test_function("/realm/profile_fields:patch") def reorder_realm_profile_fields(client: Client) -> None: # {code_example|start} # Reorder the custom profile fields in the user's organization. order = [8, 7, 6, 5, 4, 3, 2, 1] request = {"order": json.dumps(order)} result = client.call_endpoint(url="/realm/profile_fields", method="PATCH", request=request) # {code_example|end} validate_against_openapi_schema(result, "/realm/profile_fields", "patch", "200") @openapi_test_function("/realm/profile_fields:post") def create_realm_profile_field(client: Client) -> None: # {code_example|start} # Create a custom profile field in the user's organization. request = {"name": "Phone", "hint": "Contact No.", "field_type": 1} result = client.call_endpoint(url="/realm/profile_fields", method="POST", request=request) # {code_example|end} validate_against_openapi_schema(result, "/realm/profile_fields", "post", "200") @openapi_test_function("/realm/filters:post") def add_realm_filter(client: Client) -> None: # {code_example|start} # Add a filter to automatically linkify #<number> to the corresponding # issue in Zulip's server repo result = client.add_realm_filter( "#(?P<id>[0-9]+)", "https://github.com/zulip/zulip/issues/%(id)s" ) # {code_example|end} validate_against_openapi_schema(result, "/realm/filters", "post", "200") @openapi_test_function("/realm/filters/{filter_id}:delete") def remove_realm_filter(client: Client) -> None: # {code_example|start} # Remove the linkifier (realm_filter) with ID 1 result = client.remove_realm_filter(1) # {code_example|end} validate_against_openapi_schema(result, "/realm/filters/{filter_id}", "delete", "200") @openapi_test_function("/realm/playgrounds:post") def add_realm_playground(client: Client) -> None: # {code_example|start} # Add a realm playground for Python request = { "name": "Python playground", "pygments_language": json.dumps("Python"), "url_prefix": json.dumps("https://python.example.com"), } result = client.call_endpoint(url="/realm/playgrounds", method="POST", request=request) # {code_example|end} validate_against_openapi_schema(result, "/realm/playgrounds", "post", "200") @openapi_test_function("/realm/playgrounds/{playground_id}:delete") def remove_realm_playground(client: Client) -> None: # {code_example|start} # Remove the playground with ID 1 result = client.call_endpoint(url="/realm/playgrounds/1", method="DELETE") # {code_example|end} validate_against_openapi_schema(result, "/realm/playgrounds/{playground_id}", "delete", "200") @openapi_test_function("/users/me:get") def get_profile(client: Client) -> None: # {code_example|start} # Get the profile of the user/bot that requests this endpoint, # which is `client` in this case: result = client.get_profile() # {code_example|end} validate_against_openapi_schema(result, "/users/me", "get", "200") @openapi_test_function("/users/me:delete") def deactivate_own_user(client: Client, owner_client: Client) -> None: user_id = client.get_profile()["user_id"] # {code_example|start} # Deactivate the account of the current user/bot that requests. result = client.call_endpoint( url="/users/me", method="DELETE", ) # {code_example|end} # Reactivate the account to avoid polluting other tests. owner_client.reactivate_user_by_id(user_id) validate_against_openapi_schema(result, "/users/me", "delete", "200") @openapi_test_function("/get_stream_id:get") def get_stream_id(client: Client) -> int: # {code_example|start} # Get the ID of a given stream stream_name = "new stream" result = client.get_stream_id(stream_name) # {code_example|end} validate_against_openapi_schema(result, "/get_stream_id", "get", "200") return result["stream_id"] @openapi_test_function("/streams/{stream_id}:delete") def archive_stream(client: Client, stream_id: int) -> None: result = client.add_subscriptions( streams=[ { "name": "stream to be archived", "description": "New stream for testing", }, ], ) # {code_example|start} # Archive the stream named 'stream to be archived' stream_id = client.get_stream_id("stream to be archived")["stream_id"] result = client.delete_stream(stream_id) # {code_example|end} validate_against_openapi_schema(result, "/streams/{stream_id}", "delete", "200") assert result["result"] == "success" @openapi_test_function("/streams:get") def get_streams(client: Client) -> None: # {code_example|start} # Get all streams that the user has access to result = client.get_streams() # {code_example|end} validate_against_openapi_schema(result, "/streams", "get", "200") streams = [s for s in result["streams"] if s["name"] == "new stream"] assert streams[0]["description"] == "New stream for testing" # {code_example|start} # You may pass in one or more of the query parameters mentioned above # as keyword arguments, like so: result = client.get_streams(include_public=False) # {code_example|end} validate_against_openapi_schema(result, "/streams", "get", "200") assert len(result["streams"]) == 4 @openapi_test_function("/streams/{stream_id}:patch") def update_stream(client: Client, stream_id: int) -> None: # {code_example|start} # Update the stream by a given ID request = { "stream_id": stream_id, "stream_post_policy": 2, "is_private": True, } result = client.update_stream(request) # {code_example|end} validate_against_openapi_schema(result, "/streams/{stream_id}", "patch", "200") assert result["result"] == "success" @openapi_test_function("/user_groups:get") def get_user_groups(client: Client) -> int: # {code_example|start} # Get all user groups of the realm result = client.get_user_groups() # {code_example|end} validate_against_openapi_schema(result, "/user_groups", "get", "200") hamlet_user_group = [u for u in result["user_groups"] if u["name"] == "hamletcharacters"][0] assert hamlet_user_group["description"] == "Characters of Hamlet" marketing_user_group = [u for u in result["user_groups"] if u["name"] == "marketing"][0] return marketing_user_group["id"] def test_user_not_authorized_error(nonadmin_client: Client) -> None: result = nonadmin_client.get_streams(include_all_active=True) validate_against_openapi_schema(result, "/rest-error-handling", "post", "400_2") def get_subscribers(client: Client) -> None: result = client.get_subscribers(stream="new stream") assert result["subscribers"] == ["iago@zulip.com", "newbie@zulip.com"] def get_user_agent(client: Client) -> None: result = client.get_user_agent() assert result.startswith("ZulipPython/") @openapi_test_function("/users/me/subscriptions:get") def list_subscriptions(client: Client) -> None: # {code_example|start} # Get all streams that the user is subscribed to result = client.list_subscriptions() # {code_example|end} validate_against_openapi_schema(result, "/users/me/subscriptions", "get", "200") streams = [s for s in result["subscriptions"] if s["name"] == "new stream"] assert streams[0]["description"] == "New stream for testing" @openapi_test_function("/users/me/subscriptions:delete") def remove_subscriptions(client: Client) -> None: # {code_example|start} # Unsubscribe from the stream "new stream" result = client.remove_subscriptions( ["new stream"], ) # {code_example|end} validate_against_openapi_schema(result, "/users/me/subscriptions", "delete", "200") # test it was actually removed result = client.list_subscriptions() assert result["result"] == "success" streams = [s for s in result["subscriptions"] if s["name"] == "new stream"] assert len(streams) == 0 # {code_example|start} # Unsubscribe another user from the stream "new stream" result = client.remove_subscriptions( ["new stream"], principals=["newbie@zulip.com"], ) # {code_example|end} validate_against_openapi_schema(result, "/users/me/subscriptions", "delete", "200") @openapi_test_function("/users/me/subscriptions/muted_topics:patch") def toggle_mute_topic(client: Client) -> None: # Send a test message message = { "type": "stream", "to": "Denmark", "topic": "boat party", } client.call_endpoint( url="messages", method="POST", request=message, ) # {code_example|start} # Mute the topic "boat party" in the stream "Denmark" request = { "stream": "Denmark", "topic": "boat party", "op": "add", } result = client.mute_topic(request) # {code_example|end} validate_against_openapi_schema(result, "/users/me/subscriptions/muted_topics", "patch", "200") # {code_example|start} # Unmute the topic "boat party" in the stream "Denmark" request = { "stream": "Denmark", "topic": "boat party", "op": "remove", } result = client.mute_topic(request) # {code_example|end} validate_against_openapi_schema(result, "/users/me/subscriptions/muted_topics", "patch", "200") @openapi_test_function("/users/me/muted_users/{muted_user_id}:post") def add_user_mute(client: Client) -> None: ensure_users([10], ["hamlet"]) # {code_example|start} # Mute user with ID 10 muted_user_id = 10 result = client.call_endpoint(url=f"/users/me/muted_users/{muted_user_id}", method="POST") # {code_example|end} validate_against_openapi_schema(result, "/users/me/muted_users/{muted_user_id}", "post", "200") @openapi_test_function("/users/me/muted_users/{muted_user_id}:delete") def remove_user_mute(client: Client) -> None: ensure_users([10], ["hamlet"]) # {code_example|start} # Unmute user with ID 10 muted_user_id = 10 result = client.call_endpoint(url=f"/users/me/muted_users/{muted_user_id}", method="DELETE") # {code_example|end} validate_against_openapi_schema( result, "/users/me/muted_users/{muted_user_id}", "delete", "200" ) @openapi_test_function("/mark_all_as_read:post") def mark_all_as_read(client: Client) -> None: # {code_example|start} # Mark all of the user's unread messages as read result = client.mark_all_as_read() # {code_example|end} validate_against_openapi_schema(result, "/mark_all_as_read", "post", "200") @openapi_test_function("/mark_stream_as_read:post") def mark_stream_as_read(client: Client) -> None: # {code_example|start} # Mark the unread messages in stream with ID "1" as read result = client.mark_stream_as_read(1) # {code_example|end} validate_against_openapi_schema(result, "/mark_stream_as_read", "post", "200") @openapi_test_function("/mark_topic_as_read:post") def mark_topic_as_read(client: Client) -> None: # Grab an existing topic name topic_name = client.get_stream_topics(1)["topics"][0]["name"] # {code_example|start} # Mark the unread messages in stream 1's topic "topic_name" as read result = client.mark_topic_as_read(1, topic_name) # {code_example|end} validate_against_openapi_schema(result, "/mark_stream_as_read", "post", "200") @openapi_test_function("/users/me/subscriptions/properties:post") def update_subscription_settings(client: Client) -> None: # {code_example|start} # Update the user's subscription in stream #1 to pin it to the top of the # stream list; and in stream #3 to have the hex color "f00" request = [ { "stream_id": 1, "property": "pin_to_top", "value": True, }, { "stream_id": 3, "property": "color", "value": "#f00f00", }, ] result = client.update_subscription_settings(request) # {code_example|end} validate_against_openapi_schema(result, "/users/me/subscriptions/properties", "POST", "200") @openapi_test_function("/messages/render:post") def render_message(client: Client) -> None: # {code_example|start} # Render a message request = { "content": "**foo**", } result = client.render_message(request) # {code_example|end} validate_against_openapi_schema(result, "/messages/render", "post", "200") @openapi_test_function("/messages:get") def get_messages(client: Client) -> None: # {code_example|start} # Get the 100 last messages sent by "iago@zulip.com" to the stream "Verona" request: Dict[str, Any] = { "anchor": "newest", "num_before": 100, "num_after": 0, "narrow": [ {"operator": "sender", "operand": "iago@zulip.com"}, {"operator": "stream", "operand": "Verona"}, ], } result = client.get_messages(request) # {code_example|end} validate_against_openapi_schema(result, "/messages", "get", "200") assert len(result["messages"]) <= request["num_before"] @openapi_test_function("/messages/matches_narrow:get") def check_messages_match_narrow(client: Client) -> None: message = {"type": "stream", "to": "Verona", "topic": "test_topic", "content": "http://foo.com"} msg_ids = [] response = client.send_message(message) msg_ids.append(response["id"]) message["content"] = "no link here" response = client.send_message(message) msg_ids.append(response["id"]) # {code_example|start} # Check which messages within an array match a narrow. request = { "msg_ids": msg_ids, "narrow": [{"operator": "has", "operand": "link"}], } result = client.call_endpoint(url="messages/matches_narrow", method="GET", request=request) # {code_example|end} validate_against_openapi_schema(result, "/messages/matches_narrow", "get", "200") @openapi_test_function("/messages/{message_id}:get") def get_raw_message(client: Client, message_id: int) -> None: assert int(message_id) # {code_example|start} # Get the raw content of the message with ID "message_id" result = client.get_raw_message(message_id) # {code_example|end} validate_against_openapi_schema(result, "/messages/{message_id}", "get", "200") @openapi_test_function("/attachments:get") def get_attachments(client: Client) -> None: # {code_example|start} # Get your attachments. result = client.get_attachments() # {code_example|end} validate_against_openapi_schema(result, "/attachments", "get", "200") @openapi_test_function("/messages:post") def send_message(client: Client) -> int: request: Dict[str, Any] = {} # {code_example|start} # Send a stream message request = { "type": "stream", "to": "Denmark", "topic": "Castle", "content": "I come not, friends, to steal away your hearts.", } result = client.send_message(request) # {code_example|end} validate_against_openapi_schema(result, "/messages", "post", "200") # test that the message was actually sent message_id = result["id"] url = "messages/" + str(message_id) result = client.call_endpoint( url=url, method="GET", ) assert result["result"] == "success" assert result["raw_content"] == request["content"] ensure_users([10], ["hamlet"]) # {code_example|start} # Send a private message user_id = 10 request = { "type": "private", "to": [user_id], "content": "With mirth and laughter let old wrinkles come.", } result = client.send_message(request) # {code_example|end} validate_against_openapi_schema(result, "/messages", "post", "200") # test that the message was actually sent message_id = result["id"] url = "messages/" + str(message_id) result = client.call_endpoint( url=url, method="GET", ) assert result["result"] == "success" assert result["raw_content"] == request["content"] return message_id @openapi_test_function("/messages/{message_id}/reactions:post") def add_reaction(client: Client, message_id: int) -> None: request: Dict[str, Any] = {} # {code_example|start} # Add an emoji reaction request = { "message_id": message_id, "emoji_name": "octopus", } result = client.add_reaction(request) # {code_example|end} validate_against_openapi_schema(result, "/messages/{message_id}/reactions", "post", "200") @openapi_test_function("/messages/{message_id}/reactions:delete") def remove_reaction(client: Client, message_id: int) -> None: request: Dict[str, Any] = {} # {code_example|start} # Remove an emoji reaction request = { "message_id": message_id, "emoji_name": "octopus", } result = client.remove_reaction(request) # {code_example|end} validate_against_openapi_schema(result, "/messages/{message_id}/reactions", "delete", "200") def test_nonexistent_stream_error(client: Client) -> None: request = { "type": "stream", "to": "nonexistent_stream", "topic": "Castle", "content": "I come not, friends, to steal away your hearts.", } result = client.send_message(request) validate_against_openapi_schema(result, "/messages", "post", "400_0") def test_private_message_invalid_recipient(client: Client) -> None: request = { "type": "private", "to": "eeshan@zulip.com", "content": "With mirth and laughter let old wrinkles come.", } result = client.send_message(request) validate_against_openapi_schema(result, "/messages", "post", "400_1") @openapi_test_function("/messages/{message_id}:patch") def update_message(client: Client, message_id: int) -> None: assert int(message_id) # {code_example|start} # Edit a message # (make sure that message_id below is set to the ID of the # message you wish to update) request = { "message_id": message_id, "content": "New content", } result = client.update_message(request) # {code_example|end} validate_against_openapi_schema(result, "/messages/{message_id}", "patch", "200") # test it was actually updated url = "messages/" + str(message_id) result = client.call_endpoint( url=url, method="GET", ) assert result["result"] == "success" assert result["raw_content"] == request["content"] def test_update_message_edit_permission_error(client: Client, nonadmin_client: Client) -> None: request = { "type": "stream", "to": "Denmark", "topic": "Castle", "content": "I come not, friends, to steal away your hearts.", } result = client.send_message(request) request = { "message_id": result["id"], "content": "New content", } result = nonadmin_client.update_message(request) validate_against_openapi_schema(result, "/messages/{message_id}", "patch", "400") @openapi_test_function("/messages/{message_id}:delete") def delete_message(client: Client, message_id: int) -> None: # {code_example|start} # Delete the message with ID "message_id" result = client.delete_message(message_id) # {code_example|end} validate_against_openapi_schema(result, "/messages/{message_id}", "delete", "200") def test_delete_message_edit_permission_error(client: Client, nonadmin_client: Client) -> None: request = { "type": "stream", "to": "Denmark", "topic": "Castle", "content": "I come not, friends, to steal away your hearts.", } result = client.send_message(request) result = nonadmin_client.delete_message(result["id"]) validate_against_openapi_schema(result, "/messages/{message_id}", "delete", "400_1") @openapi_test_function("/messages/{message_id}/history:get") def get_message_history(client: Client, message_id: int) -> None: # {code_example|start} # Get the edit history for message with ID "message_id" result = client.get_message_history(message_id) # {code_example|end} validate_against_openapi_schema(result, "/messages/{message_id}/history", "get", "200") @openapi_test_function("/realm/emoji:get") def get_realm_emoji(client: Client) -> None: # {code_example|start} result = client.get_realm_emoji() # {code_example|end} validate_against_openapi_schema(result, "/realm/emoji", "GET", "200") @openapi_test_function("/messages/flags:post") def update_message_flags(client: Client) -> None: # Send a few test messages request: Dict[str, Any] = { "type": "stream", "to": "Denmark", "topic": "Castle", "content": "I come not, friends, to steal away your hearts.", } message_ids = [] for i in range(0, 3): message_ids.append(client.send_message(request)["id"]) # {code_example|start} # Add the "read" flag to the messages with IDs in "message_ids" request = { "messages": message_ids, "op": "add", "flag": "read", } result = client.update_message_flags(request) # {code_example|end} validate_against_openapi_schema(result, "/messages/flags", "post", "200") # {code_example|start} # Remove the "starred" flag from the messages with IDs in "message_ids" request = { "messages": message_ids, "op": "remove", "flag": "starred", } result = client.update_message_flags(request) # {code_example|end} validate_against_openapi_schema(result, "/messages/flags", "post", "200") def register_queue_all_events(client: Client) -> str: # Register the queue and get all events # Mainly for verifying schema of /register. result = client.register() validate_against_openapi_schema(result, "/register", "post", "200") return result["queue_id"] @openapi_test_function("/register:post") def register_queue(client: Client) -> str: # {code_example|start} # Register the queue result = client.register( event_types=["message", "realm_emoji"], ) # {code_example|end} validate_against_openapi_schema(result, "/register", "post", "200") return result["queue_id"] @openapi_test_function("/events:delete") def deregister_queue(client: Client, queue_id: str) -> None: # {code_example|start} # Delete a queue (queue_id is the ID of the queue # to be removed) result = client.deregister(queue_id) # {code_example|end} validate_against_openapi_schema(result, "/events", "delete", "200") # Test "BAD_EVENT_QUEUE_ID" error result = client.deregister(queue_id) validate_against_openapi_schema(result, "/events", "delete", "400") @openapi_test_function("/server_settings:get") def get_server_settings(client: Client) -> None: # {code_example|start} # Fetch the settings for this server result = client.get_server_settings() # {code_example|end} validate_against_openapi_schema(result, "/server_settings", "get", "200") @openapi_test_function("/settings/notifications:patch") def update_notification_settings(client: Client) -> None: # {code_example|start} # Enable push notifications even when online request = { "enable_offline_push_notifications": True, "enable_online_push_notifications": True, } result = client.update_notification_settings(request) # {code_example|end} validate_against_openapi_schema(result, "/settings/notifications", "patch", "200") @openapi_test_function("/settings/display:patch") def update_display_settings(client: Client) -> None: # {code_example|start} # Show user list on left sidebar in narrow windows. # Change emoji set used for display to Google modern. request = { "left_side_userlist": True, "emojiset": '"google"', } result = client.call_endpoint("settings/display", method="PATCH", request=request) # {code_example|end} validate_against_openapi_schema(result, "/settings/display", "patch", "200") @openapi_test_function("/user_uploads:post") def upload_file(client: Client) -> None: path_to_file = os.path.join(ZULIP_DIR, "zerver", "tests", "images", "img.jpg") # {code_example|start} # Upload a file with open(path_to_file, "rb") as fp: result = client.upload_file(fp) # Share the file by including it in a message. client.send_message( { "type": "stream", "to": "Denmark", "topic": "Castle", "content": "Check out [this picture]({}) of my castle!".format(result["uri"]), } ) # {code_example|end} validate_against_openapi_schema(result, "/user_uploads", "post", "200") @openapi_test_function("/users/me/{stream_id}/topics:get") def get_stream_topics(client: Client, stream_id: int) -> None: # {code_example|start} result = client.get_stream_topics(stream_id) # {code_example|end} validate_against_openapi_schema(result, "/users/me/{stream_id}/topics", "get", "200") @openapi_test_function("/typing:post") def set_typing_status(client: Client) -> None: ensure_users([10, 11], ["hamlet", "iago"]) # {code_example|start} # The user has started to type in the group PM with Iago and Polonius user_id1 = 10 user_id2 = 11 request = { "op": "start", "to": [user_id1, user_id2], } result = client.set_typing_status(request) # {code_example|end} validate_against_openapi_schema(result, "/typing", "post", "200") # {code_example|start} # The user has finished typing in the group PM with Iago and Polonius user_id1 = 10 user_id2 = 11 request = { "op": "stop", "to": [user_id1, user_id2], } result = client.set_typing_status(request) # {code_example|end} validate_against_openapi_schema(result, "/typing", "post", "200") @openapi_test_function("/realm/emoji/{emoji_name}:post") def upload_custom_emoji(client: Client) -> None: emoji_path = os.path.join(ZULIP_DIR, "zerver", "tests", "images", "img.jpg") # {code_example|start} # Upload a custom emoji; assume `emoji_path` is the path to your image. with open(emoji_path, "rb") as fp: emoji_name = "my_custom_emoji" result = client.call_endpoint( f"realm/emoji/{emoji_name}", method="POST", files=[fp], ) # {code_example|end} validate_against_openapi_schema(result, "/realm/emoji/{emoji_name}", "post", "200") @openapi_test_function("/users/me/alert_words:get") def get_alert_words(client: Client) -> None: result = client.get_alert_words() assert result["result"] == "success" @openapi_test_function("/users/me/alert_words:post") def add_alert_words(client: Client) -> None: word = ["foo", "bar"] result = client.add_alert_words(word) assert result["result"] == "success" @openapi_test_function("/users/me/alert_words:delete") def remove_alert_words(client: Client) -> None: word = ["foo"] result = client.remove_alert_words(word) assert result["result"] == "success" @openapi_test_function("/user_groups/create:post") def create_user_group(client: Client) -> None: ensure_users([6, 7, 8, 10], ["aaron", "zoe", "cordelia", "hamlet"]) # {code_example|start} request = { "name": "marketing", "description": "The marketing team.", "members": [6, 7, 8, 10], } result = client.create_user_group(request) # {code_example|end} validate_against_openapi_schema(result, "/user_groups/create", "post", "200") assert result["result"] == "success" @openapi_test_function("/user_groups/{user_group_id}:patch") def update_user_group(client: Client, user_group_id: int) -> None: # {code_example|start} request = { "group_id": user_group_id, "name": "marketing", "description": "The marketing team.", } result = client.update_user_group(request) # {code_example|end} assert result["result"] == "success" @openapi_test_function("/user_groups/{user_group_id}:delete") def remove_user_group(client: Client, user_group_id: int) -> None: # {code_example|start} result = client.remove_user_group(user_group_id) # {code_example|end} validate_against_openapi_schema(result, "/user_groups/{user_group_id}", "delete", "200") assert result["result"] == "success" @openapi_test_function("/user_groups/{user_group_id}/members:post") def update_user_group_members(client: Client, user_group_id: int) -> None: ensure_users([8, 10, 11], ["cordelia", "hamlet", "iago"]) # {code_example|start} request = { "delete": [8, 10], "add": [11], } result = client.update_user_group_members(user_group_id, request) # {code_example|end} validate_against_openapi_schema(result, "/user_groups/{group_id}/members", "post", "200") def test_invalid_api_key(client_with_invalid_key: Client) -> None: result = client_with_invalid_key.list_subscriptions() validate_against_openapi_schema(result, "/rest-error-handling", "post", "400_0") def test_missing_request_argument(client: Client) -> None: result = client.render_message({}) validate_against_openapi_schema(result, "/rest-error-handling", "post", "400_1") def test_user_account_deactivated(client: Client) -> None: request = { "content": "**foo**", } result = client.render_message(request) validate_against_openapi_schema(result, "/rest-error-handling", "post", "403_0") def test_realm_deactivated(client: Client) -> None: request = { "content": "**foo**", } result = client.render_message(request) validate_against_openapi_schema(result, "/rest-error-handling", "post", "403_1") def test_invalid_stream_error(client: Client) -> None: result = client.get_stream_id("nonexistent") validate_against_openapi_schema(result, "/get_stream_id", "get", "400") # SETUP METHODS FOLLOW def test_against_fixture( result: Dict[str, Any], fixture: Dict[str, Any], check_if_equal: Optional[Iterable[str]] = None, check_if_exists: Optional[Iterable[str]] = None, ) -> None: assertLength(result, fixture) if check_if_equal is None and check_if_exists is None: for key, value in fixture.items(): assertEqual(key, result, fixture) if check_if_equal is not None: for key in check_if_equal: assertEqual(key, result, fixture) if check_if_exists is not None: for key in check_if_exists: assertIn(key, result) def assertEqual(key: str, result: Dict[str, Any], fixture: Dict[str, Any]) -> None: if result[key] != fixture[key]: first = f"{key} = {result[key]}" second = f"{key} = {fixture[key]}" raise AssertionError( "Actual and expected outputs do not match; showing diff:\n" + mdiff.diff_strings(first, second) ) else: assert result[key] == fixture[key] def assertLength(result: Dict[str, Any], fixture: Dict[str, Any]) -> None: if len(result) != len(fixture): result_string = json.dumps(result, indent=4, sort_keys=True) fixture_string = json.dumps(fixture, indent=4, sort_keys=True) raise AssertionError( "The lengths of the actual and expected outputs do not match; showing diff:\n" + mdiff.diff_strings(result_string, fixture_string) ) else: assert len(result) == len(fixture) def assertIn(key: str, result: Dict[str, Any]) -> None: if key not in result.keys(): raise AssertionError( f"The actual output does not contain the the key `{key}`.", ) else: assert key in result def test_messages(client: Client, nonadmin_client: Client) -> None: render_message(client) message_id = send_message(client) add_reaction(client, message_id) remove_reaction(client, message_id) update_message(client, message_id) get_raw_message(client, message_id) get_messages(client) check_messages_match_narrow(client) get_message_history(client, message_id) delete_message(client, message_id) mark_all_as_read(client) mark_stream_as_read(client) mark_topic_as_read(client) update_message_flags(client) test_nonexistent_stream_error(client) test_private_message_invalid_recipient(client) test_update_message_edit_permission_error(client, nonadmin_client) test_delete_message_edit_permission_error(client, nonadmin_client) def test_users(client: Client, owner_client: Client) -> None: create_user(client) get_members(client) get_single_user(client) deactivate_user(client) reactivate_user(client) update_user(client) get_user_by_email(client) get_subscription_status(client) get_profile(client) update_notification_settings(client) update_display_settings(client) upload_file(client) get_attachments(client) set_typing_status(client) update_presence(client) get_user_presence(client) create_user_group(client) user_group_id = get_user_groups(client) update_user_group(client, user_group_id) update_user_group_members(client, user_group_id) remove_user_group(client, user_group_id) get_alert_words(client) add_alert_words(client) remove_alert_words(client) deactivate_own_user(client, owner_client) add_user_mute(client) remove_user_mute(client) def test_streams(client: Client, nonadmin_client: Client) -> None: add_subscriptions(client) test_add_subscriptions_already_subscribed(client) list_subscriptions(client) stream_id = get_stream_id(client) update_stream(client, stream_id) get_streams(client) get_subscribers(client) remove_subscriptions(client) toggle_mute_topic(client) update_subscription_settings(client) update_notification_settings(client) get_stream_topics(client, 1) archive_stream(client, stream_id) test_user_not_authorized_error(nonadmin_client) test_authorization_errors_fatal(client, nonadmin_client) def test_queues(client: Client) -> None: # Note that the example for api/get-events is not tested. # Since, methods such as client.get_events() or client.call_on_each_message # are blocking calls and since the event queue backend is already # thoroughly tested in zerver/tests/test_event_queue.py, it is not worth # the effort to come up with asynchronous logic for testing those here. queue_id = register_queue(client) deregister_queue(client, queue_id) register_queue_all_events(client) def test_server_organizations(client: Client) -> None: get_realm_linkifiers(client) add_realm_filter(client) add_realm_playground(client) get_server_settings(client) remove_realm_filter(client) remove_realm_playground(client) get_realm_emoji(client) upload_custom_emoji(client) get_realm_profile_fields(client) reorder_realm_profile_fields(client) create_realm_profile_field(client) def test_errors(client: Client) -> None: test_missing_request_argument(client) test_invalid_stream_error(client) def test_the_api(client: Client, nonadmin_client: Client, owner_client: Client) -> None: get_user_agent(client) test_users(client, owner_client) test_streams(client, nonadmin_client) test_messages(client, nonadmin_client) test_queues(client) test_server_organizations(client) test_errors(client) sys.stdout.flush() if REGISTERED_TEST_FUNCTIONS != CALLED_TEST_FUNCTIONS: print("Error! Some @openapi_test_function tests were never called:") print(" ", REGISTERED_TEST_FUNCTIONS - CALLED_TEST_FUNCTIONS) sys.exit(1)
apache-2.0
weimingtom/japanese-novel-analyser
src/freqbrowser.py
2
1753
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ This is the Frequency Browser. It uses the database created by the Japanese Novel Analyser and allows displaying the frequencies by selecting certain parts of speech or conjugations. For these it has grouping and filter options. Usage: freqbrowser.py [OPTION]... Display the frequencies with the given OPTIONs -n, --number=N Display the top N frequencies in list -t, --tablename Table to use """ import sys import getopt import os.path import sqlite3 import re import database import config import gui from logger import logger def main(): # parse command line options try: opts, args = getopt.getopt(sys.argv[1:], 'hn:t:', ['help','number=','tablename=']) except getopt.error as opterr: logger.err(opterr) logger.err('for help use --help') sys.exit(2) # process config and options list_number = config.list_number tablename = config.tablename for o, a in opts: if o in ('-h', '--help'): logger.out(__doc__) sys.exit(0) if o in ('-n', '--number'): try: top_number = int(a) except ValueError: logger.err('invalid argument for top number: %s' % a) sys.exit(2) if list_number <= 0: logger.err('invalid top number: %s' % list_number) sys.exit(2) if o in ('-t', '--tablename'): tablename = a if not re.match(r'^[_a-zA-Z][_a-zA-Z0-9]*$', tablename): logger.err('invalid table name: %s' % tablename) sys.exit(2) # open gui with database try: db = database.Database(tablename) with db: ui = gui.FreqGUI(db, list_number) ui.show() except sqlite3.Error as e: logger.err('database error: %s' % e) if __name__ == '__main__': main()
gpl-3.0
CloudI/CloudI
src/service_api/python/jsonrpclib/tests/test_server.py
1
1597
#!/usr/bin/python # -- Content-Encoding: UTF-8 -- """ Tests the pooled server :license: Apache License 2.0 """ # JSON-RPC library from jsonrpclib import ServerProxy from jsonrpclib.SimpleJSONRPCServer import PooledJSONRPCServer from jsonrpclib.threadpool import ThreadPool # Standard library import random import threading import unittest # ------------------------------------------------------------------------------ def add(a, b): return a+b class PooledServerTests(unittest.TestCase): """ These tests verify that the pooled server works correctly """ def test_default_pool(self, pool=None): """ Tests the default pool """ # Setup server server = PooledJSONRPCServer(("localhost", 0), thread_pool=pool) server.register_function(add) # Serve in a thread thread = threading.Thread(target=server.serve_forever) thread.daemon = True thread.start() # Find its port port = server.socket.getsockname()[1] # Make the client client = ServerProxy("http://localhost:{0}".format(port)) # Check calls for _ in range(10): a, b = random.random(), random.random() result = client.add(a, b) self.assertEqual(result, a+b) # Close server server.server_close() thread.join() def test_custom_pool(self): """ Tests the ability to have a custom pool """ # Setup the pool pool = ThreadPool(2) pool.start() self.test_default_pool(pool)
mit
zedr/django
django/templatetags/static.py
227
4084
from django import template from django.template.base import Node from django.utils.encoding import iri_to_uri from django.utils.six.moves.urllib.parse import urljoin register = template.Library() class PrefixNode(template.Node): def __repr__(self): return "<PrefixNode for %r>" % self.name def __init__(self, varname=None, name=None): if name is None: raise template.TemplateSyntaxError( "Prefix nodes must be given a name to return.") self.varname = varname self.name = name @classmethod def handle_token(cls, parser, token, name): """ Class method to parse prefix node and return a Node. """ # token.split_contents() isn't useful here because tags using this method don't accept variable as arguments tokens = token.contents.split() if len(tokens) > 1 and tokens[1] != 'as': raise template.TemplateSyntaxError( "First argument in '%s' must be 'as'" % tokens[0]) if len(tokens) > 1: varname = tokens[2] else: varname = None return cls(varname, name) @classmethod def handle_simple(cls, name): try: from django.conf import settings except ImportError: prefix = '' else: prefix = iri_to_uri(getattr(settings, name, '')) return prefix def render(self, context): prefix = self.handle_simple(self.name) if self.varname is None: return prefix context[self.varname] = prefix return '' @register.tag def get_static_prefix(parser, token): """ Populates a template variable with the static prefix, ``settings.STATIC_URL``. Usage:: {% get_static_prefix [as varname] %} Examples:: {% get_static_prefix %} {% get_static_prefix as static_prefix %} """ return PrefixNode.handle_token(parser, token, "STATIC_URL") @register.tag def get_media_prefix(parser, token): """ Populates a template variable with the media prefix, ``settings.MEDIA_URL``. Usage:: {% get_media_prefix [as varname] %} Examples:: {% get_media_prefix %} {% get_media_prefix as media_prefix %} """ return PrefixNode.handle_token(parser, token, "MEDIA_URL") class StaticNode(Node): def __init__(self, varname=None, path=None): if path is None: raise template.TemplateSyntaxError( "Static template nodes must be given a path to return.") self.path = path self.varname = varname def url(self, context): path = self.path.resolve(context) return self.handle_simple(path) def render(self, context): url = self.url(context) if self.varname is None: return url context[self.varname] = url return '' @classmethod def handle_simple(cls, path): return urljoin(PrefixNode.handle_simple("STATIC_URL"), path) @classmethod def handle_token(cls, parser, token): """ Class method to parse prefix node and return a Node. """ bits = token.split_contents() if len(bits) < 2: raise template.TemplateSyntaxError( "'%s' takes at least one argument (path to file)" % bits[0]) path = parser.compile_filter(bits[1]) if len(bits) >= 2 and bits[-2] == 'as': varname = bits[3] else: varname = None return cls(varname, path) @register.tag('static') def do_static(parser, token): """ Joins the given path with the STATIC_URL setting. Usage:: {% static path [as varname] %} Examples:: {% static "myapp/css/base.css" %} {% static variable_with_path %} {% static "myapp/css/base.css" as admin_base_css %} {% static variable_with_path as varname %} """ return StaticNode.handle_token(parser, token) def static(path): return StaticNode.handle_simple(path)
bsd-3-clause
AversivePlusPlus/AversivePlusPlus
tools/conan/conans/client/importer.py
2
3684
import os import fnmatch from conans.model.ref import PackageReference from conans.client.file_copier import FileCopier class FileImporter(object): """ manages the copy of files, resources, libs from the local store to the user space. E.g.: shared libs, dlls, they will be in the package folder of your configuration in the store. But you dont want to add every package to the system PATH. Those shared libs can be copied to the user folder, close to the exes where they can be found without modifying the path. Useful also for copying other resources as images or data files. It can be also used for Golang projects, in which the packages are always source based and need to be copied to the user folder to be built """ def __init__(self, deps_graph, paths, dst_folder): self._graph = deps_graph self._paths = paths self._dst_folder = dst_folder self._copies = [] def __call__(self, pattern, dst="", src="", root_package="*"): """ FileImporter is lazy, it just store requested copies, and execute them later param pattern: an fnmatch file pattern of the files that should be copied. Eg. *.dll param dst: the destination local folder, wrt to current conanfile dir, to which the files will be copied. Eg: "bin" param src: the source folder in which those files will be searched. This folder will be stripped from the dst name. Eg.: lib/Debug/x86 param root_package: fnmatch pattern of the package name ("OpenCV", "Boost") from which files will be copied. Default: all packages in deps """ self._copies.append((pattern, dst, src, root_package)) def _get_folders(self): """ given the current deps graph, compute a dict {name: store-path} of each dependency """ package_folders = {} for node in self._graph.nodes: conan_ref, conan_file = node if not conan_ref: continue package_id = conan_file.info.package_id() package_reference = PackageReference(conan_ref, package_id) short_paths = "check" if conan_file.short_paths else False package_folders[conan_file.name] = self._paths.package(package_reference, short_paths) return package_folders def _get_paths(self, conan_name_pattern): """ returns all the base paths of the dependencies matching the root_package pattern """ result_paths = [] folders = self._get_folders() for name, path in folders.items(): if fnmatch.fnmatch(name, conan_name_pattern): result_paths.append(path) return result_paths def execute(self): """ Execute the stored requested copies, using a FileCopier as helper return: set of copied files """ root_src_folder = self._paths.store file_copier = FileCopier(root_src_folder, self._dst_folder) copied_files = set() for pattern, dst_folder, src_folder, conan_name_pattern in self._copies: if os.path.isabs(dst_folder): real_dst_folder = dst_folder else: real_dst_folder = os.path.normpath(os.path.join(self._dst_folder, dst_folder)) matching_paths = self._get_paths(conan_name_pattern) for matching_path in matching_paths: real_src_folder = os.path.join(matching_path, src_folder) files = file_copier(pattern, real_dst_folder, real_src_folder) copied_files.update(files) return copied_files
bsd-3-clause
pgleeson/TestArea
pythonnC/Ex4_SaveNetworkML.py
5
2358
# # # A file which opens a neuroConstruct project, adds some cells and network connections # and then saves a NetworkML file with the net structure # # Author: Padraig Gleeson # # This file has been developed as part of the neuroConstruct project # This work has been funded by the Medical Research Council and the # Wellcome Trust # # try: from java.io import File from java.lang import System except ImportError: print "Note: this file should be run using ..\\nC.bat -python XXX.py' or './nC.sh -python XXX.py'" print "See http://www.neuroconstruct.org/docs/python.html for more details" quit() from ucl.physiol.neuroconstruct.project import ProjectManager from math import * # Load an existing neuroConstruct project projFile = File("TestPython/TestPython.neuro.xml") print "Loading project from file: " + projFile.getAbsolutePath()+", exists: "+ str(projFile.exists()) pm = ProjectManager() myProject = pm.loadProject(projFile) print "Loaded project: " + myProject.getProjectName() # Add a number of cells to the generatedCellPositions, connections to generatedNetworkConnections # and electrical inputs to generatedElecInputs numCells = 12 for i in range(0, numCells) : x = 100 * sin(i * 2 *pi / numCells) y = 100 * cos(i * 2 *pi / numCells) myProject.generatedCellPositions.addPosition("SampleCellGroup", i, x,y,0) if i != numCells-1 : myProject.generatedNetworkConnections.addSynapticConnection("NC1", i, i+1) # Print details print myProject.generatedCellPositions.details() print myProject.generatedNetworkConnections.details() # Save to a NetworkML file myNetworkMLFile = File("TestPython/savedNetworks/nmlt.nml") simConfig = myProject.simConfigInfo.getDefaultSimConfig() pm.saveNetworkStructureXML(myProject, myNetworkMLFile, 0, 0, simConfig.getName(), "Physiological Units") print "Network structure saved to file: "+ myNetworkMLFile.getAbsolutePath() import sys print sys.argv if len(sys.argv) > 1 and sys.argv[1]=='-h': myNetworkMLH5File = File("TestPython/savedNetworks/nmlt.h5") pm.saveNetworkStructureHDF5(myProject, myNetworkMLH5File, simConfig.getName(), "Physiological Units") print "Network structure also saved to file: "+ myNetworkMLH5File.getAbsolutePath() System.exit(0)
gpl-2.0
dan-cristian/haiot
gpio/io_common/__init__.py
1
3453
from common import Constant from storage.model import m from main.logger_helper import L import abc from common import utils # update in db (without propagatting the change by default) def update_custom_relay(pin_code, pin_value, notify=False, ignore_missing=False): relay = m.ZoneCustomRelay.find_one({m.ZoneCustomRelay.gpio_pin_code: pin_code, m.ZoneCustomRelay.gpio_host_name: Constant.HOST_NAME}) if relay is not None: relay.relay_is_on = pin_value relay.save_changed_fields(broadcast=notify) L.l.info('Updated relay {} val={}'.format(pin_code, pin_value)) else: if not ignore_missing: L.l.warning('Unable to find relay pin {}'.format(pin_code)) # update in db (without propagatting the change by default) def update_listener_custom_relay(relay, is_on): relay.relay_is_on = is_on relay.save_changed_fields(broadcast=True) L.l.info('Updated listener relay {} val={}'.format(relay, is_on)) class Port: _port_list = [] type = None TYPE_GPIO = 'gpio' TYPE_PIFACE = 'piface' TYPE_PCF8574 = 'pcf8574' _types = frozenset([TYPE_GPIO, TYPE_PIFACE, TYPE_PCF8574]) def __init__(self): pass class OutputPort(Port): def __init__(self): pass class InputPort(Port): def __init__(self): pass class IOPort(InputPort, OutputPort): def __init__(self): pass class GpioBase: __metaclass__ = abc.ABCMeta @staticmethod @abc.abstractmethod def get_current_record(record): return None, None @staticmethod @abc.abstractmethod def get_db_record(key): return None def record_update(self, record, changed_fields): # record = utils.json_to_record(self.obj, json_object) current_record, key = self.get_current_record(record) if current_record is not None: new_record = self.obj() kwargs = {} for field in changed_fields: val = getattr(record, field) # setattr(new_record, field, val) kwargs[field] = val if record.host_name == Constant.HOST_NAME and record.source_host != Constant.HOST_NAME: # https://stackoverflow.com/questions/1496346/passing-a-list-of-kwargs self.set(key, **kwargs) # do nothing, action done already as it was local # save will be done on model.save # record.save_changed_fields() @staticmethod @abc.abstractmethod def set(key, values): pass @staticmethod @abc.abstractmethod def save(key, values): pass @staticmethod @abc.abstractmethod def get(key): return None @staticmethod @abc.abstractmethod def sync_to_db(key): pass @staticmethod @abc.abstractmethod def unload(): pass def __init__(self, obj): self.obj = obj def format_piface_pin_code(board_index, pin_direction, pin_index): return str(board_index) + ":" + str(pin_direction) + ":" + str(pin_index) # port format is x:direction:y, e.g. 0:in:3, x=board, direction=in/out, y=pin index (0 based) def decode_piface_pin(pin_code): ar = pin_code.split(':') if len(ar) == 3: return int(ar[0]), ar[1], int(ar[2]) else: L.l.error('Invalid piface pin code {}'.format(pin_code)) return None, None, None
gpl-2.0
nanolearningllc/edx-platform-cypress
common/djangoapps/student/tests/test_create_account.py
39
20158
"""Tests for account creation""" import json import ddt import unittest from django.contrib.auth.models import User from django.test.client import RequestFactory from django.conf import settings from django.core.urlresolvers import reverse from django.contrib.auth.models import AnonymousUser from django.utils.importlib import import_module from django.test import TestCase, TransactionTestCase from django.test.utils import override_settings import mock from openedx.core.djangoapps.user_api.preferences.api import get_user_preference from lang_pref import LANGUAGE_KEY from notification_prefs import NOTIFICATION_PREF_KEY from edxmako.tests import mako_middleware_process_request from external_auth.models import ExternalAuthMap import student TEST_CS_URL = 'https://comments.service.test:123/' @ddt.ddt @override_settings( MICROSITE_CONFIGURATION={ "microsite": { "domain_prefix": "microsite", "extended_profile_fields": ["extra1", "extra2"], } }, REGISTRATION_EXTRA_FIELDS={ key: "optional" for key in [ "level_of_education", "gender", "mailing_address", "city", "country", "goals", "year_of_birth" ] } ) class TestCreateAccount(TestCase): """Tests for account creation""" def setUp(self): super(TestCreateAccount, self).setUp() self.username = "test_user" self.url = reverse("create_account") self.request_factory = RequestFactory() self.params = { "username": self.username, "email": "test@example.org", "password": "testpass", "name": "Test User", "honor_code": "true", "terms_of_service": "true", } @ddt.data("en", "eo") def test_default_lang_pref_saved(self, lang): with mock.patch("django.conf.settings.LANGUAGE_CODE", lang): response = self.client.post(self.url, self.params) self.assertEqual(response.status_code, 200) user = User.objects.get(username=self.username) self.assertEqual(get_user_preference(user, LANGUAGE_KEY), lang) @ddt.data("en", "eo") def test_header_lang_pref_saved(self, lang): response = self.client.post(self.url, self.params, HTTP_ACCEPT_LANGUAGE=lang) user = User.objects.get(username=self.username) self.assertEqual(response.status_code, 200) self.assertEqual(get_user_preference(user, LANGUAGE_KEY), lang) def create_account_and_fetch_profile(self): """ Create an account with self.params, assert that the response indicates success, and return the UserProfile object for the newly created user """ response = self.client.post(self.url, self.params, HTTP_HOST="microsite.example.com") self.assertEqual(response.status_code, 200) user = User.objects.get(username=self.username) return user.profile def test_marketing_cookie(self): response = self.client.post(self.url, self.params) self.assertEqual(response.status_code, 200) self.assertIn(settings.EDXMKTG_LOGGED_IN_COOKIE_NAME, self.client.cookies) self.assertIn(settings.EDXMKTG_USER_INFO_COOKIE_NAME, self.client.cookies) @unittest.skipUnless( "microsite_configuration.middleware.MicrositeMiddleware" in settings.MIDDLEWARE_CLASSES, "Microsites not implemented in this environment" ) def test_profile_saved_no_optional_fields(self): profile = self.create_account_and_fetch_profile() self.assertEqual(profile.name, self.params["name"]) self.assertEqual(profile.level_of_education, "") self.assertEqual(profile.gender, "") self.assertEqual(profile.mailing_address, "") self.assertEqual(profile.city, "") self.assertEqual(profile.country, "") self.assertEqual(profile.goals, "") self.assertEqual( profile.get_meta(), { "extra1": "", "extra2": "", } ) self.assertIsNone(profile.year_of_birth) @unittest.skipUnless( "microsite_configuration.middleware.MicrositeMiddleware" in settings.MIDDLEWARE_CLASSES, "Microsites not implemented in this environment" ) def test_profile_saved_all_optional_fields(self): self.params.update({ "level_of_education": "a", "gender": "o", "mailing_address": "123 Example Rd", "city": "Exampleton", "country": "US", "goals": "To test this feature", "year_of_birth": "2015", "extra1": "extra_value1", "extra2": "extra_value2", }) profile = self.create_account_and_fetch_profile() self.assertEqual(profile.level_of_education, "a") self.assertEqual(profile.gender, "o") self.assertEqual(profile.mailing_address, "123 Example Rd") self.assertEqual(profile.city, "Exampleton") self.assertEqual(profile.country, "US") self.assertEqual(profile.goals, "To test this feature") self.assertEqual( profile.get_meta(), { "extra1": "extra_value1", "extra2": "extra_value2", } ) self.assertEqual(profile.year_of_birth, 2015) @unittest.skipUnless( "microsite_configuration.middleware.MicrositeMiddleware" in settings.MIDDLEWARE_CLASSES, "Microsites not implemented in this environment" ) def test_profile_saved_empty_optional_fields(self): self.params.update({ "level_of_education": "", "gender": "", "mailing_address": "", "city": "", "country": "", "goals": "", "year_of_birth": "", "extra1": "", "extra2": "", }) profile = self.create_account_and_fetch_profile() self.assertEqual(profile.level_of_education, "") self.assertEqual(profile.gender, "") self.assertEqual(profile.mailing_address, "") self.assertEqual(profile.city, "") self.assertEqual(profile.country, "") self.assertEqual(profile.goals, "") self.assertEqual( profile.get_meta(), {"extra1": "", "extra2": ""} ) self.assertEqual(profile.year_of_birth, None) def test_profile_year_of_birth_non_integer(self): self.params["year_of_birth"] = "not_an_integer" profile = self.create_account_and_fetch_profile() self.assertIsNone(profile.year_of_birth) def base_extauth_bypass_sending_activation_email(self, bypass_activation_email): """ Tests user creation without sending activation email when doing external auth """ request = self.request_factory.post(self.url, self.params) # now indicate we are doing ext_auth by setting 'ExternalAuthMap' in the session. request.session = import_module(settings.SESSION_ENGINE).SessionStore() # empty session extauth = ExternalAuthMap(external_id='withmap@stanford.edu', external_email='withmap@stanford.edu', internal_password=self.params['password'], external_domain='shib:https://idp.stanford.edu/') request.session['ExternalAuthMap'] = extauth request.user = AnonymousUser() mako_middleware_process_request(request) with mock.patch('django.contrib.auth.models.User.email_user') as mock_send_mail: student.views.create_account(request) # check that send_mail is called if bypass_activation_email: self.assertFalse(mock_send_mail.called) else: self.assertTrue(mock_send_mail.called) @unittest.skipUnless(settings.FEATURES.get('AUTH_USE_SHIB'), "AUTH_USE_SHIB not set") @mock.patch.dict(settings.FEATURES, {'BYPASS_ACTIVATION_EMAIL_FOR_EXTAUTH': True, 'AUTOMATIC_AUTH_FOR_TESTING': False}) def test_extauth_bypass_sending_activation_email_with_bypass(self): """ Tests user creation without sending activation email when settings.FEATURES['BYPASS_ACTIVATION_EMAIL_FOR_EXTAUTH']=True and doing external auth """ self.base_extauth_bypass_sending_activation_email(True) @unittest.skipUnless(settings.FEATURES.get('AUTH_USE_SHIB'), "AUTH_USE_SHIB not set") @mock.patch.dict(settings.FEATURES, {'BYPASS_ACTIVATION_EMAIL_FOR_EXTAUTH': False, 'AUTOMATIC_AUTH_FOR_TESTING': False}) def test_extauth_bypass_sending_activation_email_without_bypass(self): """ Tests user creation without sending activation email when settings.FEATURES['BYPASS_ACTIVATION_EMAIL_FOR_EXTAUTH']=False and doing external auth """ self.base_extauth_bypass_sending_activation_email(False) @unittest.skipUnless(settings.FEATURES.get('AUTH_USE_SHIB'), "AUTH_USE_SHIB not set") @mock.patch.dict(settings.FEATURES, {'BYPASS_ACTIVATION_EMAIL_FOR_EXTAUTH': False, 'AUTOMATIC_AUTH_FOR_TESTING': False, 'SKIP_EMAIL_VALIDATION': True}) def test_extauth_bypass_sending_activation_email_without_bypass(self): """ Tests user creation without sending activation email when settings.FEATURES['BYPASS_ACTIVATION_EMAIL_FOR_EXTAUTH']=False and doing external auth """ self.base_extauth_bypass_sending_activation_email(True) @ddt.data(True, False) def test_discussions_email_digest_pref(self, digest_enabled): with mock.patch.dict("student.models.settings.FEATURES", {"ENABLE_DISCUSSION_EMAIL_DIGEST": digest_enabled}): response = self.client.post(self.url, self.params) self.assertEqual(response.status_code, 200) user = User.objects.get(username=self.username) preference = get_user_preference(user, NOTIFICATION_PREF_KEY) if digest_enabled: self.assertIsNotNone(preference) else: self.assertIsNone(preference) @ddt.ddt class TestCreateAccountValidation(TestCase): """ Test validation of various parameters in the create_account view """ def setUp(self): super(TestCreateAccountValidation, self).setUp() self.url = reverse("create_account") self.minimal_params = { "username": "test_username", "email": "test_email@example.com", "password": "test_password", "name": "Test Name", "honor_code": "true", "terms_of_service": "true", } def assert_success(self, params): """ Request account creation with the given params and assert that the response properly indicates success """ response = self.client.post(self.url, params) self.assertEqual(response.status_code, 200) response_data = json.loads(response.content) self.assertTrue(response_data["success"]) def assert_error(self, params, expected_field, expected_value): """ Request account creation with the given params and assert that the response properly indicates an error with the given field and value """ response = self.client.post(self.url, params) self.assertEqual(response.status_code, 400) response_data = json.loads(response.content) self.assertFalse(response_data["success"]) self.assertEqual(response_data["field"], expected_field) self.assertEqual(response_data["value"], expected_value) def test_minimal_success(self): self.assert_success(self.minimal_params) def test_username(self): params = dict(self.minimal_params) def assert_username_error(expected_error): """ Assert that requesting account creation results in the expected error """ self.assert_error(params, "username", expected_error) # Missing del params["username"] assert_username_error("Username must be minimum of two characters long") # Empty, too short for username in ["", "a"]: params["username"] = username assert_username_error("Username must be minimum of two characters long") # Too long params["username"] = "this_username_has_31_characters" assert_username_error("Username cannot be more than 30 characters long") # Invalid params["username"] = "invalid username" assert_username_error("Usernames must contain only letters, numbers, underscores (_), and hyphens (-).") def test_email(self): params = dict(self.minimal_params) def assert_email_error(expected_error): """ Assert that requesting account creation results in the expected error """ self.assert_error(params, "email", expected_error) # Missing del params["email"] assert_email_error("A properly formatted e-mail is required") # Empty, too short for email in ["", "a"]: params["email"] = email assert_email_error("A properly formatted e-mail is required") # Too long params["email"] = "this_email_address_has_76_characters_in_it_so_it_is_unacceptable@example.com" assert_email_error("Email cannot be more than 75 characters long") # Invalid params["email"] = "not_an_email_address" assert_email_error("A properly formatted e-mail is required") def test_password(self): params = dict(self.minimal_params) def assert_password_error(expected_error): """ Assert that requesting account creation results in the expected error """ self.assert_error(params, "password", expected_error) # Missing del params["password"] assert_password_error("A valid password is required") # Empty, too short for password in ["", "a"]: params["password"] = password assert_password_error("A valid password is required") # Password policy is tested elsewhere # Matching username params["username"] = params["password"] = "test_username_and_password" assert_password_error("Username and password fields cannot match") def test_name(self): params = dict(self.minimal_params) def assert_name_error(expected_error): """ Assert that requesting account creation results in the expected error """ self.assert_error(params, "name", expected_error) # Missing del params["name"] assert_name_error("Your legal name must be a minimum of two characters long") # Empty, too short for name in ["", "a"]: params["name"] = name assert_name_error("Your legal name must be a minimum of two characters long") def test_honor_code(self): params = dict(self.minimal_params) def assert_honor_code_error(expected_error): """ Assert that requesting account creation results in the expected error """ self.assert_error(params, "honor_code", expected_error) with override_settings(REGISTRATION_EXTRA_FIELDS={"honor_code": "required"}): # Missing del params["honor_code"] assert_honor_code_error("To enroll, you must follow the honor code.") # Empty, invalid for honor_code in ["", "false", "not_boolean"]: params["honor_code"] = honor_code assert_honor_code_error("To enroll, you must follow the honor code.") # True params["honor_code"] = "tRUe" self.assert_success(params) with override_settings(REGISTRATION_EXTRA_FIELDS={"honor_code": "optional"}): # Missing del params["honor_code"] # Need to change username/email because user was created above params["username"] = "another_test_username" params["email"] = "another_test_email@example.com" self.assert_success(params) def test_terms_of_service(self): params = dict(self.minimal_params) def assert_terms_of_service_error(expected_error): """ Assert that requesting account creation results in the expected error """ self.assert_error(params, "terms_of_service", expected_error) # Missing del params["terms_of_service"] assert_terms_of_service_error("You must accept the terms of service.") # Empty, invalid for terms_of_service in ["", "false", "not_boolean"]: params["terms_of_service"] = terms_of_service assert_terms_of_service_error("You must accept the terms of service.") # True params["terms_of_service"] = "tRUe" self.assert_success(params) @ddt.data( ("level_of_education", 1, "A level of education is required"), ("gender", 1, "Your gender is required"), ("year_of_birth", 2, "Your year of birth is required"), ("mailing_address", 2, "Your mailing address is required"), ("goals", 2, "A description of your goals is required"), ("city", 2, "A city is required"), ("country", 2, "A country is required"), ("custom_field", 2, "You are missing one or more required fields") ) @ddt.unpack def test_extra_fields(self, field, min_length, expected_error): params = dict(self.minimal_params) def assert_extra_field_error(): """ Assert that requesting account creation results in the expected error """ self.assert_error(params, field, expected_error) with override_settings(REGISTRATION_EXTRA_FIELDS={field: "required"}): # Missing assert_extra_field_error() # Empty params[field] = "" assert_extra_field_error() # Too short if min_length > 1: params[field] = "a" assert_extra_field_error() @mock.patch.dict("student.models.settings.FEATURES", {"ENABLE_DISCUSSION_SERVICE": True}) @mock.patch("lms.lib.comment_client.User.base_url", TEST_CS_URL) @mock.patch("lms.lib.comment_client.utils.requests.request", return_value=mock.Mock(status_code=200, text='{}')) class TestCreateCommentsServiceUser(TransactionTestCase): def setUp(self): super(TestCreateCommentsServiceUser, self).setUp() self.username = "test_user" self.url = reverse("create_account") self.params = { "username": self.username, "email": "test@example.org", "password": "testpass", "name": "Test User", "honor_code": "true", "terms_of_service": "true", } def test_cs_user_created(self, request): "If user account creation succeeds, we should create a comments service user" response = self.client.post(self.url, self.params) self.assertEqual(response.status_code, 200) self.assertTrue(request.called) args, kwargs = request.call_args self.assertEqual(args[0], 'put') self.assertTrue(args[1].startswith(TEST_CS_URL)) self.assertEqual(kwargs['data']['username'], self.params['username']) @mock.patch("student.models.Registration.register", side_effect=Exception) def test_cs_user_not_created(self, register, request): "If user account creation fails, we should not create a comments service user" try: response = self.client.post(self.url, self.params) except: pass with self.assertRaises(User.DoesNotExist): User.objects.get(username=self.username) self.assertTrue(register.called) self.assertFalse(request.called)
agpl-3.0
worsht/antlr4
runtime/Python2/src/antlr4/Parser.py
8
22742
# [The "BSD license"] # Copyright (c) 2012 Terence Parr # Copyright (c) 2012 Sam Harwell # Copyright (c) 2014 Eric Vergnaud # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # 1. Redistributions of source code must retain the above copyright # notice, self list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, self list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. The name of the author may not be used to endorse or promote products # derived from self software without specific prior written permission. # # self SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # self SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. from __future__ import print_function from antlr4.error.ErrorStrategy import DefaultErrorStrategy from antlr4.Recognizer import Recognizer from antlr4.Token import Token from antlr4.Lexer import Lexer from antlr4.atn.ATNDeserializer import ATNDeserializer from antlr4.atn.ATNDeserializationOptions import ATNDeserializationOptions from antlr4.error.Errors import UnsupportedOperationException from antlr4.tree.ParseTreePatternMatcher import ParseTreePatternMatcher from antlr4.tree.Tree import ParseTreeListener class TraceListener(ParseTreeListener): def __init__(self, parser): self._parser = parser def enterEveryRule(self, ctx): print("enter " + self._parser.ruleNames[ctx.getRuleIndex()] + ", LT(1)=" + self._parser._input.LT(1).text) def visitTerminal(self, node): print("consume " + str(node.symbol) + " rule " + self._parser.ruleNames[self._parser._ctx.getRuleIndex()]) def visitErrorNode(self, node): pass def exitEveryRule(self, ctx): print("exit " + self._parser.ruleNames[ctx.getRuleIndex()] + ", LT(1)=" + self._parser._input.LT(1).text) # self is all the parsing support code essentially; most of it is error recovery stuff.# class Parser (Recognizer): # self field maps from the serialized ATN string to the deserialized {@link ATN} with # bypass alternatives. # # @see ATNDeserializationOptions#isGenerateRuleBypassTransitions() # bypassAltsAtnCache = dict() def __init__(self, input): super(Parser, self).__init__() # The input stream. self._input = None # The error handling strategy for the parser. The default value is a new # instance of {@link DefaultErrorStrategy}. self._errHandler = DefaultErrorStrategy() self._precedenceStack = list() self._precedenceStack.append(0) # The {@link ParserRuleContext} object for the currently executing rule. # self is always non-null during the parsing process. self._ctx = None # Specifies whether or not the parser should construct a parse tree during # the parsing process. The default value is {@code true}. self.buildParseTrees = True # When {@link #setTrace}{@code (true)} is called, a reference to the # {@link TraceListener} is stored here so it can be easily removed in a # later call to {@link #setTrace}{@code (false)}. The listener itself is # implemented as a parser listener so self field is not directly used by # other parser methods. self._tracer = None # The list of {@link ParseTreeListener} listeners registered to receive # events during the parse. self._parseListeners = None # The number of syntax errors reported during parsing. self value is # incremented each time {@link #notifyErrorListeners} is called. self._syntaxErrors = 0 self.setInputStream(input) # reset the parser's state# def reset(self): if self._input is not None: self._input.seek(0) self._errHandler.reset(self) self._ctx = None self._syntaxErrors = 0 self.setTrace(False) self._precedenceStack = list() self._precedenceStack.append(0) if self._interp is not None: self._interp.reset() # Match current input symbol against {@code ttype}. If the symbol type # matches, {@link ANTLRErrorStrategy#reportMatch} and {@link #consume} are # called to complete the match process. # # <p>If the symbol type does not match, # {@link ANTLRErrorStrategy#recoverInline} is called on the current error # strategy to attempt recovery. If {@link #getBuildParseTree} is # {@code true} and the token index of the symbol returned by # {@link ANTLRErrorStrategy#recoverInline} is -1, the symbol is added to # the parse tree by calling {@link ParserRuleContext#addErrorNode}.</p> # # @param ttype the token type to match # @return the matched symbol # @throws RecognitionException if the current input symbol did not match # {@code ttype} and the error strategy could not recover from the # mismatched symbol def match(self, ttype): t = self.getCurrentToken() if t.type==ttype: self._errHandler.reportMatch(self) self.consume() else: t = self._errHandler.recoverInline(self) if self.buildParseTrees and t.tokenIndex==-1: # we must have conjured up a new token during single token insertion # if it's not the current symbol self._ctx.addErrorNode(t) return t # Match current input symbol as a wildcard. If the symbol type matches # (i.e. has a value greater than 0), {@link ANTLRErrorStrategy#reportMatch} # and {@link #consume} are called to complete the match process. # # <p>If the symbol type does not match, # {@link ANTLRErrorStrategy#recoverInline} is called on the current error # strategy to attempt recovery. If {@link #getBuildParseTree} is # {@code true} and the token index of the symbol returned by # {@link ANTLRErrorStrategy#recoverInline} is -1, the symbol is added to # the parse tree by calling {@link ParserRuleContext#addErrorNode}.</p> # # @return the matched symbol # @throws RecognitionException if the current input symbol did not match # a wildcard and the error strategy could not recover from the mismatched # symbol def matchWildcard(self): t = self.getCurrentToken() if t.type > 0: self._errHandler.reportMatch(self) self.consume() else: t = self._errHandler.recoverInline(self) if self.buildParseTrees and t.tokenIndex == -1: # we must have conjured up a new token during single token insertion # if it's not the current symbol self._ctx.addErrorNode(t) return t def getParseListeners(self): return list() if self._parseListeners is None else self._parseListeners # Registers {@code listener} to receive events during the parsing process. # # <p>To support output-preserving grammar transformations (including but not # limited to left-recursion removal, automated left-factoring, and # optimized code generation), calls to listener methods during the parse # may differ substantially from calls made by # {@link ParseTreeWalker#DEFAULT} used after the parse is complete. In # particular, rule entry and exit events may occur in a different order # during the parse than after the parser. In addition, calls to certain # rule entry methods may be omitted.</p> # # <p>With the following specific exceptions, calls to listener events are # <em>deterministic</em>, i.e. for identical input the calls to listener # methods will be the same.</p> # # <ul> # <li>Alterations to the grammar used to generate code may change the # behavior of the listener calls.</li> # <li>Alterations to the command line options passed to ANTLR 4 when # generating the parser may change the behavior of the listener calls.</li> # <li>Changing the version of the ANTLR Tool used to generate the parser # may change the behavior of the listener calls.</li> # </ul> # # @param listener the listener to add # # @throws NullPointerException if {@code} listener is {@code null} # def addParseListener(self, listener): if listener is None: raise ReferenceError("listener") if self._parseListeners is None: self._parseListeners = [] self._parseListeners.append(listener) # # Remove {@code listener} from the list of parse listeners. # # <p>If {@code listener} is {@code null} or has not been added as a parse # listener, self method does nothing.</p> # @param listener the listener to remove # def removeParseListener(self, listener): if self._parseListeners is not None: self._parseListeners.remove(listener) if len(self._parseListeners)==0: self._parseListeners = None # Remove all parse listeners. def removeParseListeners(self): self._parseListeners = None # Notify any parse listeners of an enter rule event. def triggerEnterRuleEvent(self): if self._parseListeners is not None: for listener in self._parseListeners: listener.enterEveryRule(self._ctx) self._ctx.enterRule(listener) # # Notify any parse listeners of an exit rule event. # # @see #addParseListener # def triggerExitRuleEvent(self): if self._parseListeners is not None: # reverse order walk of listeners for listener in reversed(self._parseListeners): self._ctx.exitRule(listener) listener.exitEveryRule(self._ctx) def getTokenFactory(self): return self._input.tokenSource._factory # Tell our token source and error strategy about a new way to create tokens.# def setTokenFactory(self, factory): self._input.tokenSource._factory = factory # The ATN with bypass alternatives is expensive to create so we create it # lazily. # # @throws UnsupportedOperationException if the current parser does not # implement the {@link #getSerializedATN()} method. # def getATNWithBypassAlts(self): serializedAtn = self.getSerializedATN() if serializedAtn is None: raise UnsupportedOperationException("The current parser does not support an ATN with bypass alternatives.") result = self.bypassAltsAtnCache.get(serializedAtn, None) if result is None: deserializationOptions = ATNDeserializationOptions() deserializationOptions.generateRuleBypassTransitions = True result = ATNDeserializer(deserializationOptions).deserialize(serializedAtn) self.bypassAltsAtnCache[serializedAtn] = result return result # The preferred method of getting a tree pattern. For example, here's a # sample use: # # <pre> # ParseTree t = parser.expr(); # ParseTreePattern p = parser.compileParseTreePattern("&lt;ID&gt;+0", MyParser.RULE_expr); # ParseTreeMatch m = p.match(t); # String id = m.get("ID"); # </pre> # def compileParseTreePattern(self, pattern, patternRuleIndex, lexer = None): if lexer is None: if self.getTokenStream() is not None: tokenSource = self.getTokenStream().getTokenSource() if isinstance( tokenSource, Lexer ): lexer = tokenSource if lexer is None: raise UnsupportedOperationException("Parser can't discover a lexer to use") m = ParseTreePatternMatcher(lexer, self) return m.compile(pattern, patternRuleIndex) def getInputStream(self): return self.getTokenStream() def setInputStream(self, input): self.setTokenStream(input) def getTokenStream(self): return self._input # Set the token stream and reset the parser.# def setTokenStream(self, input): self._input = None self.reset() self._input = input # Match needs to return the current input symbol, which gets put # into the label for the associated token ref; e.g., x=ID. # def getCurrentToken(self): return self._input.LT(1) def notifyErrorListeners(self, msg, offendingToken = None, e = None): if offendingToken is None: offendingToken = self.getCurrentToken() self._syntaxErrors += 1 line = offendingToken.line column = offendingToken.column listener = self.getErrorListenerDispatch() listener.syntaxError(self, offendingToken, line, column, msg, e) # # Consume and return the {@linkplain #getCurrentToken current symbol}. # # <p>E.g., given the following input with {@code A} being the current # lookahead symbol, self function moves the cursor to {@code B} and returns # {@code A}.</p> # # <pre> # A B # ^ # </pre> # # If the parser is not in error recovery mode, the consumed symbol is added # to the parse tree using {@link ParserRuleContext#addChild(Token)}, and # {@link ParseTreeListener#visitTerminal} is called on any parse listeners. # If the parser <em>is</em> in error recovery mode, the consumed symbol is # added to the parse tree using # {@link ParserRuleContext#addErrorNode(Token)}, and # {@link ParseTreeListener#visitErrorNode} is called on any parse # listeners. # def consume(self): o = self.getCurrentToken() if o.type != Token.EOF: self.getInputStream().consume() hasListener = self._parseListeners is not None and len(self._parseListeners)>0 if self.buildParseTrees or hasListener: if self._errHandler.inErrorRecoveryMode(self): node = self._ctx.addErrorNode(o) else: node = self._ctx.addTokenNode(o) if hasListener: for listener in self._parseListeners: listener.visitTerminal(node) return o def addContextToParseTree(self): # add current context to parent if we have a parent if self._ctx.parentCtx is not None: self._ctx.parentCtx.addChild(self._ctx) # Always called by generated parsers upon entry to a rule. Access field # {@link #_ctx} get the current context. # def enterRule(self, localctx , state , ruleIndex ): self.state = state self._ctx = localctx self._ctx.start = self._input.LT(1) if self.buildParseTrees: self.addContextToParseTree() if self._parseListeners is not None: self.triggerEnterRuleEvent() def exitRule(self): self._ctx.stop = self._input.LT(-1) # trigger event on _ctx, before it reverts to parent if self._parseListeners is not None: self.triggerExitRuleEvent() self.state = self._ctx.invokingState self._ctx = self._ctx.parentCtx def enterOuterAlt(self, localctx, altNum): # if we have new localctx, make sure we replace existing ctx # that is previous child of parse tree if self.buildParseTrees and self._ctx != localctx: if self._ctx.parentCtx is not None: self._ctx.parentCtx.removeLastChild() self._ctx.parentCtx.addChild(localctx) self._ctx = localctx # Get the precedence level for the top-most precedence rule. # # @return The precedence level for the top-most precedence rule, or -1 if # the parser context is not nested within a precedence rule. # def getPrecedence(self): if len(self._precedenceStack)==0: return -1 else: return self._precedenceStack[-1] def enterRecursionRule(self, localctx, state, ruleIndex, precedence): self.state = state self._precedenceStack.append(precedence) self._ctx = localctx self._ctx.start = self._input.LT(1) if self._parseListeners is not None: self.triggerEnterRuleEvent() # simulates rule entry for left-recursive rules # # Like {@link #enterRule} but for recursive rules. # def pushNewRecursionContext(self, localctx, state, ruleIndex): previous = self._ctx previous.parentCtx = localctx previous.invokingState = state previous.stop = self._input.LT(-1) self._ctx = localctx self._ctx.start = previous.start if self.buildParseTrees: self._ctx.addChild(previous) if self._parseListeners is not None: self.triggerEnterRuleEvent() # simulates rule entry for left-recursive rules def unrollRecursionContexts(self, parentCtx): self._precedenceStack.pop() self._ctx.stop = self._input.LT(-1) retCtx = self._ctx # save current ctx (return value) # unroll so _ctx is as it was before call to recursive method if self._parseListeners is not None: while self._ctx is not parentCtx: self.triggerExitRuleEvent() self._ctx = self._ctx.parentCtx else: self._ctx = parentCtx # hook into tree retCtx.parentCtx = parentCtx if self.buildParseTrees and parentCtx is not None: # add return ctx into invoking rule's tree parentCtx.addChild(retCtx) def getInvokingContext(self, ruleIndex): ctx = self._ctx while ctx is not None: if ctx.ruleIndex == ruleIndex: return ctx ctx = ctx.parentCtx return None def precpred(self, localctx , precedence): return precedence >= self._precedenceStack[-1] def inContext(self, context): # TODO: useful in parser? return False # # Checks whether or not {@code symbol} can follow the current state in the # ATN. The behavior of self method is equivalent to the following, but is # implemented such that the complete context-sensitive follow set does not # need to be explicitly constructed. # # <pre> # return getExpectedTokens().contains(symbol); # </pre> # # @param symbol the symbol type to check # @return {@code true} if {@code symbol} can follow the current state in # the ATN, otherwise {@code false}. # def isExpectedToken(self, symbol): atn = self._interp.atn ctx = self._ctx s = atn.states[self.state] following = atn.nextTokens(s) if symbol in following: return True if not Token.EPSILON in following: return False while ctx is not None and ctx.invokingState>=0 and Token.EPSILON in following: invokingState = atn.states[ctx.invokingState] rt = invokingState.transitions[0] following = atn.nextTokens(rt.followState) if symbol in following: return True ctx = ctx.parentCtx if Token.EPSILON in following and symbol == Token.EOF: return True else: return False # Computes the set of input symbols which could follow the current parser # state and context, as given by {@link #getState} and {@link #getContext}, # respectively. # # @see ATN#getExpectedTokens(int, RuleContext) # def getExpectedTokens(self): return self._interp.atn.getExpectedTokens(self.state, self._ctx) def getExpectedTokensWithinCurrentRule(self): atn = self._interp.atn s = atn.states[self.state] return atn.nextTokens(s) # Get a rule's index (i.e., {@code RULE_ruleName} field) or -1 if not found.# def getRuleIndex(self, ruleName): ruleIndex = self.getRuleIndexMap().get(ruleName, None) if ruleIndex is not None: return ruleIndex else: return -1 # Return List&lt;String&gt; of the rule names in your parser instance # leading up to a call to the current rule. You could override if # you want more details such as the file/line info of where # in the ATN a rule is invoked. # # this is very useful for error messages. # def getRuleInvocationStack(self, p=None): if p is None: p = self._ctx stack = list() while p is not None: # compute what follows who invoked us ruleIndex = p.getRuleIndex() if ruleIndex<0: stack.append("n/a") else: stack.append(self.ruleNames[ruleIndex]) p = p.parentCtx return stack # For debugging and other purposes.# def getDFAStrings(self): return [ unicode(dfa) for dfa in self._interp.decisionToDFA] # For debugging and other purposes.# def dumpDFA(self): seenOne = False for i in range(0, len(self._interp.decisionToDFA)): dfa = self._interp.decisionToDFA[i] if len(dfa.states)>0: if seenOne: print() print("Decision " + str(dfa.decision) + ":") print(dfa.toString(self.literalNames, self.symbolicNames), end='') seenOne = True def getSourceName(self): return self._input.sourceName # During a parse is sometimes useful to listen in on the rule entry and exit # events as well as token matches. self is for quick and dirty debugging. # def setTrace(self, trace): if not trace: self.removeParseListener(self._tracer) self._tracer = None else: if self._tracer is not None: self.removeParseListener(self._tracer) self._tracer = TraceListener(self) self.addParseListener(self._tracer)
bsd-3-clause
pimoroni/unicorn-hat-hd
examples/show-png.py
1
1382
#!/usr/bin/env python import time from sys import exit try: from PIL import Image except ImportError: exit('This script requires the pillow module\nInstall with: sudo pip install pillow') import unicornhathd print("""Unicorn HAT HD: Show a PNG image! This basic example shows use of the Python Pillow library. The tiny 16x16 bosses in lofi.png are from Oddball: http://forums.tigsource.com/index.php?topic=8834.0 Licensed under Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Press Ctrl+C to exit! """) unicornhathd.rotation(0) unicornhathd.brightness(0.6) width, height = unicornhathd.get_shape() img = Image.open('lofi.png') try: while True: for o_x in range(int(img.size[0] / width)): for o_y in range(int(img.size[1] / height)): valid = False for x in range(width): for y in range(height): pixel = img.getpixel(((o_x * width) + y, (o_y * height) + x)) r, g, b = int(pixel[0]), int(pixel[1]), int(pixel[2]) if r or g or b: valid = True unicornhathd.set_pixel(x, y, r, g, b) if valid: unicornhathd.show() time.sleep(0.5) except KeyboardInterrupt: unicornhathd.off()
mit
arifsetiawan/edx-platform
common/test/acceptance/pages/studio/settings_advanced.py
42
6648
""" Course Advanced Settings page """ from bok_choy.promise import EmptyPromise from .course_page import CoursePage from .utils import press_the_notification_button, type_in_codemirror, get_codemirror_value KEY_CSS = '.key h3.title' UNDO_BUTTON_SELECTOR = ".action-item .action-undo" MANUAL_BUTTON_SELECTOR = ".action-item .action-cancel" MODAL_SELECTOR = ".validation-error-modal-content" ERROR_ITEM_NAME_SELECTOR = ".error-item-title strong" ERROR_ITEM_CONTENT_SELECTOR = ".error-item-message" SETTINGS_NAME_SELECTOR = ".is-not-editable" class AdvancedSettingsPage(CoursePage): """ Course Advanced Settings page. """ url_path = "settings/advanced" def is_browser_on_page(self): def _is_finished_loading(): return len(self.q(css='.course-advanced-policy-list-item')) > 0 EmptyPromise(_is_finished_loading, 'Finished rendering the advanced policy items.').fulfill() return self.q(css='body.advanced').present def wait_for_modal_load(self): """ Wait for validation response from the server, and make sure that the validation error modal pops up. This method should only be called when it is guaranteed that there're validation errors in the settings changes. """ self.wait_for_ajax() self.wait_for_element_presence(MODAL_SELECTOR, 'Validation Modal is present') def refresh_and_wait_for_load(self): """ Refresh the page and wait for all resources to load. """ self.browser.refresh() self.wait_for_page() def undo_changes_via_modal(self): """ Trigger clicking event of the undo changes button in the modal. Wait for the undoing process to load via ajax call. """ self.q(css=UNDO_BUTTON_SELECTOR).click() self.wait_for_ajax() def trigger_manual_changes(self): """ Trigger click event of the manual changes button in the modal. No need to wait for any ajax. """ self.q(css=MANUAL_BUTTON_SELECTOR).click() def is_validation_modal_present(self): """ Checks if the validation modal is present. """ return self.q(css=MODAL_SELECTOR).present def get_error_item_names(self): """ Returns a list of display names of all invalid settings. """ return self.q(css=ERROR_ITEM_NAME_SELECTOR).text def get_error_item_messages(self): """ Returns a list of error messages of all invalid settings. """ return self.q(css=ERROR_ITEM_CONTENT_SELECTOR).text def _get_index_of(self, expected_key): for i, element in enumerate(self.q(css=KEY_CSS)): # Sometimes get stale reference if I hold on to the array of elements key = self.q(css=KEY_CSS).nth(i).text[0] if key == expected_key: return i return -1 def save(self): press_the_notification_button(self, "Save") def cancel(self): press_the_notification_button(self, "Cancel") def set(self, key, new_value): index = self._get_index_of(key) type_in_codemirror(self, index, new_value) self.save() def get(self, key): index = self._get_index_of(key) return get_codemirror_value(self, index) def set_values(self, key_value_map): """ Make multiple settings changes and save them. """ for key, value in key_value_map.iteritems(): index = self._get_index_of(key) type_in_codemirror(self, index, value) self.save() def get_values(self, key_list): """ Get a key-value dictionary of all keys in the given list. """ result_map = {} for key in key_list: index = self._get_index_of(key) val = get_codemirror_value(self, index) result_map[key] = val return result_map @property def displayed_settings_names(self): """ Returns all settings displayed on the advanced settings page/screen/modal/whatever We call it 'name', but it's really whatever is embedded in the 'id' element for each field """ query = self.q(css=SETTINGS_NAME_SELECTOR) return query.attrs('id') @property def expected_settings_names(self): """ Returns a list of settings expected to be displayed on the Advanced Settings screen Should match the list of settings found in cms/djangoapps/models/settings/course_metadata.py If a new setting is added to the metadata list, this test will fail and you must update it. Basically this guards against accidental exposure of a field on the Advanced Settings screen """ return [ 'advanced_modules', 'allow_anonymous', 'allow_anonymous_to_peers', 'allow_public_wiki_access', 'cert_html_view_overrides', 'cert_name_long', 'cert_name_short', 'certificates_display_behavior', 'course_image', 'cosmetic_display_price', 'advertised_start', 'announcement', 'display_name', 'info_sidebar_name', 'is_new', 'ispublic', 'issue_badges', 'max_student_enrollments_allowed', 'no_grade', 'display_coursenumber', 'display_organization', 'end_of_course_survey_url', 'catalog_visibility', 'chrome', 'days_early_for_beta', 'default_tab', 'disable_progress_graph', 'discussion_blackouts', 'discussion_sort_alpha', 'discussion_topics', 'due', 'due_date_display_format', 'edxnotes', 'use_latex_compiler', 'video_speed_optimizations', 'enrollment_domain', 'html_textbooks', 'invitation_only', 'lti_passports', 'matlab_api_key', 'max_attempts', 'mobile_available', 'rerandomize', 'remote_gradebook', 'annotation_token_secret', 'showanswer', 'show_calculator', 'show_chat', 'show_reset_button', 'static_asset_path', 'text_customization', 'annotation_storage_url', 'social_sharing_url', 'video_bumper', 'cert_html_view_enabled', 'enable_proctored_exams', ]
agpl-3.0
stuartlangridge/raspi-recorder
listener_daemon.py
1
2508
import threading, time, subprocess from bluetooth import * server_sock=BluetoothSocket( RFCOMM ) server_sock.bind(("",PORT_ANY)) server_sock.listen(1) port = server_sock.getsockname()[1] uuid = "c3091f5f-7e2f-4908-b628-18231dfb5034" advertise_service( server_sock, "PiRecorder", service_id = uuid, service_classes = [ uuid, SERIAL_PORT_CLASS ], profiles = [ SERIAL_PORT_PROFILE ], ) print "Waiting for connection on RFCOMM channel %d" % port client_sock, client_info = server_sock.accept() print "Accepted connection from ", client_info lock = threading.Lock() def mainthread(sock): try: with lock: sock.send("\r\nWelcome to recorder. [1] start recording, [2] stop.\r\n\r\n") while True: data = sock.recv(1) if len(data) == 0: break if data == "1": with lock: sock.send("Starting sound recorder\r\n") os.system("supervisorctl -c ./supervisor.conf start sound_recorder") elif data == "2": with lock: sock.send("Stopping sound recorder\r\n") os.system("supervisorctl -c ./supervisor.conf stop sound_recorder") else: print "received [%s]" % data with lock: output = "unrecognised [%s]\r\n" % (data,) sock.send(output) except IOError: print "got io error" def heartbeat(sock): while True: time.sleep(5) o = subprocess.check_output(["supervisorctl", "-c", os.path.join(os.path.split(__file__)[0], "supervisor.conf"), "status"]) procs = {} for parts in [x.split() for x in o.split("\n")]: if len(parts) > 1: procs[parts[0]] = parts[1] sr = procs.get("sound_recorder", "ABSENT") svfs = os.statvfs(".") bytes_remaining = svfs.f_frsize * svfs.f_bavail bytes_total = svfs.f_frsize * svfs.f_blocks with lock: sock.send("heartbeat %s %s %s\r\n" % ( sr, bytes_remaining, bytes_total)) mainthread = threading.Thread(target=mainthread, args=(client_sock,)) mainthread.start() heartbeatthread = threading.Thread(target=heartbeat, args=(client_sock,)) heartbeatthread.setDaemon(True) heartbeatthread.start() mainthread.join() print "disconnected" client_sock.close() server_sock.close() print "all done"
mit
dongjoon-hyun/docker
vendor/src/github.com/hashicorp/go-msgpack/codec/msgpack_test.py
1232
3478
#!/usr/bin/env python # This will create golden files in a directory passed to it. # A Test calls this internally to create the golden files # So it can process them (so we don't have to checkin the files). import msgpack, msgpackrpc, sys, os, threading def get_test_data_list(): # get list with all primitive types, and a combo type l0 = [ -8, -1616, -32323232, -6464646464646464, 192, 1616, 32323232, 6464646464646464, 192, -3232.0, -6464646464.0, 3232.0, 6464646464.0, False, True, None, "someday", "", "bytestring", 1328176922000002000, -2206187877999998000, 0, -6795364578871345152 ] l1 = [ { "true": True, "false": False }, { "true": "True", "false": False, "uint16(1616)": 1616 }, { "list": [1616, 32323232, True, -3232.0, {"TRUE":True, "FALSE":False}, [True, False] ], "int32":32323232, "bool": True, "LONG STRING": "123456789012345678901234567890123456789012345678901234567890", "SHORT STRING": "1234567890" }, { True: "true", 8: False, "false": 0 } ] l = [] l.extend(l0) l.append(l0) l.extend(l1) return l def build_test_data(destdir): l = get_test_data_list() for i in range(len(l)): packer = msgpack.Packer() serialized = packer.pack(l[i]) f = open(os.path.join(destdir, str(i) + '.golden'), 'wb') f.write(serialized) f.close() def doRpcServer(port, stopTimeSec): class EchoHandler(object): def Echo123(self, msg1, msg2, msg3): return ("1:%s 2:%s 3:%s" % (msg1, msg2, msg3)) def EchoStruct(self, msg): return ("%s" % msg) addr = msgpackrpc.Address('localhost', port) server = msgpackrpc.Server(EchoHandler()) server.listen(addr) # run thread to stop it after stopTimeSec seconds if > 0 if stopTimeSec > 0: def myStopRpcServer(): server.stop() t = threading.Timer(stopTimeSec, myStopRpcServer) t.start() server.start() def doRpcClientToPythonSvc(port): address = msgpackrpc.Address('localhost', port) client = msgpackrpc.Client(address, unpack_encoding='utf-8') print client.call("Echo123", "A1", "B2", "C3") print client.call("EchoStruct", {"A" :"Aa", "B":"Bb", "C":"Cc"}) def doRpcClientToGoSvc(port): # print ">>>> port: ", port, " <<<<<" address = msgpackrpc.Address('localhost', port) client = msgpackrpc.Client(address, unpack_encoding='utf-8') print client.call("TestRpcInt.Echo123", ["A1", "B2", "C3"]) print client.call("TestRpcInt.EchoStruct", {"A" :"Aa", "B":"Bb", "C":"Cc"}) def doMain(args): if len(args) == 2 and args[0] == "testdata": build_test_data(args[1]) elif len(args) == 3 and args[0] == "rpc-server": doRpcServer(int(args[1]), int(args[2])) elif len(args) == 2 and args[0] == "rpc-client-python-service": doRpcClientToPythonSvc(int(args[1])) elif len(args) == 2 and args[0] == "rpc-client-go-service": doRpcClientToGoSvc(int(args[1])) else: print("Usage: msgpack_test.py " + "[testdata|rpc-server|rpc-client-python-service|rpc-client-go-service] ...") if __name__ == "__main__": doMain(sys.argv[1:])
apache-2.0
joelddiaz/openshift-tools
openshift/installer/vendored/openshift-ansible-3.7.52-1/roles/lib_openshift/src/lib/base.py
6
21246
# pylint: skip-file # flake8: noqa # pylint: disable=too-many-lines # noqa: E301,E302,E303,T001 class OpenShiftCLIError(Exception): '''Exception class for openshiftcli''' pass ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')] def locate_oc_binary(): ''' Find and return oc binary file ''' # https://github.com/openshift/openshift-ansible/issues/3410 # oc can be in /usr/local/bin in some cases, but that may not # be in $PATH due to ansible/sudo paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS oc_binary = 'oc' # Use shutil.which if it is available, otherwise fallback to a naive path search try: which_result = shutil.which(oc_binary, path=os.pathsep.join(paths)) if which_result is not None: oc_binary = which_result except AttributeError: for path in paths: if os.path.exists(os.path.join(path, oc_binary)): oc_binary = os.path.join(path, oc_binary) break return oc_binary # pylint: disable=too-few-public-methods class OpenShiftCLI(object): ''' Class to wrap the command line tools ''' def __init__(self, namespace, kubeconfig='/etc/origin/master/admin.kubeconfig', verbose=False, all_namespaces=False): ''' Constructor for OpenshiftCLI ''' self.namespace = namespace self.verbose = verbose self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig) self.all_namespaces = all_namespaces self.oc_binary = locate_oc_binary() # Pylint allows only 5 arguments to be passed. # pylint: disable=too-many-arguments def _replace_content(self, resource, rname, content, force=False, sep='.'): ''' replace the current object with the content ''' res = self._get(resource, rname) if not res['results']: return res fname = Utils.create_tmpfile(rname + '-') yed = Yedit(fname, res['results'][0], separator=sep) changes = [] for key, value in content.items(): changes.append(yed.put(key, value)) if any([change[0] for change in changes]): yed.write() atexit.register(Utils.cleanup, [fname]) return self._replace(fname, force) return {'returncode': 0, 'updated': False} def _replace(self, fname, force=False): '''replace the current object with oc replace''' # We are removing the 'resourceVersion' to handle # a race condition when modifying oc objects yed = Yedit(fname) results = yed.delete('metadata.resourceVersion') if results[0]: yed.write() cmd = ['replace', '-f', fname] if force: cmd.append('--force') return self.openshift_cmd(cmd) def _create_from_content(self, rname, content): '''create a temporary file and then call oc create on it''' fname = Utils.create_tmpfile(rname + '-') yed = Yedit(fname, content=content) yed.write() atexit.register(Utils.cleanup, [fname]) return self._create(fname) def _create(self, fname): '''call oc create on a filename''' return self.openshift_cmd(['create', '-f', fname]) def _delete(self, resource, name=None, selector=None): '''call oc delete on a resource''' cmd = ['delete', resource] if selector is not None: cmd.append('--selector={}'.format(selector)) elif name is not None: cmd.append(name) else: raise OpenShiftCLIError('Either name or selector is required when calling delete.') return self.openshift_cmd(cmd) def _process(self, template_name, create=False, params=None, template_data=None): # noqa: E501 '''process a template template_name: the name of the template to process create: whether to send to oc create after processing params: the parameters for the template template_data: the incoming template's data; instead of a file ''' cmd = ['process'] if template_data: cmd.extend(['-f', '-']) else: cmd.append(template_name) if params: param_str = ["{}={}".format(key, str(value).replace("'", r'"')) for key, value in params.items()] cmd.append('-v') cmd.extend(param_str) results = self.openshift_cmd(cmd, output=True, input_data=template_data) if results['returncode'] != 0 or not create: return results fname = Utils.create_tmpfile(template_name + '-') yed = Yedit(fname, results['results']) yed.write() atexit.register(Utils.cleanup, [fname]) return self.openshift_cmd(['create', '-f', fname]) def _get(self, resource, name=None, selector=None): '''return a resource by name ''' cmd = ['get', resource] if selector is not None: cmd.append('--selector={}'.format(selector)) elif name is not None: cmd.append(name) cmd.extend(['-o', 'json']) rval = self.openshift_cmd(cmd, output=True) # Ensure results are retuned in an array if 'items' in rval: rval['results'] = rval['items'] elif not isinstance(rval['results'], list): rval['results'] = [rval['results']] return rval def _schedulable(self, node=None, selector=None, schedulable=True): ''' perform oadm manage-node scheduable ''' cmd = ['manage-node'] if node: cmd.extend(node) else: cmd.append('--selector={}'.format(selector)) cmd.append('--schedulable={}'.format(schedulable)) return self.openshift_cmd(cmd, oadm=True, output=True, output_type='raw') # noqa: E501 def _list_pods(self, node=None, selector=None, pod_selector=None): ''' perform oadm list pods node: the node in which to list pods selector: the label selector filter if provided pod_selector: the pod selector filter if provided ''' cmd = ['manage-node'] if node: cmd.extend(node) else: cmd.append('--selector={}'.format(selector)) if pod_selector: cmd.append('--pod-selector={}'.format(pod_selector)) cmd.extend(['--list-pods', '-o', 'json']) return self.openshift_cmd(cmd, oadm=True, output=True, output_type='raw') # pylint: disable=too-many-arguments def _evacuate(self, node=None, selector=None, pod_selector=None, dry_run=False, grace_period=None, force=False): ''' perform oadm manage-node evacuate ''' cmd = ['manage-node'] if node: cmd.extend(node) else: cmd.append('--selector={}'.format(selector)) if dry_run: cmd.append('--dry-run') if pod_selector: cmd.append('--pod-selector={}'.format(pod_selector)) if grace_period: cmd.append('--grace-period={}'.format(int(grace_period))) if force: cmd.append('--force') cmd.append('--evacuate') return self.openshift_cmd(cmd, oadm=True, output=True, output_type='raw') def _version(self): ''' return the openshift version''' return self.openshift_cmd(['version'], output=True, output_type='raw') def _import_image(self, url=None, name=None, tag=None): ''' perform image import ''' cmd = ['import-image'] image = '{0}'.format(name) if tag: image += ':{0}'.format(tag) cmd.append(image) if url: cmd.append('--from={0}/{1}'.format(url, image)) cmd.append('-n{0}'.format(self.namespace)) cmd.append('--confirm') return self.openshift_cmd(cmd) def _run(self, cmds, input_data): ''' Actually executes the command. This makes mocking easier. ''' curr_env = os.environ.copy() curr_env.update({'KUBECONFIG': self.kubeconfig}) proc = subprocess.Popen(cmds, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=curr_env) stdout, stderr = proc.communicate(input_data) return proc.returncode, stdout.decode('utf-8'), stderr.decode('utf-8') # pylint: disable=too-many-arguments,too-many-branches def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' cmds = [self.oc_binary] if oadm: cmds.append('adm') cmds.extend(cmd) if self.all_namespaces: cmds.extend(['--all-namespaces']) elif self.namespace is not None and self.namespace.lower() not in ['none', 'emtpy']: # E501 cmds.extend(['-n', self.namespace]) if self.verbose: print(' '.join(cmds)) try: returncode, stdout, stderr = self._run(cmds, input_data) except OSError as ex: returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex) rval = {"returncode": returncode, "cmd": ' '.join(cmds)} if output_type == 'json': rval['results'] = {} if output and stdout: try: rval['results'] = json.loads(stdout) except ValueError as verr: if "No JSON object could be decoded" in verr.args: rval['err'] = verr.args elif output_type == 'raw': rval['results'] = stdout if output else '' if self.verbose: print("STDOUT: {0}".format(stdout)) print("STDERR: {0}".format(stderr)) if 'err' in rval or returncode != 0: rval.update({"stderr": stderr, "stdout": stdout}) return rval class Utils(object): ''' utilities for openshiftcli modules ''' @staticmethod def _write(filename, contents): ''' Actually write the file contents to disk. This helps with mocking. ''' with open(filename, 'w') as sfd: sfd.write(str(contents)) @staticmethod def create_tmp_file_from_contents(rname, data, ftype='yaml'): ''' create a file in tmp with name and contents''' tmp = Utils.create_tmpfile(prefix=rname) if ftype == 'yaml': # AUDIT:no-member makes sense here due to ruamel.YAML/PyYAML usage # pylint: disable=no-member if hasattr(yaml, 'RoundTripDumper'): Utils._write(tmp, yaml.dump(data, Dumper=yaml.RoundTripDumper)) else: Utils._write(tmp, yaml.safe_dump(data, default_flow_style=False)) elif ftype == 'json': Utils._write(tmp, json.dumps(data)) else: Utils._write(tmp, data) # Register cleanup when module is done atexit.register(Utils.cleanup, [tmp]) return tmp @staticmethod def create_tmpfile_copy(inc_file): '''create a temporary copy of a file''' tmpfile = Utils.create_tmpfile('lib_openshift-') Utils._write(tmpfile, open(inc_file).read()) # Cleanup the tmpfile atexit.register(Utils.cleanup, [tmpfile]) return tmpfile @staticmethod def create_tmpfile(prefix='tmp'): ''' Generates and returns a temporary file name ''' with tempfile.NamedTemporaryFile(prefix=prefix, delete=False) as tmp: return tmp.name @staticmethod def create_tmp_files_from_contents(content, content_type=None): '''Turn an array of dict: filename, content into a files array''' if not isinstance(content, list): content = [content] files = [] for item in content: path = Utils.create_tmp_file_from_contents(item['path'] + '-', item['data'], ftype=content_type) files.append({'name': os.path.basename(item['path']), 'path': path}) return files @staticmethod def cleanup(files): '''Clean up on exit ''' for sfile in files: if os.path.exists(sfile): if os.path.isdir(sfile): shutil.rmtree(sfile) elif os.path.isfile(sfile): os.remove(sfile) @staticmethod def exists(results, _name): ''' Check to see if the results include the name ''' if not results: return False if Utils.find_result(results, _name): return True return False @staticmethod def find_result(results, _name): ''' Find the specified result by name''' rval = None for result in results: if 'metadata' in result and result['metadata']['name'] == _name: rval = result break return rval @staticmethod def get_resource_file(sfile, sfile_type='yaml'): ''' return the service file ''' contents = None with open(sfile) as sfd: contents = sfd.read() if sfile_type == 'yaml': # AUDIT:no-member makes sense here due to ruamel.YAML/PyYAML usage # pylint: disable=no-member if hasattr(yaml, 'RoundTripLoader'): contents = yaml.load(contents, yaml.RoundTripLoader) else: contents = yaml.safe_load(contents) elif sfile_type == 'json': contents = json.loads(contents) return contents @staticmethod def filter_versions(stdout): ''' filter the oc version output ''' version_dict = {} version_search = ['oc', 'openshift', 'kubernetes'] for line in stdout.strip().split('\n'): for term in version_search: if not line: continue if line.startswith(term): version_dict[term] = line.split()[-1] # horrible hack to get openshift version in Openshift 3.2 # By default "oc version in 3.2 does not return an "openshift" version if "openshift" not in version_dict: version_dict["openshift"] = version_dict["oc"] return version_dict @staticmethod def add_custom_versions(versions): ''' create custom versions strings ''' versions_dict = {} for tech, version in versions.items(): # clean up "-" from version if "-" in version: version = version.split("-")[0] if version.startswith('v'): version = version[1:] # Remove the 'v' prefix versions_dict[tech + '_numeric'] = version.split('+')[0] # "3.3.0.33" is what we have, we want "3.3" versions_dict[tech + '_short'] = "{}.{}".format(*version.split('.')) return versions_dict @staticmethod def openshift_installed(): ''' check if openshift is installed ''' import rpm transaction_set = rpm.TransactionSet() rpmquery = transaction_set.dbMatch("name", "atomic-openshift") return rpmquery.count() > 0 # Disabling too-many-branches. This is a yaml dictionary comparison function # pylint: disable=too-many-branches,too-many-return-statements,too-many-statements @staticmethod def check_def_equal(user_def, result_def, skip_keys=None, debug=False): ''' Given a user defined definition, compare it with the results given back by our query. ''' # Currently these values are autogenerated and we do not need to check them skip = ['metadata', 'status'] if skip_keys: skip.extend(skip_keys) for key, value in result_def.items(): if key in skip: continue # Both are lists if isinstance(value, list): if key not in user_def: if debug: print('User data does not have key [%s]' % key) print('User data: %s' % user_def) return False if not isinstance(user_def[key], list): if debug: print('user_def[key] is not a list key=[%s] user_def[key]=%s' % (key, user_def[key])) return False if len(user_def[key]) != len(value): if debug: print("List lengths are not equal.") print("key=[%s]: user_def[%s] != value[%s]" % (key, len(user_def[key]), len(value))) print("user_def: %s" % user_def[key]) print("value: %s" % value) return False for values in zip(user_def[key], value): if isinstance(values[0], dict) and isinstance(values[1], dict): if debug: print('sending list - list') print(type(values[0])) print(type(values[1])) result = Utils.check_def_equal(values[0], values[1], skip_keys=skip_keys, debug=debug) if not result: print('list compare returned false') return False elif value != user_def[key]: if debug: print('value should be identical') print(user_def[key]) print(value) return False # recurse on a dictionary elif isinstance(value, dict): if key not in user_def: if debug: print("user_def does not have key [%s]" % key) return False if not isinstance(user_def[key], dict): if debug: print("dict returned false: not instance of dict") return False # before passing ensure keys match api_values = set(value.keys()) - set(skip) user_values = set(user_def[key].keys()) - set(skip) if api_values != user_values: if debug: print("keys are not equal in dict") print(user_values) print(api_values) return False result = Utils.check_def_equal(user_def[key], value, skip_keys=skip_keys, debug=debug) if not result: if debug: print("dict returned false") print(result) return False # Verify each key, value pair is the same else: if key not in user_def or value != user_def[key]: if debug: print("value not equal; user_def does not have key") print(key) print(value) if key in user_def: print(user_def[key]) return False if debug: print('returning true') return True class OpenShiftCLIConfig(object): '''Generic Config''' def __init__(self, rname, namespace, kubeconfig, options): self.kubeconfig = kubeconfig self.name = rname self.namespace = namespace self._options = options @property def config_options(self): ''' return config options ''' return self._options def to_option_list(self, ascommalist=''): '''return all options as a string if ascommalist is set to the name of a key, and the value of that key is a dict, format the dict as a list of comma delimited key=value pairs''' return self.stringify(ascommalist) def stringify(self, ascommalist=''): ''' return the options hash as cli params in a string if ascommalist is set to the name of a key, and the value of that key is a dict, format the dict as a list of comma delimited key=value pairs ''' rval = [] for key in sorted(self.config_options.keys()): data = self.config_options[key] if data['include'] \ and (data['value'] is not None or isinstance(data['value'], int)): if key == ascommalist: val = ','.join(['{}={}'.format(kk, vv) for kk, vv in sorted(data['value'].items())]) else: val = data['value'] rval.append('--{}={}'.format(key.replace('_', '-'), val)) return rval
apache-2.0
kingmotley/SickRage
lib/requests/packages/urllib3/contrib/pyopenssl.py
197
10094
'''SSL with SNI_-support for Python 2. Follow these instructions if you would like to verify SSL certificates in Python 2. Note, the default libraries do *not* do certificate checking; you need to do additional work to validate certificates yourself. This needs the following packages installed: * pyOpenSSL (tested with 0.13) * ndg-httpsclient (tested with 0.3.2) * pyasn1 (tested with 0.1.6) You can install them with the following command: pip install pyopenssl ndg-httpsclient pyasn1 To activate certificate checking, call :func:`~urllib3.contrib.pyopenssl.inject_into_urllib3` from your Python code before you begin making HTTP requests. This can be done in a ``sitecustomize`` module, or at any other time before your application begins using ``urllib3``, like this:: try: import urllib3.contrib.pyopenssl urllib3.contrib.pyopenssl.inject_into_urllib3() except ImportError: pass Now you can use :mod:`urllib3` as you normally would, and it will support SNI when the required modules are installed. Activating this module also has the positive side effect of disabling SSL/TLS compression in Python 2 (see `CRIME attack`_). If you want to configure the default list of supported cipher suites, you can set the ``urllib3.contrib.pyopenssl.DEFAULT_SSL_CIPHER_LIST`` variable. Module Variables ---------------- :var DEFAULT_SSL_CIPHER_LIST: The list of supported SSL/TLS cipher suites. .. _sni: https://en.wikipedia.org/wiki/Server_Name_Indication .. _crime attack: https://en.wikipedia.org/wiki/CRIME_(security_exploit) ''' from __future__ import absolute_import try: from ndg.httpsclient.ssl_peer_verification import SUBJ_ALT_NAME_SUPPORT from ndg.httpsclient.subj_alt_name import SubjectAltName as BaseSubjectAltName except SyntaxError as e: raise ImportError(e) import OpenSSL.SSL from pyasn1.codec.der import decoder as der_decoder from pyasn1.type import univ, constraint from socket import _fileobject, timeout, error as SocketError import ssl import select from .. import connection from .. import util __all__ = ['inject_into_urllib3', 'extract_from_urllib3'] # SNI only *really* works if we can read the subjectAltName of certificates. HAS_SNI = SUBJ_ALT_NAME_SUPPORT # Map from urllib3 to PyOpenSSL compatible parameter-values. _openssl_versions = { ssl.PROTOCOL_SSLv23: OpenSSL.SSL.SSLv23_METHOD, ssl.PROTOCOL_TLSv1: OpenSSL.SSL.TLSv1_METHOD, } if hasattr(ssl, 'PROTOCOL_TLSv1_1') and hasattr(OpenSSL.SSL, 'TLSv1_1_METHOD'): _openssl_versions[ssl.PROTOCOL_TLSv1_1] = OpenSSL.SSL.TLSv1_1_METHOD if hasattr(ssl, 'PROTOCOL_TLSv1_2') and hasattr(OpenSSL.SSL, 'TLSv1_2_METHOD'): _openssl_versions[ssl.PROTOCOL_TLSv1_2] = OpenSSL.SSL.TLSv1_2_METHOD try: _openssl_versions.update({ssl.PROTOCOL_SSLv3: OpenSSL.SSL.SSLv3_METHOD}) except AttributeError: pass _openssl_verify = { ssl.CERT_NONE: OpenSSL.SSL.VERIFY_NONE, ssl.CERT_OPTIONAL: OpenSSL.SSL.VERIFY_PEER, ssl.CERT_REQUIRED: OpenSSL.SSL.VERIFY_PEER + OpenSSL.SSL.VERIFY_FAIL_IF_NO_PEER_CERT, } DEFAULT_SSL_CIPHER_LIST = util.ssl_.DEFAULT_CIPHERS # OpenSSL will only write 16K at a time SSL_WRITE_BLOCKSIZE = 16384 orig_util_HAS_SNI = util.HAS_SNI orig_connection_ssl_wrap_socket = connection.ssl_wrap_socket def inject_into_urllib3(): 'Monkey-patch urllib3 with PyOpenSSL-backed SSL-support.' connection.ssl_wrap_socket = ssl_wrap_socket util.HAS_SNI = HAS_SNI def extract_from_urllib3(): 'Undo monkey-patching by :func:`inject_into_urllib3`.' connection.ssl_wrap_socket = orig_connection_ssl_wrap_socket util.HAS_SNI = orig_util_HAS_SNI # Note: This is a slightly bug-fixed version of same from ndg-httpsclient. class SubjectAltName(BaseSubjectAltName): '''ASN.1 implementation for subjectAltNames support''' # There is no limit to how many SAN certificates a certificate may have, # however this needs to have some limit so we'll set an arbitrarily high # limit. sizeSpec = univ.SequenceOf.sizeSpec + \ constraint.ValueSizeConstraint(1, 1024) # Note: This is a slightly bug-fixed version of same from ndg-httpsclient. def get_subj_alt_name(peer_cert): # Search through extensions dns_name = [] if not SUBJ_ALT_NAME_SUPPORT: return dns_name general_names = SubjectAltName() for i in range(peer_cert.get_extension_count()): ext = peer_cert.get_extension(i) ext_name = ext.get_short_name() if ext_name != 'subjectAltName': continue # PyOpenSSL returns extension data in ASN.1 encoded form ext_dat = ext.get_data() decoded_dat = der_decoder.decode(ext_dat, asn1Spec=general_names) for name in decoded_dat: if not isinstance(name, SubjectAltName): continue for entry in range(len(name)): component = name.getComponentByPosition(entry) if component.getName() != 'dNSName': continue dns_name.append(str(component.getComponent())) return dns_name class WrappedSocket(object): '''API-compatibility wrapper for Python OpenSSL's Connection-class. Note: _makefile_refs, _drop() and _reuse() are needed for the garbage collector of pypy. ''' def __init__(self, connection, socket, suppress_ragged_eofs=True): self.connection = connection self.socket = socket self.suppress_ragged_eofs = suppress_ragged_eofs self._makefile_refs = 0 def fileno(self): return self.socket.fileno() def makefile(self, mode, bufsize=-1): self._makefile_refs += 1 return _fileobject(self, mode, bufsize, close=True) def recv(self, *args, **kwargs): try: data = self.connection.recv(*args, **kwargs) except OpenSSL.SSL.SysCallError as e: if self.suppress_ragged_eofs and e.args == (-1, 'Unexpected EOF'): return b'' else: raise SocketError(e) except OpenSSL.SSL.ZeroReturnError as e: if self.connection.get_shutdown() == OpenSSL.SSL.RECEIVED_SHUTDOWN: return b'' else: raise except OpenSSL.SSL.WantReadError: rd, wd, ed = select.select( [self.socket], [], [], self.socket.gettimeout()) if not rd: raise timeout('The read operation timed out') else: return self.recv(*args, **kwargs) else: return data def settimeout(self, timeout): return self.socket.settimeout(timeout) def _send_until_done(self, data): while True: try: return self.connection.send(data) except OpenSSL.SSL.WantWriteError: _, wlist, _ = select.select([], [self.socket], [], self.socket.gettimeout()) if not wlist: raise timeout() continue def sendall(self, data): total_sent = 0 while total_sent < len(data): sent = self._send_until_done(data[total_sent:total_sent + SSL_WRITE_BLOCKSIZE]) total_sent += sent def shutdown(self): # FIXME rethrow compatible exceptions should we ever use this self.connection.shutdown() def close(self): if self._makefile_refs < 1: try: return self.connection.close() except OpenSSL.SSL.Error: return else: self._makefile_refs -= 1 def getpeercert(self, binary_form=False): x509 = self.connection.get_peer_certificate() if not x509: return x509 if binary_form: return OpenSSL.crypto.dump_certificate( OpenSSL.crypto.FILETYPE_ASN1, x509) return { 'subject': ( (('commonName', x509.get_subject().CN),), ), 'subjectAltName': [ ('DNS', value) for value in get_subj_alt_name(x509) ] } def _reuse(self): self._makefile_refs += 1 def _drop(self): if self._makefile_refs < 1: self.close() else: self._makefile_refs -= 1 def _verify_callback(cnx, x509, err_no, err_depth, return_code): return err_no == 0 def ssl_wrap_socket(sock, keyfile=None, certfile=None, cert_reqs=None, ca_certs=None, server_hostname=None, ssl_version=None, ca_cert_dir=None): ctx = OpenSSL.SSL.Context(_openssl_versions[ssl_version]) if certfile: keyfile = keyfile or certfile # Match behaviour of the normal python ssl library ctx.use_certificate_file(certfile) if keyfile: ctx.use_privatekey_file(keyfile) if cert_reqs != ssl.CERT_NONE: ctx.set_verify(_openssl_verify[cert_reqs], _verify_callback) if ca_certs or ca_cert_dir: try: ctx.load_verify_locations(ca_certs, ca_cert_dir) except OpenSSL.SSL.Error as e: raise ssl.SSLError('bad ca_certs: %r' % ca_certs, e) else: ctx.set_default_verify_paths() # Disable TLS compression to migitate CRIME attack (issue #309) OP_NO_COMPRESSION = 0x20000 ctx.set_options(OP_NO_COMPRESSION) # Set list of supported ciphersuites. ctx.set_cipher_list(DEFAULT_SSL_CIPHER_LIST) cnx = OpenSSL.SSL.Connection(ctx, sock) cnx.set_tlsext_host_name(server_hostname) cnx.set_connect_state() while True: try: cnx.do_handshake() except OpenSSL.SSL.WantReadError: rd, _, _ = select.select([sock], [], [], sock.gettimeout()) if not rd: raise timeout('select timed out') continue except OpenSSL.SSL.Error as e: raise ssl.SSLError('bad handshake: %r' % e) break return WrappedSocket(cnx, sock)
gpl-3.0
mjirayu/sit_academy
common/lib/xmodule/xmodule/conditional_module.py
97
10074
"""Conditional module is the xmodule, which you can use for disabling some xmodules by conditions. """ import json import logging from lazy import lazy from lxml import etree from pkg_resources import resource_string from xmodule.x_module import XModule, STUDENT_VIEW from xmodule.seq_module import SequenceDescriptor from xblock.fields import Scope, ReferenceList from xmodule.modulestore.exceptions import ItemNotFoundError log = logging.getLogger('edx.' + __name__) class ConditionalFields(object): has_children = True show_tag_list = ReferenceList(help="List of urls of children that are references to external modules", scope=Scope.content) sources_list = ReferenceList(help="List of sources upon which this module is conditional", scope=Scope.content) class ConditionalModule(ConditionalFields, XModule): """ Blocks child module from showing unless certain conditions are met. Example: <conditional sources="i4x://.../problem_1; i4x://.../problem_2" completed="True"> <show sources="i4x://.../test_6; i4x://.../Avi_resources"/> <video url_name="secret_video" /> </conditional> <conditional> tag attributes: sources - location id of required modules, separated by ';' submitted - map to `is_submitted` module method. (pressing RESET button makes this function to return False.) attempted - map to `is_attempted` module method correct - map to `is_correct` module method poll_answer - map to `poll_answer` module attribute voted - map to `voted` module attribute <show> tag attributes: sources - location id of required modules, separated by ';' You can add you own rules for <conditional> tag, like "completed", "attempted" etc. To do that yo must extend `ConditionalModule.conditions_map` variable and add pair: my_attr: my_property/my_method After that you can use it: <conditional my_attr="some value" ...> ... </conditional> And my_property/my_method will be called for required modules. """ js = { 'coffee': [ resource_string(__name__, 'js/src/javascript_loader.coffee'), resource_string(__name__, 'js/src/conditional/display.coffee'), ], 'js': [ resource_string(__name__, 'js/src/collapsible.js'), ] } js_module_name = "Conditional" css = {'scss': [resource_string(__name__, 'css/capa/display.scss')]} # Map # key: <tag attribute in xml> # value: <name of module attribute> conditions_map = { 'poll_answer': 'poll_answer', # poll_question attr # problem was submitted (it can be wrong) # if student will press reset button after that, # state will be reverted 'submitted': 'is_submitted', # capa_problem attr # if student attempted problem 'attempted': 'is_attempted', # capa_problem attr # if problem is full points 'correct': 'is_correct', 'voted': 'voted' # poll_question attr } def _get_condition(self): # Get first valid condition. for xml_attr, attr_name in self.conditions_map.iteritems(): xml_value = self.descriptor.xml_attributes.get(xml_attr) if xml_value: return xml_value, attr_name raise Exception( 'Error in conditional module: no known conditional found in {!r}'.format( self.descriptor.xml_attributes.keys() ) ) @lazy def required_modules(self): return [self.system.get_module(descriptor) for descriptor in self.descriptor.get_required_module_descriptors()] def is_condition_satisfied(self): xml_value, attr_name = self._get_condition() if xml_value and self.required_modules: for module in self.required_modules: if not hasattr(module, attr_name): # We don't throw an exception here because it is possible for # the descriptor of a required module to have a property but # for the resulting module to be a (flavor of) ErrorModule. # So just log and return false. log.warn('Error in conditional module: \ required module {module} has no {module_attr}'.format(module=module, module_attr=attr_name)) return False attr = getattr(module, attr_name) if callable(attr): attr = attr() if xml_value != str(attr): break else: return True return False def get_html(self): # Calculate html ids of dependencies self.required_html_ids = [descriptor.location.html_id() for descriptor in self.descriptor.get_required_module_descriptors()] return self.system.render_template('conditional_ajax.html', { 'element_id': self.location.html_id(), 'ajax_url': self.system.ajax_url, 'depends': ';'.join(self.required_html_ids) }) def handle_ajax(self, _dispatch, _data): """This is called by courseware.moduleodule_render, to handle an AJAX call. """ if not self.is_condition_satisfied(): defmsg = "{link} must be attempted before this will become visible." message = self.descriptor.xml_attributes.get('message', defmsg) context = {'module': self, 'message': message} html = self.system.render_template('conditional_module.html', context) return json.dumps({'html': [html], 'message': bool(message)}) html = [child.render(STUDENT_VIEW).content for child in self.get_display_items()] return json.dumps({'html': html}) def get_icon_class(self): new_class = 'other' # HACK: This shouldn't be hard-coded to two types # OBSOLETE: This obsoletes 'type' class_priority = ['video', 'problem'] child_classes = [self.system.get_module(child_descriptor).get_icon_class() for child_descriptor in self.descriptor.get_children()] for c in class_priority: if c in child_classes: new_class = c return new_class class ConditionalDescriptor(ConditionalFields, SequenceDescriptor): """Descriptor for conditional xmodule.""" _tag_name = 'conditional' module_class = ConditionalModule filename_extension = "xml" has_score = False show_in_read_only_mode = True def __init__(self, *args, **kwargs): """ Create an instance of the conditional module. """ super(ConditionalDescriptor, self).__init__(*args, **kwargs) # Convert sources xml_attribute to a ReferenceList field type so Location/Locator # substitution can be done. if not self.sources_list: if 'sources' in self.xml_attributes and isinstance(self.xml_attributes['sources'], basestring): self.sources_list = [ self.location.course_key.make_usage_key_from_deprecated_string(item) for item in ConditionalDescriptor.parse_sources(self.xml_attributes) ] @staticmethod def parse_sources(xml_element): """ Parse xml_element 'sources' attr and return a list of location strings. """ sources = xml_element.get('sources') if sources: return [location.strip() for location in sources.split(';')] def get_required_module_descriptors(self): """Returns a list of XModuleDescriptor instances upon which this module depends. """ descriptors = [] for location in self.sources_list: try: descriptor = self.system.load_item(location) descriptors.append(descriptor) except ItemNotFoundError: msg = "Invalid module by location." log.exception(msg) self.system.error_tracker(msg) return descriptors @classmethod def definition_from_xml(cls, xml_object, system): children = [] show_tag_list = [] for child in xml_object: if child.tag == 'show': locations = ConditionalDescriptor.parse_sources(child) for location in locations: children.append(location) show_tag_list.append(location) else: try: descriptor = system.process_xml(etree.tostring(child)) children.append(descriptor.scope_ids.usage_id) except: msg = "Unable to load child when parsing Conditional." log.exception(msg) system.error_tracker(msg) return {'show_tag_list': show_tag_list}, children def definition_to_xml(self, resource_fs): xml_object = etree.Element(self._tag_name) for child in self.get_children(): if child.location not in self.show_tag_list: self.runtime.add_block_as_child_node(child, xml_object) if self.show_tag_list: show_str = u'<{tag_name} sources="{sources}" />'.format( tag_name='show', sources=';'.join(location.to_deprecated_string() for location in self.show_tag_list)) xml_object.append(etree.fromstring(show_str)) # Overwrite the original sources attribute with the value from sources_list, as # Locations may have been changed to Locators. stringified_sources_list = map(lambda loc: loc.to_deprecated_string(), self.sources_list) self.xml_attributes['sources'] = ';'.join(stringified_sources_list) return xml_object
agpl-3.0
Azure/azure-sdk-for-python
sdk/communication/azure-communication-sms/azure/communication/sms/_models/_models.py
1
2064
# coding=utf-8 # -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # Code generated by Microsoft (R) AutoRest Code Generator. # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- import msrest.serialization class SmsSendResult(msrest.serialization.Model): """Response for a single recipient. All required parameters must be populated in order to send to Azure. :param to: Required. The recipient's phone number in E.164 format. :type to: str :param message_id: The identifier of the outgoing Sms message. Only present if message processed. :type message_id: str :param http_status_code: Required. HTTP Status code. :type http_status_code: int :param successful: Required. Indicates if the message is processed successfully or not. :type successful: bool :param error_message: Optional error message in case of 4xx/5xx/repeatable errors. :type error_message: str """ _validation = { 'to': {'required': True}, 'http_status_code': {'required': True}, 'successful': {'required': True}, } _attribute_map = { 'to': {'key': 'to', 'type': 'str'}, 'message_id': {'key': 'messageId', 'type': 'str'}, 'http_status_code': {'key': 'httpStatusCode', 'type': 'int'}, 'successful': {'key': 'successful', 'type': 'bool'}, 'error_message': {'key': 'errorMessage', 'type': 'str'}, } def __init__( self, **kwargs ): super(SmsSendResult, self).__init__(**kwargs) self.to = kwargs['to'] self.message_id = kwargs.get('message_id', None) self.http_status_code = kwargs['http_status_code'] self.successful = kwargs['successful'] self.error_message = kwargs.get('error_message', None)
mit
groschovskiy/lerigos_music
Server/API/lib/gcloud/resource_manager/connection.py
8
1637
# Copyright 2015 Google Inc. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Create / interact with gcloud.resource_manager connections.""" from gcloud import connection as base_connection class Connection(base_connection.JSONConnection): """A connection to Google Cloud Resource Manager via the JSON REST API. :type credentials: :class:`oauth2client.client.OAuth2Credentials` :param credentials: (Optional) The OAuth2 Credentials to use for this connection. :type http: :class:`httplib2.Http` or class that defines ``request()``. :param http: (Optional) HTTP object to make requests. """ API_BASE_URL = 'https://cloudresourcemanager.googleapis.com' """The base of the API call URL.""" API_VERSION = 'v1beta1' """The version of the API, used in building the API call's URL.""" API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}' """A template for the URL of a particular API call.""" SCOPE = ('https://www.googleapis.com/auth/cloud-platform',) """The scopes required for authenticating as a Resouce Manager consumer."""
apache-2.0
rawwell/django
django/dispatch/dispatcher.py
38
17163
"""Multiple-producer-multiple-consumer signal-dispatching dispatcher is the core of the PyDispatcher system, providing the primary API and the core logic for the system. Module attributes of note: Any -- Singleton used to signal either "Any Sender" or "Any Signal". See documentation of the _Any class. Anonymous -- Singleton used to signal "Anonymous Sender" See documentation of the _Anonymous class. Internal attributes: WEAKREF_TYPES -- tuple of types/classes which represent weak references to receivers, and thus must be de- referenced on retrieval to retrieve the callable object connections -- { senderkey (id) : { signal : [receivers...]}} senders -- { senderkey (id) : weakref(sender) } used for cleaning up sender references on sender deletion sendersBack -- { receiverkey (id) : [senderkey (id)...] } used for cleaning up receiver references on receiver deletion, (considerably speeds up the cleanup process vs. the original code.) """ import types, weakref from django.dispatch import saferef, robustapply, errors __author__ = "Patrick K. O'Brien <pobrien@orbtech.com>" __cvsid__ = "$Id: dispatcher.py,v 1.9 2005/09/17 04:55:57 mcfletch Exp $" __version__ = "$Revision: 1.9 $"[11:-2] class _Parameter: """Used to represent default parameter values.""" def __repr__(self): return self.__class__.__name__ class _Any(_Parameter): """Singleton used to signal either "Any Sender" or "Any Signal" The Any object can be used with connect, disconnect, send, or sendExact to signal that the parameter given Any should react to all senders/signals, not just a particular sender/signal. """ Any = _Any() class _Anonymous(_Parameter): """Singleton used to signal "Anonymous Sender" The Anonymous object is used to signal that the sender of a message is not specified (as distinct from being "any sender"). Registering callbacks for Anonymous will only receive messages sent without senders. Sending with anonymous will only send messages to those receivers registered for Any or Anonymous. Note: The default sender for connect is Any, while the default sender for send is Anonymous. This has the effect that if you do not specify any senders in either function then all messages are routed as though there was a single sender (Anonymous) being used everywhere. """ Anonymous = _Anonymous() WEAKREF_TYPES = (weakref.ReferenceType, saferef.BoundMethodWeakref) connections = {} senders = {} sendersBack = {} def connect(receiver, signal=Any, sender=Any, weak=True): """Connect receiver to sender for signal receiver -- a callable Python object which is to receive messages/signals/events. Receivers must be hashable objects. if weak is True, then receiver must be weak-referencable (more precisely saferef.safeRef() must be able to create a reference to the receiver). Receivers are fairly flexible in their specification, as the machinery in the robustApply module takes care of most of the details regarding figuring out appropriate subsets of the sent arguments to apply to a given receiver. Note: if receiver is itself a weak reference (a callable), it will be de-referenced by the system's machinery, so *generally* weak references are not suitable as receivers, though some use might be found for the facility whereby a higher-level library passes in pre-weakrefed receiver references. signal -- the signal to which the receiver should respond if Any, receiver will receive any signal from the indicated sender (which might also be Any, but is not necessarily Any). Otherwise must be a hashable Python object other than None (DispatcherError raised on None). sender -- the sender to which the receiver should respond if Any, receiver will receive the indicated signals from any sender. if Anonymous, receiver will only receive indicated signals from send/sendExact which do not specify a sender, or specify Anonymous explicitly as the sender. Otherwise can be any python object. weak -- whether to use weak references to the receiver By default, the module will attempt to use weak references to the receiver objects. If this parameter is false, then strong references will be used. returns None, may raise DispatcherTypeError """ if signal is None: raise errors.DispatcherTypeError( 'Signal cannot be None (receiver=%r sender=%r)'%( receiver,sender) ) if weak: receiver = saferef.safeRef(receiver, onDelete=_removeReceiver) senderkey = id(sender) signals = connections.setdefault(senderkey, {}) # Keep track of senders for cleanup. # Is Anonymous something we want to clean up? if sender not in (None, Anonymous, Any): def remove(object, senderkey=senderkey): _removeSender(senderkey=senderkey) # Skip objects that can not be weakly referenced, which means # they won't be automatically cleaned up, but that's too bad. try: weakSender = weakref.ref(sender, remove) senders[senderkey] = weakSender except: pass receiverID = id(receiver) # get current set, remove any current references to # this receiver in the set, including back-references if signals.has_key(signal): receivers = signals[signal] _removeOldBackRefs(senderkey, signal, receiver, receivers) else: receivers = signals[signal] = [] try: current = sendersBack.get( receiverID ) if current is None: sendersBack[ receiverID ] = current = [] if senderkey not in current: current.append(senderkey) except: pass receivers.append(receiver) def disconnect(receiver, signal=Any, sender=Any, weak=True): """Disconnect receiver from sender for signal receiver -- the registered receiver to disconnect signal -- the registered signal to disconnect sender -- the registered sender to disconnect weak -- the weakref state to disconnect disconnect reverses the process of connect, the semantics for the individual elements are logically equivalent to a tuple of (receiver, signal, sender, weak) used as a key to be deleted from the internal routing tables. (The actual process is slightly more complex but the semantics are basically the same). Note: Using disconnect is not required to cleanup routing when an object is deleted, the framework will remove routes for deleted objects automatically. It's only necessary to disconnect if you want to stop routing to a live object. returns None, may raise DispatcherTypeError or DispatcherKeyError """ if signal is None: raise errors.DispatcherTypeError( 'Signal cannot be None (receiver=%r sender=%r)'%( receiver,sender) ) if weak: receiver = saferef.safeRef(receiver) senderkey = id(sender) try: signals = connections[senderkey] receivers = signals[signal] except KeyError: raise errors.DispatcherKeyError( """No receivers found for signal %r from sender %r""" %( signal, sender ) ) try: # also removes from receivers _removeOldBackRefs(senderkey, signal, receiver, receivers) except ValueError: raise errors.DispatcherKeyError( """No connection to receiver %s for signal %s from sender %s""" %( receiver, signal, sender ) ) _cleanupConnections(senderkey, signal) def getReceivers( sender = Any, signal = Any ): """Get list of receivers from global tables This utility function allows you to retrieve the raw list of receivers from the connections table for the given sender and signal pair. Note: there is no guarantee that this is the actual list stored in the connections table, so the value should be treated as a simple iterable/truth value rather than, for instance a list to which you might append new records. Normally you would use liveReceivers( getReceivers( ...)) to retrieve the actual receiver objects as an iterable object. """ existing = connections.get(id(sender)) if existing is not None: return existing.get(signal, []) return [] def liveReceivers(receivers): """Filter sequence of receivers to get resolved, live receivers This is a generator which will iterate over the passed sequence, checking for weak references and resolving them, then returning all live receivers. """ for receiver in receivers: if isinstance( receiver, WEAKREF_TYPES): # Dereference the weak reference. receiver = receiver() if receiver is not None: yield receiver else: yield receiver def getAllReceivers( sender = Any, signal = Any ): """Get list of all receivers from global tables This gets all dereferenced receivers which should receive the given signal from sender, each receiver should be produced only once by the resulting generator """ receivers = {} # Get receivers that receive *this* signal from *this* sender. # Add receivers that receive *any* signal from *this* sender. # Add receivers that receive *this* signal from *any* sender. # Add receivers that receive *any* signal from *any* sender. l = [] i = id(sender) if i in connections: sender_receivers = connections[i] if signal in sender_receivers: l.extend(sender_receivers[signal]) if signal is not Any and Any in sender_receivers: l.extend(sender_receivers[Any]) if sender is not Any: i = id(Any) if i in connections: sender_receivers = connections[i] if sender_receivers is not None: if signal in sender_receivers: l.extend(sender_receivers[signal]) if signal is not Any and Any in sender_receivers: l.extend(sender_receivers[Any]) for receiver in l: try: if not receiver in receivers: if isinstance(receiver, WEAKREF_TYPES): receiver = receiver() # this should only (rough guess) be possible if somehow, deref'ing # triggered a wipe. if receiver is None: continue receivers[receiver] = 1 yield receiver except TypeError: # dead weakrefs raise TypeError on hash... pass def send(signal=Any, sender=Anonymous, *arguments, **named): """Send signal from sender to all connected receivers. signal -- (hashable) signal value, see connect for details sender -- the sender of the signal if Any, only receivers registered for Any will receive the message. if Anonymous, only receivers registered to receive messages from Anonymous or Any will receive the message Otherwise can be any python object (normally one registered with a connect if you actually want something to occur). arguments -- positional arguments which will be passed to *all* receivers. Note that this may raise TypeErrors if the receivers do not allow the particular arguments. Note also that arguments are applied before named arguments, so they should be used with care. named -- named arguments which will be filtered according to the parameters of the receivers to only provide those acceptable to the receiver. Return a list of tuple pairs [(receiver, response), ... ] if any receiver raises an error, the error propagates back through send, terminating the dispatch loop, so it is quite possible to not have all receivers called if a raises an error. """ # Call each receiver with whatever arguments it can accept. # Return a list of tuple pairs [(receiver, response), ... ]. responses = [] for receiver in getAllReceivers(sender, signal): response = robustapply.robustApply( receiver, signal=signal, sender=sender, *arguments, **named ) responses.append((receiver, response)) return responses def sendExact( signal=Any, sender=Anonymous, *arguments, **named ): """Send signal only to those receivers registered for exact message sendExact allows for avoiding Any/Anonymous registered handlers, sending only to those receivers explicitly registered for a particular signal on a particular sender. """ responses = [] for receiver in liveReceivers(getReceivers(sender, signal)): response = robustapply.robustApply( receiver, signal=signal, sender=sender, *arguments, **named ) responses.append((receiver, response)) return responses def _removeReceiver(receiver): """Remove receiver from connections.""" if not sendersBack: # During module cleanup the mapping will be replaced with None return False backKey = id(receiver) for senderkey in sendersBack.get(backKey,()): try: signals = connections[senderkey].keys() except KeyError,err: pass else: for signal in signals: try: receivers = connections[senderkey][signal] except KeyError: pass else: try: receivers.remove( receiver ) except Exception, err: pass _cleanupConnections(senderkey, signal) try: del sendersBack[ backKey ] except KeyError: pass def _cleanupConnections(senderkey, signal): """Delete any empty signals for senderkey. Delete senderkey if empty.""" try: receivers = connections[senderkey][signal] except: pass else: if not receivers: # No more connected receivers. Therefore, remove the signal. try: signals = connections[senderkey] except KeyError: pass else: del signals[signal] if not signals: # No more signal connections. Therefore, remove the sender. _removeSender(senderkey) def _removeSender(senderkey): """Remove senderkey from connections.""" _removeBackrefs(senderkey) connections.pop(senderkey, None) senders.pop(senderkey, None) def _removeBackrefs( senderkey): """Remove all back-references to this senderkey""" for receiver_list in connections.pop(senderkey, {}).values(): for receiver in receiver_list: _killBackref( receiver, senderkey ) def _removeOldBackRefs(senderkey, signal, receiver, receivers): """Kill old sendersBack references from receiver This guards against multiple registration of the same receiver for a given signal and sender leaking memory as old back reference records build up. Also removes old receiver instance from receivers """ try: index = receivers.index(receiver) # need to scan back references here and remove senderkey except ValueError: return False else: oldReceiver = receivers[index] del receivers[index] found = 0 signals = connections.get(signal) if signals is not None: for sig,recs in connections.get(signal,{}).iteritems(): if sig != signal: for rec in recs: if rec is oldReceiver: found = 1 break if not found: _killBackref( oldReceiver, senderkey ) return True return False def _killBackref( receiver, senderkey ): """Do the actual removal of back reference from receiver to senderkey""" receiverkey = id(receiver) receivers_list = sendersBack.get( receiverkey, () ) while senderkey in receivers_list: try: receivers_list.remove( senderkey ) except: break if not receivers_list: try: del sendersBack[ receiverkey ] except KeyError: pass return True
bsd-3-clause
foodszhang/kbengine
kbe/src/lib/python/Tools/pynche/TextViewer.py
116
6869
"""TextViewer class. The TextViewer allows you to see how the selected color would affect various characteristics of a Tk text widget. This is an output viewer only. In the top part of the window is a standard text widget with some sample text in it. You are free to edit this text in any way you want (BAW: allow you to change font characteristics). If you want changes in other viewers to update text characteristics, turn on Track color changes. To select which characteristic tracks the change, select one of the radio buttons in the window below. Text foreground and background affect the text in the window above. The Selection is what you see when you click the middle button and drag it through some text. The Insertion is the insertion cursor in the text window (which only has a background). """ from tkinter import * import ColorDB ADDTOVIEW = 'Text Window...' class TextViewer: def __init__(self, switchboard, master=None): self.__sb = switchboard optiondb = switchboard.optiondb() root = self.__root = Toplevel(master, class_='Pynche') root.protocol('WM_DELETE_WINDOW', self.withdraw) root.title('Pynche Text Window') root.iconname('Pynche Text Window') root.bind('<Alt-q>', self.__quit) root.bind('<Alt-Q>', self.__quit) root.bind('<Alt-w>', self.withdraw) root.bind('<Alt-W>', self.withdraw) # # create the text widget # self.__text = Text(root, relief=SUNKEN, background=optiondb.get('TEXTBG', 'black'), foreground=optiondb.get('TEXTFG', 'white'), width=35, height=15) sfg = optiondb.get('TEXT_SFG') if sfg: self.__text.configure(selectforeground=sfg) sbg = optiondb.get('TEXT_SBG') if sbg: self.__text.configure(selectbackground=sbg) ibg = optiondb.get('TEXT_IBG') if ibg: self.__text.configure(insertbackground=ibg) self.__text.pack() self.__text.insert(0.0, optiondb.get('TEXT', '''\ Insert some stuff here and play with the buttons below to see how the colors interact in textual displays. See how the selection can also be affected by tickling the buttons and choosing a color.''')) insert = optiondb.get('TEXTINS') if insert: self.__text.mark_set(INSERT, insert) try: start, end = optiondb.get('TEXTSEL', (6.0, END)) self.__text.tag_add(SEL, start, end) except ValueError: # selection wasn't set pass self.__text.focus_set() # # variables self.__trackp = BooleanVar() self.__trackp.set(optiondb.get('TRACKP', 0)) self.__which = IntVar() self.__which.set(optiondb.get('WHICH', 0)) # # track toggle self.__t = Checkbutton(root, text='Track color changes', variable=self.__trackp, relief=GROOVE, command=self.__toggletrack) self.__t.pack(fill=X, expand=YES) frame = self.__frame = Frame(root) frame.pack() # # labels self.__labels = [] row = 2 for text in ('Text:', 'Selection:', 'Insertion:'): l = Label(frame, text=text) l.grid(row=row, column=0, sticky=E) self.__labels.append(l) row += 1 col = 1 for text in ('Foreground', 'Background'): l = Label(frame, text=text) l.grid(row=1, column=col) self.__labels.append(l) col += 1 # # radios self.__radios = [] for col in (1, 2): for row in (2, 3, 4): # there is no insertforeground option if row==4 and col==1: continue r = Radiobutton(frame, variable=self.__which, value=(row-2)*2 + col-1, command=self.__set_color) r.grid(row=row, column=col) self.__radios.append(r) self.__toggletrack() def __quit(self, event=None): self.__root.quit() def withdraw(self, event=None): self.__root.withdraw() def deiconify(self, event=None): self.__root.deiconify() def __forceupdate(self, event=None): self.__sb.update_views_current() def __toggletrack(self, event=None): if self.__trackp.get(): state = NORMAL fg = self.__radios[0]['foreground'] else: state = DISABLED fg = self.__radios[0]['disabledforeground'] for r in self.__radios: r.configure(state=state) for l in self.__labels: l.configure(foreground=fg) def __set_color(self, event=None): which = self.__which.get() text = self.__text if which == 0: color = text['foreground'] elif which == 1: color = text['background'] elif which == 2: color = text['selectforeground'] elif which == 3: color = text['selectbackground'] elif which == 5: color = text['insertbackground'] try: red, green, blue = ColorDB.rrggbb_to_triplet(color) except ColorDB.BadColor: # must have been a color name red, green, blue = self.__sb.colordb().find_byname(color) self.__sb.update_views(red, green, blue) def update_yourself(self, red, green, blue): if self.__trackp.get(): colorname = ColorDB.triplet_to_rrggbb((red, green, blue)) which = self.__which.get() text = self.__text if which == 0: text.configure(foreground=colorname) elif which == 1: text.configure(background=colorname) elif which == 2: text.configure(selectforeground=colorname) elif which == 3: text.configure(selectbackground=colorname) elif which == 5: text.configure(insertbackground=colorname) def save_options(self, optiondb): optiondb['TRACKP'] = self.__trackp.get() optiondb['WHICH'] = self.__which.get() optiondb['TEXT'] = self.__text.get(0.0, 'end - 1c') optiondb['TEXTSEL'] = self.__text.tag_ranges(SEL)[0:2] optiondb['TEXTINS'] = self.__text.index(INSERT) optiondb['TEXTFG'] = self.__text['foreground'] optiondb['TEXTBG'] = self.__text['background'] optiondb['TEXT_SFG'] = self.__text['selectforeground'] optiondb['TEXT_SBG'] = self.__text['selectbackground'] optiondb['TEXT_IBG'] = self.__text['insertbackground']
lgpl-3.0
Deepakpatle/phantomjs
src/qt/qtwebkit/Tools/Scripts/webkitpy/port/http_lock_unittest.py
124
5481
# Copyright (C) 2010 Gabor Rapcsanyi (rgabor@inf.u-szeged.hu), University of Szeged # # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF SZEGED OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. from http_lock import HttpLock import os # Used for os.getpid() import unittest2 as unittest from webkitpy.common.system.filesystem_mock import MockFileSystem from webkitpy.common.system.executive_mock import MockExecutive # FIXME: These tests all touch the real disk, but could be written to a MockFileSystem instead. class HttpLockTestWithRealFileSystem(unittest.TestCase): # FIXME: Unit tests do not use an __init__ method, but rather setUp and tearDown methods. def __init__(self, testFunc): self.http_lock = HttpLock(None, "WebKitTestHttpd.lock.", "WebKitTest.lock") self.filesystem = self.http_lock._filesystem # FIXME: We should be passing in a MockFileSystem instead. self.lock_file_path_prefix = self.filesystem.join(self.http_lock._lock_path, self.http_lock._lock_file_prefix) self.lock_file_name = self.lock_file_path_prefix + "0" self.guard_lock_file = self.http_lock._guard_lock_file self.clean_all_lockfile() unittest.TestCase.__init__(self, testFunc) def clean_all_lockfile(self): if self.filesystem.exists(self.guard_lock_file): self.filesystem.remove(self.guard_lock_file) lock_list = self.filesystem.glob(self.lock_file_path_prefix + '*') for file_name in lock_list: self.filesystem.remove(file_name) def assertEqual(self, first, second): if first != second: self.clean_all_lockfile() unittest.TestCase.assertEqual(self, first, second) def _check_lock_file(self): if self.filesystem.exists(self.lock_file_name): pid = os.getpid() lock_file_pid = self.filesystem.read_text_file(self.lock_file_name) self.assertEqual(pid, int(lock_file_pid)) return True return False def test_lock_lifecycle(self): self.http_lock._create_lock_file() self.assertEqual(True, self._check_lock_file()) self.assertEqual(1, self.http_lock._next_lock_number()) self.http_lock.cleanup_http_lock() self.assertEqual(False, self._check_lock_file()) self.assertEqual(0, self.http_lock._next_lock_number()) class HttpLockTest(unittest.TestCase): def setUp(self): self.filesystem = MockFileSystem() self.http_lock = HttpLock(None, "WebKitTestHttpd.lock.", "WebKitTest.lock", filesystem=self.filesystem, executive=MockExecutive()) # FIXME: Shouldn't we be able to get these values from the http_lock object directly? self.lock_file_path_prefix = self.filesystem.join(self.http_lock._lock_path, self.http_lock._lock_file_prefix) self.lock_file_name = self.lock_file_path_prefix + "0" def test_current_lock_pid(self): # FIXME: Once Executive wraps getpid, we can mock this and not use a real pid. current_pid = os.getpid() self.http_lock._filesystem.write_text_file(self.lock_file_name, str(current_pid)) self.assertEqual(self.http_lock._current_lock_pid(), current_pid) def test_extract_lock_number(self): lock_file_list = ( self.lock_file_path_prefix + "00", self.lock_file_path_prefix + "9", self.lock_file_path_prefix + "001", self.lock_file_path_prefix + "021", ) expected_number_list = (0, 9, 1, 21) for lock_file, expected in zip(lock_file_list, expected_number_list): self.assertEqual(self.http_lock._extract_lock_number(lock_file), expected) def test_lock_file_list(self): self.http_lock._filesystem = MockFileSystem({ self.lock_file_path_prefix + "6": "", self.lock_file_path_prefix + "1": "", self.lock_file_path_prefix + "4": "", self.lock_file_path_prefix + "3": "", }) expected_file_list = [ self.lock_file_path_prefix + "1", self.lock_file_path_prefix + "3", self.lock_file_path_prefix + "4", self.lock_file_path_prefix + "6", ] self.assertEqual(self.http_lock._lock_file_list(), expected_file_list)
bsd-3-clause
ruilin/RLMap
data/benchmarks/tk_results_viewer.py
53
9617
from Tkinter import * import tkMessageBox import math import sys import copy import os import shutil import time def less_start_time(t1, t2): fmt = '%a_%b_%d_%H:%M:%S_%Y' return time.strptime(t1[0].replace('__','_'), fmt) < time.strptime(t2[0].replace('__','_'), fmt) class BenchmarkResultsFrame(Frame): def __init__(self, cfg_file, results_file, master=None): Frame.__init__(self, master) self.master.title("Benchmark Results Viewer") self.grid(padx=10, pady=10) self.createWidgets() self.resultsDir = os.path.dirname(results_file) self.readResults(cfg_file, results_file) self.current = None self.poll() def scale_level(self, r): dx = 360.0 / (r[2] - r[0]) dy = 360.0 / (r[3] - r[1]) v = (dx + dy) / 2.0 l = math.log(v) / math.log(2.0) + 1 return math.floor(max(0, min(l, 17)) + 0.5) def poll(self): now = self.resultsList.curselection() if now != self.current: self.onResultsChanged(now) self.current = now self.after(100, self.poll) def onResultsChanged(self, now): if len(now) == 0: return self.resultsView.delete(1.0, END) for i in now: start_time = self.resultsList.get(int(i)) for rev_name in self.rev.keys(): if start_time in self.rev[rev_name]: self.resultsView.insert(END, rev_name + "\n") self.resultsView.insert(END, "\t%s\n" % (start_time)) for bench_name in self.rev[rev_name][start_time].keys(): if bench_name not in self.bench_cfg: s = "\t\t%s [config info not found]\n" % (bench_name) self.resultsView.insert(END, s) else: cfg_info = self.bench_cfg[bench_name] if not cfg_info[0]: s = "\t\t%s [%s %s %s %s], endScale=%d\n" % \ (bench_name, cfg_info[1], cfg_info[2], cfg_info[3], cfg_info[4], cfg_info[5]) self.resultsView.insert(END, s) else: s = "\t\t%s endScale=%d\n" % \ (bench_name, cfg_info[1]) self.resultsView.insert(END, s) k = self.rev[rev_name][start_time][bench_name].keys() k.sort() for scale_level in k: s = "\t\t\t scale: %d, duration: %f\n" % \ (scale_level, self.rev[rev_name][start_time][bench_name][scale_level]) self.resultsView.insert(END, s) self.resultsView.insert(END, "-------------------------------------------------------------------\n") if self.hasTraceAttachement(start_time): self.resultsView.insert(END, "%s attached\n" % (self.traceAttachementFile(start_time))) def readResults(self, cfg_file, results_file): self.results_file = results_file f1 = open(cfg_file, "r") lns1 = f1.readlines() lns1 = [l.split(" ") for l in lns1] # reading benchmark configuration info self.bench_cfg = {} for l in lns1: c_name = l[1] is_country = (len(l) == 3) self.bench_cfg[l[1]] = [] self.bench_cfg[l[1]].append(is_country) if len(l) > 0: if not is_country: self.bench_cfg[c_name].append(float(l[2])) self.bench_cfg[c_name].append(float(l[3])) self.bench_cfg[c_name].append(float(l[4])) self.bench_cfg[c_name].append(float(l[5])) self.bench_cfg[c_name].append(int(l[6])) else: self.bench_cfg[c_name].append(int(l[2])) # reading results file f = open(results_file, "r") lns = f.readlines() lns = [l.strip().split(" ") for l in lns] self.rev = {} cur_start_time = None is_session = False self.start_time_list = [] self.completion_status = [] for l in lns: if l[0] == "START": if cur_start_time is not None: if is_session: # unfinished benchmark, mark as incomplete self.completion_status.append(0) else: # unknown status self.completion_status.append(2) cur_start_time = l[1].strip("\n") self.start_time_list.append(cur_start_time) is_session = True continue if l[0] == "END": if not is_session: raise "END without matching START" self.completion_status.append(1) cur_start_time = None is_session = False continue if len(l) < 9: "android benchmarks don't write first item" l = [" "] + l rev_name = l[1] start_time = l[2] bench_name = l[3] if cur_start_time != start_time: # checking whether new start_time breaks current session if is_session: self.completion_status.append(0) is_session = False else: if cur_start_time is not None: # unknown session type self.completion_status.append(2) cur_start_time = start_time self.start_time_list.append(cur_start_time) rect = [float(l[4]), float(l[5]), float(l[6]), float(l[7])] dur = float(l[8]) if rev_name not in self.rev: self.rev[rev_name] = {} if start_time not in self.rev[rev_name]: self.rev[rev_name][start_time] = {} if bench_name not in self.rev[rev_name][start_time]: self.rev[rev_name][start_time][bench_name] = {} scale = self.scale_level(rect) if scale not in self.rev[rev_name][start_time][bench_name]: self.rev[rev_name][start_time][bench_name][scale] = 0 self.rev[rev_name][start_time][bench_name][scale] += dur if cur_start_time is not None: if is_session: self.completion_status.append(0) else: self.completion_status.append(2) # sorting session list. latest results go first. if len(self.start_time_list) != len(self.completion_status): raise "something wrong with file parsing, list sizes don't match" self.start_time_pairs = [(self.start_time_list[i], self.completion_status[i]) for i in range(0, len(self.start_time_list))] self.start_time_pairs.sort(less_start_time) # updating resultList with names and completion status i = 0 for e in self.start_time_pairs: self.resultsList.insert(END, e[0]) if e[1] == 0: self.resultsList.itemconfig(i, fg="red") elif e[1] == 1: self.resultsList.itemconfig(i, fg="blue") elif e[1] == 2: self.resultsList.itemconfig(i, fg="green") i += 1 def hasTraceAttachement(self, start_time): return self.traceAttachementFile(start_time) is not None def traceAttachementName(self, start_time): return start_time.strip("\n").replace("_", "").replace(":", "").replace("-", "") def traceAttachementFile(self, start_time): trace_files = [t for t in os.listdir(os.path.join(os.curdir, self.resultsDir)) if t.endswith(".trace")] sst = self.traceAttachementName(start_time) for tf in trace_files: stf = tf[0:-6].replace("_", "").replace(":", "").replace("-", "") if stf == sst: return tf def deleteTraceAttachement(self, start_time): sst = self.traceAttachementName(start_time) if tkMessageBox.askokcancel("Profiler results found", "Delete " + self.traceAttachementFile(start_time)): shutil.rmtree(self.traceAttachementFile(start_time)) def removeRecord(self, event): idx = self.resultsList.nearest(event.y) start_time = self.resultsList.get(idx) if tkMessageBox.askokcancel("Are you sure?", "Delete results for " + start_time + " session?"): lns = open(self.results_file, "r").readlines() lns = [l for l in lns if l.find(start_time) == -1] open(self.results_file, "w").writelines(lns) self.resultsList.delete(idx) if self.hasTraceAttachement(start_time): self.deleteTraceAttachement(start_time) def createWidgets(self): self.resultsList = Listbox(self, width=30, height=60, selectmode=EXTENDED) self.resultsList.grid(row=0, column=0) self.resultsList.bind("<Double-Button-1>", self.removeRecord) self.resultsView = Text(self, width=80, height=60) self.resultsView.grid(row=0, column=1, columnspan=3) if __name__ == "__main__": if len(sys.argv) < 3: print "usage: python %s [config.info] [results.txt]" % sys.argv[0] root = BenchmarkResultsFrame(sys.argv[1], sys.argv[2]) root.mainloop()
apache-2.0
JoelEager/pyTanks.Server
start.py
1
2083
""" Startup script for the pyTanks server Requirements: Python 3.5 or newer websockets 7.0 (pip install websockets==7.0) Usage: python start.py The pyTanks server uses the settings found in config.py to control how the server works. Those values can be changed directly or be overridden by appending one or more of these command line args: log=n - Overrides the default logging level. (See the usage section of the readme.) ip:port - Overrides the ip and port used to host the server. """ import sys import config def main(): """ Check the environment, apply any command line args to config.py, and start wsServer.py """ # Check Python version if sys.version_info[0] < 3 or sys.version_info[1] < 5: print("Python 3.5 or newer is required to run the pyTanks server") return # Check for websockets from importlib import util if util.find_spec("websockets") is None: print("The websockets module is required to run the pyTanks server") return # Import the code that requires the above things from serverLogic.wsServer import runServer # Parse and apply the args for arg in sys.argv: if arg == sys.argv[0] or arg == "": continue elif arg.startswith("log="): try: config.server.logLevel = int(arg[-1:]) except ValueError: print("Invalid log level") return elif arg.startswith("minPlayers="): try: num = int(arg[-1:]) if num <= 1: print("minPlayers must be greater than 1") return config.server.minPlayers = num except ValueError: print("Invalid min player count") return elif ":" in arg: config.server.ipAndPort = arg else: print(__doc__[__doc__.index("Usage:"):].strip()) return # Start the server runServer() if __name__ == "__main__": main()
mit
bgris/ODL_bgris
lib/python3.5/site-packages/docutils/utils/math/tex2mathml_extern.py
4
5635
#!/usr/bin/env python # -*- coding: utf-8 -*- # :Id: $Id: tex2mathml_extern.py 7861 2015-04-10 23:48:51Z milde $ # :Copyright: © 2015 Günter Milde. # :License: Released under the terms of the `2-Clause BSD license`_, in short: # # Copying and distribution of this file, with or without modification, # are permitted in any medium without royalty provided the copyright # notice and this notice are preserved. # This file is offered as-is, without any warranty. # # .. _2-Clause BSD license: http://www.spdx.org/licenses/BSD-2-Clause # Wrappers for TeX->MathML conversion by external tools # ===================================================== import subprocess document_template = r"""\documentclass{article} \usepackage{amsmath} \begin{document} %s \end{document} """ def latexml(math_code, reporter=None): """Convert LaTeX math code to MathML with LaTeXML_ .. _LaTeXML: http://dlmf.nist.gov/LaTeXML/ """ p = subprocess.Popen(['latexml', '-', # read from stdin # '--preload=amsmath', '--inputencoding=utf8', ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) p.stdin.write((document_template % math_code).encode('utf8')) p.stdin.close() latexml_code = p.stdout.read() latexml_err = p.stderr.read().decode('utf8') if reporter and latexml_err.find('Error') >= 0 or not latexml_code: reporter.error(latexml_err) post_p = subprocess.Popen(['latexmlpost', '-', '--nonumbersections', '--format=xhtml', # '--linelength=78', # experimental '--' ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) post_p.stdin.write(latexml_code) post_p.stdin.close() result = post_p.stdout.read().decode('utf8') post_p_err = post_p.stderr.read().decode('utf8') if reporter and post_p_err.find('Error') >= 0 or not result: reporter.error(post_p_err) # extract MathML code: start,end = result.find('<math'), result.find('</math>')+7 result = result[start:end] if 'class="ltx_ERROR' in result: raise SyntaxError(result) return result def ttm(math_code, reporter=None): """Convert LaTeX math code to MathML with TtM_ .. _TtM: http://hutchinson.belmont.ma.us/tth/mml/ """ p = subprocess.Popen(['ttm', # '-i', # italic font for equations. Default roman. '-u', # unicode character encoding. (Default iso-8859-1). '-r', # output raw MathML (no preamble or postlude) ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) p.stdin.write((document_template % math_code).encode('utf8')) p.stdin.close() result = p.stdout.read() err = p.stderr.read().decode('utf8') if err.find('**** Unknown') >= 0: msg = '\n'.join([line for line in err.splitlines() if line.startswith('****')]) raise SyntaxError('\nMessage from external converter TtM:\n'+ msg) if reporter and err.find('**** Error') >= 0 or not result: reporter.error(err) start,end = result.find('<math'), result.find('</math>')+7 result = result[start:end] return result def blahtexml(math_code, inline=True, reporter=None): """Convert LaTeX math code to MathML with blahtexml_ .. _blahtexml: http://gva.noekeon.org/blahtexml/ """ options = ['--mathml', '--indented', '--spacing', 'moderate', '--mathml-encoding', 'raw', '--other-encoding', 'raw', '--doctype-xhtml+mathml', '--annotate-TeX', ] if inline: mathmode_arg = '' else: mathmode_arg = 'mode="display"' options.append('--displaymath') p = subprocess.Popen(['blahtexml']+options, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) p.stdin.write(math_code.encode('utf8')) p.stdin.close() result = p.stdout.read().decode('utf8') err = p.stderr.read().decode('utf8') print(err) if result.find('<error>') >= 0: raise SyntaxError('\nMessage from external converter blahtexml:\n' +result[result.find('<message>')+9:result.find('</message>')]) if reporter and (err.find('**** Error') >= 0 or not result): reporter.error(err) start,end = result.find('<markup>')+9, result.find('</markup>') result = ('<math xmlns="http://www.w3.org/1998/Math/MathML"%s>\n' '%s</math>\n') % (mathmode_arg, result[start:end]) return result # self-test if __name__ == "__main__": example = r'\frac{\partial \sin^2(\alpha)}{\partial \vec r} \varpi \, \text{Grüße}' # print latexml(example).encode('utf8') # print ttm(example)#.encode('utf8') print(blahtexml(example).encode('utf8'))
gpl-3.0
nullzero/wprobot
scripts/userfixes.py
1
4673
#!/usr/bin/python # -*- coding: utf-8 -*- """ Fix misspelled words """ import init import wp import pywikibot from wp import lre def glob(): global subst subst = lre.Subst() subst.append(u"กฏหมาย", u"กฎหมาย") subst.append(u"กรกฏาคม", u"กรกฎาคม") subst.append(u"กษัตรย์", u"กษัตริย์") subst.append(u"กิติมศักดิ์", u"กิตติมศักดิ์") subst.append(u"ขาดดุลย์", u"ขาดดุล") subst.append(u"คริสตศตวรรษ", u"คริสต์ศตวรรษ") subst.append(u"คริสตศักราช", u"คริสต์ศักราช") subst.append(u"คริสตศาสนา", u"คริสต์ศาสนา") subst.append(u"คริสต์กาล", u"คริสตกาล") subst.append(u"คริสต์เตียน", u"คริสเตียน") subst.append(u"คริส(ต์)?มาส(ต์)?", u"คริสต์มาส") subst.append(u"โครงการณ์", u"โครงการ") subst.append(u"งบดุลย์", u"งบดุล") subst.append(u"ซอฟท์แวร์", u"ซอฟต์แวร์") subst.append(u"ฟัง[กค]์ชั่?น", u"ฟังก์ชัน") subst.append(u"ภาพยนต์", u"ภาพยนตร์") subst.append(u"ผูกพันธ์", u"ผูกพัน") subst.append(u"ลอส ?แองเจ[นลอ]?ล[ีิ]ส", u"ลอสแอนเจลิส") subst.append(u"ลายเซ็นต์", u"ลายเซ็น") subst.append(u"เวคเตอร์", u"เวกเตอร์") subst.append(u"เวท(ย์)?มนตร?์", u"เวทมนตร์") subst.append(u"เว็?[บป]ไซ[ทต]์?", u"เว็บไซต์") subst.append(u"เวอร์ชั่น", u"เวอร์ชัน") subst.append(u"อินเ[ตท]อ(ร์)?เน็?[ตท]", u"อินเทอร์เน็ต") subst.append(u"อั[พป]เด็?[ตท]", u"อัปเดต") subst.append(u"อัพโหลด", u"อัปโหลด") subst.append(u"(?m)^(=+) *(.*?) *(=+) *$", ur"\1 \2 \3") subst.append(u"(?m)^= (.*?) =$", ur"== \1 ==") subst.append(u"\[\[(:?)[Cc]ategory:", ur"[[\1หมวดหมู่:") subst.append(u"\[\[(:?)([Ii]mage|[Ff]ile|ภาพ):", ur"[[\1ไฟล์:") subst.append(u"(?m)^== (แหล่ง|หนังสือ|เอกสาร|แหล่งข้อมูล)อ้างอิง ==$", u"== อ้างอิง ==") subst.append(u"(?m)^== (หัวข้ออื่นที่เกี่ยวข้อง|ดูเพิ่มที่) ==$", u"== ดูเพิ่ม ==") subst.append(u"(?m)^== (เว็บไซต์|โยง|ลิงก์|Link *|(แหล่ง)?(ข้อมูล)?)(ภายนอก|อื่น) ==$", u"== แหล่งข้อมูลอื่น ==") subst.append(u"(?m)^== ลิงก์ ==$", u"== แหล่งข้อมูลอื่น ==") subst.append(u"\{\{[Rr]eflist", u"{{รายการอ้างอิง") subst.append(ur"\[\[ *(.*?)\]\]", ur"[[\1]]") subst.append(ur"\[\[(?!หมวดหมู่)(.*?) *\]\]", ur"[[\1]]") subst.append(u"(?<!วัด)ทรง(เสวย|ประชวร|มีพระ|เป็นพระ|เสด็จ|บรรทม|ผนวช|ทอดพระเนตร|สวรรคต|ตรัส|โปรด|ประสูติ)", r"\1") def fix(s): if "nofixbot" in s: return s return subst.process(s) def main(): #tl = wp.Page(u"Template:บาเบล") for page in site.allpages(filterredir=False, content=True): #for page in tl.embeddedin(content=True): #for page in tl: #page = wp.Page(u"รายชื่อวัดในจังหวัดชัยนาท") pywikibot.output(">>>" + page.title()) text = fix(page.get()) if page.get() != text: pywikibot.showDiff(page.get(), text) if raw_input("...") == "y": try: page.put(text, u"โรบอต: เก็บกวาด", async=True) except: wp.error() pass if __name__ == "__main__": args, site, conf = wp.pre(u"user-fixes") try: glob() main() except: wp.posterror() else: wp.post()
mit
alriddoch/cyphesis
rulesets/mason/world/tasks/Heaping.py
3
5118
#This file is distributed under the terms of the GNU General Public license. #Copyright (C) 2006 Al Riddoch (See the file COPYING for details). from atlas import * from physics import * import math import sys import weakref import server class Heaping(server.Task): """ A task for laying down built up terrain with a digging implement.""" class Obstructed(Exception): "An exception indicating this task is obstructed by an entity>" pass def heap_operation(self, op): """ Op handler for cut op which activates this task """ # print self.__class__.__name__,".setup" if len(op) < 1: sys.stderr.write("%s task has no target in setup op" % self.__class__.__name__) self.target = server.world.get_object_ref(op[0].id) self.tool = op.to self.pos = Point3D(op[0].pos) def tick_operation(self, op): """ Op handler for regular tick op """ # print self.__class__.__name__,".tick" if self.target() is None: # print "Target is no more" self.irrelevant() return old_rate = self.rate self.rate = 0.5 / 0.75 self.progress += 0.5 if old_rate < 0.01: self.progress = 0 # print "%s" % self.pos if self.progress < 1: # print "Not done yet" return self.next_tick(0.75) self.progress = 0 if not hasattr(self, 'terrain_mod') or self.terrain_mod() is None: try: mod = self._find_mod('motte') except Heaping.Obstructed: print "obstructed" self.irrelevant() return if mod is None: # There is no terrain mod where we are digging, return self._create_initial_mod() print "found existing mod" self.terrain_mod = weakref.ref(mod) return self._grow_mod() def _grow_mod(self): mod = self.terrain_mod() # FIXME The face direction should be relative to the shape centre, # rather than entity coords, then when the character is facing out, # the shape should only grow in that direction # Determine which way the user is facing relative to the mod user_to_mod = (mod.location - self.character.location).unit_vector() user_facing = Vector3D(1,0,0) face_factor = user_facing.rotate(self.character.location.orientation) area = mod.terrainmod.shape.area() if face_factor > 0.2: # Character is facing the centre. Heap up mod.terrainmod.height += 1 / area else: # Character is facing away, Heap out area_factor = math.sqrt((area + 1) / area) mod.terrainmod.shape *= area_factor area_map = {'shape': mod.terrainmod.shape.as_data(), 'layer': 7} # FIXME This area is not getting broadcast mod.area = area_map box = mod.terrainmod.shape.footprint() mod.bbox = [box.low_corner().x, box.low_corner().y, 0, box.high_corner().x, box.high_corner().y, mod.terrainmod.height] # We have modified the attribute in place, # so must send an update op to propagate res=Oplist() res.append(Operation("update", to=mod.id)) res.append(self.next_tick(0.75)) return res def _find_mod(self, name): mods = self.target().terrain.find_mods(self.pos) if len(mods) != 0: for mod in mods: if hasattr(mod, 'name') and mod.name == name: return mod raise Heaping.Obstructed, "Another mod is in the way" def _create_initial_mod(self): print "no existing mod" z=self.character.location.coordinates.z + 1.0 modmap = {'height': z, 'shape': Polygon([[ -0.7, -0.7 ], [ -1.0, 0.0 ], [ -0.7, 0.7 ], [ 0.0, 1.0 ], [ 0.7, 0.7 ], [ 1.0, 0.0 ], [ 0.7, -0.7 ], [ 0.0, -1.0 ]]).as_data(), 'type': 'levelmod' } area_map = {'shape': modmap['shape'], 'layer': 7} mod_loc = Location(self.character.location.parent) mod_loc.velocity = Vector3D() mod_loc.coordinates = self.pos mod_create=Operation("create", Entity(name="motte", type="path", location = mod_loc, terrainmod = modmap, area = area_map), to=self.target()) res=Oplist() res.append(mod_create) res.append(self.next_tick(0.75)) return res
gpl-2.0
zstyblik/infernal-twin
build/pillow/Tests/test_image_crop.py
12
1793
from helper import unittest, PillowTestCase, hopper from PIL import Image class TestImageCrop(PillowTestCase): def test_crop(self): def crop(mode): out = hopper(mode).crop((50, 50, 100, 100)) self.assertEqual(out.mode, mode) self.assertEqual(out.size, (50, 50)) for mode in "1", "P", "L", "RGB", "I", "F": crop(mode) def test_wide_crop(self): def crop(*bbox): i = im.crop(bbox) h = i.histogram() while h and not h[-1]: del h[-1] return tuple(h) im = Image.new("L", (100, 100), 1) self.assertEqual(crop(0, 0, 100, 100), (0, 10000)) self.assertEqual(crop(25, 25, 75, 75), (0, 2500)) # sides self.assertEqual(crop(-25, 0, 25, 50), (1250, 1250)) self.assertEqual(crop(0, -25, 50, 25), (1250, 1250)) self.assertEqual(crop(75, 0, 125, 50), (1250, 1250)) self.assertEqual(crop(0, 75, 50, 125), (1250, 1250)) self.assertEqual(crop(-25, 25, 125, 75), (2500, 5000)) self.assertEqual(crop(25, -25, 75, 125), (2500, 5000)) # corners self.assertEqual(crop(-25, -25, 25, 25), (1875, 625)) self.assertEqual(crop(75, -25, 125, 25), (1875, 625)) self.assertEqual(crop(75, 75, 125, 125), (1875, 625)) self.assertEqual(crop(-25, 75, 25, 125), (1875, 625)) def test_negative_crop(self): # Check negative crop size (@PIL171) im = Image.new("L", (512, 512)) im = im.crop((400, 400, 200, 200)) self.assertEqual(im.size, (0, 0)) self.assertEqual(len(im.getdata()), 0) self.assertRaises(IndexError, lambda: im.getdata()[0]) if __name__ == '__main__': unittest.main() # End of file
gpl-3.0
zstackio/zstack-woodpecker
integrationtest/vm/mn_ha/test_all_mn_hosts_force_stop_recover_create_vm.py
2
4023
''' Integration Test for creating KVM VM in MN HA mode with all mn hosts force stop and recovery. @author: Mirabel ''' import zstackwoodpecker.test_util as test_util import zstackwoodpecker.test_state as test_state import zstackwoodpecker.test_lib as test_lib import zstackwoodpecker.operations.node_operations as node_ops import zstackwoodpecker.zstack_test.zstack_test_vm as test_vm_header import test_stub import random import time import os vm = None mn_host_list = None need_recover_mn_host_list = None def test(): global vm global mn_host_list global need_recover_mn_host_list mn_host_list = test_stub.get_mn_host(test_lib.all_scenario_config, test_lib.scenario_file) mn_host_num = len(mn_host_list) test_mn_host_list = random.sample(range(mn_host_num), (mn_host_num + 1) / 2) for host in mn_host_list: test_util.test_logger("force stop host [%s]" % (host.ip_)) test_stub.stop_host(host, test_lib.all_scenario_config, 'cold') need_recover_mn_host_list = range(mn_host_num) test_util.test_logger("wait 10s for MN VM to stop") time.sleep(10) mn_host = test_stub.get_host_by_mn_vm(test_lib.all_scenario_config, test_lib.scenario_file) if len(mn_host) != 0: test_util.test_fail('MN VM is still running on %d host(s)' % len(mn_host)) for index in test_mn_host_list: test_util.test_logger("recover host [%s]" % (mn_host_list[index].ip_)) test_stub.recover_host(mn_host_list[index], test_lib.all_scenario_config, test_lib.deploy_config) need_recover_mn_host_list.remove(index) test_util.test_logger("wait for 20 seconds to see if management node VM starts on any host") time.sleep(20) new_mn_host_ip = test_stub.get_host_by_consul_leader(test_lib.all_scenario_config, test_lib.scenario_file) if new_mn_host_ip == "": test_util.test_fail("management node VM not run correctly on [%s] after its former host [%s] down for 20s" % (new_mn_host_ip, mn_host_list[0].ip_)) count = 60 while count > 0: new_mn_host = test_stub.get_host_by_mn_vm(test_lib.all_scenario_config, test_lib.scenario_file) if len(new_mn_host) == 1: test_util.test_logger("management node VM run after its former host down for 30s") break elif len(new_mn_host) > 1: test_util.test_fail("management node VM runs on more than one host after its former host down") time.sleep(5) count -= 1 if len(new_mn_host) == 0: test_util.test_fail("management node VM does not run after its former host down for 30s") elif len(new_mn_host) > 1: test_util.test_fail("management node VM runs on more than one host after its former host down") #node_ops.wait_for_management_server_start(300) test_stub.wrapper_of_wait_for_management_server_start(600) test_stub.ensure_hosts_connected(exclude_host=[mn_host_list[need_recover_mn_host_list[0]]]) test_stub.ensure_bss_host_connected_from_stop(test_lib.scenario_file, test_lib.all_scenario_config, test_lib.deploy_config) test_stub.ensure_bss_connected() test_stub.ensure_pss_connected() test_stub.return_pass_ahead_if_3sites("TEST PASS") vm = test_stub.create_basic_vm() vm.check() vm.destroy() test_util.test_pass('Create VM Test Success') #Will be called what ever test result is def env_recover(): if need_recover_mn_host_list: for index in need_recover_mn_host_list: test_util.test_logger("recover host: %s" % (mn_host_list[index].ip_)) test_stub.recover_host(mn_host_list[index], test_lib.all_scenario_config, test_lib.deploy_config) test_stub.wait_for_mn_ha_ready(test_lib.all_scenario_config, test_lib.scenario_file) #Will be called only if exception happens in test(). def error_cleanup(): global vm if vm: try: vm.destroy() except: pass
apache-2.0
inscriptionweb/sslstrip
sslstrip/ServerConnectionFactory.py
73
1648
# Copyright (c) 2004-2009 Moxie Marlinspike # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # USA # import logging from twisted.internet.protocol import ClientFactory class ServerConnectionFactory(ClientFactory): def __init__(self, command, uri, postData, headers, client): self.command = command self.uri = uri self.postData = postData self.headers = headers self.client = client def buildProtocol(self, addr): return self.protocol(self.command, self.uri, self.postData, self.headers, self.client) def clientConnectionFailed(self, connector, reason): logging.debug("Server connection failed.") destination = connector.getDestination() if (destination.port != 443): logging.debug("Retrying via SSL") self.client.proxyViaSSL(self.headers['host'], self.command, self.uri, self.postData, self.headers, 443) else: self.client.finish()
gpl-3.0
fedya/ajenti
plugins/squid/main.py
17
1237
from ajenti.ui import * from ajenti.com import implements from ajenti.api import * from ajenti import apis from backend import * class SquidPlugin(apis.services.ServiceControlPlugin): text = 'Squid' icon = '/dl/squid/icon.png' folder = 'servers' service_name = 'squid' def get_config(self): return self.app.get_config(self._cfg) def on_session_start(self): self._tab = 0 self._cfg = SquidConfig(self.app) self._cfg.load() self._parts = sorted(self.app.grab_plugins(apis.squid.IPluginPart), key=lambda x: x.weight) idx = 0 for p in self._parts: p.init(self, self._cfg, idx) idx += 1 def get_main_ui(self): tc = UI.TabControl(active=self._tab) for p in self._parts: tc.add(p.title, p.get_ui()) return UI.Pad(tc) @event('button/click') def on_click(self, event, params, vars=None): for p in self._parts: p.on_click(event, params, vars) @event('dialog/submit') @event('form/submit') def on_submit(self, event, params, vars=None): for p in self._parts: p.on_submit(event, params, vars)
lgpl-3.0
LinusU/ansible-modules-extras
windows/win_nssm.py
80
5380
#!/usr/bin/python # -*- coding: utf-8 -*- # (c) 2015, Heyo # # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see <http://www.gnu.org/licenses/>. # this is a windows documentation stub. actual code lives in the .ps1 # file of the same name DOCUMENTATION = ''' --- module: win_nssm version_added: "2.0" short_description: NSSM - the Non-Sucking Service Manager description: - nssm is a service helper which doesn't suck. See https://nssm.cc/ for more information. requirements: - "nssm >= 2.24.0 # (install via win_chocolatey) win_chocolatey: name=nssm" options: name: description: - Name of the service to operate on required: true state: description: - State of the service on the system - Note that NSSM actions like "pause", "continue", "rotate" do not fit the declarative style of ansible, so these should be implemented via the ansible command module required: false choices: - present - started - stopped - restarted - absent default: started application: description: - The application binary to run as a service - "Specify this whenever the service may need to be installed (state: present, started, stopped, restarted)" - "Note that the application name must look like the following, if the directory includes spaces:" - 'nssm install service "c:\\Program Files\\app.exe\\" "C:\\Path with spaces\\"' - "See commit 0b386fc1984ab74ee59b7bed14b7e8f57212c22b in the nssm.git project for more info (https://git.nssm.cc/?p=nssm.git;a=commit;h=0b386fc1984ab74ee59b7bed14b7e8f57212c22b)" required: false default: null stdout_file: description: - Path to receive output required: false default: null stderr_file: description: - Path to receive error output required: false default: null app_parameters: description: - Parameters to be passed to the application when it starts required: false default: null dependencies: description: - Service dependencies that has to be started to trigger startup, separated by comma. required: false default: null user: description: - User to be used for service startup required: false default: null password: description: - Password to be used for service startup required: false default: null start_mode: description: - If C(auto) is selected, the service will start at bootup. C(manual) means that the service will start only when another service needs it. C(disabled) means that the service will stay off, regardless if it is needed or not. required: true default: auto choices: - auto - manual - disabled author: - "Adam Keech (@smadam813)" - "George Frank (@georgefrank)" - "Hans-Joachim Kliemeck (@h0nIg)" ''' EXAMPLES = ''' # Install and start the foo service - win_nssm: name: foo application: C:\windows\\foo.exe # Install and start the foo service with a key-value pair argument # This will yield the following command: C:\windows\\foo.exe bar "true" - win_nssm: name: foo application: C:\windows\\foo.exe app_parameters: bar: true # Install and start the foo service with a key-value pair argument, where the argument needs to start with a dash # This will yield the following command: C:\windows\\foo.exe -bar "true" - win_nssm: name: foo application: C:\windows\\foo.exe app_parameters: "-bar": true # Install and start the foo service with a single parameter # This will yield the following command: C:\windows\\foo.exe bar - win_nssm: name: foo application: C:\windows\\foo.exe app_parameters: _: bar # Install and start the foo service with a mix of single params, and key value pairs # This will yield the following command: C:\windows\\foo.exe bar -file output.bat - win_nssm: name: foo application: C:\windows\\foo.exe app_parameters: _: bar "-file": "output.bat" # Install and start the foo service, redirecting stdout and stderr to the same file - win_nssm: name: foo application: C:\windows\\foo.exe stdout_file: C:\windows\\foo.log stderr_file: C:\windows\\foo.log # Install and start the foo service, but wait for dependencies tcpip and adf - win_nssm: name: foo application: C:\windows\\foo.exe dependencies: 'adf,tcpip' # Install and start the foo service with dedicated user - win_nssm: name: foo application: C:\windows\\foo.exe user: foouser password: secret # Install the foo service but do not start it automatically - win_nssm: name: foo application: C:\windows\\foo.exe state: present start_mode: manual # Remove the foo service - win_nssm: name: foo state: absent '''
gpl-3.0
kmonsoor/python-for-android
python3-alpha/python3-src/Lib/idlelib/AutoComplete.py
67
9061
"""AutoComplete.py - An IDLE extension for automatically completing names. This extension can complete either attribute names of file names. It can pop a window with all available names, for the user to select from. """ import os import sys import string from idlelib.configHandler import idleConf # This string includes all chars that may be in a file name (without a path # separator) FILENAME_CHARS = string.ascii_letters + string.digits + os.curdir + "._~#$:-" # This string includes all chars that may be in an identifier ID_CHARS = string.ascii_letters + string.digits + "_" # These constants represent the two different types of completions COMPLETE_ATTRIBUTES, COMPLETE_FILES = range(1, 2+1) from idlelib import AutoCompleteWindow from idlelib.HyperParser import HyperParser import __main__ SEPS = os.sep if os.altsep: # e.g. '/' on Windows... SEPS += os.altsep class AutoComplete: menudefs = [ ('edit', [ ("Show Completions", "<<force-open-completions>>"), ]) ] popupwait = idleConf.GetOption("extensions", "AutoComplete", "popupwait", type="int", default=0) def __init__(self, editwin=None): self.editwin = editwin if editwin is None: # subprocess and test return self.text = editwin.text self.autocompletewindow = None # id of delayed call, and the index of the text insert when the delayed # call was issued. If _delayed_completion_id is None, there is no # delayed call. self._delayed_completion_id = None self._delayed_completion_index = None def _make_autocomplete_window(self): return AutoCompleteWindow.AutoCompleteWindow(self.text) def _remove_autocomplete_window(self, event=None): if self.autocompletewindow: self.autocompletewindow.hide_window() self.autocompletewindow = None def force_open_completions_event(self, event): """Happens when the user really wants to open a completion list, even if a function call is needed. """ self.open_completions(True, False, True) def try_open_completions_event(self, event): """Happens when it would be nice to open a completion list, but not really necessary, for example after an dot, so function calls won't be made. """ lastchar = self.text.get("insert-1c") if lastchar == ".": self._open_completions_later(False, False, False, COMPLETE_ATTRIBUTES) elif lastchar in SEPS: self._open_completions_later(False, False, False, COMPLETE_FILES) def autocomplete_event(self, event): """Happens when the user wants to complete his word, and if necessary, open a completion list after that (if there is more than one completion) """ if hasattr(event, "mc_state") and event.mc_state: # A modifier was pressed along with the tab, continue as usual. return if self.autocompletewindow and self.autocompletewindow.is_active(): self.autocompletewindow.complete() return "break" else: opened = self.open_completions(False, True, True) if opened: return "break" def _open_completions_later(self, *args): self._delayed_completion_index = self.text.index("insert") if self._delayed_completion_id is not None: self.text.after_cancel(self._delayed_completion_id) self._delayed_completion_id = \ self.text.after(self.popupwait, self._delayed_open_completions, *args) def _delayed_open_completions(self, *args): self._delayed_completion_id = None if self.text.index("insert") != self._delayed_completion_index: return self.open_completions(*args) def open_completions(self, evalfuncs, complete, userWantsWin, mode=None): """Find the completions and create the AutoCompleteWindow. Return True if successful (no syntax error or so found). if complete is True, then if there's nothing to complete and no start of completion, won't open completions and return False. If mode is given, will open a completion list only in this mode. """ # Cancel another delayed call, if it exists. if self._delayed_completion_id is not None: self.text.after_cancel(self._delayed_completion_id) self._delayed_completion_id = None hp = HyperParser(self.editwin, "insert") curline = self.text.get("insert linestart", "insert") i = j = len(curline) if hp.is_in_string() and (not mode or mode==COMPLETE_FILES): self._remove_autocomplete_window() mode = COMPLETE_FILES while i and curline[i-1] in FILENAME_CHARS: i -= 1 comp_start = curline[i:j] j = i while i and curline[i-1] in FILENAME_CHARS + SEPS: i -= 1 comp_what = curline[i:j] elif hp.is_in_code() and (not mode or mode==COMPLETE_ATTRIBUTES): self._remove_autocomplete_window() mode = COMPLETE_ATTRIBUTES while i and curline[i-1] in ID_CHARS: i -= 1 comp_start = curline[i:j] if i and curline[i-1] == '.': hp.set_index("insert-%dc" % (len(curline)-(i-1))) comp_what = hp.get_expression() if not comp_what or \ (not evalfuncs and comp_what.find('(') != -1): return else: comp_what = "" else: return if complete and not comp_what and not comp_start: return comp_lists = self.fetch_completions(comp_what, mode) if not comp_lists[0]: return self.autocompletewindow = self._make_autocomplete_window() self.autocompletewindow.show_window(comp_lists, "insert-%dc" % len(comp_start), complete, mode, userWantsWin) return True def fetch_completions(self, what, mode): """Return a pair of lists of completions for something. The first list is a sublist of the second. Both are sorted. If there is a Python subprocess, get the comp. list there. Otherwise, either fetch_completions() is running in the subprocess itself or it was called in an IDLE EditorWindow before any script had been run. The subprocess environment is that of the most recently run script. If two unrelated modules are being edited some calltips in the current module may be inoperative if the module was not the last to run. """ try: rpcclt = self.editwin.flist.pyshell.interp.rpcclt except: rpcclt = None if rpcclt: return rpcclt.remotecall("exec", "get_the_completion_list", (what, mode), {}) else: if mode == COMPLETE_ATTRIBUTES: if what == "": namespace = __main__.__dict__.copy() namespace.update(__main__.__builtins__.__dict__) bigl = eval("dir()", namespace) bigl.sort() if "__all__" in bigl: smalll = eval("__all__", namespace) smalll.sort() else: smalll = [s for s in bigl if s[:1] != '_'] else: try: entity = self.get_entity(what) bigl = dir(entity) bigl.sort() if "__all__" in bigl: smalll = entity.__all__ smalll.sort() else: smalll = [s for s in bigl if s[:1] != '_'] except: return [], [] elif mode == COMPLETE_FILES: if what == "": what = "." try: expandedpath = os.path.expanduser(what) bigl = os.listdir(expandedpath) bigl.sort() smalll = [s for s in bigl if s[:1] != '.'] except OSError: return [], [] if not smalll: smalll = bigl return smalll, bigl def get_entity(self, name): """Lookup name in a namespace spanning sys.modules and __main.dict__""" namespace = sys.modules.copy() namespace.update(__main__.__dict__) return eval(name, namespace)
apache-2.0
geminy/aidear
oss/qt/qt-everywhere-opensource-src-5.9.0/qtwebengine/src/3rdparty/chromium/third_party/Python-Markdown/markdown/extensions/attr_list.py
72
6259
""" Attribute List Extension for Python-Markdown ============================================ Adds attribute list syntax. Inspired by [maruku](http://maruku.rubyforge.org/proposal.html#attribute_lists)'s feature of the same name. See <https://pythonhosted.org/Markdown/extensions/attr_list.html> for documentation. Original code Copyright 2011 [Waylan Limberg](http://achinghead.com/). All changes Copyright 2011-2014 The Python Markdown Project License: [BSD](http://www.opensource.org/licenses/bsd-license.php) """ from __future__ import absolute_import from __future__ import unicode_literals from . import Extension from ..treeprocessors import Treeprocessor from ..util import isBlockLevel import re try: Scanner = re.Scanner except AttributeError: # pragma: no cover # must be on Python 2.4 from sre import Scanner def _handle_double_quote(s, t): k, v = t.split('=') return k, v.strip('"') def _handle_single_quote(s, t): k, v = t.split('=') return k, v.strip("'") def _handle_key_value(s, t): return t.split('=') def _handle_word(s, t): if t.startswith('.'): return '.', t[1:] if t.startswith('#'): return 'id', t[1:] return t, t _scanner = Scanner([ (r'[^ ]+=".*?"', _handle_double_quote), (r"[^ ]+='.*?'", _handle_single_quote), (r'[^ ]+=[^ =]+', _handle_key_value), (r'[^ =]+', _handle_word), (r' ', None) ]) def get_attrs(str): """ Parse attribute list and return a list of attribute tuples. """ return _scanner.scan(str)[0] def isheader(elem): return elem.tag in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'] class AttrListTreeprocessor(Treeprocessor): BASE_RE = r'\{\:?([^\}]*)\}' HEADER_RE = re.compile(r'[ ]+%s[ ]*$' % BASE_RE) BLOCK_RE = re.compile(r'\n[ ]*%s[ ]*$' % BASE_RE) INLINE_RE = re.compile(r'^%s' % BASE_RE) NAME_RE = re.compile(r'[^A-Z_a-z\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02ff' r'\u0370-\u037d\u037f-\u1fff\u200c-\u200d' r'\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff' r'\uf900-\ufdcf\ufdf0-\ufffd' r'\:\-\.0-9\u00b7\u0300-\u036f\u203f-\u2040]+') def run(self, doc): for elem in doc.getiterator(): if isBlockLevel(elem.tag): # Block level: check for attrs on last line of text RE = self.BLOCK_RE if isheader(elem) or elem.tag == 'dt': # header or def-term: check for attrs at end of line RE = self.HEADER_RE if len(elem) and elem.tag == 'li': # special case list items. children may include a ul or ol. pos = None # find the ul or ol position for i, child in enumerate(elem): if child.tag in ['ul', 'ol']: pos = i break if pos is None and elem[-1].tail: # use tail of last child. no ul or ol. m = RE.search(elem[-1].tail) if m: self.assign_attrs(elem, m.group(1)) elem[-1].tail = elem[-1].tail[:m.start()] elif pos is not None and pos > 0 and elem[pos-1].tail: # use tail of last child before ul or ol m = RE.search(elem[pos-1].tail) if m: self.assign_attrs(elem, m.group(1)) elem[pos-1].tail = elem[pos-1].tail[:m.start()] elif elem.text: # use text. ul is first child. m = RE.search(elem.text) if m: self.assign_attrs(elem, m.group(1)) elem.text = elem.text[:m.start()] elif len(elem) and elem[-1].tail: # has children. Get from tail of last child m = RE.search(elem[-1].tail) if m: self.assign_attrs(elem, m.group(1)) elem[-1].tail = elem[-1].tail[:m.start()] if isheader(elem): # clean up trailing #s elem[-1].tail = elem[-1].tail.rstrip('#').rstrip() elif elem.text: # no children. Get from text. m = RE.search(elem.text) if not m and elem.tag == 'td': m = re.search(self.BASE_RE, elem.text) if m: self.assign_attrs(elem, m.group(1)) elem.text = elem.text[:m.start()] if isheader(elem): # clean up trailing #s elem.text = elem.text.rstrip('#').rstrip() else: # inline: check for attrs at start of tail if elem.tail: m = self.INLINE_RE.match(elem.tail) if m: self.assign_attrs(elem, m.group(1)) elem.tail = elem.tail[m.end():] def assign_attrs(self, elem, attrs): """ Assign attrs to element. """ for k, v in get_attrs(attrs): if k == '.': # add to class cls = elem.get('class') if cls: elem.set('class', '%s %s' % (cls, v)) else: elem.set('class', v) else: # assign attr k with v elem.set(self.sanitize_name(k), v) def sanitize_name(self, name): """ Sanitize name as 'an XML Name, minus the ":"'. See http://www.w3.org/TR/REC-xml-names/#NT-NCName """ return self.NAME_RE.sub('_', name) class AttrListExtension(Extension): def extendMarkdown(self, md, md_globals): md.treeprocessors.add( 'attr_list', AttrListTreeprocessor(md), '>prettify' ) def makeExtension(*args, **kwargs): return AttrListExtension(*args, **kwargs)
gpl-3.0
Orav/kbengine
kbe/tools/server/webconsole/six.py
18
30966
"""Utilities for writing code that runs on Python 2 and 3""" # Copyright (c) 2010-2015 Benjamin Peterson # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. from __future__ import absolute_import import functools import itertools import operator import sys import types __author__ = "Benjamin Peterson <benjamin@python.org>" __version__ = "1.10.0" # Useful for very coarse version differentiation. PY2 = sys.version_info[0] == 2 PY3 = sys.version_info[0] == 3 PY34 = sys.version_info[0:2] >= (3, 4) if PY3: string_types = str, integer_types = int, class_types = type, text_type = str binary_type = bytes MAXSIZE = sys.maxsize else: string_types = basestring, integer_types = (int, long) class_types = (type, types.ClassType) text_type = unicode binary_type = str if sys.platform.startswith("java"): # Jython always uses 32 bits. MAXSIZE = int((1 << 31) - 1) else: # It's possible to have sizeof(long) != sizeof(Py_ssize_t). class X(object): def __len__(self): return 1 << 31 try: len(X()) except OverflowError: # 32-bit MAXSIZE = int((1 << 31) - 1) else: # 64-bit MAXSIZE = int((1 << 63) - 1) del X def _add_doc(func, doc): """Add documentation to a function.""" func.__doc__ = doc def _import_module(name): """Import module, returning the module after the last dot.""" __import__(name) return sys.modules[name] class _LazyDescr(object): def __init__(self, name): self.name = name def __get__(self, obj, tp): result = self._resolve() setattr(obj, self.name, result) # Invokes __set__. try: # This is a bit ugly, but it avoids running this again by # removing this descriptor. delattr(obj.__class__, self.name) except AttributeError: pass return result class MovedModule(_LazyDescr): def __init__(self, name, old, new=None): super(MovedModule, self).__init__(name) if PY3: if new is None: new = name self.mod = new else: self.mod = old def _resolve(self): return _import_module(self.mod) def __getattr__(self, attr): _module = self._resolve() value = getattr(_module, attr) setattr(self, attr, value) return value class _LazyModule(types.ModuleType): def __init__(self, name): super(_LazyModule, self).__init__(name) self.__doc__ = self.__class__.__doc__ def __dir__(self): attrs = ["__doc__", "__name__"] attrs += [attr.name for attr in self._moved_attributes] return attrs # Subclasses should override this _moved_attributes = [] class MovedAttribute(_LazyDescr): def __init__(self, name, old_mod, new_mod, old_attr=None, new_attr=None): super(MovedAttribute, self).__init__(name) if PY3: if new_mod is None: new_mod = name self.mod = new_mod if new_attr is None: if old_attr is None: new_attr = name else: new_attr = old_attr self.attr = new_attr else: self.mod = old_mod if old_attr is None: old_attr = name self.attr = old_attr def _resolve(self): module = _import_module(self.mod) return getattr(module, self.attr) class _SixMetaPathImporter(object): """ A meta path importer to import six.moves and its submodules. This class implements a PEP302 finder and loader. It should be compatible with Python 2.5 and all existing versions of Python3 """ def __init__(self, six_module_name): self.name = six_module_name self.known_modules = {} def _add_module(self, mod, *fullnames): for fullname in fullnames: self.known_modules[self.name + "." + fullname] = mod def _get_module(self, fullname): return self.known_modules[self.name + "." + fullname] def find_module(self, fullname, path=None): if fullname in self.known_modules: return self return None def __get_module(self, fullname): try: return self.known_modules[fullname] except KeyError: raise ImportError("This loader does not know module " + fullname) def load_module(self, fullname): try: # in case of a reload return sys.modules[fullname] except KeyError: pass mod = self.__get_module(fullname) if isinstance(mod, MovedModule): mod = mod._resolve() else: mod.__loader__ = self sys.modules[fullname] = mod return mod def is_package(self, fullname): """ Return true, if the named module is a package. We need this method to get correct spec objects with Python 3.4 (see PEP451) """ return hasattr(self.__get_module(fullname), "__path__") def get_code(self, fullname): """Return None Required, if is_package is implemented""" self.__get_module(fullname) # eventually raises ImportError return None get_source = get_code # same as get_code _importer = _SixMetaPathImporter(__name__) class _MovedItems(_LazyModule): """Lazy loading of moved objects""" __path__ = [] # mark as package _moved_attributes = [ MovedAttribute("cStringIO", "cStringIO", "io", "StringIO"), MovedAttribute("filter", "itertools", "builtins", "ifilter", "filter"), MovedAttribute("filterfalse", "itertools", "itertools", "ifilterfalse", "filterfalse"), MovedAttribute("input", "__builtin__", "builtins", "raw_input", "input"), MovedAttribute("intern", "__builtin__", "sys"), MovedAttribute("map", "itertools", "builtins", "imap", "map"), MovedAttribute("getcwd", "os", "os", "getcwdu", "getcwd"), MovedAttribute("getcwdb", "os", "os", "getcwd", "getcwdb"), MovedAttribute("range", "__builtin__", "builtins", "xrange", "range"), MovedAttribute("reload_module", "__builtin__", "importlib" if PY34 else "imp", "reload"), MovedAttribute("reduce", "__builtin__", "functools"), MovedAttribute("shlex_quote", "pipes", "shlex", "quote"), MovedAttribute("StringIO", "StringIO", "io"), MovedAttribute("UserDict", "UserDict", "collections"), MovedAttribute("UserList", "UserList", "collections"), MovedAttribute("UserString", "UserString", "collections"), MovedAttribute("xrange", "__builtin__", "builtins", "xrange", "range"), MovedAttribute("zip", "itertools", "builtins", "izip", "zip"), MovedAttribute("zip_longest", "itertools", "itertools", "izip_longest", "zip_longest"), MovedModule("builtins", "__builtin__"), MovedModule("configparser", "ConfigParser"), MovedModule("copyreg", "copy_reg"), MovedModule("dbm_gnu", "gdbm", "dbm.gnu"), MovedModule("_dummy_thread", "dummy_thread", "_dummy_thread"), MovedModule("http_cookiejar", "cookielib", "http.cookiejar"), MovedModule("http_cookies", "Cookie", "http.cookies"), MovedModule("html_entities", "htmlentitydefs", "html.entities"), MovedModule("html_parser", "HTMLParser", "html.parser"), MovedModule("http_client", "httplib", "http.client"), MovedModule("email_mime_multipart", "email.MIMEMultipart", "email.mime.multipart"), MovedModule("email_mime_nonmultipart", "email.MIMENonMultipart", "email.mime.nonmultipart"), MovedModule("email_mime_text", "email.MIMEText", "email.mime.text"), MovedModule("email_mime_base", "email.MIMEBase", "email.mime.base"), MovedModule("BaseHTTPServer", "BaseHTTPServer", "http.server"), MovedModule("CGIHTTPServer", "CGIHTTPServer", "http.server"), MovedModule("SimpleHTTPServer", "SimpleHTTPServer", "http.server"), MovedModule("cPickle", "cPickle", "pickle"), MovedModule("queue", "Queue"), MovedModule("reprlib", "repr"), MovedModule("socketserver", "SocketServer"), MovedModule("_thread", "thread", "_thread"), MovedModule("tkinter", "Tkinter"), MovedModule("tkinter_dialog", "Dialog", "tkinter.dialog"), MovedModule("tkinter_filedialog", "FileDialog", "tkinter.filedialog"), MovedModule("tkinter_scrolledtext", "ScrolledText", "tkinter.scrolledtext"), MovedModule("tkinter_simpledialog", "SimpleDialog", "tkinter.simpledialog"), MovedModule("tkinter_tix", "Tix", "tkinter.tix"), MovedModule("tkinter_ttk", "ttk", "tkinter.ttk"), MovedModule("tkinter_constants", "Tkconstants", "tkinter.constants"), MovedModule("tkinter_dnd", "Tkdnd", "tkinter.dnd"), MovedModule("tkinter_colorchooser", "tkColorChooser", "tkinter.colorchooser"), MovedModule("tkinter_commondialog", "tkCommonDialog", "tkinter.commondialog"), MovedModule("tkinter_tkfiledialog", "tkFileDialog", "tkinter.filedialog"), MovedModule("tkinter_font", "tkFont", "tkinter.font"), MovedModule("tkinter_messagebox", "tkMessageBox", "tkinter.messagebox"), MovedModule("tkinter_tksimpledialog", "tkSimpleDialog", "tkinter.simpledialog"), MovedModule("urllib_parse", __name__ + ".moves.urllib_parse", "urllib.parse"), MovedModule("urllib_error", __name__ + ".moves.urllib_error", "urllib.error"), MovedModule("urllib", __name__ + ".moves.urllib", __name__ + ".moves.urllib"), MovedModule("urllib_robotparser", "robotparser", "urllib.robotparser"), MovedModule("xmlrpc_client", "xmlrpclib", "xmlrpc.client"), MovedModule("xmlrpc_server", "SimpleXMLRPCServer", "xmlrpc.server"), ] # Add windows specific modules. if sys.platform == "win32": _moved_attributes += [ MovedModule("winreg", "_winreg"), ] for attr in _moved_attributes: setattr(_MovedItems, attr.name, attr) if isinstance(attr, MovedModule): _importer._add_module(attr, "moves." + attr.name) del attr _MovedItems._moved_attributes = _moved_attributes moves = _MovedItems(__name__ + ".moves") _importer._add_module(moves, "moves") class Module_six_moves_urllib_parse(_LazyModule): """Lazy loading of moved objects in six.moves.urllib_parse""" _urllib_parse_moved_attributes = [ MovedAttribute("ParseResult", "urlparse", "urllib.parse"), MovedAttribute("SplitResult", "urlparse", "urllib.parse"), MovedAttribute("parse_qs", "urlparse", "urllib.parse"), MovedAttribute("parse_qsl", "urlparse", "urllib.parse"), MovedAttribute("urldefrag", "urlparse", "urllib.parse"), MovedAttribute("urljoin", "urlparse", "urllib.parse"), MovedAttribute("urlparse", "urlparse", "urllib.parse"), MovedAttribute("urlsplit", "urlparse", "urllib.parse"), MovedAttribute("urlunparse", "urlparse", "urllib.parse"), MovedAttribute("urlunsplit", "urlparse", "urllib.parse"), MovedAttribute("quote", "urllib", "urllib.parse"), MovedAttribute("quote_plus", "urllib", "urllib.parse"), MovedAttribute("unquote", "urllib", "urllib.parse"), MovedAttribute("unquote_plus", "urllib", "urllib.parse"), MovedAttribute("urlencode", "urllib", "urllib.parse"), MovedAttribute("splitquery", "urllib", "urllib.parse"), MovedAttribute("splittag", "urllib", "urllib.parse"), MovedAttribute("splituser", "urllib", "urllib.parse"), MovedAttribute("uses_fragment", "urlparse", "urllib.parse"), MovedAttribute("uses_netloc", "urlparse", "urllib.parse"), MovedAttribute("uses_params", "urlparse", "urllib.parse"), MovedAttribute("uses_query", "urlparse", "urllib.parse"), MovedAttribute("uses_relative", "urlparse", "urllib.parse"), ] for attr in _urllib_parse_moved_attributes: setattr(Module_six_moves_urllib_parse, attr.name, attr) del attr Module_six_moves_urllib_parse._moved_attributes = _urllib_parse_moved_attributes _importer._add_module(Module_six_moves_urllib_parse(__name__ + ".moves.urllib_parse"), "moves.urllib_parse", "moves.urllib.parse") class Module_six_moves_urllib_error(_LazyModule): """Lazy loading of moved objects in six.moves.urllib_error""" _urllib_error_moved_attributes = [ MovedAttribute("URLError", "urllib2", "urllib.error"), MovedAttribute("HTTPError", "urllib2", "urllib.error"), MovedAttribute("ContentTooShortError", "urllib", "urllib.error"), ] for attr in _urllib_error_moved_attributes: setattr(Module_six_moves_urllib_error, attr.name, attr) del attr Module_six_moves_urllib_error._moved_attributes = _urllib_error_moved_attributes _importer._add_module(Module_six_moves_urllib_error(__name__ + ".moves.urllib.error"), "moves.urllib_error", "moves.urllib.error") class Module_six_moves_urllib_request(_LazyModule): """Lazy loading of moved objects in six.moves.urllib_request""" _urllib_request_moved_attributes = [ MovedAttribute("urlopen", "urllib2", "urllib.request"), MovedAttribute("install_opener", "urllib2", "urllib.request"), MovedAttribute("build_opener", "urllib2", "urllib.request"), MovedAttribute("pathname2url", "urllib", "urllib.request"), MovedAttribute("url2pathname", "urllib", "urllib.request"), MovedAttribute("getproxies", "urllib", "urllib.request"), MovedAttribute("Request", "urllib2", "urllib.request"), MovedAttribute("OpenerDirector", "urllib2", "urllib.request"), MovedAttribute("HTTPDefaultErrorHandler", "urllib2", "urllib.request"), MovedAttribute("HTTPRedirectHandler", "urllib2", "urllib.request"), MovedAttribute("HTTPCookieProcessor", "urllib2", "urllib.request"), MovedAttribute("ProxyHandler", "urllib2", "urllib.request"), MovedAttribute("BaseHandler", "urllib2", "urllib.request"), MovedAttribute("HTTPPasswordMgr", "urllib2", "urllib.request"), MovedAttribute("HTTPPasswordMgrWithDefaultRealm", "urllib2", "urllib.request"), MovedAttribute("AbstractBasicAuthHandler", "urllib2", "urllib.request"), MovedAttribute("HTTPBasicAuthHandler", "urllib2", "urllib.request"), MovedAttribute("ProxyBasicAuthHandler", "urllib2", "urllib.request"), MovedAttribute("AbstractDigestAuthHandler", "urllib2", "urllib.request"), MovedAttribute("HTTPDigestAuthHandler", "urllib2", "urllib.request"), MovedAttribute("ProxyDigestAuthHandler", "urllib2", "urllib.request"), MovedAttribute("HTTPHandler", "urllib2", "urllib.request"), MovedAttribute("HTTPSHandler", "urllib2", "urllib.request"), MovedAttribute("FileHandler", "urllib2", "urllib.request"), MovedAttribute("FTPHandler", "urllib2", "urllib.request"), MovedAttribute("CacheFTPHandler", "urllib2", "urllib.request"), MovedAttribute("UnknownHandler", "urllib2", "urllib.request"), MovedAttribute("HTTPErrorProcessor", "urllib2", "urllib.request"), MovedAttribute("urlretrieve", "urllib", "urllib.request"), MovedAttribute("urlcleanup", "urllib", "urllib.request"), MovedAttribute("URLopener", "urllib", "urllib.request"), MovedAttribute("FancyURLopener", "urllib", "urllib.request"), MovedAttribute("proxy_bypass", "urllib", "urllib.request"), ] for attr in _urllib_request_moved_attributes: setattr(Module_six_moves_urllib_request, attr.name, attr) del attr Module_six_moves_urllib_request._moved_attributes = _urllib_request_moved_attributes _importer._add_module(Module_six_moves_urllib_request(__name__ + ".moves.urllib.request"), "moves.urllib_request", "moves.urllib.request") class Module_six_moves_urllib_response(_LazyModule): """Lazy loading of moved objects in six.moves.urllib_response""" _urllib_response_moved_attributes = [ MovedAttribute("addbase", "urllib", "urllib.response"), MovedAttribute("addclosehook", "urllib", "urllib.response"), MovedAttribute("addinfo", "urllib", "urllib.response"), MovedAttribute("addinfourl", "urllib", "urllib.response"), ] for attr in _urllib_response_moved_attributes: setattr(Module_six_moves_urllib_response, attr.name, attr) del attr Module_six_moves_urllib_response._moved_attributes = _urllib_response_moved_attributes _importer._add_module(Module_six_moves_urllib_response(__name__ + ".moves.urllib.response"), "moves.urllib_response", "moves.urllib.response") class Module_six_moves_urllib_robotparser(_LazyModule): """Lazy loading of moved objects in six.moves.urllib_robotparser""" _urllib_robotparser_moved_attributes = [ MovedAttribute("RobotFileParser", "robotparser", "urllib.robotparser"), ] for attr in _urllib_robotparser_moved_attributes: setattr(Module_six_moves_urllib_robotparser, attr.name, attr) del attr Module_six_moves_urllib_robotparser._moved_attributes = _urllib_robotparser_moved_attributes _importer._add_module(Module_six_moves_urllib_robotparser(__name__ + ".moves.urllib.robotparser"), "moves.urllib_robotparser", "moves.urllib.robotparser") class Module_six_moves_urllib(types.ModuleType): """Create a six.moves.urllib namespace that resembles the Python 3 namespace""" __path__ = [] # mark as package parse = _importer._get_module("moves.urllib_parse") error = _importer._get_module("moves.urllib_error") request = _importer._get_module("moves.urllib_request") response = _importer._get_module("moves.urllib_response") robotparser = _importer._get_module("moves.urllib_robotparser") def __dir__(self): return ['parse', 'error', 'request', 'response', 'robotparser'] _importer._add_module(Module_six_moves_urllib(__name__ + ".moves.urllib"), "moves.urllib") def add_move(move): """Add an item to six.moves.""" setattr(_MovedItems, move.name, move) def remove_move(name): """Remove item from six.moves.""" try: delattr(_MovedItems, name) except AttributeError: try: del moves.__dict__[name] except KeyError: raise AttributeError("no such move, %r" % (name,)) if PY3: _meth_func = "__func__" _meth_self = "__self__" _func_closure = "__closure__" _func_code = "__code__" _func_defaults = "__defaults__" _func_globals = "__globals__" else: _meth_func = "im_func" _meth_self = "im_self" _func_closure = "func_closure" _func_code = "func_code" _func_defaults = "func_defaults" _func_globals = "func_globals" try: advance_iterator = next except NameError: def advance_iterator(it): return it.next() next = advance_iterator try: callable = callable except NameError: def callable(obj): return any("__call__" in klass.__dict__ for klass in type(obj).__mro__) if PY3: def get_unbound_function(unbound): return unbound create_bound_method = types.MethodType def create_unbound_method(func, cls): return func Iterator = object else: def get_unbound_function(unbound): return unbound.im_func def create_bound_method(func, obj): return types.MethodType(func, obj, obj.__class__) def create_unbound_method(func, cls): return types.MethodType(func, None, cls) class Iterator(object): def next(self): return type(self).__next__(self) callable = callable _add_doc(get_unbound_function, """Get the function out of a possibly unbound function""") get_method_function = operator.attrgetter(_meth_func) get_method_self = operator.attrgetter(_meth_self) get_function_closure = operator.attrgetter(_func_closure) get_function_code = operator.attrgetter(_func_code) get_function_defaults = operator.attrgetter(_func_defaults) get_function_globals = operator.attrgetter(_func_globals) if PY3: def iterkeys(d, **kw): return iter(d.keys(**kw)) def itervalues(d, **kw): return iter(d.values(**kw)) def iteritems(d, **kw): return iter(d.items(**kw)) def iterlists(d, **kw): return iter(d.lists(**kw)) viewkeys = operator.methodcaller("keys") viewvalues = operator.methodcaller("values") viewitems = operator.methodcaller("items") else: def iterkeys(d, **kw): return d.iterkeys(**kw) def itervalues(d, **kw): return d.itervalues(**kw) def iteritems(d, **kw): return d.iteritems(**kw) def iterlists(d, **kw): return d.iterlists(**kw) viewkeys = operator.methodcaller("viewkeys") viewvalues = operator.methodcaller("viewvalues") viewitems = operator.methodcaller("viewitems") _add_doc(iterkeys, "Return an iterator over the keys of a dictionary.") _add_doc(itervalues, "Return an iterator over the values of a dictionary.") _add_doc(iteritems, "Return an iterator over the (key, value) pairs of a dictionary.") _add_doc(iterlists, "Return an iterator over the (key, [values]) pairs of a dictionary.") if PY3: def b(s): return s.encode("latin-1") def u(s): return s unichr = chr import struct int2byte = struct.Struct(">B").pack del struct byte2int = operator.itemgetter(0) indexbytes = operator.getitem iterbytes = iter import io StringIO = io.StringIO BytesIO = io.BytesIO _assertCountEqual = "assertCountEqual" if sys.version_info[1] <= 1: _assertRaisesRegex = "assertRaisesRegexp" _assertRegex = "assertRegexpMatches" else: _assertRaisesRegex = "assertRaisesRegex" _assertRegex = "assertRegex" else: def b(s): return s # Workaround for standalone backslash def u(s): return unicode(s.replace(r'\\', r'\\\\'), "unicode_escape") unichr = unichr int2byte = chr def byte2int(bs): return ord(bs[0]) def indexbytes(buf, i): return ord(buf[i]) iterbytes = functools.partial(itertools.imap, ord) import StringIO StringIO = BytesIO = StringIO.StringIO _assertCountEqual = "assertItemsEqual" _assertRaisesRegex = "assertRaisesRegexp" _assertRegex = "assertRegexpMatches" _add_doc(b, """Byte literal""") _add_doc(u, """Text literal""") def assertCountEqual(self, *args, **kwargs): return getattr(self, _assertCountEqual)(*args, **kwargs) def assertRaisesRegex(self, *args, **kwargs): return getattr(self, _assertRaisesRegex)(*args, **kwargs) def assertRegex(self, *args, **kwargs): return getattr(self, _assertRegex)(*args, **kwargs) if PY3: exec_ = getattr(moves.builtins, "exec") def reraise(tp, value, tb=None): if value is None: value = tp() if value.__traceback__ is not tb: raise value.with_traceback(tb) raise value else: def exec_(_code_, _globs_=None, _locs_=None): """Execute code in a namespace.""" if _globs_ is None: frame = sys._getframe(1) _globs_ = frame.f_globals if _locs_ is None: _locs_ = frame.f_locals del frame elif _locs_ is None: _locs_ = _globs_ exec("""exec _code_ in _globs_, _locs_""") exec_("""def reraise(tp, value, tb=None): raise tp, value, tb """) if sys.version_info[:2] == (3, 2): exec_("""def raise_from(value, from_value): if from_value is None: raise value raise value from from_value """) elif sys.version_info[:2] > (3, 2): exec_("""def raise_from(value, from_value): raise value from from_value """) else: def raise_from(value, from_value): raise value print_ = getattr(moves.builtins, "print", None) if print_ is None: def print_(*args, **kwargs): """The new-style print function for Python 2.4 and 2.5.""" fp = kwargs.pop("file", sys.stdout) if fp is None: return def write(data): if not isinstance(data, basestring): data = str(data) # If the file has an encoding, encode unicode with it. if (isinstance(fp, file) and isinstance(data, unicode) and fp.encoding is not None): errors = getattr(fp, "errors", None) if errors is None: errors = "strict" data = data.encode(fp.encoding, errors) fp.write(data) want_unicode = False sep = kwargs.pop("sep", None) if sep is not None: if isinstance(sep, unicode): want_unicode = True elif not isinstance(sep, str): raise TypeError("sep must be None or a string") end = kwargs.pop("end", None) if end is not None: if isinstance(end, unicode): want_unicode = True elif not isinstance(end, str): raise TypeError("end must be None or a string") if kwargs: raise TypeError("invalid keyword arguments to print()") if not want_unicode: for arg in args: if isinstance(arg, unicode): want_unicode = True break if want_unicode: newline = unicode("\n") space = unicode(" ") else: newline = "\n" space = " " if sep is None: sep = space if end is None: end = newline for i, arg in enumerate(args): if i: write(sep) write(arg) write(end) if sys.version_info[:2] < (3, 3): _print = print_ def print_(*args, **kwargs): fp = kwargs.get("file", sys.stdout) flush = kwargs.pop("flush", False) _print(*args, **kwargs) if flush and fp is not None: fp.flush() _add_doc(reraise, """Reraise an exception.""") if sys.version_info[0:2] < (3, 4): def wraps(wrapped, assigned=functools.WRAPPER_ASSIGNMENTS, updated=functools.WRAPPER_UPDATES): def wrapper(f): f = functools.wraps(wrapped, assigned, updated)(f) f.__wrapped__ = wrapped return f return wrapper else: wraps = functools.wraps def with_metaclass(meta, *bases): """Create a base class with a metaclass.""" # This requires a bit of explanation: the basic idea is to make a dummy # metaclass for one level of class instantiation that replaces itself with # the actual metaclass. class metaclass(meta): def __new__(cls, name, this_bases, d): return meta(name, bases, d) return type.__new__(metaclass, 'temporary_class', (), {}) def add_metaclass(metaclass): """Class decorator for creating a class with a metaclass.""" def wrapper(cls): orig_vars = cls.__dict__.copy() slots = orig_vars.get('__slots__') if slots is not None: if isinstance(slots, str): slots = [slots] for slots_var in slots: orig_vars.pop(slots_var) orig_vars.pop('__dict__', None) orig_vars.pop('__weakref__', None) return metaclass(cls.__name__, cls.__bases__, orig_vars) return wrapper def python_2_unicode_compatible(klass): """ A decorator that defines __unicode__ and __str__ methods under Python 2. Under Python 3 it does nothing. To support Python 2 and 3 with a single code base, define a __str__ method returning text and apply this decorator to the class. """ if PY2: if '__str__' not in klass.__dict__: raise ValueError("@python_2_unicode_compatible cannot be applied " "to %s because it doesn't define __str__()." % klass.__name__) klass.__unicode__ = klass.__str__ klass.__str__ = lambda self: self.__unicode__().encode('utf-8') return klass # Complete the moves implementation. # This code is at the end of this module to speed up module loading. # Turn this module into a package. __path__ = [] # required for PEP 302 and PEP 451 __package__ = __name__ # see PEP 366 @ReservedAssignment if globals().get("__spec__") is not None: __spec__.submodule_search_locations = [] # PEP 451 @UndefinedVariable # Remove other six meta path importers, since they cause problems. This can # happen if six is removed from sys.modules and then reloaded. (Setuptools does # this for some reason.) if sys.meta_path: for i, importer in enumerate(sys.meta_path): # Here's some real nastiness: Another "instance" of the six module might # be floating around. Therefore, we can't use isinstance() to check for # the six meta path importer, since the other six instance will have # inserted an importer with different class. if (type(importer).__name__ == "_SixMetaPathImporter" and importer.name == __name__): del sys.meta_path[i] break del i, importer # Finally, add the importer to the meta path import hook. sys.meta_path.append(_importer)
lgpl-3.0
twoolie/ProjectNarwhal
narwhal/core/profile/admin.py
1
1124
# -*- coding: utf-8 -*- from django.contrib import admin from django.utils.translation import ugettext_lazy as _ from sorl.thumbnail.admin import AdminImageMixin from treebeard.admin import TreeAdmin from models import Profile class ProfileAdmin(AdminImageMixin, admin.ModelAdmin): search_fields = ('user__username', 'extra_data') list_display = (#'user__username', 'user__date_joined', 'user', 'uploaded', 'downloaded',) list_filter = ('user__is_staff',) fields = ('user', 'avatar', 'key', 'downloaded', 'uploaded' ) #fieldsets = ( # (None, { # 'fields': ( ('title', 'slug'), # ('user', 'comments_enabled'), # 'description', ) # }), # (_('Files'), { # 'fields': ( ('torrent', 'image'), ), # }), #(_('Quick Stats'), { # 'classes': ('collapse',), # 'fields': ( ('size', 'files'), # ('seeders', 'leechers'), # ('pub_date', 'comment_count'), ) #}), #) admin.site.register(Profile, ProfileAdmin)
gpl-3.0