Fixing flake8 warnings and errors

flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --exclude benchmark

Excluding the benchmark directory (due to protobuf generated files)
Also removing some Python2 specific code
This commit is contained in:
Jacob Alexander
2019-09-26 22:18:28 -07:00
parent 1f0200af9c
commit b3021e4f6b
37 changed files with 536 additions and 452 deletions

View File

@@ -2,6 +2,7 @@
Largely adapted from h5py Largely adapted from h5py
""" """
# flake8: noqa F401 F403
from .msg import * from .msg import *
from .config import * from .config import *

View File

@@ -5,6 +5,9 @@ import os
import tempfile import tempfile
def build_libcapnp(bundle_dir, build_dir, verbose=False): def build_libcapnp(bundle_dir, build_dir, verbose=False):
'''
Build capnproto
'''
bundle_dir = os.path.abspath(bundle_dir) bundle_dir = os.path.abspath(bundle_dir)
capnp_dir = os.path.join(bundle_dir, 'capnproto-c++') capnp_dir = os.path.join(bundle_dir, 'capnproto-c++')
build_dir = os.path.abspath(build_dir) build_dir = os.path.abspath(build_dir)

View File

@@ -1,12 +1,11 @@
"""utilities for fetching build dependencies.""" """utilities for fetching build dependencies."""
#----------------------------------------------------------------------------- #
# Copyright (C) PyZMQ Developers # Copyright (C) PyZMQ Developers
# Distributed under the terms of the Modified BSD License. # Distributed under the terms of the Modified BSD License.
# #
# This bundling code is largely adapted from pyzmq-static's get.sh by # This bundling code is largely adapted from pyzmq-static's get.sh by
# Brandon Craig-Rhodes, which is itself BSD licensed. # Brandon Craig-Rhodes, which is itself BSD licensed.
#-----------------------------------------------------------------------------
# #
# Adapted for use in pycapnp from pyzmq. See https://github.com/zeromq/pyzmq # Adapted for use in pycapnp from pyzmq. See https://github.com/zeromq/pyzmq
# for original project. # for original project.
@@ -17,7 +16,6 @@ import shutil
import stat import stat
import sys import sys
import tarfile import tarfile
from glob import glob
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
try: try:
@@ -27,27 +25,28 @@ except ImportError:
# py3 # py3
from urllib.request import urlopen from urllib.request import urlopen
from .msg import fatal, debug, info, warn from .msg import fatal, info, warn
pjoin = os.path.join pjoin = os.path.join
#----------------------------------------------------------------------------- #
# Constants # Constants
#----------------------------------------------------------------------------- #
bundled_version = (0,7,0) bundled_version = (0, 7, 4)
libcapnp = "capnproto-c++-%i.%i.%i.tar.gz" % (bundled_version) libcapnp_name = "capnproto-c++-%i.%i.%i.tar.gz" % (bundled_version)
libcapnp_url = "https://capnproto.org/" + libcapnp libcapnp_url = "https://capnproto.org/" + libcapnp_name
HERE = os.path.dirname(__file__) HERE = os.path.dirname(__file__)
ROOT = os.path.dirname(HERE) ROOT = os.path.dirname(HERE)
#----------------------------------------------------------------------------- #
# Utilities # Utilities
#----------------------------------------------------------------------------- #
def untgz(archive): def untgz(archive):
"""Remove .tar.gz"""
return archive.replace('.tar.gz', '') return archive.replace('.tar.gz', '')
def localpath(*args): def localpath(*args):
@@ -69,9 +68,9 @@ def fetch_archive(savedir, url, fname, force=False):
f.write(req.read()) f.write(req.read())
return dest return dest
#----------------------------------------------------------------------------- #
# libcapnp # libcapnp
#----------------------------------------------------------------------------- #
def fetch_libcapnp(savedir, url=None): def fetch_libcapnp(savedir, url=None):
"""download and extract libcapnp""" """download and extract libcapnp"""
@@ -83,7 +82,7 @@ def fetch_libcapnp(savedir, url=None):
if os.path.exists(dest): if os.path.exists(dest):
info("already have %s" % dest) info("already have %s" % dest)
return return
fname = fetch_archive(savedir, url, libcapnp) fname = fetch_archive(savedir, url, libcapnp_name)
tf = tarfile.open(fname) tf = tarfile.open(fname)
with_version = pjoin(savedir, tf.firstmember.path) with_version = pjoin(savedir, tf.firstmember.path)
tf.extractall(savedir) tf.extractall(savedir)
@@ -120,7 +119,7 @@ def stage_platform_hpp(capnproot):
p = Popen('./configure', cwd=capnproot, shell=True, p = Popen('./configure', cwd=capnproot, shell=True,
stdout=PIPE, stderr=PIPE, stdout=PIPE, stderr=PIPE,
) )
o,e = p.communicate() _, e = p.communicate()
if p.returncode: if p.returncode:
warn("failed to configure libcapnp:\n%s" % e) warn("failed to configure libcapnp:\n%s" % e)
if sys.platform == 'darwin': if sys.platform == 'darwin':
@@ -146,14 +145,14 @@ def copy_and_patch_libcapnp(capnp, libcapnp):
if sys.platform.startswith('win'): if sys.platform.startswith('win'):
return return
# copy libcapnp into capnp for bdist # copy libcapnp into capnp for bdist
local = localpath('capnp',libcapnp) local = localpath('capnp', libcapnp)
if not capnp and not os.path.exists(local): if not capnp and not os.path.exists(local):
fatal("Please specify capnp prefix via `setup.py configure --capnp=/path/to/capnp` " fatal("Please specify capnp prefix via `setup.py configure --capnp=/path/to/capnp` "
"or copy libcapnp into capnp/ manually prior to running bdist.") "or copy libcapnp into capnp/ manually prior to running bdist.")
try: try:
# resolve real file through symlinks # resolve real file through symlinks
lib = os.path.realpath(pjoin(capnp, 'lib', libcapnp)) lib = os.path.realpath(pjoin(capnp, 'lib', libcapnp))
print ("copying %s -> %s"%(lib, local)) print ("copying %s -> %s" % (lib, local))
shutil.copy(lib, local) shutil.copy(lib, local)
except Exception: except Exception:
if not os.path.exists(local): if not os.path.exists(local):
@@ -167,11 +166,11 @@ def copy_and_patch_libcapnp(capnp, libcapnp):
mode = os.stat(local).st_mode mode = os.stat(local).st_mode
os.chmod(local, mode | stat.S_IWUSR) os.chmod(local, mode | stat.S_IWUSR)
# patch install_name on darwin, instead of using rpath # patch install_name on darwin, instead of using rpath
cmd = ['install_name_tool', '-id', '@loader_path/../%s'%libcapnp, local] cmd = ['install_name_tool', '-id', '@loader_path/../%s' % libcapnp, local]
try: try:
p = Popen(cmd, stdout=PIPE,stderr=PIPE) p = Popen(cmd, stdout=PIPE, stderr=PIPE)
except OSError: except OSError:
fatal("install_name_tool not found, cannot patch libcapnp for bundling.") fatal("install_name_tool not found, cannot patch libcapnp for bundling.")
out,err = p.communicate() _, err = p.communicate()
if p.returncode: if p.returncode:
fatal("Could not patch bundled libcapnp install_name: %s"%err, p.returncode) fatal("Could not patch bundled libcapnp install_name: %s" % err, p.returncode)

View File

@@ -1,5 +1,5 @@
"""Config functions""" """Config functions"""
#----------------------------------------------------------------------------- #
# Copyright (C) PyZMQ Developers # Copyright (C) PyZMQ Developers
# #
# This file is part of pyzmq, copied and adapted from h5py. # This file is part of pyzmq, copied and adapted from h5py.
@@ -9,23 +9,24 @@
# #
# Distributed under the terms of the New BSD License. The full license is in # Distributed under the terms of the New BSD License. The full license is in
# the file COPYING.BSD, distributed as part of this software. # the file COPYING.BSD, distributed as part of this software.
#----------------------------------------------------------------------------- #
import sys import sys
import os import os
import json import json
from .msg import debug, warn
try: try:
from configparser import ConfigParser from configparser import ConfigParser
except: except Exception:
from ConfigParser import ConfigParser from ConfigParser import ConfigParser
pjoin = os.path.join pjoin = os.path.join
from .msg import debug, fatal, warn
#----------------------------------------------------------------------------- #
# Utility functions (adapted from h5py: http://h5py.googlecode.com) # Utility functions (adapted from h5py: http://h5py.googlecode.com)
#----------------------------------------------------------------------------- #
def load_config(name, base='conf'): def load_config(name, base='conf'):
@@ -46,7 +47,7 @@ def save_config(name, data, base='conf'):
"""Save config dict to JSON""" """Save config dict to JSON"""
if not os.path.exists(base): if not os.path.exists(base):
os.mkdir(base) os.mkdir(base)
fname = pjoin(base, name+'.json') fname = pjoin(base, name + '.json')
with open(fname, 'w') as f: with open(fname, 'w') as f:
json.dump(data, f, indent=2) json.dump(data, f, indent=2)
@@ -130,9 +131,8 @@ def merge(into, d):
else: else:
into[key] = merge(into[key], d[key]) into[key] = merge(into[key], d[key])
return into return into
elif isinstance(into, list): if isinstance(into, list):
return into + d return into + d
else:
return d return d
def discover_settings(conf_base=None): def discover_settings(conf_base=None):

