diff -r 2766b8a66f99 Cython/Compiler/PyrexTypes.py
--- a/Cython/Compiler/PyrexTypes.py	Thu May 14 17:47:21 2009 -0300
+++ b/Cython/Compiler/PyrexTypes.py	Thu May 14 20:32:50 2009 -0300
@@ -165,6 +165,10 @@
     
     is_typedef = 1
     typedef_is_external = 0
+
+    _to_py_function = None
+    _from_py_function = None
+    
     
     def __init__(self, cname, base_type, is_external=0):
         self.typedef_cname = cname
@@ -195,14 +199,62 @@
             return self.typedef_base_type.cast_code(expr_code)
         else:
             return BaseType.cast_code(self, expr_code)
-    
+
     def __repr__(self):
         return "<CTypedefType %s>" % self.typedef_cname
     
     def __str__(self):
         return self.declaration_name(for_display = 1)
-    
+
+    def create_to_py_utility_code(self, env):
+        base_type = self.typedef_base_type
+        if self.typedef_is_external:
+            if not self._to_py_function:
+                if base_type.is_int:
+                    type_name = self.qualified_name.split('.')[-1]
+                    sign_word = base_type.sign_words[base_type.signed].strip().title()
+                    #
+                    utility_code = c_tpdef_int_to_py_function.specialize(
+                        None,
+                        type = self.typedef_cname,
+                        signed=base_type.signed,
+                        TypeName=type_name)
+                    env.use_utility_code(utility_code)
+                    self._to_py_function = '__Pyx_PyInt_From_%s' % type_name
+                    return True
+        return base_type.create_to_py_utility_code(env)
+
+    def create_from_py_utility_code(self, env):
+        base_type = self.typedef_base_type
+        if self.typedef_is_external:
+            if not self._from_py_function:
+                if base_type.is_int:
+                    type_name = self.qualified_name.split('.')[-1]
+                    sign_word = base_type.sign_words[base_type.signed].strip().title()
+                    #
+                    utility_code = c_tpdef_int_from_py_function.specialize(
+                        None,
+                        type = self.typedef_cname,
+                        SignWord=sign_word,
+                        TypeName=type_name)
+                    env.use_utility_code(utility_code)
+                    self._from_py_function = '__Pyx_PyInt_As_%s' % type_name
+                    return True
+        return base_type.create_from_py_utility_code(env)
+
+
     def __getattr__(self, name):
+        if self.typedef_base_type.is_int:
+            base_type = self.typedef_base_type
+            type_name = self.qualified_name.split('.')[-1]
+            if name == 'error_condition':
+                return lambda result_code: "PyErr_Occurred()"
+            elif name == 'to_py_function':
+                assert self._to_py_function
+                return self._to_py_function
+            elif name == 'from_py_function':
+                assert self._from_py_function
+                return self._from_py_function
         return getattr(self.typedef_base_type, name)
 
 class BufferType(BaseType):
