git clone –mirror

It seems there will be a new option for git-clone, ‘–mirror’. The command line

1
$ git clone --mirror $URL

is now a short-hand for

1
2
$ git clone --bare $URL
$ (cd $(basename $URL) && git remote add --mirror origin $URL)

Refer to http://kerneltrap.org/mailarchive/git/2008/8/2/2793244 about this.

This is extremely useful if you need to check the contents of different branches in different directories at the same time. Each directory will be a local git clone -l -s, and git push will use --mirror as well by default.

bug in python-opengl + mesa

Since like forever, I have never succeed in executing python-opengl demos on debian lenny. It always segfault during glutInitDisplayMode. After some debugging, here is why.

One should be able to call glGetError at any time. If there’s nothing wrong, it should just return something like GLNOERROR. Any raw (native) function call in python-opengl, no matter it belongs to GL, GLU or GLUT, will always call glGetError right after each call to check for error. I think this is wrong since functions like glutInitDisplayMode have NOTHING to do with glGetError. With mesa 7.0.3-5 in my system, the call to glGetError without glInit will cause segfault. I check the same thing on Ubuntu, which has an older version of mesa and python-opengl, and it does not happen.

python-opengl enabled error checking by default with OpenGL.ERROR_CHECKING, which is set to True in OpenGL/__init__.py. This code snippet can disable it:

1
2
3
import OpenGL
OpenGL.ERROR_CHECKING = False
# import other stuffs such as OpenGL.GL, etc.

or you can just modify the __init__.py to disable it by default.

Given the current status of python-opengl and low level x protocol support in python, I think the best language to do 3D in FOSS world will still be pure simple C.

oprofile

First you have to enable oprofile in your kernel, i.e. you must have

1
2
CONFIG_PROFILING=y
CONFIG_OPROFILE=y

in your kernel config. Then you need userspace utilities to use it. I used oprofile 0.9.4. First of all you need to setup it with something like

1
2
3
opcontrol --init
opcontrol --no-vmlinux
opcontrol --callgraph=5

After that, here is the script I used to do profiling against a program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
john@buddha:~$ cat bin/profile
#!/bin/sh

which sudo && SUDO=sudo || SUDO=
which sudo && SUDO=sudo || SUDO=