View File

@@ -23,7 +23,7 @@ pjoin = os.path.join
root = os.path.abspath(pjoin(os.path.dirname(__file__), os.path.pardir)) root = os.path.abspath(pjoin(os.path.dirname(__file__), os.path.pardir))
sys.path.insert(0, pjoin(root, 'zmq', 'utils')) sys.path.insert(0, pjoin(root, 'zmq', 'utils'))
from constant_names import all_names, no_prefix from constant_names import all_names, no_prefix # noqa: E402
ifndef_t = """#ifndef {0} ifndef_t = """#ifndef {0}
#define {0} (_PYZMQ_UNDEFINED) #define {0} (_PYZMQ_UNDEFINED)
@@ -79,5 +79,6 @@ def render_constants():
generate_file("constants.pxi", constants_pyx, pjoin(root, 'zmq', 'backend', 'cython')) generate_file("constants.pxi", constants_pyx, pjoin(root, 'zmq', 'backend', 'cython'))
generate_file("zmq_constants.h", ifndefs, pjoin(root, 'zmq', 'utils')) generate_file("zmq_constants.h", ifndefs, pjoin(root, 'zmq', 'utils'))
if __name__ == '__main__': if __name__ == '__main__':
render_constants() render_constants()

View File

@@ -1,5 +1,5 @@
"""Detect zmq version""" """Detect zmq version"""
#----------------------------------------------------------------------------- #
# Copyright (C) PyZMQ Developers # Copyright (C) PyZMQ Developers
# #
# This file is part of pyzmq, copied and adapted from h5py. # This file is part of pyzmq, copied and adapted from h5py.
@@ -9,7 +9,7 @@
# #
# Distributed under the terms of the New BSD License. The full license is in # Distributed under the terms of the New BSD License. The full license is in
# the file COPYING.BSD, distributed as part of this software. # the file COPYING.BSD, distributed as part of this software.
#----------------------------------------------------------------------------- #
# #
# Adapted for use in pycapnp from pyzmq. See https://github.com/zeromq/pyzmq # Adapted for use in pycapnp from pyzmq. See https://github.com/zeromq/pyzmq
# for original project. # for original project.
@@ -29,20 +29,20 @@ from .patch import patch_lib_paths
pjoin = os.path.join pjoin = os.path.join
#----------------------------------------------------------------------------- #
# Utility functions (adapted from h5py: http://h5py.googlecode.com) # Utility functions (adapted from h5py: http://h5py.googlecode.com)
#----------------------------------------------------------------------------- #
def test_compilation(cfile, compiler=None, **compiler_attrs): def test_compilation(cfile, compiler=None, **compiler_attrs):
"""Test simple compilation with given settings""" """Test simple compilation with given settings"""
cc = get_compiler(compiler, **compiler_attrs) cc = get_compiler(compiler, **compiler_attrs)
efile, ext = os.path.splitext(cfile) efile, _ = os.path.splitext(cfile)
cpreargs = lpreargs = [] cpreargs = lpreargs = []
if sys.platform == 'darwin': if sys.platform == 'darwin':
# use appropriate arch for compiler # use appropriate arch for compiler
if platform.architecture()[0]=='32bit': if platform.architecture()[0] == '32bit':
if platform.processor() == 'powerpc': if platform.processor() == 'powerpc':
cpu = 'ppc' cpu = 'ppc'
else: else:
@@ -53,7 +53,7 @@ def test_compilation(cfile, compiler=None, **compiler_attrs):
# allow for missing UB arch, since it will still work: # allow for missing UB arch, since it will still work:
lpreargs = ['-undefined', 'dynamic_lookup'] lpreargs = ['-undefined', 'dynamic_lookup']
if sys.platform == 'sunos5': if sys.platform == 'sunos5':
if platform.architecture()[0]=='32bit': if platform.architecture()[0] == '32bit':
lpreargs = ['-m32'] lpreargs = ['-m32']
else: else:
lpreargs = ['-m64'] lpreargs = ['-m64']
@@ -65,6 +65,7 @@ def test_compilation(cfile, compiler=None, **compiler_attrs):
return efile return efile
def compile_and_run(basedir, src, compiler=None, **compiler_attrs): def compile_and_run(basedir, src, compiler=None, **compiler_attrs):
"""Compile and run"""
if not os.path.exists(basedir): if not os.path.exists(basedir):
os.makedirs(basedir) os.makedirs(basedir)
cfile = pjoin(basedir, os.path.basename(src)) cfile = pjoin(basedir, os.path.basename(src))
@@ -130,7 +131,7 @@ def detect_version(basedir, compiler=None, **compiler_attrs):
rc, so, se = get_output_error([efile]) rc, so, se = get_output_error([efile])
if rc: if rc:
msg = "Error running version detection script:\n%s\n%s" % (so,se) msg = "Error running version detection script:\n%s\n%s" % (so, se)
logging.error(msg) logging.error(msg)
raise IOError(msg) raise IOError(msg)
@@ -161,8 +162,9 @@ def test_build():
return detected return detected
def erase_dir(dir): def erase_dir(path):
"""Erase directory"""
try: try:
shutil.rmtree(dir) shutil.rmtree(path)
except Exception: except Exception:
pass pass

View File

@@ -4,7 +4,6 @@
# Distributed under the terms of the Modified BSD License. # Distributed under the terms of the Modified BSD License.
import os import os
import sys
import logging import logging
from distutils import ccompiler from distutils import ccompiler
from distutils.sysconfig import customize_compiler from distutils.sysconfig import customize_compiler
@@ -14,13 +13,8 @@ from subprocess import Popen, PIPE
pjoin = os.path.join pjoin = os.path.join
if sys.version_info[0] >= 3:
u = lambda x: x
else:
u = lambda x: x.decode('utf8', 'replace')
def customize_mingw(cc): def customize_mingw(cc):
"""customize mingw"""
# strip -mno-cygwin from mingw32 (Python Issue #12641) # strip -mno-cygwin from mingw32 (Python Issue #12641)
for cmd in [cc.compiler, cc.compiler_cxx, cc.compiler_so, cc.linker_exe, cc.linker_so]: for cmd in [cc.compiler, cc.compiler_cxx, cc.compiler_so, cc.linker_exe, cc.linker_so]:
if '-mno-cygwin' in cmd: if '-mno-cygwin' in cmd:
@@ -55,11 +49,10 @@ def get_output_error(cmd):
try: try:
result = Popen(cmd, stdout=PIPE, stderr=PIPE) result = Popen(cmd, stdout=PIPE, stderr=PIPE)
except IOError as e: except IOError as e:
return -1, u(''), u('Failed to run %r: %r' % (cmd, e)) return -1, '', 'Failed to run %r: %r' % (cmd, e)
so, se = result.communicate() so, se = result.communicate()
# unicode: # unicode:
so = so.decode('utf8', 'replace') so = so.decode('utf8', 'replace')
se = se.decode('utf8', 'replace') se = se.decode('utf8', 'replace')
return result.returncode, so, se return result.returncode, so, se

View File

