From 3dbe05302ec29cc03a363117fd96733eb84ba904 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Sun, 5 Nov 2017 11:12:38 -0800 Subject: [PATCH] Make hy-repr support regex match objects --- hy/contrib/hy_repr.hy | 9 +++++++++ tests/native_tests/contrib/hy_repr.hy | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/hy/contrib/hy_repr.hy b/hy/contrib/hy_repr.hy index 8d67522..c414ffe 100644 --- a/hy/contrib/hy_repr.hy +++ b/hy/contrib/hy_repr.hy @@ -4,6 +4,7 @@ (import [math [isnan]] + re [hy._compat [PY3 str-type bytes-type long-type]] [hy.models [HyObject HyExpression HySymbol HyKeyword HyInteger HyFloat HyComplex HyList HyDict HySet HyString HyBytes]]) @@ -101,6 +102,14 @@ (hy-repr-register fraction (fn [x] (.format "{}/{}" (hy-repr x.numerator) (hy-repr x.denominator)))) +(setv matchobject-type (type (re.match "" ""))) +(hy-repr-register matchobject-type (fn [x] + (.format "<{}.{} object; :span {} :match {}>" + matchobject-type.__module__ + matchobject-type.__name__ + (hy-repr (.span x)) + (hy-repr (.group x 0))))) + (for [[types fmt] (partition [ list "[...]" [set HySet] "#{...}" diff --git a/tests/native_tests/contrib/hy_repr.hy b/tests/native_tests/contrib/hy_repr.hy index 076c16b..a295cec 100644 --- a/tests/native_tests/contrib/hy_repr.hy +++ b/tests/native_tests/contrib/hy_repr.hy @@ -113,6 +113,15 @@ (+ "{" (.join " " p) "}") [p (permutations ["1 2" "3 [4 {...}]" "6 7"])])))) +(defn test-matchobject [] + (import re) + (setv mo (re.search "b+" "aaaabbbccc")) + (assert (= (hy-repr mo) + (.format + #[[<{}.SRE_Match object; :span {} :match "bbb">]] + (. (type mo) __module__) + (if PY3 "(, 4 7)" "(, (int 4) (int 7))"))))) + (defn test-hy-repr-custom [] (defclass C [object])