From d7d0eb37c35607d217819d5b2ae5783a875cb023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 14 Apr 2014 16:41:30 +0200 Subject: [PATCH] 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. --- pygit2/ffi.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/pygit2/ffi.py b/pygit2/ffi.py index 0f225c8..468b9bc 100644 --- a/pygit2/ffi.py +++ b/pygit2/ffi.py @@ -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: