小编mag*_*gie的帖子

py.test:将参数传递给fixture函数

我正在使用py.test来测试包含在python类MyTester中的一些DLL代码.为了验证目的,我需要在测试期间记录一些测试数据,然后再进行更多处理.由于我有很多测试_...文件,我想在大多数测试中重用测试器对象创建(MyTester实例).

由于测试对象是获得对DLL的变量和函数的引用的对象,我需要将DLL的变量列表传递给每个测试文件的测试对象(要记录的变量对于test_ .. .文件).列表的内容应用于记录指定的数据.

我的想法是以某种方式这样做:

import pytest

class MyTester():
    def __init__(self, arg = ["var0", "var1"]):
        self.arg = arg
        # self.use_arg_to_init_logging_part()

    def dothis(self):
        print "this"

    def dothat(self):
        print "that"

# located in conftest.py (because other test will reuse it)

@pytest.fixture()
def tester(request):
    """ create tester object """
    # how to use the list below for arg?
    _tester = MyTester()
    return _tester

# located in test_...py

# @pytest.mark.usefixtures("tester") 
class TestIt():

    # def __init__(self):
    #     self.args_for_tester = ["var1", "var2"]
    #     # how to …
Run Code Online (Sandbox Code Playgroud)

python fixtures pytest

92
推荐指数
9
解决办法
6万
查看次数

ctypes 为 c_ulong 重新实现 rshift

我正在通过 ctypes 访问 C 库,但遇到以下问题:

我正在使用 ctypeslib 生成“包装器”(使用 ctypes 访问库的​​ ctypes 命令)。C 库包含在此步骤中转换为 python 函数的宏。(为了尽可能独立于库内部,我想在 python 中使用其中一些宏。)

这些宏之一如下所示:

# using the ctypes types
myuint16_t = c_ushort
myuint32_t = c_ulong

def mymacro(x): return (myuint16_t)((myuint32_t)(x) >> 16) # macro
Run Code Online (Sandbox Code Playgroud)

我想通过以下方式(在函数内部)在单独的模块中使用生成的函数:

return wrapper.mymacro(valueToBeConverted) # valueToBeConverted is an int
Run Code Online (Sandbox Code Playgroud)

但是使用这一行我得到了以下错误:

....   
def mymacro(x): return (myuint16_t)((myuint32_t)(x) >> 16) # macro
TypeError: unsupported operand type(s) for >>: 'c_ulong' and 'int'
Run Code Online (Sandbox Code Playgroud)

(我知道移动 c_ulong 的常用方法是,c_ulongvar.value >> x但每次 C 库中的某些内容发生变化时,我都必须修补生成的包装器。所以我尽量避免这种情况)。

这里好像__rshift__不能用c_ulong的实现。

print c_ulong.__rshift__
# throws …
Run Code Online (Sandbox Code Playgroud)

python ctypes operator-overloading bit-shift

5
推荐指数
1
解决办法
1766
查看次数

打印cython变量的地址

我只是想打印cython变量的地址,但我无法解决错误信息:

cdef int myvar
print &myvar
Run Code Online (Sandbox Code Playgroud)

Cannot convert 'int *' to Python object
Run Code Online (Sandbox Code Playgroud)

打印地址需要做什么?

cython

3
推荐指数
1
解决办法
1435
查看次数