2013-06-21 60 views
3

當我試圖加載醃列表,它說:取儲存失敗__new __()恰恰1參數(2給出)

>>> import pickle 
>>> with open('tests/unit/support/modules_state.samples2.6') as f: 
...  print(pickle.load(f)) 
...  
Traceback (most recent call last): 
    File "<console>", line 2, in <module> 
    File "/usr/lib/python2.6/pickle.py", line 1370, in load 
    return Unpickler(file).load() 
    File "/usr/lib/python2.6/pickle.py", line 858, in load 
    dispatch[key](self) 
    File "/usr/lib/python2.6/pickle.py", line 1083, in load_newobj 
    obj = cls.__new__(cls, *args) 
TypeError: __new__() takes exactly 1 argument (2 given) 

下面是加載/轉儲醃製列表的代碼:

class FakeModuleNameGenerator(str): 
    def __new__(cls): 
     return super(FakeModuleNameGenerator, cls).__new__(cls, binascii.b2a_hex(os.urandom(15))) 


class FakeModule(object): 
    def __new__(cls, *args, **kwargs): 
     return choice([object(), TestDouble()]) 


class SamplesIterator(object): 
    MAX_SAMPLE_LENGTH = os.getenv('MAX_SAMPLE_LENGTH', 12) if is_executing_under_continuous_integration_server() else 6 

    def __iter__(self): 
     for sample_length in range(1, SamplesIterator.MAX_SAMPLE_LENGTH): 
      combinations = [(FakeModuleNameGenerator(), FakeModule()) for i in range(0, sample_length)] 

      for r in range(1, sample_length + 1): 
       logger.info("Generating sample in length %d with r=%d" % (sample_length, r)) 
       yield itertools.combinations_with_replacement(combinations, r) 


def load_samples(): 
    if is_executing_under_continuous_integration_server() and os.getenv('USE_CACHES_SAMPLES', 'false') != 'true': 
     list(itertools.chain.from_iterable(SamplesIterator())) 

    import platform 

    version = '%s.%s' % (
     sys.version_info[0], sys.version_info[1]) if platform.python_implementation() != 'PyPy' else 'pypy' 
    samples_file = '%s%s' % (get_support_path(), 'modules_state.samples-%s' % version) 

    if os.path.exists(samples_file) and os.path.getsize(samples_file) == 0 or not os.path.exists(samples_file): 
     with open(samples_file, 'wb') as f: 
      samples = list(itertools.chain.from_iterable(SamplesIterator())) 

      try: 
       return samples 
      finally: 
       pickle.dump(samples, f, pickle.HIGHEST_PROTOCOL) 
    else: 
     with open(samples_file, 'rb') as f: 
      return pickle.load(f) 

正如你所看到的我正在讀寫二進制模式。

下面是相同的代碼完全TOX輸出加載列表之前(當第一次執行代碼後,該列表緩存):

/usr/bin/python2.7 /usr/local/bin/tox 
GLOB sdist-make: /home/omer/Documents/Projects/Python/nose2-testsuite/setup.py 
py26 inst-nodeps: /home/omer/Documents/Projects/Python/nose2-testsuite/.tox/dist/nose2-testsuite-0.1.0.zip 
py26 runtests: commands[0] 
................................................................................................................................. 
---------------------------------------------------------------------- 
Ran 129 tests in 0.034s 

OK 
py27 inst-nodeps: /home/omer/Documents/Projects/Python/nose2-testsuite/.tox/dist/nose2-testsuite-0.1.0.zip 
py27 runtests: commands[0] 
................................................................................................................................. 
---------------------------------------------------------------------- 
Ran 129 tests in 0.029s 

OK 
py33 inst-nodeps: /home/omer/Documents/Projects/Python/nose2-testsuite/.tox/dist/nose2-testsuite-0.1.0.zip 
py33 runtests: commands[0] 
................................................................................................................................. 
---------------------------------------------------------------------- 
Ran 129 tests in 0.034s 

