OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Using PyModule_AddIntConstant() in an extension

  • Thread starter Thread starter Xantium
  • Start date Start date
X

Xantium

Guest
I have seen Adding symbolic constants with hex values to Python extension module and I am trying to reproduce this effect:

Code:
#include <Python.h>
#include <Windows.h>

static PyObject * sys_shutdown(PyObject *self, PyObject *args) {
    int val;

    if (!PyArg_ParseTuple(args, "i", &val))
        val = SHTDN_REASON_MINOR_OTHER; // Provide failsafe

    ExitWindowsEx(EWX_POWEROFF, val); // Shutdown
    return Py_BuildValue("");
}

static PyObject * sys_restart(PyObject *self, PyObject *args) {
    int val;

    if (!PyArg_ParseTuple(args, "i", &val))
        val = SHTDN_REASON_MINOR_OTHER; // Provide failsafe
    ExitWindowsEx(EWX_REBOOT, val); // Restart
    return Py_BuildValue("");
}

static PyObject * sys_log_out(PyObject *self, PyObject *args) {
    int val;

    if (!PyArg_ParseTuple(args, "i", &val))
        val = SHTDN_REASON_MINOR_OTHER; // Provide failsafe

    ExitWindowsEx(EWX_LOGOFF, val); // Log out
    return Py_BuildValue("");
}

static PyMethodDef localMethods[] = {
    {"shutdown", (PyCFunction)sys_shutdown, METH_VARARGS, "..."},
    {"restart", (PyCFunction)sys_restart, METH_VARARGS, "..."},
    {"log_out", (PyCFunction)sys_log_out, METH_VARARGS, "..."},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef func = {
    PyModuleDef_HEAD_INIT,
    "utilities",
    "...",
    -1,
    localMethods,
};

PyMODINIT_FUNC PyInit_utilities(void) {
    PyObject *value;

    value = PyModule_New(&func);

    PyModule_AddIntConstant(value, "DEFINED", AF_INET);

    return PyModule_Create(&func);
}

Setup Script:

Code:
from distutils.core import setup, Extension

module = Extension(
    "utilities", 
    sources = ["main.c"],
        libraries = ["user32"]
)

setup (
    name = "Utilities",
    version = "1.0",
    ext_modules = [module])

Everything builds as expected, however I cannot use DEFINED in my extension:

Code:
import utilities
for i in utilities.__dict__: print(i)
utilities.DEFINED # AttributeError: module 'utilities' has no attribute 'DEFINED'

Returns:

Code:
__name__
__doc__
__package__
__loader__
__spec__
shutdown
restart
log_out
__file__

I thought of returning value like so:

Code:
return PyModule_Create(&value);

But that returns:

LINK : fatal error LNK1104: cannot open file 'build\lib.win32-3.6\WinUtils.cp36-win32.pyd' error: command 'C:\Program Files (x86)\Microsoft Visual Studio\2017\WDExpress\VC\Tools\MSVC\14.14.26428\bin\HostX86\x86\link.exe' failed with exit status 1104

How can I add the DEFINED value to my extension (so that I can run utilities.DEFINED)?

Edit:

As mentioned in the answer below closing everything and trying again builds the extension successfully, however using return PyModule_Create(&value); still crashes.
<p>I have seen <a href="https://stackoverflow.com/questions...ts-with-hex-values-to-python-extension-module">Adding symbolic constants with hex values to Python extension module</a> and I am trying to reproduce this effect:</p>

<pre><code>#include <Python.h>
#include <Windows.h>

static PyObject * sys_shutdown(PyObject *self, PyObject *args) {
int val;

if (!PyArg_ParseTuple(args, "i", &val))
val = SHTDN_REASON_MINOR_OTHER; // Provide failsafe

ExitWindowsEx(EWX_POWEROFF, val); // Shutdown
return Py_BuildValue("");
}

static PyObject * sys_restart(PyObject *self, PyObject *args) {
int val;

if (!PyArg_ParseTuple(args, "i", &val))
val = SHTDN_REASON_MINOR_OTHER; // Provide failsafe
ExitWindowsEx(EWX_REBOOT, val); // Restart
return Py_BuildValue("");
}

static PyObject * sys_log_out(PyObject *self, PyObject *args) {
int val;

if (!PyArg_ParseTuple(args, "i", &val))
val = SHTDN_REASON_MINOR_OTHER; // Provide failsafe

ExitWindowsEx(EWX_LOGOFF, val); // Log out
return Py_BuildValue("");
}

static PyMethodDef localMethods[] = {
{"shutdown", (PyCFunction)sys_shutdown, METH_VARARGS, "..."},
{"restart", (PyCFunction)sys_restart, METH_VARARGS, "..."},
{"log_out", (PyCFunction)sys_log_out, METH_VARARGS, "..."},
{NULL, NULL, 0, NULL}
};

static struct PyModuleDef func = {
PyModuleDef_HEAD_INIT,
"utilities",
"...",
-1,
localMethods,
};

PyMODINIT_FUNC PyInit_utilities(void) {
PyObject *value;

value = PyModule_New(&func);

PyModule_AddIntConstant(value, "DEFINED", AF_INET);

return PyModule_Create(&func);
}
</code></pre>

<p>Setup Script:</p>

<pre><code>from distutils.core import setup, Extension

module = Extension(
"utilities",
sources = ["main.c"],
libraries = ["user32"]
)

setup (
name = "Utilities",
version = "1.0",
ext_modules = [module])
</code></pre>

<p>Everything builds as expected, however I cannot use <code>DEFINED</code> in my extension:</p>

<pre><code>import utilities
for i in utilities.__dict__: print(i)
utilities.DEFINED # AttributeError: module 'utilities' has no attribute 'DEFINED'
</code></pre>

<p>Returns:</p>

<pre><code>__name__
__doc__
__package__
__loader__
__spec__
shutdown
restart
log_out
__file__
</code></pre>

<p>I thought of returning <code>value</code> like so:</p>

<pre><code>return PyModule_Create(&value);
</code></pre>

<p>But that returns:</p>

<blockquote>
<p>LINK : fatal error LNK1104: cannot open file 'build\lib.win32-3.6\WinUtils.cp36-win32.pyd'
error: command 'C:\Program Files (x86)\Microsoft Visual Studio\2017\WDExpress\VC\Tools\MSVC\14.14.26428\bin\HostX86\x86\link.exe' failed with exit status 1104</p>
</blockquote>

<p>How can I add the <code>DEFINED</code> value to my extension (so that I can run <code>utilities.DEFINED</code>)?</p>

<p><strong>Edit:</strong></p>

<p>As mentioned in the answer below closing everything and trying again builds the extension successfully, however using <code>return PyModule_Create(&value);</code> still crashes.</p>
 
Top