Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the ExtUtils-ParseXS CPAN distribution.

Report information
The Basics
Id: 52873
Status: resolved
Priority: 0/
Queue: ExtUtils-ParseXS

People
Owner: Nobody in particular
Requestors: smueller [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: (no value)
Fixed in: (no value)



Subject: [PATCH] Add safer INCLUDE_COMMAND directive
Hi, the attached patches (against ad167437c3908a3553d7a86c877869b1d423179c) do two things: 0001 adds a INCLUDE_COMMAND directive which does the same as "INCLUDE: command |" except it uses three-arg open and doesn't require (or allow) a pipe in the command string. This also adds a deprecation warning for the more unsafe "INCLUDE: command |". 0002 adds a tiny little bit of magic that allows users to tell xsubpp that they want to run something *using the current perl interpreter*. That is, not *in the same instance* but with the same executable. Syntax is simple and rather obvious to the Perl reader: "INCLUDE_COMMAND: $^X ..." where $^X is expanded by this patch to EU::PXS. The only downside of this is that $^X may make users expect they can use other Perl code/variables in such commands. This functionality would be very, very useful (read: important) for any distribution that uses ExtUtils::XSpp to wrap a C++ library for Perl. Most notably, this will fix recent versions of Wx.pm which uses the construct "INCLUDE: perl -M... -e". This may fail if the current perl is not the first in PATH. Thanks! The code is also on github at http://github.com/tsee/extutils-parsexs Steffen
Yes, attaching patches is a virtue. Sorry.
From f7c0709c49a8c425e75da68a88996818133c1d18 Mon Sep 17 00:00:00 2001 From: Steffen Mueller <smueller@cpan.org> Date: Thu, 17 Dec 2009 20:44:53 +0100 Subject: [PATCH] Implement INCLUDE_COMMAND directive Also adds a deprecation warning for "INCLUDE: command |". --- lib/ExtUtils/ParseXS.pm | 81 ++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 66 insertions(+), 15 deletions(-) diff --git a/lib/ExtUtils/ParseXS.pm b/lib/ExtUtils/ParseXS.pm index 83441fe..3e783f3 100644 --- a/lib/ExtUtils/ParseXS.pm +++ b/lib/ExtUtils/ParseXS.pm @@ -230,9 +230,10 @@ sub process_file { # Match an XS keyword $BLOCK_re= '\s*(' . join('|', qw( - REQUIRE BOOT CASE PREINIT INPUT INIT CODE PPCODE OUTPUT - CLEANUP ALIAS ATTRS PROTOTYPES PROTOTYPE VERSIONCHECK INCLUDE - SCOPE INTERFACE INTERFACE_MACRO C_ARGS POSTCALL OVERLOAD FALLBACK + REQUIRE BOOT CASE PREINIT INPUT INIT CODE PPCODE + OUTPUT CLEANUP ALIAS ATTRS PROTOTYPES PROTOTYPE + VERSIONCHECK INCLUDE INCLUDE_COMMAND SCOPE INTERFACE + INTERFACE_MACRO C_ARGS POSTCALL OVERLOAD FALLBACK )) . "|$END)\\s*:"; @@ -448,7 +449,7 @@ EOF $xsreturn = 0; $_ = shift(@line); - while (my $kwd = check_keyword("REQUIRE|PROTOTYPES|FALLBACK|VERSIONCHECK|INCLUDE|SCOPE")) { + while (my $kwd = check_keyword("REQUIRE|PROTOTYPES|FALLBACK|VERSIONCHECK|INCLUDE(?:_COMMAND)?|SCOPE")) { &{"${kwd}_handler"}() ; next PARAGRAPH unless @line ; $_ = shift(@line); @@ -1481,6 +1482,22 @@ sub PROTOTYPES_handler () } +sub PushXSStack + { + # Save the current file context. + push(@XSStack, { + type => 'file', + LastLine => $lastline, + LastLineNo => $lastline_no, + Line => \@line, + LineNo => \@line_no, + Filename => $filename, + Filepathname => $filepathname, + Handle => $FH, + }) ; + + } + sub INCLUDE_handler () { # the rest of the current line should contain a valid filename @@ -1499,17 +1516,11 @@ sub INCLUDE_handler () ++ $IncludedFiles{$_} unless /\|\s*$/ ; - # Save the current file context. - push(@XSStack, { - type => 'file', - LastLine => $lastline, - LastLineNo => $lastline_no, - Line => \@line, - LineNo => \@line_no, - Filename => $filename, - Filepathname => $filepathname, - Handle => $FH, - }) ; + Warn("The INCLUDE directive with a command is deprecated." . + " Use INCLUDE_COMMAND instead!") + if /\|\s*$/ ; + + PushXSStack(); $FH = Symbol::gensym(); @@ -1535,7 +1546,47 @@ EOF $lastline = $_ ; $lastline_no = $. ; + } + +sub INCLUDE_COMMAND_handler () + { + # the rest of the current line should contain a valid command + TrimWhitespace($_) ; + + death("INCLUDE_COMMAND: command missing") + unless $_ ; + + death("INCLUDE_COMMAND: pipes are illegal") + if /^\s*\|/ or /\|\s*$/ ; + + $FH = Symbol::gensym(); + + PushXSStack(); + + # open the new file + open ($FH, "-|", "$_") + or death("Cannot run command '$_' to include its output: $!") ; + + print Q(<<"EOF"); +# +#/* INCLUDE_COMMAND: Including output of '$_' from '$filename' */ +# +EOF + + $filename = $_ ; + $filepathname = "$dir/$filename"; + + # Prime the pump by reading the first + # non-blank line + + # skip leading blank lines + while (<$FH>) { + last unless /^\s*$/ ; + } + + $lastline = $_ ; + $lastline_no = $. ; } sub PopFile() -- 1.6.0.4
From 97cc78cdacf9d3af1a82e2fb85cc2f1c06e6106a Mon Sep 17 00:00:00 2001 From: Steffen Mueller <smueller@cpan.org> Date: Thu, 17 Dec 2009 21:01:18 +0100 Subject: [PATCH] Recognize INCLUDE_COMMAND: $^X -e ... In order to give the users a way to say "Run with the current perl interpreter" as opposed to "INCLUDE_COMMAND: perl -e ..." which runs the perl from the path. --- lib/ExtUtils/ParseXS.pm | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-) diff --git a/lib/ExtUtils/ParseXS.pm b/lib/ExtUtils/ParseXS.pm index 3e783f3..2d17446 100644 --- a/lib/ExtUtils/ParseXS.pm +++ b/lib/ExtUtils/ParseXS.pm @@ -1564,6 +1564,10 @@ sub INCLUDE_COMMAND_handler () PushXSStack(); + # If $^X is used in INCLUDE_COMMAND, we know it's supposed to be + # the same perl interpreter as we're currently running + s/^\s*\$\^X/$^X/; + # open the new file open ($FH, "-|", "$_") or death("Cannot run command '$_' to include its output: $!") ; -- 1.6.0.4
This implementation does not detect when the command exits with a non-zero exit code. Since the command does not end with a pipe, $isPipe is not set in PopFile and $? is not checked. A quick and dirty fix is to add a dummy '|' to $filename in INCLUDE_COMMAND_handler.
Hi Mattia, On Sun Jul 04 09:06:16 2010, MBARBON wrote: Show quoted text
> This implementation does not detect when the command exits with a non- > zero exit code. > > Since the command does not end with a pipe, $isPipe is not set in > PopFile and $? is not checked. > > A quick and dirty fix is to add a dummy '|' to $filename in > INCLUDE_COMMAND_handler.
... oops. Sorry about that. I think a slightly better fix is to tell PushXSStack that it's a pipe. It's done as: http://github.com/tsee/extutils-parsexs/commit/288ce7408b2abb7a85d42995a1d33e7576f69fc2 Does that address the issue alright or am I being Sunday-brain-damaged? David, is this good to go in? Cheers, Steffen