${SUDO} opcontrol --shutdown
${SUDO} rm -rf /var/lib/oprofile/*
${SUDO} opcontrol --status
${SUDO} opcontrol --start-daemon
${SUDO} opcontrol --start
$@
${SUDO} opcontrol --stop
${SUDO} opcontrol --dump
${SUDO} opcontrol --shutdown

Use profile <the name of your executable> to get the profile. Use opreport to read the result. opreport -l might be more meaningful. You can use opreport <the name of your executable> to only get the result related to your program, but personally I prefer to check the whole system.

generate object methods at runtime

I have been working on a dialer button class since yesterday. It makes sense to use the command design pattern here, and I want to separate the commands and the buttons so I can change the functionality of every button at runtime.

So we are talking about something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
class DialerButtons:
def __init__(self):
self.command_table = [
self.numpad_1, self.numpad_2, self.numpad_3,
self.numpad_4, self.numpad_5, self.numpad_6,
self.numpad_7, self.numpad_8, self.numpad_9,
self.cancel, self.numpad_0, self.dial]

def numpad_0(self):
self.text.append('0')

def execute(self, n):
self.command_table[n]()

and you can use this class like this:

1
2
3
dialer = DialerButtons()
dialer.execute(0)
dialer.execute(8)
Now obviously define numpad_0 to numpad_9 is a boring task. What happens if you need to define numpad 0 to 99? So, I came out with this code piece:
1
2
3
4
5
6
    @classmethod
def _numpad_commands_factory(cls):
for n in xrange(0, 10):
setattr(cls, 'numpad_%d' % n, lambda self: self.text.append(str(n)))

DialerButtons._numpad_commands_factory()
This way you initialize DialerButtons AFTER you start the program and make methods numpad_0 to 9 on the fly. At least that’s what I was trying to do. However, it didn’t come out as I expected. Every numpad method will just add ‘9’ to self.text, instead of the respective ‘0’ to ‘9’. Why?

The reason is that the context of numpad_0, for example, is actually “f(self): self.text.append(str(n)))” instead of “f(self): self.text.append(‘0’)”. so, what it does here is that it refers to the variable n inside _numpad_commands_factory, and the value of n is 9 after you executed it.

The correct code piece is:

1
2
3
4
5
6
@classmethod
def _numpad_commands_factory(cls):
def f(chr):
return lambda self: self.text.append(chr)
for n in xrange(0, 10):
setattr(cls, 'numpad_%d' % n, f(str(n)))
This way we can evaluate the value of str(n) first, then generate the appropriate function and assign it to numpad_n.

python-efl on ubuntu gutsy

hey if you have a extremely slow laptop (Pentium III) like me yet you still want to use python-efl on it, here is how.

first, compiling the whole e17 from scratch is not an option. it will take forever. there’s a ubuntu package repository for e17:

1
2
3
4
5
$ cat /etc/apt/sources.list.d/e17.list
deb http://e17.dunnewind.net/ubuntu gutsy e17
deb-src http://e17.dunnewind.net/ubuntu gutsy e17
$ sudo apt-get update
$ sudo apt-get install efl-dev libecore-imf-evas-dev

this can save a lot of time.

the other problem is that the latest cython release (0.9.8) does NOT work with python-efl. you have to install 0.9.6.14 instead. don’t forget to install python-pyrex as well.

now you’re all set, get python-efl.

1
2
cvs -d :pserver:anonymous@anoncvs.enlightenment.org:/var/cvs/e login
cvs -z3 -d :pserver:anonymous@anoncvs.enlightenment.org:/var/cvs/e co e17

it’s under e17/proto/python-efl. another funny gotcha is that the binary packages I installed were built on 20080309. so the latest python-efl will not build successfully.

1
cvs -z3 update -dP -D 20080309

fixs this.

the default build-all.sh under python-efl directory builds evas ecore edje emotion e_dbus epsilon. that’s too many. for me I just need evas ecore edje e_dbus.

git first, git-svn later.

the whole story is that I started a project locally and I used git as my SCM. now I’m going to put it into a svn repository with full history but I still want to use git.

normally you should make this decision at the beginning. that means you

1
git-svn clone http://svn.somewhere.com/myproject

first, then you use git as usual, do git-svn rebase and git-svn dcommit. you have to do this because git-svn must know where to start, namely you should have at least one git-svn-id in your git log to start with.

here is how I add svn support into an existing git repository. basically it’s easy, you just

1
2
git-svn init http://svn.somewhere.com/myproject
git-svn fetch

now git branch -r should tell you there is a branch called git-svn. you git rebase git-svn your current master. if it succeeded then you’re all set.

however, in order to do this, there must be a point back in time that these two branches are the same. if it’s not the case, you’re in trouble. you have to use git-svn set-tree to force a svn commit to be your starting point. in my case, the svn repository started out empty, so I forced the first commit in my git. after that git rebase succeeded like I expected.

fonts disappeared

I’m using debian testing (lenny) and after the latest update my xserver no longer display fonts. This is not totally true because I can still see the fonts on gdm, but after logging in, no fonts whatsoever. It turns out the i810 Xorg driver is messed up and I have to set NoAccel to true to avoid it.

1
2
3
4
5
6
7
8
Section "Device"
Identifier "Intel Corporation 82Q963/Q965 Integrated Graphics Controller"
Driver "i810"
BusID "PCI:0:2:0"
Option "NoAccel" "true"
# Option "AccelMethod" "EXA"
Option "XAANoOffscreenPixmaps"
EndSection

BTW I can use compiz without NoAccel before if I delete the device id from the blacklist in compiz start up script.

我果然不懂 C 啊。

from gcc-4.2.info:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
5.34 An Inline Function is As Fast As a Macro

<..snipped..>

If you specify both `inline' and `extern' in the function definition,
then the definition is used only for inlining. In no case is the
function compiled on its own, not even if you refer to its address
explicitly. Such an address becomes an external reference, as if you
had only declared the function, and had not defined it.

This combination of `inline' and `extern' has almost the effect of a
macro. The way to use it is to put a function definition in a header
file with these keywords, and put another copy of the definition
(lacking `inline' and `extern') in a library file. The definition in
the header file will cause most calls to the function to be inlined.
If any uses of the function remain, they will refer to the single copy
in the library.

Since GCC 4.3 will implement ISO C99 semantics for inline functions,
it is simplest to use `static inline' only to guarantee compatibility.
(The existing semantics will remain available when `-std=gnu89' is
specified, but eventually the default will be `-std=gnu99'; that will
implement the C99 semantics, though it does not do so in versions of
GCC before 4.3\. After the default changes, the existing semantics will
still be available via the `-fgnu89-inline' option or the `gnu_inline'
function attribute.)

GCC does not inline any functions when not optimizing unless you
specify the `always_inline' attribute for the function, like this:

/* Prototype. */
inline void foo (const char) __attribute__((always_inline));

So, consider the following program:

gcctest.c:

1
2
3
4
5
6
7
#include "inline.h"

int main()
{
puts(externinline());
return 0;
}

inline.h:

1
2
3
4
__inline__ extern char *externinline()
{
return "inline";
}

lib.c:

1
2
3
4
char *externinline()
{
return "extern";
}

The behavior of the executables will be different with or without the -O option of gcc. Compile it without -O, the program will print “extern”. Compile it with -O, the program will print “intern”.