Skip to content

Commit 17b6ca6

Browse files
committed
initial attempt at python3 compatibility fix
1 parent 9ccd7cd commit 17b6ca6

File tree

3 files changed

+54
-18
lines changed

3 files changed

+54
-18
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# code editor
2+
.vscode/
3+
14
# Object files
25
*.o
36
*.ko
@@ -41,4 +44,4 @@ install-Linux
4144

4245
# Other cruft
4346
.DS_Store
44-
*~
47+
*~

Makefile.OSX

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66

77
# Configure the include path for Pure Data. This is typically a path buried
88
# within the OS X application folder containing the m_pd.h header file.
9-
PD_INCLUDE_PATH=/Applications/local/Pd-extended-0.43.4-i386.app/Contents/Resources/include
9+
PD_INCLUDE_PATH=/Applications/Studio/Pd-0.51-4.app/Contents/Resources/src
1010

1111
# Configure to compile and link against the default Python system. Note that OS
1212
# X provides a Python interpreter, but alternative interpreters and virtualenv
1313
# environments could also be configured here.
1414

15-
PYTHON_CFLAGS := `python-config --cflags`
16-
PYTHON_LDFLAGS := `python-config --ldflags`
15+
PYTHON_CFLAGS := `python3-config --cflags`
16+
PYTHON_LDFLAGS := `python3-config --ldflags`
17+
PYTHON_LIB=-lpython3.9
1718

1819
# ---- The following settings do not usually need to be changed ----
1920

2021
# Specify the extension to use for the loadable module.
21-
EXTERNALS_EXT = pd_Darwin
22+
EXTERNALS_EXT = pd_darwin
2223

2324
# Specify a folder to hold the compiled binary Pd loadable modules.
2425
EXTERNALS_DIR = install-OSX
@@ -35,8 +36,8 @@ MODULE_CFLAGS = -bundle -undefined suppress -flat_namespace -I$(PD_INCLUDE_PATH)
3536

3637
# Build the loadable module and remove the extraneous symbols folder.
3738
$(EXTERNALS_DIR)/python.$(EXTERNALS_EXT): src/pdpython.c
38-
$(CC) $(MODULE_CFLAGS) $^ -o $@ $(PYTHON_CFLAGS) $(PYTHON_LDFLAGS)
39-
-rm -r $(EXTERNALS_DIR)/python.pd_Darwin.dSYM
39+
$(CC) $(MODULE_CFLAGS) $^ -o $@ $(PYTHON_CFLAGS) $(PYTHON_LDFLAGS) $(PYTHON_LIB)
40+
-rm -r $(EXTERNALS_DIR)/python.pd_darwin.dSYM
4041

4142
# Copy over the help patch.
4243
$(EXTERNALS_DIR)/python-help.pd: src/python-help.pd

src/pdpython.c

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static PyObject *t_atom_to_PyObject( t_atom *atom )
5353

5454
case A_SYMBOL:
5555
// symbols are returned as strings
56-
return PyString_FromString( atom->a_w.w_symbol->s_name );
56+
return PyUnicode_FromString( atom->a_w.w_symbol->s_name );
5757

5858
case A_NULL:
5959
Py_RETURN_NONE;
@@ -96,8 +96,7 @@ static void PyObject_to_atom( PyObject *value, t_atom *atom )
9696
else if (value == Py_False) SETFLOAT( atom, 0.0 );
9797
else if ( PyFloat_Check(value)) SETFLOAT( atom, (float) PyFloat_AsDouble( value ));
9898
else if ( PyLong_Check(value)) SETFLOAT( atom, (float) PyLong_AsLong( value ));
99-
else if ( PyInt_Check(value)) SETFLOAT( atom, (float) PyLong_AsLong( value ));
100-
else if ( PyString_Check(value)) SETSYMBOL( atom, gensym( PyString_AsString(value) ));
99+
else if ( PyUnicode_Check(value)) SETSYMBOL( atom, gensym( PyUnicode_AsUTF8(value) ));
101100
else SETSYMBOL( atom, gensym("error"));
102101
}
103102
/****************************************************************/
@@ -132,8 +131,7 @@ static void emit_outlet_message( PyObject *value, t_outlet *x_outlet )
132131
// scalar numbers of various types come out as float
133132
else if ( PyFloat_Check(value)) outlet_float( x_outlet, (float) PyFloat_AsDouble( value ));
134133
else if ( PyLong_Check(value)) outlet_float( x_outlet, (float) PyLong_AsLong( value ));
135-
else if ( PyInt_Check(value)) outlet_float( x_outlet, (float) PyLong_AsLong( value ));
136-
else if ( PyString_Check(value)) outlet_symbol( x_outlet, gensym( PyString_AsString(value) ));
134+
else if ( PyUnicode_Check(value)) outlet_symbol( x_outlet, gensym( PyUnicode_AsUTF8(value) ));
137135

