~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Lib/ctypes/test/test_python_api.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from ctypes import *
 
2
import unittest, sys
 
3
from ctypes.test import is_resource_enabled
 
4
 
 
5
################################################################
 
6
# This section should be moved into ctypes\__init__.py, when it's ready.
 
7
 
 
8
from _ctypes import PyObj_FromPtr
 
9
 
 
10
################################################################
 
11
 
 
12
from sys import getrefcount as grc
 
13
if sys.version_info > (2, 4):
 
14
    c_py_ssize_t = c_size_t
 
15
else:
 
16
    c_py_ssize_t = c_int
 
17
 
 
18
class PythonAPITestCase(unittest.TestCase):
 
19
 
 
20
    def test_PyBytes_FromStringAndSize(self):
 
21
        PyBytes_FromStringAndSize = pythonapi.PyBytes_FromStringAndSize
 
22
 
 
23
        PyBytes_FromStringAndSize.restype = py_object
 
24
        PyBytes_FromStringAndSize.argtypes = c_char_p, c_py_ssize_t
 
25
 
 
26
        self.failUnlessEqual(PyBytes_FromStringAndSize(b"abcdefghi", 3), b"abc")
 
27
 
 
28
    def test_PyString_FromString(self):
 
29
        pythonapi.PyBytes_FromString.restype = py_object
 
30
        pythonapi.PyBytes_FromString.argtypes = (c_char_p,)
 
31
 
 
32
        s = b"abc"
 
33
        refcnt = grc(s)
 
34
        pyob = pythonapi.PyBytes_FromString(s)
 
35
        self.failUnlessEqual(grc(s), refcnt)
 
36
        self.failUnlessEqual(s, pyob)
 
37
        del pyob
 
38
        self.failUnlessEqual(grc(s), refcnt)
 
39
 
 
40
    if is_resource_enabled("refcount"):
 
41
        # This test is unreliable, because it is possible that code in
 
42
        # unittest changes the refcount of the '42' integer.  So, it
 
43
        # is disabled by default.
 
44
        def test_PyLong_Long(self):
 
45
            ref42 = grc(42)
 
46
            pythonapi.PyLong_FromLong.restype = py_object
 
47
            self.failUnlessEqual(pythonapi.PyLong_FromLong(42), 42)
 
48
 
 
49
            self.failUnlessEqual(grc(42), ref42)
 
50
 
 
51
            pythonapi.PyLong_AsLong.argtypes = (py_object,)
 
52
            pythonapi.PyLong_AsLong.restype = c_long
 
53
 
 
54
            res = pythonapi.PyLong_AsLong(42)
 
55
            self.failUnlessEqual(grc(res), ref42 + 1)
 
56
            del res
 
57
            self.failUnlessEqual(grc(42), ref42)
 
58
 
 
59
    def test_PyObj_FromPtr(self):
 
60
        s = "abc def ghi jkl"
 
61
        ref = grc(s)
 
62
        # id(python-object) is the address
 
63
        pyobj = PyObj_FromPtr(id(s))
 
64
        self.failUnless(s is pyobj)
 
65
 
 
66
        self.failUnlessEqual(grc(s), ref + 1)
 
67
        del pyobj
 
68
        self.failUnlessEqual(grc(s), ref)
 
69
 
 
70
    def test_PyOS_snprintf(self):
 
71
        PyOS_snprintf = pythonapi.PyOS_snprintf
 
72
        PyOS_snprintf.argtypes = POINTER(c_char), c_size_t, c_char_p
 
73
 
 
74
        buf = c_buffer(256)
 
75
        PyOS_snprintf(buf, sizeof(buf), "Hello from %s", b"ctypes")
 
76
        self.failUnlessEqual(buf.value, b"Hello from ctypes")
 
77
 
 
78
        PyOS_snprintf(buf, sizeof(buf), "Hello from %s (%d, %d, %d)", b"ctypes", 1, 2, 3)
 
79
        self.failUnlessEqual(buf.value, b"Hello from ctypes (1, 2, 3)")
 
80
 
 
81
        # not enough arguments
 
82
        self.failUnlessRaises(TypeError, PyOS_snprintf, buf)
 
83
 
 
84
    def test_pyobject_repr(self):
 
85
        self.failUnlessEqual(repr(py_object()), "py_object(<NULL>)")
 
86
        self.failUnlessEqual(repr(py_object(42)), "py_object(42)")
 
87
        self.failUnlessEqual(repr(py_object(object)), "py_object(%r)" % object)
 
88
 
 
89
if __name__ == "__main__":
 
90
    unittest.main()