Commits

Yosuke Onoue committed b6ae7ca

Python2.5 support

  • Participants
  • Parent commits 1054413

Comments (0)

Files changed (2)

File otypes/template.py

 
     #define Field(x, i) (((value *)(x)) [i])           /* Also an l-value. */
     '''
-    return ml_value_p.from_buffer(x)[i]
+    return force_cast(x, ml_value_p)[i]
 
 def store_field(block, offset, val):
     '''
     #define Tag_val(val) (((unsigned char *) (val)) [-sizeof(value)]) /* Also an l-value. */
     #endif
     '''
-    p = ctypes.POINTER(ctypes.c_ubyte).from_buffer(val)
+    p = force_cast(val, ctypes.POINTER(ctypes.c_ubyte))
     if sys.byteorder == 'big':
         return p[-1]
     else:
 
     #define Code_val(val) (((code_t *) (val)) [0])     /* Also an l-value. */
     '''
-    return ctypes.POINTER(ml_code_t).from_buffer(val).contents
+    return force_cast(val, ctypes.POINTER(ml_code_t)).contents
 
 def caml_string_length(v):
     '''
 
     #define Byte(x, i) (((char *) (x)) [i])            /* Also an l-value. */
     '''
-    return ctypes.POINTER(ctypes.c_char).from_buffer(v)[n].decode()
+    return force_cast(v, ctypes.POINTER(ctypes.c_char))[n].decode()
 
 def bp_val(v):
     '''
     #define Bp_val(v) ((char *) (v))
     '''
-    return ctypes.c_char_p.from_buffer(v)
+    return force_cast(v, ctypes.c_char_p)
 
 def string_val(x):
     '''
 
     #define Double_field(v,i) Double_val((value)((double *)(v) + (i)))
     '''
-    return ctypes.POINTER(ctypes.c_double).from_buffer(v)[i]
+    return force_cast(v, ctypes.POINTER(ctypes.c_double))[i]
 
 def store_double_field(v, i, d):
     '''
       Store_double_val((value)((double *) (v) + caml__temp_i), caml__temp_d); \\
     }while(0)
     '''
-    ctypes.POINTER(ctypes.c_double).from_buffer(v)[i] = d
+    force_cast(v, ctypes.POINTER(ctypes.c_double))[i] = d
 
 def data_custom_val(v):
     '''
 def caml_call(f):
     @wraps(f)
     def _caml_call(*args, **kw):
-        frame = caml__roots_block_p.from_buffer_copy(caml_local_roots())
+        frame = force_cast_copy(caml_local_roots, caml__roots_block_p)
         res = f(*args, **kw)
         if frame:
             caml_local_roots().contents = frame.contents
         else:
-            ctypes.c_long.from_buffer(caml_local_roots()).value = 0
+            force_cast(caml_local_roots(), ctypes.c_long).value = 0
         return res
     return _caml_call
 
 #define Val_false Val_int(0)
 val_false = val_int(0)
 
+def force_cast(obj, type):
+    if hasattr(type, 'from_buffer'):
+        return type.from_buffer(obj)
+    else:
+        return type.from_address(ctypes.addressof(obj))
+
+def force_cast_copy(obj, type):
+    if hasattr(type, 'from_buffer_copy'):
+        return type.from_buffer_copy(obj)
+    else:
+        raise NotImplementedError # TODO
+
 import sys
 
 tests_require = ['nose']
-major = sys.version_info.major
-minor = sys.version_info.minor
+major = sys.version_info[0]
+minor = sys.version_info[1]
 if major == 2 and minor < 7:
     tests_require.append('unittest2')
 elif major == 3 and minor < 2: