public
Description: Google's Webkit, ported to Python, ported to Desktops.
Homepage: http://lkcl.net/pyjamas-desktop
Clone URL: git://github.com/lkcl/pyjamas-desktop.git
100755 87 lines (67 sloc) 2.053 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#! /usr/bin/env python2.3
 
import threading
import unittest
 
"""Thread local storage
 
This module provides the same API for local the threading module in
python 2.4.x.
"""
 
def _getdata(data):
 
    t = threading.currentThread()
    try:
        return data[t]
    except KeyError:
        d = {}
        data[t] = d
        return d
 
 
class local(object):
    """Thread local storage
 
Attributes stored on an instance of this class are local to the
current thread.
"""
 
    def __init__(self):
        object.__setattr__(self, '_local_data', {})
 
    def __setattr__(self, key, value):
        _getdata(self._local_data)[key] = value
 
    def __getattr__(self, key):
        try:
            return _getdata(self._local_data)[key]
        except KeyError:
            raise AttributeError('%s has no attribute' %
                                 (repr(self), repr(key)))
    def __delattr__(self, key):
        try:
            del _getdata(self._local_data)[key]
        except KeyError:
            raise AttributeError('%s has no attribute' %
                                 (repr(self), repr(key)))
    def __delitem__(self, key):
        del self._local_data[key]
 
    def __contains__(self, key):
        return key in _getdata(self._local_data)
 
 
def _test_thread():
    l.x = 'test thread'
    import sys
    sys.exit(0)
 
 
class TestSequenceFunctions(unittest.TestCase):
 
    def test_register_user(self):
       l = local()
       globals()['l'] = l
       self.assert_('x' not in l)
       l.x = 'only thread'
       self.assertEqual(l.x, 'only thread')
       self.assert_('x' in l)
       del l.x
       self.assert_('x' not in l)
       t = threading.Thread(target=_test_thread, name='tt')
       t.setDaemon(True)
       t.start()
       t.join()
       self.assert_('x' not in l)
       l.x = 'main thread'
       self.assertEqual(l.x, 'main thread')
       self.assert_('x' in l)
       del l[t]
       self.assertEqual(l.x, 'main thread')
       self.assert_('x' in l)
 
if __name__ == '__main__':
    unittest.main()