@@ -19,6 +19,8 @@ declare function _pyproxy_getflags(ptr: number): number;
1919
2020declare function __pyproxy_contains ( ptr : number , key : any ) : number ;
2121declare function __pyproxy_getitem ( ptr : number , key : any ) : any ;
22+ declare function __pyproxy_setitem ( ptr : number , key : any , val : any ) : number ;
23+ declare function __pyproxy_delitem ( ptr : number , key : any ) : number ;
2224declare function __pyproxy_apply (
2325 ptr : number ,
2426 jsargs : any [ ] ,
@@ -273,6 +275,7 @@ function getPyProxyClass(flags: number) {
273275 [ HAS_CONTAINS , PyContainsMethods ] ,
274276 [ HAS_GET , PyGetItemMethods ] ,
275277 [ HAS_LENGTH , PyLengthMethods ] ,
278+ [ HAS_SET , PySetItemMethods ] ,
276279 [ IS_CALLABLE , PyCallableMethods ] ,
277280 ] ;
278281 for ( let [ feature_flag , methods ] of FLAG_TYPE_PAIRS ) {
@@ -504,7 +507,7 @@ interface PyProxyWithLength extends PyLengthMethods {}
504507
505508// Controlled by HAS_LENGTH, appears for any object with __len__ or sq_length
506509// or mp_length methods
507- export class PyLengthMethods {
510+ class PyLengthMethods {
508511 /**
509512 * The length of the object.
510513 */
@@ -526,6 +529,52 @@ export class PyLengthMethods {
526529}
527530
528531
532+
533+ interface PyProxyWithSet extends PySetItemMethods { }
534+ // Controlled by HAS_SET, appears for any class with __setitem__, __delitem__,
535+ // mp_ass_subscript, or sq_ass_item.
536+ class PySetItemMethods {
537+ /**
538+ * This translates to the Python code ``obj[key] = value``.
539+ *
540+ * @param key The key to set.
541+ * @param value The value to set it to.
542+ */
543+ set ( key : any , value : any ) {
544+ let ptrobj = _getPtr ( this ) ;
545+ let err ;
546+ try {
547+ Py_ENTER ( ) ;
548+ err = __pyproxy_setitem ( ptrobj , key , value ) ;
549+ Py_EXIT ( ) ;
550+ } catch ( e ) {
551+ API . fatal_error ( e ) ;
552+ }
553+ if ( err === - 1 ) {
554+ _pythonexc2js ( ) ;
555+ }
556+ }
557+ /**
558+ * This translates to the Python code ``del obj[key]``.
559+ *
560+ * @param key The key to delete.
561+ */
562+ delete ( key : any ) {
563+ let ptrobj = _getPtr ( this ) ;
564+ let err ;
565+ try {
566+ Py_ENTER ( ) ;
567+ err = __pyproxy_delitem ( ptrobj , key ) ;
568+ Py_EXIT ( ) ;
569+ } catch ( e ) {
570+ API . fatal_error ( e ) ;
571+ }
572+ if ( err === - 1 ) {
573+ _pythonexc2js ( ) ;
574+ }
575+ }
576+ }
577+
529578function _adjustArgs ( proxyobj : any , jsthis : any , jsargs : any [ ] ) : any [ ] {
530579 const { captureThis, boundArgs, boundThis, isBound } =
531580 _getAttrs ( proxyobj ) . props ;
0 commit comments