jack.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. #include <python2.7/Python.h>
  2. #include <jack/jack.h>
  3. typedef struct {
  4. PyObject_HEAD
  5. jack_client_t* client;
  6. PyObject* port_registered_callback;
  7. PyObject* port_registered_callback_argument;
  8. PyObject* port_renamed_callback;
  9. PyObject* port_renamed_callback_argument;
  10. PyObject* port_unregistered_callback;
  11. PyObject* port_unregistered_callback_argument;
  12. PyObject* shutdown_callback;
  13. PyObject* shutdown_callback_argument;
  14. } Client;
  15. typedef struct {
  16. PyObject_HEAD
  17. jack_port_t* port;
  18. } Port;
  19. static PyObject* error;
  20. static PyObject* failure;
  21. static PyObject* connection_exists;
  22. static PyTypeObject port_type = {
  23. PyObject_HEAD_INIT(NULL)
  24. };
  25. static PyObject* python_import(const char* name)
  26. {
  27. PyObject* python_name = PyString_FromString(name);
  28. PyObject* object = PyImport_Import(python_name);
  29. Py_DECREF(python_name);
  30. return object;
  31. }
  32. long get_reference_count(PyObject* object)
  33. {
  34. PyObject* sys = python_import("sys");
  35. PyObject* getrefcount = PyObject_GetAttrString(sys, "getrefcount");
  36. // Return the reference count of the object.
  37. // The count returned is generally one higher than you might expect,
  38. // because it includes the (temporary) reference as an argument to getrefcount().
  39. PyObject* argument_list = PyTuple_Pack(1, object);
  40. PyObject* result = PyObject_CallObject(getrefcount, argument_list);
  41. Py_DECREF(argument_list);
  42. long count;
  43. if(!result) {
  44. PyErr_PrintEx(0);
  45. count = -2;
  46. } else {
  47. count = PyInt_AsLong(result);
  48. Py_DECREF(result);
  49. }
  50. Py_DECREF(getrefcount);
  51. Py_DECREF(sys);
  52. return count;
  53. }
  54. static void jack_registration_callback(jack_port_id_t port_id, int registered, void* arg)
  55. {
  56. // register: non-zero if the port is being registered, zero if the port is being unregistered
  57. Client* client = (Client*)arg;
  58. PyObject* callback;
  59. PyObject* callback_argument;
  60. if(registered) {
  61. callback = client->port_registered_callback;
  62. callback_argument = client->port_registered_callback_argument;
  63. } else {
  64. callback = client->port_unregistered_callback;
  65. callback_argument = client->port_unregistered_callback_argument;
  66. }
  67. if(callback) {
  68. // Ensure that the current thread is ready to call the Python API.
  69. // No Python API calls are allowed before this call.
  70. PyGILState_STATE gil_state = PyGILState_Ensure();
  71. Port* port = PyObject_New(Port, &port_type);
  72. port->port = jack_port_by_id(client->client, port_id);
  73. // 'O' increases reference count
  74. PyObject* callback_argument_list;
  75. if(callback_argument) {
  76. callback_argument_list = Py_BuildValue("(O,O,O)", (PyObject*)client, (PyObject*)port, callback_argument);
  77. } else {
  78. callback_argument_list = Py_BuildValue("(O,O)", (PyObject*)client, (PyObject*)port);
  79. }
  80. PyObject* result = PyObject_CallObject(callback, callback_argument_list);
  81. Py_DECREF(callback_argument_list);
  82. if(!result) {
  83. PyErr_PrintEx(0);
  84. } else {
  85. Py_DECREF(result);
  86. }
  87. // Release the thread. No Python API calls are allowed beyond this point.
  88. PyGILState_Release(gil_state);
  89. }
  90. }
  91. // typedef int (*JackPortRenameCallback)(jack_port_id_t port, const char* old_name, const char* new_name, void *arg);
  92. static int jack_port_renamed_callback(jack_port_id_t port_id, const char* old_name, const char* new_name, void* arg)
  93. {
  94. int return_code = 0;
  95. Client* client = (Client*)arg;
  96. if(client->port_renamed_callback) {
  97. // Ensure that the current thread is ready to call the Python API.
  98. // No Python API calls are allowed before this call.
  99. PyGILState_STATE gil_state = PyGILState_Ensure();
  100. Port* port = PyObject_New(Port, &port_type);
  101. port->port = jack_port_by_id(client->client, port_id);
  102. // 'O' increases reference count
  103. PyObject* callback_argument_list = Py_BuildValue(
  104. "(O,O,s,s,O)",
  105. (PyObject*)client,
  106. (PyObject*)port,
  107. old_name,
  108. new_name,
  109. client->port_renamed_callback_argument
  110. );
  111. PyObject* result = PyObject_CallObject(client->port_renamed_callback, callback_argument_list);
  112. Py_DECREF(callback_argument_list);
  113. if(!result) {
  114. PyErr_PrintEx(0);
  115. return_code = -1;
  116. } else {
  117. Py_DECREF(result);
  118. return_code = 0;
  119. }
  120. // Release the thread. No Python API calls are allowed beyond this point.
  121. PyGILState_Release(gil_state);
  122. }
  123. return return_code;
  124. }
  125. // typedef void(* JackInfoShutdownCallback)(jack_status_t code, const char *reason, void *arg)
  126. static void jack_shutdown_callback(jack_status_t code, const char* reason, void* arg)
  127. {
  128. Client* client = (Client*)arg;
  129. if(client->shutdown_callback) {
  130. // Ensure that the current thread is ready to call the Python API.
  131. // No Python API calls are allowed before this call.
  132. PyGILState_STATE gil_state = PyGILState_Ensure();
  133. // 'O' increases reference count
  134. PyObject* callback_argument_list = NULL;
  135. if(client->shutdown_callback_argument) {
  136. callback_argument_list = Py_BuildValue(
  137. "(O,s,O)",
  138. (PyObject*)client,
  139. reason,
  140. client->shutdown_callback_argument
  141. );
  142. } else {
  143. callback_argument_list = Py_BuildValue(
  144. "(O,s)",
  145. (PyObject*)client,
  146. reason
  147. );
  148. }
  149. PyObject* result = PyObject_CallObject(client->shutdown_callback, callback_argument_list);
  150. Py_DECREF(callback_argument_list);
  151. if(!result) {
  152. PyErr_PrintEx(0);
  153. } else {
  154. Py_DECREF(result);
  155. }
  156. // Release the thread. No Python API calls are allowed beyond this point.
  157. PyGILState_Release(gil_state);
  158. }
  159. }
  160. static PyObject* client___new__(PyTypeObject* type, PyObject* args, PyObject* kwargs)
  161. {
  162. Client* self = (Client*)type->tp_alloc(type, 0);
  163. if(self) {
  164. char* client_name;
  165. char* server_name = NULL;
  166. static char* kwlist[] = {"client_name", "server_name", NULL};
  167. if(!PyArg_ParseTupleAndKeywords(args, kwargs, "s|s", kwlist, &client_name, &server_name)) {
  168. return NULL;
  169. }
  170. jack_options_t open_options = JackNullOption;
  171. if(server_name) {
  172. open_options |= JackServerName;
  173. }
  174. jack_status_t status;
  175. self->client = jack_client_open(client_name, open_options, &status, server_name);
  176. if(!self->client) {
  177. if(status & JackFailure) {
  178. PyErr_SetString(failure, "Overall operation failed.");
  179. }
  180. return NULL;
  181. }
  182. self->port_registered_callback = NULL;
  183. self->port_unregistered_callback = NULL;
  184. int error_code = jack_set_port_registration_callback(
  185. self->client,
  186. jack_registration_callback,
  187. (void*)self
  188. );
  189. if(error_code) {
  190. PyErr_SetString(error, "Could not set port registration callback.");
  191. return NULL;
  192. }
  193. self->port_renamed_callback = NULL;
  194. self->port_renamed_callback_argument = NULL;
  195. error_code = jack_set_port_rename_callback(
  196. self->client,
  197. jack_port_renamed_callback,
  198. (void*)self
  199. );
  200. if(error_code) {
  201. PyErr_SetString(error, "Could not set port rename callback.");
  202. return NULL;
  203. }
  204. self->shutdown_callback = NULL;
  205. self->shutdown_callback_argument = NULL;
  206. jack_on_info_shutdown(
  207. self->client,
  208. jack_shutdown_callback,
  209. (void*)self
  210. );
  211. }
  212. return (PyObject*)self;
  213. }
  214. static PyObject* client_activate(Client* self) {
  215. int error_code = jack_activate(self->client);
  216. if(error_code) {
  217. PyErr_SetString(error, "");
  218. return NULL;
  219. } else {
  220. Py_INCREF(Py_None);
  221. return Py_None;
  222. }
  223. }
  224. static PyObject* client_deactivate(Client* self) {
  225. int error_code = jack_deactivate(self->client);
  226. if(error_code) {
  227. PyErr_SetString(error, "");
  228. return NULL;
  229. } else {
  230. Py_INCREF(Py_None);
  231. return Py_None;
  232. }
  233. }
  234. #include <stdio.h>
  235. static PyObject* client_connect(Client* self, PyObject* args)
  236. {
  237. PyObject *source_port_python, *target_port_python;
  238. // The object’s reference count is not increased.
  239. if(!PyArg_ParseTuple(args, "O!O!", &port_type, &source_port_python, &port_type, &target_port_python)) {
  240. return NULL;
  241. }
  242. Port* source_port = (Port*) source_port_python;
  243. Port* target_port = (Port*) target_port_python;
  244. int return_code = jack_connect(
  245. self->client,
  246. jack_port_name(source_port->port),
  247. jack_port_name(target_port->port)
  248. );
  249. if(return_code) {
  250. if(return_code == EEXIST) {
  251. PyErr_SetString(connection_exists, "The specified ports are already connected.");
  252. }
  253. return NULL;
  254. } else {
  255. Py_INCREF(Py_None);
  256. return Py_None;
  257. }
  258. }
  259. static PyObject* client_get_name(Client* self)
  260. {
  261. return (PyObject*)PyString_FromString(jack_get_client_name(self->client));
  262. }
  263. static PyObject* client_get_ports(Client* self)
  264. {
  265. PyObject* ports = PyList_New(0);
  266. if(!ports) {
  267. return NULL;
  268. }
  269. const char** port_names = jack_get_ports(self->client, NULL, NULL, 0);
  270. int port_index;
  271. for(port_index = 0; port_names[port_index] != NULL; port_index++) {
  272. Port* port = PyObject_New(Port, &port_type);
  273. port->port = jack_port_by_name(self->client, port_names[port_index]);
  274. if(PyList_Append(ports, (PyObject*)port)) {
  275. return NULL;
  276. }
  277. }
  278. jack_free(port_names);
  279. return ports;
  280. }
  281. static PyObject* client_set_port_registered_callback(Client* self, PyObject* args)
  282. {
  283. PyObject* callback = 0;
  284. PyObject* callback_argument = 0;
  285. if(!PyArg_ParseTuple(args, "O|O", &callback, &callback_argument)) {
  286. return NULL;
  287. }
  288. if(!PyCallable_Check(callback)) {
  289. PyErr_SetString(PyExc_TypeError, "Parameter must be callable.");
  290. return NULL;
  291. }
  292. Py_XINCREF(callback);
  293. Py_XDECREF(self->port_registered_callback);
  294. self->port_registered_callback = callback;
  295. Py_XINCREF(callback_argument);
  296. Py_XDECREF(self->port_registered_callback_argument);
  297. self->port_registered_callback_argument = callback_argument;
  298. Py_INCREF(Py_None);
  299. return Py_None;
  300. }
  301. static PyObject* client_set_port_unregistered_callback(Client* self, PyObject* args)
  302. {
  303. PyObject* callback = 0;
  304. PyObject* callback_argument = 0;
  305. if(!PyArg_ParseTuple(args, "O|O", &callback, &callback_argument)) {
  306. return NULL;
  307. }
  308. if(!PyCallable_Check(callback)) {
  309. PyErr_SetString(PyExc_TypeError, "Parameter must be callable.");
  310. return NULL;
  311. }
  312. Py_XINCREF(callback);
  313. Py_XDECREF(self->port_unregistered_callback);
  314. self->port_unregistered_callback = callback;
  315. Py_XINCREF(callback_argument);
  316. Py_XDECREF(self->port_unregistered_callback_argument);
  317. self->port_unregistered_callback_argument = callback_argument;
  318. Py_INCREF(Py_None);
  319. return Py_None;
  320. }
  321. static PyObject* client_set_port_renamed_callback(Client* self, PyObject* args)
  322. {
  323. PyObject* callback = 0;
  324. PyObject* callback_argument = 0;
  325. if(!PyArg_ParseTuple(args, "O|O", &callback, &callback_argument)) {
  326. return NULL;
  327. }
  328. if(!PyCallable_Check(callback)) {
  329. PyErr_SetString(PyExc_TypeError, "Parameter must be callable.");
  330. return NULL;
  331. }
  332. Py_XINCREF(callback);
  333. Py_XDECREF(self->port_renamed_callback);
  334. self->port_renamed_callback = callback;
  335. Py_XINCREF(callback_argument);
  336. Py_XDECREF(self->port_renamed_callback_argument);
  337. self->port_renamed_callback_argument = callback_argument;
  338. Py_INCREF(Py_None);
  339. return Py_None;
  340. }
  341. static PyObject* client_set_shutdown_callback(Client* self, PyObject* args)
  342. {
  343. PyObject* callback = 0;
  344. PyObject* callback_argument = 0;
  345. if(!PyArg_ParseTuple(args, "O|O", &callback, &callback_argument)) {
  346. return NULL;
  347. }
  348. if(!PyCallable_Check(callback)) {
  349. PyErr_SetString(PyExc_TypeError, "Parameter must be callable.");
  350. return NULL;
  351. }
  352. Py_XINCREF(callback);
  353. Py_XDECREF(self->shutdown_callback);
  354. self->shutdown_callback = callback;
  355. Py_XINCREF(callback_argument);
  356. Py_XDECREF(self->shutdown_callback_argument);
  357. self->shutdown_callback_argument = callback_argument;
  358. Py_INCREF(Py_None);
  359. return Py_None;
  360. }
  361. static void client_dealloc(Client* self)
  362. {
  363. jack_client_close(self->client);
  364. Py_XDECREF(self->port_registered_callback);
  365. Py_XDECREF(self->port_registered_callback_argument);
  366. Py_XDECREF(self->port_renamed_callback);
  367. Py_XDECREF(self->port_renamed_callback_argument);
  368. Py_XDECREF(self->port_unregistered_callback);
  369. Py_XDECREF(self->port_unregistered_callback_argument);
  370. Py_XDECREF(self->shutdown_callback);
  371. Py_XDECREF(self->shutdown_callback_argument);
  372. self->ob_type->tp_free((PyObject*)self);
  373. }
  374. static PyMethodDef client_methods[] = {
  375. {
  376. "activate",
  377. (PyCFunction)client_activate,
  378. METH_NOARGS,
  379. "Tell the Jack server that the program is ready to start processing audio.",
  380. },
  381. {
  382. "connect",
  383. (PyCFunction)client_connect,
  384. METH_VARARGS,
  385. "Establish a connection between two ports.",
  386. },
  387. {
  388. "deactivate",
  389. (PyCFunction)client_deactivate,
  390. METH_NOARGS,
  391. "Tell the Jack server that the program is ready to start processing audio.",
  392. },
  393. {
  394. "get_name",
  395. (PyCFunction)client_get_name,
  396. METH_NOARGS,
  397. "Return client's actual name.",
  398. },
  399. {
  400. "get_ports",
  401. (PyCFunction)client_get_ports,
  402. METH_NOARGS,
  403. "Return list of ports.",
  404. },
  405. {
  406. "set_port_registered_callback",
  407. (PyCFunction)client_set_port_registered_callback,
  408. METH_VARARGS,
  409. "Tell the JACK server to call a function whenever a port is registered.",
  410. },
  411. {
  412. "set_port_unregistered_callback",
  413. (PyCFunction)client_set_port_unregistered_callback,
  414. METH_VARARGS,
  415. "Tell the JACK server to call a function whenever a port is unregistered.",
  416. },
  417. {
  418. "set_port_renamed_callback",
  419. (PyCFunction)client_set_port_renamed_callback,
  420. METH_VARARGS,
  421. "Tell the JACK server to call a function whenever a port is renamed.",
  422. },
  423. {
  424. "set_shutdown_callback",
  425. (PyCFunction)client_set_shutdown_callback,
  426. METH_VARARGS,
  427. "Tell the JACK server to call a function when the server shuts down the client.",
  428. },
  429. {NULL},
  430. };
  431. static PyObject* port_is_input(Port* self)
  432. {
  433. // The flags "JackPortIsInput" and "JackPortIsOutput" are mutually exclusive.
  434. return (PyObject*)PyBool_FromLong(jack_port_flags(self->port) & JackPortIsInput);
  435. }
  436. static PyObject* port_is_output(Port* self)
  437. {
  438. // The flags "JackPortIsInput" and "JackPortIsOutput" are mutually exclusive.
  439. return (PyObject*)PyBool_FromLong(jack_port_flags(self->port) & JackPortIsOutput);
  440. }
  441. static PyObject* port_get_name(Port* self)
  442. {
  443. return (PyObject*)PyString_FromString(jack_port_name(self->port));
  444. }
  445. static PyObject* port_get_short_name(Port* self)
  446. {
  447. return (PyObject*)PyString_FromString(jack_port_short_name(self->port));
  448. }
  449. static PyObject* port_get_type(Port* self)
  450. {
  451. return (PyObject*)PyString_FromString(jack_port_type(self->port));
  452. }
  453. static PyObject* port_set_short_name(Port* self, PyObject* args)
  454. {
  455. const char* name;
  456. if(!PyArg_ParseTuple(args, "s", &name)) {
  457. return NULL;
  458. }
  459. // 0 on success, otherwise a non-zero error code.
  460. if(jack_port_set_name(self->port, name)) {
  461. return NULL;
  462. }
  463. Py_INCREF(Py_None);
  464. return Py_None;
  465. }
  466. static PyObject* port_get_client_name(Port* self)
  467. {
  468. // e.g. "PulseAudio JACK Sink:front-left"
  469. const char* port_name = jack_port_name(self->port);
  470. int client_name_length = strlen(port_name) - strlen(jack_port_short_name(self->port)) - 1;
  471. // e.g. "PulseAudio JACK Sink"
  472. char* client_name = (char*) malloc(jack_port_name_size());
  473. strncpy(client_name, port_name, client_name_length);
  474. client_name[client_name_length] = '\0';
  475. PyObject* client_name_python = PyString_FromString(client_name);
  476. free(client_name);
  477. return client_name_python;
  478. }
  479. static PyObject* port_get_aliases(Port* self)
  480. {
  481. PyObject* aliases_list = PyList_New(0);
  482. if(!aliases_list) {
  483. return NULL;
  484. }
  485. char* aliases[2];
  486. aliases[0] = (char*) malloc(jack_port_name_size());
  487. aliases[1] = (char*) malloc(jack_port_name_size());
  488. int alias_count = jack_port_get_aliases(self->port, aliases);
  489. int alias_index;
  490. for(alias_index = 0; alias_index < alias_count; alias_index++) {
  491. PyList_Append(aliases_list, PyString_FromString(aliases[alias_index]));
  492. }
  493. free(aliases[0]);
  494. free(aliases[1]);
  495. return aliases_list;
  496. }
  497. static PyObject* port___repr__(Port* self)
  498. {
  499. static PyObject* format;
  500. if(!format) {
  501. format = PyString_FromString("jack.Port(name = '%s', type = '%s')");
  502. }
  503. PyObject* args = Py_BuildValue(
  504. "(s,s)",
  505. jack_port_name(self->port),
  506. jack_port_type(self->port)
  507. );
  508. PyObject* repr = PyString_Format(format, args);
  509. Py_DECREF(args);
  510. return repr;
  511. }
  512. static PyMethodDef port_methods[] = {
  513. {
  514. "is_input",
  515. (PyCFunction)port_is_input,
  516. METH_NOARGS,
  517. "Return true if the port can receive data.",
  518. },
  519. {
  520. "is_output",
  521. (PyCFunction)port_is_output,
  522. METH_NOARGS,
  523. "Return true if data can be read from the port.",
  524. },
  525. {
  526. "get_name",
  527. (PyCFunction)port_get_name,
  528. METH_NOARGS,
  529. "Return port's name.",
  530. },
  531. {
  532. "get_short_name",
  533. (PyCFunction)port_get_short_name,
  534. METH_NOARGS,
  535. "Return port's name without the preceding name of the associated client.",
  536. },
  537. {
  538. "get_type",
  539. (PyCFunction)port_get_type,
  540. METH_NOARGS,
  541. "Return port's type.",
  542. },
  543. {
  544. "get_client_name",
  545. (PyCFunction)port_get_client_name,
  546. METH_NOARGS,
  547. "Return the name of the associated client.",
  548. },
  549. {
  550. "set_short_name",
  551. (PyCFunction)port_set_short_name,
  552. METH_VARARGS,
  553. "Modify a port's short name. May be called at any time.",
  554. },
  555. {
  556. "get_aliases",
  557. (PyCFunction)port_get_aliases,
  558. METH_NOARGS,
  559. "Return list of assigned aliases.",
  560. },
  561. {NULL},
  562. };
  563. #ifndef PyMODINIT_FUNC // declarations for DLL import/export
  564. #define PyMODINIT_FUNC void
  565. #endif
  566. PyMODINIT_FUNC initjack(void)
  567. {
  568. // Initialize and acquire the global interpreter lock.
  569. // This must be done in the main thread before creating engaging in any thread operations.
  570. PyEval_InitThreads();
  571. PyObject* module = Py_InitModule("jack", NULL);
  572. if(!module) {
  573. return;
  574. }
  575. error = PyErr_NewException((char*)"jack.Error", NULL, NULL);
  576. Py_INCREF(error);
  577. PyModule_AddObject(module, "Error", error);
  578. failure = PyErr_NewException((char*)"jack.Failure", error, NULL);
  579. Py_INCREF(failure);
  580. PyModule_AddObject(module, "Failure", failure);
  581. connection_exists = PyErr_NewException((char*)"jack.ConnectionExists", error, NULL);
  582. Py_INCREF(connection_exists);
  583. PyModule_AddObject(module, "ConnectionExists", connection_exists);
  584. static PyTypeObject client_type = {
  585. PyObject_HEAD_INIT(NULL)
  586. };
  587. client_type.tp_name = "jack.Client";
  588. client_type.tp_basicsize = sizeof(Client);
  589. client_type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
  590. client_type.tp_new = client___new__;
  591. client_type.tp_dealloc = (destructor)client_dealloc;
  592. client_type.tp_methods = client_methods;
  593. if(PyType_Ready(&client_type) < 0) {
  594. return;
  595. }
  596. Py_INCREF(&client_type);
  597. PyModule_AddObject(module, "Client", (PyObject*)&client_type);
  598. port_type.tp_name = "jack.Port";
  599. port_type.tp_basicsize = sizeof(Port);
  600. port_type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
  601. // Forbid direct instantiation.
  602. port_type.tp_new = NULL;
  603. port_type.tp_repr = (reprfunc)port___repr__;
  604. port_type.tp_methods = port_methods;
  605. if(PyType_Ready(&port_type) < 0) {
  606. return;
  607. }
  608. Py_INCREF(&port_type);
  609. PyModule_AddObject(module, "Port", (PyObject*)&port_type);
  610. }