138136
else if ( PyList_Check(value) ) {
139137
// Create an atom array representing a 1D Python list.
@@ -251,7 +249,7 @@ static void *pdpython_new(t_symbol *selector, int argcount, t_atom *argvec)
251249
// present. This will help the module import to find Python modules
252250
// located in the same folder as the patch.
253251
t_symbol *canvas_path = canvas_getcurrentdir();
254-
PyObject* modulePath = PyString_FromString( canvas_path->s_name );
252+
PyObject* modulePath = PyUnicode_FromString( canvas_path->s_name );
255253
PyObject* sysPath = PySys_GetObject( (char*) "path" ); // borrowed reference
256254

257255
if ( !PySequence_Contains( sysPath, modulePath )) {
@@ -332,6 +330,19 @@ static PyMethodDef pdgui_methods[] = {
332330
{ NULL, NULL, 0, NULL }
333331
};
334332

333+
static struct PyModuleDef pdguimodule = {
334+
PyModuleDef_HEAD_INIT,
335+
"pdgui", /* name of module */
336+
NULL, /* module documentation, may be NULL */
337+
-1, /* size of per-interpreter state of the module,
338+
or -1 if the module keeps state in global variables. */
339+
pdgui_methods};
340+
341+
PyMODINIT_FUNC
342+
PyInit_pdgui(void)
343+
{
344+
return PyModule_Create(&pdguimodule);
345+
}
335346
/****************************************************************/
336347
/// Initialization entry point for the Pd 'python' external. This is
337348
/// automatically called by Pd after loading the dynamic module to initialize
@@ -352,18 +363,39 @@ void python_setup(void)
352363
// inlet-callback function.
353364
class_addanything( pdpython_class, (t_method) pdpython_eval); // (t_class *c, t_method fn)
354365

366+
wchar_t *program;
367+
program = Py_DecodeLocale("py", NULL);
368+
if (program == NULL) {
369+
exit(1);
370+
}
371+
372+
Py_SetProgramName(program);
373+
355374
// static initialization follows
356-
Py_SetProgramName("pd");
375+
// Py_SetProgramName("pd");
357376
Py_Initialize();
358377

359-
// Make sure that sys.argv is defined.
360-
static char *arg0 = NULL;
378+
// Make sure that sys.argv is defined.
379+
// static char *arg0 = NULL;
380+
static wchar_t *arg0 = NULL;
361381
PySys_SetArgv( 0, &arg0 );
362382

363383
// make the internal pdgui wrapper module available for Python->C callbacks
364-
if (Py_InitModule("pdgui", pdgui_methods ) == NULL) {
384+
385+
if (PyInit_pdgui() == NULL)
386+
{
365387
post("Error: unable to create the pdgui module.");
366-
}
388+
}
389+
390+
// if (Py_InitModule("pdgui", pdgui_methods ) == NULL) {
391+
// post("Error: unable to create the pdgui module.");
392+
// }
367393
}
394+
395+
// PyMODINIT_FUNC
396+
// PyInit_pdgui(void)
397+
// {
398+
// return PyModule_Create(&pdguimodule);
399+
// }
368400
/****************************************************************/
369401

0 commit comments

Comments
 (0)