Note that there are some explanatory texts on larger screens.

plurals
  1. POUnicodeDecodeError on import of a .pyd file
    text
    copied!<p>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. </p> <p>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?</p> <p>When I try this, I get the following trace:</p> <pre><code>Traceback (most recent call last): File "&lt; pyshell#25 &gt;", line 1, in &lt;module&gt; import spam UnicodeDecodeError: 'utf8' codec can't decode byte 0x89 in position 1: unexpected code byte </code></pre> <p>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.</p> <p>Thanks for reading this!</p> <p>EDIT:</p> <p>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 <a href="http://docs.python.org/py3k/extending/extending.html#a-simple-example" rel="nofollow noreferrer">here</a>.</p> <p><strong>Is this an usual problem to have??</strong></p> <p>Here is all the code that was used if it helps any: <code></p> <pre><code>#include &lt; Python.h &gt; static PyObject *spam_system(PyObject *self, PyObject *args) { const char *command; int sts; if (!PyArg_ParseTuple(args, "s", &amp;command)) return NULL; sts = system(command); return Py_BuildValue("i", sts); } static PyMethodDef SpamMethods[] = { {"system", spam_system, METH_VARARGS, "Execute a shell command."}, {NULL, NULL, 0, NULL} }; static struct PyModuleDef spammodule = { PyModuleDef_HEAD_INIT, "spam", NULL, -1, SpamMethods }; PyMODINIT_FUNC PyInit_spam(void) { return PyModule_Create(&amp;spammodule); } </code></pre> <p></code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload