diff -r 0a8509023c09 Doc/library/re.rst --- a/Doc/library/re.rst Sun Jun 05 01:38:29 2016 +0200 +++ b/Doc/library/re.rst Sat Jun 04 17:42:47 2016 -0700 @@ -926,6 +926,17 @@ The pattern string from which the RE object was compiled. +.. attribute:: regex.__code__ + + The compiled bytecode of the regex, as a string. + + .. impl-detail:: + + This attribute is not guaranteed to exist in all implementations of + Python. The bytecode returned is an implementation detail that may change + without warning between CPython versions. + + .. _match-objects: Match Objects diff -r 0a8509023c09 Lib/test/test_re.py --- a/Lib/test/test_re.py Sun Jun 05 01:38:29 2016 +0200 +++ b/Lib/test/test_re.py Sat Jun 04 17:42:47 2016 -0700 @@ -587,6 +587,12 @@ p.groupindex['other'] = 0 self.assertEqual(p.groupindex['other'], 2) + @cpython_only + def test_code(self): + pattern = re.compile('foo') + # contents are implementation-defined + self.assertIsInstance(pattern.__code__, str) + def test_special_escapes(self): self.assertEqual(re.search(r"\b(b.)\b", "abcd abc bcd bx").group(1), "bx") diff -r 0a8509023c09 Lib/test/test_turtle.py --- a/Lib/test/test_turtle.py Sun Jun 05 01:38:29 2016 +0200 +++ b/Lib/test/test_turtle.py Sat Jun 04 17:42:47 2016 -0700 @@ -396,5 +396,27 @@ self.assertTrue(tpen.isvisible()) +class TestScreen(unittest.TestCase): + + @support.requires_resource('gui') + def test_color_triple_to_hexstring(self): + screen = turtle.Screen() + self.assertEqual( + '#000000', screen._color_triple_to_hexstring((0, 0, 0))) + self.assertEqual( + '#000000', screen._color_triple_to_hexstring((0., 0., 0.))) + + with self.assertRaisesRegex(turtle.TurtleGraphicsError, + 'bad color arguments'): + screen._color_triple_to_hexstring((1,)) + + with self.assertRaisesRegex(turtle.TurtleGraphicsError, + 'bad color sequence'): + screen._color_triple_to_hexstring((300, 500, 400)) + + with self.assertRaises(TypeError): + screen._color_triple_to_hexstring((None, (), 'spam')) + + if __name__ == '__main__': unittest.main() diff -r 0a8509023c09 Lib/turtle.py --- a/Lib/turtle.py Sun Jun 05 01:38:29 2016 +0200 +++ b/Lib/turtle.py Sat Jun 04 17:42:47 2016 -0700 @@ -1156,12 +1156,22 @@ return color else: raise TurtleGraphicsError("bad color string: %s" % str(color)) + return self._color_triple_to_hexstring(color) + + def _color_triple_to_hexstring(self, color): + """Return color string corresponding to the hex triple given in color. + + The argument must be a tuple of three numbers in the range 0<=n<=colormode. + + """ try: r, g, b = color - except: + except ValueError: raise TurtleGraphicsError("bad color arguments: %s" % str(color)) if self._colormode == 1.0: r, g, b = [round(255.0*x) for x in (r, g, b)] + else: + r, g, b = [round(x) for x in (r, g, b)] if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)): raise TurtleGraphicsError("bad color sequence: %s" % str(color)) return "#%02x%02x%02x" % (r, g, b) @@ -2700,15 +2710,7 @@ """ if isinstance(args, str): return args - try: - r, g, b = args - except: - raise TurtleGraphicsError("bad color arguments: %s" % str(args)) - if self.screen._colormode == 1.0: - r, g, b = [round(255.0*x) for x in (r, g, b)] - if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)): - raise TurtleGraphicsError("bad color sequence: %s" % str(args)) - return "#%02x%02x%02x" % (r, g, b) + return self.screen._color_triple_to_hexstring(args) def clone(self): """Create and return a clone of the turtle. diff -r 0a8509023c09 Modules/_sre.c --- a/Modules/_sre.c Sun Jun 05 01:38:29 2016 +0200 +++ b/Modules/_sre.c Sat Jun 04 17:42:47 2016 -0700 @@ -1429,13 +1429,21 @@ PyDoc_STRVAR(pattern_doc, "Compiled regular expression objects"); -/* PatternObject's 'groupindex' method. */ +/* PatternObject's 'groupindex' attribute. */ static PyObject * pattern_groupindex(PatternObject *self) { return PyDictProxy_New(self->groupindex); } +/* PatternObject's '__code__' attribute. */ +static PyObject * +pattern_code(PatternObject *self) +{ + return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, self->code, + self->codesize); +} + static int _validate(PatternObject *self); /* Forward */ /*[clinic input] @@ -2671,6 +2679,8 @@ static PyGetSetDef pattern_getset[] = { {"groupindex", (getter)pattern_groupindex, (setter)NULL, "A dictionary mapping group names to group numbers."}, + {"__code__", (getter)pattern_code, (setter)NULL, + "The pattern's bytecode string."}, {NULL} /* Sentinel */ };