mirror of https://github.com/kivy/kivy.git
py3: introduce new kivy.compat module for checking if we are on python3, and define new "types", like string_types.
This commit is contained in:
parent
823ce7f7e2
commit
e894a28cdf
|
@ -63,6 +63,7 @@ __all__ = ('Animation', 'AnimationTransition')
|
|||
from math import sqrt, cos, sin, pi
|
||||
from kivy.event import EventDispatcher
|
||||
from kivy.clock import Clock
|
||||
from kivy.compat import string_types
|
||||
|
||||
|
||||
class Animation(EventDispatcher):
|
||||
|
@ -105,7 +106,7 @@ class Animation(EventDispatcher):
|
|||
self._duration = kw.get('d', kw.get('duration', 1.))
|
||||
self._transition = kw.get('t', kw.get('transition', 'linear'))
|
||||
self._step = kw.get('s', kw.get('step', 1. / 60.))
|
||||
if isinstance(self._transition, str):
|
||||
if isinstance(self._transition, string_types):
|
||||
self._transition = getattr(AnimationTransition, self._transition)
|
||||
for key in ('d', 't', 's', 'step', 'duration', 'transition'):
|
||||
kw.pop(key, None)
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
'''
|
||||
Compatibility module for Python 2.7 and > 3.3
|
||||
=============================================
|
||||
'''
|
||||
|
||||
import sys
|
||||
|
||||
#: True if the code is running on Python 3 interpreter.
|
||||
is_py3 = sys.version >= '3'
|
||||
|
||||
#: String types that can be used for checking if a object is a string
|
||||
string_types = str
|
||||
if not is_py3:
|
||||
string_types = basestring
|
|
@ -21,6 +21,7 @@ from kivy.clock import Clock
|
|||
from kivy.atlas import Atlas
|
||||
from kivy.resources import resource_find
|
||||
from kivy.utils import platform
|
||||
from kivy.compat import string_types
|
||||
import zipfile
|
||||
try:
|
||||
import io as SIO
|
||||
|
@ -420,10 +421,10 @@ class Image(EventDispatcher):
|
|||
self._size = self.texture.size
|
||||
elif isinstance(arg, ImageLoaderBase):
|
||||
self.image = arg
|
||||
elif isinstance(arg, str):
|
||||
elif isinstance(arg, string_types):
|
||||
self.filename = arg
|
||||
else:
|
||||
raise Exception('Unable to load image type %s' % str(type(arg)))
|
||||
raise Exception('Unable to load image type {0!r}'.format(arg))
|
||||
|
||||
# check if the image hase sequences for animation in it
|
||||
self._img_iterate()
|
||||
|
|
|
@ -174,6 +174,7 @@ __all__ = ('Property',
|
|||
include "graphics/config.pxi"
|
||||
|
||||
from weakref import ref
|
||||
from kivy.compat import string_types
|
||||
|
||||
cdef float g_dpi = -1
|
||||
cdef float g_density = -1
|
||||
|
@ -491,16 +492,10 @@ cdef class StringProperty(Property):
|
|||
cdef check(self, EventDispatcher obj, value):
|
||||
if Property.check(self, obj, value):
|
||||
return True
|
||||
IF PY3:
|
||||
if not isinstance(value, str):
|
||||
raise ValueError('%s.%s accept only str' % (
|
||||
obj.__class__.__name__,
|
||||
self.name))
|
||||
ELSE:
|
||||
if not isinstance(value, basestring):
|
||||
raise ValueError('%s.%s accept only str/unicode' % (
|
||||
obj.__class__.__name__,
|
||||
self.name))
|
||||
if not isinstance(value, string_types):
|
||||
raise ValueError('%s.%s accept only str' % (
|
||||
obj.__class__.__name__,
|
||||
self.name))
|
||||
|
||||
cdef inline void observable_list_dispatch(object self):
|
||||
cdef Property prop = self.prop
|
||||
|
|
|
@ -22,6 +22,7 @@ from kivy.properties import StringProperty
|
|||
|
||||
from kivy.factory import Factory
|
||||
from kivy.lang import Builder
|
||||
from kivy.compat import string_types
|
||||
|
||||
from nose.tools import raises
|
||||
|
||||
|
@ -490,7 +491,7 @@ class AdaptersTestCase(unittest.TestCase):
|
|||
|
||||
cat_data_item = list_adapter.get_data_item(0)
|
||||
self.assertEqual(cat_data_item, 'cat')
|
||||
self.assertTrue(isinstance(cat_data_item, str))
|
||||
self.assertTrue(isinstance(cat_data_item, string_types))
|
||||
|
||||
view = list_adapter.get_view(0)
|
||||
self.assertTrue(isinstance(view, ListItemButton))
|
||||
|
@ -670,7 +671,7 @@ class AdaptersTestCase(unittest.TestCase):
|
|||
|
||||
cat_data_item = list_adapter.get_data_item(0)
|
||||
self.assertEqual(cat_data_item, 'cat')
|
||||
self.assertTrue(isinstance(cat_data_item, str))
|
||||
self.assertTrue(isinstance(cat_data_item, string_types))
|
||||
|
||||
view = list_adapter.get_view(0)
|
||||
self.assertTrue(isinstance(view, ListItemButton))
|
||||
|
|
Loading…
Reference in New Issue