From e8529e73c0e3920a8519e89920403feda1ac918d Mon Sep 17 00:00:00 2001 From: Stephen Hopper Date: Thu, 20 May 2021 07:27:47 -0500 Subject: [PATCH] Add codegen handling for infinity and NaN values. Make generic codegen method a bit cleaner. --- src/libpython_clj2/codegen.clj | 39 +++++++++++++++++----------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/libpython_clj2/codegen.clj b/src/libpython_clj2/codegen.clj index 4aa0e4b..7f0ea7b 100644 --- a/src/libpython_clj2/codegen.clj +++ b/src/libpython_clj2/codegen.clj @@ -64,29 +64,28 @@ (get-docs v) (:arglists v)))) - (defn- output-module-generic [^Writer writer clj-name k v vv] - (if (or (number? vv) - (string? vv) - (boolean? vv) - (nil? vv)) - (.write writer - (format " - -(def ^{:doc \"%s\"} %s %s)" - (get-docs v) - clj-name - (if (string? vv) - (format "\"%s\"" vv) - vv))) + (let [output-value (cond + ;;nils and boolean require no extra formatting + (or (nil? vv) (boolean? vv)) vv + ;;string values should be output surrounded by double quotes + (string? vv) (format "\"%s\"" vv) + ;;NaN is a number and should use the proper reader macro + (and (number? vv) (Double/isNaN vv)) "##NaN" + ;;Finite, non NAN numbers should be treated as literals + (and (number? vv) (Double/isFinite vv)) vv + ;;Positive and negative infinity are numbers and should use the proper reader macros + (and (number? vv) (not (neg? vv)) (Double/isInfinite vv)) "##Inf" + (and (number? vv) (neg? vv) (Double/isInfinite vv)) "##-Inf" + ;;Anything else should be derefed by name from Python + :else (format "(as-jvm/generic-pyobject (py-global-delay (py/get-attr @src-obj* \"%s\")))" k) + )] (.write writer - (format " - -(def ^{:doc \"%s\"} %s (as-jvm/generic-pyobject (py-global-delay (py/get-attr @src-obj* \"%s\"))))" - (get-docs v) - clj-name - k)))) + (format "\n\n(def ^{:doc \"%s\"} %s %s)" + (get-docs v) + clj-name + output-value)))) (def ^:no-doc default-exclude