@@ -580,7 +632,43 @@
             return (%(type)s)-1;
         }"""
 
+c_tpdef_int_from_py_function = UtilityCode(
+proto="""
+static INLINE %(type)s __Pyx_PyInt_As_%(TypeName)s(PyObject *);
+""",
+impl="""
+static INLINE %(type)s __Pyx_PyInt_As_%(TypeName)s(PyObject* x) {
+  /**/ if (sizeof(%(type)s) == sizeof(char))
+      return (%(type)s)__Pyx_PyInt_As%(SignWord)sChar(x);
+  else if (sizeof(%(type)s) == sizeof(short))
+      return (%(type)s)__Pyx_PyInt_As%(SignWord)sShort(x);
+  else if (sizeof(%(type)s) == sizeof(int))
+      return (%(type)s)__Pyx_PyInt_As%(SignWord)sInt(x);
+  else if (sizeof(%(type)s) == sizeof(long))
+      return (%(type)s)__Pyx_PyInt_As%(SignWord)sLong(x);
+  else if (sizeof(%(type)s) == sizeof(PY_LONG_LONG))
+      return (%(type)s)__Pyx_PyInt_As%(SignWord)sLongLong(x);
+  PyErr_SetString(PyExc_TypeError, "%(TypeName)s");
+  return (%(type)s)-1;
+}
+""")
 
+c_tpdef_int_to_py_function = UtilityCode(
+proto="""
+static INLINE PyObject *__Pyx_PyInt_From_%(TypeName)s(%(type)s);
+""",
+impl="""
+static INLINE PyObject * __Pyx_PyInt_From_%(TypeName)s(%(type)s val) {
+  /**/ if (sizeof(%(type)s) <  sizeof(long))
+      return PyInt_FromLong((long)val);
+  else if (sizeof(%(type)s) == sizeof(long))
+     return %(signed)d ? PyInt_FromLong((long)val) :
+               PyLong_FromUnsignedLong((unsigned long)val);
+  else /* (sizeof(%(type)s) >  sizeof(long)) */
+     return %(signed)d ? PyLong_FromLongLong((PY_LONG_LONG)val) :
+               PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)val);
+}
+""")
 
 class CIntType(CNumericType):
 
diff -r 2766b8a66f99 tests/run/tpdef_int_types.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/run/tpdef_int_types.h	Thu May 14 20:32:50 2009 -0300
@@ -0,0 +1,10 @@
+typedef signed   char  	    SChar;
+typedef unsigned char  	    UChar;
+typedef signed   short 	    SShort;
+typedef unsigned short 	    UShort;
+typedef signed   int   	    SInt;
+typedef unsigned int   	    UInt;
+typedef signed   long  	    SLong;
+typedef unsigned long  	    ULong;
+typedef signed   long long  SLongLong;
+typedef unsigned long long  ULongLong;
diff -r 2766b8a66f99 tests/run/tpdef_int_types.pyx
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/run/tpdef_int_types.pyx	Thu May 14 20:32:50 2009 -0300
@@ -0,0 +1,448 @@
+__doc__ = u""
+
+cdef extern from "tpdef_int_types.h":
+    ctypedef signed   SChar     ## "signed char"
+    ctypedef unsigned UChar     ## "unsigned char"
+    ctypedef signed   SShort    ## "signed short"
+    ctypedef unsigned UShort    ## "unsigned short"
+    ctypedef signed   SInt      ## "signed int"
+    ctypedef unsigned UInt      ## "unsigned int"
+    ctypedef signed   SLong     ## "signed long"
+    ctypedef unsigned ULong     ## "unsigned long"
+    ctypedef signed   SLongLong ## "signed PY_LONG_LONG"
+    ctypedef unsigned ULongLong ## "unsigned PY_LONG_LONG"
+
+# -------------------------------------------------------------------
+
+SCHAR_MAX = <SChar>((<UChar>-1)>>1)
+SCHAR_MIN = (-SCHAR_MAX-1)
+
+def test_schar(SChar x):
+   u"""
+   >>> test_schar(-129) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   >>> test_schar(-128)
+   -128
+   >>> test_schar(0)
+   0
+   >>> test_schar(127)
+   127
+   >>> test_schar(128) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   return x
+
+def test_add_schar(x, y):
+   u"""
+   >>> test_add_schar(SCHAR_MIN, -1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   >>> test_add_schar(SCHAR_MIN, 0) == SCHAR_MIN
+   True
+   >>> test_add_schar(SCHAR_MIN, 1) == SCHAR_MIN+1
+   True
+   >>> test_add_schar(SCHAR_MAX, -1) == SCHAR_MAX-1
+   True
+   >>> test_add_schar(SCHAR_MAX, 0) == SCHAR_MAX
+   True
+   >>> test_add_schar(SCHAR_MAX, 1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   cdef SChar r = x + y
+   return r
+
+UCHAR_MAX = <UChar>((<UChar>-1))
+
+def test_uchar(UChar x):
+   u"""
+   >>> test_uchar(-1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   >>> test_uchar(0)
+   0
+   >>> test_uchar(1)
+   1
+   >>> test_uchar(UCHAR_MAX) == UCHAR_MAX
+   True
+   >>> test_uchar(UCHAR_MAX+1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   return x
+
+def test_add_uchar(x, y):
+   u"""
+   >>> test_add_uchar(UCHAR_MAX, 0) == UCHAR_MAX
+   True
+   >>> test_add_uchar(UCHAR_MAX, 1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   cdef UChar r = x + y
+   return r
+
+# -------------------------------------------------------------------
+
+SSHORT_MAX = <SShort>((<UShort>-1)>>1)
+SSHORT_MIN = (-SSHORT_MAX-1)
+
+def test_sshort(short x):
+   u"""
+   >>> test_sshort(SSHORT_MIN-1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   >>> test_sshort(SSHORT_MIN) == SSHORT_MIN
+   True
+   >>> test_sshort(-1)
+   -1
+   >>> test_sshort(0)
+   0
+   >>> test_sshort(1)
+   1
+   >>> test_sshort(SSHORT_MAX) == SSHORT_MAX
+   True
+   >>> test_sshort(SSHORT_MAX+1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   return x
+
+def test_add_sshort(x, y):
+   u"""
+   >>> test_add_sshort(SSHORT_MIN, -1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   >>> test_add_sshort(SSHORT_MIN, 0) == SSHORT_MIN
+   True
+   >>> test_add_sshort(SSHORT_MIN, 1) == SSHORT_MIN+1
+   True
+   >>> test_add_sshort(SSHORT_MAX, -1) == SSHORT_MAX-1
+   True
+   >>> test_add_sshort(SSHORT_MAX, 0) == SSHORT_MAX
+   True
+   >>> test_add_sshort(SSHORT_MAX, 1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   cdef SShort r = x + y
+   return r
+
+USHORT_MAX = <UShort>((<UShort>-1))
+
+def test_ushort(UShort x):
+   u"""
+   >>> test_ushort(-1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   >>> test_ushort(0)
+   0
+   >>> test_ushort(1)
+   1
+   >>> test_ushort(USHORT_MAX) == USHORT_MAX
+   True
+   >>> test_ushort(USHORT_MAX+1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   return x
+
+def test_add_ushort(x, y):
+   u"""
+   >>> test_add_ushort(USHORT_MAX, 0) == USHORT_MAX
+   True
+   >>> test_add_ushort(USHORT_MAX, 1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   cdef UShort r = x + y
+   return r
+
+# -------------------------------------------------------------------
+
+SINT_MAX = <SInt>((<UInt>-1)>>1)
+SINT_MIN = (-SINT_MAX-1)
+
+def test_sint(int x):
+   u"""
+   >>> test_sint(SINT_MIN-1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   >>> test_sint(SINT_MIN) == SINT_MIN
+   True
+   >>> test_sint(-1)
+   -1
+   >>> test_sint(0)
+   0
+   >>> test_sint(1)
+   1
+   >>> test_sint(SINT_MAX) == SINT_MAX
+   True
+   >>> test_sint(SINT_MAX+1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   return x
+
+def test_add_sint(x, y):
+   u"""
+   >>> test_add_sint(SINT_MIN, -1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   >>> test_add_sint(SINT_MIN, 0) == SINT_MIN
+   True
+   >>> test_add_sint(SINT_MIN, 1) == SINT_MIN+1
+   True
+   >>> test_add_sint(SINT_MAX, -1) == SINT_MAX-1
+   True
+   >>> test_add_sint(SINT_MAX, 0) == SINT_MAX
+   True
+   >>> test_add_sint(SINT_MAX, 1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   cdef SInt r = x + y
+   return r
+
+UINT_MAX = <UInt>(<UInt>-1)
+
+def test_uint(UInt x):
+   u"""
+   >>> test_uint(-1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   >>> print(test_uint(0))
+   0
+   >>> print(test_uint(1))
+   1
+   >>> test_uint(UINT_MAX) == UINT_MAX
+   True
+   >>> test_uint(UINT_MAX+1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   return x
+
+def test_add_uint(x, y):
+   u"""
+   >>> test_add_uint(UINT_MAX, 0) == UINT_MAX
+   True
+   >>> test_add_uint(UINT_MAX, 1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   cdef UInt r = x + y
+   return r
+
+# -------------------------------------------------------------------
+
+SLONG_MAX = <SLong>((<ULong>-1)>>1)
+SLONG_MIN = (-SLONG_MAX-1)
+
+def test_slong(long x):
+   u"""
+   >>> test_slong(SLONG_MIN-1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   >>> test_slong(SLONG_MIN) == SLONG_MIN
+   True
+   >>> test_slong(-1)
+   -1
+   >>> test_slong(0)
+   0
+   >>> test_slong(1)
+   1
+   >>> test_slong(SLONG_MAX) == SLONG_MAX
+   True
+   >>> test_slong(SLONG_MAX+1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   return x
+
+def test_add_slong(x, y):
+   u"""
+   >>> test_add_slong(SLONG_MIN, -1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   >>> test_add_slong(SLONG_MIN, 0) == SLONG_MIN
+   True
+   >>> test_add_slong(SLONG_MIN, 1) == SLONG_MIN+1
+   True
+   >>> test_add_slong(SLONG_MAX, -1) == SLONG_MAX-1
+   True
+   >>> test_add_slong(SLONG_MAX, 0) == SLONG_MAX
+   True
+   >>> test_add_slong(SLONG_MAX, 1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   cdef SLong r = x + y
+   return r
+
+ULONG_MAX = <ULong>(<ULong>-1)
+
+def test_ulong(ULong x):
+   u"""
+   >>> test_ulong(-1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   >>> print(test_ulong(0))
+   0
+   >>> print(test_ulong(1))
+   1
+   >>> test_ulong(ULONG_MAX) == ULONG_MAX
+   True
+   >>> test_ulong(ULONG_MAX+1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   return x
+
+def test_add_ulong(x, y):
+   u"""
+   >>> test_add_ulong(ULONG_MAX, 0) == ULONG_MAX
+   True
+   >>> test_add_ulong(ULONG_MAX, 1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   cdef ULong r = x + y
+   return r
+
+# -------------------------------------------------------------------
+
+SLONGLONG_MAX = <SLongLong>((<ULongLong>-1)>>1)
+SLONGLONG_MIN = (-SLONGLONG_MAX-1)
+
+def test_slonglong(long long x):
+   u"""
+   >>> test_slonglong(SLONGLONG_MIN-1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   >>> test_slonglong(SLONGLONG_MIN) == SLONGLONG_MIN
+   True
+   >>> print(test_slonglong(-1))
+   -1
+   >>> print(test_slonglong(0))
+   0
+   >>> print(test_slonglong(1))
+   1
+   >>> test_slonglong(SLONGLONG_MAX) == SLONGLONG_MAX
+   True
+   >>> test_slonglong(SLONGLONG_MAX+1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   return x
+
+def test_add_slonglong(x, y):
+   u"""
+   >>> test_add_slonglong(SLONGLONG_MIN, -1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   >>> test_add_slonglong(SLONGLONG_MIN, 0) == SLONGLONG_MIN
+   True
+   >>> test_add_slonglong(SLONGLONG_MIN, 1) == SLONGLONG_MIN+1
+   True
+   >>> test_add_slonglong(SLONGLONG_MAX, -1) == SLONGLONG_MAX-1
+   True
+   >>> test_add_slonglong(SLONGLONG_MAX, 0) == SLONGLONG_MAX
+   True
+   >>> test_add_slonglong(SLONGLONG_MAX, 1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   cdef SLongLong r = x + y
+   return r
+
+ULONGLONG_MAX = <ULongLong>(<ULongLong>-1)
+
+def test_ulonglong(ULongLong x):
+   u"""
+   >>> test_ulonglong(-1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   >>> print(test_ulonglong(0))
+   0
+   >>> print(test_ulonglong(1))
+   1
+   >>> test_ulonglong(ULONGLONG_MAX) == ULONGLONG_MAX
+   True
+   >>> test_ulonglong(ULONGLONG_MAX+1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   return x
+
+def test_add_ulonglong(x, y):
+   u"""
+   >>> test_add_ulonglong(ULONGLONG_MAX, 0) == ULONGLONG_MAX
+   True
+   >>> test_add_ulonglong(ULONGLONG_MAX, 1) #doctest: +ELLIPSIS
+   Traceback (most recent call last):
+       ...
+   OverflowError: ...
+   """
+   cdef ULongLong r = x + y
+   return r
+
+# -------------------------------------------------------------------
+
+__doc__ = u"".join([
+      f.__doc__ for f in (
+         #
+         test_schar, test_add_schar,
+         test_uchar, test_add_uchar,
+         #
+         test_sshort, test_add_sshort,
+         test_ushort, test_add_ushort,
+         #
+         test_sint, test_add_sint,
+         test_uint, test_add_uint,
+         #
+         test_slong, test_add_slong,
+         test_ulong, test_add_ulong,
+         #
+         test_slonglong, test_add_slonglong,
+         test_ulonglong, test_add_ulonglong,
+         )
+    ])
+
+# -------------------------------------------------------------------
