Skip to content

Commit

Permalink
Merge pull request #2 from NextThought/extensionclass
Browse files Browse the repository at this point in the history
Make the pure-Python implementation extend ExtensionClass.Base like the C version
  • Loading branch information
tseaver committed Apr 11, 2015
2 parents 3e7473e + 5ef5ba4 commit c5baae5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 16 deletions.
21 changes: 11 additions & 10 deletions .travis.yml
@@ -1,16 +1,17 @@
language: python
sudo: false
python:
- 2.6
- 2.7
- 3.2
- 3.3
- 3.4
- pypy
- pypy3
python: 2.7
env:
- TOX_ENV=py26
- TOX_ENV=py27
- TOX_ENV=py32
- TOX_ENV=py33
- TOX_ENV=py34
- TOX_ENV=pypy
- TOX_ENV=pypy3
install:
- pip install nose
- pip install tox
script:
- nosetests
- tox -e $TOX_ENV
notifications:
email: false
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -67,4 +67,5 @@
install_requires=['ExtensionClass'],
include_package_data=True,
zip_safe=False,
test_suite="MultiMapping.tests",
)
7 changes: 4 additions & 3 deletions src/MultiMapping/_MultiMapping.c
Expand Up @@ -139,10 +139,11 @@ MM_getattr(MMobject *self, char *name)
return Py_FindMethod(MM_methods, (PyObject *)self, name);
}

static int
static Py_ssize_t
MM_length(MMobject *self)
{
long l=0, el, i;
Py_ssize_t l=0;
long el, i;
PyObject *e=0;

UNLESS(-1 != (i=PyList_Size(self->data))) return -1;
Expand All @@ -156,7 +157,7 @@ MM_length(MMobject *self)
}

static PyMappingMethods MM_as_mapping = {
(inquiry)MM_length, /*mp_length*/
(lenfunc)MM_length, /*mp_length*/
(binaryfunc)MM_subscript, /*mp_subscript*/
(objobjargproc)NULL, /*mp_ass_subscript*/
};
Expand Down
4 changes: 3 additions & 1 deletion src/MultiMapping/__init__.py
@@ -1,7 +1,8 @@
import os

from ExtensionClass import Base

class MultiMapping:
class MultiMapping(Base):
__slots__ = ('__dicts__',)

def __init__(self, *args):
Expand Down Expand Up @@ -38,6 +39,7 @@ def pop(self, n=-1):
def __len__(self):
return sum(len(d) for d in self.__dicts__)

pyMultiMapping = MultiMapping

if 'PURE_PYTHON' not in os.environ: # pragma no cover
try:
Expand Down
17 changes: 15 additions & 2 deletions src/MultiMapping/tests.py
Expand Up @@ -17,9 +17,12 @@

class TestMultiMapping(unittest.TestCase):

def _makeOne(self):
def _getTargetClass(self):
from MultiMapping import MultiMapping
return MultiMapping()
return MultiMapping

def _makeOne(self):
return self._getTargetClass()()

def test_push(self):
m = self._makeOne()
Expand Down Expand Up @@ -63,10 +66,20 @@ def test_len(self):
m.push({'spam': 1})
self.assertEqual(len(m), 1)

def test_is_extension_class(self):
from ExtensionClass import Base
self.assertTrue(isinstance(self._makeOne(), Base))

class TestPyMultiMapping(TestMultiMapping):

def _getTargetClass(self):
from MultiMapping import pyMultiMapping
return pyMultiMapping

def test_suite():
return unittest.TestSuite((
unittest.makeSuite(TestMultiMapping),
unittest.makeSuite(TestPyMultiMapping),
))

if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Expand Up @@ -7,6 +7,7 @@ commands =
nosetests
deps =
nose
ExtensionClass

[testenv:py27-pure]
basepython =
Expand Down

0 comments on commit c5baae5

Please sign in to comment.