OK 
pypy inst-nodeps: /home/omer/Documents/Projects/Python/nose2-testsuite/.tox/dist/nose2-testsuite-0.1.0.zip 
pypy runtests: commands[0] 
................................................................................................................................. 
---------------------------------------------------------------------- 
Ran 129 tests in 0.058s 

OK 
___________________________________ summary ____________________________________ 
    py26: commands succeeded 
    py27: commands succeeded 
    py33: commands succeeded 
    pypy: commands succeeded 
    congratulations :) 

Process finished with exit code 0 

我們都很高興,因爲一切工作正常嗎? 現在再次運行tox時,結果如下:

/usr/bin/python2.7 /usr/local/bin/tox 
GLOB sdist-make: /home/omer/Documents/Projects/Python/nose2-testsuite/setup.py 
py26 inst-nodeps: /home/omer/Documents/Projects/Python/nose2-testsuite/.tox/dist/nose2-testsuite-0.1.0.zip 
py26 runtests: commands[0] 
EE........... 
====================================================================== 
ERROR: tests.functional.test_isolators (nose2.loader.ModuleImportFailure) 
---------------------------------------------------------------------- 
ImportError: Failed to import test module: tests.functional.test_isolators 
Traceback (most recent call last): 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/py26/lib/python2.6/site-packages/nose2/plugins/loader/discovery.py", line 188, in _find_tests_in_file 
    module = util.module_from_name(name) 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/py26/lib/python2.6/site-packages/nose2/util.py", line 78, in module_from_name 
    __import__(name) 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/tests/functional/test_isolators.py", line 14, in <module> 
    for current_modules_state in load_samples(): 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/tests/common/support/isolators.py", line 63, in load_samples 
    return pickle.load(f) 
    File "/usr/lib/python2.6/pickle.py", line 1370, in load 
    return Unpickler(file).load() 
    File "/usr/lib/python2.6/pickle.py", line 858, in load 
    dispatch[key](self) 
    File "/usr/lib/python2.6/pickle.py", line 1083, in load_newobj 
    obj = cls.__new__(cls, *args) 
TypeError: __new__() takes exactly 1 argument (2 given) 


====================================================================== 
ERROR: tests.unit.test_isolators (nose2.loader.ModuleImportFailure) 
---------------------------------------------------------------------- 
ImportError: Failed to import test module: tests.unit.test_isolators 
Traceback (most recent call last): 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/py26/lib/python2.6/site-packages/nose2/plugins/loader/discovery.py", line 188, in _find_tests_in_file 
    module = util.module_from_name(name) 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/py26/lib/python2.6/site-packages/nose2/util.py", line 78, in module_from_name 
    __import__(name) 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/tests/unit/test_isolators.py", line 48, in <module> 
    for current_modules_state in load_samples(): 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/tests/common/support/isolators.py", line 63, in load_samples 
    return pickle.load(f) 
    File "/usr/lib/python2.6/pickle.py", line 1370, in load 
    return Unpickler(file).load() 
    File "/usr/lib/python2.6/pickle.py", line 858, in load 
    dispatch[key](self) 
    File "/usr/lib/python2.6/pickle.py", line 1083, in load_newobj 
    obj = cls.__new__(cls, *args) 
TypeError: __new__() takes exactly 1 argument (2 given) 


---------------------------------------------------------------------- 
Ran 13 tests in 0.002s 

FAILED (errors=2) 
ERROR: InvocationError: '/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/py26/bin/nose2' 
py27 inst-nodeps: /home/omer/Documents/Projects/Python/nose2-testsuite/.tox/dist/nose2-testsuite-0.1.0.zip 
py27 runtests: commands[0] 
EE........... 
====================================================================== 
ERROR: tests.functional.test_isolators (nose2.loader.ModuleImportFailure) 
---------------------------------------------------------------------- 
ImportError: Failed to import test module: tests.functional.test_isolators 
Traceback (most recent call last): 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/py27/local/lib/python2.7/site-packages/nose2/plugins/loader/discovery.py", line 188, in _find_tests_in_file 
    module = util.module_from_name(name) 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/py27/local/lib/python2.7/site-packages/nose2/util.py", line 78, in module_from_name 
    __import__(name) 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/tests/functional/test_isolators.py", line 14, in <module> 
    for current_modules_state in load_samples(): 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/tests/common/support/isolators.py", line 63, in load_samples 
    return pickle.load(f) 
    File "/usr/lib/python2.7/pickle.py", line 1378, in load 
    return Unpickler(file).load() 
    File "/usr/lib/python2.7/pickle.py", line 858, in load 
    dispatch[key](self) 
    File "/usr/lib/python2.7/pickle.py", line 1083, in load_newobj 
    obj = cls.__new__(cls, *args) 
