Skip to content

Commit

Permalink
Merge pull request #868 from fniephaus/flake8
Browse files Browse the repository at this point in the history
More Flake8 fixes
  • Loading branch information
timfel committed Mar 18, 2017
2 parents 751eb8b + 8e2fe6b commit ce54619
Show file tree
Hide file tree
Showing 41 changed files with 405 additions and 285 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Expand Up @@ -16,9 +16,6 @@ matrix:
# This times out because untranslated is so slow :/
- python: pypy-5.3.1
env: TEST_TYPE=rubyspec_untranslated
# We're tracking this, but not failing for it right now :(
- python: pypy-5.3.1
env: TEST_TYPE=flake8
include:
- os: osx
language: generic
Expand Down
2 changes: 1 addition & 1 deletion targettopaz.py
Expand Up @@ -15,7 +15,7 @@ def jitpolicy(driver):

def handle_config(config, translateconfig):
from rpython.translator.platform import host_factory
max_stack_size = 11 << 18 # 2.8 Megs
max_stack_size = 11 << 18 # 2.8 Megs
if host_factory.name == 'msvc':
host_factory.cflags += ('/DMAX_STACK_SIZE=%d' % max_stack_size,)
elif host_factory.name in ('linux', 'darwin'):
Expand Down
4 changes: 2 additions & 2 deletions tasks/base.py
Expand Up @@ -2,8 +2,8 @@

import invoke


invoke.run = os.system
if os.environ.get('TRAVIS_OS_NAME') == 'osx':
invoke.run = os.system


class BaseTest(object):
Expand Down
2 changes: 2 additions & 0 deletions tasks/travis.py
Expand Up @@ -116,9 +116,11 @@ def run_translate_jit_tests(env):
run_specs("`pwd`/bin/topaz")
invoke.run("PYTHONPATH={rpython_path}:$PYTHONPATH py.test --topaz=bin/topaz tests/jit/".format(**env))


def run_translate_jit(env):
invoke.run("PYTHONPATH={rpython_path}:$PYTHONPATH python {rpython_path}/rpython/bin/rpython --batch -Ojit targettopaz.py".format(**env))


