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':
@@ -172,6 +171,6 @@ def copy_and_patch_libcapnp(capnp, libcapnp):
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'):
@@ -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,15 +29,15 @@ 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':
@@ -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))
@@ -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

@@ -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,7 +14,9 @@
# 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
@@ -48,11 +53,10 @@ 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
@@ -173,7 +177,6 @@ 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',
@@ -182,6 +185,7 @@ latex_elements = {
# 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

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,

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,7 +169,12 @@ 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,
@@ -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

@@ -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

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,11 +191,6 @@ def test_set_dict_union(addressbook):
assert person.employment.employer.name == 'foo' assert person.employment.employer.name == 'foo'
try:
basestring # attempt to evaluate basestring
def isstr(s):
return isinstance(s, basestring)
except NameError:
def isstr(s): 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']

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)