TypeError: __new__() takes exactly 1 argument (2 given) 


====================================================================== 
ERROR: tests.unit.test_isolators (nose2.loader.ModuleImportFailure) 
---------------------------------------------------------------------- 
ImportError: Failed to import test module: tests.unit.test_isolators 
Traceback (most recent call last): 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/py27/local/lib/python2.7/site-packages/nose2/plugins/loader/discovery.py", line 188, in _find_tests_in_file 
    module = util.module_from_name(name) 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/py27/local/lib/python2.7/site-packages/nose2/util.py", line 78, in module_from_name 
    __import__(name) 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/tests/unit/test_isolators.py", line 48, in <module> 
    for current_modules_state in load_samples(): 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/tests/common/support/isolators.py", line 63, in load_samples 
    return pickle.load(f) 
    File "/usr/lib/python2.7/pickle.py", line 1378, in load 
    return Unpickler(file).load() 
    File "/usr/lib/python2.7/pickle.py", line 858, in load 
    dispatch[key](self) 
    File "/usr/lib/python2.7/pickle.py", line 1083, in load_newobj 
    obj = cls.__new__(cls, *args) 
TypeError: __new__() takes exactly 1 argument (2 given) 


---------------------------------------------------------------------- 
Ran 13 tests in 0.002s 

FAILED (errors=2) 
ERROR: InvocationError: '/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/py27/bin/nose2' 
py33 inst-nodeps: /home/omer/Documents/Projects/Python/nose2-testsuite/.tox/dist/nose2-testsuite-0.1.0.zip 
py33 runtests: commands[0] 
EE........... 
====================================================================== 
ERROR: tests.functional.test_isolators (nose2.loader.ModuleImportFailure) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/usr/lib/python3.3/unittest/case.py", line 385, in _executeTestPart 
    function() 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/py33/lib/python3.3/site-packages/nose2/loader.py", line 113, in testFailure 
    raise exception 
ImportError: Failed to import test module: tests.functional.test_isolators 
Traceback (most recent call last): 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/py33/lib/python3.3/site-packages/nose2/plugins/loader/discovery.py", line 188, in _find_tests_in_file 
    module = util.module_from_name(name) 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/py33/lib/python3.3/site-packages/nose2/util.py", line 78, in module_from_name 
    __import__(name) 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/tests/functional/test_isolators.py", line 14, in <module> 
    for current_modules_state in load_samples(): 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/tests/common/support/isolators.py", line 63, in load_samples 
    return pickle.load(f) 
TypeError: __new__() takes 1 positional argument but 2 were given 


====================================================================== 
ERROR: tests.unit.test_isolators (nose2.loader.ModuleImportFailure) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/usr/lib/python3.3/unittest/case.py", line 385, in _executeTestPart 
    function() 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/py33/lib/python3.3/site-packages/nose2/loader.py", line 113, in testFailure 
    raise exception 
ImportError: Failed to import test module: tests.unit.test_isolators 
Traceback (most recent call last): 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/py33/lib/python3.3/site-packages/nose2/plugins/loader/discovery.py", line 188, in _find_tests_in_file 
    module = util.module_from_name(name) 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/py33/lib/python3.3/site-packages/nose2/util.py", line 78, in module_from_name 
    __import__(name) 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/tests/unit/test_isolators.py", line 48, in <module> 
    for current_modules_state in load_samples(): 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/tests/common/support/isolators.py", line 63, in load_samples 
    return pickle.load(f) 
