66
77using System ;
88using System . Collections . Generic ;
9+ using System . Numerics ;
910using System . Runtime . InteropServices ;
1011
1112using Microsoft . Scripting . Runtime ;
1213
1314using IronPython . Runtime ;
1415using IronPython . Runtime . Operations ;
15-
16- using System . Numerics ;
16+ using IronPython . Runtime . Exceptions ;
17+ using IronPython . Runtime . Types ;
1718
1819[ assembly: PythonModule ( "grp" , typeof ( IronPython . Modules . PythonGrp ) , PlatformsAttribute . PlatformFamily . Unix ) ]
1920namespace IronPython . Modules {
20-
21+
2122 public static class PythonGrp {
2223 public const string __doc__ = @"Access to the Unix group database.
2324
@@ -79,13 +80,10 @@ private static struct_group Make(IntPtr pwd) {
7980 return new struct_group ( g . gr_name , g . gr_passwd , g . gr_gid , new PythonList ( MarshalStringArray ( g . gr_mem ) ) ) ;
8081 }
8182
82- private static IEnumerable < string > MarshalStringArray ( IntPtr arrayPtr )
83- {
84- if ( arrayPtr != IntPtr . Zero )
85- {
83+ private static IEnumerable < string > MarshalStringArray ( IntPtr arrayPtr ) {
84+ if ( arrayPtr != IntPtr . Zero ) {
8685 IntPtr ptr = Marshal . ReadIntPtr ( arrayPtr ) ;
87- while ( ptr != IntPtr . Zero )
88- {
86+ while ( ptr != IntPtr . Zero ) {
8987 string key = Marshal . PtrToStringAnsi ( ptr ) ;
9088 yield return key ;
9189 arrayPtr = new IntPtr ( arrayPtr . ToInt64 ( ) + IntPtr . Size ) ;
@@ -96,16 +94,27 @@ private static IEnumerable<string> MarshalStringArray(IntPtr arrayPtr)
9694
9795 public static struct_group getgrgid ( int gid ) {
9896 var grp = _getgrgid ( gid ) ;
99- if ( grp == IntPtr . Zero ) {
97+ if ( grp == IntPtr . Zero ) {
10098 throw PythonOps . KeyError ( $ "getgrgid(): gid not found: { gid } ") ;
10199 }
102100
103101 return Make ( grp ) ;
104102 }
105103
104+ public static struct_group getgrgid ( CodeContext context , object gid ) {
105+ var g = BigIntegerOps . __new__ ( context , TypeCache . BigInteger , gid ) ;
106+ PythonOps . Warn ( context , PythonExceptions . DeprecationWarning , $ "group id must be int, not { PythonOps . GetPythonTypeName ( gid ) } ") ;
107+ return g switch {
108+ int i => getgrgid ( i ) ,
109+ BigInteger bi => getgrgid ( ( int ) bi ) ,
110+ _ => throw new InvalidOperationException ( ) ,
111+ } ;
112+ }
113+
106114 public static struct_group getgrnam ( string name ) {
115+ if ( name is not null && name . IndexOf ( ( char ) 0 ) != - 1 ) throw PythonOps . ValueError ( "embedded null byte" ) ;
107116 var grp = _getgrnam ( name ) ;
108- if ( grp == IntPtr . Zero ) {
117+ if ( grp == IntPtr . Zero ) {
109118 throw PythonOps . KeyError ( $ "getgrnam()): name not found: { name } ") ;
110119 }
111120
@@ -116,31 +125,32 @@ public static PythonList getgrall() {
116125 var res = new PythonList ( ) ;
117126 setgrent ( ) ;
118127 IntPtr val = getgrent ( ) ;
119- while ( val != IntPtr . Zero ) {
128+ while ( val != IntPtr . Zero ) {
120129 res . Add ( Make ( val ) ) ;
121130 val = getgrent ( ) ;
122131 }
123-
132+
124133 return res ;
125134 }
126135
127136
128137 #region P/Invoke Declarations
129138
130- [ DllImport ( "libc" , EntryPoint = "getgrgid" , CallingConvention = CallingConvention . Cdecl ) ]
139+ [ DllImport ( "libc" , EntryPoint = "getgrgid" , CallingConvention = CallingConvention . Cdecl ) ]
131140 private static extern IntPtr _getgrgid ( int uid ) ;
132141
133- [ DllImport ( "libc" , EntryPoint = "getgrnam" , CallingConvention = CallingConvention . Cdecl ) ]
142+ [ DllImport ( "libc" , EntryPoint = "getgrnam" , CallingConvention = CallingConvention . Cdecl ) ]
134143 private static extern IntPtr _getgrnam ( [ MarshalAs ( UnmanagedType . LPStr ) ] string name ) ;
135144
136- [ DllImport ( "libc" , CallingConvention = CallingConvention . Cdecl ) ]
145+ [ DllImport ( "libc" , CallingConvention = CallingConvention . Cdecl ) ]
137146 private static extern void setgrent ( ) ;
138147
139- [ DllImport ( "libc" , CallingConvention = CallingConvention . Cdecl ) ]
148+ [ DllImport ( "libc" , CallingConvention = CallingConvention . Cdecl ) ]
140149 private static extern IntPtr getgrent ( ) ;
141150
142151 #endregion
143152
144153 }
145154}
155+
146156#endif
0 commit comments