def works(self, expr, g=None, l=None): try: answer = eval(expr, g, l) result = rl_safe_eval(expr, g, l) except: print('expr=%r' % expr) annotateException('\nexpr=%r\n' % expr) self.assertEqual( answer, result, "rl_safe_eval(%r) = %r not expected %r" % (expr, result, answer))
def fails(self, expr, g=None, l=None, timeout=None, exception=BadCode): try: result = rl_safe_eval(expr, g, l, timeout=timeout) self.assertEqual( True, False, "rl_safe_eval(%r)=%r did not raise %s" % (expr, result, exception.__name__)) except exception: return except: self.assertEqual( True, False, "rl_safe_eval(%r) raised %s: %s instead of %s" % (expr, sys.exc_info()[0].__name__, str( sys.exc_info()[1]), exception.__name__))
def __call__(self, arg, default=None): '''try to map an arbitrary arg to a color instance ''' if isinstance(arg, Color): return arg if isinstance(arg, (tuple, list)): assert 3 <= len( arg) <= 4, 'Can only convert 3 and 4 sequences to color' assert 0 <= min(arg) and max(arg) <= 1 return len(arg) == 3 and Color(arg[0], arg[1], arg[2]) or CMYKColor( arg[0], arg[1], arg[2], arg[3]) elif isStr(arg): arg = asNative(arg) C = cssParse(arg) if C: return C if arg in self.extraColorsNS: return self.extraColorsNS[arg] C = getAllNamedColors() s = arg.lower() if s in C: return C[s] G = C.copy() G.update(self.extraColorsNS) if not self._G: C = globals() self._G = { s: C[s] for s in '''Blacker CMYKColor CMYKColorSep Color ColorType HexColor PCMYKColor PCMYKColorSep Whiter _chooseEnforceColorSpace _enforceCMYK _enforceError _enforceRGB _enforceSEP _enforceSEP_BLACK _enforceSEP_CMYK _namedColors _re_css asNative cmyk2rgb cmykDistance color2bw colorDistance cssParse describe fade fp_str getAllNamedColors hsl2rgb hue2rgb isStr linearlyInterpolatedColor literal_eval obj_R_G_B opaqueColor rgb2cmyk setColors toColor toColorOrNone''' .split() } G.update(self._G) try: return toColor(rl_safe_eval(arg, g=G, l={})) except: pass try: return HexColor(arg) except: if default is None: raise ValueError('Invalid color value %r' % arg) return default
def test_002(self): self.assertTrue(rl_safe_eval("GA=='ga'"))
def test_001(self): A = 3 self.assertTrue(rl_safe_eval("A==3"))