def run_specs(binary, prefix=""):
invoke.run("{prefix}../mspec/bin/mspec run -G fails -t {binary} --config=topaz.mspec".format(
prefix=prefix,
Expand Down
6 changes: 3 additions & 3 deletions tests/jit/conftest.py
@@ -1,4 +1,3 @@
import os
import py
import sys

Expand All @@ -12,7 +11,8 @@ def __init__(self, trace, expected_ops, exception):
self.exception = exception

def find_test_file(self):
import traceback, re
import re
import traceback
stk = traceback.extract_stack()
stk.reverse()
for filename, lineno, funcname, text in stk:
Expand All @@ -21,7 +21,7 @@ def find_test_file(self):
return None, None

def get_updated_contents(self, filename, lineno):
lno = lineno - 10 # heuristic ;)
lno = lineno - 10 # heuristic ;)
with open(filename) as f:
contents = f.readlines()
newline = "\r\n" if contents[0].endswith("\r\n") else "\n"
Expand Down
1 change: 1 addition & 0 deletions tests/modules/ffi/base.py
@@ -1,5 +1,6 @@
from tests.base import BaseTopazTest


class BaseFFITest(BaseTopazTest):
def ask(self, space, question):
w_answer = space.execute(question)
Expand Down
6 changes: 3 additions & 3 deletions tests/modules/ffi/conftest.py
Expand Up @@ -2,22 +2,22 @@
import subprocess
import sys

from rpython.rtyper.lltypesystem.ll2ctypes import ALLOCATED

def pytest_funcarg__ffis(request, space):
system, _, _, _, cpu = os.uname() # not for windows
system, _, _, _, cpu = os.uname() # not for windows
space.execute("""
RUBY_ENGINE = 'topaz'
RUBY_PLATFORM = '%s-%s'
load 'ffi.rb'
""" % (cpu, system.lower()))
return space


def pytest_funcarg__libtest_so():
ext = 'dylib' if sys.platform == 'darwin' else 'so'
self_dir = os.path.join(os.path.dirname(__file__))
makefile = os.path.join('libtest', 'GNUmakefile')
subprocess.call(["make" , "-f", makefile], cwd=self_dir)
subprocess.call(["make", "-f", makefile], cwd=self_dir)
rel_to_libtest_so = os.path.join('build', 'libtest.' + ext)
libtest_so = os.path.abspath(os.path.join(self_dir, rel_to_libtest_so))
return libtest_so
84 changes: 50 additions & 34 deletions tests/modules/ffi/test_abstract_memory.py
Expand Up @@ -6,7 +6,6 @@
from pypy.module._cffi_backend import misc

import sys
import pytest

test_types = [ffitype.INT8, ffitype.INT16, ffitype.INT32, ffitype.INT64,
ffitype.UINT8, ffitype.UINT16, ffitype.UINT32, ffitype.UINT64,
Expand All @@ -23,38 +22,44 @@
minval = {}
maxval = {}
for bits in [8, 16, 32, 64]:
minval['int' + str(bits)] = int(-2**(bits-1))
minval['int' + str(bits)] = int(-2**(bits - 1))
minval['uint' + str(bits)] = 0
maxval['int' + str(bits)] = int(2**(bits-1) - 1)
maxval['int' + str(bits)] = int(2**(bits - 1) - 1)
# for some reason int(2**64 -1) is not accepted as biggest uint64
maxval['uint' + str(bits)] = int(2**(bits-1) - 1)
minval['long'] = int(-2**(8*rffi.sizeof(rffi.LONG)))
maxval['uint' + str(bits)] = int(2**(bits - 1) - 1)
minval['long'] = int(-2**(8 * rffi.sizeof(rffi.LONG)))
minval['ulong'] = 0
maxval['long'] = int(2**(8*rffi.sizeof(rffi.LONG)) - 1)
maxval['ulong'] = int(2**(8*rffi.sizeof(rffi.LONG)-1) - 1)
maxval['long'] = int(2**(8 * rffi.sizeof(rffi.LONG)) - 1)
maxval['ulong'] = int(2**(8 * rffi.sizeof(rffi.LONG) - 1) - 1)
minval['float32'] = 1.1754943508222875e-38
maxval['float32'] = 3.4028234663852886e+38
minval['float64'] = sys.float_info.min
maxval['float64'] = sys.float_info.max


def new_cast_method(typ):
ctype = ffitype.lltypes[typ]

def cast_method(memory):
return rffi.cast(ffitype.lltype.Ptr(rffi.CArray(ctype)), memory.ptr)
return cast_method


def new_numberof_method(typ):
csize = ffitype.lltype_sizes[typ]
if csize & (csize - 1) == 0: # csize is a power of 2
shift = 0
while 1 << shift < csize: shift += 1
while 1 << shift < csize:
shift += 1

def numberof_method(self):
return self.sizeof_memory >> shift
else:
def numberof_method(self):
return self.sizeof_memory / csize
return numberof_method


def put_method_test_code(typename):
# For some reason, str(some_float) cuts off a few digits.
# Only repr prints the entire float.
Expand All @@ -66,9 +71,10 @@ def put_method_test_code(typename):
mem_ptr.put_TYPE(FFI::Type::UPPER.size, MAX)
mem_ptr
""".replace('TYPE', typename).
replace('UPPER', typename.upper()).
replace('MIN', strfunc(minval[typename])).
replace('MAX', strfunc(maxval[typename])))
replace('UPPER', typename.upper()).
replace('MIN', strfunc(minval[typename])).
replace('MAX', strfunc(maxval[typename])))


def get_method_test_code(typename):
strfunc = ((lambda x: repr(x)) if 'float' in typename else
Expand All @@ -81,9 +87,10 @@ def get_method_test_code(typename):
mem_ptr.put_TYPE(pos_4, MAX)
[mem_ptr.get_TYPE(pos_3), mem_ptr.get_TYPE(pos_4)]
""".replace('TYPE', typename).
replace('UPPER', typename.upper()).
replace('MIN', strfunc(minval[typename])).
replace('MAX', strfunc(maxval[typename])))
replace('UPPER', typename.upper()).
replace('MIN', strfunc(minval[typename])).
replace('MAX', strfunc(maxval[typename])))


class TestAbstractMemory_put_methods(BaseFFITest):
def test_they_put_a_single_value_into_the_given_offset(self, space):
Expand All @@ -106,7 +113,7 @@ def test_they_refuse_negative_offsets(self, space):
sizeof_t = ffitype.lltype_sizes[t]
with self.raises(space, 'IndexError',
"Memory access offset=-1 size=%s is out of bounds"
% sizeof_t):
% sizeof_t):
space.execute("""
FFI::MemoryPointer.new(:T, 1).put_T(-1, 230)
""".replace('T', tn.lower()))
Expand All @@ -115,13 +122,15 @@ def test_they_refuse_too_large_offsets(self, space):
for t in test_types:
tn = ffitype.type_names[t]
sizeof_t = ffitype.lltype_sizes[t]
with self.raises(space, 'IndexError',
"Memory access offset=1000 size=%s is out of bounds"
% sizeof_t):
with self.raises(
space, 'IndexError',
("Memory access offset=1000 size=%s is out of bounds" %
sizeof_t)):
space.execute("""
FFI::MemoryPointer.new(:T, 3).put_T(1000, 15)
""".replace('T', tn.lower()))


