2

I have a Python program in which I am trying to use this rhyming dictionary to look up rhymes.

Part of the dictionary library setup works by a C program creating three gdbm .db files. The code that does this is publicly available here (from 'get the source' section), and the key part of the C gdbm builder compile.c looks like this:

#include <gdbm.h>

...<snip>...

int main(int argc, char *argv[]) {
  FILE *input;
  GDBM_FILE output;

...<snip>...

output = gdbm_open(argv[2], 0, GDBM_NEWDB, 0644, 0);

...<snip>...

gdbm_sync(output);

...<snip>...

words.db is created, and it can be successfully accessed from the bundled command line interface.

According to the library's example page, I should be able to use the python gdbm module to access the database.

However, I cannot. When I run the following:

import gdbm
words = gdbm.open('/usr/share/rhyme/words.db')

I get the following error:

    words = gdbm.open('/usr/share/rhyme/words.db')
gdbm.error: Bad magic number

Is there a file incompatibility problem? Do some gdbm files created in C not open with Python?

(This is with Python 2.7 on OSX 10.6.8. Python's gdbm was installed via MacPorts)

Bill Cheatham
  • 11,396
  • 17
  • 69
  • 104
  • Works fine for me: r["AY1-M"] = "ANTICRIME BEIM CHIME CLIMB CLIME CRIME DIME GRIME HAIM HEIM HIME I'M KIME LIME LYME MIME ONETIME PART-TIME PRIME RHYME RIME SEIM SIME SLIME SUBLIME SYME THYME TIME" I compiled the database using the standard "libgdbm-dev" package on Ubuntu (Precise), and read it with Python 2.7.3. – Lee Daniel Crocker Apr 17 '13 at 09:50
  • Ok thanks, this makes me think that it is an OSX-specific problem. Probably to do with the flavour of GDBM I have installed via MacPorts vs the OS's GDBM. – Bill Cheatham Apr 17 '13 at 10:36
  • Hey Bill. Were you able to remove the library? Were you able to compare the gdbm headers? – Man Vs Code Apr 18 '13 at 20:27

2 Answers2

1

How did you create the words.db file? It looks like the words.db is corrupted or in a format not supported by your specific version of the gdbm library on Mac OS X.

Man Vs Code
  • 1,058
  • 10
  • 14
  • The gdbm files are created by a C program on my platform; I have given snippets of the code used in my question, and the whole source file of this C program is also linked in the question. So the problem seems to be that the gdbm files created by C on my platform cannot be read by Python on my platform. – Bill Cheatham Apr 17 '13 at 20:57
  • As a quick test, write a python program to create an empty gdbm database. Compare the headers of both .db files--the one created by your C program and the one created by Python. Check if the first 2 or 4 bytes are the same? Maybe you installed two different libgdbm libraries? – Man Vs Code Apr 17 '13 at 21:07
  • I think I did install two different libraries - the C one from source and the Python via MacPorts. I will do as you suggested and comapare the headers. – Bill Cheatham Apr 17 '13 at 21:09
  • The error message means that the "magic number" used to identify the file as a GNU dbm file does not match what the libgdbm was expecting. These magic numbers are usually the first 2-4 bytes in a file. One library may be expecting something different. You could also do a "make uninstall" from the source code and uninstall the installed library. If you're lucky this will work. – Man Vs Code Apr 17 '13 at 21:12
  • If the libraries produced two different headers then make sure to come back and mention that. It could help someone else later. – Man Vs Code Apr 17 '13 at 21:33
  • 1
    Ok thanks, I now have this figured out. For some reason, as you suggest, the first couple of bytes of gdbm files created in C are different to those created in Python. I manually changed the C one to match the Python one with a Hex editor, and now it works! Only a one-time solution, but it does the trick for now. Thanks. – Bill Cheatham Apr 22 '13 at 15:54
1

Quoting the creator of Python (all kneel down and repeat we are not worthy! x3 :-)

Simple -- gdbm is sensitive to the byte order. Alas, I don't know what to do about it except converting it to a more portable format for transfer, or patching the gdbm source :-(

--Guido van Rossum (home page: http://www.python.org/~guido/)

Alvin K.
  • 4,329
  • 20
  • 25