I am using https://github.com/ekonda/kutana.
plugins/test.py:
from kutana import Plugin
import pickle
plugin = Plugin(name="Tictactoe",
description="Tictactoe Game",
invites={},
games=[])
#plugin.on_start()
async def _():
plugin.games = [Game()]
# Backup games when bot is shutting down
#plugin.on_shutdown()
async def _():
try:
with open("games.pickle", "wb") as f:
pickle.dump(plugin.games, f)
except Exception as e:
print(e)
class Game:
def __init__(self):
pass
run.py:
from kutana import Kutana, load_plugins
# Create application
app = Kutana()
# Load and register plugins
app.add_plugins(load_plugins("plugins/"))
if __name__ == "__main__":
# Run application
app.run()
When plugin.games is empty, pickle dumps without errors. But if I put any created object of class Game here, I get an error when trying to backup:
Traceback (most recent call last):
File "C:\Users\root\Desktop\Celestiana\plugins\games\tictactoe.py", line 17, in _
pickle.dump(pl.games, f)
_pickle.PicklingError: Can't pickle <class 'plugins/games\tictactoe.py.Game'>: import of module 'plugins/games\\tictactoe.py' failed
How can I fix it? I try to do something like pickle.dump(plugin.games, f, fix_imports=True) but it does not work.
Fix how you import a Python source file directly.
In load_plugins_from_file, specify the correct name.
def load_plugins_from_file(path, verbose=False):
...
# mod = import_module(path, path) # Change this
name = path.split(".py")[0].replace("/", ".") # to these
mod = import_module(name, path) # two lines
...
In your custom import_module, insert the module into sys.modules as shown in the recipe https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly.
def import_module(name, path):
...
spec = importlib.util.spec_from_file_location(name, os.path.abspath(path))
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module # Add this
spec.loader.exec_module(module)
return module
I'm trying this for almost two hours now, without any luck.
I have a module that looks like this:
try:
from zope.component import queryUtility # and things like this
except ImportError:
# do some fallback operations <-- how to test this?
Later in the code:
try:
queryUtility(foo)
except NameError:
# do some fallback actions <-- this one is easy with mocking
# zope.component.queryUtility to raise a NameError
Any ideas?
EDIT:
Alex's suggestion doesn't seem to work:
>>> import __builtin__
>>> realimport = __builtin__.__import__
>>> def fakeimport(name, *args, **kw):
... if name == 'zope.component':
... raise ImportError
... realimport(name, *args, **kw)
...
>>> __builtin__.__import__ = fakeimport
When running the tests:
aatiis#aiur ~/work/ao.shorturl $ ./bin/test --coverage .
Running zope.testing.testrunner.layer.UnitTests tests:
Set up zope.testing.testrunner.layer.UnitTests in 0.000 seconds.
Error in test /home/aatiis/work/ao.shorturl/src/ao/shorturl/shorturl.txt
Traceback (most recent call last):
File "/usr/lib64/python2.5/unittest.py", line 260, in run
testMethod()
File "/usr/lib64/python2.5/doctest.py", line 2123, in runTest
test, out=new.write, clear_globs=False)
File "/usr/lib64/python2.5/doctest.py", line 1361, in run
return self.__run(test, compileflags, out)
File "/usr/lib64/python2.5/doctest.py", line 1282, in __run
exc_info)
File "/usr/lib64/python2.5/doctest.py", line 1148, in report_unexpected_exception
'Exception raised:\n' + _indent(_exception_traceback(exc_info)))
File "/usr/lib64/python2.5/doctest.py", line 1163, in _failure_header
out.append(_indent(source))
File "/usr/lib64/python2.5/doctest.py", line 224, in _indent
return re.sub('(?m)^(?!$)', indent*' ', s)
File "/usr/lib64/python2.5/re.py", line 150, in sub
return _compile(pattern, 0).sub(repl, string, count)
File "/usr/lib64/python2.5/re.py", line 239, in _compile
p = sre_compile.compile(pattern, flags)
File "/usr/lib64/python2.5/sre_compile.py", line 507, in compile
p = sre_parse.parse(p, flags)
AttributeError: 'NoneType' object has no attribute 'parse'
Error in test BaseShortUrlHandler (ao.shorturl)
Traceback (most recent call last):
File "/usr/lib64/python2.5/unittest.py", line 260, in run
testMethod()
File "/usr/lib64/python2.5/doctest.py", line 2123, in runTest
test, out=new.write, clear_globs=False)
File "/usr/lib64/python2.5/doctest.py", line 1351, in run
self.debugger = _OutputRedirectingPdb(save_stdout)
File "/usr/lib64/python2.5/doctest.py", line 324, in __init__
pdb.Pdb.__init__(self, stdout=out)
File "/usr/lib64/python2.5/pdb.py", line 57, in __init__
cmd.Cmd.__init__(self, completekey, stdin, stdout)
File "/usr/lib64/python2.5/cmd.py", line 90, in __init__
import sys
File "<doctest shorturl.txt[10]>", line 4, in fakeimport
NameError: global name 'realimport' is not defined
However, it does work when I run the same code from the python interactive console.
MORE EDIT:
I'm using zope.testing and a test file, shorturl.txt that has all the tests specific to this part of my module. First I'm importing the module with zope.component available, to demonstrate & test the usual usage. The absence of zope.* packages is considered an edge-case, so I'm testing it later. Thus, I have to reload() my module, after making zope.* unavailable, somehow.
So far I've even tried using tempfile.mktempdir() and empty zope/__init__.py and zope/component/__init__.py files in the tempdir, then inserting tempdir to sys.path[0], and removing the old zope.* packages from sys.modules.
Didn't work either.
EVEN MORE EDIT:
In the meantime, I've tried this:
>>> class NoZope(object):
... def find_module(self, fullname, path):
... if fullname.startswith('zope'):
... raise ImportError
...
>>> import sys
>>> sys.path.insert(0, NoZope())
And it works well for the namespace of the test suite (= for all imports in shorturl.txt), but it is not executed in my main module, ao.shorturl. Not even when I reload() it. Any idea why?
>>> import zope # ok, this raises an ImportError
>>> reload(ao.shorturl) <module ...>
Importing zope.interfaces raises an ImportError, so it doesn't get to the part where I import zope.component, and it remains in the ao.shorturl namespace. Why?!
>>> ao.shorturl.zope.component # why?!
<module ...>
Just monkeypatch into the builtins your own version of __import__ -- it can raise whatever you wish when it recognizes it's being called on the specific modules for which you want to mock up errors. See the docs for copious detail. Roughly:
try:
import builtins
except ImportError:
import __builtin__ as builtins
realimport = builtins.__import__
def myimport(name, globals, locals, fromlist, level):
if ...:
raise ImportError
return realimport(name, globals, locals, fromlist, level)
builtins.__import__ = myimport
In lieu of the ..., you can hardcode name == 'zope.component', or arrange things more flexibly with a callback of your own that can make imports raise on demand in different cases, depending on your specific testing needs, without requiring you to code multiple __import__-alike functions;-).
Note also that if what you use, instead of import zope.component or from zope.component import something, is from zope import component, the name will then be 'zope', and 'component' will then be the only item in the fromlist.
Edit: the docs for the __import__ function say that the name to import is builtin (like in Python 3), but in fact you need __builtins__ -- I've edited the code above so that it works either way.
This is what I justed in my unittests.
It uses PEP-302 "New Import Hooks". (Warning: the PEP-302 document and the more concise release notes I linked aren't exactly accurate.)
I use meta_path because it's as early as possible in the import sequence.
If the module has already been imported (as in my case, because earlier unittests mock against it), then it's necessary to remove it from sys.modules before doing the reload on the dependent module.
# Ensure we fallback to using ~/.pif if XDG doesn't exist.
>>> import sys
>>> class _():
... def __init__(self, modules):
... self.modules = modules
...
... def find_module(self, fullname, path=None):
... if fullname in self.modules:
... raise ImportError('Debug import failure for %s' % fullname)
>>> fail_loader = _(['xdg.BaseDirectory'])
>>> sys.meta_path.append(fail_loader)
>>> del sys.modules['xdg.BaseDirectory']
>>> reload(pif.index) #doctest: +ELLIPSIS
<module 'pif.index' from '...'>
>>> pif.index.CONFIG_DIR == os.path.expanduser('~/.pif')
True
>>> sys.meta_path.remove(fail_loader)
Where the code inside pif.index looks like:
try:
import xdg.BaseDirectory
CONFIG_DIR = os.path.join(xdg.BaseDirectory.xdg_data_home, 'pif')
except ImportError:
CONFIG_DIR = os.path.expanduser('~/.pif')
To answer the question about why the newly reloaded module has properties of the old and new loads, here are two example files.
The first is a module y with an import failure case.
# y.py
try:
import sys
_loaded_with = 'sys'
except ImportError:
import os
_loaded_with = 'os'
The second is x which demonstrates how leaving handles about for a module can affect its properties when being reloaded.
# x.py
import sys
import y
assert y._loaded_with == 'sys'
assert y.sys
class _():
def __init__(self, modules):
self.modules = modules
def find_module(self, fullname, path=None):
if fullname in self.modules:
raise ImportError('Debug import failure for %s' % fullname)
# Importing sys will not raise an ImportError.
fail_loader = _(['sys'])
sys.meta_path.append(fail_loader)
# Demonstrate that reloading doesn't work if the module is already in the
# cache.
reload(y)
assert y._loaded_with == 'sys'
assert y.sys
# Now we remove sys from the modules cache, and try again.
del sys.modules['sys']
reload(y)
assert y._loaded_with == 'os'
assert y.sys
assert y.os
# Now we remove the handles to the old y so it can get garbage-collected.
del sys.modules['y']
del y
import y
assert y._loaded_with == 'os'
try:
assert y.sys
except AttributeError:
pass
assert y.os
If you don't mind changing your program itself, you could also put the import call in a function and patch that in your tests.
When I run the following python code on Python 3.4
# -*- coding: utf-8 -*-
import unittest, time, re
from selenium.selenium import selenium
class google(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*chrome", "https://www.google.com/")
self.selenium.start()
def test_google(self):
sel = self.selenium
sel.open("/?gws_rd=ssl")
sel.type("id=gbqfq", "")
sel.type("id=gbqfq", "test")
def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
I have the error name 'unicode' is not defined:
======================================================================
ERROR: test_google (__main__.google)
----------------------------------------------------------------------
Traceback (most recent call last):
File "google.py", line 11, in setUp
self.selenium.start()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium-2.43.0-py3.4.egg/selenium/selenium.py", line 201, in start
result = self.get_string("getNewBrowserSession", start_args)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium-2.43.0-py3.4.egg/selenium/selenium.py", line 236, in get_string
result = self.do_command(verb, args)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium-2.43.0-py3.4.egg/selenium/selenium.py", line 215, in do_command
body = 'cmd=' + urllib_parse.quote_plus(unicode(verb).encode('utf-8'))
NameError: name 'unicode' is not defined
----------------------------------------------------------------------
What's wrong? Is there a way to workaround it?
Exactly as it says, unicode is no longer a built-in in 3.x - instead, the str type describes a Unicode string.
You should not be getting this message, because it's occurring within the Selenium code. This indicates that you have installed Selenium improperly - you have the 2.x code but are trying to use it from 3.x.
A quick check of the documentation suggests that 3.x is supported by Selenium - but you'll need to uninstall and reinstall it, and make sure you get it right this time.
In Python, how do I mock an object created in a with statement using mox unit test library
Code
class MyCode:
def generate_gzip_file(self):
with gzip.GzipFile('file_name.txt.gz','wb') as f:
f.write('data')
Unit Test
class MyCodeTest(unittest.TestCase):
def test_generate_gzip_file(self):
mox = mox.Mox()
mock_gzip_file = self.mox.CreateMock(gzip.GzipFile)
mox.StubOutWithMock(gzip, 'GzipFile')
gzip.GzipFile('file_name.txt.gz','wb').AndReturn(mock_file)
mock_gzip_file.write('data')
mox.ReplayAll()
MyCode().generate_gzip_file()
mox.VerifyAll()
I get the error AttributeError: __exit__ on line
with gzip.GzipFile('file_name.txt.gz','wb') as f:
DSM is correct that the mocked instance of gzip.GzipFile isn't ending up with a __exit__ method for some reason. You'll get exactly the same error if you forget to define __exit__ on a class you use with a with statement. For example:
>>> class C(object):
... def __enter__(self):
... return self
...
>>> with C() as c:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: __exit__
Fortunately, you can work around the problem by using Mox's CreateMockAnything() method to create a mock_gzip_file object that doesn't enforce a particular interface. You'll need to be careful to ensure that you set up the expectations for the mock_gzip_file object correctly (i.e. that you set up expectations for when and how the __enter__() and __exit__(...) methods will be called). Here's an example that worked for me:
import gzip
import mox
import unittest
class MyCode:
def generate_gzip_file(self):
with gzip.GzipFile('file_name.txt.gz', 'wb') as f:
f.write('data')
class MyCodeTest(unittest.TestCase):
def test_generate_gzip_file(self):
mymox = mox.Mox()
mock_gzip_file = mymox.CreateMockAnything()
mymox.StubOutWithMock(gzip, 'GzipFile')
gzip.GzipFile('file_name.txt.gz', 'wb').AndReturn(mock_gzip_file)
mock_gzip_file.__enter__().AndReturn(mock_gzip_file)
mock_gzip_file.write('data')
mock_gzip_file.__exit__(None, None, None).AndReturn(None)
mymox.ReplayAll()
MyCode().generate_gzip_file()
mymox.VerifyAll()
if __name__ == '__main__':
unittest.main()
When I run this I get:
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Which way is proper for mocking and testing code that iters object returned by open(), using mock library?
whitelist_data.py:
WHITELIST_FILE = "testdata.txt"
format_str = lambda s: s.rstrip().lstrip('www.')
whitelist = None
with open(WHITELIST_FILE) as whitelist_data:
whitelist = set(format_str(line) for line in whitelist_data)
if not whitelist:
raise RuntimeError("Can't read data from %s file" % WHITELIST_FILE)
def is_whitelisted(substr):
return 1 if format_str(substr) in whitelist else 0
Here's how I try to test it.
import unittest
import mock
TEST_DATA = """
domain1.com
domain2.com
domain3.com
"""
class TestCheckerFunctions(unittest.TestCase):
def test_is_whitelisted_method(self):
open_mock = mock.MagicMock()
with mock.patch('__builtin__.open',open_mock):
manager = open_mock.return_value.__enter__.return_value
manager.__iter__ = lambda s: iter(TEST_DATA.splitlines())
from whitelist_data import is_whitelisted
self.assertTrue(is_whitelisted('domain1.com'))
if __name__ == '__main__':
unittest.main()
Result of python tests.py is:
$ python tests.py
E
======================================================================
ERROR: test_is_whitelisted_method (__main__.TestCheckerFunctions)
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests.py", line 39, in test_is_whitelisted_method
from whitelist_data import is_whitelisted
File "/Users/supa/Devel/python/whitelist/whitelist_data.py", line 20, in <module>
whitelist = set(format_str(line) for line in whitelist_data)
TypeError: 'Mock' object is not iterable
----------------------------------------------------------------------
Ran 1 test in 0.001s
UPD: Thanks to Adam, I've reinstalled mock library(pip install -e hg+https://code.google.com/p/mock#egg=mock) and updated tests.py. Works like a charm.
You're looking for a MagicMock. This supports iteration.
In mock 0.80beta4, patch returns a MagicMock. So this simple example works:
import mock
def foo():
for line in open('myfile'):
print line
#mock.patch('__builtin__.open')
def test_foo(open_mock):
foo()
assert open_mock.called
If you're running mock 0.7.x (It looks like you are), I don't think you can accomplish this with patch alone. You'll need to create the mock separately, then pass it into patch:
import mock
def foo():
for line in open('myfile'):
print line
def test_foo():
open_mock = mock.MagicMock()
with mock.patch('__builtin__.open', open_mock):
foo()
assert open_mock.called
Note - I've run these with py.test, however, these same approaches will work with unittest as well.