Skip to content

Commit 75afa15

Browse files
author
Anselm Kruis
committed
Issue python#102: Improve stackless.cstack
- Fix stackless.cstack.__str__. It was broken for Python 3. Now it returns the stack decoded as iso-8859-1 byte string. - New read-only attribute stackless.cstack.nesting_level. It can be used to order multiple cstack objects belonging to the same tasklet. - Added a few test cases https://bitbucket.org/stackless-dev/stackless/issues/102
1 parent 864d8a1 commit 75afa15

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

Stackless/core/stacklesseval.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,12 @@ Note: For inspection, str() can dump it as a string.\
189189

190190

191191
static PyMemberDef cstack_members[] = {
192-
{"size", T_INT, offsetof(PyCStackObject, ob_base.ob_size), READONLY},
192+
{"size", T_PYSSIZET, offsetof(PyCStackObject, ob_base.ob_size), READONLY},
193193
{"next", T_OBJECT, offsetof(PyCStackObject, next), READONLY},
194194
{"prev", T_OBJECT, offsetof(PyCStackObject, prev), READONLY},
195195
{"task", T_OBJECT, offsetof(PyCStackObject, task), READONLY},
196196
{"startaddr", T_ADDR, offsetof(PyCStackObject, startaddr), READONLY},
197+
{"nesting_level", T_INT, offsetof(PyCStackObject, nesting_level), READONLY},
197198
{0}
198199
};
199200

@@ -203,8 +204,9 @@ static PyObject *
203204
cstack_str(PyObject *o)
204205
{
205206
PyCStackObject *cst = (PyCStackObject*)o;
206-
return PyUnicode_FromStringAndSize((char*)&cst->stack,
207-
Py_SIZE(cst)*sizeof(cst->stack[0]));
207+
return PyUnicode_Decode((char*)&cst->stack,
208+
Py_SIZE(cst)*sizeof(cst->stack[0]),
209+
"latin_1", "strict");
208210
}
209211

210212
PyTypeObject PyCStack_Type = {

Stackless/unittests/test_miscell.py

+25
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import contextlib
1111
import time
1212
import os
13+
import struct
1314
from stackless import _test_nostacklesscall as apply_not_stackless
1415
try:
1516
import _thread as thread
@@ -1224,6 +1225,30 @@ def test_threads(self):
12241225
self.assertEqual(type(stackless.threads), list)
12251226

12261227

1228+
class TestCstate(StacklessTestCase):
1229+
def test_cstate(self):
1230+
self.assertIsInstance(stackless.main.cstate, stackless.cstack)
1231+
1232+
def test_str_size(self):
1233+
c = stackless.main.cstate
1234+
s = str(c)
1235+
self.assertEqual(len(s), c.size * struct.calcsize("P"))
1236+
1237+
def test_nesting_level(self):
1238+
c = stackless.main.cstate
1239+
l1 = c.nesting_level
1240+
self.assertIsInstance(l1, int)
1241+
1242+
def test_chain(self):
1243+
start = stackless.main.cstate
1244+
c = start.next
1245+
self.assertIsNot(c, start)
1246+
while(c is not start):
1247+
self.assertIsInstance(c, stackless.cstack)
1248+
self.assertIs(c.prev.next, c)
1249+
c = c.next
1250+
1251+
12271252
#///////////////////////////////////////////////////////////////////////////////
12281253

12291254
if __name__ == '__main__':

0 commit comments

Comments
 (0)