Category Archives: quick tips

Locale-sensitive IO encoding in GHC 6.12

One thing to watch out for in the latest versions of GHC (6.12) is the new locale-sensitive text IO. For instance, when using EasyFilter to render Pandoc documents in WordPress, you must make sure you set the LANG environment variable to en_US.UTF-8 (e.g. in /etc/apache2/envvars), or it will typically default to POSIX and thus cause Pandoc to crash whenever it reads a non-ASCII character:

pandoc: <stdin>: hGetContents: invalid argument (Invalid or incomplete multibyte or wide character)

or writes one:

pandoc: <stdout>: commitAndReleaseBuffer: invalid argument (Invalid or incomplete multibyte or wide character)

256-color xterm

I somehow managed to never discover this until today. I was chatting with Ryan about myriad topics from spatial programming abstractions to distributed system consistency, but by far the bit of information that would most immediately and dramatically change my life forever was xterm-256color. Simply export TERM=xterm-256color, and bam—you now have a rainbow of colors in Vim and Emacs. It should work right out of the box these days—it works for me in GNOME Terminal and Putty. Fantastic.

Bitten by Python scoping

Yet again, I wasted too many minutes staring at and debugging my Python code due to the language’s funky variable scoping:

def relevant(xs, y):
  "Return elements in xs that are relevant to y."
  pairs = ((x, relevance(x,y)) for x in xs)
  return [(x,y) for x,y in pairs if y > 0]

In this case, the y in the list comprehension modifies the binding used by the generator expression.

Re-enabling desktop effects in Windows 7

Every once in a while, Windows 7’s Desktop Window Manager (DWM) spontaneously goes funky on me and loses its effects (transparency, blurring, shadows).

When this happens, you can get your effects back by going to Services and restarting the “Desktop Window Manager Session Manager” service, or by going to an Administrative command prompt and running:

net stop uxsms
net start uxsms

IE and Chrome

IE7 (2006) introduced Protected Mode, which runs browser tabs in separate, sandboxed processes to isolate tabs from each other and from the OS, communicating with central Broker processes that remain privileged and provide system services. Chrome’s architecture was designed similarly.

IE7’s Protected Mode uses Vista’s Mandatory Integrity Control, which prevents processes from modifying any files, allowing them to write only to locations marked as low, such as Temporary Internet Files. Files touched by processes are marked with their integrity level. Integrity Levels correspond to Internet Zones. Chrome on Vista also leverages MIC.

IE8 introduced InPrivate, among a bevy of other features. InPrivate is similar to Incognito browsing in Chrome.

Application-based packet filtering on Linux

iptables can’t filter on process ID or any other “direct” application identifier, which means you can’t say things like, “Allow only Firefox to send/receive any packets.” However, it can filter on user/group ID, allowing you to do user-based packet filtering, so that you can at least restrict applications if you run them as a certain uid/gid. The owner module (xt_owner) matches the owner of the socket (man iptables for more details).

# iptables -m owner --help
iptables v1.4.4
[...]
owner match options:
[!] --uid-owner userid[-userid]      Match local UID
[!] --gid-owner groupid[-groupid]    Match local GID
[!] --socket-exists                  Match if socket exists

Of course, this all applies only to local sockets; if this system is serving as a router for other hosts, then you don’t have the uid/gid information for their sockets (if their OS even has those notions).

When you release shift too early

$ diff /tmp/list.txt ~/Desktop/list.txt
$ rm !!;1
rm: cannot remove `diff': No such file or directory
bash: 1: command not found

The semi-colon should’ve been a colon, which selects just word 1 from the previous line. Instead, I deleted everything. Of course that’s what I intended to do, you piece of crap.

I usually use trash-cli and don’t bother deleting most files in /tmp. duplicity helped me get my files back, thankfully.

While on this: freedesktop.org’s trash specification leaves a few things to be desired:

  • Consistent support for trashing on mounts besides the one containing your home directory.
  • Automatic erasing of old files on space depletion.

C trigraphs. WTF.

I added a “loud” but seemingly innocuous print statement to debug a C++ program:

cout << "WAITING!?!?!?!??!" << endl;

On compilation, I got:

$ g++ -Wall -c -o main.o main.cc
In file included from main.lzz:12,
                 from main.cc:4:
/opt/armed/include/commons/st/st.h:229:35: 
warning: trigraph ??! ignored, use -trigraphs to enable

Turns out I used just the right combo of ?? and ! in my program. I had run into C trigraphs (gcc documentation).

Python default parameters

I was recently bitten by this: “Default parameter values are evaluated when the function definition is executed.” Demo:

def mklist():
  print 'making list'
  return []

def f(x=[]):
  x.append(3)
  print x

print 'start'
f()
f()

The output:

making list
start
[3]
[3, 3]

Annoyingly, the above page from the language reference acknowledges that “This is generally not what was intended,” without justifying the status quo.