tags:

views:

98

answers:

1

I'm attempting to use clojure.contrib.io's (1.2) append-spit to append to a file (go figure).

If I create a text file on my desktop, as a test, and attempt to append to it in a fresh repl, this is what I get:

user> (append-spit "/Users/ihodes/Desktop/test.txt" "frank")
Backtrace:
  0: clojure.contrib.io$assert_not_appending.invoke(io.clj:115)
  1: clojure.contrib.io$outputstream__GT_writer.invoke(io.clj:266)
  2: clojure.contrib.io$eval1604$fn__1616$G__1593__1621.invoke(io.clj:121)
  3: clojure.contrib.io$fn__1660.invoke(io.clj:185)
  4: clojure.contrib.io$eval1604$fn__1616$G__1593__1621.invoke(io.clj:121)
  5: clojure.contrib.io$append_writer.invoke(io.clj:294)
  6: clojure.contrib.io$append_spit.invoke(io.clj:342)
  7: user$eval1974.invoke(NO_SOURCE_FILE:1)
  8: clojure.lang.Compiler.eval(Compiler.java:5424)
  9: clojure.lang.Compiler.eval(Compiler.java:5391)
 10: clojure.core$eval.invoke(core.clj:2382)
 11: swank.commands.basic$eval_region.invoke(basic.clj:47)
 12: swank.commands.basic$eval_region.invoke(basic.clj:37)
 13: swank.commands.basic$eval807$listener_eval__808.invoke(basic.clj:71)
 14: clojure.lang.Var.invoke(Var.java:365)
 15: user$eval1972.invoke(NO_SOURCE_FILE)
 16: clojure.lang.Compiler.eval(Compiler.java:5424)
 17: clojure.lang.Compiler.eval(Compiler.java:5391)
 18: clojure.core$eval.invoke(core.clj:2382)
 19: swank.core$eval_in_emacs_package.invoke(core.clj:94)
 20: swank.core$eval_for_emacs.invoke(core.clj:241)
 21: clojure.lang.Var.invoke(Var.java:373)
 22: clojure.lang.AFn.applyToHelper(AFn.java:169)
 23: clojure.lang.Var.applyTo(Var.java:482)
 24: clojure.core$apply.invoke(core.clj:540)
 25: swank.core$eval_from_control.invoke(core.clj:101)
 26: swank.core$eval_loop.invoke(core.clj:106)
 27: swank.core$spawn_repl_thread$fn__489$fn__490.invoke(core.clj:311)
 28: clojure.lang.AFn.applyToHelper(AFn.java:159)
 29: clojure.lang.AFn.applyTo(AFn.java:151)
 30: clojure.core$apply.invoke(core.clj:540)
 31: swank.core$spawn_repl_thread$fn__489.doInvoke(core.clj:308)
 32: clojure.lang.RestFn.invoke(RestFn.java:398)
 33: clojure.lang.AFn.run(AFn.java:24)
 34: java.lang.Thread.run(Thread.java:637)

Which clearly isn't what I wanted.

I was wondering if anyone else had these problems, or if I'm doing something incorrectly? The file I'm appending to it not open (at least by me). I'm at a loss.

Thanks so much!

+6  A: 

I notice that the relevant functions are marked as deprecated in 1.2, but I'm also under the impression that, as written, they've got some bugs in need of ironing out.

First, a non-deprecated way to do what you were trying to do (which works fine for me):

(require '[clojure.java.io :as io])

(with-open [w (io/writer (io/file "/path/to/file")
                         :append true)]
  (spit w "Foo foo foo.\n"))

(Skipping io/file and simply passing the string to io/writer would work too -- I prefer to use the wrapper partly as a matter of personal taste and partly so that c.j.io doesn't try to treat the string as an URL (only to back out via an exception and go for a file in this case), which is its first choice of interpretation.)

As for why I think clojure.contrib.io might be suffering from a bug:

(require '[clojure.contrib.io :as cio])

(with-bindings {#'cio/assert-not-appending (constantly true)}
  (cio/append-spit "/home/windfall/scratch/SO/clj/append-test.txt" "Quux quux quux?\n"))

This does not complain, but neither does it append to the file -- the current contents gets replaced instead. I'm not yet sure what exactly the problem is, but switching to clojure.java.io should avoid it. (Clearly this needs further investigation -- deprecated code still shouldn't be buggy -- I'll try to figure it out.)

Michał Marczyk
I didn't know about the :append option. Cool I can use this in sexpbot's log plugin. Needs an overhaul anyway.
Rayne
Yea, this is a known bug in clojure-contrib 1.2: http://www.assembla.com/spaces/clojure-contrib/tickets/90 . A fix has been proposed. The best fix though, is to use clojure.java.io, since clojure.contrib.io is deprecated and will disappear with 1.3.
bendin
@bendin: Ah, good to know, thanks. Good job triaging and fixing this! @Rayne: Cool. :-)
Michał Marczyk
Great-thanks! I'll use your alternative—was planning on using java—didn't now about the :append option, so that's quite nice :)
Isaac Hodes