TypeError: __new__() takes 1 positional argument but 2 were given 


---------------------------------------------------------------------- 
Ran 13 tests in 0.002s 

FAILED (errors=2) 
ERROR: InvocationError: '/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/py33/bin/nose2' 
pypy inst-nodeps: /home/omer/Documents/Projects/Python/nose2-testsuite/.tox/dist/nose2-testsuite-0.1.0.zip 
pypy runtests: commands[0] 
EE........... 
====================================================================== 
ERROR: tests.functional.test_isolators (nose2.loader.ModuleImportFailure) 
---------------------------------------------------------------------- 
ImportError: Failed to import test module: tests.functional.test_isolators 
Traceback (most recent call last): 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/pypy/site-packages/nose2/plugins/loader/discovery.py", line 188, in _find_tests_in_file 
    module = util.module_from_name(name) 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/pypy/site-packages/nose2/util.py", line 78, in module_from_name 
    __import__(name) 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/tests/functional/test_isolators.py", line 14, in <module> 
ERROR: InvocationError: '/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/pypy/bin/nose2' 
    for current_modules_state in load_samples(): 
___________________________________ summary ____________________________________ 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/tests/common/support/isolators.py", line 63, in load_samples 
ERROR: py26: commands failed 
    return pickle.load(f) 
ERROR: py27: commands failed 
    File "/usr/lib/pypy/lib-python/2.7/pickle.py", line 1421, in load 
ERROR: py33: commands failed 
    return Unpickler(file).load() 
ERROR: pypy: commands failed 
    File "/usr/lib/pypy/lib-python/2.7/pickle.py", line 901, in load 
    dispatch[key](self) 
    File "/usr/lib/pypy/lib-python/2.7/pickle.py", line 1126, in load_newobj 
    obj = cls.__new__(cls, *args) 
TypeError: __new__() takes exactly 1 argument (2 given) 


====================================================================== 
ERROR: tests.unit.test_isolators (nose2.loader.ModuleImportFailure) 
---------------------------------------------------------------------- 
ImportError: Failed to import test module: tests.unit.test_isolators 
Traceback (most recent call last): 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/pypy/site-packages/nose2/plugins/loader/discovery.py", line 188, in _find_tests_in_file 
    module = util.module_from_name(name) 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/.tox/pypy/site-packages/nose2/util.py", line 78, in module_from_name 
    __import__(name) 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/tests/unit/test_isolators.py", line 48, in <module> 
    for current_modules_state in load_samples(): 
    File "/home/omer/Documents/Projects/Python/nose2-testsuite/tests/common/support/isolators.py", line 63, in load_samples 
    return pickle.load(f) 
    File "/usr/lib/pypy/lib-python/2.7/pickle.py", line 1421, in load 
    return Unpickler(file).load() 
    File "/usr/lib/pypy/lib-python/2.7/pickle.py", line 901, in load 
    dispatch[key](self) 
    File "/usr/lib/pypy/lib-python/2.7/pickle.py", line 1126, in load_newobj 
    obj = cls.__new__(cls, *args) 
TypeError: __new__() takes exactly 1 argument (2 given) 


---------------------------------------------------------------------- 
Ran 13 tests in 0.004s 

FAILED (errors=2) 

Process finished with exit code 1 
+0

我不認爲這是錯誤,但我只是在文檔中閱讀:_確保始終以二進制模式打開使用協議> = 1創建的pickle文件._ –

+0

我們可以假設'modules_state.samples2.6 '只是一些隨機損壞的文件。你將不得不向我們展示一個代碼示例,它正確地醃製了一個對象,但在取消它時會失敗。 –

+0

@ArminRigo完成。 –

回答

3

我發現它了!

的問題在這裏:

class FakeModuleNameGenerator(str): 
    @classmethod 
    def __new__(cls, *args, **kwargs): 
     return str(binascii.b2a_hex(os.urandom(15))) 

由於這個來自海峽繼承,__new__必須接受另一種說法。 Doh!

相關問題