4040 */
4141package com .oracle .graal .python .builtins .objects .cext .capi ;
4242
43- import static com .oracle .graal .python .builtins .PythonBuiltinClassType .ValueError ;
4443import static com .oracle .graal .python .builtins .objects .cext .capi .NativeCAPISymbol .FUN_PTR_ADD ;
4544import static com .oracle .graal .python .builtins .objects .cext .structs .CFields .PyMemoryViewObject__exports ;
4645import static com .oracle .graal .python .builtins .objects .cext .structs .CFields .PyMemoryViewObject__flags ;
5958import com .oracle .graal .python .builtins .objects .ints .PInt ;
6059import com .oracle .graal .python .builtins .objects .memoryview .PMemoryView ;
6160import com .oracle .graal .python .builtins .objects .object .PythonObject ;
62- import com .oracle .graal .python .nodes .ErrorMessages ;
63- import com .oracle .graal .python .nodes .PRaiseNode ;
6461import com .oracle .graal .python .nodes .object .GetClassNode ;
6562import com .oracle .graal .python .runtime .PythonContext ;
6663import com .oracle .truffle .api .CompilerDirectives ;
@@ -91,9 +88,6 @@ private static Object intArrayToNativePySSizeArray(int[] intArray) {
9188
9289 @ TruffleBoundary
9390 private static Object allocate (PMemoryView object ) {
94- if (object .isReleased ()) {
95- throw PRaiseNode .raiseStatic (null , ValueError , ErrorMessages .MEMORYVIEW_FORBIDDEN_RELEASED );
96- }
9791 GetElementPtrNode getElementNode = GetElementPtrNode .getUncached ();
9892 CStructAccess .WritePointerNode writePointerNode = CStructAccess .WritePointerNode .getUncached ();
9993 CStructAccess .WriteLongNode writeI64Node = CStructAccess .WriteLongNode .getUncached ();
@@ -115,31 +109,33 @@ private static Object allocate(PMemoryView object) {
115109
116110 Object view = getElementNode .getElementPtr (mem , CFields .PyMemoryViewObject__view );
117111
118- Object buf = object .getBufferPointer ();
119- if (buf == null ) {
120- buf = PythonBufferAccessLibrary .getUncached ().getNativePointer (object .getBuffer ());
112+ if (object .getBuffer () != null ) {
113+ Object buf = object .getBufferPointer ();
121114 if (buf == null ) {
122- CompilerDirectives .transferToInterpreterAndInvalidate ();
123- throw shouldNotReachHere ("Cannot convert managed object to native storage: " + object .getBuffer ().getClass ().getSimpleName ());
115+ buf = PythonBufferAccessLibrary .getUncached ().getNativePointer (object .getBuffer ());
116+ if (buf == null ) {
117+ CompilerDirectives .transferToInterpreterAndInvalidate ();
118+ throw shouldNotReachHere ("Cannot convert managed object to native storage: " + object .getBuffer ().getClass ().getSimpleName ());
119+ }
124120 }
125- }
126- if (object .getOffset () != 0 ) {
127- if (buf instanceof Long ptr ) {
128- buf = ptr + object .getOffset ();
129- } else {
130- InteropLibrary ptrLib = InteropLibrary .getUncached (buf );
131- if (ptrLib .isPointer (buf )) {
132- try {
133- buf = ptrLib .asPointer (buf ) + object .getOffset ();
134- } catch (UnsupportedMessageException e ) {
135- throw CompilerDirectives .shouldNotReachHere (e );
136- }
121+ if (object .getOffset () != 0 ) {
122+ if (buf instanceof Long ptr ) {
123+ buf = ptr + object .getOffset ();
137124 } else {
138- buf = CExtNodes .PCallCapiFunction .callUncached (FUN_PTR_ADD , buf , (long ) object .getOffset ());
125+ InteropLibrary ptrLib = InteropLibrary .getUncached (buf );
126+ if (ptrLib .isPointer (buf )) {
127+ try {
128+ buf = ptrLib .asPointer (buf ) + object .getOffset ();
129+ } catch (UnsupportedMessageException e ) {
130+ throw CompilerDirectives .shouldNotReachHere (e );
131+ }
132+ } else {
133+ buf = CExtNodes .PCallCapiFunction .callUncached (FUN_PTR_ADD , buf , (long ) object .getOffset ());
134+ }
139135 }
140136 }
137+ writePointerNode .write (view , CFields .Py_buffer__buf , buf );
141138 }
142- writePointerNode .write (view , CFields .Py_buffer__buf , buf );
143139
144140 if (object .getOwner () != null ) {
145141 writePointerNode .write (view , CFields .Py_buffer__obj , PythonToNativeNewRefNode .executeUncached (object .getOwner ()));
0 commit comments