ffi: style changes

Make to_str() accept None as well as ffi.NULL to return as a negative
value, and grab the version in a more compatible way.
This commit is contained in:
Carlos Martín Nieto
2014-04-14 16:41:30 +02:00
parent 5a20510f8a
commit d7d0eb37c3

View File

@@ -34,23 +34,29 @@ from os import path, getenv
from cffi import FFI
import sys
if sys.version_info.major < 3:
(major_version, _, _, _, _) = sys.version_info
if major_version < 3:
def to_str(s, encoding='utf-8', errors='strict'):
if s == ffi.NULL:
if s == ffi.NULL or s == None:
return ffi.NULL
encoding = encoding or 'utf-8'
if isinstance(s, unicode):
encoding = encoding or 'utf-8'
return s.encode(encoding, errors)
return s
else:
def to_str(s, encoding='utf-8', errors='strict'):
if s == ffi.NULL or s == None:
return ffi.NULL
if isinstance(s, bytes):
return s
else:
return bytes(s, encoding, errors)
if sys.version_info.major < 3:
return s.encode(encoding, errors)
if major_version < 3:
def is_string(s):
return isinstance(s, basestring)
else: