I've started to slowly dabble with the Python/C API and after much fiddling and finagling, I was able to build a spam.pyd file.

However, I must be missing something with this process and was hoping that someone could point me in the right direction. I thought that once spam.pyd was created, I could call it from Python via import spam. Is this true?

Any ideas as to what I am doing wrong? I am working with Python 3.1.2 on Windows XP. I compiled spam.c via the mingw32 compiler.

Thanks for reading this!

EDIT:

Well, it looks like the problem was that I had written the C code in an editor that saved the file with ANSI encoding. Strangely, if I retyped the code in Notepad, and saved the file with UTF8 encoding, I would get compile time errors complaining about invalid characters. When I used the built-in IDLE editor, everything worked fine. I was just following the example from the Python tutorial here.

Well then, you'd better shows us the [relevant parts of the] source of spam.c. Is this the first Python extension module that you've written in C? What textbook or tutorial are you following?
–
John MachinAug 21 '10 at 6:06

yes, it is the first time that i've written a Python extension module. I'm just following the tutorial on the Python website. I updated my question with more (hopefully) relevant information.
–
B RiveraAug 21 '10 at 19:50

1 Answer
1

You say: Well, it looks like the problem was that I had written the C code in an editor that saved the file with ANSI encoding.

This is exceedingly unlikely. There are no non-ASCII characters visible in your published C source. If there were any, you would have got an error message from the C compiler (except maybe if it was in a string constant; I've never tried that).

You say: Strangely, if I retyped the code in Notepad, and saved the file with UTF8 encoding, I would get compile time errors complaining about invalid characters.

Not strangely. Notepad prepends a UTF-8 BOM. This means your C compiler was being presented with a source file which started with 3 bytes of junk. Don't use Notepad. Use a proper text editor.

The indications are that the problem is much more likely to be in your Python input. The default source-file encoding in Python 3 is UTF-8. Your file contains "byte 0x89" which is not a valid UTF-8 lead byte and which the Windows cp125X encodings map to ‰ alias U+2030 PER MILLE SIGN -- either you have this in a string constant or you've typed that by mistake for a % (PER CENT SIGN). However it's difficult to guess how you got the traceback that you did. Getting into an interpreter (e.g. IDLE) and typing import spam should NOT give you that traceback.

+1 thanks for the explanation. i tried saving the code in a very old version of microsoft visual studio and got the same traceback (the file compiled just fine) so it's still a bit of a mystery to me. when moving over to a better editor, these problems did go away.
–
B RiveraAug 26 '10 at 0:24