| 
									
										
										
										
											2014-10-31 14:35:15 -04:00
										 |  |  | USING: accessors alien.data alien.libraries classes.struct | 
					
						
							|  |  |  | io.encodings.ascii io.encodings.utf8 kernel libc literals | 
					
						
							|  |  |  | python.errors python.ffi ;
 | 
					
						
							| 
									
										
										
										
											2014-01-31 09:59:04 -05:00
										 |  |  | IN: python.objects | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-26 19:23:54 -04:00
										 |  |  | ! The None object | 
					
						
							|  |  |  | : (none) ( -- none )
 | 
					
						
							|  |  |  |     "_Py_NoneStruct" "python" address-of ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <none> ( -- none )
 | 
					
						
							|  |  |  |     (none) check-borrowed-ref ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-31 09:59:04 -05:00
										 |  |  | ! Objects | 
					
						
							|  |  |  | : getattr ( obj str -- value )
 | 
					
						
							|  |  |  |     PyObject_GetAttrString check-new-ref ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : setattr ( obj str value -- )
 | 
					
						
							|  |  |  |     PyObject_SetAttrString check-zero ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : call-object ( obj args -- value )
 | 
					
						
							|  |  |  |     PyObject_CallObject check-new-ref ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : call-object-full ( obj args kwargs -- value )
 | 
					
						
							|  |  |  |     PyObject_Call check-new-ref ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Tuples | 
					
						
							|  |  |  | : <py-tuple> ( length -- tuple )
 | 
					
						
							|  |  |  |     PyTuple_New check-new-ref ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : py-tuple-set-item ( obj pos val -- )
 | 
					
						
							| 
									
										
										
										
											2014-10-26 19:23:54 -04:00
										 |  |  |     dup unsteal-ref PyTuple_SetItem check-zero ;
 | 
					
						
							| 
									
										
										
										
											2014-01-31 09:59:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : py-tuple-get-item ( obj pos -- val )
 | 
					
						
							|  |  |  |     PyTuple_GetItem dup Py_IncRef check-new-ref ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : py-tuple-size ( obj -- len )
 | 
					
						
							|  |  |  |     PyTuple_Size ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <1py-tuple> ( alien -- tuple )
 | 
					
						
							|  |  |  |     1 <py-tuple> [ 0 rot py-tuple-set-item ] keep ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Dicts | 
					
						
							|  |  |  | : <py-dict> ( -- dict )
 | 
					
						
							|  |  |  |     PyDict_New check-new-ref ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : py-dict-set-item ( obj key val -- )
 | 
					
						
							|  |  |  |     PyDict_SetItem check-zero ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : py-dict-set-item-string ( dict key val -- )
 | 
					
						
							|  |  |  |     PyDict_SetItemString check-zero ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : py-dict-get-item-string ( obj key -- val )
 | 
					
						
							|  |  |  |     PyDict_GetItemString check-borrowed-ref ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : py-dict-size ( obj -- len )
 | 
					
						
							|  |  |  |     PyDict_Size ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Lists | 
					
						
							|  |  |  | : <py-list> ( length -- list )
 | 
					
						
							|  |  |  |     PyList_New check-new-ref ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : py-list-size ( list -- len )
 | 
					
						
							|  |  |  |     PyList_Size ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : py-list-get-item ( obj pos -- val )
 | 
					
						
							|  |  |  |     PyList_GetItem check-borrowed-ref ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : py-list-set-item ( obj pos val -- )
 | 
					
						
							| 
									
										
										
										
											2014-10-26 19:23:54 -04:00
										 |  |  |     dup unsteal-ref PyList_SetItem check-zero ;
 | 
					
						
							| 
									
										
										
										
											2014-10-23 19:01:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Functions | 
					
						
							| 
									
										
										
										
											2014-10-27 18:12:47 -04:00
										 |  |  | : <PyMethodDef> ( alien name doc/f -- cfunction )
 | 
					
						
							|  |  |  |     PyMethodDef malloc-struct &free | 
					
						
							|  |  |  |     swap [ utf8 malloc-string &free >>ml_doc ] when*
 | 
					
						
							|  |  |  |     swap ascii malloc-string &free >>ml_name | 
					
						
							|  |  |  |     swap >>ml_meth | 
					
						
							| 
									
										
										
										
											2014-10-31 14:35:15 -04:00
										 |  |  |     flags{ METH_VARARGS METH_KEYWORDS } >>ml_flags ;
 | 
					
						
							| 
									
										
										
										
											2014-10-27 18:12:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-23 19:01:15 -04:00
										 |  |  | : <py-cfunction> ( alien -- cfunction )
 | 
					
						
							| 
									
										
										
										
											2014-10-27 18:12:47 -04:00
										 |  |  |     "cfunction" f <PyMethodDef> f f
 | 
					
						
							| 
									
										
										
										
											2014-10-23 19:01:15 -04:00
										 |  |  |     ! It's not clear from the docs whether &Py_DecRef is right for | 
					
						
							|  |  |  |     ! PyCFunction_NewEx, but I'm betting on it. | 
					
						
							|  |  |  |     PyCFunction_NewEx check-new-ref ;
 |