@@ -275,16 +275,20 @@ private Object createSocketInternal(VirtualFrame frame, LazyPythonClass cls, int
275275 @ GenerateNodeFactory
276276 public abstract static class GetHostnameNode extends PythonBuiltinNode {
277277 @ Specialization
278- @ TruffleBoundary
279278 String doGeneric () {
280279 try {
281- return InetAddress . getLocalHost (). getHostName ( );
280+ return getHostName ( getLocalHost ());
282281 } catch (UnknownHostException e ) {
283282 throw raise (PythonBuiltinClassType .OSError );
284283 }
285284 }
286285 }
287286
287+ @ TruffleBoundary
288+ private static InetAddress getLocalHost () throws UnknownHostException {
289+ return InetAddress .getLocalHost ();
290+ }
291+
288292 @ Builtin (name = "gethostbyaddr" , minNumOfPositionalArgs = 1 )
289293 @ GenerateNodeFactory
290294 public abstract static class GetHostByAddrNode extends PythonBuiltinNode {
@@ -296,52 +300,50 @@ PTuple doGeneric(VirtualFrame frame, PString ip_address) {
296300 @ Specialization
297301 PTuple doGeneric (VirtualFrame frame , String ip_address ) {
298302 try {
299- InetAddress [] adresses = getAllAddresses (ip_address );
303+ InetAddress [] adresses = getAllByName (ip_address );
300304 Object hostname = PNone .NONE ;
301305 Object [] strAdresses = new Object [adresses .length ];
302306 for (int i = 0 ; i < adresses .length ; i ++) {
303307 if (hostname == null ) {
304- hostname = getCanonicalHostName (adresses , i );
308+ hostname = getCanonicalHostName (adresses [ i ] );
305309 }
306- strAdresses [i ] = getHostAddress (adresses , i );
310+ strAdresses [i ] = getHostAddress (adresses [ i ] );
307311 }
308312 PList pAdresses = factory ().createList (strAdresses );
309313 return factory ().createTuple (new Object []{hostname , factory ().createList (), pAdresses });
310314 } catch (UnknownHostException e ) {
311315 throw raiseOSError (frame , OSErrorEnum .EHOSTUNREACH , e );
312316 }
313317 }
318+ }
314319
315- @ TruffleBoundary
316- private static String getHostAddress (InetAddress [] adresses , int i ) {
317- return adresses [i ].getHostAddress ();
318- }
319320
320- @ TruffleBoundary
321- private static String getCanonicalHostName (InetAddress [] adresses , int i ) {
322- return adresses [ i ]. getCanonicalHostName ();
323- }
321+ @ TruffleBoundary
322+ private static String getHostAddress (InetAddress address ) {
323+ return address . getHostAddress ();
324+ }
324325
325- @ TruffleBoundary
326- private static InetAddress [] getAllAddresses (String ip_address ) throws UnknownHostException {
327- return InetAddress .getAllByName (ip_address );
328- }
326+ @ TruffleBoundary
327+ private static String getCanonicalHostName (InetAddress address ) {
328+ return address .getCanonicalHostName ();
329+ }
330+
331+ @ TruffleBoundary
332+ private static InetAddress [] getAllByName (String ip_address ) throws UnknownHostException {
333+ return InetAddress .getAllByName (ip_address );
329334 }
330335
331336 @ Builtin (name = "gethostbyname" , minNumOfPositionalArgs = 1 )
332337 @ GenerateNodeFactory
333338 public abstract static class GetHostByNameNode extends PythonBuiltinNode {
334339 @ Specialization
335- @ TruffleBoundary
336340 Object getHostByName (String name ) {
337341 try {
338- InetAddress [] adresses = InetAddress .getAllByName (name );
339-
342+ InetAddress [] adresses = getAllByName (name );
340343 if (adresses .length == 0 ) {
341344 return PNone .NONE ;
342345 }
343-
344- return factory ().createString (adresses [0 ].getHostAddress ());
346+ return factory ().createString (getHostAddress (adresses [0 ]));
345347 } catch (UnknownHostException e ) {
346348 throw raise (PythonBuiltinClassType .OSError );
347349 }
@@ -352,7 +354,6 @@ Object getHostByName(String name) {
352354 @ GenerateNodeFactory
353355 public abstract static class GetServByNameNode extends PythonBuiltinNode {
354356 @ Specialization (guards = {"isNoValue(protocolName)" })
355- @ TruffleBoundary
356357 Object getServByName (String serviceName , @ SuppressWarnings ("unused" ) PNone protocolName ) {
357358 if (services == null ) {
358359 services = parseServices ();
@@ -368,19 +369,26 @@ Object getServByName(String serviceName, @SuppressWarnings("unused") PNone proto
368369 }
369370
370371 @ Specialization
371- @ TruffleBoundary
372372 Object getServByName (String serviceName , String protocolName ) {
373373 if (services == null ) {
374374 services = parseServices ();
375375 }
376+ int port = op (serviceName , protocolName );
377+ if (port >= 0 ) {
378+ return port ;
379+ } else {
380+ throw raise (PythonBuiltinClassType .OSError );
381+ }
382+ }
376383
384+ @ TruffleBoundary
385+ private static int op (String serviceName , String protocolName ) {
377386 for (Service service : services .get (serviceName )) {
378387 if (service .protocol .equals (protocolName )) {
379- return factory (). createInt ( service .port ) ;
388+ return service .port ;
380389 }
381390 }
382-
383- throw raise (PythonBuiltinClassType .OSError );
391+ return -1 ;
384392 }
385393 }
386394
@@ -447,7 +455,7 @@ Object getNameInfo(VirtualFrame frame, PTuple sockaddr, Object flagArg,
447455
448456 if ((flags & PSocket .NI_NUMERICHOST ) != PSocket .NI_NUMERICHOST ) {
449457 try {
450- address = getHostName (address );
458+ address = getHostName (getByName ( address ) );
451459 } catch (UnknownHostException e ) {
452460 throw raise (PythonBuiltinClassType .OSError );
453461 }
@@ -463,13 +471,16 @@ Object getNameInfo(VirtualFrame frame, PTuple sockaddr, Object flagArg,
463471
464472 return factory ().createTuple (new Object []{address , portServ });
465473 }
474+ }
466475
467- @ TruffleBoundary
468- private String getHostName (String address ) throws UnknownHostException {
469- InetAddress inetAddress = InetAddress .getByName (address );
470- address = inetAddress .getHostName ();
471- return address ;
472- }
476+ @ TruffleBoundary
477+ private static InetAddress getByName (String address ) throws UnknownHostException {
478+ return InetAddress .getByName (address );
479+ }
480+
481+ @ TruffleBoundary
482+ private static String getHostName (InetAddress address ) {
483+ return address .getHostName ();
473484 }
474485
475486 @ Builtin (name = "getaddrinfo" , parameterNames = {"host" , "port" , "family" , "type" , "proto" , "flags" })
@@ -494,6 +505,13 @@ Object getAddrInfoNone(@SuppressWarnings("unused") PNone host, Object port, Obje
494505 @ Specialization
495506 Object getAddrInfoString (String host , Object port , Object family , Object type , Object proto , Object flags ,
496507 @ Cached CastToJavaIntNode cast ) {
508+ InetAddress [] addresses ;
509+ try {
510+ addresses = getAllByName (host );
511+ } catch (UnknownHostException e ) {
512+ throw raise (PythonBuiltinClassType .OSError );
513+ }
514+
497515 String stringPort = null ;
498516 if (port instanceof PString ) {
499517 stringPort = ((PString ) port ).getValue ();
@@ -503,39 +521,34 @@ Object getAddrInfoString(String host, Object port, Object family, Object type, O
503521
504522 if (stringPort != null ) {
505523 stringPortProfile .enter ();
506- return getAddrInfo (host , stringPort , cast .execute (family ), cast .execute (type ), cast .execute (proto ), cast .execute (flags ));
507- }
508-
509- if (port instanceof PNone ) {
524+ return getAddrInfo (addresses , stringPort , cast .execute (family ), cast .execute (type ), cast .execute (proto ), cast .execute (flags ));
525+ } else if (port instanceof PNone ) {
510526 nonePortProfile .enter ();
511- InetAddress [] adresses = resolveHost (host );
512- return mergeAdressesAndServices (adresses , null , cast .execute (family ), cast .execute (type ), cast .execute (proto ), cast .execute (flags ));
527+ return mergeAdressesAndServices (addresses , null , cast .execute (family ), cast .execute (type ), cast .execute (proto ), cast .execute (flags ));
528+ } else {
529+ intPortProfile .enter ();
530+ return getAddrInfo (addresses , cast .execute (port ), cast .execute (family ), cast .execute (type ), cast .execute (proto ), cast .execute (flags ));
513531 }
514-
515- intPortProfile .enter ();
516- return getAddrInfo (host , cast .execute (port ), cast .execute (family ), cast .execute (type ), cast .execute (proto ), cast .execute (flags ));
517532 }
518533
519534 @ TruffleBoundary
520- private Object getAddrInfo (String host , int port , int family , int type , int proto , int flags ) {
521- InetAddress [] adresses = resolveHost (host );
535+ private Object getAddrInfo (InetAddress [] addresses , int port , int family , int type , int proto , int flags ) {
522536 List <Service > serviceList = new ArrayList <>();
523537 serviceList .add (new Service (port , "tcp" ));
524538 serviceList .add (new Service (port , "udp" ));
525- return mergeAdressesAndServices (adresses , serviceList , family , type , proto , flags );
539+ return mergeAdressesAndServices (addresses , serviceList , family , type , proto , flags );
526540 }
527541
528542 @ TruffleBoundary
529- private Object getAddrInfo (String host , String port , int family , int type , int proto , int flags ) {
543+ private Object getAddrInfo (InetAddress [] addresses , String port , int family , int type , int proto , int flags ) {
530544 if (!StandardCharsets .US_ASCII .newEncoder ().canEncode (port )) {
531545 throw raise (PythonBuiltinClassType .UnicodeEncodeError );
532546 }
533547 if (services == null ) {
534548 services = parseServices ();
535549 }
536550 List <Service > serviceList = services .get (port );
537- InetAddress [] adresses = resolveHost (host );
538- return mergeAdressesAndServices (adresses , serviceList , family , type , proto , flags );
551+ return mergeAdressesAndServices (addresses , serviceList , family , type , proto , flags );
539552 }
540553
541554 @ TruffleBoundary
@@ -584,15 +597,6 @@ private PTuple createAddressTuple(InetAddress address, int port, int family, int
584597 String canonname = (flags & PSocket .AI_CANONNAME ) == PSocket .AI_CANONNAME ? address .getHostName () : "" ;
585598 return factory ().createTuple (new Object []{addressFamily , addressType , proto , canonname , sockAddr });
586599 }
587-
588- @ TruffleBoundary
589- InetAddress [] resolveHost (String host ) {
590- try {
591- return InetAddress .getAllByName (host );
592- } catch (UnknownHostException e ) {
593- throw raise (PythonBuiltinClassType .OSError );
594- }
595- }
596600 }
597601
598602 @ Builtin (name = "close" , minNumOfPositionalArgs = 1 , parameterNames = {"fd" })
0 commit comments