class TestAbstractMemory_write_methods(BaseFFITest):
def test_they_are_like_calling_put_with_0_as_1st_arg(self, space):
for t in test_types:
Expand All @@ -135,24 +144,26 @@ def test_they_are_like_calling_put_with_0_as_1st_arg(self, space):
casted_ptr = cast_method(w_mem_ptr)
assert casted_ptr[0] == 'y' if tn == 'INT8' else 121


class TestAbstractMemory_get_methods(BaseFFITest):
def test_they_get_a_single_value_from_the_given_offset(self, space):
for tn in [ffitype.type_names[t].lower()
for t in test_types]:
w_res = space.execute(get_method_test_code(tn))
assert self.unwrap(space, w_res) == [minval[tn], maxval[tn]]


class TestAbstractMemory_read_methods(BaseFFITest):
def test_they_are_like_calling_get_with_0(self, space):
for tn in [ffitype.type_names[t].lower()
for t in test_types]:
for tn in [ffitype.type_names[t].lower() for t in test_types]:
w_res = space.execute("""
mem_ptr = FFI::MemoryPointer.new(:T, 1)
mem_ptr.write_T(1)
mem_ptr.read_T
""".replace('T', tn))
assert self.unwrap(space, w_res) == 1


class TestAbstractMemory_put_pointer(BaseFFITest):
def test_it_puts_a_single_pointer_into_the_given_offset(self, space):
w_mem_ptr = space.execute("""
Expand All @@ -167,6 +178,7 @@ def test_it_puts_a_single_pointer_into_the_given_offset(self, space):
assert w_adr_ptr[0] == 88
assert w_adr_ptr[1] == 55


class TestAbstractMemory_get_pointer(BaseFFITest):
def test_it_gets_a_single_pointer_from_the_given_offset(self, space):
assert self.ask(space, """
Expand Down Expand Up @@ -196,6 +208,7 @@ def test_it_doesnt_loose_the_content(self, space):
""")
assert self.unwrap(space, w_res) == 300


class TestAbstractMemory_write_pointer(BaseFFITest):
def test_it_is_like_calling_put_pointer_with_0_as_1st_arg(self, space):
w_res = space.execute("""
Expand All @@ -205,6 +218,7 @@ def test_it_is_like_calling_put_pointer_with_0_as_1st_arg(self, space):
""")
assert self.unwrap(space, w_res) == 11


class TestAbstractMemory_write_uint64(BaseFFITest):
def test_it_is_like_calling_put_uint64_with_0_as_1st_arg(self, space):
w_res = space.execute("""
Expand All @@ -214,6 +228,7 @@ def test_it_is_like_calling_put_uint64_with_0_as_1st_arg(self, space):
""")
assert self.unwrap(space, w_res) == 14


class TestAbstractMemory_read_pointer(BaseFFITest):
def test_it_is_like_calling_get_pointer_with_0(self, space):
w_res = space.execute("""
Expand All @@ -223,6 +238,7 @@ def test_it_is_like_calling_get_pointer_with_0(self, space):
""")
assert self.unwrap(space, w_res) == 13


class TestAbstractMemory_read_uint64(BaseFFITest):
def test_it_is_like_calling_get_uint64_with_0(self, space):
w_res = space.execute("""
Expand All @@ -232,20 +248,20 @@ def test_it_is_like_calling_get_uint64_with_0(self, space):
""")
assert self.unwrap(space, w_res) == 12


class TestAbstractMemory(BaseFFITest):
def test_it_defines_the_following_aliases(self, space):
for aliases in [
('int8', 'char'),
('uint8', 'uchar'),
('int16', 'short'),
('uint16', 'ushort'),
('int32', 'int'),
('uint32', 'uint'),
('int64', 'long_long'),
('uint64', 'ulong_long'),
('float32', 'float'),
('float64', 'double')
]:
('int8', 'char'),
('uint8', 'uchar'),
('int16', 'short'),
('uint16', 'ushort'),
('int32', 'int'),
('uint32', 'uint'),
('int64', 'long_long'),
('uint64', 'ulong_long'),
('float32', 'float'),
('float64', 'double')]:
assert self.ask(space, """
FFI::AbstractMemory.instance_method(:put_%s) ==
FFI::AbstractMemory.instance_method(:put_%s)
Expand All @@ -269,11 +285,11 @@ def test_it_defines_long_methods_in_the_following_way(self, space):
FFI::AbstractMemory.instance_method(:PREFIXintSIZEOF_LONG_IN_BITS)
""".replace('SIZEOF_LONG_IN_BITS', str(8 * rffi.sizeof(rffi.LONG)))
signed_meths = ['put_', 'get_', 'write_', 'read_']
unsigned_meths = [meth+'u' for meth in signed_meths]
unsigned_meths = [meth + 'u' for meth in signed_meths]
for prefix in signed_meths + unsigned_meths:
assert self.ask(space, question.replace('PREFIX', prefix))

#class TestAbstractMemory__put_array_of_int32(BaseFFITest):
# class TestAbstractMemory__put_array_of_int32(BaseFFITest):
# def test_it_writes_into_array(self, space):
# w_mem_ptr = space.execute("""
# mem_ptr = FFI::MemoryPointer.new(:int32, 10)
Expand Down Expand Up @@ -316,7 +332,7 @@ def test_it_defines_long_methods_in_the_following_way(self, space):
# mem_ptr.put_array_of_int32(0, [1, 2, 3])
# """)
#
#class TestAbstractMemory__get_array_of_int32(BaseFFITest):
# class TestAbstractMemory__get_array_of_int32(BaseFFITest):
# def test_it_reads_from_array(self, space):
# w_res = space.execute("""
# mem_ptr = FFI::MemoryPointer.new(:int32, 10)
Expand Down
1 change: 0 additions & 1 deletion tests/modules/ffi/test_callback.py
@@ -1,4 +1,3 @@
import os
import py

from topaz.modules.ffi import _callback
Expand Down

0 comments on commit ce54619

Please sign in to comment.