@@ -9,9 +9,9 @@ import os
import sys import sys
import logging import logging
#----------------------------------------------------------------------------- #
# Logging (adapted from h5py: http://h5py.googlecode.com) # Logging (adapted from h5py: http://h5py.googlecode.com)
#----------------------------------------------------------------------------- #
logger = logging.getLogger() logger = logging.getLogger()
@@ -22,18 +22,22 @@ else:
logger.addHandler(logging.StreamHandler(sys.stderr)) logger.addHandler(logging.StreamHandler(sys.stderr))
def debug(msg): def debug(msg):
"""Debug"""
logger.debug(msg) logger.debug(msg)
def info(msg): def info(msg):
"""Info"""
logger.info(msg) logger.info(msg)
def fatal(msg, code=1): def fatal(msg, code=1):
logger.error("Fatal: " + msg) """Fatal"""
logger.error("Fatal: %s", msg)
exit(code) exit(code)
def warn(msg): def warn(msg):
logger.error("Warning: " + msg) """Warning"""
logger.error("Warning: %s", msg)
def line(c='*', width=48): def line(c='*', width=48):
"""Horizontal rule"""
print(c * (width // len(c))) print(c * (width // len(c)))

View File

@@ -20,7 +20,7 @@ LIB_PAT = re.compile(r"\s*(.*) \(compatibility version (\d+\.\d+\.\d+), "
def _get_libs(fname): def _get_libs(fname):
rc, so, se = get_output_error(['otool', '-L', fname]) rc, so, se = get_output_error(['otool', '-L', fname])
if rc: if rc:
logging.error("otool -L %s failed: %r" % (fname, se)) logging.error("otool -L %s failed: %r", fname, se)
return return
for line in so.splitlines()[1:]: for line in so.splitlines()[1:]:
m = LIB_PAT.match(line) m = LIB_PAT.match(line)
@@ -33,6 +33,7 @@ def _find_library(lib, path):
real_lib = os.path.join(d, lib) real_lib = os.path.join(d, lib)
if os.path.exists(real_lib): if os.path.exists(real_lib):
return real_lib return real_lib
return None
def _install_name_change(fname, lib, real_lib): def _install_name_change(fname, lib, real_lib):
rc, so, se = get_output_error(['install_name_tool', '-change', lib, real_lib, fname]) rc, so, se = get_output_error(['install_name_tool', '-change', lib, real_lib, fname])

View File

@@ -31,8 +31,26 @@ Example Usage::
for phone in person.phones: for phone in person.phones:
print(phone.type, ':', phone.number) print(phone.type, ':', phone.number)
""" """
# flake8: noqa F401 F403 F405
from .version import version as __version__ from .version import version as __version__
from .lib.capnp import * from .lib.capnp import *
from .lib.capnp import _DynamicStructReader, _DynamicStructBuilder, _DynamicResizableListBuilder, _DynamicListReader, _DynamicListBuilder, _DynamicOrphan, _DynamicResizableListBuilder, _MallocMessageBuilder, _PackedFdMessageReader, _StreamFdMessageReader, _write_message_to_fd, _write_packed_message_to_fd, _StructModule, _InterfaceModule, _DynamicCapabilityClient, _CapabilityClient, _EventLoop from .lib.capnp import (
_CapabilityClient,
_DynamicCapabilityClient,
_DynamicListBuilder,
_DynamicListReader,
_DynamicOrphan,
_DynamicResizableListBuilder,
_DynamicStructBuilder,
_DynamicStructReader,
_EventLoop,
_InterfaceModule,
_MallocMessageBuilder,
_PackedFdMessageReader,
_StreamFdMessageReader,
_StructModule,
_write_message_to_fd,
_write_packed_message_to_fd,
)
add_import_hook() # enable import hook by default add_import_hook() # enable import hook by default

View File

@@ -15,10 +15,10 @@ def find_type(code, id):
def main(): def main():
env = Environment(loader=PackageLoader('capnp', 'templates')) env = Environment(loader=PackageLoader('capnp', 'templates'))
env.filters['format_name'] = lambda name: name[name.find(':')+1:] env.filters['format_name'] = lambda name: name[name.find(':') + 1:]
code = schema_capnp.CodeGeneratorRequest.read(sys.stdin) code = schema_capnp.CodeGeneratorRequest.read(sys.stdin)
code=code.to_dict() code = code.to_dict()
code['nodes'] = [node for node in code['nodes'] if 'struct' in node and node['scopeId'] != 0] code['nodes'] = [node for node in code['nodes'] if 'struct' in node and node['scopeId'] != 0]
for node in code['nodes']: for node in code['nodes']:
displayName = node['displayName'] displayName = node['displayName']

View File

@@ -1,3 +1,6 @@
'''
Docs configuration
'''
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
# capnp documentation build configuration file, created by # capnp documentation build configuration file, created by
@@ -11,17 +14,19 @@
# All configuration values have a default; values that are commented out # All configuration values have a default; values that are commented out
# serve to show the default. # serve to show the default.
import sys, os, string import string
# import sys, os
import capnp
# If extensions (or modules to document with autodoc) are in another directory, # If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the # add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here. # documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.')) # sys.path.insert(0, os.path.abspath('.'))
# -- General configuration ----------------------------------------------------- # -- General configuration -----------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here. # If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0' # needs_sphinx = '1.0'
# Add any Sphinx extension module names here, as strings. They can be extensions # Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones. # coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
@@ -34,7 +39,7 @@ templates_path = ['_templates']
source_suffix = '.rst' source_suffix = '.rst'
# The encoding of source files. # The encoding of source files.
#source_encoding = 'utf-8-sig' # source_encoding = 'utf-8-sig'
# The master toctree document. # The master toctree document.
master_doc = 'index' master_doc = 'index'
@@ -48,47 +53,46 @@ copyright = u'2013, Author'
# built documents. # built documents.
# #
# The short X.Y version. # The short X.Y version.
import capnp
vs = capnp.__version__ vs = capnp.__version__
# The short X.Y version. # The short X.Y version.
version = vs.rstrip(string.letters) version = vs.rstrip(string.ascii_letters)
# The full version, including alpha/beta/rc tags. # The full version, including alpha/beta/rc tags.
release = vs release = vs
# The language for content autogenerated by Sphinx. Refer to documentation # The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages. # for a list of supported languages.
#language = None # language = None
# There are two options for replacing |today|: either, you set today to some # There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used: # non-false value, then it is used:
#today = '' # today = ''
# Else, today_fmt is used as the format for a strftime call. # Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y' # today_fmt = '%B %d, %Y'
# List of patterns, relative to source directory, that match files and # List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files. # directories to ignore when looking for source files.
exclude_patterns = ['_build'] exclude_patterns = ['_build']
# The reST default role (used for this markup: `text`) to use for all documents. # The reST default role (used for this markup: `text`) to use for all documents.
#default_role = None # default_role = None
# If true, '()' will be appended to :func: etc. cross-reference text. # If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True # add_function_parentheses = True
# If true, the current module name will be prepended to all description # If true, the current module name will be prepended to all description
# unit titles (such as .. function::). # unit titles (such as .. function::).
#add_module_names = True # add_module_names = True
# If true, sectionauthor and moduleauthor directives will be shown in the # If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default. # output. They are ignored by default.
#show_authors = False # show_authors = False
# The name of the Pygments (syntax highlighting) style to use. # The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx' pygments_style = 'sphinx'
# A list of ignored prefixes for module index sorting. # A list of ignored prefixes for module index sorting.
#modindex_common_prefix = [] # modindex_common_prefix = []
# -- Options for HTML output --------------------------------------------------- # -- Options for HTML output ---------------------------------------------------
@@ -100,26 +104,26 @@ html_theme = 'nature'
# Theme options are theme-specific and customize the look and feel of a theme # Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the # further. For a list of options available for each theme, see the
# documentation. # documentation.
#html_theme_options = {} # html_theme_options = {}
# Add any paths that contain custom themes here, relative to this directory. # Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = [] # html_theme_path = []
# The name for this set of Sphinx documents. If None, it defaults to # The name for this set of Sphinx documents. If None, it defaults to
# "<project> v<release> documentation". # "<project> v<release> documentation".
#html_title = None # html_title = None
# A shorter title for the navigation bar. Default is the same as html_title. # A shorter title for the navigation bar. Default is the same as html_title.
#html_short_title = None # html_short_title = None
# The name of an image file (relative to this directory) to place at the top # The name of an image file (relative to this directory) to place at the top
# of the sidebar. # of the sidebar.
#html_logo = None # html_logo = None
# The name of an image file (within the static path) to use as favicon of the # The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large. # pixels large.
#html_favicon = None # html_favicon = None
# Add any paths that contain custom static files (such as style sheets) here, # Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files, # relative to this directory. They are copied after the builtin static files,
@@ -128,44 +132,44 @@ html_static_path = ['_static']
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format. # using the given strftime format.
#html_last_updated_fmt = '%b %d, %Y' # html_last_updated_fmt = '%b %d, %Y'
# If true, SmartyPants will be used to convert quotes and dashes to # If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities. # typographically correct entities.
#html_use_smartypants = True # html_use_smartypants = True
# Custom sidebar templates, maps document names to template names. # Custom sidebar templates, maps document names to template names.
#html_sidebars = {} # html_sidebars = {}
# Additional templates that should be rendered to pages, maps page names to # Additional templates that should be rendered to pages, maps page names to
# template names. # template names.
#html_additional_pages = {} # html_additional_pages = {}
# If false, no module index is generated. # If false, no module index is generated.
#html_domain_indices = True # html_domain_indices = True
# If false, no index is generated. # If false, no index is generated.
#html_use_index = True # html_use_index = True
# If true, the index is split into individual pages for each letter. # If true, the index is split into individual pages for each letter.
#html_split_index = False # html_split_index = False
# If true, links to the reST sources are added to the pages. # If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True # html_show_sourcelink = True
# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. # If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True # html_show_sphinx = True
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True # html_show_copyright = True
# If true, an OpenSearch description file will be output, and all pages will # If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it. The value of this option must be the # contain a <link> tag referring to it. The value of this option must be the
# base URL from which the finished HTML is served. # base URL from which the finished HTML is served.
#html_use_opensearch = '' # html_use_opensearch = ''
# This is the file name suffix for HTML files (e.g. ".xhtml"). # This is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = None # html_file_suffix = None
# Output file base name for HTML help builder. # Output file base name for HTML help builder.
htmlhelp_basename = 'capnpdoc' htmlhelp_basename = 'capnpdoc'
@@ -173,15 +177,15 @@ htmlhelp_basename = 'capnpdoc'
# -- Options for LaTeX output -------------------------------------------------- # -- Options for LaTeX output --------------------------------------------------
latex_elements = {
# The paper size ('letterpaper' or 'a4paper'). # The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper', # 'papersize': 'letterpaper',
# The font size ('10pt', '11pt' or '12pt'). # The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt', # 'pointsize': '10pt',
# Additional stuff for the LaTeX preamble. # Additional stuff for the LaTeX preamble.
#'preamble': '', # 'preamble': '',
latex_elements = {
} }
# Grouping the document tree into LaTeX files. List of tuples # Grouping the document tree into LaTeX files. List of tuples
@@ -193,23 +197,23 @@ latex_documents = [
# The name of an image file (relative to this directory) to place at the top of # The name of an image file (relative to this directory) to place at the top of
# the title page. # the title page.
#latex_logo = None # latex_logo = None
# For "manual" documents, if this is true, then toplevel headings are parts, # For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters. # not chapters.
#latex_use_parts = False # latex_use_parts = False
# If true, show page references after internal links. # If true, show page references after internal links.
#latex_show_pagerefs = False # latex_show_pagerefs = False
# If true, show URL addresses after external links. # If true, show URL addresses after external links.
#latex_show_urls = False # latex_show_urls = False
# Documents to append as an appendix to all manuals. # Documents to append as an appendix to all manuals.
#latex_appendices = [] # latex_appendices = []
# If false, no module index is generated. # If false, no module index is generated.
#latex_domain_indices = True # latex_domain_indices = True
# -- Options for manual page output -------------------------------------------- # -- Options for manual page output --------------------------------------------
@@ -222,7 +226,7 @@ man_pages = [
] ]
# If true, show URL addresses after external links. # If true, show URL addresses after external links.
#man_show_urls = False # man_show_urls = False
# -- Options for Texinfo output ------------------------------------------------ # -- Options for Texinfo output ------------------------------------------------
@@ -237,13 +241,13 @@ texinfo_documents = [
] ]
# Documents to append as an appendix to all manuals. # Documents to append as an appendix to all manuals.
#texinfo_appendices = [] # texinfo_appendices = []
# If false, no module index is generated. # If false, no module index is generated.
#texinfo_domain_indices = True # texinfo_domain_indices = True
# How to display URL addresses: 'footnote', 'no', or 'inline'. # How to display URL addresses: 'footnote', 'no', or 'inline'.
#texinfo_show_urls = 'footnote' # texinfo_show_urls = 'footnote'
# -- Options for Epub output --------------------------------------------------- # -- Options for Epub output ---------------------------------------------------
@@ -256,36 +260,36 @@ epub_copyright = u'2013, Author'
# The language of the text. It defaults to the language option # The language of the text. It defaults to the language option
# or en if the language is not set. # or en if the language is not set.
#epub_language = '' # epub_language = ''
# The scheme of the identifier. Typical schemes are ISBN or URL. # The scheme of the identifier. Typical schemes are ISBN or URL.
#epub_scheme = '' # epub_scheme = ''
# The unique identifier of the text. This can be a ISBN number # The unique identifier of the text. This can be a ISBN number
# or the project homepage. # or the project homepage.
#epub_identifier = '' # epub_identifier = ''
# A unique identification for the text. # A unique identification for the text.
#epub_uid = '' # epub_uid = ''
# A tuple containing the cover image and cover page html template filenames. # A tuple containing the cover image and cover page html template filenames.
#epub_cover = () # epub_cover = ()
# HTML files that should be inserted before the pages created by sphinx. # HTML files that should be inserted before the pages created by sphinx.
# The format is a list of tuples containing the path and title. # The format is a list of tuples containing the path and title.
#epub_pre_files = [] # epub_pre_files = []
# HTML files shat should be inserted after the pages created by sphinx. # HTML files shat should be inserted after the pages created by sphinx.
# The format is a list of tuples containing the path and title. # The format is a list of tuples containing the path and title.
#epub_post_files = [] # epub_post_files = []
# A list of files that should not be packed into the epub file. # A list of files that should not be packed into the epub file.
#epub_exclude_files = [] # epub_exclude_files = []
# The depth of the table of contents in toc.ncx. # The depth of the table of contents in toc.ncx.
#epub_tocdepth = 3 # epub_tocdepth = 3
# Allow duplicate toc entries. # Allow duplicate toc entries.
#epub_tocdup = True # epub_tocdup = True
intersphinx_mapping = {'http://docs.python.org/': None} intersphinx_mapping = {'http://docs.python.org/': None}

View File

@@ -1,6 +1,5 @@
from __future__ import print_function from __future__ import print_function
import os import capnp # noqa: F401
import capnp
import addressbook_capnp import addressbook_capnp

View File

@@ -4,7 +4,6 @@ from __future__ import print_function
import asyncio import asyncio
import argparse import argparse
import threading
import time import time
import capnp import capnp
import socket import socket
@@ -24,7 +23,6 @@ at the given address and does some RPCs')
class StatusSubscriber(thread_capnp.Example.StatusSubscriber.Server): class StatusSubscriber(thread_capnp.Example.StatusSubscriber.Server):
'''An implementation of the StatusSubscriber interface''' '''An implementation of the StatusSubscriber interface'''
def status(self, value, **kwargs): def status(self, value, **kwargs):
@@ -60,7 +58,7 @@ async def main(host):
reader, writer = await asyncio.open_connection( reader, writer = await asyncio.open_connection(
addr, port, addr, port,
) )
except: except Exception:
print("Try IPv6") print("Try IPv6")
reader, writer = await asyncio.open_connection( reader, writer = await asyncio.open_connection(
addr, port, addr, port,

View File

@@ -72,7 +72,7 @@ async def main():
myserver, myserver,
addr, port, addr, port,
) )
except: except Exception:
print("Try IPv6") print("Try IPv6")
server = await asyncio.start_server( server = await asyncio.start_server(
myserver, myserver,
@@ -83,5 +83,6 @@ async def main():
async with server: async with server:
await server.serve_forever() await server.serve_forever()
if __name__ == '__main__': if __name__ == '__main__':
asyncio.run(main()) asyncio.run(main())

View File

@@ -4,7 +4,6 @@ from __future__ import print_function
import asyncio import asyncio
import argparse import argparse
import threading
import time import time
import capnp import capnp
import socket import socket
@@ -25,7 +24,6 @@ at the given address and does some RPCs')
class StatusSubscriber(thread_capnp.Example.StatusSubscriber.Server): class StatusSubscriber(thread_capnp.Example.StatusSubscriber.Server):
'''An implementation of the StatusSubscriber interface''' '''An implementation of the StatusSubscriber interface'''
def status(self, value, **kwargs): def status(self, value, **kwargs):
@@ -66,7 +64,7 @@ async def main(host):
addr, port, addr, port,
ssl=ctx, ssl=ctx,
) )
except: except Exception:
print("Try IPv6") print("Try IPv6")
reader, writer = await asyncio.open_connection( reader, writer = await asyncio.open_connection(
addr, port, addr, port,
@@ -98,6 +96,6 @@ async def main(host):
if __name__ == '__main__': if __name__ == '__main__':
# Using asyncio.run hits an asyncio ssl bug # Using asyncio.run hits an asyncio ssl bug
# https://bugs.python.org/issue36709 # https://bugs.python.org/issue36709
#asyncio.run(main(parse_args().host), loop=loop, debug=True) # asyncio.run(main(parse_args().host), loop=loop, debug=True)
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.run_until_complete(main(parse_args().host)) loop.run_until_complete(main(parse_args().host))

View File

@@ -78,7 +78,7 @@ async def main():
addr, port, addr, port,
ssl=ctx, ssl=ctx,
) )
except: except Exception:
print("Try IPv6") print("Try IPv6")
server = await asyncio.start_server( server = await asyncio.start_server(
myserver, myserver,

View File

@@ -2,7 +2,6 @@
from __future__ import print_function from __future__ import print_function
import argparse import argparse
import socket
import capnp import capnp
import calculator_capnp import calculator_capnp
@@ -302,5 +301,6 @@ def main(host):
print("PASS") print("PASS")
if __name__ == '__main__': if __name__ == '__main__':
main(parse_args().host) main(parse_args().host)

View File

@@ -2,8 +2,6 @@
from __future__ import print_function from __future__ import print_function
import argparse import argparse
import socket
import random
import capnp import capnp
import calculator_capnp import calculator_capnp
@@ -136,5 +134,6 @@ def main():
server = capnp.TwoPartyServer(address, bootstrap=CalculatorImpl()) server = capnp.TwoPartyServer(address, bootstrap=CalculatorImpl())
server.run_forever() server.run_forever()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@@ -54,5 +54,6 @@ def main(host):
cap.longRunning().wait() cap.longRunning().wait()
print('main: {}'.format(time.time())) print('main: {}'.format(time.time()))
if __name__ == '__main__': if __name__ == '__main__':
main(parse_args().host) main(parse_args().host)

View File

@@ -37,5 +37,6 @@ def main():
server = capnp.TwoPartyServer(address, bootstrap=ExampleImpl()) server = capnp.TwoPartyServer(address, bootstrap=ExampleImpl())
server.run_forever() server.run_forever()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@@ -41,4 +41,5 @@ def main():
globals()[command](**kwargs) # hacky way to get defined functions, and call function with name=command globals()[command](**kwargs) # hacky way to get defined functions, and call function with name=command
main() main()

View File

@@ -1,12 +1,13 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function from __future__ import print_function
import capnp
import os import os
import sys
import capnp
capnp.add_import_hook([os.getcwd(), "/usr/local/include/"]) # change this to be auto-detected? capnp.add_import_hook([os.getcwd(), "/usr/local/include/"]) # change this to be auto-detected?
import test_capnp import test_capnp # noqa: E402
import sys
def decode(name): def decode(name):
class_name = name[0].upper() + name[1:] class_name = name[0].upper() + name[1:]
@@ -18,6 +19,7 @@ def encode(name):
message = getattr(test_capnp, class_name).from_dict(val.to_dict()) message = getattr(test_capnp, class_name).from_dict(val.to_dict())
print(message.to_bytes()) print(message.to_bytes())
if sys.argv[1] == 'decode': if sys.argv[1] == 'decode':
decode(sys.argv[2]) decode(sys.argv[2])
else: else:

View File

@@ -1,15 +1,23 @@
#!/usr/bin/env python #!/usr/bin/env python
'''
pycapnp distutils setup.py
'''
from __future__ import print_function from __future__ import print_function
use_cython = False
from setuptools import setup
import os import os
import sys import sys
from buildutils import test_build, fetch_libcapnp, build_libcapnp, info
from distutils.command.clean import clean as _clean
from distutils.errors import CompileError from distutils.errors import CompileError
from distutils.extension import Extension from distutils.extension import Extension
from setuptools import setup
from buildutils import test_build, fetch_libcapnp, build_libcapnp, info
use_cython = False
_this_dir = os.path.dirname(__file__) _this_dir = os.path.dirname(__file__)
MAJOR = 0 MAJOR = 0
@@ -20,10 +28,14 @@ VERSION = '%d.%d.%d' % (MAJOR, MINOR, MICRO)
# Write version info # Write version info
def write_version_py(filename=None): def write_version_py(filename=None):
'''
Generate pycapnp version
'''
cnt = """\ cnt = """\
version = '%s' version = '%s'
short_version = '%s' short_version = '%s'
# flake8: noqa E402 F401
from .lib.capnp import _CAPNP_VERSION_MAJOR as LIBCAPNP_VERSION_MAJOR from .lib.capnp import _CAPNP_VERSION_MAJOR as LIBCAPNP_VERSION_MAJOR
from .lib.capnp import _CAPNP_VERSION_MINOR as LIBCAPNP_VERSION_MINOR from .lib.capnp import _CAPNP_VERSION_MINOR as LIBCAPNP_VERSION_MINOR
from .lib.capnp import _CAPNP_VERSION_MICRO as LIBCAPNP_VERSION_MICRO from .lib.capnp import _CAPNP_VERSION_MICRO as LIBCAPNP_VERSION_MICRO
@@ -39,6 +51,7 @@ from .lib.capnp import _CAPNP_VERSION as LIBCAPNP_VERSION
finally: finally:
a.close() a.close()
write_version_py() write_version_py()
# Try to convert README using pandoc # Try to convert README using pandoc
@@ -49,13 +62,14 @@ try:
changelog = '\nChangelog\n=============\n' + changelog changelog = '\nChangelog\n=============\n' + changelog
long_description += changelog long_description += changelog
except (IOError, ImportError): except (IOError, ImportError):
if len(sys.argv) and sys.argv[-1] == 'sdist': if sys.argv and sys.argv[-1] == 'sdist':
raise raise
long_description = '' long_description = ''
# Clean command, invoked with `python setup.py clean`
from distutils.command.clean import clean as _clean
class clean(_clean): class clean(_clean):
'''
Clean command, invoked with `python setup.py clean`
'''
def run(self): def run(self):
_clean.run(self) _clean.run(self)
for x in [ 'capnp/lib/capnp.cpp', 'capnp/lib/capnp.h', 'capnp/version.py' ]: for x in [ 'capnp/lib/capnp.cpp', 'capnp/lib/capnp.h', 'capnp/version.py' ]:
@@ -65,6 +79,7 @@ class clean(_clean):
except OSError: except OSError:
pass pass
# set use_cython if lib/capnp.cpp is not detected # set use_cython if lib/capnp.cpp is not detected
capnp_compiled_file = os.path.join(os.path.dirname(__file__), 'capnp', 'lib', 'capnp.cpp') capnp_compiled_file = os.path.join(os.path.dirname(__file__), 'capnp', 'lib', 'capnp.cpp')
if not os.path.isfile(capnp_compiled_file): if not os.path.isfile(capnp_compiled_file):
@@ -87,7 +102,7 @@ try:
libcapnp_url = sys.argv[libcapnp_url_index + 1] libcapnp_url = sys.argv[libcapnp_url_index + 1]
sys.argv.remove("--libcapnp-url") sys.argv.remove("--libcapnp-url")
sys.argv.remove(libcapnp_url) sys.argv.remove(libcapnp_url)
except: except Exception:
pass pass
if use_cython: if use_cython:
@@ -96,6 +111,9 @@ else:
from distutils.command.build_ext import build_ext as build_ext_c from distutils.command.build_ext import build_ext as build_ext_c
class build_libcapnp_ext(build_ext_c): class build_libcapnp_ext(build_ext_c):
'''
Build capnproto library
'''
def build_extension(self, ext): def build_extension(self, ext):
build_ext_c.build_extension(self, ext) build_ext_c.build_extension(self, ext)
@@ -114,7 +132,12 @@ class build_libcapnp_ext(build_ext_c):
need_build = True need_build = True
if need_build: if need_build:
info("*WARNING* no libcapnp detected or rebuild forced. Will download and build it from source now. If you have C++ Cap'n Proto installed, it may be out of date or is not being detected. Downloading and building libcapnp may take a while.") info(
"*WARNING* no libcapnp detected or rebuild forced. "
"Will download and build it from source now. "
"If you have C++ Cap'n Proto installed, it may be out of date or is not being detected. "
"Downloading and building libcapnp may take a while."
)
bundle_dir = os.path.join(_this_dir, "bundled") bundle_dir = os.path.join(_this_dir, "bundled")
if not os.path.exists(bundle_dir): if not os.path.exists(bundle_dir):
os.mkdir(bundle_dir) os.mkdir(bundle_dir)
@@ -130,9 +153,10 @@ class build_libcapnp_ext(build_ext_c):
return build_ext_c.run(self) return build_ext_c.run(self)
if use_cython: if use_cython:
from Cython.Build import cythonize from Cython.Build import cythonize
import Cython import Cython # noqa: F401
extensions = cythonize('capnp/lib/*.pyx') extensions = cythonize('capnp/lib/*.pyx')
else: else:
extensions = [Extension("capnp.lib.capnp", ["capnp/lib/capnp.cpp"], extensions = [Extension("capnp.lib.capnp", ["capnp/lib/capnp.cpp"],
@@ -145,9 +169,14 @@ setup(
name="pycapnp", name="pycapnp",
packages=["capnp"], packages=["capnp"],
version=VERSION, version=VERSION,
package_data={'capnp': ['*.pxd', '*.h', '*.capnp', 'helpers/*.pxd', 'helpers/*.h', 'includes/*.pxd', 'lib/*.pxd', 'lib/*.py', 'lib/*.pyx', 'templates/*']}, package_data={
'capnp': [
'*.pxd', '*.h', '*.capnp', 'helpers/*.pxd', 'helpers/*.h',
'includes/*.pxd', 'lib/*.pxd', 'lib/*.py', 'lib/*.pyx', 'templates/*'
]
},
ext_modules=extensions, ext_modules=extensions,
cmdclass = { cmdclass={
'clean': clean, 'clean': clean,
'build_ext': build_libcapnp_ext 'build_ext': build_libcapnp_ext
}, },
@@ -161,10 +190,10 @@ setup(
license='BSD', license='BSD',
author="Jason Paryani", author="Jason Paryani",
author_email="pypi-contact@jparyani.com", author_email="pypi-contact@jparyani.com",
url = 'https://github.com/jparyani/pycapnp', url='https://github.com/jparyani/pycapnp',
download_url = 'https://github.com/jparyani/pycapnp/archive/v%s.zip' % VERSION, download_url='https://github.com/jparyani/pycapnp/archive/v%s.zip' % VERSION,
keywords = ['capnp', 'capnproto', "Cap'n Proto"], keywords=['capnp', 'capnproto', "Cap'n Proto"],
classifiers = [ classifiers=[
'Development Status :: 4 - Beta', 'Development Status :: 4 - Beta',
'Intended Audience :: Developers', 'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License', 'License :: OSI Approved :: BSD License',
@@ -172,12 +201,9 @@ setup(
'Operating System :: POSIX', 'Operating System :: POSIX',
'Programming Language :: C++', 'Programming Language :: C++',
'Programming Language :: Cython', 'Programming Language :: Cython',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: Implementation :: PyPy', 'Programming Language :: Python :: Implementation :: PyPy',
'Topic :: Communications'], 'Topic :: Communications'],
) )

View File

@@ -1,8 +1,7 @@
import pytest import pytest
import capnp
import os
import time import time
import capnp
import test_capability_capnp as capability import test_capability_capnp as capability
class Server(capability.TestInterface.Server): class Server(capability.TestInterface.Server):
@@ -160,6 +159,7 @@ class BadPipelineServer(capability.TestPipeline.Server):
_results = _context.results _results = _context.results
_results.s = response.x + '_foo' _results.s = response.x + '_foo'
_results.outBox.cap = Server(100) _results.outBox.cap = Server(100)
def _error(error): def _error(error):
raise Exception('test was a success') raise Exception('test was a success')
@@ -186,7 +186,7 @@ def test_pipeline_exception():
pipelinePromise = outCap.foo(i=10) pipelinePromise = outCap.foo(i=10)
with pytest.raises(Exception): with pytest.raises(Exception):
loop.wait(pipelinePromise) pipelinePromise.wait()
with pytest.raises(Exception): with pytest.raises(Exception):
remote.wait() remote.wait()
@@ -194,7 +194,7 @@ def test_pipeline_exception():
def test_casting(): def test_casting():
client = capability.TestExtends._new_client(Server()) client = capability.TestExtends._new_client(Server())
client2 = client.upcast(capability.TestInterface) client2 = client.upcast(capability.TestInterface)
client3 = client2.cast_as(capability.TestInterface) _ = client2.cast_as(capability.TestInterface)
with pytest.raises(Exception): with pytest.raises(Exception):
client.upcast(capability.TestPipeline) client.upcast(capability.TestPipeline)

View File

@@ -1,6 +1,7 @@
import pytest
import capnp
import os import os
import pytest
import capnp
this_dir = os.path.dirname(__file__) this_dir = os.path.dirname(__file__)
@@ -150,6 +151,7 @@ class BadPipelineServer:
def _then(response): def _then(response):
context.results.s = response.x + '_foo' context.results.s = response.x + '_foo'
context.results.outBox.cap = capability().TestInterface._new_server(Server(100)) context.results.outBox.cap = capability().TestInterface._new_server(Server(100))
def _error(error): def _error(error):
raise Exception('test was a success') raise Exception('test was a success')
@@ -176,7 +178,7 @@ def test_pipeline_exception_context(capability):
pipelinePromise = outCap.foo(i=10) pipelinePromise = outCap.foo(i=10)
with pytest.raises(Exception): with pytest.raises(Exception):
loop.wait(pipelinePromise) pipelinePromise.wait()
with pytest.raises(Exception): with pytest.raises(Exception):
remote.wait() remote.wait()
@@ -184,7 +186,7 @@ def test_pipeline_exception_context(capability):
def test_casting_context(capability): def test_casting_context(capability):
client = capability.TestExtends._new_client(Server()) client = capability.TestExtends._new_client(Server())
client2 = client.upcast(capability.TestInterface) client2 = client.upcast(capability.TestInterface)
client3 = client2.cast_as(capability.TestInterface) _ = client2.cast_as(capability.TestInterface)
with pytest.raises(Exception): with pytest.raises(Exception):
client.upcast(capability.TestPipeline) client.upcast(capability.TestPipeline)

View File

@@ -1,6 +1,7 @@
import pytest
import capnp
import os import os
import pytest
import capnp
this_dir = os.path.dirname(__file__) this_dir = os.path.dirname(__file__)
@@ -154,6 +155,7 @@ class BadPipelineServer:
_results = _context.results _results = _context.results
_results.s = response.x + '_foo' _results.s = response.x + '_foo'
_results.outBox.cap = capability().TestInterface._new_server(Server(100)) _results.outBox.cap = capability().TestInterface._new_server(Server(100))
def _error(error): def _error(error):
raise Exception('test was a success') raise Exception('test was a success')
@@ -180,7 +182,7 @@ def test_pipeline_exception(capability):
pipelinePromise = outCap.foo(i=10) pipelinePromise = outCap.foo(i=10)
with pytest.raises(Exception): with pytest.raises(Exception):
loop.wait(pipelinePromise) pipelinePromise.wait()
with pytest.raises(Exception): with pytest.raises(Exception):
remote.wait() remote.wait()
@@ -188,7 +190,7 @@ def test_pipeline_exception(capability):
def test_casting(capability): def test_casting(capability):
client = capability.TestExtends._new_client(Server()) client = capability.TestExtends._new_client(Server())
client2 = client.upcast(capability.TestInterface) client2 = client.upcast(capability.TestInterface)
client3 = client2.cast_as(capability.TestInterface) _ = client2.cast_as(capability.TestInterface)
with pytest.raises(Exception): with pytest.raises(Exception):
client.upcast(capability.TestPipeline) client.upcast(capability.TestPipeline)

View File

@@ -1,9 +1,10 @@
import pytest
import platform
import capnp
import os import os
import platform
import tempfile import tempfile
import sys
import pytest
import capnp
this_dir = os.path.dirname(__file__) this_dir = os.path.dirname(__file__)
@@ -81,19 +82,3 @@ def test_large_read_mutltiple_bytes_memoryview(test_capnp):
data = get_two_adjacent_messages(test_capnp) + b' ' data = get_two_adjacent_messages(test_capnp) + b' '
for m in test_capnp.Msg.read_multiple_bytes(memoryview(data)): for m in test_capnp.Msg.read_multiple_bytes(memoryview(data)):
pass pass
@pytest.mark.skipif(sys.version_info[0] == 3, reason="Legacy buffer support only for python 2.7")
def test_large_read_mutltiple_bytes_buffer(test_capnp):
data = get_two_adjacent_messages(test_capnp)
for m in test_capnp.Msg.read_multiple_bytes(buffer(data)):
pass
with pytest.raises(capnp.KjException):
data = get_two_adjacent_messages(test_capnp)[:-1]
for m in test_capnp.Msg.read_multiple_bytes(buffer(data)):
pass
with pytest.raises(capnp.KjException):
data = get_two_adjacent_messages(test_capnp) + b' '
for m in test_capnp.Msg.read_multiple_bytes(buffer(data)):
pass

View File

@@ -56,13 +56,13 @@ def test_failed_import():
bar.foo = foo bar.foo = foo
def test_defualt_import_hook(): def test_defualt_import_hook():
import addressbook_capnp import addressbook_capnp # noqa: F401
def test_dash_import(): def test_dash_import():
import addressbook_with_dashes_capnp import addressbook_with_dashes_capnp # noqa: F401
def test_spaces_import(): def test_spaces_import():
import addressbook_with_spaces_capnp import addressbook_with_spaces_capnp # noqa: F401
def test_add_import_hook(): def test_add_import_hook():
capnp.add_import_hook([this_dir]) capnp.add_import_hook([this_dir])
@@ -86,4 +86,4 @@ def test_remove_import_hook():
del sys.modules['addressbook_capnp'] # hack to deal with it being imported already del sys.modules['addressbook_capnp'] # hack to deal with it being imported already
with pytest.raises(ImportError): with pytest.raises(ImportError):
import addressbook_capnp import addressbook_capnp # noqa: F401

View File

@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import pytest import pytest
import capnp import capnp
@@ -71,7 +71,7 @@ def test_addressbook_message_classes(addressbook):
assert bobPhones[0].type == 'home' assert bobPhones[0].type == 'home'
assert bobPhones[1].number == "555-7654" assert bobPhones[1].number == "555-7654"
assert bobPhones[1].type == 'work' assert bobPhones[1].type == 'work'
assert bob.employment.unemployed == None assert bob.employment.unemployed is None
f = open('example', 'w') f = open('example', 'w')
writeAddressBook(f.fileno()) writeAddressBook(f.fileno())
@@ -130,7 +130,7 @@ def test_addressbook(addressbook):
assert bobPhones[0].type == 'home' assert bobPhones[0].type == 'home'
assert bobPhones[1].number == "555-7654" assert bobPhones[1].number == "555-7654"
assert bobPhones[1].type == 'work' assert bobPhones[1].type == 'work'
assert bob.employment.unemployed == None assert bob.employment.unemployed is None
f = open('example', 'w') f = open('example', 'w')
@@ -192,7 +192,7 @@ def test_addressbook_resizable(addressbook):
assert bobPhones[0].type == 'home' assert bobPhones[0].type == 'home'
assert bobPhones[1].number == "555-7654" assert bobPhones[1].number == "555-7654"
assert bobPhones[1].type == 'work' assert bobPhones[1].type == 'work'
assert bob.employment.unemployed == None assert bob.employment.unemployed is None
f = open('example', 'w') f = open('example', 'w')
@@ -262,7 +262,7 @@ def test_addressbook_explicit_fields(addressbook):
assert bobPhones[1]._get_by_field(phone_fields['number']) == "555-7654" assert bobPhones[1]._get_by_field(phone_fields['number']) == "555-7654"
assert bobPhones[1]._get_by_field(phone_fields['type']) == 'work' assert bobPhones[1]._get_by_field(phone_fields['type']) == 'work'
employment = bob._get_by_field(person_fields['employment']) employment = bob._get_by_field(person_fields['employment'])
employment._get_by_field(addressbook.Person.Employment.schema.fields['unemployed']) == None employment._get_by_field(addressbook.Person.Employment.schema.fields['unemployed']) is None
f = open('example', 'w') f = open('example', 'w')
@@ -371,8 +371,8 @@ def check_list(reader, expected):
assert reader[i] == v assert reader[i] == v
def check_all_types(reader): def check_all_types(reader):
assert reader.voidField == None assert reader.voidField is None
assert reader.boolField == True assert reader.boolField
assert reader.int8Field == -123 assert reader.int8Field == -123
assert reader.int16Field == -12345 assert reader.int16Field == -12345
assert reader.int32Field == -12345678 assert reader.int32Field == -12345678
@@ -387,8 +387,8 @@ def check_all_types(reader):
assert reader.dataField == b"bar" assert reader.dataField == b"bar"
subReader = reader.structField subReader = reader.structField
assert subReader.voidField == None assert subReader.voidField is None
assert subReader.boolField == True assert subReader.boolField
assert subReader.int8Field == -12 assert subReader.int8Field == -12
assert subReader.int16Field == 3456 assert subReader.int16Field == 3456
assert subReader.int32Field == -78901234 assert subReader.int32Field == -78901234
@@ -495,7 +495,7 @@ def test_build_first_segment_size(all_types):
expectedText = open(os.path.join(this_dir, 'all-types.txt'), 'r').read() expectedText = open(os.path.join(this_dir, 'all-types.txt'), 'r').read()
assert str(root) + '\n' == expectedText assert str(root) + '\n' == expectedText
root = all_types.TestAllTypes.new_message(1024*1024) root = all_types.TestAllTypes.new_message(1024 * 1024)
init_all_types(root) init_all_types(root)
expectedText = open(os.path.join(this_dir, 'all-types.txt'), 'r').read() expectedText = open(os.path.join(this_dir, 'all-types.txt'), 'r').read()
assert str(root) + '\n' == expectedText assert str(root) + '\n' == expectedText

View File

@@ -1,8 +1,3 @@
import pytest
import capnp
import os
import time
import test_response_capnp import test_response_capnp
class FooServer(test_response_capnp.Foo.Server): class FooServer(test_response_capnp.Foo.Server):

View File

@@ -1,6 +1,5 @@
import pytest import pytest
import capnp import capnp
import os
import socket import socket
import test_capability_capnp import test_capability_capnp
@@ -30,7 +29,7 @@ def test_simple_rpc():
read, write = socket.socketpair(socket.AF_UNIX) read, write = socket.socketpair(socket.AF_UNIX)
restorer = SimpleRestorer() restorer = SimpleRestorer()
server = capnp.TwoPartyServer(write, restorer) _ = capnp.TwoPartyServer(write, restorer)
client = capnp.TwoPartyClient(read) client = capnp.TwoPartyClient(read)
ref = test_capability_capnp.TestSturdyRefObjectId.new_message(tag='testInterface') ref = test_capability_capnp.TestSturdyRefObjectId.new_message(tag='testInterface')
@@ -47,7 +46,7 @@ def test_simple_rpc_with_options():
read, write = socket.socketpair(socket.AF_UNIX) read, write = socket.socketpair(socket.AF_UNIX)
restorer = SimpleRestorer() restorer = SimpleRestorer()
server = capnp.TwoPartyServer(write, restorer) _ = capnp.TwoPartyServer(write, restorer)
# This traversal limit is too low to receive the response in, so we expect # This traversal limit is too low to receive the response in, so we expect
# an exception during the call. # an exception during the call.
client = capnp.TwoPartyClient(read, traversal_limit_in_words=1) client = capnp.TwoPartyClient(read, traversal_limit_in_words=1)
@@ -58,13 +57,13 @@ def test_simple_rpc_with_options():
remote = cap.foo(i=5) remote = cap.foo(i=5)
with pytest.raises(capnp.KjException): with pytest.raises(capnp.KjException):
response = remote.wait() _ = remote.wait()
def test_simple_rpc_restore_func(): def test_simple_rpc_restore_func():
read, write = socket.socketpair(socket.AF_UNIX) read, write = socket.socketpair(socket.AF_UNIX)
server = capnp.TwoPartyServer(write, restore_func) _ = capnp.TwoPartyServer(write, restore_func)
client = capnp.TwoPartyClient(read) client = capnp.TwoPartyClient(read)
ref = test_capability_capnp.TestSturdyRefObjectId.new_message(tag='testInterface') ref = test_capability_capnp.TestSturdyRefObjectId.new_message(tag='testInterface')
@@ -86,7 +85,7 @@ def text_restore_func(objectId):
def test_ez_rpc(): def test_ez_rpc():
read, write = socket.socketpair(socket.AF_UNIX) read, write = socket.socketpair(socket.AF_UNIX)
server = capnp.TwoPartyServer(write, text_restore_func) _ = capnp.TwoPartyServer(write, text_restore_func)
client = capnp.TwoPartyClient(read) client = capnp.TwoPartyClient(read)
cap = client.ez_restore('testInterface') cap = client.ez_restore('testInterface')
@@ -108,7 +107,7 @@ def test_ez_rpc():
def test_simple_rpc_bootstrap(): def test_simple_rpc_bootstrap():
read, write = socket.socketpair(socket.AF_UNIX) read, write = socket.socketpair(socket.AF_UNIX)
server = capnp.TwoPartyServer(write, bootstrap=Server(100)) _ = capnp.TwoPartyServer(write, bootstrap=Server(100))
client = capnp.TwoPartyClient(read) client = capnp.TwoPartyClient(read)
cap = client.bootstrap() cap = client.bootstrap()

View File

@@ -1,21 +1,23 @@
import capnp import gc
import os import os
import socket import socket
import gc
import subprocess import subprocess
import sys # add examples dir to sys.path
import time import time
import sys # add examples dir to sys.path import capnp
examples_dir = os.path.join(os.path.dirname(__file__), '..', 'examples') examples_dir = os.path.join(os.path.dirname(__file__), '..', 'examples')
sys.path.append(examples_dir) sys.path.append(examples_dir)
import calculator_client
import calculator_server import calculator_client # noqa: E402
import calculator_server # noqa: E402
def test_calculator(): def test_calculator():
read, write = socket.socketpair(socket.AF_UNIX) read, write = socket.socketpair(socket.AF_UNIX)
server = capnp.TwoPartyServer(write, bootstrap=calculator_server.CalculatorImpl()) _ = capnp.TwoPartyServer(write, bootstrap=calculator_server.CalculatorImpl())
calculator_client.main(read) calculator_client.main(read)
@@ -57,7 +59,7 @@ def test_calculator_gc():
evaluate_impl_orig = calculator_server.evaluate_impl evaluate_impl_orig = calculator_server.evaluate_impl
calculator_server.evaluate_impl = new_evaluate_impl(evaluate_impl_orig) calculator_server.evaluate_impl = new_evaluate_impl(evaluate_impl_orig)
server = capnp.TwoPartyServer(write, bootstrap=calculator_server.CalculatorImpl()) _ = capnp.TwoPartyServer(write, bootstrap=calculator_server.CalculatorImpl())
calculator_client.main(read) calculator_client.main(read)
calculator_server.evaluate_impl = evaluate_impl_orig calculator_server.evaluate_impl = evaluate_impl_orig

View File

@@ -42,7 +42,10 @@ def test_roundtrip_bytes(all_types):
msg = all_types.TestAllTypes.from_bytes(message_bytes) msg = all_types.TestAllTypes.from_bytes(message_bytes)
test_regression.check_all_types(msg) test_regression.check_all_types(msg)
@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason="TODO: Investigate why this works on CPython but fails on PyPy.") @pytest.mark.skipif(
platform.python_implementation() == 'PyPy',
reason="TODO: Investigate why this works on CPython but fails on PyPy."
)
def test_roundtrip_segments(all_types): def test_roundtrip_segments(all_types):
msg = all_types.TestAllTypes.new_message() msg = all_types.TestAllTypes.new_message()
test_regression.init_all_types(msg) test_regression.init_all_types(msg)
@@ -79,7 +82,10 @@ def test_roundtrip_bytes_fail(all_types):
with pytest.raises(TypeError): with pytest.raises(TypeError):
all_types.TestAllTypes.from_bytes(42) all_types.TestAllTypes.from_bytes(42)
@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason="This works in PyPy 4.0.1 but travisci's version of PyPy has some bug that fails this test.") @pytest.mark.skipif(
platform.python_implementation() == 'PyPy',
reason="This works in PyPy 4.0.1 but travisci's version of PyPy has some bug that fails this test."
)
def test_roundtrip_bytes_packed(all_types): def test_roundtrip_bytes_packed(all_types):
msg = all_types.TestAllTypes.new_message() msg = all_types.TestAllTypes.new_message()
test_regression.init_all_types(msg) test_regression.init_all_types(msg)
@@ -146,7 +152,10 @@ def test_roundtrip_bytes_multiple_packed(all_types):
i += 1 i += 1
assert i == 3 assert i == 3
@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason="This works on my local PyPy v2.5.0, but is for some reason broken on TravisCI. Skip for now.") @pytest.mark.skipif(
platform.python_implementation() == 'PyPy',
reason="This works on my local PyPy v2.5.0, but is for some reason broken on TravisCI. Skip for now."
)
def test_roundtrip_dict(all_types): def test_roundtrip_dict(all_types):
msg = all_types.TestAllTypes.new_message() msg = all_types.TestAllTypes.new_message()
test_regression.init_all_types(msg) test_regression.init_all_types(msg)

View File

@@ -77,7 +77,10 @@ def test_which_reader(addressbook):
addresses.which addresses.which
@pytest.mark.skipif(capnp.version.LIBCAPNP_VERSION < 5000, reason="Using ints as enums requires v0.5.0+ of the C++ capnp library") @pytest.mark.skipif(
capnp.version.LIBCAPNP_VERSION < 5000,
reason="Using ints as enums requires v0.5.0+ of the C++ capnp library"
)
def test_enum(addressbook): def test_enum(addressbook):
addresses = addressbook.AddressBook.new_message() addresses = addressbook.AddressBook.new_message()
people = addresses.init('people', 2) people = addresses.init('people', 2)
@@ -188,12 +191,7 @@ def test_set_dict_union(addressbook):
assert person.employment.employer.name == 'foo' assert person.employment.employer.name == 'foo'
try: def isstr(s):
basestring # attempt to evaluate basestring
def isstr(s):
return isinstance(s, basestring)
except NameError:
def isstr(s):
return isinstance(s, str) return isinstance(s, str)
@@ -227,7 +225,12 @@ def test_to_dict_verbose(addressbook):
def test_to_dict_ordered(addressbook): def test_to_dict_ordered(addressbook):
person = addressbook.Person.new_message(**{'name': 'Alice', 'phones': [{'type': 'mobile', 'number': '555-1212'}], 'id': 123, 'employment': {'school': 'MIT'}, 'email': 'alice@example.com'}) person = addressbook.Person.new_message(**{
'name': 'Alice',
'phones': [{'type': 'mobile', 'number': '555-1212'}],
'id': 123,
'employment': {'school': 'MIT'}, 'email': 'alice@example.com'
})
if sys.version_info >= (2, 7): if sys.version_info >= (2, 7):
assert list(person.to_dict(ordered=True).keys()) == ['id', 'name', 'email', 'phones', 'employment'] assert list(person.to_dict(ordered=True).keys()) == ['id', 'name', 'email', 'phones', 'employment']
@@ -246,4 +249,4 @@ def test_nested_list(addressbook):
struct.list[1][0] = 2 struct.list[1][0] = 2
struct.list[1][1] = 3 struct.list[1][1] = 3
assert struct.to_dict()["list"] == [[1], [2,3]] assert struct.to_dict()["list"] == [[1], [2, 3]]

View File

@@ -1,20 +1,38 @@
import capnp '''
import pytest thread test
import test_capability_capnp '''
import platform
import socket import socket
import threading import threading
import platform
@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason="pycapnp's GIL handling isn't working properly at the moment for PyPy") import pytest
import capnp
import test_capability_capnp
@pytest.mark.skipif(
platform.python_implementation() == 'PyPy',
reason="pycapnp's GIL handling isn't working properly at the moment for PyPy"
)
def test_making_event_loop(): def test_making_event_loop():
'''
Event loop test
'''
capnp.remove_event_loop(True) capnp.remove_event_loop(True)
capnp.create_event_loop() capnp.create_event_loop()
capnp.remove_event_loop() capnp.remove_event_loop()
capnp.create_event_loop() capnp.create_event_loop()
@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason="pycapnp's GIL handling isn't working properly at the moment for PyPy") @pytest.mark.skipif(
platform.python_implementation() == 'PyPy',
reason="pycapnp's GIL handling isn't working properly at the moment for PyPy"
)
def test_making_threaded_event_loop(): def test_making_threaded_event_loop():
'''
Threaded event loop test
'''
capnp.remove_event_loop(True) capnp.remove_event_loop(True)
capnp.create_event_loop(True) capnp.create_event_loop(True)
@@ -23,23 +41,40 @@ def test_making_threaded_event_loop():
class Server(test_capability_capnp.TestInterface.Server): class Server(test_capability_capnp.TestInterface.Server):
'''
Server
'''
def __init__(self, val=1): def __init__(self, val=1):
self.val = val self.val = val
def foo(self, i, j, **kwargs): def foo(self, i, j, **kwargs):
'''
foo
'''
return str(i * 5 + self.val) return str(i * 5 + self.val)
class SimpleRestorer(test_capability_capnp.TestSturdyRefObjectId.Restorer): class SimpleRestorer(test_capability_capnp.TestSturdyRefObjectId.Restorer):
'''
SimpleRestorer
'''
def restore(self, ref_id): def restore(self, ref_id):
'''
Restore
'''
assert ref_id.tag == 'testInterface' assert ref_id.tag == 'testInterface'
return Server(100) return Server(100)
@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason="pycapnp's GIL handling isn't working properly at the moment for PyPy") @pytest.mark.skipif(
platform.python_implementation() == 'PyPy',
reason="pycapnp's GIL handling isn't working properly at the moment for PyPy"
)
def test_using_threads(): def test_using_threads():
'''
Thread test
'''
capnp.remove_event_loop(True) capnp.remove_event_loop(True)
capnp.create_event_loop(True) capnp.create_event_loop(True)
@@ -47,7 +82,7 @@ def test_using_threads():
def run_server(): def run_server():
restorer = SimpleRestorer() restorer = SimpleRestorer()
server = capnp.TwoPartyServer(write, restorer) _ = capnp.TwoPartyServer(write, restorer)
capnp.wait_forever() capnp.wait_forever()
server_thread = threading.Thread(target=run_server) server_thread = threading.Thread(target=run_server)